Search in sources :

Example 46 with NormalDistribution

use of org.deeplearning4j.nn.conf.distribution.NormalDistribution in project deeplearning4j by deeplearning4j.

the class GradientCheckTestsComputationGraph method testLSTMWithDuplicateToTimeSeries.

@Test
public void testLSTMWithDuplicateToTimeSeries() {
    Nd4j.getRandom().setSeed(12345);
    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0, 1)).updater(Updater.NONE).learningRate(1.0).graphBuilder().addInputs("input1", "input2").setOutputs("out").addLayer("lstm1", new GravesLSTM.Builder().nIn(3).nOut(4).activation(Activation.TANH).build(), "input1").addLayer("lstm2", new GravesLSTM.Builder().nIn(4).nOut(5).activation(Activation.SOFTSIGN).build(), "input2").addVertex("lastTS", new LastTimeStepVertex("input2"), "lstm2").addVertex("duplicate", new DuplicateToTimeSeriesVertex("input2"), "lastTS").addLayer("out", new RnnOutputLayer.Builder().nIn(5 + 4).nOut(3).activation(Activation.SOFTMAX).lossFunction(LossFunctions.LossFunction.MCXENT).build(), "lstm1", "duplicate").pretrain(false).backprop(true).build();
    ComputationGraph graph = new ComputationGraph(conf);
    graph.init();
    Random r = new Random(12345);
    INDArray input1 = Nd4j.rand(new int[] { 3, 3, 5 });
    INDArray input2 = Nd4j.rand(new int[] { 3, 4, 5 });
    INDArray labels = Nd4j.zeros(3, 3, 5);
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 5; j++) {
            labels.putScalar(new int[] { i, r.nextInt(3), j }, 1.0);
        }
    }
    if (PRINT_RESULTS) {
        System.out.println("testLSTMWithDuplicateToTimeSeries()");
        for (int j = 0; j < graph.getNumLayers(); j++) System.out.println("Layer " + j + " # params: " + graph.getLayer(j).numParams());
    }
    boolean gradOK = GradientCheckUtil.checkGradients(graph, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR, DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, new INDArray[] { input1, input2 }, new INDArray[] { labels });
    String msg = "testLSTMWithDuplicateToTimeSeries()";
    assertTrue(msg, gradOK);
}
Also used : NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) DuplicateToTimeSeriesVertex(org.deeplearning4j.nn.conf.graph.rnn.DuplicateToTimeSeriesVertex) Random(java.util.Random) INDArray(org.nd4j.linalg.api.ndarray.INDArray) NormalDistribution(org.deeplearning4j.nn.conf.distribution.NormalDistribution) ComputationGraphConfiguration(org.deeplearning4j.nn.conf.ComputationGraphConfiguration) LastTimeStepVertex(org.deeplearning4j.nn.conf.graph.rnn.LastTimeStepVertex) ComputationGraph(org.deeplearning4j.nn.graph.ComputationGraph) Test(org.junit.Test)

Example 47 with NormalDistribution

use of org.deeplearning4j.nn.conf.distribution.NormalDistribution in project deeplearning4j by deeplearning4j.

the class GradientCheckTestsComputationGraph method testBasicIrisWithMerging.

@Test
public void testBasicIrisWithMerging() {
    Nd4j.getRandom().setSeed(12345);
    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0, 1)).updater(Updater.NONE).learningRate(1.0).graphBuilder().addInputs("input").addLayer("l1", new DenseLayer.Builder().nIn(4).nOut(5).activation(Activation.TANH).build(), "input").addLayer("l2", new DenseLayer.Builder().nIn(4).nOut(5).activation(Activation.TANH).build(), "input").addVertex("merge", new MergeVertex(), "l1", "l2").addLayer("outputLayer", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(5 + 5).nOut(3).build(), "merge").setOutputs("outputLayer").pretrain(false).backprop(true).build();
    ComputationGraph graph = new ComputationGraph(conf);
    graph.init();
    int numParams = (4 * 5 + 5) + (4 * 5 + 5) + (10 * 3 + 3);
    assertEquals(numParams, graph.numParams());
    Nd4j.getRandom().setSeed(12345);
    int nParams = graph.numParams();
    INDArray newParams = Nd4j.rand(1, nParams);
    graph.setParams(newParams);
    DataSet ds = new IrisDataSetIterator(150, 150).next();
    INDArray min = ds.getFeatureMatrix().min(0);
    INDArray max = ds.getFeatureMatrix().max(0);
    ds.getFeatureMatrix().subiRowVector(min).diviRowVector(max.sub(min));
    INDArray input = ds.getFeatureMatrix();
    INDArray labels = ds.getLabels();
    if (PRINT_RESULTS) {
        System.out.println("testBasicIrisWithMerging()");
        for (int j = 0; j < graph.getNumLayers(); j++) System.out.println("Layer " + j + " # params: " + graph.getLayer(j).numParams());
    }
    boolean gradOK = GradientCheckUtil.checkGradients(graph, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR, DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, new INDArray[] { input }, new INDArray[] { labels });
    String msg = "testBasicIrisWithMerging()";
    assertTrue(msg, gradOK);
}
Also used : IrisDataSetIterator(org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator) DataSet(org.nd4j.linalg.dataset.DataSet) INDArray(org.nd4j.linalg.api.ndarray.INDArray) NormalDistribution(org.deeplearning4j.nn.conf.distribution.NormalDistribution) ComputationGraphConfiguration(org.deeplearning4j.nn.conf.ComputationGraphConfiguration) ComputationGraph(org.deeplearning4j.nn.graph.ComputationGraph) Test(org.junit.Test)

