Search in sources :

Example 21 with DynamicCustomOp

use of org.nd4j.linalg.api.ops.DynamicCustomOp in project nd4j by deeplearning4j.

the class ConvolutionTests method testPooling11.

@Test
public void testPooling11() {
    for (char outputOrder : new char[] { 'c', 'f' }) {
        INDArray exp = Nd4j.create(new float[] { 3, 4, 6, 7 }, new int[] { 1, 1, 2, 2 }, 'c');
        int len = 1 * 1 * 3 * 3;
        INDArray x = Nd4j.linspace(1, len, len).reshape('c', 1, 1, 3, 3);
        DynamicCustomOp op = DynamicCustomOp.builder("avgpool2d").addIntegerArguments(new int[] { 2, 2, 1, 1, 0, 0, 1, 1, 0, 0, 0 }).addInputs(x).addOutputs(Nd4j.create(new int[] { 1, 1, 2, 2 }, outputOrder)).build();
        Nd4j.getExecutioner().exec(op);
        INDArray out = op.getOutputArgument(0);
        assertEquals("Output order: " + outputOrder, exp, out);
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DynamicCustomOp(org.nd4j.linalg.api.ops.DynamicCustomOp) NDArrayIndex.point(org.nd4j.linalg.indexing.NDArrayIndex.point) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 22 with DynamicCustomOp

use of org.nd4j.linalg.api.ops.DynamicCustomOp in project nd4j by deeplearning4j.

the class ConvolutionTests method testPoolingEdgeCases.

@Test
public void testPoolingEdgeCases() {
    for (char inputOrder : new char[] { 'c', 'f' }) {
        for (char outputOrder : new char[] { 'c', 'f' }) {
            INDArray input = Nd4j.create(1, 1, 3, 3);
            input.get(point(0), point(0), all(), all()).assign(Nd4j.linspace(1, 9, 9).reshape('c', 3, 3)).dup(inputOrder);
            input = input.dup('c');
            // .dup(inputOrder);
            INDArray input2 = Nd4j.create(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new int[] { 1, 1, 3, 3 }, 'c');
            assertEquals(input, input2);
            input = input2;
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    System.out.print(input.getDouble(0, 0, i, j) + ",");
                }
                System.out.println();
            }
            System.out.println();
            INDArray sums = Nd4j.create(new double[][] { { (1 + 2 + 4 + 5), (2 + 3 + 5 + 6), (3 + 6) }, { (4 + 5 + 7 + 8), (5 + 6 + 8 + 9), (6 + 9) }, { (7 + 8), (8 + 9), (9) } });
            INDArray divEnabled = Nd4j.create(new double[][] { { 4, 4, 2 }, { 4, 4, 2 }, { 2, 2, 1 } });
            INDArray expEnabled = sums.div(divEnabled);
            INDArray expDl4j = sums.div(4);
            // https://github.com/deeplearning4j/libnd4j/blob/master/include/ops/declarable/generic/convo/pooling/avgpool2d.cpp
            DynamicCustomOp op1 = DynamicCustomOp.builder("avgpool2d").addIntegerArguments(// ky, kx, sy, sx, py, px, dy, dx, isSameMode, ???, divisor, nchw
            new int[] { 2, 2, 1, 1, 0, 0, 1, 1, 1, 0, 0 }).addInputs(input).addOutputs(Nd4j.create(new int[] { 1, 1, 3, 3 }, outputOrder)).build();
            DynamicCustomOp op2 = DynamicCustomOp.builder("avgpool2d").addIntegerArguments(// ky, kx, sy, sx, py, px, dy, dx, isSameMode, ???, divisor, nchw
            new int[] { 2, 2, 1, 1, 0, 0, 1, 1, 1, 2, 0 }).addInputs(input).addOutputs(Nd4j.create(new int[] { 1, 1, 3, 3 }, outputOrder)).build();
            Nd4j.getExecutioner().exec(op1);
            Nd4j.getExecutioner().exec(op2);
            INDArray actEnabled = op1.getOutputArgument(0);
            INDArray actDl4j = op2.getOutputArgument(0);
            String msg = "inOrder=" + inputOrder + ", outOrder=" + outputOrder;
            assertEquals(msg, expDl4j, actDl4j.get(point(0), point(0), all(), all()));
            assertEquals(msg, expEnabled, actEnabled.get(point(0), point(0), all(), all()));
        }
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DynamicCustomOp(org.nd4j.linalg.api.ops.DynamicCustomOp) NDArrayIndex.point(org.nd4j.linalg.indexing.NDArrayIndex.point) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 23 with DynamicCustomOp

use of org.nd4j.linalg.api.ops.DynamicCustomOp in project nd4j by deeplearning4j.

the class ConvolutionTests method testPooling13.

@Test
public void testPooling13() {
    for (char outputOrder : new char[] { 'c' }) {
        INDArray exp = Nd4j.create(new float[] { 3.f, 4.f, 4.5f, 6.f, 7.f, 7.5f, 7.5f, 8.5f, 9.f }, new int[] { 1, 1, 3, 3 }, 'c');
        int len = 1 * 1 * 3 * 3;
        INDArray x = Nd4j.linspace(1, len, len).reshape('c', 1, 1, 3, 3);
        DynamicCustomOp op = DynamicCustomOp.builder("avgpool2d").addIntegerArguments(new int[] { 2, 2, 1, 1, 0, 0, 1, 1, 1, 0, 0 }).addInputs(x).addOutputs(Nd4j.create(new int[] { 1, 1, 3, 3 }, outputOrder)).build();
        Nd4j.getExecutioner().exec(op);
        INDArray out = op.getOutputArgument(0);
        assertEquals("Output order: " + outputOrder, exp, out);
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DynamicCustomOp(org.nd4j.linalg.api.ops.DynamicCustomOp) NDArrayIndex.point(org.nd4j.linalg.indexing.NDArrayIndex.point) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 24 with DynamicCustomOp

use of org.nd4j.linalg.api.ops.DynamicCustomOp in project nd4j by deeplearning4j.

the class ConvolutionTests method testPooling8.

@Test
public void testPooling8() {
    for (char outputOrder : new char[] { 'c', 'f' }) {
        INDArray exp = Nd4j.create(new float[] { 1.f, 2.5f, 4.5f, 8.5f, 10.f, 12.f, 18.5f, 20.f, 22.f, 26.f, 27.5f, 29.5f, 33.5f, 35.f, 37.f, 43.5f, 45.f, 47.f, 51.f, 52.5f, 54.5f, 58.5f, 60.f, 62.f, 68.5f, 70.f, 72.f, 76.f, 77.5f, 79.5f, 83.5f, 85.f, 87.f, 93.5f, 95.f, 97.f }, new int[] { 2, 2, 3, 3 }, 'c');
        int len = 2 * 2 * 5 * 5;
        INDArray x = Nd4j.linspace(1, len, len).reshape('c', 2, 2, 5, 5);
        DynamicCustomOp op = DynamicCustomOp.builder("avgpool2d").addIntegerArguments(new int[] { 2, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0 }).addInputs(x).addOutputs(Nd4j.create(new int[] { 2, 2, 3, 3 }, outputOrder)).build();
        Nd4j.getExecutioner().exec(op);
        INDArray out = op.getOutputArgument(0);
        assertEquals("Output order: " + outputOrder, exp, out);
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DynamicCustomOp(org.nd4j.linalg.api.ops.DynamicCustomOp) NDArrayIndex.point(org.nd4j.linalg.indexing.NDArrayIndex.point) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 25 with DynamicCustomOp

use of org.nd4j.linalg.api.ops.DynamicCustomOp in project nd4j by deeplearning4j.

the class ConvolutionTests method testPooling6.

@Test
public void testPooling6() {
    for (char outputOrder : new char[] { 'c', 'f' }) {
        INDArray exp = Nd4j.create(new float[] { 7.f, 8.f, 11.f, 12.f, 27.f, 28.f, 31.f, 32.f, 57.f, 58.f, 61.f, 62.f, 77.f, 78.f, 81.f, 82.f }, new int[] { 2, 2, 2, 2 }, 'c');
        int len = 2 * 5 * 5 * 2;
        INDArray x = Nd4j.linspace(1, len, len).reshape('c', 2, 5, 5, 2);
        DynamicCustomOp op = DynamicCustomOp.builder("avgpool2d").addIntegerArguments(new int[] { 2, 2, 2, 2, 0, 0, 1, 1, 0, 1, 1 }).addInputs(x).addOutputs(Nd4j.create(new int[] { 2, 2, 2, 2 }, outputOrder)).build();
        Nd4j.getExecutioner().exec(op);
        INDArray out = op.getOutputArgument(0);
        assertEquals("Output order: " + outputOrder, exp, out);
    }
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) DynamicCustomOp(org.nd4j.linalg.api.ops.DynamicCustomOp) NDArrayIndex.point(org.nd4j.linalg.indexing.NDArrayIndex.point) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Aggregations

Test (org.junit.Test)26 INDArray (org.nd4j.linalg.api.ndarray.INDArray)26 DynamicCustomOp (org.nd4j.linalg.api.ops.DynamicCustomOp)26 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)15 NDArrayIndex.point (org.nd4j.linalg.indexing.NDArrayIndex.point)14 SDVariable (org.nd4j.autodiff.samediff.SDVariable)10 SameDiff (org.nd4j.autodiff.samediff.SameDiff)10 ArrayList (java.util.ArrayList)2 DifferentialFunction (org.nd4j.autodiff.functions.DifferentialFunction)2 Ignore (org.junit.Ignore)1 MMulTranspose (org.nd4j.linalg.api.blas.params.MMulTranspose)1 Mmul (org.nd4j.linalg.api.ops.impl.accum.Mmul)1 TruncateDivOp (org.nd4j.linalg.api.ops.impl.transforms.arithmetic.TruncateDivOp)1 GreaterThanOrEqual (org.nd4j.linalg.api.ops.impl.transforms.comparison.GreaterThanOrEqual)1 LessThanOrEqual (org.nd4j.linalg.api.ops.impl.transforms.comparison.LessThanOrEqual)1 OldMax (org.nd4j.linalg.api.ops.impl.transforms.comparison.OldMax)1 OldMin (org.nd4j.linalg.api.ops.impl.transforms.comparison.OldMin)1 BernoulliDistribution (org.nd4j.linalg.api.ops.random.impl.BernoulliDistribution)1 Pair (org.nd4j.linalg.primitives.Pair)1