Search in sources :

Example 36 with INDArray

use of org.nd4j.linalg.api.ndarray.INDArray in project deeplearning4j by deeplearning4j.

the class EvalTest method testEvalMasking.

@Test
public void testEvalMasking() {
    int miniBatch = 5;
    int nOut = 3;
    int tsLength = 6;
    INDArray labels = Nd4j.zeros(miniBatch, nOut, tsLength);
    INDArray predicted = Nd4j.zeros(miniBatch, nOut, tsLength);
    Nd4j.getRandom().setSeed(12345);
    Random r = new Random(12345);
    for (int i = 0; i < miniBatch; i++) {
        for (int j = 0; j < tsLength; j++) {
            INDArray rand = Nd4j.rand(1, nOut);
            rand.divi(rand.sumNumber());
            predicted.put(new INDArrayIndex[] { NDArrayIndex.point(i), NDArrayIndex.all(), NDArrayIndex.point(j) }, rand);
            int idx = r.nextInt(nOut);
            labels.putScalar(new int[] { i, idx, j }, 1.0);
        }
    }
    //Create a longer labels/predicted with mask for first and last time step
    //Expect masked evaluation to be identical to original evaluation
    INDArray labels2 = Nd4j.zeros(miniBatch, nOut, tsLength + 2);
    labels2.put(new INDArrayIndex[] { NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(1, tsLength + 1) }, labels);
    INDArray predicted2 = Nd4j.zeros(miniBatch, nOut, tsLength + 2);
    predicted2.put(new INDArrayIndex[] { NDArrayIndex.all(), NDArrayIndex.all(), NDArrayIndex.interval(1, tsLength + 1) }, predicted);
    INDArray labelsMask = Nd4j.ones(miniBatch, tsLength + 2);
    for (int i = 0; i < miniBatch; i++) {
        labelsMask.putScalar(new int[] { i, 0 }, 0.0);
        labelsMask.putScalar(new int[] { i, tsLength + 1 }, 0.0);
    }
    Evaluation evaluation = new Evaluation();
    evaluation.evalTimeSeries(labels, predicted);
    Evaluation evaluation2 = new Evaluation();
    evaluation2.evalTimeSeries(labels2, predicted2, labelsMask);
    System.out.println(evaluation.stats());
    System.out.println(evaluation2.stats());
    assertEquals(evaluation.accuracy(), evaluation2.accuracy(), 1e-12);
    assertEquals(evaluation.f1(), evaluation2.f1(), 1e-12);
    assertMapEquals(evaluation.falsePositives(), evaluation2.falsePositives());
    assertMapEquals(evaluation.falseNegatives(), evaluation2.falseNegatives());
    assertMapEquals(evaluation.truePositives(), evaluation2.truePositives());
    assertMapEquals(evaluation.trueNegatives(), evaluation2.trueNegatives());
    for (int i = 0; i < nOut; i++) assertEquals(evaluation.classCount(i), evaluation2.classCount(i));
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Example 37 with INDArray

use of org.nd4j.linalg.api.ndarray.INDArray in project deeplearning4j by deeplearning4j.

the class EvalTest method testEval.

@Test
public void testEval() {
    int classNum = 5;
    Evaluation eval = new Evaluation(classNum);
    // Testing the edge case when some classes do not have true positive
    //[1,0,0,0,0]
    INDArray trueOutcome = FeatureUtil.toOutcomeVector(0, 5);
    //[1,0,0,0,0]
    INDArray predictedOutcome = FeatureUtil.toOutcomeVector(0, 5);
    eval.eval(trueOutcome, predictedOutcome);
    assertEquals(1, eval.classCount(0));
    assertEquals(1.0, eval.f1(), 1e-1);
    // Testing more than one sample. eval() does not reset the Evaluation instance
    //[0,1,0,0,0]
    INDArray trueOutcome2 = FeatureUtil.toOutcomeVector(1, 5);
    //[1,0,0,0,0]
    INDArray predictedOutcome2 = FeatureUtil.toOutcomeVector(0, 5);
    eval.eval(trueOutcome2, predictedOutcome2);
    // Verified with sklearn in Python
    // from sklearn.metrics import classification_report
    // classification_report(['a', 'a'], ['a', 'b'], labels=['a', 'b', 'c', 'd', 'e'])
    assertEquals(eval.f1(), 0.6, 1e-1);
    // The first entry is 0 label
    assertEquals(1, eval.classCount(0));
    // The first entry is 1 label
    assertEquals(1, eval.classCount(1));
    // Class 0: one positive, one negative -> (one true positive, one false positive); no true/false negatives
    assertEquals(1, eval.positive().get(0), 0);
    assertEquals(1, eval.negative().get(0), 0);
    assertEquals(1, eval.truePositives().get(0), 0);
    assertEquals(1, eval.falsePositives().get(0), 0);
    assertEquals(0, eval.trueNegatives().get(0), 0);
    assertEquals(0, eval.falseNegatives().get(0), 0);
    // The rest are negative
    assertEquals(1, eval.negative().get(0), 0);
    // 2 rows and only the first is correct
    assertEquals(0.5, eval.accuracy(), 0);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Example 38 with INDArray

use of org.nd4j.linalg.api.ndarray.INDArray in project deeplearning4j by deeplearning4j.

the class EvaluationToolsTests method testRocHtml.

@Test
public void testRocHtml() throws Exception {
    DataSetIterator iter = new IrisDataSetIterator(150, 150);
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).list().layer(0, new DenseLayer.Builder().nIn(4).nOut(4).activation(Activation.TANH).build()).layer(1, new OutputLayer.Builder().nIn(4).nOut(2).activation(Activation.SOFTMAX).lossFunction(LossFunctions.LossFunction.MCXENT).build()).build();
    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.init();
    NormalizerStandardize ns = new NormalizerStandardize();
    DataSet ds = iter.next();
    ns.fit(ds);
    ns.transform(ds);
    INDArray newLabels = Nd4j.create(150, 2);
    newLabels.getColumn(0).assign(ds.getLabels().getColumn(0));
    newLabels.getColumn(0).addi(ds.getLabels().getColumn(1));
    newLabels.getColumn(1).assign(ds.getLabels().getColumn(2));
    ds.setLabels(newLabels);
    for (int i = 0; i < 30; i++) {
        net.fit(ds);
    }
    ROC roc = new ROC(20);
    iter.reset();
    INDArray f = ds.getFeatures();
    INDArray l = ds.getLabels();
    INDArray out = net.output(f);
    roc.eval(l, out);
    String str = EvaluationTools.rocChartToHtml(roc);
//        System.out.println(str);
}
Also used : OutputLayer(org.deeplearning4j.nn.conf.layers.OutputLayer) IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSet(org.nd4j.linalg.dataset.api.DataSet) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) DenseLayer(org.deeplearning4j.nn.conf.layers.DenseLayer) INDArray(org.nd4j.linalg.api.ndarray.INDArray) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) NormalizerStandardize(org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize) IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) Test(org.junit.Test)

