Java Code Examples for org.deeplearning4j.nn.gradient.Gradient#gradientForVariable()

The following examples show how to use org.deeplearning4j.nn.gradient.Gradient#gradientForVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RnnDataFormatTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private static List<String> differentGrads(Gradient g1, Gradient g2){
    List<String> differs = new ArrayList<>();
    Map<String,INDArray> m1 = g1.gradientForVariable();
    Map<String,INDArray> m2 = g2.gradientForVariable();
    for(String s : m1.keySet()){
        INDArray a1 = m1.get(s);
        INDArray a2 = m2.get(s);
        if(!a1.equals(a2)){
            differs.add(s);
        }
    }
    return differs;
}
 
Example 2
Source File: ConvDataFormatTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private static List<String> differentGrads(Gradient g1, Gradient g2){
    List<String> differs = new ArrayList<>();
    Map<String,INDArray> m1 = g1.gradientForVariable();
    Map<String,INDArray> m2 = g2.gradientForVariable();
    for(String s : m1.keySet()){
        INDArray a1 = m1.get(s);
        INDArray a2 = m2.get(s);
        if(!a1.equals(a2)){
            differs.add(s);
        }
    }
    return differs;
}
 
Example 3
Source File: ConvDataFormatTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
private static List<String> differentGrads(Gradient g1, Gradient g2){
    List<String> differs = new ArrayList<>();
    Map<String,INDArray> m1 = g1.gradientForVariable();
    Map<String,INDArray> m2 = g2.gradientForVariable();
    for(String s : m1.keySet()){
        INDArray a1 = m1.get(s);
        INDArray a2 = m2.get(s);
        if(!a1.equals(a2)){
            differs.add(s);
        }
    }
    return differs;
}
 
Example 4
Source File: TestSameDiffDense.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testSameDiffDenseBackward() {
        int nIn = 3;
        int nOut = 4;

        for (boolean workspaces : new boolean[]{false, true}) {

            for (int minibatch : new int[]{5, 1}) {

                Activation[] afns = new Activation[]{
                        Activation.TANH,
                        Activation.SIGMOID,
                        Activation.ELU,
                        Activation.IDENTITY,
                        Activation.SOFTPLUS,
                        Activation.SOFTSIGN,
                        Activation.HARDTANH,
                        Activation.CUBE,
                        Activation.RELU
                };

                for (Activation a : afns) {
                    log.info("Starting test - " + a + " - minibatch " + minibatch + ", workspaces: " + workspaces);
                    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                            .trainingWorkspaceMode(workspaces ? WorkspaceMode.ENABLED : WorkspaceMode.NONE)
                            .inferenceWorkspaceMode(workspaces ? WorkspaceMode.ENABLED : WorkspaceMode.NONE)
                            .list()
                            .layer(new SameDiffDense.Builder().nIn(nIn).nOut(nOut)
                                    .activation(a)
                                    .build())
                            .layer(new OutputLayer.Builder().nIn(nOut).nOut(nOut).activation(Activation.SOFTMAX)
                                    .lossFunction(LossFunctions.LossFunction.MCXENT).build())
                            .build();

                    MultiLayerNetwork netSD = new MultiLayerNetwork(conf);
                    netSD.init();

                    MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder()
                            .list()
                            .layer(new DenseLayer.Builder().activation(a).nIn(nIn).nOut(nOut).build())
                            .layer(new OutputLayer.Builder().nIn(nOut).nOut(nOut).activation(Activation.SOFTMAX)
                                    .lossFunction(LossFunctions.LossFunction.MCXENT).build())
                            .build();

                    MultiLayerNetwork netStandard = new MultiLayerNetwork(conf2);
                    netStandard.init();

                    netSD.params().assign(netStandard.params());

                    //Check params:
                    assertEquals(netStandard.params(), netSD.params());
                    assertEquals(netStandard.paramTable(), netSD.paramTable());

                    INDArray in = Nd4j.rand(minibatch, nIn);
                    INDArray l = TestUtils.randomOneHot(minibatch, nOut, 12345);
                    netSD.setInput(in);
                    netStandard.setInput(in);
                    netSD.setLabels(l);
                    netStandard.setLabels(l);

                    netSD.computeGradientAndScore();
                    netStandard.computeGradientAndScore();

                    Gradient gSD = netSD.gradient();
                    Gradient gStd = netStandard.gradient();

                    Map<String, INDArray> m1 = gSD.gradientForVariable();
                    Map<String, INDArray> m2 = gStd.gradientForVariable();

                    assertEquals(m2.keySet(), m1.keySet());

                    for (String s : m1.keySet()) {
                        INDArray i1 = m1.get(s);
                        INDArray i2 = m2.get(s);

                        assertEquals(s, i2, i1);
                    }

                    assertEquals(gStd.gradient(), gSD.gradient());

                    //Sanity check: different minibatch size
                    in = Nd4j.rand(2 * minibatch, nIn);
                    l = TestUtils.randomOneHot(2 * minibatch, nOut, 12345);
                    netSD.setInput(in);
                    netStandard.setInput(in);
                    netSD.setLabels(l);
                    netStandard.setLabels(l);

                    netSD.computeGradientAndScore();
//                    netStandard.computeGradientAndScore();
//                    assertEquals(netStandard.gradient().gradient(), netSD.gradient().gradient());

                    //Sanity check on different minibatch sizes:
                    INDArray newIn = Nd4j.vstack(in, in);
                    INDArray outMbsd = netSD.output(newIn);
                    INDArray outMb = netStandard.output(newIn);
                    assertEquals(outMb, outMbsd);
                }
            }
        }
    }
 
