Tensor

public interface Tensor
Known Indirect Subclasses

A statically typed multi-dimensional array.

There are two categories of tensors in TensorFlow Java: typed tensors and raw tensors . The former maps the tensor native memory to an n-dimensional typed data space, allowing direct I/O operations from the JVM, while the latter is only a reference to a native tensor allowing basic operations and flat data access.

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 try-with-resources block:

try (Tensor t = Tensor.of(...)) {
   doSomethingWith(t);
 
 }

Instances of a Tensor are not thread-safe.

Public Methods

abstract RawTensor
asRawTensor ()
Returns a raw (untyped) representation of this tensor
abstract void
close ()
Release resources associated with the Tensor.
abstract DataType
dataType ()
Returns the DataType of elements stored in the tensor.
abstract long
numBytes ()
Returns the size, in bytes, of the tensor data.
abstract static <T extends TType > T
of (Class<T> type, Shape shape, long size, Consumer<T> dataInitializer)
Allocates a tensor of a given datatype, shape and size.
abstract static <T extends TType > T
of (Class<T> type, Shape shape)
Allocates a tensor of a given datatype and shape.
abstract static <T extends TType > T
of (Class<T> type, Shape shape, long size)
Allocates a tensor of a given datatype, shape and size.
abstract static <T extends TType > T
of (Class<T> type, Shape shape, Consumer<T> dataInitializer)
Allocates and initialize a tensor of a given datatype and shape.
abstract static <T extends TType > T
of (Class<T> type, Shape shape, ByteDataBuffer rawData)
Creates a Tensor of any type from the raw data provided by the given buffer.
abstract Shape
shape ()
Returns the shape of the tensor.

Inherited Methods

Public Methods

public abstract RawTensor asRawTensor ()

Returns a raw (untyped) representation of this tensor

public abstract void close ()

Release resources associated with the Tensor.

WARNING: This must be invoked for all tensors that were not been produced by an eager operation or memory will be leaked.

The Tensor object is no longer usable after close returns.

public abstract DataType dataType ()

Returns the DataType of elements stored in the tensor.

public abstract long numBytes ()

Returns the size, in bytes, of the tensor data.

public static abstract T of (Class<T> type, Shape shape, long size, Consumer<T> dataInitializer)

Allocates a tensor of a given datatype, shape and size.

This method is identical to of(Class, Shape, Consumer) , except that the final size for the tensor can be explicitly set instead of being computed from the datatype and shape.

This could be useful for tensor types that stores data but also metadata in the tensor memory, such as the lookup table in a tensor of strings.

Parameters
type the tensor type class
shape shape of the tensor
size size in bytes of the tensor or -1 to compute the size from the shape
dataInitializer method receiving accessor to the allocated tensor data for initialization
Returns
  • an allocated and initialized tensor
Throws
IllegalArgumentException if size is smaller than the minimum space required to store the tensor data
IllegalArgumentException if size is set to -1 but elements of the given type are of variable length (e.g. strings)
IllegalArgumentException if shape is totally or partially unknown
IllegalStateException if tensor failed to be allocated

public static abstract T of (Class<T> type, Shape shape)

Allocates a tensor of a given datatype and shape.

The amount of memory to allocate is derived from the datatype and the shape of the tensor, and is left uninitialized.

Parameters
type the tensor type class
shape shape of the tensor
Returns
  • an allocated but uninitialized tensor
Throws
IllegalArgumentException if elements of the given type are of variable length (e.g. strings)
IllegalArgumentException if shape is totally or partially unknown
IllegalStateException if tensor failed to be allocated

public static abstract T of (Class<T> type, Shape shape, long size)

Allocates a tensor of a given datatype, shape and size.

This method is identical to of(Class, Shape) , except that the final size of the tensor can be explicitly set instead of computing it from the datatype and shape, which could be larger than the actual space required to store the data but not smaller.

Parameters
type the tensor type class
shape shape of the tensor
size size in bytes of the tensor or -1 to compute the size from the shape
Returns
  • an allocated but uninitialized tensor
Throws
IllegalArgumentException if size is smaller than the minimum space required to store the tensor data
IllegalArgumentException if size is set to -1 but elements of the given type are of variable length (e.g. strings)
IllegalArgumentException if shape is totally or partially unknown
IllegalStateException if tensor failed to be allocated

public static abstract T of (Class<T> type, Shape shape, Consumer<T> dataInitializer)

Allocates and initialize a tensor of a given datatype and shape.

The amount of memory to allocate is derived from the datatype and the shape of the tensor. Tensor data is initialized by calling the dataInitializer , which receives in argument the value returned by ERROR(/#data()) on the allocated tensor. For example:

FloatNdArray data = ...
 try (TFloat32 t = Tensor.of(TFloat32.class, Shape.of(2, 2), data::copyTo)) {
   ...
 
 }

If dataInitializer fails and throws an exception, the allocated tensor will be automatically released before rethrowing the same exception.

Parameters
type the tensor type class
shape shape of the tensor
dataInitializer method receiving accessor to the allocated tensor data for initialization
Returns
  • an allocated and initialized tensor
Throws
IllegalArgumentException if elements of the given type are of variable length (e.g. strings)
IllegalArgumentException if shape is totally or partially unknown
IllegalStateException if tensor failed to be allocated

public static abstract T of (Class<T> type, Shape shape, ByteDataBuffer rawData)

Creates a Tensor of any type from the raw data provided by the given buffer.

Data must have been encoded into data as per the specification of the TensorFlow C API .

Parameters
type the tensor type class
shape the tensor shape.
rawData a buffer containing the tensor raw data.
Throws
IllegalArgumentException if rawData is not large enough to contain the tensor data
IllegalArgumentException if shape is totally or partially unknown
IllegalStateException if tensor failed to be allocated with the given parameters

public abstract Shape shape ()

Returns the shape of the tensor.