Git Product home page Git Product logo

Comments (5)

karllessard avatar karllessard commented on May 18, 2024 3

Just jumping into this ticket as well, so the proper way to create tensors now is by using factories exposed by the type themselves. Here's a few examples for 32-bit integer tensors:

Tensor<TInt32> scalar = TInt32.scalarOf(0);

Tensor<TInt32> array = TInt32.vectorOf(1, 2, 3, 4);

// this is efficient for large tensors, as it does not require to preallocate or 
// duplicate the whole data before feeding the tensor
Tensor<TInt32> matrix = TInt32.tensorOf(Shape.of(2, 4), data -> 

    // your input loop here, you could read from an `InputStream`, etc. but let's use
    // a simple case as an example
    data.set(NdArrays.vectorOf(1, 2, 3, 4), 0)
        .set(NdArrays.vectorOf(5, 6, 7, 8), 1)
); 

// this can be more convenient for small tensors, for the improved syntax
Tensor<TInt32> matrix3d = TInt32.tensorOf(StdArrays.ndCopyOf(new int[][][] {
  {
     {1, 2, 3}, {4, 5, 6}
  },
  {
     {7, 8, 9}, {10, 11, 12}
  }  
}));

// and more options...

You can do pretty much the same using the Constant for tensors with a fix value in a graph.

And finally, you can rely on the Tensor factories if you need better fine-tuning or selection of the datatype at runtime.

from java.

Craigacp avatar Craigacp commented on May 18, 2024 2

It depends what you are trying to do, but the Tensor infrastructure now creates tensors from NdArrays which are defined in tensorflow-tools. To use the old slow path you can create an NdArray using org.tensorflow.tools.ndarray.StdArrays, and then feed the resulting object into <Type>.tensorOf(), where is the type of Tensor you want to create (e.g. TFloat32, TInt32 etc). If your data isn't stored in a java multidimensional array then we recommend that you write it directly into an NdArray after allocating one of the appropriate size (using org.tensorflow.types.<type>.tensorOf(shape).

from java.

vissood avatar vissood commented on May 18, 2024 1

Thanks for reply!!
Can you please share what is alternative way?

from java.

Craigacp avatar Craigacp commented on May 18, 2024

Those methods are horribly inefficient due to the way Java multidimensional arrays are constructed, so they've been removed from the new API.

from java.

hegdeashwin avatar hegdeashwin commented on May 18, 2024

I'm too looking for some better alternative, any help would be great.

from java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.