Search in sources :

Example 46 with NeuralNetConfiguration

use of org.deeplearning4j.nn.conf.NeuralNetConfiguration in project deeplearning4j by deeplearning4j.

the class TestPreProcessors method testRnnToCnnPreProcessor.

@Test
public void testRnnToCnnPreProcessor() {
    //Two ways to test this:
    // (a) check that doing preProcess + backprop on a given input gives same result
    // (b) compare to ComposableInputPreProcessor(CNNtoFF, FFtoRNN)
    int[] miniBatchSizes = { 5, 1 };
    int[] timeSeriesLengths = { 9, 1 };
    int[] inputHeights = { 10, 30 };
    int[] inputWidths = { 10, 30 };
    int[] numChannels = { 1, 3, 6 };
    int cnnNChannelsIn = 3;
    Nd4j.getRandom().setSeed(12345);
    System.out.println();
    for (int miniBatchSize : miniBatchSizes) {
        for (int timeSeriesLength : timeSeriesLengths) {
            for (int inputHeight : inputHeights) {
                for (int inputWidth : inputWidths) {
                    for (int nChannels : numChannels) {
                        InputPreProcessor proc = new RnnToCnnPreProcessor(inputHeight, inputWidth, nChannels);
                        NeuralNetConfiguration nnc = new NeuralNetConfiguration.Builder().layer(new org.deeplearning4j.nn.conf.layers.ConvolutionLayer.Builder(inputWidth, inputHeight).nIn(cnnNChannelsIn).nOut(nChannels).build()).build();
                        int numParams = nnc.getLayer().initializer().numParams(nnc);
                        INDArray params = Nd4j.create(1, numParams);
                        ConvolutionLayer layer = (ConvolutionLayer) nnc.getLayer().instantiate(nnc, null, 0, params, true);
                        layer.setInputMiniBatchSize(miniBatchSize);
                        int[] shape_rnn = new int[] { miniBatchSize, nChannels * inputHeight * inputWidth, timeSeriesLength };
                        INDArray rand = Nd4j.rand(shape_rnn);
                        INDArray activationsRnn_c = Nd4j.create(shape_rnn, 'c');
                        INDArray activationsRnn_f = Nd4j.create(shape_rnn, 'f');
                        activationsRnn_c.assign(rand);
                        activationsRnn_f.assign(rand);
                        assertEquals(activationsRnn_c, activationsRnn_f);
                        //Check shape of outputs:
                        INDArray activationsCnn_c = proc.preProcess(activationsRnn_c, miniBatchSize);
                        INDArray activationsCnn_f = proc.preProcess(activationsRnn_f, miniBatchSize);
                        int[] shape_cnn = new int[] { miniBatchSize * timeSeriesLength, nChannels, inputHeight, inputWidth };
                        assertArrayEquals(shape_cnn, activationsCnn_c.shape());
                        assertArrayEquals(shape_cnn, activationsCnn_f.shape());
                        assertEquals(activationsCnn_c, activationsCnn_f);
                        //Check backward pass. Given that activations and epsilons have same shape, they should
                        //be opposite operations - i.e., get the same thing back out
                        INDArray twiceProcessed_c = proc.backprop(activationsCnn_c, miniBatchSize);
                        INDArray twiceProcessed_f = proc.backprop(activationsCnn_c, miniBatchSize);
                        assertArrayEquals(shape_rnn, twiceProcessed_c.shape());
                        assertArrayEquals(shape_rnn, twiceProcessed_f.shape());
                        assertEquals(activationsRnn_c, twiceProcessed_c);
                        assertEquals(activationsRnn_c, twiceProcessed_f);
                        //Second way to check: compare to ComposableInputPreProcessor(RNNtoFF, FFtoCNN)
                        InputPreProcessor compProc = new ComposableInputPreProcessor(new RnnToFeedForwardPreProcessor(), new FeedForwardToCnnPreProcessor(inputHeight, inputWidth, nChannels));
                        INDArray activationsCnnComp_c = compProc.preProcess(activationsRnn_c, miniBatchSize);
                        INDArray activationsCnnComp_f = compProc.preProcess(activationsRnn_f, miniBatchSize);
                        assertEquals(activationsCnnComp_c, activationsCnn_c);
                        assertEquals(activationsCnnComp_f, activationsCnn_f);
                        int[] epsilonShape = new int[] { miniBatchSize * timeSeriesLength, nChannels, inputHeight, inputWidth };
                        rand = Nd4j.rand(epsilonShape);
                        INDArray epsilonsCnn_c = Nd4j.create(epsilonShape, 'c');
                        INDArray epsilonsCnn_f = Nd4j.create(epsilonShape, 'f');
                        epsilonsCnn_c.assign(rand);
                        epsilonsCnn_f.assign(rand);
                        INDArray epsilonsRnnComp_c = compProc.backprop(epsilonsCnn_c, miniBatchSize);
                        INDArray epsilonsRnnComp_f = compProc.backprop(epsilonsCnn_f, miniBatchSize);
                        assertEquals(epsilonsRnnComp_c, epsilonsRnnComp_f);
                        INDArray epsilonsRnn_c = proc.backprop(epsilonsCnn_c, miniBatchSize);
                        INDArray epsilonsRnn_f = proc.backprop(epsilonsCnn_f, miniBatchSize);
                        assertEquals(epsilonsRnn_c, epsilonsRnn_f);
                        if (!epsilonsRnn_c.equals(epsilonsRnnComp_c)) {
                            System.out.println(miniBatchSize + "\t" + timeSeriesLength + "\t" + inputHeight + "\t" + inputWidth + "\t" + nChannels);
                            System.out.println("expected - epsilonsRnnComp");
                            System.out.println(Arrays.toString(epsilonsRnnComp_c.shape()));
                            System.out.println(epsilonsRnnComp_c);
                            System.out.println("actual - epsilonsRnn");
                            System.out.println(Arrays.toString(epsilonsRnn_c.shape()));
                            System.out.println(epsilonsRnn_c);
                        }
                        assertEquals(epsilonsRnnComp_c, epsilonsRnn_c);
                        assertEquals(epsilonsRnnComp_c, epsilonsRnn_f);
                    }
                }
            }
        }
    }
}
Also used : InputPreProcessor(org.deeplearning4j.nn.conf.InputPreProcessor) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ConvolutionLayer(org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Example 47 with NeuralNetConfiguration

use of org.deeplearning4j.nn.conf.NeuralNetConfiguration in project deeplearning4j by deeplearning4j.

the class TestPreProcessors method testCnnToRnnPreProcessor.

@Test
public void testCnnToRnnPreProcessor() {
    //Two ways to test this:
    // (a) check that doing preProcess + backprop on a given input gives same result
    // (b) compare to ComposableInputPreProcessor(CNNtoFF, FFtoRNN)
    int[] miniBatchSizes = { 5, 1 };
    int[] timeSeriesLengths = { 9, 1 };
    int[] inputHeights = { 10, 30 };
    int[] inputWidths = { 10, 30 };
    int[] numChannels = { 1, 3, 6 };
    int cnnNChannelsIn = 3;
    Nd4j.getRandom().setSeed(12345);
    System.out.println();
    for (int miniBatchSize : miniBatchSizes) {
        for (int timeSeriesLength : timeSeriesLengths) {
            for (int inputHeight : inputHeights) {
                for (int inputWidth : inputWidths) {
                    for (int nChannels : numChannels) {
                        String msg = "miniBatch=" + miniBatchSize + ", tsLength=" + timeSeriesLength + ", h=" + inputHeight + ", w=" + inputWidth + ", ch=" + nChannels;
                        InputPreProcessor proc = new CnnToRnnPreProcessor(inputHeight, inputWidth, nChannels);
                        NeuralNetConfiguration nnc = new NeuralNetConfiguration.Builder().layer(new org.deeplearning4j.nn.conf.layers.ConvolutionLayer.Builder(inputWidth, inputHeight).nIn(cnnNChannelsIn).nOut(nChannels).build()).build();
                        int numParams = nnc.getLayer().initializer().numParams(nnc);
                        INDArray params = Nd4j.create(1, numParams);
                        ConvolutionLayer layer = (ConvolutionLayer) nnc.getLayer().instantiate(nnc, null, 0, params, true);
                        layer.setInputMiniBatchSize(miniBatchSize);
                        INDArray activationsCnn = Nd4j.rand(new int[] { miniBatchSize * timeSeriesLength, nChannels, inputHeight, inputWidth });
                        //Check shape of outputs:
                        int prod = nChannels * inputHeight * inputWidth;
                        INDArray activationsRnn = proc.preProcess(activationsCnn, miniBatchSize);
                        assertArrayEquals(msg, new int[] { miniBatchSize, prod, timeSeriesLength }, activationsRnn.shape());
                        //Check backward pass. Given that activations and epsilons have same shape, they should
                        //be opposite operations - i.e., get the same thing back out
                        INDArray twiceProcessed = proc.backprop(activationsRnn, miniBatchSize);
                        assertArrayEquals(msg, activationsCnn.shape(), twiceProcessed.shape());
                        assertEquals(msg, activationsCnn, twiceProcessed);
                        //Second way to check: compare to ComposableInputPreProcessor(CNNtoFF, FFtoRNN)
                        InputPreProcessor compProc = new ComposableInputPreProcessor(new CnnToFeedForwardPreProcessor(inputHeight, inputWidth, nChannels), new FeedForwardToRnnPreProcessor());
                        INDArray activationsRnnComp = compProc.preProcess(activationsCnn, miniBatchSize);
                        assertEquals(msg, activationsRnnComp, activationsRnn);
                        INDArray epsilonsRnn = Nd4j.rand(new int[] { miniBatchSize, nChannels * inputHeight * inputWidth, timeSeriesLength });
                        INDArray epsilonsCnnComp = compProc.backprop(epsilonsRnn, miniBatchSize);
                        INDArray epsilonsCnn = proc.backprop(epsilonsRnn, miniBatchSize);
                        if (!epsilonsCnn.equals(epsilonsCnnComp)) {
                            System.out.println(miniBatchSize + "\t" + timeSeriesLength + "\t" + inputHeight + "\t" + inputWidth + "\t" + nChannels);
                            System.out.println("expected - epsilonsCnnComp");
                            System.out.println(Arrays.toString(epsilonsCnnComp.shape()));
                            System.out.println(epsilonsCnnComp);
                            System.out.println("actual - epsilonsCnn");
                            System.out.println(Arrays.toString(epsilonsCnn.shape()));
                            System.out.println(epsilonsCnn);
                        }
                        assertEquals(msg, epsilonsCnnComp, epsilonsCnn);
                    }
                }
            }
        }
    }
}
Also used : InputPreProcessor(org.deeplearning4j.nn.conf.InputPreProcessor) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ConvolutionLayer(org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Example 48 with NeuralNetConfiguration

use of org.deeplearning4j.nn.conf.NeuralNetConfiguration in project deeplearning4j by deeplearning4j.

the class TestCustomActivation method testCustomActivationFn.

@Test
public void testCustomActivationFn() {
    //First: Ensure that the CustomActivation class is registered
    ObjectMapper mapper = NeuralNetConfiguration.mapper();
    AnnotatedClass ac = AnnotatedClass.construct(IActivation.class, mapper.getSerializationConfig().getAnnotationIntrospector(), null);
    Collection<NamedType> types = mapper.getSubtypeResolver().collectAndResolveSubtypes(ac, mapper.getSerializationConfig(), mapper.getSerializationConfig().getAnnotationIntrospector());
    boolean found = false;
    for (NamedType nt : types) {
        System.out.println(nt);
        if (nt.getType() == CustomActivation.class)
            found = true;
    }
    assertTrue("CustomActivation: not registered with NeuralNetConfiguration mapper", found);
    //Second: let's create a MultiLayerCofiguration with one, and check JSON and YAML config actually works...
    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().learningRate(0.1).list().layer(0, new DenseLayer.Builder().nIn(10).nOut(10).activation(new CustomActivation()).build()).layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(10).nOut(10).build()).pretrain(false).backprop(true).build();
    String json = conf.toJson();
    String yaml = conf.toYaml();
    System.out.println(json);
    MultiLayerConfiguration confFromJson = MultiLayerConfiguration.fromJson(json);
    assertEquals(conf, confFromJson);
    MultiLayerConfiguration confFromYaml = MultiLayerConfiguration.fromYaml(yaml);
    assertEquals(conf, confFromYaml);
}
Also used : OutputLayer(org.deeplearning4j.nn.conf.layers.OutputLayer) NamedType(org.nd4j.shade.jackson.databind.jsontype.NamedType) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) MultiLayerConfiguration(org.deeplearning4j.nn.conf.MultiLayerConfiguration) DenseLayer(org.deeplearning4j.nn.conf.layers.DenseLayer) AnnotatedClass(org.nd4j.shade.jackson.databind.introspect.AnnotatedClass) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper) CustomActivation(org.deeplearning4j.nn.layers.custom.testclasses.CustomActivation) Test(org.junit.Test)

