Package org.pytorch
Class Tensor
- java.lang.Object
-
- org.pytorch.Tensor
-
public abstract class Tensor extends Object
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 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.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static ByteBufferallocateByteBuffer(int numElements)Allocates a new directByteBufferwith native byte order with specified capacity that can be used infromBlob(ByteBuffer, long[]),fromBlobUnsigned(ByteBuffer, long[]).static DoubleBufferallocateDoubleBuffer(int numElements)Allocates a new directDoubleBufferwith native byte order with specified capacity that can be used infromBlob(DoubleBuffer, long[]).static FloatBufferallocateFloatBuffer(int numElements)Allocates a new directFloatBufferwith native byte order with specified capacity that can be used infromBlob(FloatBuffer, long[]).static IntBufferallocateIntBuffer(int numElements)Allocates a new directIntBufferwith native byte order with specified capacity that can be used infromBlob(IntBuffer, long[]).static LongBufferallocateLongBuffer(int numElements)Allocates a new directLongBufferwith native byte order with specified capacity that can be used infromBlob(LongBuffer, long[]).abstract DTypedtype()static TensorfromBlob(byte[] data, long[] shape)static TensorfromBlob(byte[] data, long[] shape, MemoryFormat memoryFormat)Creates a new Tensor instance with dtype torch.int8 with specified shape and data as array of bytes.static TensorfromBlob(double[] data, long[] shape)static TensorfromBlob(double[] data, long[] shape, MemoryFormat memoryFormat)Creates a new Tensor instance with dtype torch.float64 with specified shape and data as array of doubles.static TensorfromBlob(float[] data, long[] shape)static TensorfromBlob(float[] data, long[] shape, MemoryFormat memoryFormat)Creates a new Tensor instance with dtype torch.float32 with specified shape and data as array of floats.static TensorfromBlob(int[] data, long[] shape)static TensorfromBlob(int[] data, long[] shape, MemoryFormat memoryFormat)Creates a new Tensor instance with dtype torch.int32 with specified shape and data as array of ints.static TensorfromBlob(long[] data, long[] shape)static TensorfromBlob(long[] data, long[] shape, MemoryFormat memoryFormat)Creates a new Tensor instance with dtype torch.int64 with specified shape and data as array of longs.static TensorfromBlob(ByteBuffer data, long[] shape)static TensorfromBlob(ByteBuffer data, long[] shape, MemoryFormat memoryFormat)Creates a new Tensor instance with dtype torch.int8 with specified shape and data.static TensorfromBlob(DoubleBuffer data, long[] shape)static TensorfromBlob(DoubleBuffer data, long[] shape, MemoryFormat memoryFormat)Creates a new Tensor instance with dtype torch.float64 with specified shape and data.static TensorfromBlob(FloatBuffer data, long[] shape)static TensorfromBlob(FloatBuffer data, long[] shape, MemoryFormat memoryFormat)Creates a new Tensor instance with dtype torch.float32 with specified shape and data.static TensorfromBlob(IntBuffer data, long[] shape)static TensorfromBlob(IntBuffer data, long[] shape, MemoryFormat memoryFormat)Creates a new Tensor instance with dtype torch.int32 with specified shape and data.static TensorfromBlob(LongBuffer data, long[] shape)static TensorfromBlob(LongBuffer data, long[] shape, MemoryFormat memoryFormat)Creates a new Tensor instance with dtype torch.int64 with specified shape and data.static TensorfromBlobUnsigned(byte[] data, long[] shape)static TensorfromBlobUnsigned(byte[] data, long[] shape, MemoryFormat memoryFormat)Creates a new Tensor instance with dtype torch.uint8 with specified shape and data as array of bytes.static TensorfromBlobUnsigned(ByteBuffer data, long[] shape)static TensorfromBlobUnsigned(ByteBuffer data, long[] shape, MemoryFormat memoryFormat)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()MemoryFormatmemoryFormat()Returns the memory format of this tensor.longnumel()Returns the number of elements in this tensor.static longnumel(long[] shape)Calculates the number of elements in a tensor with the specified shape.long[]shape()Returns the shape of this tensor.
-
-
-
Method Detail
-
allocateByteBuffer
public static ByteBuffer allocateByteBuffer(int numElements)
Allocates a new directByteBufferwith 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 IntBuffer allocateIntBuffer(int numElements)
Allocates a new directIntBufferwith 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 FloatBuffer allocateFloatBuffer(int numElements)
Allocates a new directFloatBufferwith 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 LongBuffer allocateLongBuffer(int numElements)
Allocates a new directLongBufferwith 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 DoubleBuffer allocateDoubleBuffer(int numElements)
Allocates a new directDoubleBufferwith native byte order with specified capacity that can be used infromBlob(DoubleBuffer, long[]).- Parameters:
numElements- capacity (number of elements) of result buffer.
-
fromBlobUnsigned
public static Tensor fromBlobUnsigned(byte[] data, long[] shape, MemoryFormat memoryFormat)
Creates a new Tensor instance with dtype torch.uint8 with specified shape and data as array of bytes.- Parameters:
data- Tensor elementsshape- Tensor shape
-
fromBlobUnsigned
public static Tensor fromBlobUnsigned(byte[] data, long[] shape)
-
fromBlob
public static Tensor fromBlob(byte[] data, long[] shape, MemoryFormat memoryFormat)
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
public static Tensor fromBlob(byte[] data, long[] shape)
-
fromBlob
public static Tensor fromBlob(int[] data, long[] shape, MemoryFormat memoryFormat)
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
public static Tensor fromBlob(int[] data, long[] shape)
-
fromBlob
public static Tensor fromBlob(float[] data, long[] shape, MemoryFormat memoryFormat)
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
public static Tensor fromBlob(float[] data, long[] shape)
-
fromBlob
public static Tensor fromBlob(long[] data, long[] shape, MemoryFormat memoryFormat)
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
public static Tensor fromBlob(long[] data, long[] shape)
-
fromBlob
public static Tensor fromBlob(double[] data, long[] shape, MemoryFormat memoryFormat)
Creates a new Tensor instance with dtype torch.float64 with specified shape and data as array of doubles.- Parameters:
shape- Tensor shapedata- Tensor elements
-
fromBlob
public static Tensor fromBlob(double[] data, long[] shape)
-
fromBlobUnsigned
public static Tensor fromBlobUnsigned(ByteBuffer data, long[] shape, MemoryFormat memoryFormat)
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
-
fromBlobUnsigned
public static Tensor fromBlobUnsigned(ByteBuffer data, long[] shape)
-
fromBlob
public static Tensor fromBlob(ByteBuffer data, long[] shape, MemoryFormat memoryFormat)
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
public static Tensor fromBlob(ByteBuffer data, long[] shape)
-
fromBlob
public static Tensor fromBlob(IntBuffer data, long[] shape, MemoryFormat memoryFormat)
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
public static Tensor fromBlob(FloatBuffer data, long[] shape, MemoryFormat memoryFormat)
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
public static Tensor fromBlob(FloatBuffer data, long[] shape)
-
fromBlob
public static Tensor fromBlob(LongBuffer data, long[] shape, MemoryFormat memoryFormat)
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
public static Tensor fromBlob(LongBuffer data, long[] shape)
-
fromBlob
public static Tensor fromBlob(DoubleBuffer data, long[] shape, MemoryFormat memoryFormat)
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
-
fromBlob
public static Tensor fromBlob(DoubleBuffer data, long[] 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.)
-
memoryFormat
public MemoryFormat memoryFormat()
Returns the memory format of this tensor.
-
dtype
public abstract DType 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:
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:
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:
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:
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:
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:
IllegalStateException- if it is called for a non-float64 tensor.
-
-