Search in sources :

Example 81 with DataSet

use of org.nd4j.linalg.dataset.DataSet in project deeplearning4j by deeplearning4j.

the class BatchNormalizationTest method checkMeanVarianceEstimateCNN.

@Test
public void checkMeanVarianceEstimateCNN() throws Exception {
    Nd4j.getRandom().setSeed(12345);
    //Check that the internal global mean/variance estimate is approximately correct
    //First, Mnist data as 2d input (NOT taking into account convolution property)
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).updater(Updater.RMSPROP).seed(12345).list().layer(0, new BatchNormalization.Builder().nIn(3).nOut(3).eps(1e-5).decay(0.95).build()).layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MSE).weightInit(WeightInit.XAVIER).activation(Activation.IDENTITY).nOut(10).build()).backprop(true).pretrain(false).setInputType(InputType.convolutional(5, 5, 3)).build();
    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.init();
    int minibatch = 32;
    List<DataSet> list = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        list.add(new DataSet(Nd4j.rand(new int[] { minibatch, 3, 5, 5 }), Nd4j.rand(minibatch, 10)));
    }
    DataSetIterator iter = new ListDataSetIterator(list);
    INDArray expMean = Nd4j.valueArrayOf(new int[] { 1, 3 }, 0.5);
    //Expected variance of U(0,1) distribution: 1/12 * (1-0)^2 = 0.0833
    INDArray expVar = Nd4j.valueArrayOf(new int[] { 1, 3 }, 1 / 12.0);
    for (int i = 0; i < 10; i++) {
        iter.reset();
        net.fit(iter);
    }
    INDArray estMean = net.getLayer(0).getParam(BatchNormalizationParamInitializer.GLOBAL_MEAN);
    INDArray estVar = net.getLayer(0).getParam(BatchNormalizationParamInitializer.GLOBAL_VAR);
    float[] fMeanExp = expMean.data().asFloat();
    float[] fMeanAct = estMean.data().asFloat();
    float[] fVarExp = expVar.data().asFloat();
    float[] fVarAct = estVar.data().asFloat();
    //        System.out.println("Mean vs. estimated mean:");
    //        System.out.println(Arrays.toString(fMeanExp));
    //        System.out.println(Arrays.toString(fMeanAct));
    //
    //        System.out.println("Var vs. estimated var:");
    //        System.out.println(Arrays.toString(fVarExp));
    //        System.out.println(Arrays.toString(fVarAct));
    assertArrayEquals(fMeanExp, fMeanAct, 0.01f);
    assertArrayEquals(fVarExp, fVarAct, 0.01f);
}
Also used : ListDataSetIterator(org.deeplearning4j.datasets.iterator.impl.ListDataSetIterator) DataSet(org.nd4j.linalg.dataset.DataSet) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) BatchNormalization(org.deeplearning4j.nn.conf.layers.BatchNormalization) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) MnistDataSetIterator(org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator) ListDataSetIterator(org.deeplearning4j.datasets.iterator.impl.ListDataSetIterator) Test(org.junit.Test)

Example 82 with DataSet

use of org.nd4j.linalg.dataset.DataSet in project deeplearning4j by deeplearning4j.

the class BatchNormalizationTest method checkMeanVarianceEstimate.

