A typed multidimensional array.
Instances of a Tensor are not threadsafe.
WARNING: Resources consumed by the Tensor object must be explicitly freed by
invoking the close()
method when the object is no longer needed. For example, using a
trywithresources block like:
try(Tensor t = Tensor.create(...)) {
doSomethingWith(t);
}
Public Methods
boolean 
booleanValue()
Returns the value in a scalar
BOOL tensor. 
byte[] 
bytesValue()
Returns the value in a scalar
STRING tensor. 
void 
close()
Release resources associated with the Tensor.

<T> T 
copyTo(T dst)
Copies the contents of the tensor to
dst and returns dst . 
static Tensor  
static Tensor 
create(Object obj)
Create a Tensor from a Java object.

static Tensor  
static Tensor  
static Tensor  
static Tensor  
DataType  
double 
doubleValue()
Returns the value in a scalar
DOUBLE tensor. 
float 
floatValue()
Returns the value in a scalar
FLOAT tensor. 
int  
long  
int 
numBytes()
Returns the size, in bytes, of the tensor data.

int 
numDimensions()
Returns the number of dimensions (sometimes referred to as rank) of the Tensor.

int 
numElements()
Returns the number of elements in a flattened (1D) view of the tensor.

long[]  
String 
toString()
Returns a string describing the type and shape of the Tensor.

void  
void  
void  
void 
writeTo(ByteBuffer dst)
Write the tensor data into the given buffer.

void 
Inherited Methods
Public Methods
public boolean booleanValue ()
Returns the value in a scalar BOOL
tensor.
Throws
IllegalArgumentException  if the Tensor does not represent a boolean scalar. 

public byte[] bytesValue ()
Returns the value in a scalar STRING
tensor.
Throws
IllegalArgumentException  if the Tensor does not represent a boolean scalar. 

public void close ()
Release resources associated with the Tensor.
WARNING:If not invoked, memory will be leaked.
The Tensor object is no longer usable after close
returns.
public T copyTo (T dst)
Copies the contents of the tensor to dst
and returns dst
.
For nonscalar tensors, this method copies the contents of the underlying tensor to a Java
array. For scalar tensors, use one of floatValue()
, doubleValue()
, intValue()
, longValue()
or booleanValue()
instead. The type and shape of
dst
must be compatible with the tensor. For example:
int matrix[2][2] = { {1,2},{3,4} };
try(Tensor t = Tensor.create(matrix)) {
// Succeeds and prints "3"
int[][] copy = new int[2][2];
System.out.println(t.copyTo(copy)[1][0]);
// Throws IllegalArgumentException since the shape of dst does not match the shape of t.
int[][] dst = new int[4][1];
t.copyTo(dst);
}
Parameters
dst 

Throws
IllegalArgumentException  if the tensor is a scalar or if dst is not compatible
with the tensor (for example, mismatched data types or shapes).


public static Tensor create (DataType dataType, long[] shape, ByteBuffer data)
Create a Tensor with data from the given buffer.
Creates a Tensor with the provided shape of any type where the tensor's data has been
encoded into data
as per the specification of the TensorFlow C API.
Parameters
dataType  the tensor datatype. 

shape  the tensor shape. 
data  a buffer containing the tensor data. 
Throws
IllegalArgumentException  If the tensor datatype or shape is not compatible with the buffer 

public static Tensor create (Object obj)
Create a Tensor from a Java object.
A Tensor is a multidimensional array of elements of a limited set of types (DataType
). Thus, not all Java objects can be converted to a Tensor. In particular, obj
must be either a primitive (float, double, int, long, boolean) or a multidimensional array of
one of those primitives. For example:
// Valid: A 64bit integer scalar.
Tensor s = Tensor.create(42L);
// Valid: A 3x2 matrix of floats.
float[][] matrix = new float[3][2];
Tensor m = Tensor.create(matrix);
// Invalid: Will throw an IllegalArgumentException as an arbitrary Object
// does not fit into the TensorFlow type system.
Tensor o = Tensor.create(new Object());
// Invalid: Will throw an IllegalArgumentException since there are
// a differing number of elements in each row of this 2D array.
int[][] twoD = new int[2][];
twoD[0] = new int[1];
twoD[1] = new int[2];
Tensor x = Tensor.create(twoD);
Parameters
obj 

Throws
IllegalArgumentException  if obj is not compatible with the TensorFlow type
system, or if obj does not disambiguate between multiple DataTypes. In that case, consider
using create(DataType, long[], ByteBuffer) instead.


public static Tensor create (long[] shape, DoubleBuffer data)
Create a DOUBLE
Tensor with data from the given buffer.
Creates a Tensor with the given shape by copying elements from the buffer (starting from its
current position) into the tensor. For example, if shape = {2,3}
(which represents a
2x3 matrix) then the buffer must have 6 elements remaining, which will be consumed by this
method.
Parameters
shape  the tensor shape. 

