Search code examples
javaneural-networkdeeplearning4j

"Invalid classification data: expect label value"


I'm trying to train a model using deep learning in java, when I start training the train data it gives an error

Invalid classification data: expect label value (at label index column = 0) to be in range 0 to 1 inclusive (0 to numClasses-1, with numClasses=2); got label value of 2

I didn't understand the error since I am a beginner in deep learning 4j. I am using a data set which views relationship between two people (if there is a relationship between two people then the class label is going to be 1 otherwise 0).

The Java code

public class SNA {
private static Logger log = LoggerFactory.getLogger(SNA.class);

public static void main(String[] args) throws Exception {
    int seed = 123;
    double learningRate = 0.01;
    int batchSize = 50;
    int nEpochs = 30;
    int numInputs = 2;
    int numOutputs = 2;
    int numHiddenNodes = 20;

    //load the training data
    RecordReader rr = new CSVRecordReader(0,",");
    rr.initialize(new FileSplit(new File("C:\\Users\\GTS\\Desktop\\SNA project\\experiments\\First experiment\\train\\slashdotTrain.csv")));
    DataSetIterator trainIter = new RecordReaderDataSetIterator(rr, batchSize,0, 2);

    // load test data
    RecordReader rrTest = new CSVRecordReader();
    rr.initialize(new FileSplit(new File("C:\\Users\\GTS\\Desktop\\SNA project\\experiments\\First experiment\\test\\slashdotTest.csv")));
    DataSetIterator testIter = new RecordReaderDataSetIterator(rrTest, batchSize,0, 2);

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

    MultiLayerNetwork model = new MultiLayerNetwork(conf);
    model.init();

    // Listener to show how the network is training in the log
    model.setListeners(new ScoreIterationListener(10));

    log.info(" **** Train Model **** ");
    for (int i = 0; i < nEpochs; i++) {
        model.fit(trainIter);
    }

    System.out.println("**** Evaluate Model ****");
    Evaluation evaluation = new Evaluation(numOutputs);
    while (testIter.hasNext()) {
        DataSet t = testIter.next();
        INDArray feature = t.getFeatureMatrix();
        INDArray labels = t.getLabels();
        INDArray predicted = model.output(feature, false);
        evaluation.eval(labels, predicted);
    }

    System.out.println(evaluation.stats());
}

}

Any help Please? Thanks A lot


Solution

  • problem solved: Change the third parameter of RecordReaderDataSetIterator in
    DataSetIterator testIter = new RecordReaderDataSetIterator(rrTest, batchSize,0, 2); from 0 to 2; because the data set has three columns and the index of the class label is 2 because its the third column.

    solution:

    DataSetIterator trainIter = new RecordReaderDataSetIterator(rr, batchSize,2, 2);
    

    refrences: enter link description here