Java Code Examples for org.nd4j.autodiff.samediff.SameDiff#create()

The following examples show how to use org.nd4j.autodiff.samediff.SameDiff#create() . 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: MiscOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testOneHot2() {

    INDArray indicesArr = Nd4j.createFromArray(0, 2, -1, 1);

    SameDiff sd = SameDiff.create();
    SDVariable indices = sd.constant("indices", indicesArr);
    int depth = 3;
    int axis = -1;
    SDVariable oneHot = sd.oneHot("oneHot", indices, depth, axis, 5.0, 0.0, DataType.DOUBLE);

    INDArray exp = Nd4j.create(new double[][]{{5, 0, 0}, {0,0,5}, {0,0,0}, {0, 5, 0}});

    String err = OpValidation.validate(new TestCase(sd)
            .expected(oneHot, exp)
            .gradientCheck(false));

    assertNull(err);
}
 
Example 2
Source File: LayerOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void exceptionThrown_WhenConv2DConfigInvalid() {

    Nd4j.getRandom().setSeed(12345);

    SameDiff sd = SameDiff.create();
    SDVariable in = null;

    int[] inSizeNCHW = {1, 3, 8, 8};

    String msg = "0 - conv2d+bias, nchw - input " + Arrays.toString(inSizeNCHW);
    SDVariable w0 = sd.var("w0", Nd4j.rand(new int[]{3, 3, inSizeNCHW[1], 3}).muli(10));  //kH,kW,iC,oC
    SDVariable b0 = sd.var("b0", Nd4j.rand(new long[]{3}).muli(10));
    SDVariable out = sd.cnn().conv2d(in, w0, b0, Conv2DConfig.builder()
            .dataFormat(Conv2DConfig.NCHW)
            .isSameMode(true)
            .kH(3).kW(-3)
            .sH(1).sW(0)
            .build());
}
 
Example 3
Source File: LayerOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testMaxPooling3dBasic() {
    int nIn = 3;
    int kH = 2;
    int kW = 2;
    int kD = 2;

    int mb = 3;
    int imgH = 5;
    int imgW = 5;
    int imgD = 5;

    SameDiff sd = SameDiff.create();
    INDArray inArr = Nd4j.create(mb, nIn, imgD, imgH, imgW);

    SDVariable in = sd.var("in", inArr);

    Pooling3DConfig pooling3DConfig = Pooling3DConfig.builder()
            .kH(kH).kW(kW).kD(kD)
            .pH(0).pW(0).pD(0)
            .sH(1).sW(1).sD(1)
            .dH(1).dW(1).dD(1)
            .isSameMode(false)
            .build();

    SDVariable out = sd.cnn().maxPooling3d(in, pooling3DConfig);
    out = sd.nn().tanh("loss", out).shape().rename("out");

    sd.setLossVariables("loss");

    // oH = (iH - (kH + (kH-1)*(dH-1)) + 2*pH)/sH + 1;
    INDArray outArr = Nd4j.createFromArray(mb, nIn, 4, 4, 4L);

    TestCase tc = new TestCase(sd).expectedOutput("out", outArr).gradientCheck(false);
    String err = OpValidation.validate(tc);
    assertNull(err);
}
 
Example 4
Source File: TransformOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCross() {
    INDArray a = Nd4j.create(new double[]{4, 2, 1}, new int[]{1, 3});
    INDArray b = Nd4j.create(new double[]{1, 3, 4}, new int[]{1, 3});

    INDArray expOut = Nd4j.create(DataType.DOUBLE, 1, 3);

    val op = new Cross(a, b, expOut);
    Nd4j.getExecutioner().exec(op);

    SameDiff sd = SameDiff.create();

    SDVariable sdA = sd.var("a", expOut.shape());
    SDVariable sdB = sd.var("b", expOut.shape());


    sd.associateArrayWithVariable(a, sdA);
    sd.associateArrayWithVariable(b, sdB);

    SDVariable t = sd.math().cross("cross", sdA, sdB);
    SDVariable loss = sd.mean("loss", t);

    String err = OpValidation.validate(new TestCase(sd)
            .expectedOutput("cross", expOut)
            .gradientCheck(true));
    assertNull(err, err);
}
 
