Java Code Examples for org.nd4j.autodiff.samediff.SDVariable#addi()

The following examples show how to use org.nd4j.autodiff.samediff.SDVariable#addi() . 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: GraphExecutionerTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * VarSpace should dump everything. 4 variables in our case
 * @throws Exception
 */
@Test
public void testEquality1() throws Exception {
    GraphExecutioner executionerA = new BasicGraphExecutioner();
    GraphExecutioner executionerB = new NativeGraphExecutioner();

    SameDiff sameDiff = SameDiff.create();
    INDArray ones = Nd4j.ones(4);
    SDVariable sdVariable = sameDiff.var("ones",ones);
    SDVariable scalarOne = sameDiff.var("add1",Nd4j.scalar(1.0));
    SDVariable result = sdVariable.addi(scalarOne);
    SDVariable total = sameDiff.sum(result,Integer.MAX_VALUE);

    log.info("TOTAL: {}; Id: {}", total.getVarName(), total);

    INDArray[] resB = executionerB.executeGraph(sameDiff, configVarSpace);

    assertEquals(6, resB.length);
    assertEquals(Nd4j.create(new float[]{2f, 2f, 2f, 2f}), resB[4]);
    assertEquals(Nd4j.scalar(1), resB[1]);
    assertEquals(Nd4j.scalar(8.0), resB[5]);
}
 
Example 2
Source File: GraphExecutionerTest.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
     * Implicit should return tree edges. So, one variable
     * @throws Exception
     */
    @Test
    public void testEquality2() throws Exception {
        GraphExecutioner executionerA = new BasicGraphExecutioner();
        GraphExecutioner executionerB = new NativeGraphExecutioner();

        SameDiff sameDiff = SameDiff.create();
        INDArray ones = Nd4j.ones(4);
        SDVariable sdVariable = sameDiff.var("ones",ones);
        SDVariable scalarOne = sameDiff.var("add1",Nd4j.scalar(1.0));
        SDVariable result = sdVariable.addi(scalarOne);
        SDVariable total = sameDiff.sum(result,Integer.MAX_VALUE);

//        log.info("ID: {}",sameDiff.getGraph().getVertex(1).getValue().getId());

        INDArray[] resB = executionerB.executeGraph(sameDiff, configImplicit);

        assertEquals(1, resB.length);
        assertEquals(Nd4j.scalar(8.0), resB[0]);

        //INDArray resA = executionerA.executeGraph(sameDiff)[0];

        //assertEquals(resA, resB);
    }
 
Example 3
Source File: GraphExecutionerTest.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void testConversion() 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();

    ByteBuffer buffer = executioner.convertToFlatBuffers(sameDiff, ExecutorConfiguration.builder().profilingMode(OpExecutioner.ProfilingMode.DISABLED).executionMode(ExecutionMode.SEQUENTIAL).outputMode(OutputMode.IMPLICIT).build());

    val offset = buffer.position();
    val array = buffer.array();

    try (val fos = new FileOutputStream("../../libnd4j/tests/resources/adam_sum.fb"); val dos = new DataOutputStream(fos)) {
        dos.write(array, offset, array.length - offset);
    }


    //INDArray[] res = executioner.executeGraph(sameDiff);
    //assertEquals(8.0, res[0].getDouble(0), 1e-5);
    /*
    INDArray output = null;
    for(int i = 0; i < 5; i++) {
        output = sameDiff.execAndEndResult(ops);
        System.out.println("Ones " + ones);
        System.out.println(output);
    }

    assertEquals(Nd4j.valueArrayOf(4,7),ones);
    assertEquals(28,output.getDouble(0),1e-1);
    */
}
 
Example 4
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);
}