我将使用 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]
...........
数据向量部分:
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,将不胜感激。我在 Arbiter 上工作,但没有运气。不确定我是否使用的是 0.9.1 稳定版本。
谢谢十亿!