Java Code Examples for org.nd4j.linalg.factory.Nd4j#createFromFlatArray()

The following examples show how to use org.nd4j.linalg.factory.Nd4j#createFromFlatArray() . 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: ByteOrderTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testStringEncoding_1() {
    val strings = Arrays.asList("alpha", "beta", "gamma");
    val vector = Nd4j.create(strings, 3);

    val bufferBuilder = new FlatBufferBuilder(0);

    val fb = vector.toFlatArray(bufferBuilder);
    bufferBuilder.finish(fb);
    val db = bufferBuilder.dataBuffer();

    val flat = FlatArray.getRootAsFlatArray(db);

    val restored = Nd4j.createFromFlatArray(flat);

    assertEquals(vector, restored);
}
 
Example 2
Source File: StringArrayTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasicStrings_4() {
    val arrayX = Nd4j.create("alpha", "beta", "gamma");

    val fb = new FlatBufferBuilder();
    val i = arrayX.toFlatArray(fb);
    fb.finish(i);
    val db = fb.dataBuffer();

    val flat = FlatArray.getRootAsFlatArray(db);
    val restored = Nd4j.createFromFlatArray(flat);

    assertEquals(arrayX, restored);
    assertEquals("alpha", restored.getString(0));
    assertEquals("beta", restored.getString(1));
    assertEquals("gamma", restored.getString(2));
}
 
Example 3
Source File: StringArrayTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasicStrings_4a() {
    val arrayX = Nd4j.scalar("alpha");

    val fb = new FlatBufferBuilder();
    val i = arrayX.toFlatArray(fb);
    fb.finish(i);
    val db = fb.dataBuffer();

    val flat = FlatArray.getRootAsFlatArray(db);
    val restored = Nd4j.createFromFlatArray(flat);

    assertEquals("alpha", arrayX.getString(0));

    assertEquals(arrayX, restored);
    assertEquals("alpha", restored.getString(0));
}
 
Example 4
Source File: ByteOrderTests.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testByteArrayOrder2() {
    val original = Nd4j.linspace(1, 25, 25, DataType.FLOAT).reshape(5, 5);
    val bufferBuilder = new FlatBufferBuilder(0);

    int array = original.toFlatArray(bufferBuilder);
    bufferBuilder.finish(array);

    val flatArray = FlatArray.getRootAsFlatArray(bufferBuilder.dataBuffer());

    val restored = Nd4j.createFromFlatArray(flatArray);

    assertEquals(original, restored);
}
 
Example 5
Source File: ByteOrderTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testByteArrayOrder3() {
    val original = Nd4j.linspace(1, 25, 25).reshape('f', 5, 5);
    val bufferBuilder = new FlatBufferBuilder(0);

    int array = original.toFlatArray(bufferBuilder);
    bufferBuilder.finish(array);

    val flatArray = FlatArray.getRootAsFlatArray(bufferBuilder.dataBuffer());

    val restored = Nd4j.createFromFlatArray(flatArray);

    assertEquals(original, restored);
}
 
Example 6
Source File: ByteOrderTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testVectorEncoding_2() {
    val scalar = Nd4j.createFromArray(new double[]{1, 2, 3, 4, 5});

    FlatBufferBuilder bufferBuilder = new FlatBufferBuilder(0);
    val fb = scalar.toFlatArray(bufferBuilder);
    bufferBuilder.finish(fb);
    val db = bufferBuilder.dataBuffer();

    val flat = FlatArray.getRootAsFlatArray(db);

    val restored = Nd4j.createFromFlatArray(flat);

    assertEquals(scalar, restored);
}
 
Example 7
Source File: ByteOrderTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testVectorEncoding_1() {
    val scalar = Nd4j.createFromArray(new float[]{1, 2, 3, 4, 5});

    FlatBufferBuilder bufferBuilder = new FlatBufferBuilder(0);
    val fb = scalar.toFlatArray(bufferBuilder);
    bufferBuilder.finish(fb);
    val db = bufferBuilder.dataBuffer();

    val flat = FlatArray.getRootAsFlatArray(db);

    val restored = Nd4j.createFromFlatArray(flat);

    assertEquals(scalar, restored);
}
 
