tensorflow / java

Java bindings for TensorFlow
Apache License 2.0
813 stars 200 forks source link

Which is the "org.tensorflow.Tensor#create(java.lang.Object)" substitute? #536

Open Mhuang77 opened 5 months ago

Mhuang77 commented 5 months ago

In the TF1.x use the "org.tensorflow.Tensor#create(java.lang.Object)" can conveniently construct various types. Which method in TF2 is equivalent to org.tensorflow.Tensor#create(java.lang.Object) in TF1?

Craigacp commented 5 months ago

Tensors are created from ndarrays now, you should look at https://github.com/tensorflow/java-ndarray.

karllessard commented 5 months ago

In fact, if you are looking at creating TF Tensors, you should use factory methods of classes part of the TType family. For example, to create a integer scalar, you should call TInt32.scalarOf(value) and so on.

TF tensors are ndArrays, but not necessarily the other way around, meaning that if you create tensors only using the java-ndarray library (e.g. NdArrays.scalarOf(integer), you'll get a n-dimensional data structure, yes, but you won't be able to use it directly with TensorFlow and you'll need to convert to a TF Tensor later (e.g. TInt32.tensorOf(ndArray)).