我第一次使用dl4j,所以对我很轻松。
我写了以下简单程序
import org.datavec.api.records.reader.RecordReader;
import org.datavec.api.records.reader.impl.csv.CSVRecordReader;
import org.datavec.api.split.FileSplit;
import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator;
import org.deeplearning4j.nn.api.OptimizationAlgorithm;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.Updater;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.weights.WeightInit;
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.lossfunctions.LossFunctions;
import org.deeplearning4j.eval.Evaluation;
import java.io.File;
import java.util.Collection;
public class MLPClassifierLinear
{
public static void main(String[] args) throws Exception
{
int seed = 123;
double learnRate = 0.01;
int batchSize = 50;
int nEpochs = 30;
int numInputs = 2;
int numOutputs = 2;
int numHiddenNodes = 20;
int labelField = 0;
int numOfLabels = 2;
//Load Training Data
RecordReader rr = new CSVRecordReader();
rr.initialize(new FileSplit(new File("C:\\Users\\Oria\\MLP\\linear_data_train.csv")));
DataSetIterator trainIter = new RecordReaderDataSetIterator(rr, batchSize,0,2);
//Load Testing Data
RecordReader rrTest = new CSVRecordReader();
rrTest.initialize(new FileSplit(new File("C:\\Users\\Oria\\MLP\\linear_data_eval.csv")));
DataSetIterator testIter = new RecordReaderDataSetIterator(rrTest, batchSize,0,2);
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.seed(seed)
.iterations(1)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.learningRate(learnRate)
.updater(Updater.NESTEROVS).momentum(0.9)
.list()
.layer(0, new DenseLayer.Builder()
.nIn(numInputs)
.nOut(numHiddenNodes)
.weightInit(WeightInit.XAVIER)
.activation("relu")
.build())
.layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.weightInit(WeightInit.XAVIER)
.activation("softmax")
.nIn(numHiddenNodes)
.nOut(numOutputs)
.build())
.pretrain(false).backprop(true).build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
model.setListeners(new ScoreIterationListener(10));
for(int i = 0; i < nEpochs; i++)
model.fit(trainIter);
System.out.println("Evaluate model.......");
Evaluation eval = new Evaluation(numOutputs);
while(testIter.hasNext())
{
DataSet t = testIter.next();
INDArray features = t.getFeatureMatrix();
INDArray lables = t.getLabels();
INDArray predicted = model.output(features,false);
eval.eval(lables,predicted);
}
System.out.println(eval.stats());
}
}
代码应该正常工作。它是https://www.youtube.com/watch?v=8EIBIfVlgmU&t=1063s的副本,这是dl4j上的一个众所周知的教程。
但是,代码无法编译。我在model.SetListeners行上遇到错误,“MultiLayerNetwork类型中的方法setListeners(Collection)不适用于参数(ScoreIterationListener)”
当我将其更改为“model.setListeners((Collection)new ScoreIterationListener(10));”编译错误消失,但我得到一个运行时错误“线程中的异常”主“java.lang.ClassCastException:org.deeplearning4j.optimize.listeners.ScoreIterationListener无法强制转换为java.util.Collection 在MLPClassifierLinear.main(MLPClassifierLinear.java:71)“
发生了什么事?有经验的dl4j可以帮我解决这个问题吗?
答案 0 :(得分:0)
我将从我们的示例开始,确保您使用的是最新版本(撰写本文时为0.7.2): https://github.com/deeplearning4j/dl4j-examples
MultiLayerNetwork.setListeners在IterationListener上有变量args: https://github.com/deeplearning4j/deeplearning4j/blob/master/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/multilayer/MultiLayerNetwork.java#L1232
我觉得真正的根本原因可能是别的什么?