0

我是第一次使用 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 无法在 MLPClassifierLinear.main(MLPClassifierLinear) 处强制转换为 java.util.Collection .java:71)"

这是怎么回事?任何有 dl4j 经验的人都可以帮我解决这个问题吗?

4

1 回答 1

0

我将从我们的示例开始,并确保您使用的是最新版本(撰写本文时为 0.7.2): https ://github.com/deeplearning4j/dl4j-examples

MultiLayerNetwork.setListeners 在 IterationListener 上有可变参数: https ://github.com/deeplearning4j/deeplearning4j/blob/master/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/multilayer/MultiLayerNetwork.java#L1232

我感觉真正的根本原因可能是别的什么?

于 2017-02-19T10:40:08.770 回答