Java Code Examples for org.nd4j.autodiff.samediff.SameDiff#outputAll()

The following examples show how to use org.nd4j.autodiff.samediff.SameDiff#outputAll() . 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: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testStridedSlice2dBasic() {
    INDArray inArr = Nd4j.linspace(1, 12, 12).reshape('c', 3, 4);

    SameDiff sd = SameDiff.create();
    SDVariable in = sd.var("in", inArr);
    SDVariable slice_full = sd.stridedSlice(in,new long[]{0, 0},new long[]{3, 4},new long[]{1, 1});
    SDVariable subPart = sd.stridedSlice(in,new long[]{1, 2},new long[]{3, 4},new long[]{1, 1});
    // SDVariable subPart2 = sd.stridedSlice(in,new long[]{0, 0},new long[]{4, 5},new long[]{2, 2});

    sd.outputAll(null);

    assertEquals(inArr, slice_full.getArr());
    assertEquals(inArr.get(interval(1, 3), interval(2, 4)), subPart.getArr());
    // assertEquals(inArr.get(interval(0, 2, 4), interval(0, 2, 5)), subPart2.getArr());
}
 
Example 2
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testStridedSliceEllipsisMask() {
    INDArray inArr = Nd4j.linspace(1, 60, 60).reshape('c', 3, 4, 5);
    SameDiff sd = SameDiff.create();
    SDVariable in = sd.var("in", inArr);

    //[1:3,...] -> [1:3,:,:]
    SDVariable slice = sd.stridedSlice(in,new long[]{1},new long[]{3},new long[]{1}, 0, 0, 1 << 1, 0, 0);
    //[1:3,...,1:4] -> [1:3,:,1:4]
    SDVariable slice2 = sd.stridedSlice(in,new long[]{1, 1},new long[]{3, 4},new long[]{1, 1}, 0, 0, 1 << 1, 0, 0);

    sd.outputAll(Collections.emptyMap());

    assertEquals(inArr.get(interval(1, 3), all(), all()), slice.getArr());
    assertEquals(inArr.get(interval(1, 3), all(), all()), slice2.getArr());
}
 
Example 3
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testStridedSliceShrinkAxisMask() {

    INDArray inArr = Nd4j.linspace(1, 60, 60).reshape('c', 3, 4, 5);
    SameDiff sd = SameDiff.create();
    SDVariable in = sd.var("in", inArr);
    SDVariable slice = sd.stridedSlice(in,new long[]{0, 0, 0},new long[]{-999, 4, 5},new long[]{1, 1, 1}, 0, 0, 0, 0, 1);
    SDVariable slice2 = sd.stridedSlice(in,new long[]{2, 0, 0},new long[]{-999, 4, 5},new long[]{1, 1, 1}, 0, 0, 0, 0, 1);
    SDVariable slice3 = sd.stridedSlice(in,new long[]{1, 2, 1},new long[]{-999, -999, 5},new long[]{1, 1, 1}, 0, 0, 0, 0, 1 | 1 << 1);

    sd.outputAll(null);

    assertEquals(inArr.get(point(0), all(), all()), slice.getArr());
    assertEquals(inArr.get(point(2), all(), all()), slice2.getArr());
    assertEquals(inArr.get(point(1), point(2), interval(1, 5)).reshape(4), slice3.getArr());
}
 
Example 4
Source File: SameDiffInferenceExecutionerTests.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)

    public void testSameDiff() throws Exception {
        SameDiffInferenceExecutioner sameDiffInferenceExecutioner = new SameDiffInferenceExecutioner();
        SameDiff sameDiff = SameDiff.create();
        SDVariable input1 = sameDiff.placeHolder("input1", DataType.FLOAT,2, 2);
        SDVariable input2 = sameDiff.placeHolder("input2", DataType.FLOAT,2, 2);
        SDVariable result = input1.add("output", input2);
        INDArray input1Arr = Nd4j.linspace(1, 4, 4).reshape(2, 2);
        INDArray input2Arr = Nd4j.linspace(1, 4, 4).reshape(2, 2);
        sameDiff.associateArrayWithVariable(input1Arr, input1.name());
        sameDiff.associateArrayWithVariable(input2Arr, input2.name());
        Map<String, INDArray> indArrays = new LinkedHashMap<>();
        indArrays.put(input1.name(), input1Arr);
        indArrays.put(input2.name(), input2Arr);
        Map<String, INDArray> outputs = sameDiff.outputAll(indArrays);
        assertEquals(3, outputs.size());

        ParallelInferenceConfig parallelInferenceConfig = ParallelInferenceConfig.defaultConfig();
        File newFile = temporary.newFile();
        sameDiff.asFlatFile(newFile);
        SameDiffModelLoader sameDiffModelLoader = new SameDiffModelLoader(newFile, Arrays.asList("input1", "input2"), Arrays.asList("output"));


        sameDiffInferenceExecutioner.initialize(sameDiffModelLoader, parallelInferenceConfig);


        INDArray[] execute = sameDiffInferenceExecutioner.execute(new INDArray[]{input1Arr, input2Arr});
        assertEquals(outputs.values().iterator().next(), execute[0]);
    }
 
