1

" I'm new in neural networks and DL4j, and I want to train neural network with CSV and build linear regression. How can I fix these errors "Cannot resolve method'.iterations and getFeatureMatrix()'"?


"Previously I'm tried to do that, but have another error in 'seed'".

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.evaluation.classification.Evaluation;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.api.DataSet;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.lossfunctions.LossFunctions;
import java.io.File;






public class Data {
    public static void main(String[] args) throws Exception {

Parameters:

        int seed = 3000;
        int batchSize = 200;
        double learningRate = 0.001;
        int nEpochs = 150;
        int numInputs = 2;
        int numOutputs = 2;
        int numHiddenNodes = 100;

Load data:

        //load data train
        RecordReader rr = new CSVRecordReader();
        rr.initialize(new FileSplit(new File("train.csv")));
        DataSetIterator trainIter = new RecordReaderDataSetIterator(rr, batchSize, 0, 2);

        //load test data

        RecordReader rrTest = new CSVRecordReader();
        rr.initialize(new FileSplit(new File("test.csv")));


        DataSetIterator testIter = new RecordReaderDataSetIterator(rrTest, batchSize, 0, 2);

Network Configuration:

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                .seed(seed)
                .iterations(1000)
                .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                .learningRate(learningRate)
                .updater(Updater.NESTEROVS).momentum(0.9)
                .list()
                .layer(0, new DenseLayer.Builder()
                        .nIn(numInputs)
                        .nOut(numHiddenNodes)
                        .weightInit(WeightInit.XAVIER)
                        .activation(Activation.fromString("relu"))
                        .build())
                .layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                        .weightInit(WeightInit.XAVIER)
                        .activation(Activation.fromString("softmax"))
                        .weightInit(WeightInit.XAVIER)
                        .nIn(numHiddenNodes)
                        .nOut(numOutputs)
                        .build()
                )
                .pretrain(false).backprop(true).build();

        MultiLayerNetwork model = new MultiLayerNetwork(conf);
        model.init();
        model.setListeners(new ScoreIterationListener((15)));
        for (int n = 0; n < nEpochs; n++) {
            model.fit((trainIter));
            System.out.println(("--------------eval model"));
            Evaluation eval = new Evaluation(numOutputs);
            while (testIter.hasNext()) {
                DataSet t = testIter.next();
                INDArray features = getFeatureMatrix();
                INDArray lables = t.getLabels();
                INDArray predicted = model.output(features, false);
                eval.eval(lables, predicted);
            }
            System.out.println(eval.stats());
        }
    }
}

Logs

Build

Lemair
  • 45
  • 5

1 Answers1

1

First you should consider to use more class (like one for the definition of the neural network, one for the training process etc, ...). Just a best practice stuff.

I do not know which version of DL4J you're using but we can notice that getFeatureMatrix() has been removed. One more thing is that this function should be called on a DataSet object and not "statically" like you seem to do. (you should do t.getFeatureMatrix()).

It is pretty same things about iterations() function of the neural network creation; This function has been removed since some DL4J releases. You can get more information about this function on this thread. Now you have to find an alternative to set up number of iteration, you can take a look at this thread. Hope it is answering your question !

bastien enjalbert
  • 318
  • 1
  • 3
  • 13