Example 5
Source File: LayerOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDepthwiseConv2D(){

    int bS = 10;

    int kernelHeight = 2;
    int kernelWidth = 2;
    int strideHeight = 2;
    int strideWidth = 2;
    int inChannels = 2;
    int outChannels = 3;
    Nd4j.getRandom().setSeed(12345);
    SameDiff sd = SameDiff.create();
    SDVariable in = sd.var("in", Nd4j.rand(bS, inChannels, 5,5));
    SDVariable weights = sd.var("weights", Nd4j.rand(DataType.DOUBLE, kernelHeight, kernelWidth, inChannels, outChannels));
    SDVariable bias = sd.var("bias", Nd4j.rand(DataType.DOUBLE, inChannels*outChannels));
    Conv2DConfig config = Conv2DConfig.builder()
            .kH(kernelHeight)
            .kW(kernelWidth)
            .sH(strideHeight)
            .sW(strideWidth)
            .dataFormat("NCHW")
            .build();

    SDVariable out = sd.cnn.depthWiseConv2d(in, weights, bias, config);
    SDVariable loss = sd.standardDeviation("loss", out, true);
    loss.markAsLoss();

    String err = OpValidation.validate(new TestCase(sd)
            .gradientCheck(true)
    );
    assertNull(err);



}
 
Example 6
Source File: LayerOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testLayerNorm4d() {
    int mb = 3;
    int ch = 4;
    for (boolean nchw : new boolean[]{true, false}) {
        double eps = 0.0;
        INDArray x = Nd4j.rand(DataType.DOUBLE, nchw ? new long[]{mb, ch, 8, 8} : new long[]{mb, 8, 8, ch});
        INDArray gain4d = Nd4j.rand(DataType.DOUBLE, nchw ? new long[]{1, ch, 1, 1} : new long[]{1, 1, 1, ch});
        INDArray bias4d = Nd4j.rand(DataType.DOUBLE, nchw ? new long[]{1, ch, 1, 1} : new long[]{1, 1, 1, ch});
        INDArray mean = x.mean(true, 1, 2, 3);
        INDArray std = Transforms.sqrt(x.var(false, 1, 2, 3).addi(eps)).reshape(mb, 1, 1, 1);

        INDArray standardized = x.sub(mean).div(std);
        INDArray exp = standardized.mul(gain4d).add(bias4d);

        final int[] axis = new int[]{1, 2, 3};
        SameDiff sd = SameDiff.create();
        SDVariable sdInput = sd.var("input", x);
        SDVariable sdGain = sd.var("gain", gain4d.reshape(ch));
        SDVariable sdBias = sd.var("bias", bias4d.reshape(ch));
        SDVariable out = sd.nn.layerNorm("layernorm", sdInput, sdGain, sdBias, nchw, axis);

        SDVariable loss = sd.loss.l2Loss(out);

        String err = OpValidation.validate(new TestCase(sd)
                .expectedOutput("layernorm", exp)
                .gradientCheck(true));
        assertNull(err);
    }
}
 
Example 7
Source File: LossOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void TestStdLossMixedDataType(){
    // Default Data Type in this test suite is Double.
    // This test used to throw an Exception that we have mixed data types.

    SameDiff sd = SameDiff.create();
    SDVariable v = sd.placeHolder("x", DataType.FLOAT, 3,4);
    SDVariable loss = v.std(true);
}
 
Example 8
Source File: LayerOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testLayerNormNoDeviation() {
    final INDArray random = Nd4j.rand(DataType.DOUBLE, 10, 4);
    for (int i = 0; i < 4; i++) {
        random.putScalar(1, i, 7);
    }

    final INDArray standardized = random.ulike();
    Nd4j.getExecutioner().exec(new Standardize(random, standardized, 1));

    final INDArray gain = Nd4j.rand(DataType.DOUBLE, 4);
    final INDArray bias = Nd4j.rand(DataType.DOUBLE, 4);
    final INDArray res = standardized.mulRowVector(gain).addRowVector(bias);
    final INDArray expOut = res.norm1();

    final int[] axis = new int[]{1};
    SameDiff sd = SameDiff.create();
    SDVariable sdInput = sd.var("input", standardized);
    SDVariable sdGain = sd.var("gain", gain);
    SDVariable sdBias = sd.var("bias", bias);
    SDVariable out = sd.nn.layerNorm(sdInput, sdGain, sdBias, true, axis);
    out.norm1("out");

    String err = OpValidation.validate(new TestCase(sd)
            .expectedOutput("out", expOut)
            .gradCheckMask(Collections.singletonMap("input", random.neq(7)))
            .gradientCheck(true));
    assertNull(err, err);
}
 
