Tensor¶
-
public abstract class
Tensor¶ Representation of a Tensor. Behavior is similar to PyTorch’s tensor objects.
Most tensors will be constructed as
Tensor.fromBlob(data, shape), wheredatacan be an array or a directBuffer(of the proper subclass). Helper methods are provided to allocate buffers properly.To access Tensor data, see
dtype(),shape(), and variousgetDataAs*methods.When constructing
Tensorobjects withdataas an array, it is not specified whether this data is is copied or retained as a reference so it is recommended not to modify it after constructing.datapassed as aBufferis not copied, so it can be modified betweenModulecalls to avoid reallocation. Data retrieved fromTensorobjects may be copied or may be a reference to theTensor’s internal data buffer.shapeis always copied.
Methods¶
allocateByteBuffer¶
-
public static ByteBuffer
allocateByteBuffer(int numElements)¶ Allocates a new direct
java.nio.ByteBufferwith native byte order with specified capacity that can be used inTensor.fromBlob(ByteBuffer,long[]),Tensor.fromBlobUnsigned(ByteBuffer,long[]).- Parameters
numElements – capacity (number of elements) of result buffer.
allocateDoubleBuffer¶
-
public static DoubleBuffer
allocateDoubleBuffer(int numElements)¶ Allocates a new direct
java.nio.DoubleBufferwith native byte order with specified capacity that can be used inTensor.fromBlob(DoubleBuffer,long[]).- Parameters
numElements – capacity (number of elements) of result buffer.
allocateFloatBuffer¶
-
public static FloatBuffer
allocateFloatBuffer(int numElements)¶ Allocates a new direct
java.nio.FloatBufferwith native byte order with specified capacity that can be used inTensor.fromBlob(FloatBuffer,long[]).- Parameters
numElements – capacity (number of elements) of result buffer.
allocateIntBuffer¶
-
public static IntBuffer
allocateIntBuffer(int numElements)¶ Allocates a new direct
java.nio.IntBufferwith native byte order with specified capacity that can be used inTensor.fromBlob(IntBuffer,long[]).- Parameters
numElements – capacity (number of elements) of result buffer.
allocateLongBuffer¶
-
public static LongBuffer
allocateLongBuffer(int numElements)¶ Allocates a new direct
java.nio.LongBufferwith native byte order with specified capacity that can be used inTensor.fromBlob(LongBuffer,long[]).- Parameters
numElements – capacity (number of elements) of result buffer.
fromBlob¶
fromBlob¶
fromBlob¶
fromBlob¶
fromBlob¶
fromBlob¶
-
public static Tensor
fromBlob(ByteBuffer data, long[] shape)¶ Creates a new Tensor instance with dtype torch.int8 with specified shape and data.
- Parameters
data – Direct buffer with native byte order that contains
Tensor.numel(shape)elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape – Tensor shape
fromBlob¶
-
public static Tensor
fromBlob(IntBuffer data, long[] shape)¶ Creates a new Tensor instance with dtype torch.int32 with specified shape and data.
- Parameters
data – Direct buffer with native byte order that contains
Tensor.numel(shape)elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape – Tensor shape
fromBlob¶
-
public static Tensor
fromBlob(FloatBuffer data, long[] shape)¶ Creates a new Tensor instance with dtype torch.float32 with specified shape and data.
- Parameters
data – Direct buffer with native byte order that contains
Tensor.numel(shape)elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape – Tensor shape
fromBlob¶
-
public static Tensor
fromBlob(LongBuffer data, long[] shape)¶ Creates a new Tensor instance with dtype torch.int64 with specified shape and data.
- Parameters
data – Direct buffer with native byte order that contains
Tensor.numel(shape)elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape – Tensor shape
fromBlob¶
-
public static Tensor
fromBlob(DoubleBuffer data, long[] shape)¶ Creates a new Tensor instance with dtype torch.float64 with specified shape and data.
- Parameters
data – Direct buffer with native byte order that contains
Tensor.numel(shape)elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape – Tensor shape
fromBlobUnsigned¶
fromBlobUnsigned¶
-
public static Tensor
fromBlobUnsigned(ByteBuffer data, long[] shape)¶ Creates a new Tensor instance with dtype torch.uint8 with specified shape and data.
- Parameters
data – Direct buffer with native byte order that contains
Tensor.numel(shape)elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape – Tensor shape
getDataAsByteArray¶
-
public byte[]
getDataAsByteArray()¶ - Throws
IllegalStateException – if it is called for a non-int8 tensor.
- Returns
a Java byte array that contains the tensor data. This may be a copy or reference.
getDataAsDoubleArray¶
-
public double[]
getDataAsDoubleArray()¶ - Throws
IllegalStateException – if it is called for a non-float64 tensor.
- Returns
a Java double array that contains the tensor data. This may be a copy or reference.
getDataAsFloatArray¶
-
public float[]
getDataAsFloatArray()¶ - Throws
IllegalStateException – if it is called for a non-float32 tensor.
- Returns
a Java float array that contains the tensor data. This may be a copy or reference.
getDataAsIntArray¶
-
public int[]
getDataAsIntArray()¶ - Throws
IllegalStateException – if it is called for a non-int32 tensor.
- Returns
a Java int array that contains the tensor data. This may be a copy or reference.
getDataAsLongArray¶
-
public long[]
getDataAsLongArray()¶ - Throws
IllegalStateException – if it is called for a non-int64 tensor.
- Returns
a Java long array that contains the tensor data. This may be a copy or reference.
getDataAsUnsignedByteArray¶
-
public byte[]
getDataAsUnsignedByteArray()¶ - Throws
IllegalStateException – if it is called for a non-uint8 tensor.
- Returns
a Java byte array that contains the tensor data. This may be a copy or reference.