Example 49 with NeuralNetConfiguration

use of org.deeplearning4j.nn.conf.NeuralNetConfiguration in project deeplearning4j by deeplearning4j.

the class AutoEncoderTest method testBackProp.

@Test
public void testBackProp() throws Exception {
    MnistDataFetcher fetcher = new MnistDataFetcher(true);
    //        LayerFactory layerFactory = LayerFactories.getFactory(new org.deeplearning4j.nn.conf.layers.AutoEncoder());
    NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().momentum(0.9f).optimizationAlgo(OptimizationAlgorithm.LINE_GRADIENT_DESCENT).iterations(100).learningRate(1e-1f).layer(new org.deeplearning4j.nn.conf.layers.AutoEncoder.Builder().nIn(784).nOut(600).corruptionLevel(0.6).lossFunction(LossFunctions.LossFunction.RECONSTRUCTION_CROSSENTROPY).build()).build();
    fetcher.fetch(100);
    DataSet d2 = fetcher.next();
    INDArray input = d2.getFeatureMatrix();
    int numParams = conf.getLayer().initializer().numParams(conf);
    INDArray params = Nd4j.create(1, numParams);
    AutoEncoder da = (AutoEncoder) conf.getLayer().instantiate(conf, null, 0, params, true);
    Gradient g = new DefaultGradient();
    g.gradientForVariable().put(DefaultParamInitializer.WEIGHT_KEY, da.decode(da.activate(input)).sub(input));
}
Also used : Gradient(org.deeplearning4j.nn.gradient.Gradient) DefaultGradient(org.deeplearning4j.nn.gradient.DefaultGradient) DefaultGradient(org.deeplearning4j.nn.gradient.DefaultGradient) MnistDataFetcher(org.deeplearning4j.datasets.fetchers.MnistDataFetcher) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataSet(org.nd4j.linalg.dataset.DataSet) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) Test(org.junit.Test)