Example 9
Source File: ReductionOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDotProductAttentionWeirdInputs(){
    final INDArray keys = Nd4j.rand(new int[]{10, 4, 3});
    final INDArray values = Nd4j.rand(new int[]{10, 4, 3});
    final INDArray query = Nd4j.rand(new int[]{10, 4, 1});
    final INDArray mask = Nd4j.rand(10, 3).gte(0.2).castTo(DataType.DOUBLE);

    final INDArray exec = Nd4j.matmul(keys, query, true, false, false)
            .divi(Math.sqrt(keys.size(1)));
    exec.addi(mask.reshape(10, 3, 1).sub(1).muli(1e9));
    Nd4j.exec((CustomOp) new SoftMax(exec, exec, 1));
    final INDArray finalOut = Nd4j.matmul(values, exec).norm1();

    for (char queryOrder : new char[]{'f', 'c'}) {
        for (char keyOrder : new char[]{'f', 'c'}) {
            for (char valueOrder : new char[]{'f', 'c'}) {
                log.info("-*- Starting Test: query order = {}, key order = {}, value order = {}-*-", queryOrder, keyOrder, valueOrder);
                SameDiff sd = SameDiff.create();
                SDVariable sdQ = sd.var("q", query.dup(queryOrder));
                SDVariable sdK = sd.var("k", keys.dup(keyOrder));
                SDVariable sdV = sd.var("v", values.dup(valueOrder));
                SDVariable sdMask = sd.constant("mask", mask);

                SDVariable t = sd.nn.dotProductAttention(sdQ, sdK, sdV, sdMask, true);
                t.norm1("out").markAsLoss();

                String err = OpValidation.validate(new TestCase(sd)
                        .expectedOutput("out", finalOut)
                        .gradientCheck(true)
                        .gradCheckPrint(false)
                        .gradCheckSkipVariables("mask"));
                assertNull(err);
            }
        }
    }
}
 
Example 10
Source File: SameDiffInferenceExecutionerTests.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)

    public void testSameDiff() throws Exception {
        SameDiffInferenceExecutioner sameDiffInferenceExecutioner = new SameDiffInferenceExecutioner();
        SameDiff sameDiff = SameDiff.create();
        SDVariable input1 = sameDiff.placeHolder("input1", DataType.FLOAT,2, 2);
        SDVariable input2 = sameDiff.placeHolder("input2", DataType.FLOAT,2, 2);
        SDVariable result = input1.add("output", input2);
        INDArray input1Arr = Nd4j.linspace(1, 4, 4).reshape(2, 2);
        INDArray input2Arr = Nd4j.linspace(1, 4, 4).reshape(2, 2);
        sameDiff.associateArrayWithVariable(input1Arr, input1.name());
        sameDiff.associateArrayWithVariable(input2Arr, input2.name());
        Map<String, INDArray> indArrays = new LinkedHashMap<>();
        indArrays.put(input1.name(), input1Arr);
        indArrays.put(input2.name(), input2Arr);
        Map<String, INDArray> outputs = sameDiff.outputAll(indArrays);
        assertEquals(3, outputs.size());

        ParallelInferenceConfig parallelInferenceConfig = ParallelInferenceConfig.defaultConfig();
        File newFile = temporary.newFile();
        sameDiff.asFlatFile(newFile);
        SameDiffModelLoader sameDiffModelLoader = new SameDiffModelLoader(newFile, Arrays.asList("input1", "input2"), Arrays.asList("output"));


        sameDiffInferenceExecutioner.initialize(sameDiffModelLoader, parallelInferenceConfig);


        INDArray[] execute = sameDiffInferenceExecutioner.execute(new INDArray[]{input1Arr, input2Arr});
        assertEquals(outputs.values().iterator().next(), execute[0]);
    }
 
