public abstract class Tensor
extends java.lang.Object
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.
Modifier and Type | Method and Description |
---|---|
static java.nio.ByteBuffer |
allocateByteBuffer(int numElements)
Allocates a new direct
ByteBuffer with native byte order with specified
capacity that can be used in fromBlob(ByteBuffer, long[]) , fromBlobUnsigned(ByteBuffer, long[]) . |
static java.nio.DoubleBuffer |
allocateDoubleBuffer(int numElements)
Allocates a new direct
DoubleBuffer with native byte order with specified
capacity that can be used in fromBlob(DoubleBuffer, long[]) . |
static java.nio.FloatBuffer |
allocateFloatBuffer(int numElements)
Allocates a new direct
FloatBuffer with native byte order with specified
capacity that can be used in fromBlob(FloatBuffer, long[]) . |
static java.nio.IntBuffer |
allocateIntBuffer(int numElements)
Allocates a new direct
IntBuffer with native byte order with specified
capacity that can be used in fromBlob(IntBuffer, long[]) . |
static java.nio.LongBuffer |
allocateLongBuffer(int numElements)
Allocates a new direct
LongBuffer with native byte order with specified
capacity that can be used in fromBlob(LongBuffer, long[]) . |
abstract DType |
dtype() |
static Tensor |
fromBlob(byte[] data,
long[] shape) |
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.
|
static Tensor |
fromBlob(java.nio.ByteBuffer data,
long[] shape) |
static Tensor |
fromBlob(java.nio.ByteBuffer data,
long[] shape,
MemoryFormat memoryFormat)
Creates a new Tensor instance with dtype torch.int8 with specified shape and data.
|
static Tensor |
fromBlob(double[] data,
long[] shape) |
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.
|
static Tensor |
fromBlob(java.nio.DoubleBuffer data,
long[] shape) |
static Tensor |
fromBlob(java.nio.DoubleBuffer data,
long[] shape,
MemoryFormat memoryFormat)
Creates a new Tensor instance with dtype torch.float64 with specified shape and data.
|
static Tensor |
fromBlob(float[] data,
long[] shape) |
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.
|
static Tensor |
fromBlob(java.nio.FloatBuffer data,
long[] shape) |
static Tensor |
fromBlob(java.nio.FloatBuffer data,
long[] shape,
MemoryFormat memoryFormat)
Creates a new Tensor instance with dtype torch.float32 with specified shape and data.
|
static Tensor |
fromBlob(int[] data,
long[] shape) |
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.
|
static Tensor |
fromBlob(java.nio.IntBuffer data,
long[] shape) |
static Tensor |
fromBlob(java.nio.IntBuffer data,
long[] shape,
MemoryFormat memoryFormat)
Creates a new Tensor instance with dtype torch.int32 with specified shape and data.
|
static Tensor |
fromBlob(long[] data,
long[] shape) |
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.
|
static Tensor |
fromBlob(java.nio.LongBuffer data,
long[] shape) |
static Tensor |
fromBlob(java.nio.LongBuffer data,
long[] shape,
MemoryFormat memoryFormat)
Creates a new Tensor instance with dtype torch.int64 with specified shape and data.
|
static Tensor |
fromBlobUnsigned(byte[] data,
long[] shape) |
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.
|
static Tensor |
fromBlobUnsigned(java.nio.ByteBuffer data,
long[] shape) |
static Tensor |
fromBlobUnsigned(java.nio.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() |
MemoryFormat |
memoryFormat()
Returns the memory format of this tensor.
|
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.
|
public static java.nio.ByteBuffer allocateByteBuffer(int numElements)
ByteBuffer
with native byte order with specified
capacity that can be used in fromBlob(ByteBuffer, long[])
, fromBlobUnsigned(ByteBuffer, long[])
.numElements
- capacity (number of elements) of result buffer.public static java.nio.IntBuffer allocateIntBuffer(int numElements)
IntBuffer
with native byte order with specified
capacity that can be used in fromBlob(IntBuffer, long[])
.numElements
- capacity (number of elements) of result buffer.public static java.nio.FloatBuffer allocateFloatBuffer(int numElements)
FloatBuffer
with native byte order with specified
capacity that can be used in fromBlob(FloatBuffer, long[])
.numElements
- capacity (number of elements) of result buffer.public static java.nio.LongBuffer allocateLongBuffer(int numElements)
LongBuffer
with native byte order with specified
capacity that can be used in fromBlob(LongBuffer, long[])
.numElements
- capacity (number of elements) of result buffer.public static java.nio.DoubleBuffer allocateDoubleBuffer(int numElements)
DoubleBuffer
with native byte order with specified
capacity that can be used in fromBlob(DoubleBuffer, long[])
.numElements
- capacity (number of elements) of result buffer.public static Tensor fromBlobUnsigned(byte[] data, long[] shape, MemoryFormat memoryFormat)
data
- Tensor elementsshape
- Tensor shapepublic static Tensor fromBlobUnsigned(byte[] data, long[] shape)
public static Tensor fromBlob(byte[] data, long[] shape, MemoryFormat memoryFormat)
data
- Tensor elementsshape
- Tensor shapepublic static Tensor fromBlob(byte[] data, long[] shape)
public static Tensor fromBlob(int[] data, long[] shape, MemoryFormat memoryFormat)
data
- Tensor elementsshape
- Tensor shapepublic static Tensor fromBlob(int[] data, long[] shape)
public static Tensor fromBlob(float[] data, long[] shape, MemoryFormat memoryFormat)
data
- Tensor elementsshape
- Tensor shapepublic static Tensor fromBlob(float[] data, long[] shape)
public static Tensor fromBlob(long[] data, long[] shape, MemoryFormat memoryFormat)
data
- Tensor elementsshape
- Tensor shapepublic static Tensor fromBlob(long[] data, long[] shape)
public static Tensor fromBlob(double[] data, long[] shape, MemoryFormat memoryFormat)
shape
- Tensor shapedata
- Tensor elementspublic static Tensor fromBlob(double[] data, long[] shape)
public static Tensor fromBlobUnsigned(java.nio.ByteBuffer data, long[] shape, MemoryFormat memoryFormat)
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 shapepublic static Tensor fromBlobUnsigned(java.nio.ByteBuffer data, long[] shape)
public static Tensor fromBlob(java.nio.ByteBuffer data, long[] shape, MemoryFormat memoryFormat)
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 shapepublic static Tensor fromBlob(java.nio.ByteBuffer data, long[] shape)
public static Tensor fromBlob(java.nio.IntBuffer data, long[] shape, MemoryFormat memoryFormat)
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 shapepublic static Tensor fromBlob(java.nio.IntBuffer data, long[] shape)
public static Tensor fromBlob(java.nio.FloatBuffer data, long[] shape, MemoryFormat memoryFormat)
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 shapepublic static Tensor fromBlob(java.nio.FloatBuffer data, long[] shape)
public static Tensor fromBlob(java.nio.LongBuffer data, long[] shape, MemoryFormat memoryFormat)
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 shapepublic static Tensor fromBlob(java.nio.LongBuffer data, long[] shape)
public static Tensor fromBlob(java.nio.DoubleBuffer data, long[] shape, MemoryFormat memoryFormat)
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 shapepublic static Tensor fromBlob(java.nio.DoubleBuffer data, long[] shape)
public long numel()
public static long numel(long[] shape)
public long[] shape()
public MemoryFormat memoryFormat()
public abstract DType dtype()
public byte[] getDataAsByteArray()
java.lang.IllegalStateException
- if it is called for a non-int8 tensor.public byte[] getDataAsUnsignedByteArray()
java.lang.IllegalStateException
- if it is called for a non-uint8 tensor.public int[] getDataAsIntArray()
java.lang.IllegalStateException
- if it is called for a non-int32 tensor.public float[] getDataAsFloatArray()
java.lang.IllegalStateException
- if it is called for a non-float32 tensor.public long[] getDataAsLongArray()
java.lang.IllegalStateException
- if it is called for a non-int64 tensor.public double[] getDataAsDoubleArray()
java.lang.IllegalStateException
- if it is called for a non-float64 tensor.