Deeplearning4j(DL4J)低精度,召回率和F1

时间:2018-10-18 13:20:07

标签: java deep-learning deeplearning4j dl4j

我将使用DL4J针对条件矩阵找到一个好的模型。我已经准备好了类似CSV的数据集(如下所示的样本),并且在对超参数进行了微调并对模型进行了多次训练之后,我仍然无法获得合理的Precision,Recall和F1结果。请问我是否实施了错误的操作?

样本数据集:

##基本上,每列定义每个样本是否存在条件(1)(0)。第一列是标签类,只有2个输出,即1/0

[1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0]
...........

DataVec部分:

int OUTPUT_NEURONS = 2;  // Only 2 classes for output
int CLASS_INDEX = 0;     // First column is the label
int FILE_SIZE = 0;       // FILE_SIZE will be calculated while preparing the datavecRecords below

List<List<Writable>> datavecRecords = new ArrayList<>();

......
Prepare the datavecRecords using above csv data 
......

CollectionRecordReader crr = new CollectionRecordReader(datavecRecords);
RecordReaderDataSetIterator iter = new RecordReaderDataSetIterator(crr, FILE_SIZE, CLASS_INDEX, OUTPUT_NEURONS);
allData = iter.next();

SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.6);
DataSet trainingData = testAndTrain.getTrain();
DataSet testData = testAndTrain.getTest();

DataNormalization normalizer = new NormalizerStandardize();
normalizer.fit(trainingData);
normalizer.transform(trainingData);
normalizer.transform(testData);

// For early escaping use
DataSetIterator trainSetIterator = new ListDataSetIterator(trainingData.asList()); 
DataSetIterator testSetIterator = new ListDataSetIterator(testData.asList()); 

// sortedKeys is the calculated number of input columns

INPUT_NEURONS = sortedKeys.size() - 1; 
HIDDEN_NEURONS = FILE_SIZE / (2 * (INPUT_NEURONS + OUTPUT_NEURONS));
HIDDEN_NEURONS = HIDDEN_NEURONS <= 0 ? 1 : HIDDEN_NEURONS;

型号:

int n=0;
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
    .seed(12345)
    .iterations(1)
    .learningRate(0.001)
    .weightInit(WeightInit.XAVIER)
    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
    .regularization(true).l2(1e-4)
    .updater(new Nesterovs(0.001,0.9))
    .list()
        .layer(n++, new DenseLayer.Builder()
            .nIn(INPUT_NEURONS)
            .nOut(HIDDEN_NEURONS)
            .activation(Activation.RELU)
            .build())
        .layer(n++, new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD)
            .nIn(HIDDEN_NEURONS)
            .nOut(OUTPUT_NEURONS)
            .activation(Activation.SOFTMAX)
            .build())
    .pretrain(false).backprop(true).build();            

EarlyStoppingConfiguration esConf = new EarlyStoppingConfiguration.Builder()
    .epochTerminationConditions(
        new MaxEpochsTerminationCondition(10000), 
        new ScoreImprovementEpochTerminationCondition(50))
    .iterationTerminationConditions(new MaxTimeIterationTerminationCondition(5, TimeUnit.MINUTES))
    .scoreCalculator(new DataSetLossCalculator(testSetIterator, true))
    .evaluateEveryNEpochs(1)
    .modelSaver(saver)
    .build();

培训和测试代码

StatsStorage statsStorage = new InMemoryStatsStorage();
MultiLayerNetwork networkModel = new MultiLayerNetwork(conf);
networkModel.setListeners(new StatsListener(statsStorage), new ScoreIterationListener(10));


IEarlyStoppingTrainer trainer = new EarlyStoppingTrainer(esConf, networkModel, trainSetIterator);
EarlyStoppingResult<MultiLayerNetwork> result = trainer.fit();


// -------------------------- Evaluation trained model and print results --------------------------
System.out.println("Termination reason: " + result.getTerminationReason());
System.out.println("Termination details: " + result.getTerminationDetails());
System.out.println("Total epochs: " + result.getTotalEpochs());
System.out.println("Best epoch number: " + result.getBestModelEpoch());
System.out.println("Score at best epoch: " + result.getBestModelScore());

MultiLayerNetwork bestNetwork = result.getBestModel();
Evaluation eval1 = new Evaluation(OUTPUT_NEURONS);
testSetIterator.reset();

for (int i = 0; i < testData.numExamples(); i++) {
    DataSet t = testData.get(i);
    INDArray features = t.getFeatureMatrix();
    INDArray labels = t.getLabels();
    INDArray output = bestNetwork.output(features, false);
    eval1.eval(labels, output);
}

M.messageln(eval1.stats());

结果:

Termination reason: EpochTerminationCondition
Termination details: ScoreImprovementEpochTerminationCondition(maxEpochsWithNoImprovement=50, minImprovement=0.0)
Total epochs: 55
Best epoch number: 4
Score at best epoch: 0.6579822991097982

Examples labeled as 0 classified by model as 0: 397 times
Examples labeled as 0 classified by model as 1: 58 times
Examples labeled as 1 classified by model as 0: 190 times
Examples labeled as 1 classified by model as 1: 55 times


==========================Scores========================================
 # of classes:    2
 Accuracy:        0.6457
 Precision:       0.5815
 Recall:          0.5485
 F1 Score:        0.3073
========================================================================
Pattern1 :      Accuracy: 0.6457142857142857 | Precision: 0.5815229681446081 | Recall: 0.54850863422292 | F1: 0.3072625698324022

无论我如何调整学习率,输入和输出激活方法,更新程序,法规等,我仍然无法获得令人满意的结果。如果您能帮助我,如何更好地操作DL4J,将不胜感激。我正在研究仲裁器,但是没有运气。不确定我是否正在使用0.9.1稳定版本。

谢谢十亿!

2 个答案:

答案 0 :(得分:0)

您可以在以下用户社区中找到问题的答案:https://gitter.im/deeplearning4j/deeplearning4j/tuninghelp

答案 1 :(得分:0)

代码看起来不错。您在这里没有做错任何事情。我只需要调整它。

您进行的60/40测试/火车划分高于我通常看到的70/30或80/20。您是否有理由保留更多数据进行测试?

当您说合理时,​​您的意思是与基线相比?如果不是这样,我将仅从本质上是逻辑回归的OutputLayer开始,您可以将其用作基线。

您是否尝试过添加更多图层?还有一层带有辍学的层,可能会有用。