0

I am first time working with dl4j, so go easy on me.

I wrote the following simple program

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());
    }
}

The code should be working fine. It's a copy of https://www.youtube.com/watch?v=8EIBIfVlgmU&t=1063s which is a well known tutorial on dl4j.

However, the code does not compile. I get an error on the model.SetListeners line, "The method setListeners(Collection) in the type MultiLayerNetwork is not applicable for the arguments (ScoreIterationListener)"

When I change it to "model.setListeners((Collection) new ScoreIterationListener(10));" the compilation error goes away, but I instead get a runtime error "Exception in thread "main" java.lang.ClassCastException: org.deeplearning4j.optimize.listeners.ScoreIterationListener cannot be cast to java.util.Collection at MLPClassifierLinear.main(MLPClassifierLinear.java:71)"

What's going on? Anyone experienced with dl4j can help me solve this issue?

Oria Gruber
  • 1,513
  • 2
  • 22
  • 44

1 Answers1

0

I would start from our examples and ensure that you are using the latest version (0.7.2 as of this writing): https://github.com/deeplearning4j/dl4j-examples

MultiLayerNetwork.setListeners has variable args on the IterationListener: https://github.com/deeplearning4j/deeplearning4j/blob/master/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/multilayer/MultiLayerNetwork.java#L1232

I'm getting the feeling the real root cause might be something else?

Adam Gibson
  • 3,055
  • 1
  • 10
  • 12
  • Could be something else. I've tinkered around with it and I'm now getting java.lang.ClassNotFoundException: org.bytedeco.javacpp.openblas exception. guess theres nothing wrong with my dl4j installation but rather something else. – Oria Gruber Feb 19 '17 at 13:08
  • The CSVExample doesn't work. Also says " java.lang.ClassNotFoundException: org.bytedeco.javacpp.openblas" – Oria Gruber Feb 19 '17 at 13:28
  • Could you file an issue? I'm obviously missing something here: https://github.com/deeplearning4j/dl4j-examples/issues We aren't running in to this in our gitter channel. That example works fine. – Adam Gibson Feb 22 '17 at 07:00