Package org.pytorch
Class Tensor
java.lang.Object
org.pytorch.Tensor
public abstract class Tensor
extends java.lang.Object
Representation of a Tensor. Behavior is similar to PyTorch's tensor objects.
Most tensors will be constructed as Tensor.fromBlob(data, shape)
,
where data
can be an array or a direct Buffer
(of the proper subclass).
Helper methods are provided to allocate buffers properly.
To access Tensor data, see dtype()
, shape()
,
and various getDataAs*
methods.
When constructing Tensor
objects with data
as 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. data
passed as a
Buffer
is not copied, so it can be modified between Module
calls
to avoid reallocation. Data retrieved from Tensor
objects may be copied or
may be a reference to the Tensor
's internal data buffer.
shape
is always copied.
-
Method Summary
Modifier and Type Method Description static java.nio.ByteBuffer
allocateByteBuffer(int numElements)
Allocates a new directByteBuffer
with native byte order with specified capacity that can be used infromBlob(ByteBuffer, long[])
,fromBlobUnsigned(ByteBuffer, long[])
.static java.nio.DoubleBuffer
allocateDoubleBuffer(int numElements)
Allocates a new directDoubleBuffer
with native byte order with specified capacity that can be used infromBlob(DoubleBuffer, long[])
.static java.nio.FloatBuffer
allocateFloatBuffer(int numElements)
Allocates a new directFloatBuffer
with native byte order with specified capacity that can be used infromBlob(FloatBuffer, long[])
.static java.nio.IntBuffer
allocateIntBuffer(int numElements)
Allocates a new directIntBuffer
with native byte order with specified capacity that can be used infromBlob(IntBuffer, long[])
.static java.nio.LongBuffer
allocateLongBuffer(int numElements)
Allocates a new directLongBuffer
with native byte order with specified capacity that can be used infromBlob(LongBuffer, long[])
.abstract DType
dtype()
static Tensor
fromBlob(byte[] data, long[] shape)
Creates a new Tensor instance with dtype torch.int8 with specified shape and data as array of bytes.static Tensor
fromBlob(float[] data, long[] shape)
Creates a new Tensor instance with dtype torch.float32 with specified shape and data as array of floats.static Tensor
fromBlob(int[] data, long[] shape)
Creates a new Tensor instance with dtype torch.int32 with specified shape and data as array of ints.static Tensor
fromBlob(long[] shape, double[] data)
Creates a new Tensor instance with dtype torch.float64 with specified shape and data as array of doubles.static Tensor
fromBlob(long[] data, long[] shape)
Creates a new Tensor instance with dtype torch.int64 with specified shape and data as array of longs.static Tensor
fromBlob(java.nio.ByteBuffer data, long[] shape)
Creates a new Tensor instance with dtype torch.int8 with specified shape and data.static Tensor
fromBlob(java.nio.DoubleBuffer data, long[] shape)
Creates a new Tensor instance with dtype torch.float64 with specified shape and data.static Tensor
fromBlob(java.nio.FloatBuffer data, long[] shape)
Creates a new Tensor instance with dtype torch.float32 with specified shape and data.static Tensor
fromBlob(java.nio.IntBuffer data, long[] shape)
Creates a new Tensor instance with dtype torch.int32 with specified shape and data.static Tensor
fromBlob(java.nio.LongBuffer data, long[] shape)
Creates a new Tensor instance with dtype torch.int64 with specified shape and data.static Tensor
fromBlobUnsigned(byte[] data, long[] shape)
Creates a new Tensor instance with dtype torch.uint8 with specified shape and data as array of bytes.static Tensor
fromBlobUnsigned(java.nio.ByteBuffer data, long[] shape)
Creates a new Tensor instance with dtype torch.uint8 with specified shape and data.byte[]
getDataAsByteArray()
double[]
getDataAsDoubleArray()
float[]
getDataAsFloatArray()
int[]
getDataAsIntArray()
long[]
getDataAsLongArray()
byte[]
getDataAsUnsignedByteArray()
long
numel()
Returns the number of elements in this tensor.static long
numel(long[] shape)
Calculates the number of elements in a tensor with the specified shape.long[]
shape()
Returns the shape of this tensor.
-
Method Details
-
allocateByteBuffer
public static java.nio.ByteBuffer allocateByteBuffer(int numElements)Allocates a new directByteBuffer
with native byte order with specified capacity that can be used infromBlob(ByteBuffer, long[])
,fromBlobUnsigned(ByteBuffer, long[])
.- Parameters:
numElements
- capacity (number of elements) of result buffer.
-
allocateIntBuffer
public static java.nio.IntBuffer allocateIntBuffer(int numElements)Allocates a new directIntBuffer
with native byte order with specified capacity that can be used infromBlob(IntBuffer, long[])
.- Parameters:
numElements
- capacity (number of elements) of result buffer.
-
allocateFloatBuffer
public static java.nio.FloatBuffer allocateFloatBuffer(int numElements)Allocates a new directFloatBuffer
with native byte order with specified capacity that can be used infromBlob(FloatBuffer, long[])
.- Parameters:
numElements
- capacity (number of elements) of result buffer.
-
allocateLongBuffer
public static java.nio.LongBuffer allocateLongBuffer(int numElements)Allocates a new directLongBuffer
with native byte order with specified capacity that can be used infromBlob(LongBuffer, long[])
.- Parameters:
numElements
- capacity (number of elements) of result buffer.
-
allocateDoubleBuffer
public static java.nio.DoubleBuffer allocateDoubleBuffer(int numElements)Allocates a new directDoubleBuffer
with native byte order with specified capacity that can be used infromBlob(DoubleBuffer, long[])
.- Parameters:
numElements
- capacity (number of elements) of result buffer.
-
fromBlobUnsigned
Creates a new Tensor instance with dtype torch.uint8 with specified shape and data as array of bytes.- Parameters:
data
- Tensor elementsshape
- Tensor shape
-
fromBlob
Creates a new Tensor instance with dtype torch.int8 with specified shape and data as array of bytes.- Parameters:
data
- Tensor elementsshape
- Tensor shape
-
fromBlob
Creates a new Tensor instance with dtype torch.int32 with specified shape and data as array of ints.- Parameters:
data
- Tensor elementsshape
- Tensor shape
-
fromBlob
Creates a new Tensor instance with dtype torch.float32 with specified shape and data as array of floats.- Parameters:
data
- Tensor elementsshape
- Tensor shape
-
fromBlob
Creates a new Tensor instance with dtype torch.int64 with specified shape and data as array of longs.- Parameters:
data
- Tensor elementsshape
- Tensor shape
-
fromBlob
Creates a new Tensor instance with dtype torch.float64 with specified shape and data as array of doubles.- Parameters:
shape
- Tensor shapedata
- Tensor elements
-
fromBlobUnsigned
Creates a new Tensor instance with dtype torch.uint8 with specified shape and data.- Parameters:
data
- Direct buffer with native byte order that containsTensor.numel(shape)
elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape
- Tensor shape
-
fromBlob
Creates a new Tensor instance with dtype torch.int8 with specified shape and data.- Parameters:
data
- Direct buffer with native byte order that containsTensor.numel(shape)
elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape
- Tensor shape
-
fromBlob
Creates a new Tensor instance with dtype torch.int32 with specified shape and data.- Parameters:
data
- Direct buffer with native byte order that containsTensor.numel(shape)
elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape
- Tensor shape
-
fromBlob
Creates a new Tensor instance with dtype torch.float32 with specified shape and data.- Parameters:
data
- Direct buffer with native byte order that containsTensor.numel(shape)
elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape
- Tensor shape
-
fromBlob
Creates a new Tensor instance with dtype torch.int64 with specified shape and data.- Parameters:
data
- Direct buffer with native byte order that containsTensor.numel(shape)
elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape
- Tensor shape
-
fromBlob
Creates a new Tensor instance with dtype torch.float64 with specified shape and data.- Parameters:
data
- Direct buffer with native byte order that containsTensor.numel(shape)
elements. The buffer is used directly without copying, and changes to its content will change the tensor.shape
- Tensor shape
-
numel
public long numel()Returns the number of elements in this tensor. -
numel
public static long numel(long[] shape)Calculates the number of elements in a tensor with the specified shape. -
shape
public long[] shape()Returns the shape of this tensor. (The array is a fresh copy.) -
dtype
- Returns:
- data type of this tensor.
-
getDataAsByteArray
public byte[] getDataAsByteArray()- Returns:
- a Java byte array that contains the tensor data. This may be a copy or reference.
- Throws:
java.lang.IllegalStateException
- if it is called for a non-int8 tensor.
-
getDataAsUnsignedByteArray
public byte[] getDataAsUnsignedByteArray()- Returns:
- a Java byte array that contains the tensor data. This may be a copy or reference.
- Throws:
java.lang.IllegalStateException
- if it is called for a non-uint8 tensor.
-
getDataAsIntArray
public int[] getDataAsIntArray()- Returns:
- a Java int array that contains the tensor data. This may be a copy or reference.
- Throws:
java.lang.IllegalStateException
- if it is called for a non-int32 tensor.
-
getDataAsFloatArray
public float[] getDataAsFloatArray()- Returns:
- a Java float array that contains the tensor data. This may be a copy or reference.
- Throws:
java.lang.IllegalStateException
- if it is called for a non-float32 tensor.
-
getDataAsLongArray
public long[] getDataAsLongArray()- Returns:
- a Java long array that contains the tensor data. This may be a copy or reference.
- Throws:
java.lang.IllegalStateException
- if it is called for a non-int64 tensor.
-
getDataAsDoubleArray
public double[] getDataAsDoubleArray()- Returns:
- a Java double array that contains the tensor data. This may be a copy or reference.
- Throws:
java.lang.IllegalStateException
- if it is called for a non-float64 tensor.
-