Search in sources :

Example 1 with GreaterThanOrEqual

use of org.nd4j.linalg.api.ops.impl.transforms.comparison.GreaterThanOrEqual in project nd4j by deeplearning4j.

the class GradCheckTransforms method testPairwiseTransforms.

@Test
public void testPairwiseTransforms() {
    /*
        add, sub, mul, div, rsub, rdiv
        eq, neq, gt, lt, gte, lte, or, and, xor
        min, max
        mmul
        tensormmul
         */
    // Test transforms (pairwise)
    Nd4j.getRandom().setSeed(12345);
    List<String> allSkipped = new ArrayList<>();
    List<String> allFailed = new ArrayList<>();
    for (int i = 0; i < 23; i++) {
        boolean skipBackward = false;
        SameDiff sd = SameDiff.create();
        int nOut = 4;
        int minibatch = 5;
        SDVariable in1 = sd.var("in1", new int[] { -1, nOut });
        SDVariable in2 = sd.var("in2", new int[] { -1, nOut });
        INDArray ia = Nd4j.randn(minibatch, nOut);
        INDArray ib = Nd4j.randn(minibatch, nOut);
        SDVariable t;
        INDArray expOut;
        switch(i) {
            case 0:
                t = in1.add(in2);
                expOut = ia.add(ib);
                break;
            case 1:
                t = in1.sub(in2);
                expOut = ia.sub(ib);
                break;
            case 2:
                t = in1.mul(in2);
                expOut = ia.mul(ib);
                break;
            case 3:
                // break;
                continue;
            case 4:
                t = in1.rsub(in2);
                expOut = ia.rsub(ib);
                break;
            case 5:
                t = in1.rdiv(in2);
                expOut = ia.rdiv(ib);
                break;
            case 6:
                t = sd.eq(in1, in2);
                expOut = ia.eq(ib);
                break;
            case 7:
                t = sd.neq(in1, in2);
                expOut = ia.neq(ib);
                break;
            case 8:
                t = sd.gt(in1, in2);
                expOut = ia.gt(ib);
                break;
            case 9:
                t = sd.lt(in1, in2);
                expOut = ia.lt(ib);
                break;
            case 10:
                t = sd.gte(in1, in2);
                expOut = ia.dup();
                Nd4j.getExecutioner().exec(new GreaterThanOrEqual(new INDArray[] { ia, ib }, new INDArray[] { expOut }));
                break;
            case 11:
                t = sd.lte(in1, in2);
                expOut = ia.dup();
                Nd4j.getExecutioner().exec(new LessThanOrEqual(new INDArray[] { ia, ib }, new INDArray[] { expOut }));
                break;
            case 12:
                ia = Nd4j.getExecutioner().exec(new BernoulliDistribution(ia, 0.5));
                ib = Nd4j.getExecutioner().exec(new BernoulliDistribution(ib, 0.5));
                t = sd.or(in1, in2);
                expOut = Transforms.or(ia, ib);
                break;
            case 13:
                ib = Nd4j.randn(nOut, nOut);
                t = sd.mmul(in1, in2);
                expOut = ia.mmul(ib);
                break;
            case 14:
                t = sd.max(in1, in2);
                expOut = Nd4j.getExecutioner().execAndReturn(new OldMax(ia, ib, ia.dup(), ia.length()));
                break;
            case 15:
                t = sd.min(in1, in2);
                expOut = Nd4j.getExecutioner().execAndReturn(new OldMin(ia, ib, ia.dup(), ia.length()));
                break;
            case 16:
                ia = Nd4j.getExecutioner().exec(new BernoulliDistribution(ia, 0.5));
                ib = Nd4j.getExecutioner().exec(new BernoulliDistribution(ib, 0.5));
                t = sd.and(in1, in2);
                expOut = Transforms.and(ia, ib);
                break;
            case 17:
                ia = Nd4j.getExecutioner().exec(new BernoulliDistribution(ia, 0.5));
                ib = Nd4j.getExecutioner().exec(new BernoulliDistribution(ib, 0.5));
                t = sd.xor(in1, in2);
                expOut = Transforms.xor(ia, ib);
                break;
            case 18:
                t = sd.assign(in1, in2);
                expOut = ib;
                break;
            case 19:
                t = sd.atan2(in1, in2);
                // Note: y,x order for samediff; x,y order for transforms
                expOut = Transforms.atan2(ib, ia);
                skipBackward = true;
                break;
            case 20:
                t = sd.mergeAdd(in1, in2, in2);
                expOut = ia.add(ib).add(ib);
                break;
            case 21:
                ia = Nd4j.create(new float[] { 2, 4 });
                ib = Nd4j.create(new float[] { 42, 2 });
                in1 = sd.var("in1", new int[] { 1, 2 });
                in2 = sd.var("in2", new int[] { 1, 2 });
                t = in1.truncatedDiv(in2);
                expOut = Nd4j.create(ia.shape(), ia.ordering());
                Nd4j.getExecutioner().exec(new TruncateDivOp(ia, ib, expOut));
                skipBackward = true;
                break;
            case 22:
                t = in1.squaredDifference(in2);
                expOut = Nd4j.create(ia.shape(), ia.ordering());
                DynamicCustomOp squareDiff = DynamicCustomOp.builder("squaredsubtract").addInputs(ia, ib).addOutputs(expOut).build();
                Nd4j.getExecutioner().exec(squareDiff);
                skipBackward = true;
                break;
            default:
                throw new RuntimeException();
        }
        DifferentialFunction[] funcs = sd.functions();
        String name = funcs[0].opName();
        String msg = "test: " + i + " - " + name;
        log.info("*** Starting test: " + msg);
        SDVariable loss = sd.mean("loss", t);
        sd.associateArrayWithVariable(ia, in1);
        sd.associateArrayWithVariable(ib, in2);
        sd.exec();
        INDArray out = t.getArr();
        assertEquals(msg, expOut, out);
        boolean ok;
        if (skipBackward) {
            ok = true;
            msg += " - SKIPPED";
            allSkipped.add(msg);
        } else {
            try {
                ok = GradCheckUtil.checkGradients(sd);
            } catch (Exception e) {
                e.printStackTrace();
                msg += " - EXCEPTION";
                ok = false;
            }
        }
        if (!ok) {
            allFailed.add(msg);
        }
    }
    if (allSkipped.size() > 0) {
        log.info("All backward skipped transforms: " + allSkipped);
        log.info(allSkipped.size() + " backward passes were skipped.");
    }
    if (allFailed.size() > 0) {
        log.error("All failed transforms: " + allFailed);
        fail(allFailed.size() + " transforms failed");
    }
}
Also used : LessThanOrEqual(org.nd4j.linalg.api.ops.impl.transforms.comparison.LessThanOrEqual) OldMin(org.nd4j.linalg.api.ops.impl.transforms.comparison.OldMin) SameDiff(org.nd4j.autodiff.samediff.SameDiff) ArrayList(java.util.ArrayList) GreaterThanOrEqual(org.nd4j.linalg.api.ops.impl.transforms.comparison.GreaterThanOrEqual) SDVariable(org.nd4j.autodiff.samediff.SDVariable) OldMax(org.nd4j.linalg.api.ops.impl.transforms.comparison.OldMax) INDArray(org.nd4j.linalg.api.ndarray.INDArray) BernoulliDistribution(org.nd4j.linalg.api.ops.random.impl.BernoulliDistribution) DifferentialFunction(org.nd4j.autodiff.functions.DifferentialFunction) DynamicCustomOp(org.nd4j.linalg.api.ops.DynamicCustomOp) TruncateDivOp(org.nd4j.linalg.api.ops.impl.transforms.arithmetic.TruncateDivOp) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 DifferentialFunction (org.nd4j.autodiff.functions.DifferentialFunction)1 SDVariable (org.nd4j.autodiff.samediff.SDVariable)1 SameDiff (org.nd4j.autodiff.samediff.SameDiff)1 INDArray (org.nd4j.linalg.api.ndarray.INDArray)1 DynamicCustomOp (org.nd4j.linalg.api.ops.DynamicCustomOp)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