Example 5
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPermuteSimple() {
    SameDiff sameDiff = SameDiff.create();
    INDArray arr = Transforms.sigmoid(Nd4j.linspace(1, 6, 6).reshape(2, 3));
    SDVariable x = sameDiff.var("x", arr);
    SDVariable result = sameDiff.permute(x, 1, 0);
    Map<String,INDArray> m = sameDiff.outputAll(null);
    assertArrayEquals(new long[]{3, 2}, m.get(result.name()).shape());

}
 
Example 6
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSlice2d() {
    INDArray inArr = Nd4j.linspace(1, 12, 12).reshape('c', 3, 4);

    SameDiff sd = SameDiff.create();
    SDVariable in = sd.var("in", inArr);
    SDVariable slice_full = sd.slice(in, new int[]{0, 0}, new int[]{3, 4});
    SDVariable subPart = sd.slice(in, new int[]{1, 2}, new int[]{2, 2});

    Map<String,INDArray> m = sd.outputAll(Collections.emptyMap());

    assertEquals(inArr, m.get(slice_full.name()));
    assertEquals(inArr.get(interval(1, 3), interval(2, 4)), m.get(subPart.name()));
}
 
Example 7
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSlice3d() {
    INDArray inArr = Nd4j.linspace(1, 60, 60).reshape('c', 3, 4, 5);

    SameDiff sd = SameDiff.create();
    SDVariable in = sd.var("in", inArr);
    SDVariable slice_full = sd.slice(in, new int[]{0, 0, 0}, new int[]{3, 4, 5});
    SDVariable subPart = sd.slice(in, new int[]{1, 2, 3}, new int[]{2, 2, 1});

    Map<String,INDArray> m = sd.outputAll(null);

    assertEquals(inArr, m.get(slice_full.name()));
    assertEquals(inArr.get(interval(1, 3), interval(2, 4), interval(3, 4)), m.get(subPart.name()));
}
 
Example 8
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testStridedSliceBeginEndMask() {
    INDArray inArr = Nd4j.linspace(1, 12, 12).reshape('c', 3, 4);

    SameDiff sd = SameDiff.create();
    SDVariable in = sd.var("in", inArr);
    SDVariable slice1 = sd.stridedSlice(in,new long[]{-999, 0},new long[]{2, 4},new long[]{1, 1}, 1 << 1, 0, 0, 0, 0);
    SDVariable slice2 = sd.stridedSlice(in,new long[]{1, 0},new long[]{-999, 4},new long[]{1, 1}, 0, 1, 0, 0, 0);

    sd.outputAll(null);

    assertEquals(inArr.get(NDArrayIndex.interval(0, 2), NDArrayIndex.all()), slice1.getArr());
    assertEquals(inArr.get(NDArrayIndex.interval(1, 3), NDArrayIndex.all()), slice2.getArr());
}
 
Example 9
Source File: TransformOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testRank0EdgeCase() {
    SameDiff sd = SameDiff.create();
    SDVariable v1 = sd.sum(sd.var(Nd4j.create(new double[]{4, 4})));
    double d0 = v1.eval().getDouble(0);
    assertEquals(8, d0, 0);

    SDVariable v2 = sd.sum(sd.var(Nd4j.create(new double[]{4, 4}))).div(2.0);
    Map<String, INDArray> m = sd.outputAll(Collections.emptyMap());
    double d1 = m.get(v2.name()).getDouble(0);
    assertEquals(4, d1, 0);
}
 
Example 10
Source File: MiscOpValidation.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testMulGradient() {
    INDArray arr1 = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);
    INDArray arr2 = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).reshape(2, 2);

    INDArray gradAssertion = Nd4j.ones(arr1.shape());
    INDArray scalar = Nd4j.scalar(1.0);
    INDArray aGradAssertion = Nd4j.create(new double[][]{
            {1, 4},
            {9, 16}
    });

    INDArray cGradAssertion = Nd4j.create(new double[][]{
            {1, 2},
            {3, 4}
    });

    INDArray wGradAssertion = Nd4j.create(new double[][]{
            {2, 8},
            {18, 32}
    });

    INDArray dGradAssertion = Nd4j.ones(2, 2);

    SameDiff sameDiff = SameDiff.create();

    SDVariable sdVariable = sameDiff.var("a", arr1);
    SDVariable sdVariable1 = sameDiff.var("w", arr2);
    SDVariable varMulPre = sdVariable.mul("c", sdVariable1);
    SDVariable varMul = varMulPre.mul("d", sdVariable1);
    SDVariable sum = sameDiff.sum("ret", varMul, Integer.MAX_VALUE);

    Map<String,INDArray> m = sameDiff.outputAll(null);
    Map<String,INDArray> gm = sameDiff.calculateGradients(null, m.keySet());

    SDVariable finalResult = sameDiff.grad(sum.name());

    SDVariable cGrad = sameDiff.grad(varMulPre.name());

    SDVariable mulGradResult = sameDiff.grad(varMul.name());
    SDVariable aGrad = sameDiff.grad(sdVariable.name());
    SDVariable wGrad = sameDiff.grad(sdVariable1.name());
    SDVariable dGrad = sameDiff.grad(varMul.name());

    INDArray scalarGradTest = gm.get(sum.name());
    assertEquals(scalar, scalarGradTest);


    INDArray gradTest = mulGradResult.getArr();
    assertEquals(gradAssertion, gradTest);

    INDArray aGradTest = aGrad.getArr();
    assertEquals(aGradAssertion, aGradTest);

    INDArray cGradTest = cGrad.getArr();
    assertEquals(cGradAssertion, cGradTest);

    INDArray wGradTest = wGrad.getArr();
    assertEquals(wGradAssertion, wGradTest);

    INDArray dGradTest = dGrad.getArr();
    assertEquals(dGradAssertion, dGradTest);
}
 