Example 11
Source File: GraphExecutionerTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void testSums1() throws Exception {
    SameDiff sameDiff = SameDiff.create();
    INDArray ones = Nd4j.ones(4);
    SDVariable sdVariable = sameDiff.var("ones",ones);
    SDVariable result = sdVariable.addi(1.0);
    SDVariable total = sameDiff.sum(result,Integer.MAX_VALUE);

    val executioner = new NativeGraphExecutioner();

    INDArray[] res = executioner.executeGraph(sameDiff);
    assertEquals(8.0, res[0].getDouble(0), 1e-5);
}
 
Example 12
Source File: MiscOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testLu() {

    SameDiff sameDiff = SameDiff.create();

    INDArray in1 = Nd4j.createFromArray(new double[]{
            1., 2., 3., 0., 2., 3., 0., 0., 7.
    }).reshape(3,3);

    SDVariable input1 = sameDiff.var(in1);

    INDArray expected = Nd4j.createFromArray(new double[]{
            1., 2., 3., 0., 2., 3., 0., 0., 7
    }).reshape(3,3);

    INDArray pexpected = Nd4j.createFromArray(new int[]{
            0, 1, 2
    });

    sameDiff.loss.l2Loss(input1);
    SDVariable[] output = new Lu(sameDiff, input1).outputVariables();

    TestCase tc = new TestCase(sameDiff)
            .gradientCheck(true)
            .expectedOutput(output[0].name(), expected)
            .expectedOutput(output[1].name(), pexpected);

    String err = OpValidation.validate(tc);
    assertNull(err);
}
 
Example 13
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSize() {
    SameDiff sameDiff = SameDiff.create();
    val shape = new long[]{2, 3};
    SDVariable x = sameDiff.var("x", DataType.FLOAT, shape);
    SDVariable result = sameDiff.size(x);

    String err = OpValidation.validate(new TestCase(sameDiff)
            .gradientCheck(false)
            .expected(result, Nd4j.scalar(6L)));

    assertNull(err);
}
 
Example 14
Source File: MiscOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testShapeFn2() {

    INDArray i = Nd4j.create(1,3);

    SameDiff sd = SameDiff.create();
    SDVariable var = sd.var("in", i);
    SDVariable shape = sd.shape(var);
    SDVariable sum = shape.castTo(DataType.DOUBLE).sum();
    sum.eval();
}
 
Example 15
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testTriOp() {

    SameDiff sd = SameDiff.create();
    SDVariable out = new Tri(sd, DataType.INT32, 3, 5, 2).outputVariable();
    INDArray expected = Nd4j.createFromArray(new int[][]{{1, 1, 1, 0, 0}, {1, 1, 1, 1, 0}, {1, 1, 1, 1, 1}});
    String err = OpValidation.validate(new TestCase(sd)
            .expectedOutput("tri", expected)
            .gradientCheck(false));
    assertNull(err);
}
 
Example 16
Source File: SameDiffVerticleClassificationMetricsTest.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
@Override
public JsonObject getConfigObject() throws Exception {
    SameDiff sameDiff = SameDiff.create();
    SDVariable x = sameDiff.placeHolder("x", DataType.FLOAT, 2);
    SDVariable y = sameDiff.placeHolder("y", DataType.FLOAT, 2);
    SDVariable add = x.add("output", y);
    File tmpSameDiffFile = temporary.newFile();
    sameDiff.asFlatFile(tmpSameDiffFile);
    SameDiff values = SameDiff.fromFlatFile(tmpSameDiffFile);

    ServingConfig servingConfig = ServingConfig.builder()
            .outputDataFormat(Output.DataFormat.ND4J)
            .metricsConfigurations(Collections.singletonList(ClassificationMetricsConfig.builder()
                    .classificationLabels(Arrays.asList("0", "1")).build()))
            .metricTypes(Collections.singletonList(MetricType.CLASSIFICATION))
            .httpPort(port)
            .build();

    SameDiffStep modelPipelineConfig = SameDiffStep.builder()
            .path(tmpSameDiffFile.getAbsolutePath())
            .inputNames(Arrays.asList("x", "y"))
            .outputNames(Collections.singletonList("output"))
            .build();

    InferenceConfiguration inferenceConfiguration = InferenceConfiguration.builder()
            .servingConfig(servingConfig)
            .step(modelPipelineConfig)
            .build();

    return new JsonObject(inferenceConfiguration.toJson());
}
 