Example 50 with NeuralNetConfiguration

use of org.deeplearning4j.nn.conf.NeuralNetConfiguration in project deeplearning4j by deeplearning4j.

the class AutoEncoderTest method testAutoEncoder.

@Test
public void testAutoEncoder() throws Exception {
    MnistDataFetcher fetcher = new MnistDataFetcher(true);
    NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder().momentum(0.9f).optimizationAlgo(OptimizationAlgorithm.LINE_GRADIENT_DESCENT).iterations(1).learningRate(1e-1f).layer(new org.deeplearning4j.nn.conf.layers.AutoEncoder.Builder().nIn(784).nOut(600).corruptionLevel(0.6).lossFunction(LossFunctions.LossFunction.RECONSTRUCTION_CROSSENTROPY).build()).build();
    fetcher.fetch(100);
    DataSet d2 = fetcher.next();
    INDArray input = d2.getFeatureMatrix();
    int numParams = conf.getLayer().initializer().numParams(conf);
    INDArray params = Nd4j.create(1, numParams);
    AutoEncoder da = (AutoEncoder) conf.getLayer().instantiate(conf, Arrays.<IterationListener>asList(new ScoreIterationListener(1)), 0, params, true);
    assertEquals(da.params(), da.params());
    assertEquals(471784, da.params().length());
    da.setParams(da.params());
    da.fit(input);
}
Also used : MnistDataFetcher(org.deeplearning4j.datasets.fetchers.MnistDataFetcher) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataSet(org.nd4j.linalg.dataset.DataSet) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) IterationListener(org.deeplearning4j.optimize.api.IterationListener) NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) ScoreIterationListener(org.deeplearning4j.optimize.listeners.ScoreIterationListener) Test(org.junit.Test)

