如何使用现有的DL4J训练模型对新输入进行分类

时间:2019-02-14 05:09:00

标签: dl4j

我有一个DL4J LSTM模型,该模型生成顺序输入的二进制分类。我已经训练和测试了模型,并对精度/召回率感到满意。现在,我想使用此模型来预测新输入的二进制分类。我该怎么做呢?即如何给受过训练的神经网络一个输入(包含特征行序列的文件)并获得该输入文件的二进制分类。

这是我原始的训练数据集迭代器:

        SequenceRecordReader trainFeatures = new CSVSequenceRecordReader(0, ",");  //skip no header lines
    try {
        trainFeatures.initialize( new NumberedFileInputSplit(featureBaseDir + "/s_%d.csv", 0,this._modelDefinition.getNB_TRAIN_EXAMPLES()-1));
    } catch (IOException e) {
        trainFeatures.close();
        throw new IOException(String.format("IO error %s. during trainFeatures", e.getMessage()));
    } catch (InterruptedException e) {
        trainFeatures.close();
        throw new IOException(String.format("Interrupted exception error %s. during trainFeatures", e.getMessage()));
    }

    SequenceRecordReader trainLabels = new CSVSequenceRecordReader();
    try {
        trainLabels.initialize(new NumberedFileInputSplit(labelBaseDir + "/s_%d.csv", 0,this._modelDefinition.getNB_TRAIN_EXAMPLES()-1));
    } catch (InterruptedException e) {
        trainLabels.close();
        trainFeatures.close();
        throw new IOException(String.format("Interrupted exception error %s. during trainLabels initialise", e.getMessage()));
    }



    DataSetIterator trainData = new SequenceRecordReaderDataSetIterator(trainFeatures, trainLabels,
            this._modelDefinition.getBATCH_SIZE(),this._modelDefinition.getNUM_LABEL_CLASSES(), false, SequenceRecordReaderDataSetIterator.AlignmentMode.ALIGN_END);

这是我的模特:

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .seed(this._modelDefinition.getRANDOM_SEED())    //Random number generator seed for improved repeatability. Optional.
            .weightInit(WeightInit.XAVIER)
            .updater(new Nesterovs(this._modelDefinition.getLEARNING_RATE()))
            .gradientNormalization(GradientNormalization.ClipElementWiseAbsoluteValue)  //Not always required, but helps with this data set
            .gradientNormalizationThreshold(0.5)
            .list()
            .layer(0, new LSTM.Builder().activation(Activation.TANH).nIn(this._modelDefinition.getNB_INPUTS()).nOut(this._modelDefinition.getLSTM_LAYER_SIZE()).build())
            .layer(1, new LSTM.Builder().activation(Activation.TANH).nIn(this._modelDefinition.getLSTM_LAYER_SIZE()).nOut(this._modelDefinition.getLSTM_LAYER_SIZE()).build())
            .layer(2,new DenseLayer.Builder().nIn(this._modelDefinition.getLSTM_LAYER_SIZE()).nOut(this._modelDefinition.getLSTM_LAYER_SIZE())
                    .weightInit(WeightInit.XAVIER)
                    .build())
            .layer(3, new RnnOutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                    .activation(Activation.SOFTMAX).nIn(this._modelDefinition.getLSTM_LAYER_SIZE()).nOut(this._modelDefinition.getNUM_LABEL_CLASSES()).build())
            .pretrain(false).backprop(true).build();

我在N个时期内训练模型以获得最佳分数。我保存了模型,现在我想打开模型并获取新的顺序特征文件的分类。

如果有这样的例子-请告诉我在哪里。

谢谢

anton

1 个答案:

答案 0 :(得分:0)

答案是为模型提供与我们训练时完全相同的输入,除了将标签设置为-1。输出将是一个INDarray,它在一个数组中包含0的概率,在另一个数组中包含1的概率,显示在最后一个序列行中。

代码如下:

w

// [[[0,0,0,0,0.9882,0,0,0,0], // [0,0,0,0,0.0118,0,0,0,0]], // // [[0,0.1443,0,0,0,0,0,0,0], // [0,0.8557,0,0,0,0,0,0,0]], // // [[0,0,0,0,0,0,0,0,0.9975], // [0,0,0,0,0,0,0,0,0.0025]], // // [[0,0,0,0,0,0,0.8482,0,0], // [0,0,0,0,0,0,0.1518,0,0]], // // [[0,0,0,0.8760,0,0,0,0,0], // [0,0,0,0.1240,0,0,0,0,0]]]

griddata