Example 8
Source File: ByteOrderTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testByteArrayOrder3() {
    val original = Nd4j.linspace(1, 25, 25, DataType.FLOAT).reshape('f', 5, 5);
    val bufferBuilder = new FlatBufferBuilder(0);

    int array = original.toFlatArray(bufferBuilder);
    bufferBuilder.finish(array);

    val flatArray = FlatArray.getRootAsFlatArray(bufferBuilder.dataBuffer());

    val restored = Nd4j.createFromFlatArray(flatArray);

    assertEquals(original, restored);
}
 
Example 9
Source File: MixedDataTypesTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlatSerde_3() {
    val arrayX = Nd4j.create(new boolean[]{true, false, true, true}, new  long[]{4}, DataType.BOOL);

    val builder = new FlatBufferBuilder(512);
    val flat = arrayX.toFlatArray(builder);
    builder.finish(flat);
    val db = builder.dataBuffer();

    val flatb = FlatArray.getRootAsFlatArray(db);

    val restored = Nd4j.createFromFlatArray(flatb);

    assertEquals(arrayX, restored);
}
 
Example 10
Source File: MixedDataTypesTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlatSerde_2() {
    val arrayX = Nd4j.create(new long[]{1, 2, 3, 4}, new  long[]{4}, DataType.LONG);

    val builder = new FlatBufferBuilder(512);
    val flat = arrayX.toFlatArray(builder);
    builder.finish(flat);
    val db = builder.dataBuffer();

    val flatb = FlatArray.getRootAsFlatArray(db);

    val restored = Nd4j.createFromFlatArray(flatb);

    assertEquals(arrayX, restored);
}
 
Example 11
Source File: MixedDataTypesTests.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlatSerde_1() {
    val arrayX = Nd4j.create(new int[]{1, 2, 3, 4}, new  long[]{4}, DataType.INT);

    val builder = new FlatBufferBuilder(512);
    val flat = arrayX.toFlatArray(builder);
    builder.finish(flat);
    val db = builder.dataBuffer();

    val flatb = FlatArray.getRootAsFlatArray(db);

    val restored = Nd4j.createFromFlatArray(flatb);

    assertEquals(arrayX, restored);
}
 
Example 12
Source File: ByteOrderTests.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testByteArrayOrder2() {
    val original = Nd4j.linspace(1, 25, 25).reshape(5, 5);
    val bufferBuilder = new FlatBufferBuilder(0);

    int array = original.toFlatArray(bufferBuilder);
    bufferBuilder.finish(array);

    val flatArray = FlatArray.getRootAsFlatArray(bufferBuilder.dataBuffer());

    val restored = Nd4j.createFromFlatArray(flatArray);

    assertEquals(original, restored);
}
 
Example 13
Source File: NativeGraphExecutioner.java    From nd4j with Apache License 2.0 4 votes vote down vote up
/**
   * This method executes given graph and returns results
   *
   * @param sd
   * @return
   */
  @Override
  public INDArray[] executeGraph(SameDiff sd, ExecutorConfiguration configuration) {

      Map<Integer, Node> intermediate = new HashMap<>();

      ByteBuffer buffer = convertToFlatBuffers(sd, configuration, intermediate);

      BytePointer bPtr = new BytePointer(buffer);

      log.info("Buffer length: {}", buffer.limit());

      Pointer res  = NativeOpsHolder.getInstance().getDeviceNativeOps().executeFlatGraphFloat(null, bPtr);
      if (res == null)
          throw new ND4JIllegalStateException("Graph execution failed");

      // FIXME: this is BAD
      PagedPointer pagedPointer = new PagedPointer(res,1024 * 1024L);
      FlatResult fr = FlatResult.getRootAsFlatResult(pagedPointer.asBytePointer().asByteBuffer());


      log.info("VarMap: {}", sd.variableMap());

      INDArray[] results = new INDArray[fr.variablesLength()];

      for (int e = 0; e < fr.variablesLength(); e++) {
          FlatVariable var = fr.variables(e);
          log.info("Var received: id: [{}:{}/<{}>];", var.id().first(), var.id().second(), var.name());
          FlatArray ndarray = var.ndarray();


          INDArray val = Nd4j.createFromFlatArray(ndarray);
          results[e] = val;

          if (var.name() != null && sd.variableMap().containsKey(var.name())) {
              //log.info("VarName: {}; Exists: {}; NDArrayInfo: {};", var.opName(), sd.variableMap().containsKey(var.opName()), sd.getVertexToArray().containsKey(var.opName()));
//              log.info("storing: {}; array: {}", var.name(), val);
              sd.associateArrayWithVariable(val, sd.variableMap().get(var.name()));

          } else {
              //log.info("Original id: {}; out: {}; out2: {}", original, sd.getVertexIdxToInfo().get(original), graph.getVariableForVertex(original));
              if (sd.variableMap().get(var.name()) != null) {
                  sd.associateArrayWithVariable(val,sd.getVariable(var.name()));
              } else {
  //                log.info("BAD");
                  //sd.var("",val);

                  throw new ND4JIllegalStateException("Unknown variable received as result: ["+ var.name() +"]");
              }
          }
      }


      return results;
  }
 
