Search in sources :

Example 1 with Triple

use of org.nd4j.linalg.primitives.Triple in project nd4j by deeplearning4j.

the class GradCheckMisc method testGradientAutoBroadcast3.

@Test
public void testGradientAutoBroadcast3() {
    // These tests: output size > input sizes
    Nd4j.getRandom().setSeed(12345);
    List<String> allFailed = new ArrayList<>();
    // Test cases: in1Shape, in2Shape, shapeOf(op(in1,in2))
    List<Triple<int[], int[], int[]>> testCases = new ArrayList<>();
    testCases.add(new Triple<>(new int[] { 3, 1 }, new int[] { 1, 4 }, new int[] { 3, 4 }));
    testCases.add(new Triple<>(new int[] { 3, 1 }, new int[] { 3, 4 }, new int[] { 3, 4 }));
    testCases.add(new Triple<>(new int[] { 3, 4 }, new int[] { 1, 4 }, new int[] { 3, 4 }));
    testCases.add(new Triple<>(new int[] { 3, 4, 1 }, new int[] { 1, 1, 5 }, new int[] { 3, 4, 5 }));
    testCases.add(new Triple<>(new int[] { 3, 4, 1 }, new int[] { 3, 1, 5 }, new int[] { 3, 4, 5 }));
    testCases.add(new Triple<>(new int[] { 3, 1, 5 }, new int[] { 1, 4, 1 }, new int[] { 3, 4, 5 }));
    testCases.add(new Triple<>(new int[] { 3, 1, 5 }, new int[] { 1, 4, 5 }, new int[] { 3, 4, 5 }));
    testCases.add(new Triple<>(new int[] { 3, 1, 5 }, new int[] { 3, 4, 5 }, new int[] { 3, 4, 5 }));
    testCases.add(new Triple<>(new int[] { 3, 1, 1, 1 }, new int[] { 1, 4, 5, 6 }, new int[] { 3, 4, 5, 6 }));
    testCases.add(new Triple<>(new int[] { 1, 1, 1, 6 }, new int[] { 3, 4, 5, 6 }, new int[] { 3, 4, 5, 6 }));
    testCases.add(new Triple<>(new int[] { 1, 4, 5, 1 }, new int[] { 3, 1, 1, 6 }, new int[] { 3, 4, 5, 6 }));
    testCases.add(new Triple<>(new int[] { 1, 6 }, new int[] { 3, 4, 5, 1 }, new int[] { 3, 4, 5, 6 }));
    for (Triple<int[], int[], int[]> p : testCases) {
        for (int i = 0; i < 6; i++) {
            SameDiff sd = SameDiff.create();
            SDVariable in3 = sd.var("in1", p.getFirst());
            SDVariable in2 = sd.var("in2", p.getSecond());
            String name;
            SDVariable bcOp;
            switch(i) {
                case 0:
                    bcOp = in3.add(in2);
                    name = "add";
                    break;
                case 1:
                    bcOp = in3.sub(in2);
                    name = "sub";
                    break;
                case 2:
                    bcOp = in3.mul(in2);
                    name = "mul";
                    break;
                case 3:
                    bcOp = in3.div(in2);
                    name = "div";
                    break;
                case 4:
                    bcOp = in3.rsub(in2);
                    name = "rsub";
                    break;
                case 5:
                    bcOp = in3.rdiv(in2);
                    name = "rdiv";
                    break;
                case 6:
                    bcOp = sd.f().floorDiv(in3, in2);
                    name = "floordiv";
                    break;
                case 7:
                    bcOp = sd.f().floorMod(in3, in2);
                    name = "floormod";
                    break;
                default:
                    throw new RuntimeException();
            }
            SDVariable outVar = sd.sum(bcOp);
            String msg = "(test " + i + ": " + name + ", array 1 size =" + Arrays.toString(p.getFirst()) + ", array 2 size = " + Arrays.toString(p.getSecond()) + ")";
            log.info("*** Starting test: " + msg);
            INDArray in3Arr = Nd4j.randn(p.getFirst()).muli(100);
            INDArray in2Arr = Nd4j.randn(p.getSecond()).muli(100);
            sd.associateArrayWithVariable(in3Arr, in3);
            sd.associateArrayWithVariable(in2Arr, in2);
            try {
                INDArray out = sd.execAndEndResult();
                assertNotNull(out);
                assertArrayEquals(new int[] { 1, 1 }, out.shape());
                INDArray bcOut = bcOp.getArr();
                assertNotNull(bcOp);
                assertArrayEquals(p.getThird(), bcOut.shape());
                // System.out.println(sd.asFlatPrint());
                boolean ok = GradCheckUtil.checkGradients(sd);
                if (!ok) {
                    allFailed.add(msg);
                }
            } catch (Exception e) {
                e.printStackTrace();
                allFailed.add(msg + " - EXCEPTION");
            }
        }
    }
    assertEquals("Failed: " + allFailed, 0, allFailed.size());
}
Also used : SameDiff(org.nd4j.autodiff.samediff.SameDiff) ArrayList(java.util.ArrayList) Triple(org.nd4j.linalg.primitives.Triple) SDVariable(org.nd4j.autodiff.samediff.SDVariable) INDArray(org.nd4j.linalg.api.ndarray.INDArray) Test(org.junit.Test)