Example 48 with NormalDistribution

use of org.deeplearning4j.nn.conf.distribution.NormalDistribution in project deeplearning4j by deeplearning4j.

the class GradientCheckTestsComputationGraph method testMultipleOutputsMergeVertex.

@Test
public void testMultipleOutputsMergeVertex() {
    Nd4j.getRandom().setSeed(12345);
    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0, 1)).updater(Updater.NONE).learningRate(1.0).activation(Activation.TANH).graphBuilder().addInputs("i0", "i1", "i2").addLayer("d0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "i0").addLayer("d1", new DenseLayer.Builder().nIn(2).nOut(2).build(), "i1").addLayer("d2", new DenseLayer.Builder().nIn(2).nOut(2).build(), "i2").addVertex("m", new MergeVertex(), "d0", "d1", "d2").addLayer("D0", new DenseLayer.Builder().nIn(6).nOut(2).build(), "m").addLayer("D1", new DenseLayer.Builder().nIn(6).nOut(2).build(), "m").addLayer("D2", new DenseLayer.Builder().nIn(6).nOut(2).build(), "m").addLayer("out", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MSE).nIn(6).nOut(2).build(), "D0", "D1", "D2").setOutputs("out").pretrain(false).backprop(true).build();
    ComputationGraph graph = new ComputationGraph(conf);
    graph.init();
    int[] minibatchSizes = { 1, 3 };
    for (int mb : minibatchSizes) {
        INDArray[] input = new INDArray[3];
        for (int i = 0; i < 3; i++) {
            input[i] = Nd4j.rand(mb, 2);
        }
        INDArray out = Nd4j.rand(mb, 2);
        String msg = "testMultipleOutputsMergeVertex() - minibatchSize = " + mb;
        if (PRINT_RESULTS) {
            System.out.println(msg);
            for (int j = 0; j < graph.getNumLayers(); j++) System.out.println("Layer " + j + " # params: " + graph.getLayer(j).numParams());
        }
        boolean gradOK = GradientCheckUtil.checkGradients(graph, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR, DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, new INDArray[] { out });
        assertTrue(msg, gradOK);
    }
}
Also used : NeuralNetConfiguration(org.deeplearning4j.nn.conf.NeuralNetConfiguration) INDArray(org.nd4j.linalg.api.ndarray.INDArray) NormalDistribution(org.deeplearning4j.nn.conf.distribution.NormalDistribution) ComputationGraphConfiguration(org.deeplearning4j.nn.conf.ComputationGraphConfiguration) ComputationGraph(org.deeplearning4j.nn.graph.ComputationGraph) Test(org.junit.Test)

Example 49 with NormalDistribution

use of org.deeplearning4j.nn.conf.distribution.NormalDistribution in project deeplearning4j by deeplearning4j.

the class GradientCheckTestsComputationGraph method testMultipleInputsLayer.

@Test
public void testMultipleInputsLayer() {
    Nd4j.getRandom().setSeed(12345);
    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0, 1)).updater(Updater.NONE).learningRate(1.0).activation(Activation.TANH).graphBuilder().addInputs("i0", "i1", "i2").addLayer("d0", new DenseLayer.Builder().nIn(2).nOut(2).build(), "i0").addLayer("d1", new DenseLayer.Builder().nIn(2).nOut(2).build(), "i1").addLayer("d2", new DenseLayer.Builder().nIn(2).nOut(2).build(), "i2").addLayer("d3", new DenseLayer.Builder().nIn(6).nOut(2).build(), "d0", "d1", "d2").addLayer("out", new OutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MSE).nIn(2).nOut(2).build(), "d3").setOutputs("out").pretrain(false).backprop(true).build();
    ComputationGraph graph = new ComputationGraph(conf);
    graph.init();
    int[] minibatchSizes = { 1, 3 };
    for (int mb : minibatchSizes) {
        INDArray[] inputs = new INDArray[3];
        for (int i = 0; i < 3; i++) {
            inputs[i] = Nd4j.rand(mb, 2);
        }
        INDArray out = Nd4j.rand(mb, 2);
        String msg = "testMultipleInputsLayer() - minibatchSize = " + mb;
        if (PRINT_RESULTS) {
            System.out.println(msg);
            for (int j = 0; j < graph.getNumLayers(); j++) System.out.println("Layer " + j + " # params: " + graph.getLayer(j).numParams());
        }
        boolean gradOK = GradientCheckUtil.checkGradients(graph, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR, DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, inputs, new INDArray[] { out });
        assertTrue(msg, gradOK);
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) NormalDistribution(org.deeplearning4j.nn.conf.distribution.NormalDistribution) ComputationGraphConfiguration(org.deeplearning4j.nn.conf.ComputationGraphConfiguration) ComputationGraph(org.deeplearning4j.nn.graph.ComputationGraph) Test(org.junit.Test)

