Search code examples
javamachine-learningneural-networkdeeplearning4j

Make DL4J classifier return score


I'm playing a bit with DeepLearning4J and I wonder how I can make a classifier return a score instead of a label. Suppose I use the code from the linear classifier tutorial, I'd like to make the ANN return the probabilities for a given training example to be labeled 0 or 1. The current configuration looks as follows:

MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
        .seed(123)
        .iterations(1)
        .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
        .learningRate(0.01)
        .updater(Updater.NESTEROVS)
        .momentum(0.9)
        .list()
        .layer(0, new DenseLayer.Builder()
                .nIn(2)
                .nOut(20)
                .weightInit(WeightInit.XAVIER)
                .activation(Activation.RELU)
                .build())
        .layer(1, new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD)
                .nIn(20)
                .nOut(2)
                .weightInit(WeightInit.XAVIER)
                .activation(Activation.SOFTMAX)
                .build())
        .pretrain(false)
        .backprop(true)
        .build();

Solution

  • Use model.output .

    You'll get back out an ndarray (http://nd4j.org/tensor)

    It uses softmax on the output which means you get back a batch size x number of labels output.