Example 39 with INDArray

use of org.nd4j.linalg.api.ndarray.INDArray in project deeplearning4j by deeplearning4j.

the class ROCTest method testRocBasic.

@Test
public void testRocBasic() {
    //2 outputs here - probability distribution over classes (softmax)
    INDArray predictions = Nd4j.create(new double[][] { //add 0.001 to avoid numerical/rounding issues (float vs. double, etc)
    { 1.0, 0.001 }, { 0.899, 0.101 }, { 0.799, 0.201 }, { 0.699, 0.301 }, { 0.599, 0.401 }, { 0.499, 0.501 }, { 0.399, 0.601 }, { 0.299, 0.701 }, { 0.199, 0.801 }, { 0.099, 0.901 } });
    INDArray actual = Nd4j.create(new double[][] { { 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 0 }, { 1, 0 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 }, { 0, 1 } });
    ROC roc = new ROC(10);
    roc.eval(actual, predictions);
    List<ROC.ROCValue> list = roc.getResults();
    //0 + 10 steps
    assertEquals(11, list.size());
    for (int i = 0; i < 11; i++) {
        ROC.ROCValue v = list.get(i);
        double expThreshold = i / 10.0;
        assertEquals(expThreshold, v.getThreshold(), 1e-5);
        //            System.out.println("t=" + expThreshold + "\t" + v.getFalsePositiveRate() + "\t" + v.getTruePositiveRate());
        double efpr = expFPR.get(expThreshold);
        double afpr = v.getFalsePositiveRate();
        assertEquals(efpr, afpr, 1e-5);
        double etpr = expTPR.get(expThreshold);
        double atpr = v.getTruePositiveRate();
        assertEquals(etpr, atpr, 1e-5);
    }
    //Expect AUC == 1.0 here
    double auc = roc.calculateAUC();
    assertEquals(1.0, auc, 1e-6);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Example 40 with INDArray

use of org.nd4j.linalg.api.ndarray.INDArray in project deeplearning4j by deeplearning4j.

the class RecordReaderMultiDataSetIteratorTest method testSplittingCSV.

@Test
public void testSplittingCSV() throws Exception {
    //Here's the idea: take Iris, and split it up into 2 inputs and 2 output arrays
    //Inputs: columns 0 and 1-2
    //Outputs: columns 3, and 4->OneHot
    //need to manually extract
    RecordReader rr = new CSVRecordReader(0, ",");
    rr.initialize(new FileSplit(new ClassPathResource("iris.txt").getTempFileFromArchive()));
    RecordReaderDataSetIterator rrdsi = new RecordReaderDataSetIterator(rr, 10, 4, 3);
    RecordReader rr2 = new CSVRecordReader(0, ",");
    rr2.initialize(new FileSplit(new ClassPathResource("iris.txt").getTempFileFromArchive()));
    MultiDataSetIterator rrmdsi = new RecordReaderMultiDataSetIterator.Builder(10).addReader("reader", rr2).addInput("reader", 0, 0).addInput("reader", 1, 2).addOutput("reader", 3, 3).addOutputOneHot("reader", 4, 3).build();
    while (rrdsi.hasNext()) {
        DataSet ds = rrdsi.next();
        INDArray fds = ds.getFeatureMatrix();
        INDArray lds = ds.getLabels();
        MultiDataSet mds = rrmdsi.next();
        assertEquals(2, mds.getFeatures().length);
        assertEquals(2, mds.getLabels().length);
        assertNull(mds.getFeaturesMaskArrays());
        assertNull(mds.getLabelsMaskArrays());
        INDArray[] fmds = mds.getFeatures();
        INDArray[] lmds = mds.getLabels();
        assertNotNull(fmds);
        assertNotNull(lmds);
        for (int i = 0; i < fmds.length; i++) assertNotNull(fmds[i]);
        for (int i = 0; i < lmds.length; i++) assertNotNull(lmds[i]);
        //Get the subsets of the original iris data
        INDArray expIn1 = fds.get(NDArrayIndex.all(), NDArrayIndex.point(0));
        INDArray expIn2 = fds.get(NDArrayIndex.all(), NDArrayIndex.interval(1, 2, true));
        INDArray expOut1 = fds.get(NDArrayIndex.all(), NDArrayIndex.point(3));
        INDArray expOut2 = lds;
        assertEquals(expIn1, fmds[0]);
        assertEquals(expIn2, fmds[1]);
        assertEquals(expOut1, lmds[0]);
        assertEquals(expOut2, lmds[1]);
    }
    assertFalse(rrmdsi.hasNext());
}
Also used : MultiDataSetIterator(org.nd4j.linalg.dataset.api.iterator.MultiDataSetIterator) INDArray(org.nd4j.linalg.api.ndarray.INDArray) MultiDataSet(org.nd4j.linalg.dataset.api.MultiDataSet) DataSet(org.nd4j.linalg.dataset.DataSet) MultiDataSet(org.nd4j.linalg.dataset.api.MultiDataSet) RecordReader(org.datavec.api.records.reader.RecordReader) ImageRecordReader(org.datavec.image.recordreader.ImageRecordReader) CSVSequenceRecordReader(org.datavec.api.records.reader.impl.csv.CSVSequenceRecordReader) CSVRecordReader(org.datavec.api.records.reader.impl.csv.CSVRecordReader) SequenceRecordReader(org.datavec.api.records.reader.SequenceRecordReader) CSVRecordReader(org.datavec.api.records.reader.impl.csv.CSVRecordReader) FileSplit(org.datavec.api.split.FileSplit) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) Test(org.junit.Test)

Aggregations

INDArray (org.nd4j.linalg.api.ndarray.INDArray)1034 Test (org.junit.Test)453 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)173 DataSet (org.nd4j.linalg.dataset.DataSet)171 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)166 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)143 Gradient (org.deeplearning4j.nn.gradient.Gradient)100 Layer (org.deeplearning4j.nn.api.Layer)82 NormalDistribution (org.deeplearning4j.nn.conf.distribution.NormalDistribution)77 OutputLayer (org.deeplearning4j.nn.conf.layers.OutputLayer)69 DefaultGradient (org.deeplearning4j.nn.gradient.DefaultGradient)68 File (java.io.File)67 DenseLayer (org.deeplearning4j.nn.conf.layers.DenseLayer)66 ArrayList (java.util.ArrayList)65 ComputationGraph (org.deeplearning4j.nn.graph.ComputationGraph)62 DataSetIterator (org.nd4j.linalg.dataset.api.iterator.DataSetIterator)62 Pair (org.deeplearning4j.berkeley.Pair)56 Random (java.util.Random)54 ComputationGraphConfiguration (org.deeplearning4j.nn.conf.ComputationGraphConfiguration)53 IrisDataSetIterator (org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator)44