data  a buffer containing the tensor data. 
Throws
IllegalArgumentException  If the tensor shape is not compatible with the buffer 

public static Tensor create (long[] shape, LongBuffer data)
Create an INT64
Tensor with data from the given buffer.
Creates a Tensor with the given shape by copying elements from the buffer (starting from its
current position) into the tensor. For example, if shape = {2,3}
(which represents a
2x3 matrix) then the buffer must have 6 elements remaining, which will be consumed by this
method.
Parameters
shape  the tensor shape. 

data  a buffer containing the tensor data. 
Throws
IllegalArgumentException  If the tensor shape is not compatible with the buffer 

public static Tensor create (long[] shape, IntBuffer data)
Create an INT32
Tensor with data from the given buffer.
Creates a Tensor with the given shape by copying elements from the buffer (starting from its
current position) into the tensor. For example, if shape = {2,3}
(which represents a
2x3 matrix) then the buffer must have 6 elements remaining, which will be consumed by this
method.
Parameters
shape  the tensor shape. 

data  a buffer containing the tensor data. 
Throws
IllegalArgumentException  If the tensor shape is not compatible with the buffer 

public static Tensor create (long[] shape, FloatBuffer data)
Create a FLOAT
Tensor with data from the given buffer.
Creates a Tensor with the given shape by copying elements from the buffer (starting from its
current position) into the tensor. For example, if shape = {2,3}
(which represents a
2x3 matrix) then the buffer must have 6 elements remaining, which will be consumed by this
method.
Parameters
shape  the tensor shape. 

data  a buffer containing the tensor data. 
Throws
IllegalArgumentException  If the tensor shape is not compatible with the buffer 

public double doubleValue ()
Returns the value in a scalar DOUBLE
tensor.
Throws
IllegalArgumentException  if the Tensor does not represent a double scalar. 

public float floatValue ()
Returns the value in a scalar FLOAT
tensor.
Throws
IllegalArgumentException  if the Tensor does not represent a float scalar. 

public int intValue ()
Returns the value in a scalar INT32
tensor.
Throws
IllegalArgumentException  if the Tensor does not represent a int scalar. 

public long longValue ()
Returns the value in a scalar INT64
tensor.
Throws
IllegalArgumentException  if the Tensor does not represent a long scalar. 

public int numBytes ()
Returns the size, in bytes, of the tensor data.
public int numDimensions ()
Returns the number of dimensions (sometimes referred to as rank) of the Tensor.
Will be 0 for a scalar, 1 for a vector, 2 for a matrix, 3 for a 3dimensional tensor etc.
public int numElements ()
Returns the number of elements in a flattened (1D) view of the tensor.
public long[] shape ()
Returns the shape of the Tensor, i.e., the sizes of each dimension.
Returns
 an array where the ith element is the size of the ith dimension of the tensor.
public String toString ()
Returns a string describing the type and shape of the Tensor.
public void writeTo (LongBuffer dst)
Write the data of a INT64
tensor into the given buffer.
Copies numElements()
elements to the buffer.
Parameters
dst  the destination buffer 

Throws
BufferOverflowException  If there is insufficient space in the given buffer for the data in this tensor 

IllegalArgumentException  If the tensor datatype is not INT64

public void writeTo (DoubleBuffer dst)
Write the data of a DOUBLE
tensor into the given buffer.
Copies numElements()
elements to the buffer.
Parameters
dst  the destination buffer 

Throws
BufferOverflowException  If there is insufficient space in the given buffer for the data in this tensor 

IllegalArgumentException  If the tensor datatype is not DOUBLE

public void writeTo (IntBuffer dst)
Write the data of a INT32
tensor into the given buffer.
Copies numElements()
elements to the buffer.
Parameters
dst  the destination buffer 

Throws
BufferOverflowException  If there is insufficient space in the given buffer for the data in this tensor 

IllegalArgumentException  If the tensor datatype is not INT32

public void writeTo (ByteBuffer dst)
Write the tensor data into the given buffer.
Copies numBytes()
bytes to the buffer in native byte order for primitive types.
Parameters
dst  the destination buffer 

Throws
BufferOverflowException  If there is insufficient space in the given buffer for the data in this tensor 

public void writeTo (FloatBuffer dst)
Write the data of a FLOAT
tensor into the given buffer.
Copies numElements()
elements to the buffer.
Parameters
dst  the destination buffer 

Throws
BufferOverflowException  If there is insufficient space in the given buffer for the data in this tensor 

IllegalArgumentException  If the tensor datatype is not FLOAT