Example 50 with NormalDistribution

use of org.deeplearning4j.nn.conf.distribution.NormalDistribution in project deeplearning4j by deeplearning4j.

the class GradientCheckTestsComputationGraph method testBasicIrisTripletStackingL2Loss.

@Test
public void testBasicIrisTripletStackingL2Loss() {
    Nd4j.getRandom().setSeed(12345);
    ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345).optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0, 1)).updater(Updater.NONE).learningRate(1.0).graphBuilder().addInputs("input1", "input2", "input3").addVertex("stack1", new StackVertex(), "input1", "input2", "input3").addLayer("l1", new DenseLayer.Builder().nIn(4).nOut(5).activation(Activation.TANH).build(), "stack1").addVertex("unstack0", new UnstackVertex(0, 3), "l1").addVertex("unstack1", new UnstackVertex(1, 3), "l1").addVertex("unstack2", new UnstackVertex(2, 3), "l1").addVertex("l2-1", new L2Vertex(), "unstack1", // x - x-
    "unstack0").addVertex("l2-2", new L2Vertex(), "unstack1", // x - x+
    "unstack2").addLayer("lossLayer", new LossLayer.Builder().lossFunction(LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).build(), "l2-1", "l2-2").setOutputs("lossLayer").pretrain(false).backprop(true).build();
    ComputationGraph graph = new ComputationGraph(conf);
    graph.init();
    int numParams = (4 * 5 + 5);
    assertEquals(numParams, graph.numParams());
    Nd4j.getRandom().setSeed(12345);
    int nParams = graph.numParams();
    INDArray newParams = Nd4j.rand(1, nParams);
    graph.setParams(newParams);
    INDArray pos = Nd4j.rand(150, 4);
    INDArray anc = Nd4j.rand(150, 4);
    INDArray neg = Nd4j.rand(150, 4);
    INDArray labels = Nd4j.zeros(150, 2);
    Random r = new Random(12345);
    for (int i = 0; i < 150; i++) {
        labels.putScalar(i, r.nextInt(2), 1.0);
    }
    Map<String, INDArray> out = graph.feedForward(new INDArray[] { pos, anc, neg }, true);
    for (String s : out.keySet()) {
        System.out.println(s + "\t" + Arrays.toString(out.get(s).shape()));
    }
    if (PRINT_RESULTS) {
        System.out.println("testBasicIrisTripletStackingL2Loss()");
        for (int j = 0; j < graph.getNumLayers(); j++) System.out.println("Layer " + j + " # params: " + graph.getLayer(j).numParams());
    }
    boolean gradOK = GradientCheckUtil.checkGradients(graph, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR, DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, new INDArray[] { pos, anc, neg }, new INDArray[] { labels });
    String msg = "testBasicIrisTripletStackingL2Loss()";
    assertTrue(msg, gradOK);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) Random(java.util.Random) NormalDistribution(org.deeplearning4j.nn.conf.distribution.NormalDistribution) ComputationGraphConfiguration(org.deeplearning4j.nn.conf.ComputationGraphConfiguration) ComputationGraph(org.deeplearning4j.nn.graph.ComputationGraph) Test(org.junit.Test)

Aggregations

NormalDistribution (org.deeplearning4j.nn.conf.distribution.NormalDistribution)90 Test (org.junit.Test)87 INDArray (org.nd4j.linalg.api.ndarray.INDArray)76 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)49 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)43 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)41 Random (java.util.Random)28 ComputationGraphConfiguration (org.deeplearning4j.nn.conf.ComputationGraphConfiguration)28 ComputationGraph (org.deeplearning4j.nn.graph.ComputationGraph)22 GravesLSTM (org.deeplearning4j.nn.layers.recurrent.GravesLSTM)13 DataSet (org.nd4j.linalg.dataset.DataSet)13 RnnOutputLayer (org.deeplearning4j.nn.conf.layers.RnnOutputLayer)12 IrisDataSetIterator (org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator)9 RnnToFeedForwardPreProcessor (org.deeplearning4j.nn.conf.preprocessor.RnnToFeedForwardPreProcessor)6 Activation (org.nd4j.linalg.activations.Activation)5 DataSetIterator (org.nd4j.linalg.dataset.api.iterator.DataSetIterator)5 DataNormalization (org.nd4j.linalg.dataset.api.preprocessor.DataNormalization)5 NormalizerMinMaxScaler (org.nd4j.linalg.dataset.api.preprocessor.NormalizerMinMaxScaler)5 LossFunction (org.nd4j.linalg.lossfunctions.LossFunctions.LossFunction)5 DenseLayer (org.deeplearning4j.nn.conf.layers.DenseLayer)4