@Test
public void checkMeanVarianceEstimate() throws Exception {
    Nd4j.getRandom().setSeed(12345);
    //Check that the internal global mean/variance estimate is approximately correct
    //First, Mnist data as 2d input (NOT taking into account convolution property)
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).updater(Updater.RMSPROP).seed(12345).list().layer(0, new BatchNormalization.Builder().nIn(10).nOut(10).eps(1e-5).decay(0.95).build()).layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MSE).weightInit(WeightInit.XAVIER).activation(Activation.IDENTITY).nIn(10).nOut(10).build()).backprop(true).pretrain(false).build();
    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.init();
    int minibatch = 32;
    List<DataSet> list = new ArrayList<>();
    for (int i = 0; i < 200; i++) {
        list.add(new DataSet(Nd4j.rand(minibatch, 10), Nd4j.rand(minibatch, 10)));
    }
    DataSetIterator iter = new ListDataSetIterator(list);
    INDArray expMean = Nd4j.valueArrayOf(new int[] { 1, 10 }, 0.5);
    //Expected variance of U(0,1) distribution: 1/12 * (1-0)^2 = 0.0833
    INDArray expVar = Nd4j.valueArrayOf(new int[] { 1, 10 }, 1 / 12.0);
    for (int i = 0; i < 10; i++) {
        iter.reset();
        net.fit(iter);
    }
    INDArray estMean = net.getLayer(0).getParam(BatchNormalizationParamInitializer.GLOBAL_MEAN);
    INDArray estVar = net.getLayer(0).getParam(BatchNormalizationParamInitializer.GLOBAL_VAR);
    float[] fMeanExp = expMean.data().asFloat();
    float[] fMeanAct = estMean.data().asFloat();
    float[] fVarExp = expVar.data().asFloat();
    float[] fVarAct = estVar.data().asFloat();
    //        System.out.println("Mean vs. estimated mean:");
    //        System.out.println(Arrays.toString(fMeanExp));
    //        System.out.println(Arrays.toString(fMeanAct));
    //
    //        System.out.println("Var vs. estimated var:");
    //        System.out.println(Arrays.toString(fVarExp));
    //        System.out.println(Arrays.toString(fVarAct));
    assertArrayEquals(fMeanExp, fMeanAct, 0.02f);
    assertArrayEquals(fVarExp, fVarAct, 0.02f);
}
Also used : ListDataSetIterator(org.deeplearning4j.datasets.iterator.impl.ListDataSetIterator) DataSet(org.nd4j.linalg.dataset.DataSet) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) MnistDataSetIterator(org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator) ListDataSetIterator(org.deeplearning4j.datasets.iterator.impl.ListDataSetIterator) Test(org.junit.Test)

Example 83 with DataSet

use of org.nd4j.linalg.dataset.DataSet in project deeplearning4j by deeplearning4j.

the class ConvolutionLayerTest method testCNNTooLargeKernel.

@Test(expected = DL4JException.class)
public void testCNNTooLargeKernel() {
    int imageHeight = 20;
    int imageWidth = 23;
    int nChannels = 1;
    int classes = 2;
    int numSamples = 200;
    int kernelHeight = imageHeight;
    int kernelWidth = imageWidth + 1;
    DataSet trainInput;
    MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().seed(123).iterations(1).list().layer(0, //(img-kernel+2*padding)/stride + 1: must be >= 1. Therefore: with p=0, kernel <= img size
    new ConvolutionLayer.Builder(kernelHeight, kernelWidth).stride(1, 1).nOut(2).activation(Activation.RELU).weightInit(WeightInit.XAVIER).build()).layer(1, new OutputLayer.Builder().nOut(classes).weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).build()).setInputType(InputType.convolutionalFlat(imageHeight, imageWidth, nChannels)).backprop(true).pretrain(false);
    MultiLayerConfiguration conf = builder.build();
    MultiLayerNetwork model = new MultiLayerNetwork(conf);
    model.init();
    INDArray emptyFeatures = Nd4j.zeros(numSamples, imageWidth * imageHeight * nChannels);
    INDArray emptyLables = Nd4j.zeros(numSamples, classes);
    trainInput = new DataSet(emptyFeatures, emptyLables);
    model.fit(trainInput);
}
Also used : MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataSet(org.nd4j.linalg.dataset.DataSet) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) Test(org.junit.Test)

Example 84 with DataSet

use of org.nd4j.linalg.dataset.DataSet in project deeplearning4j by deeplearning4j.

the class ConvolutionLayerTest method testCNNMLNPretrain.