Example 11
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testExpandDimsGradient() {
    val origShape = new long[]{3, 4};

    List<String> failed = new ArrayList<>();

    boolean first = true;
    for (int i = 0; i < 3; i++) {

        long[] expExpandShape;
        switch (i) {
            case 0:
                expExpandShape = new long[]{1, 3, 4};
                break;
            case 1:
                expExpandShape = new long[]{3, 1, 4};
                break;
            case 2:
                expExpandShape = new long[]{3, 4, 1};
                break;
            default:
                throw new RuntimeException();
        }

        for (Pair<INDArray, String> p : NDArrayCreationUtil.getAllTestMatricesWithShape(origShape[0], origShape[1], 12345, DataType.DOUBLE)) {
            INDArray inArr = p.getFirst().muli(100);

            SameDiff sd = SameDiff.create();
            SDVariable in = sd.var("in", inArr);
            SDVariable expand = sd.expandDims(in, i);
            //Using stdev here: mean/sum would backprop the same gradient for each input...
            SDVariable stdev = sd.standardDeviation("out", expand, true);

            Map<String,INDArray> m = sd.outputAll(null);
            INDArray expOut = in.getArr().std(true);

            assertArrayEquals(expExpandShape, m.get(expand.name()).shape());
            INDArray expExpand = inArr.dup('c').reshape(expExpandShape);

            String msg = "expandDim=" + i + ", source=" + p.getSecond();
            log.info("Starting: " + msg);

            TestCase tc = new TestCase(sd);
            tc.testName(msg)
                    .expectedOutput("out", expOut)
                    .expectedOutput(expand.name(), expExpand);

            String error = OpValidation.validate(tc);
            if(error != null){
                failed.add(error);
            }
        }
    }
    assertEquals(failed.toString(), 0, failed.size());
}
 
Example 12
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testSqueezeGradient() {
        val origShape = new long[]{3, 4, 5};

        List<String> failed = new ArrayList<>();

        for (int i = 0; i < 3; i++) {

            val shape = origShape.clone();
            shape[i] = 1;

            for (Pair<INDArray, String> p : NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, shape, DataType.DOUBLE)) {
                INDArray inArr = p.getFirst().muli(100);

                SameDiff sd = SameDiff.create();
                SDVariable in = sd.var("in", inArr);
                SDVariable squeeze = sd.squeeze(in, i);
                //Using stdev here: mean/sum would backprop the same gradient for each input...
                SDVariable stdev = sd.standardDeviation("out", squeeze, true);

                long[] expShapePostSqueeze;
                switch (i) {
                    case 0:
                        expShapePostSqueeze = new long[]{4, 5};
                        break;
                    case 1:
                        expShapePostSqueeze = new long[]{3, 5};
                        break;
                    case 2:
                        expShapePostSqueeze = new long[]{3, 4};
                        break;
                    default:
                        throw new RuntimeException();
                }

                INDArray exp = inArr.dup('c').reshape('c', expShapePostSqueeze);

                Map<String,INDArray> m = sd.outputAll(null);

                INDArray squeezed = m.get(squeeze.name());
//                assertArrayEquals(expShapePostSqueeze, squeezed.shape());

                INDArray out = m.get(stdev.name());
                INDArray expOut = in.getArr().std(true, Integer.MAX_VALUE);
                assertEquals(expOut, out);

                String msg = "squeezeDim=" + i + ", source=" + p.getSecond();
                TestCase tc = new TestCase(sd)
                        .testName(msg)
                        .expected(squeeze.name(), exp)
                        .expectedOutput("out", expOut);


                String error = OpValidation.validate(tc, true);
                if(error != null){
                    failed.add(name);
                }
            }
        }

        assertEquals(failed.toString(), 0, failed.size());
    }