Java Code Examples for org.nd4j.linalg.checkutil.NDArrayCreationUtil#getAll3dTestArraysWithShape()

The following examples show how to use org.nd4j.linalg.checkutil.NDArrayCreationUtil#getAll3dTestArraysWithShape() . 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: SameDiffTests.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSqueezeExpandChain() {

    val origShape = new long[]{3, 4, 5};

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

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

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

            SameDiff sd = SameDiff.create();
            SDVariable in = sd.var("in", inArr);
            SDVariable squeeze = sd.squeeze(in, i);
            SDVariable expand = sd.expandDims(squeeze, i);

            INDArray out = sd.execAndEndResult();

            String msg = "expand/Squeeze=" + i + ", source=" + p.getSecond();

            assertEquals(msg, out, inArr);  //squeeze -> expand: should be opposite ops
        }
    }
}
 
Example 2
Source File: GradCheckMisc.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testReshapeGradient() {
    int[] origShape = new int[]{3, 4, 5};

    for (int[] toShape : new int[][]{{3, 4 * 5}, {3 * 4, 5}, {1, 3 * 4 * 5}, {3 * 4 * 5, 1}}) {
        for (Pair<INDArray, String> p : NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, origShape)) {
            INDArray inArr = p.getFirst().muli(100);

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

            INDArray out = sd.execAndEndResult();
            INDArray expOut = in.getArr().std(true, Integer.MAX_VALUE);
            assertEquals(expOut, out);

            String msg = "toShape=" + Arrays.toString(toShape) + ", source=" + p.getSecond();
            boolean ok = GradCheckUtil.checkGradients(sd);
            assertTrue(msg, ok);
        }
    }
}
 
Example 3
Source File: GradCheckMisc.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testPermuteGradient() {
    int[] origShape = new int[]{3, 4, 5};

    for (int[] perm : new int[][]{{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}) {
        for (Pair<INDArray, String> p : NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, origShape)) {
            INDArray inArr = p.getFirst().muli(100);

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

            INDArray out = sd.execAndEndResult();
            INDArray expOut = in.getArr().std(true, Integer.MAX_VALUE);
            assertEquals(expOut, out);

            String msg = "permute=" + Arrays.toString(perm) + ", source=" + p.getSecond();
            boolean ok = GradCheckUtil.checkGradients(sd);
            assertTrue(msg, ok);
        }
    }
}
 
Example 4
Source File: SameDiffTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSqueezeDims() {
    val origShape = new long[]{3, 4, 5};

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

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

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

            SameDiff sd = SameDiff.create();
            SDVariable in = sd.var("in", inArr);
            SDVariable squeeze = sd.f().squeeze(in, i);

            INDArray out = sd.execAndEndResult();

            INDArray expOut;
            switch (i) {
                case 0:
                    expOut = inArr.dup('c').reshape('c', origShape[1], origShape[2]);
                    break;
                case 1:
                    expOut = inArr.dup('c').reshape('c', origShape[0], origShape[2]);
                    break;
                case 2:
                    expOut = inArr.dup('c').reshape('c', origShape[0], origShape[1]);
                    break;
                default:
                    throw new RuntimeException();
            }

            String msg = "squeezeDim=" + i + ", source=" + p.getSecond();

            assertEquals(msg, out, expOut);
        }
    }
}
 
Example 5
Source File: SameDiffTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSqueezeDims() {
    val origShape = new long[]{3, 4, 5};

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

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

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

            SameDiff sd = SameDiff.create();
            SDVariable in = sd.var("in", inArr);
            SDVariable squeeze = sd.squeeze(in, i);

            INDArray out = squeeze.eval();

            INDArray expOut;
            switch (i) {
                case 0:
                    expOut = inArr.dup('c').reshape('c', origShape[1], origShape[2]);
                    break;
                case 1:
                    expOut = inArr.dup('c').reshape('c', origShape[0], origShape[2]);
                    break;
                case 2:
                    expOut = inArr.dup('c').reshape('c', origShape[0], origShape[1]);
                    break;
                default:
                    throw new RuntimeException();
            }

            String msg = "squeezeDim=" + i + ", source=" + p.getSecond();

            assertEquals(msg, out, expOut);
        }
    }
}
 
