Java Code Examples for org.nd4j.linalg.api.ndarray.INDArray#addRowVector()

The following examples show how to use org.nd4j.linalg.api.ndarray.INDArray#addRowVector() . 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: LayerOpValidation.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBiasAdd() {
    Nd4j.getRandom().setSeed(12345);

    SameDiff sameDiff = SameDiff.create();
    INDArray input = Nd4j.linspace(1, 8, 8, DataType.DOUBLE).reshape(new long[]{2, 4});
    INDArray b = Nd4j.linspace(1, 4, 4, DataType.DOUBLE).divi(4);

    SDVariable sdInput = sameDiff.var("input", input);
    SDVariable sdBias = sameDiff.var("bias", b);

    SDVariable res = sameDiff.nn().biasAdd(sdInput, sdBias, true);
    SDVariable loss = sameDiff.standardDeviation(res, true);

    INDArray exp = input.addRowVector(b);

    TestCase tc = new TestCase(sameDiff)
            .gradientCheck(true)
            .expectedOutput(res.name(), exp);

    String err = OpValidation.validate(tc);
    assertNull(err);
}
 
Example 2
Source File: SameDiffTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDenseLayerForwardPass() {
    Nd4j.getRandom().setSeed(12345);

    SameDiff sd = SameDiff.create();

    INDArray iInput = Nd4j.rand(3, 4);
    INDArray iWeights = Nd4j.rand(4, 5);
    INDArray iBias = Nd4j.rand(1, 5);

    SDVariable input = sd.var("input", iInput);
    SDVariable weights = sd.var("weights", iWeights);
    SDVariable bias = sd.var("bias", iBias);

    SDVariable mmul = sd.mmul("mmul", input, weights);
    SDVariable z = mmul.add("z", bias);
    SDVariable out = sd.sigmoid("out", z);

    INDArray expMmul = iInput.mmul(iWeights);
    INDArray expZ = expMmul.addRowVector(iBias);
    INDArray expOut = Transforms.sigmoid(expZ, true);

    sd.exec();

    assertEquals(expMmul, mmul.getArr());
    assertEquals(expZ, z.getArr());
    assertEquals(expOut, out.getArr());
}
 
Example 3
Source File: OpExecutionerTestsC.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddiRowVector() {
    INDArray arr = Nd4j.linspace(1, 6, 6).reshape(2, 3);
    INDArray arr2 = Nd4j.linspace(1, 3, 3);
    INDArray assertion = Nd4j.create(new double[] {2, 4, 6, 5, 7, 9}).reshape(2, 3);
    INDArray test = arr.addRowVector(arr2);
    assertEquals(assertion, test);
}
 
Example 4
Source File: OpExecutionerTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddBroadcast() {
    INDArray arr = Nd4j.linspace(1, 6, 6).reshape('f', 2, 3);
    INDArray arrRow = Nd4j.create(new double[] {1, 2, 3});
    INDArray assertion = Nd4j.create(new double[] {2, 3, 5, 6, 8, 9}, new int[] {2, 3}, 'f');
    INDArray add = arr.addRowVector(arrRow);
    assertEquals(assertion, add);

    INDArray colVec = Nd4j.linspace(1, 2, 2).reshape(2, 1);
    INDArray colAssertion = Nd4j.create(new double[] {2, 4, 4, 6, 6, 8}, new int[] {2, 3}, 'f');
    INDArray colTest = arr.addColumnVector(colVec);
    assertEquals(colAssertion, colTest);
}
 
Example 5
Source File: SameDiffTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDenseLayerForwardPass() {
    Nd4j.getRandom().setSeed(12345);

    SameDiff sd = SameDiff.create();

    INDArray iInput = Nd4j.rand(3, 4);
    INDArray iWeights = Nd4j.rand(4, 5);
    INDArray iBias = Nd4j.rand(1, 5);

    SDVariable input = sd.var("input", iInput);
    SDVariable weights = sd.var("weights", iWeights);
    SDVariable bias = sd.var("bias", iBias);

    SDVariable mmul = sd.mmul("mmul", input, weights);
    SDVariable z = mmul.add("z", bias);
    SDVariable out = sd.nn().sigmoid("out", z);

    INDArray expMmul = iInput.mmul(iWeights);
    INDArray expZ = expMmul.addRowVector(iBias);
    INDArray expOut = Transforms.sigmoid(expZ, true);

    Map<String,INDArray> m = sd.outputAll(Collections.emptyMap());

    assertEquals(expMmul, m.get(mmul.name()));
    assertEquals(expZ, m.get(z.name()));
    assertEquals(expOut, m.get(out.name()));
}
 
Example 6
Source File: OpExecutionerTestsC.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddiRowVector() {
    INDArray arr = Nd4j.linspace(1, 6, 6, DataType.DOUBLE).reshape(2, 3);
    INDArray arr2 = Nd4j.linspace(1, 3, 3, DataType.DOUBLE);
    INDArray assertion = Nd4j.create(new double[] {2, 4, 6, 5, 7, 9}).reshape(2, 3);
    INDArray test = arr.addRowVector(arr2);
    assertEquals(assertion, test);
}
 
Example 7
Source File: OpExecutionerTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddBroadcast() {
    INDArray arr = Nd4j.linspace(1, 6, 6, DataType.DOUBLE).reshape('f', 2, 3);
    INDArray arrRow = Nd4j.create(new double[] {1, 2, 3});
    INDArray assertion = Nd4j.create(new double[] {2, 3, 5, 6, 8, 9}, new int[] {2, 3}, 'f');
    INDArray add = arr.addRowVector(arrRow);
    assertEquals(assertion, add);

    INDArray colVec = Nd4j.linspace(1, 2, 2, DataType.DOUBLE).reshape(2, 1);
    INDArray colAssertion = Nd4j.create(new double[] {2, 4, 4, 6, 6, 8}, new int[] {2, 3}, 'f');
    INDArray colTest = arr.addColumnVector(colVec);
    assertEquals(colAssertion, colTest);
}
 
Example 8
Source File: TestSessions.java    From deeplearning4j with Apache License 2.0 3 votes vote down vote up
@Test
public void testInferenceSessionBasic(){
    //So far: trivial test to check execution order

    SameDiff sd = SameDiff.create();

    SDVariable ph1 = sd.placeHolder("x", DataType.FLOAT, 3,4);
    SDVariable ph2 = sd.placeHolder("y", DataType.FLOAT, 1,4);

    SDVariable out = ph1.add("out", ph2);

    //NOTE: normally sessions are internal and completely hidden from users

    InferenceSession is = new InferenceSession(sd);

    INDArray x = Nd4j.linspace(1, 12, 12).castTo(DataType.FLOAT).reshape(3,4);
    INDArray y = Nd4j.linspace(0.1, 0.4, 4, DataType.DOUBLE).castTo(DataType.FLOAT).reshape(1,4);

    INDArray outExp = x.addRowVector(y);

    Map<String,INDArray> m = new HashMap<>();
    m.put("x", x);
    m.put("y", y);

    Map<String,INDArray> outMap = is.output(Collections.singletonList("out"), m, null,
            Collections.<String>emptyList(), null, At.defaultAt(Operation.TRAINING));

    assertEquals(1, outMap.size());
    assertEquals(outExp, outMap.get("out"));
}