Example 17
Source File: JsonModelServerTest.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testStartStopParallel() throws Exception {
    val sd = SameDiff.create();
    val sdVariable = sd.placeHolder("input", DataType.INT, 1,4);
    val result = sdVariable.add(1.0);
    val total = result.mean("total", Integer.MAX_VALUE);

    val serverDL = new JsonModelServer.Builder<House, PredictedPrice>(model)
            .outputSerializer(new PredictedPrice.PredictedPriceSerializer())
            .inputDeserializer(new House.HouseDeserializer())
            .numWorkers(1)
            .inferenceMode(SEQUENTIAL)
            .inferenceAdapter(new HouseToPredictedPriceAdapter())
            .port(PORT)
            .build();

    val serverSD = new JsonModelServer.Builder<House, PredictedPrice>(sd)
            .outputSerializer(new PredictedPrice.PredictedPriceSerializer())
            .inputDeserializer(new House.HouseDeserializer())
            .inferenceAdapter(new HouseToPredictedPriceAdapter())
            .orderedInputNodes(new String[]{"input"})
            .orderedOutputNodes(new String[]{"total"})
            .port(PORT+1)
            .build();
    try {
        serverDL.start();
        serverSD.start();

        val clientDL = JsonRemoteInference.<House, PredictedPrice>builder()
                .inputSerializer(new House.HouseSerializer())
                .outputDeserializer(new PredictedPrice.PredictedPriceDeserializer())
                .endpointAddress("http://localhost:" + PORT  + "/v1/serving")
                .build();

        int district = 2;
        House house = House.builder().area(100).bathrooms(2).bedrooms(3).district(district).build();
        PredictedPrice price = clientDL.predict(house);
        long timeStart = System.currentTimeMillis();
        price = clientDL.predict(house);
        long timeStop = System.currentTimeMillis();
        log.info("Time spent: {} ms", timeStop - timeStart);
        assertNotNull(price);
        assertEquals((float) 0.421444, price.getPrice(), 1e-5);

        val clientSD = JsonRemoteInference.<House, PredictedPrice>builder()
                .inputSerializer(new House.HouseSerializer())
                .outputDeserializer(new PredictedPrice.PredictedPriceDeserializer())
                .endpointAddress("http://localhost:" + (PORT+1)  + "/v1/serving")
                .build();

        PredictedPrice price2 = clientSD.predict(house);
        timeStart = System.currentTimeMillis();
        price = clientSD.predict(house);
        timeStop = System.currentTimeMillis();
        log.info("Time spent: {} ms", timeStop - timeStart);
        assertNotNull(price);
        assertEquals((float) 3.0, price.getPrice(), 1e-5);

    }
    finally {
        serverSD.stop();
        serverDL.stop();
    }
}
 