Example 6
Source File: SameDiffTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSqueezeExpandChain() {

    val origShape = new long[]{3, 4, 5};

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

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

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

            SameDiff sd = SameDiff.create();
            SDVariable in = sd.var("in", inArr);
            SDVariable squeeze = sd.squeeze(in, i);
            SDVariable expand = sd.expandDims(squeeze, i);

            INDArray out = expand.eval();

            String msg = "expand/Squeeze=" + i + ", source=" + p.getSecond();

            assertEquals(msg, out, inArr);  //squeeze -> expand: should be opposite ops
        }
    }
}
 
Example 7
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPermuteGradient() {
    int[] origShape = new int[]{3, 4, 5};

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

    for (int[] perm : new int[][]{{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}) {
        for (Pair<INDArray, String> p : NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, origShape, DataType.DOUBLE)) {
            String msg = "permute=" + Arrays.toString(perm) + ", source=" + p.getSecond();
            System.out.println(msg);

            INDArray inArr = p.getFirst().muli(100);

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

            INDArray exp = inArr.permute(perm);
            INDArray expOut = in.getArr().std(true, Integer.MAX_VALUE);


            TestCase tc = new TestCase(sd);
            tc.testName(msg)
                    .expected("out", expOut)
                    .expected(permute, exp);

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

    assertEquals(failed.toString(), 0, failed.size());
}
 
Example 8
Source File: TestNdArrReadWriteTxt.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
    public void testNd4jReadWriteText() throws Exception {

        File dir = testDir.newFolder();
        int count = 0;
        for(val testShape : new long[][]{{1,1}, {3,1}, {4,5}, {1,2,3}, {2,1,3}, {2,3,1}, {2,3,4}, {1,2,3,4}, {2,3,4,2}}){
            List<Pair<INDArray, String>> l = null;
            switch (testShape.length){
                case 2:
                    l = NDArrayCreationUtil.getAllTestMatricesWithShape(testShape[0], testShape[1], 12345, Nd4j.defaultFloatingPointType());
                    break;
                case 3:
                    l = NDArrayCreationUtil.getAll3dTestArraysWithShape(12345, testShape, Nd4j.defaultFloatingPointType());
                    break;
                case 4:
                    l = NDArrayCreationUtil.getAll4dTestArraysWithShape(12345, testShape, Nd4j.defaultFloatingPointType());
                    break;
                default:
                    throw new RuntimeException();
            }


            for (Pair<INDArray, String> p : l) {
                File f = new File(dir, (count++) + ".txt");
                Nd4j.writeTxt(p.getFirst(), f.getAbsolutePath());

                INDArray read = Nd4j.readTxt(f.getAbsolutePath());
                String s = FileUtils.readFileToString(f, StandardCharsets.UTF_8);
//                System.out.println(s);

                assertEquals(p.getFirst(), read);
            }
        }
    }
 
Example 9
Source File: GradCheckMisc.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testSqueezeGradient() {
    val origShape = new long[]{3, 4, 5};

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

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

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

            SameDiff sd = SameDiff.create();
            SDVariable in = sd.var("in", inArr);
            SDVariable squeeze = sd.f().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();
            }

            sd.execAndEndResult();

            INDArray squeezed = squeeze.getArr();
            assertArrayEquals(expShapePostSqueeze, squeezed.shape());

            INDArray out = sd.execAndEndResult();
            INDArray expOut = in.getArr().std(true, Integer.MAX_VALUE);
            assertEquals(expOut, out);

            String msg = "squeezeDim=" + i + ", source=" + p.getSecond();
            boolean ok = GradCheckUtil.checkGradients(sd);
            assertTrue(msg, ok);
        }
    }
}
 
Example 10
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());
    }