//////////////////////////////////////////////////////////////////////////////////
@Test
public void testCNNMLNPretrain() throws Exception {
    // Note CNN does not do pretrain
    int numSamples = 10;
    int batchSize = 10;
    DataSetIterator mnistIter = new MnistDataSetIterator(batchSize, numSamples, true);
    MultiLayerNetwork model = getCNNMLNConfig(false, true);
    model.fit(mnistIter);
    mnistIter.reset();
    MultiLayerNetwork model2 = getCNNMLNConfig(false, true);
    model2.fit(mnistIter);
    mnistIter.reset();
    DataSet test = mnistIter.next();
    Evaluation eval = new Evaluation();
    INDArray output = model.output(test.getFeatureMatrix());
    eval.eval(test.getLabels(), output);
    double f1Score = eval.f1();
    Evaluation eval2 = new Evaluation();
    INDArray output2 = model2.output(test.getFeatureMatrix());
    eval2.eval(test.getLabels(), output2);
    double f1Score2 = eval2.f1();
    assertEquals(f1Score, f1Score2, 1e-4);
}
Also used : Evaluation(org.deeplearning4j.eval.Evaluation) MnistDataSetIterator(org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataSet(org.nd4j.linalg.dataset.DataSet) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) MnistDataSetIterator(org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator) Test(org.junit.Test)

Example 85 with DataSet

use of org.nd4j.linalg.dataset.DataSet in project deeplearning4j by deeplearning4j.

the class ConvolutionLayerTest method testCNNZeroStride.

@Test(expected = Exception.class)
public void testCNNZeroStride() {
    int imageHeight = 20;
    int imageWidth = 23;
    int nChannels = 1;
    int classes = 2;
    int numSamples = 200;
    int kernelHeight = imageHeight;
    int kernelWidth = imageWidth;
    DataSet trainInput;
    MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().seed(123).iterations(1).list().layer(0, new ConvolutionLayer.Builder(kernelHeight, kernelWidth).stride(1, 0).nOut(2).activation(Activation.RELU).weightInit(WeightInit.XAVIER).build()).layer(1, new OutputLayer.Builder().nOut(classes).weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).build()).backprop(true).pretrain(false);
    new ConvolutionLayerSetup(builder, imageHeight, imageWidth, nChannels);
    MultiLayerConfiguration conf = builder.build();
    MultiLayerNetwork model = new MultiLayerNetwork(conf);
    model.init();
    INDArray emptyFeatures = Nd4j.zeros(numSamples, imageWidth * imageHeight * nChannels);
    INDArray emptyLables = Nd4j.zeros(numSamples, classes);
    trainInput = new DataSet(emptyFeatures, emptyLables);
    model.fit(trainInput);
}
Also used : OutputLayer(org.deeplearning4j.nn.conf.layers.OutputLayer) DataSet(org.nd4j.linalg.dataset.DataSet) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ConvolutionLayer(org.deeplearning4j.nn.conf.layers.ConvolutionLayer) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) ConvolutionLayerSetup(org.deeplearning4j.nn.conf.layers.setup.ConvolutionLayerSetup) MultiLayerNetwork(org.deeplearning4j.nn.multilayer.MultiLayerNetwork) Test(org.junit.Test)

Aggregations

DataSet (org.nd4j.linalg.dataset.DataSet)334 Test (org.junit.Test)226 INDArray (org.nd4j.linalg.api.ndarray.INDArray)194 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)93 DataSetIterator (org.nd4j.linalg.dataset.api.iterator.DataSetIterator)82 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)79 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)73 IrisDataSetIterator (org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator)62 ArrayList (java.util.ArrayList)50 MnistDataSetIterator (org.deeplearning4j.datasets.iterator.impl.MnistDataSetIterator)41 ScoreIterationListener (org.deeplearning4j.optimize.listeners.ScoreIterationListener)38 BaseSparkTest (org.deeplearning4j.spark.BaseSparkTest)34 OutputLayer (org.deeplearning4j.nn.conf.layers.OutputLayer)32 DenseLayer (org.deeplearning4j.nn.conf.layers.DenseLayer)31 MultiDataSet (org.nd4j.linalg.dataset.MultiDataSet)31 ComputationGraph (org.deeplearning4j.nn.graph.ComputationGraph)25 SequenceRecordReader (org.datavec.api.records.reader.SequenceRecordReader)24 ComputationGraphConfiguration (org.deeplearning4j.nn.conf.ComputationGraphConfiguration)24 CSVSequenceRecordReader (org.datavec.api.records.reader.impl.csv.CSVSequenceRecordReader)23 ClassPathResource (org.nd4j.linalg.io.ClassPathResource)23