Example 18
Source File: ValidationUtilTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testValidateSameDiff() throws Exception {
        Nd4j.setDataType(DataType.FLOAT);

        File f = testDir.newFolder();
        SameDiff sd = SameDiff.create();
        SDVariable v = sd.placeHolder("x", DataType.FLOAT, 3,4);
        SDVariable loss = v.std(true);

        File fOrig = new File(f, "sd_fb.fb");
        sd.asFlatFile(fOrig);;


        //Test not existent file:
        File fNonExistent = new File("doesntExist.fb");
        ValidationResult vr0 = Nd4jValidator.validateSameDiffFlatBuffers(fNonExistent);
        assertFalse(vr0.isValid());
        assertEquals("SameDiff FlatBuffers file", vr0.getFormatType());
        assertTrue(vr0.getIssues().get(0), vr0.getIssues().get(0).contains("exist"));
//        System.out.println(vr0.toString());

        //Test empty file:
        File fEmpty = new File(f, "empty.fb");
        fEmpty.createNewFile();
        assertTrue(fEmpty.exists());
        ValidationResult vr1 = Nd4jValidator.validateSameDiffFlatBuffers(fEmpty);
        assertEquals("SameDiff FlatBuffers file", vr1.getFormatType());
        assertFalse(vr1.isValid());
        assertTrue(vr1.getIssues().get(0), vr1.getIssues().get(0).contains("empty"));
//        System.out.println(vr1.toString());

        //Test directory (not zip file)
        File directory = new File(f, "dir");
        boolean created = directory.mkdir();
        assertTrue(created);
        ValidationResult vr2 = Nd4jValidator.validateSameDiffFlatBuffers(directory);
        assertEquals("SameDiff FlatBuffers file", vr2.getFormatType());
        assertFalse(vr2.isValid());
        assertTrue(vr2.getIssues().get(0), vr2.getIssues().get(0).contains("directory"));
//        System.out.println(vr2.toString());

        //Test non-flatbuffers
        File fText = new File(f, "text.fb");
        FileUtils.writeStringToFile(fText, "Not a flatbuffers file :)", StandardCharsets.UTF_8);
        ValidationResult vr3 = Nd4jValidator.validateSameDiffFlatBuffers(fText);
        assertEquals("SameDiff FlatBuffers file", vr3.getFormatType());
        assertFalse(vr3.isValid());
        String s = vr3.getIssues().get(0);
        assertTrue(s, s.contains("FlatBuffers") && s.contains("SameDiff") && s.contains("corrupt"));
//        System.out.println(vr3.toString());

        //Test corrupted flatbuffers format:
        byte[] fbBytes = FileUtils.readFileToByteArray(fOrig);
        for( int i=0; i<30; i++ ){
            fbBytes[i] = (byte)('a' + i);
        }
        File fCorrupt = new File(f, "corrupt.fb");
        FileUtils.writeByteArrayToFile(fCorrupt, fbBytes);

        ValidationResult vr4 = Nd4jValidator.validateSameDiffFlatBuffers(fCorrupt);
        assertEquals("SameDiff FlatBuffers file", vr4.getFormatType());
        assertFalse(vr4.isValid());
        s = vr4.getIssues().get(0);
        assertTrue(s, s.contains("FlatBuffers") && s.contains("SameDiff") && s.contains("corrupt"));
//        System.out.println(vr4.toString());


        //Test valid npz format:
        ValidationResult vr5 = Nd4jValidator.validateSameDiffFlatBuffers(fOrig);
        assertEquals("SameDiff FlatBuffers file", vr5.getFormatType());
        assertTrue(vr5.isValid());
        assertNull(vr5.getIssues());
        assertNull(vr5.getException());
//        System.out.println(vr4.toString());
    }
 
Example 19
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testStridedSliceGradient() {
    Nd4j.getRandom().setSeed(12345);

    //Order here: original shape, begin, size
    List<SSCase> testCases = new ArrayList<>();
    testCases.add(SSCase.builder().shape(3, 4).begin(0, 0).end(3, 4).strides(1, 1).build());
    testCases.add(SSCase.builder().shape(3, 4).begin(1, 1).end(2, 3).strides(1, 1).build());
    testCases.add(SSCase.builder().shape(3, 4).begin(-999, 0).end(3, 4).strides(1, 1).beginMask(1).build());
    testCases.add(SSCase.builder().shape(3, 4).begin(1, 1).end(3, -999).strides(1, 1).endMask(1 << 1).build());
    testCases.add(SSCase.builder().shape(3, 4).begin(-999, 0).end(-999, 4).strides(1, 1).beginMask(1).endMask(1).build());

    testCases.add(SSCase.builder().shape(3, 4, 5).begin(0, 0, 0).end(3, 4, 5).strides(1, 1, 1).build());
    testCases.add(SSCase.builder().shape(3, 4, 5).begin(1, 2, 3).end(3, 4, 5).strides(1, 1, 1).build());
    testCases.add(SSCase.builder().shape(3, 4, 5).begin(0, 0, 0).end(3, 3, 5).strides(1, 2, 2).build());
    testCases.add(SSCase.builder().shape(3, 4, 5).begin(1, -999, 1).end(3, 3, 4).strides(1, 1, 1).beginMask(1 << 1).build());
    testCases.add(SSCase.builder().shape(3, 4, 5).begin(1, -999, 1).end(3, 3, -999).strides(1, 1, 1).beginMask(1 << 1).endMask(1 << 2).build());
    testCases.add(SSCase.builder().shape(3, 4, 5).begin(1, 2).end(3, 4).strides(1, 1).ellipsisMask(1 << 1).build());   //[1:3,...,2:4]
    testCases.add(SSCase.builder().shape(3, 4, 5).begin(1, -999, 1, 2).end(3, -999, 3, 4).strides(1, -999, 1, 2).newAxisMask(1 << 1).build());
    testCases.add(SSCase.builder().shape(3, 4, 5).begin(1, 0, 1).end(3, -999, 4).strides(1, 1, 1).shrinkAxisMask(1 << 1).build());
    testCases.add(SSCase.builder().shape(3, 4, 5).begin(1, 1, 1).end(3, -999, 4).strides(1, 1, 1).shrinkAxisMask(1 << 1).build());

    Map<Integer,INDArrayIndex[]> indices = new HashMap<>();
    indices.put(0, new INDArrayIndex[]{all(), all()});
    indices.put(1, new INDArrayIndex[]{interval(1,2), interval(1,3)});
    indices.put(2, new INDArrayIndex[]{interval(0,3), interval(0,4)});
    indices.put(3, new INDArrayIndex[]{interval(1,3), interval(1,4)});

    indices.put(5, new INDArrayIndex[]{all(), all(), all()});
    indices.put(7, new INDArrayIndex[]{interval(0,1,3), interval(0,2,3), interval(0,2,5)});


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

    for (int i = 0; i < testCases.size(); i++) {
        SSCase t = testCases.get(i);
        INDArray arr = Nd4j.rand(t.getShape());

        SameDiff sd = SameDiff.create();
        SDVariable in = sd.var("in", arr);
        SDVariable slice = sd.stridedSlice(in, t.getBegin(), t.getEnd(), t.getStrides(), t.getBeginMask(),
                t.getEndMask(), t.getEllipsisMask(), t.getNewAxisMask(), t.getShrinkAxisMask());
        SDVariable stdev = sd.standardDeviation(slice, true);

        String msg = "i=" + i + ": " + t;
        log.info("Starting test: " + msg);

        TestCase tc = new TestCase(sd);
        tc.testName(msg);

        if(indices.containsKey(i)){
            tc.expected(slice, arr.get(indices.get(i)).dup());
        }

        String error = OpValidation.validate(tc, true);
        if(error != null){
            failed.add(error);
        }
    }
    assertEquals(failed.toString(), 0, failed.size());
}
 