Example 2 with Triple

use of org.nd4j.linalg.primitives.Triple in project nd4j by deeplearning4j.

the class GradCheckMisc method testSliceGradient.

@Test
public void testSliceGradient() {
    Nd4j.getRandom().setSeed(12345);
    // Order here: original shape, begin, size
    List<Triple<int[], int[], int[]>> testCases = new ArrayList<>();
    testCases.add(new Triple<>(new int[] { 3, 4 }, new int[] { 0, 0 }, new int[] { 3, 4 }));
    testCases.add(new Triple<>(new int[] { 3, 4 }, new int[] { 1, 1 }, new int[] { 3, 4 }));
    testCases.add(new Triple<>(new int[] { 3, 4 }, new int[] { 1, 2 }, new int[] { 2, 3 }));
    testCases.add(new Triple<>(new int[] { 3, 4, 5 }, new int[] { 0, 0, 0 }, new int[] { 3, 4, 5 }));
    testCases.add(new Triple<>(new int[] { 3, 4, 5 }, new int[] { 1, 1, 1 }, new int[] { 2, 3, 4 }));
    testCases.add(new Triple<>(new int[] { 3, 4, 5 }, new int[] { 1, 0, 2 }, new int[] { 3, 3, 4 }));
    for (int i = 0; i < testCases.size(); i++) {
        Triple<int[], int[], int[]> t = testCases.get(i);
        int[] os = t.getFirst();
        int[] b = t.getSecond();
        int[] e = t.getThird();
        INDArray arr = Nd4j.rand(os);
        SameDiff sd = SameDiff.create();
        SDVariable in = sd.var("in", arr);
        SDVariable slice = sd.slice(in, b, e);
        SDVariable stdev = sd.standardDeviation(slice, true);
        String msg = "i=" + i + ": inShape=" + Arrays.toString(os) + ", begin=" + Arrays.toString(b) + ", end=" + Arrays.toString(e);
        log.info("Starting test: " + msg);
        GradCheckUtil.checkGradients(sd);
    }
}
Also used : Triple(org.nd4j.linalg.primitives.Triple) SDVariable(org.nd4j.autodiff.samediff.SDVariable) INDArray(org.nd4j.linalg.api.ndarray.INDArray) SameDiff(org.nd4j.autodiff.samediff.SameDiff) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 SDVariable (org.nd4j.autodiff.samediff.SDVariable)2 SameDiff (org.nd4j.autodiff.samediff.SameDiff)2 INDArray (org.nd4j.linalg.api.ndarray.INDArray)2 Triple (org.nd4j.linalg.primitives.Triple)2