Aggregations

NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)83 INDArray (org.nd4j.linalg.api.ndarray.INDArray)65 Test (org.junit.Test)55 Layer (org.deeplearning4j.nn.api.Layer)29 Gradient (org.deeplearning4j.nn.gradient.Gradient)26 DenseLayer (org.deeplearning4j.nn.conf.layers.DenseLayer)24 Updater (org.deeplearning4j.nn.api.Updater)22 OutputLayer (org.deeplearning4j.nn.conf.layers.OutputLayer)21 DefaultGradient (org.deeplearning4j.nn.gradient.DefaultGradient)21 DataSet (org.nd4j.linalg.dataset.DataSet)14 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)11 ScoreIterationListener (org.deeplearning4j.optimize.listeners.ScoreIterationListener)9 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)8 IrisDataSetIterator (org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator)6 UniformDistribution (org.deeplearning4j.nn.conf.distribution.UniformDistribution)6 RnnOutputLayer (org.deeplearning4j.nn.layers.recurrent.RnnOutputLayer)6 MnistDataFetcher (org.deeplearning4j.datasets.fetchers.MnistDataFetcher)4 Evaluation (org.deeplearning4j.eval.Evaluation)4 Model (org.deeplearning4j.nn.api.Model)4 ConvolutionLayer (org.deeplearning4j.nn.conf.layers.ConvolutionLayer)4