Example 20
Source File: ShapeOpValidation.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
    public void testDistancesExec(){
        //https://github.com/deeplearning4j/deeplearning4j/issues/7001
        for(String s : new String[]{"euclidean", "manhattan", "cosinesim", "cosinedist", "jaccard"}) {
            log.info("Starting: {}", s);
            INDArray defaultTestCase = Nd4j.create(4, 4);
            defaultTestCase.putRow(0, Nd4j.create(new float[]{0, 2, -2, 0}));
            defaultTestCase.putRow(1, Nd4j.create(new float[]{0, 1, -1, 0}));
            defaultTestCase.putRow(2, Nd4j.create(new float[]{0, -1, 1, 0}));
            defaultTestCase.putRow(3, Nd4j.create(new float[]{0, -2, 2, 0}));
            long singleEmbeddingSize = defaultTestCase.size(1) / 2L;

            // Split vectors
            INDArray x = defaultTestCase.get(NDArrayIndex.all(), NDArrayIndex.interval(0, singleEmbeddingSize));
            INDArray y = defaultTestCase.get(NDArrayIndex.all(), NDArrayIndex.interval(singleEmbeddingSize, defaultTestCase.size(1)));

            log.info(y.shapeInfoToString());

            SameDiff sd = SameDiff.create();
            sd.enableDebugMode();

            SDVariable xSd = sd.var("x", x);
            SDVariable ySd = sd.var("y", y);

            ySd = ySd.add(ySd);
            SDVariable dist;
            switch (s){
                case "euclidean":
                    dist = sd.math().euclideanDistance(s, ySd, xSd, 0);
                    break;
                case "manhattan":
                    dist = sd.math().manhattanDistance(s, ySd, xSd, 0);
                    break;
                case "cosinesim":
                    dist = sd.math().cosineSimilarity(s, ySd, xSd, 0);
                    break;
                case "cosinedist":
                    dist = sd.math().cosineDistance(s, ySd, xSd, 0);
                    break;
                case "jaccard":
                    dist = sd.math().jaccardDistance(s, ySd, xSd, 0);
                    break;
                default:
                    throw new RuntimeException();
            }

            SDVariable loss = dist.sum();


//            log.info(sd.summary());
            sd.output(Collections.emptyMap(), Lists.newArrayList(s));
            sd.calculateGradients(Collections.emptyMap(), sd.getVariables().keySet());
        }
    }