Search in sources :

Example 86 with SDVariable

use of org.nd4j.autodiff.samediff.SDVariable 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 87 with SDVariable

use of org.nd4j.autodiff.samediff.SDVariable in project nd4j by deeplearning4j.

the class GradCheckMisc method testExpandDimsGradient.

@Test
public void testExpandDimsGradient() {
    int[] origShape = new int[] { 3, 4 };
    boolean first = true;
    for (int i = 0; i < 3; i++) {
        int[] expExpandShape;
        switch(i) {
            case 0:
                expExpandShape = new int[] { 1, 3, 4 };
                break;
            case 1:
                expExpandShape = new int[] { 3, 1, 4 };
                break;
            case 2:
                expExpandShape = new int[] { 3, 4, 1 };
                break;
            default:
                throw new RuntimeException();
        }
        for (Pair<INDArray, String> p : NDArrayCreationUtil.getAllTestMatricesWithShape(origShape[0], origShape[1], 12345)) {
            INDArray inArr = p.getFirst().muli(100);
            SameDiff sd = SameDiff.create();
            SDVariable in = sd.var("in", inArr);
            SDVariable expand = sd.f().expandDims(in, i);
            // Using stdev here: mean/sum would backprop the same gradient for each input...
            SDVariable stdev = sd.standardDeviation("out", expand, true);
            INDArray out = sd.execAndEndResult();
            INDArray expOut = in.getArr().std(true, Integer.MAX_VALUE);
            assertEquals(expOut, out);
            assertArrayEquals(expExpandShape, expand.getArr().shape());
            INDArray expExpand = inArr.dup('c').reshape(expExpandShape);
            assertEquals(expExpand, expand.getArr());
            String msg = "expandDim=" + i + ", source=" + p.getSecond();
            log.info("Starting: " + msg);
            boolean ok = GradCheckUtil.checkGradients(sd);
            assertTrue(msg, ok);
        }
    }
}
Also used : SDVariable(org.nd4j.autodiff.samediff.SDVariable) INDArray(org.nd4j.linalg.api.ndarray.INDArray) SameDiff(org.nd4j.autodiff.samediff.SameDiff) Test(org.junit.Test)

Example 88 with SDVariable

use of org.nd4j.autodiff.samediff.SDVariable 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)

Example 89 with SDVariable

use of org.nd4j.autodiff.samediff.SDVariable in project nd4j by deeplearning4j.

the class GradCheckMisc method testSqueezeGradient.

@Test
public void testSqueezeGradient() {
    int[] origShape = new int[] { 3, 4, 5 };
    for (int i = 0; i < 3; i++) {
        int[] 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);
            int[] expShapePostSqueeze;
            switch(i) {
                case 0:
                    expShapePostSqueeze = new int[] { 4, 5 };
                    break;
                case 1:
                    expShapePostSqueeze = new int[] { 3, 5 };
                    break;
                case 2:
                    expShapePostSqueeze = new int[] { 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);
        }
    }
}
Also used : SDVariable(org.nd4j.autodiff.samediff.SDVariable) INDArray(org.nd4j.linalg.api.ndarray.INDArray) SameDiff(org.nd4j.autodiff.samediff.SameDiff) Test(org.junit.Test)

Example 90 with SDVariable

use of org.nd4j.autodiff.samediff.SDVariable in project nd4j by deeplearning4j.

the class LossFunctions method mse.

/**
 * Mean squared error: L = mean( (predicted - label)^2)
 *
 * @param outputName  Name of the output SDVariable
 * @param predictions Predictions variable
 * @param label       Label variable
 * @param weights     Weights array. May be null, or any broadcastable shape (with predictions/label arrays).
 *                    Note that this is also used for masking (weight of 0 = 'masked out')
 * @param reduction   Type of reduction to perform for the loss function
 * @param dimensions  Dimension(s) to apply the loss function on
 * @return LossInfo - bean with variables etc for the loss function
 */
public static LossInfo mse(String outputName, SDVariable predictions, SDVariable label, SDVariable weights, Reduction reduction, int... dimensions) {
    LossInfo.Builder b = validate("mse", predictions, label, reduction);
    SameDiff sd = predictions.getSameDiff();
    if (weights == null) {
        weights = sd.one("mse_loss_weights", SCALAR);
    }
    SDVariable diff = predictions.sub(label);
    String name = (reduction == Reduction.NONE ? outputName : null);
    SDVariable preReduceLoss = sd.square(diff).mul(name, weights);
    return doReduce(sd, outputName, true, b, reduction, preReduceLoss, label, weights, dimensions);
}
Also used : SDVariable(org.nd4j.autodiff.samediff.SDVariable) SameDiff(org.nd4j.autodiff.samediff.SameDiff)

Aggregations

SDVariable (org.nd4j.autodiff.samediff.SDVariable)104 SameDiff (org.nd4j.autodiff.samediff.SameDiff)41 INDArray (org.nd4j.linalg.api.ndarray.INDArray)38 Test (org.junit.Test)36 ArrayList (java.util.ArrayList)18 DynamicCustomOp (org.nd4j.linalg.api.ops.DynamicCustomOp)10 lombok.val (lombok.val)7 LossFunctions (org.nd4j.autodiff.loss.LossFunctions)4 LossInfo (org.nd4j.autodiff.loss.LossInfo)4 BernoulliDistribution (org.nd4j.linalg.api.ops.random.impl.BernoulliDistribution)4 Ignore (org.junit.Ignore)3 DifferentialFunction (org.nd4j.autodiff.functions.DifferentialFunction)3 ND4JIllegalStateException (org.nd4j.linalg.exception.ND4JIllegalStateException)3 Triple (org.nd4j.linalg.primitives.Triple)2 DataOutputStream (java.io.DataOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 NoOpNameFoundException (org.nd4j.imports.NoOpNameFoundException)1 NdIndexIterator (org.nd4j.linalg.api.iter.NdIndexIterator)1 TruncateDivOp (org.nd4j.linalg.api.ops.impl.transforms.arithmetic.TruncateDivOp)1