Example 5
Source File: MultiLayerTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testBackpropGradient() {
    //Testing: MultiLayerNetwork.backpropGradient()
    //i.e., specifically without an output layer

    int nIn = 10;
    int nOut = 40;
    int miniBatch = 5;

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                    .updater(new Sgd(0.1)).list()
                    .layer(0, new DenseLayer.Builder().nIn(nIn).nOut(20).activation(Activation.RELU)
                                    .weightInit(WeightInit.XAVIER).build())
                    .layer(1, new DenseLayer.Builder().nIn(20).nOut(30).activation(Activation.RELU)
                                    .weightInit(WeightInit.XAVIER).build())
                    .layer(2, new DenseLayer.Builder().nIn(30).nOut(nOut).activation(Activation.RELU)
                                    .weightInit(WeightInit.XAVIER).build())
                    .build();
    MultiLayerNetwork net = new MultiLayerNetwork(conf);
    net.init();

    Nd4j.getRandom().setSeed(12345);
    INDArray eps = Nd4j.rand(miniBatch, nOut);
    INDArray input = Nd4j.rand(miniBatch, nIn);

    net.setInput(input);
    net.feedForward(true, false); //Need to feed forward before backprop

    Pair<Gradient, INDArray> pair = net.backpropGradient(eps, LayerWorkspaceMgr.noWorkspaces());
    INDArray epsOut = pair.getSecond();
    assertNotNull(epsOut);
    assertArrayEquals(new long[] {miniBatch, nIn}, epsOut.shape());

    Gradient g = pair.getFirst();
    Map<String, INDArray> gradMap = g.gradientForVariable();
    assertEquals(6, gradMap.size()); //3 layers, weight + bias gradients for each

    String[] expKeys = {"0_" + DefaultParamInitializer.WEIGHT_KEY, "0_" + DefaultParamInitializer.BIAS_KEY,
                    "1_" + DefaultParamInitializer.WEIGHT_KEY, "2_" + DefaultParamInitializer.BIAS_KEY,
                    "2_" + DefaultParamInitializer.WEIGHT_KEY, "2_" + DefaultParamInitializer.BIAS_KEY};
    Set<String> keys = gradMap.keySet();
    for (String s : expKeys) {
        assertTrue(keys.contains(s));
    }

    /*
    System.out.println(pair);
    
    //Use updater to go from raw gradients -> updates
    //Apply learning rate, gradient clipping, adagrad/momentum/rmsprop etc
    Updater updater = UpdaterCreator.getUpdater(net);
    updater.update(net, g, 0, miniBatch);
    
    StepFunction stepFunction = new NegativeGradientStepFunction();
    INDArray params = net.params();
    System.out.println(Arrays.toString(params.get(NDArrayIndex.all(), NDArrayIndex.interval(0, 10)).dup().data().asFloat()));
    stepFunction.step(params, g.gradient());
    net.setParams(params);    //params() may not be in-place
    System.out.println(Arrays.toString(params.get(NDArrayIndex.all(), NDArrayIndex.interval(0, 10)).dup().data().asFloat()));
    */
}