Example 14
Source File: ByteOrderTests.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testScalarEncoding() {
    val scalar = Nd4j.scalar(2.0f);

    FlatBufferBuilder bufferBuilder = new FlatBufferBuilder(0);
    val fb = scalar.toFlatArray(bufferBuilder);
    bufferBuilder.finish(fb);
    val db = bufferBuilder.dataBuffer();

    val flat = FlatArray.getRootAsFlatArray(db);


    val restored = Nd4j.createFromFlatArray(flat);

    assertEquals(scalar, restored);
}
 
Example 15
Source File: ByteOrderTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testVectorEncoding() {
    val scalar = Nd4j.trueVector(new float[]{1, 2, 3, 4, 5});

    FlatBufferBuilder bufferBuilder = new FlatBufferBuilder(0);
    val fb = scalar.toFlatArray(bufferBuilder);
    bufferBuilder.finish(fb);
    val db = bufferBuilder.dataBuffer();

    val flat = FlatArray.getRootAsFlatArray(db);


    val restored = Nd4j.createFromFlatArray(flat);

    assertEquals(scalar, restored);
}
 
Example 16
Source File: ByteOrderTests.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Test
public void testScalarEncoding() {
    val scalar = Nd4j.trueScalar(2.0f);

    FlatBufferBuilder bufferBuilder = new FlatBufferBuilder(0);
    val fb = scalar.toFlatArray(bufferBuilder);
    bufferBuilder.finish(fb);
    val db = bufferBuilder.dataBuffer();

    val flat = FlatArray.getRootAsFlatArray(db);


    val restored = Nd4j.createFromFlatArray(flat);

    assertEquals(scalar, restored);
}
 
Example 17
Source File: NativeGraphExecutioner.java    From deeplearning4j with Apache License 2.0 4 votes vote down vote up
/**
     * This method executes given graph and returns results
     *
     * @param sd
     * @return
     */
    @Override
    public INDArray[] executeGraph(SameDiff sd, ExecutorConfiguration configuration) {

        ByteBuffer buffer = convertToFlatBuffers(sd, configuration);

        BytePointer bPtr = new BytePointer(buffer);

        log.info("Buffer length: {}", buffer.limit());

        NativeOps nativeOps = NativeOpsHolder.getInstance().getDeviceNativeOps();
        OpaqueResultWrapper res = nativeOps.executeFlatGraph(null, bPtr);
        if (res == null)
            throw new ND4JIllegalStateException("Graph execution failed");

        PagedPointer pagedPointer = new PagedPointer(nativeOps.getResultWrapperPointer(res), nativeOps.getResultWrapperSize(res));
        FlatResult fr = FlatResult.getRootAsFlatResult(pagedPointer.asBytePointer().asByteBuffer());

        log.info("VarMap: {}", sd.variableMap());

        INDArray[] results = new INDArray[fr.variablesLength()];

        for (int e = 0; e < fr.variablesLength(); e++) {
            FlatVariable var = fr.variables(e);
            String varName = var.name();
//            log.info("Var received: id: [{}:{}/<{}>];", var.id().first(), var.id().second(), var.name());
            FlatArray ndarray = var.ndarray();

            INDArray val = Nd4j.createFromFlatArray(ndarray);
            results[e] = val;

            if (var.name() != null && sd.variableMap().containsKey(var.name())) {
                if(sd.getVariable(varName).getVariableType() != VariableType.ARRAY){
                    sd.associateArrayWithVariable(val, sd.variableMap().get(var.name()));
                }
            } else {
                if (sd.variableMap().get(var.name()) != null) {
                    sd.associateArrayWithVariable(val,sd.getVariable(var.name()));
                } else {
                    log.warn("Unknown variable received: [{}]", var.name());
                }
            }
        }

        // now we need to release native memory
        nativeOps.deleteResultWrapper(res);

        return results;
    }