Class Tensor
- All Implemented Interfaces:
AutoCloseable
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
A class that encapsulates the construction axes of a tensor. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionacos()
Returns a new tensor with the arccosine of the elements of input.acos_()
Computes the arccosine of the elements of input in place.add
(double other) Returns A + b.add
(float other) Returns A + b.Returns A + B.Returns A + alpha * B.add_
(double other) Returns A += b.add_
(float other) Returns A += b.Returns A += B.Returns A += alpha * B.boolean
all()
Tests if all elements in the tensor are true.Returns logical AND of two boolean tensors.Returns logical AND of two boolean tensors.static Tensor
arange
(long start, long end, long step) Returns a 1-D tensor of size (end - start) / step with values from the interval [start, end) taken with common difference step beginning from start.argmax
(int dim, boolean keepDim) Returns the indices of the maximum value of a tensor across a dimension.asin()
Returns a new tensor with the arcsine of the elements of input.asin_()
Computes the arcsine of the elements of input in place.org.bytedeco.pytorch.Tensor
asTorch()
Returns the PyTorch tensor object.void
backward()
Computes the gradients.bernoulli_
(double p) Draws binary random numbers (0 or 1) from a Bernoulli distribution.boolean
Returns the boolean value when the tensor holds a single value.byte[]
Returns the byte array of tensor elementsbyte
Returns the byte value when the tensor holds a single value.void
close()
Returns a contiguous in memory tensor containing the same data as this tensor.cos()
Returns a new tensor with the cosine of the elements of input.cos_()
Computes the cosine of the elements of input in place.static Tensor
crossEntropy
(Tensor input, Tensor target, String reduction, long ignoreIndex) Computes the cross entropy loss between input logits and target.detach()
Returns a new tensor, detached from the current graph.device()
Returns the device on which the tensor is.int
dim()
Returns the number of dimensions of tensor.div
(double other) Returns A / b.div
(float other) Returns A / b.Returns A / B element wisely.div_
(double other) Returns A /= b.div_
(float other) Returns A /= b.Returns A /= B element wisely.double[]
Returns the double array of tensor elementsdouble
Returns the double value when the tensor holds a single value.dropout
(double p) Randomly zeroes some elements of the input tensor with probability p.dropout_
(double p) Randomly zeroes some elements in place with probability p.dtype()
Returns the element data type.static Tensor
empty
(long... shape) Returns a tensor with uninitialized data.static Tensor
empty
(Tensor.Options options, long... shape) Returns a tensor with uninitialized data.eq
(double other) Computes element-wise equality.eq
(int other) Computes element-wise equality.Computes element-wise equality.boolean
exp()
Returns the exponential of elements in the tensor.exp_()
Returns the exponential of elements in the tensor in place.expand
(long... size) Returns a new view of this tensor with singleton dimensions expanded to a larger size.static Tensor
eye
(long shape) Returns an identity matrix.static Tensor
eye
(Tensor.Options options, long shape) Returns an identity matrix.fill_
(double x) Fills this tensor with the specified value.fill_
(int x) Fills this tensor with the specified value.flatten()
Flattens the tensor by reshaping it into a one-dimensional tensor.flatten
(int startDim) Flattens the tensor by reshaping it into a one-dimensional tensor.flatten
(int startDim, int endDim) Flattens the tensor by reshaping it into a one-dimensional tensor.float[]
Returns the float array of tensor elementsfloat
Returns the float value when the tensor holds a single value.static Tensor
full
(double value, long... shape) Returns a tensor filled with the given value.static Tensor
full
(long value, long... shape) Returns a tensor filled with the given value.Gathers values along an axis specified by dim.ge
(double other) Computes element-wise greater-than-or-equal-to comparison.ge
(int other) Computes element-wise greater-than-or-equal-to comparison.Computes element-wise greater-than-or-equal-to comparison.get
(int... indices) Returns a portion of tensor given the indices.get
(long... indices) Returns a portion of tensor given the indices.Returns a portion of tensor given the indices.Returns a portion of tensor given the indices.byte
getByte
(int... indices) Returns the byte value of element at given index.byte
getByte
(long... indices) Returns the byte value of element at given index.double
getDouble
(int... indices) Returns the double value of element at given index.double
getDouble
(long... indices) Returns the double value of element at given index.float
getFloat
(int... indices) Returns the float value of element at given index.float
getFloat
(long... indices) Returns the float value of element at given index.int
getInt
(int... indices) Returns the int value of element at given index.int
getInt
(long... indices) Returns the int value of element at given index.long
getLong
(int... indices) Returns the long value of element at given index.long
getLong
(long... indices) Returns the long value of element at given index.boolean
Returns true if autograd should record operations on this tensor.short
getShort
(int... indices) Returns the short value of element at given index.short
getShort
(long... indices) Returns the short value of element at given index.gt
(double other) Computes element-wise greater-than comparison.gt
(int other) Computes element-wise greater-than comparison.Computes element-wise greater-than comparison.static Tensor
Stacks tensors in sequence horizontally (column wise).int[]
intArray()
Returns the integer array of tensor elementsint
intValue()
Returns the int value when the tensor holds a single value.static boolean
Checks if the CUDA device supports bf16.Tests if each element of this tensor is in other tensor.le
(double other) Computes element-wise less-than-or-equal-to comparison.le
(int other) Computes element-wise less-than-or-equal-to comparison.Computes element-wise less-than-or-equal-to comparison.long
length()
Returns the number of tensor elements.long[]
Returns the long integer array of tensor elementslong
Returns the long value when the tensor holds a single value.lt
(double other) Computes element-wise less-than comparison.lt
(int other) Computes element-wise less-than comparison.Computes element-wise less-than comparison.Returns the matrix product of two tensors.mean()
Returns the mean of all elements in the tensor.mean
(int dim, boolean keepDim) Returns the mean along a dimension in the tensor.mul
(double other) Returns A * b.mul
(float other) Returns A * b.Returns A * B element wisely.mul_
(double other) Returns A *= b.mul_
(float other) Returns A *= b.Returns A *= B element wisely.ne
(double other) Computes element-wise inequality.ne
(int other) Computes element-wise inequality.Computes element-wise inequality.neg()
Returns a new tensor with the negative of the elements of input.neg_()
Returns the tensor with the negative of the elements of input.newOnes
(long... shape) Returns a tensor filled with all ones.newZeros
(long... shape) Returns a tensor filled with all zeros.static org.bytedeco.pytorch.NoGradGuard
Disables gradient calculation.not()
Returns logical NOT of this tensor.not_()
Returns logical NOT of this tensor.static Tensor
of
(boolean[] data, long... shape) Returns a tensor with given data and shape.static Tensor
of
(byte[] data, long... shape) Returns a tensor with given data and shape.static Tensor
of
(double[] data, long... shape) Returns a tensor with given data and shape.static Tensor
of
(float[] data, long... shape) Returns a tensor with given data and shape.static Tensor
of
(int[] data, long... shape) Returns a tensor with given data and shape.static Tensor
of
(long[] data, long... shape) Returns a tensor with given data and shape.static Tensor
of
(short[] data, long... shape) Returns a tensor with given data and shape.static Tensor
ones
(long... shape) Returns a tensor filled with all ones.static Tensor
ones
(Tensor.Options options, long... shape) Returns a tensor filled with all ones.Returns logical OR of two boolean tensors.Returns logical OR of two boolean tensors.Returns the outer product of two tensors.permute
(long... dims) Returns a view of the original tensor input with its dimensions permuted.static Tensor
Returns a complex tensor whose elements are Cartesian coordinates corresponding to the polar coordinates with abs and angle.static AutoScope
pop()
Removes the scope at the top of the tensor stack.pow
(double exponent) Returns a new tensor with the power of the elements of input.pow_
(double exponent) Computes the power of the elements of input in place.void
print()
Prints the tensor on the standard output.static void
Pushes a scope onto the top of the tensor scope stack.Updates a portion of tensor.Updates a portion of tensor.put_
(byte x, int... indices) Updates an element in place.put_
(byte x, long... indices) Updates an element in place.put_
(double x, int... indices) Updates an element in place.put_
(double x, long... indices) Updates an element in place.put_
(float x, int... indices) Updates an element in place.put_
(float x, long... indices) Updates an element in place.put_
(int x, int... indices) Updates an element in place.put_
(int x, long... indices) Updates an element in place.put_
(long x, int... indices) Updates an element in place.put_
(long x, long... indices) Updates an element in place.put_
(short x, int... indices) Updates an element in place.put_
(short x, long... indices) Updates an element in place.Updates a portion of tensor in place.Updates a portion of tensor in place.static Tensor
rand
(long... shape) Returns a tensor filled with values drawn from a uniform distribution on [0, 1).static Tensor
rand
(Tensor.Options options, long... shape) Returns a tensor filled with values drawn from a uniform distribution on [0, 1).static Tensor
randn
(long... shape) Returns a tensor filled with values drawn from a unit normal distribution.static Tensor
randn
(Tensor.Options options, long... shape) Returns a tensor filled with values drawn from a unit normal distribution.reshape
(long... shape) Returns a tensor with the same data and number of elements but with the specified shape.rsqrt()
Returns the reciprocal of the square-root of each of the elements in the tensor.rsqrt_()
Returns the reciprocal of the square-root of each of the elements in the tensor.scatterReduce
(int dim, Tensor index, Tensor source, String reduce) Writes all values from the tensor src into this tensor at the indices specified in the index tensor.scatterReduce_
(int dim, Tensor index, Tensor source, String reduce) Writes all values from the tensor src into this tensor at the indices specified in the index tensor.static void
setDefaultOptions
(Tensor.Options options) Sets the default options to create tensors.setRequireGrad
(boolean required) Sets if autograd should record operations on this tensor.long[]
shape()
Returns the shape of the tensor.short[]
Returns the short integer array of tensor elementsshort
Returns the short value when the tensor holds a single value.sin()
Returns a new tensor with the sine of the elements of input.sin_()
Computes the sine of the elements of input in place.long
size
(int dim) Returns the size of given dimension.softmax
(int dim) Rescales a tensor so that the elements lie in the range [0,1] and sum to 1.sub
(double other) Returns A -= b.sub
(float other) Returns A - b.Returns A - B.Returns A - alpha * B.sub_
(double other) Returns A -= b.sub_
(float other) Returns A - b.Returns A -= B.Returns A -= alpha * B.sum()
Returns the sum of all elements in the tensor.Clone the tensor to a device.to
(Device device, ScalarType dtype) Clone the tensor to a device with a different data type.to
(ScalarType dtype) Clone the tensor with a different data type.topk
(int k) Returns the k largest elements.topk
(int k, int dim, boolean largest, boolean sorted) Returns the k largest elements along a given dimension.topp
(double p) Performs top-p (nucleus) sampling on a probability distribution.toString()
transpose
(long dim0, long dim1) Returns a tensor that is a transposed version of input.triu
(long diagonal) Returns the upper triangular part of a matrix (2-D tensor) or batch of matrices input, the other elements of the result tensor out are set to 0.triu_
(long diagonal) Returns the upper triangular part of a matrix (2-D tensor) or batch of matrices input, the other elements of the result tensor out are set to 0.unsqueeze
(long dim) Returns a new tensor with a dimension of size one inserted at the specified position.view
(long... shape) Returns a view tensor that shares the same underlying data with this base tensor.Returns a view of tensor as a complex tensor.Returns a view of tensor as a real tensor.static Tensor
Stacks tensors in sequence vertically (row wise).static Tensor
Returns a tensor of elements selected from either input or other, depending on condition.static Tensor
Returns a tensor of elements selected from either input or other, depending on condition.static Tensor
Returns a tensor of elements selected from either input or other, depending on condition.static Tensor
zeros
(long... shape) Returns a tensor filled with all zeros.static Tensor
zeros
(Tensor.Options options, long... shape) Returns a tensor filled with all zeros.
-
Constructor Details
-
Tensor
public Tensor(org.bytedeco.pytorch.Tensor tensor) Constructor.- Parameters:
tensor
- PyTorch Tensor object.
-
-
Method Details
-
setDefaultOptions
Sets the default options to create tensors. This does not affect factory function calls which are called with an explicit options argument.- Parameters:
options
- the construction options of a tensor.
-
isBF16Supported
public static boolean isBF16Supported()Checks if the CUDA device supports bf16. On pre-ampere hardware bf16 works, but doesn't provide speed-ups compared to fp32 matmul operations, and some matmul operations are failing outright, so this check is more like "guaranteed to work and be performant" than "works somehow".- Returns:
- true if bf16 works and is performant.
-
noGradGuard
public static org.bytedeco.pytorch.NoGradGuard noGradGuard()Disables gradient calculation. Disabling gradient calculation is useful for inference, when you are sure that you will not call backward. It will reduce memory consumption for computations that would otherwise have requireGrad(true).In this mode, the result of every computation will have requireGrad(false), even when the inputs have requireGrad(true).
This context manager is thread-local; it will not affect computation in other threads.
- Returns:
- no grad guard to be used with try-with scope.
-
push
Pushes a scope onto the top of the tensor scope stack. Newly created tensors will be automatically added to this scope.- Parameters:
scope
- a scope to automatically release tensors.
-
pop
Removes the scope at the top of the tensor stack. All tensors added to this scope will be released.- Returns:
- the top level scope.
-
print
public void print()Prints the tensor on the standard output. -
equals
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-
toString
-
asTorch
public org.bytedeco.pytorch.Tensor asTorch()Returns the PyTorch tensor object.- Returns:
- the PyTorch tensor object.
-
setRequireGrad
Sets if autograd should record operations on this tensor.- Parameters:
required
- the flag indicating if autograd should record operations on this tensor.- Returns:
- this tensor.
-
getRequireGrad
public boolean getRequireGrad()Returns true if autograd should record operations on this tensor.- Returns:
- true if autograd should record operations on this tensor.
-
detach
Returns a new tensor, detached from the current graph. The result will never require gradient.- Returns:
- a new tensor that doesn't require gradient.
-
to
Clone the tensor with a different data type.- Parameters:
dtype
- the element data type of new tensor.- Returns:
- The cloned tensor.
-
to
Clone the tensor to a device.- Parameters:
device
- the compute device of new tensor.- Returns:
- The cloned tensor.
-
to
Clone the tensor to a device with a different data type.- Parameters:
device
- the compute device of new tensor.dtype
- the element data type of new tensor.- Returns:
- The cloned tensor.
-
dtype
Returns the element data type.- Returns:
- the element data type.
-
device
Returns the device on which the tensor is.- Returns:
- the device.
-
dim
public int dim()Returns the number of dimensions of tensor.- Returns:
- the number of dimensions of tensor
-
shape
public long[] shape()Returns the shape of the tensor.- Returns:
- the shape of the tensor.
-
size
public long size(int dim) Returns the size of given dimension.- Parameters:
dim
- dimension index.- Returns:
- the size of given dimension.
-
length
public long length()Returns the number of tensor elements.- Returns:
- the number of tensor elements.
-
neg
Returns a new tensor with the negative of the elements of input.- Returns:
- the output tensor.
-
neg_
Returns the tensor with the negative of the elements of input.- Returns:
- this tensor.
-
contiguous
Returns a contiguous in memory tensor containing the same data as this tensor.- Returns:
- a contiguous in memory tensor containing the same data as this tensor.
-
expand
Returns a new view of this tensor with singleton dimensions expanded to a larger size.- Parameters:
size
- the desired expanded size.- Returns:
- the tensor view with the expanded size.
-
reshape
Returns a tensor with the same data and number of elements but with the specified shape. This method returns a view if shape is compatible with the current shape.- Parameters:
shape
- the new shape of tensor.- Returns:
- the tensor with the specified shape.
-
flatten
Flattens the tensor by reshaping it into a one-dimensional tensor. This function may return the original object, a view, or copy.- Returns:
- the tensor with the specified shape.
-
flatten
Flattens the tensor by reshaping it into a one-dimensional tensor. Only dimensions starting with startDim and ending with endDim are flattened. The order of elements in input is unchanged. This function may return the original object, a view, or copy.- Parameters:
startDim
- the first dim to flatten.- Returns:
- the tensor with the specified shape.
-
flatten
Flattens the tensor by reshaping it into a one-dimensional tensor. Only dimensions starting with startDim and ending with endDim are flattened. The order of elements in input is unchanged. This function may return the original object, a view, or copy.- Parameters:
startDim
- the first dim to flatten.endDim
- the last dim to flatten- Returns:
- the tensor with the specified shape.
-
backward
public void backward()Computes the gradients. -
fill_
Fills this tensor with the specified value.- Parameters:
x
- the value.- Returns:
- this tensor.
-
fill_
Fills this tensor with the specified value.- Parameters:
x
- the value.- Returns:
- this tensor.
-
bernoulli_
Draws binary random numbers (0 or 1) from a Bernoulli distribution.- Parameters:
p
- Bernoulli probability.- Returns:
- this tensor.
-
permute
Returns a view of the original tensor input with its dimensions permuted.- Parameters:
dims
- The desired ordering of dimensions.- Returns:
- the permuted tensor.
-
put
Updates a portion of tensor.- Parameters:
source
- the new sub-tensor values.indices
- the indices along the dimensions.- Returns:
- the output tensor.
-
put
Updates a portion of tensor.- Parameters:
source
- the new sub-tensor value.index
- the sub-tensor index.- Returns:
- the output tensor.
-
put_
Updates a portion of tensor in place.- Parameters:
source
- the new sub-tensor values.indices
- the indices along the dimensions.- Returns:
- this tensor.
-
put_
Updates a portion of tensor in place.- Parameters:
source
- the new sub-tensor value.index
- the sub-tensor index.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
put_
Updates an element in place.- Parameters:
x
- the new element value.indices
- the element indices.- Returns:
- this tensor.
-
get
Returns a portion of tensor given the indices.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the sub-tensor.
-
get
Returns a portion of tensor given the indices.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the sub-tensor.
-
get
Returns a portion of tensor given the indices.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the sub-tensor.
-
get
Returns a portion of tensor given the indices.- Parameters:
index
- the tensor index.- Returns:
- the sub-tensor.
-
getByte
public byte getByte(int... indices) Returns the byte value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
getByte
public byte getByte(long... indices) Returns the byte value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
getShort
public short getShort(int... indices) Returns the short value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
getShort
public short getShort(long... indices) Returns the short value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
getInt
public int getInt(int... indices) Returns the int value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
getInt
public int getInt(long... indices) Returns the int value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
getLong
public long getLong(int... indices) Returns the long value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
getLong
public long getLong(long... indices) Returns the long value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
getFloat
public float getFloat(int... indices) Returns the float value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
getFloat
public float getFloat(long... indices) Returns the float value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
getDouble
public double getDouble(int... indices) Returns the double value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
getDouble
public double getDouble(long... indices) Returns the double value of element at given index.- Parameters:
indices
- the indices along the dimensions.- Returns:
- the element value.
-
boolValue
public boolean boolValue()Returns the boolean value when the tensor holds a single value.- Returns:
- the boolean value when the tensor holds a single value.
-
byteValue
public byte byteValue()Returns the byte value when the tensor holds a single value.- Returns:
- the byte value when the tensor holds a single value.
-
shortValue
public short shortValue()Returns the short value when the tensor holds a single value.- Returns:
- the short value when the tensor holds a single value.
-
intValue
public int intValue()Returns the int value when the tensor holds a single value.- Returns:
- the int value when the tensor holds a single value.
-
longValue
public long longValue()Returns the long value when the tensor holds a single value.- Returns:
- the long value when the tensor holds a single value.
-
floatValue
public float floatValue()Returns the float value when the tensor holds a single value.- Returns:
- the float value when the tensor holds a single value.
-
doubleValue
public double doubleValue()Returns the double value when the tensor holds a single value.- Returns:
- the double value when the tensor holds a single value.
-
byteArray
public byte[] byteArray()Returns the byte array of tensor elements- Returns:
- the byte array of tensor elements.
-
shortArray
public short[] shortArray()Returns the short integer array of tensor elements- Returns:
- the short integer array of tensor elements.
-
intArray
public int[] intArray()Returns the integer array of tensor elements- Returns:
- the integer array of tensor elements.
-
longArray
public long[] longArray()Returns the long integer array of tensor elements- Returns:
- the long integer array of tensor elements.
-
floatArray
public float[] floatArray()Returns the float array of tensor elements- Returns:
- the float array of tensor elements.
-
doubleArray
public double[] doubleArray()Returns the double array of tensor elements- Returns:
- the double array of tensor elements.
-
unsqueeze
Returns a new tensor with a dimension of size one inserted at the specified position.The returned tensor shares the same underlying data with this tensor.
A dim value within the range [-input.dim() - 1, input.dim() + 1) can be used. Negative dim will correspond to unsqueeze() applied at dim = dim + input.dim() + 1.
- Parameters:
dim
- the index at which to insert the singleton dimension.- Returns:
- the output tensor.
-
transpose
Returns a tensor that is a transposed version of input. The given dimensions dim0 and dim1 are swapped.If input is a strided tensor then the resulting out tensor shares its underlying storage with the input tensor, so changing the content of one would change the content of the other.
If input is a sparse tensor then the resulting out tensor does not share the underlying storage with the input tensor.
If input is a sparse tensor with compressed layout (SparseCSR, SparseBSR, SparseCSC or SparseBSC) the arguments dim0 and dim1 must be both batch dimensions, or must both be sparse dimensions. The batch dimensions of a sparse tensor are the dimensions preceding the sparse dimensions.
- Parameters:
dim0
- the first dimension to be transposed.dim1
- the second dimension to be transposed.- Returns:
- the output tensor.
-
triu
Returns the upper triangular part of a matrix (2-D tensor) or batch of matrices input, the other elements of the result tensor out are set to 0.- Parameters:
diagonal
- The parameter diagonal controls which diagonal to consider. If diagonal = 0, all elements on and above the main diagonal are retained. A positive value excludes just as many diagonals above the main diagonal, and similarly a negative value includes just as many diagonals below the main diagonal.- Returns:
- the output tensor.
-
triu_
Returns the upper triangular part of a matrix (2-D tensor) or batch of matrices input, the other elements of the result tensor out are set to 0.- Parameters:
diagonal
- The parameter diagonal controls which diagonal to consider. If diagonal = 0, all elements on and above the main diagonal are retained. A positive value excludes just as many diagonals above the main diagonal, and similarly a negative value includes just as many diagonals below the main diagonal.- Returns:
- this tensor.
-
view
Returns a view tensor that shares the same underlying data with this base tensor. Supporting View avoids explicit data copy, thus allows us to do fast and memory efficient reshaping, slicing and element-wise operations.- Parameters:
shape
- the shape of view tensor.- Returns:
- the view tensor.
-
viewAsComplex
Returns a view of tensor as a complex tensor.- Returns:
- the complex tensor view.
-
viewAsReal
Returns a view of tensor as a real tensor.- Returns:
- the real tensor view.
-
argmax
Returns the indices of the maximum value of a tensor across a dimension.- Parameters:
dim
- the dimension to reduce.keepDim
- whether the output tensor has dim retained or not.- Returns:
- the indices of the maximum value of a tensor across a dimension.
-
topk
Returns the k largest elements.- Parameters:
k
- the number of largest elements.- Returns:
- the values and indices of the largest k elements.
-
topk
Returns the k largest elements along a given dimension.- Parameters:
k
- the number of largest elements.dim
- the dimension to sort along.largest
- controls whether to return largest or smallest elements.sorted
- controls whether to return the elements in sorted order.- Returns:
- the values and indices of the largest k elements.
-
topp
Performs top-p (nucleus) sampling on a probability distribution. Top-p sampling selects the smallest set of tokens whose cumulative probability mass exceeds the threshold p. The distribution is renormalized based on the selected tokens.- Parameters:
p
- Probability threshold for top-p sampling.- Returns:
- Sampled token indices.
-
hstack
Stacks tensors in sequence horizontally (column wise).- Parameters:
tensors
- the tensors to concatenate.- Returns:
- the output tensor.
-
vstack
Stacks tensors in sequence vertically (row wise).- Parameters:
tensors
- the tensors to concatenate.- Returns:
- the output tensor.
-
polar
Returns a complex tensor whose elements are Cartesian coordinates corresponding to the polar coordinates with abs and angle.- Parameters:
abs
- The absolute value the complex tensor. Must be float or double.angle
- The angle of the complex tensor. Must be same dtype as abs.- Returns:
- the complex tensor.
-
crossEntropy
Computes the cross entropy loss between input logits and target.- Parameters:
input
- Predicted unnormalized logits.target
- Ground truth class indices or class probabilities.reduction
- Specifies the reduction to apply to the output: "none" | "mean" | "sum". "none": no reduction will be applied, "mean": the sum of the output will be divided by the number of elements in the output, "sum": the output will be summed.ignoreIndex
- Specifies a target value that is ignored and does not contribute to the input gradient. Note that ignoreIndex is only applicable when the target contains class indices.- Returns:
- the cross entropy loss between input logits and target.
-
where
Returns a tensor of elements selected from either input or other, depending on condition.- Parameters:
condition
- a boolean tensor. When true (nonzero), yield input, otherwise yield other.input
- value selected at indices where condition is true.other
- value selected at indices where condition is false.- Returns:
- the output tensor.
-
where
Returns a tensor of elements selected from either input or other, depending on condition.- Parameters:
condition
- a boolean tensor. When true (nonzero), yield input, otherwise yield other.input
- value selected at indices where condition is true.other
- value selected at indices where condition is false.- Returns:
- the output tensor.
-
where
Returns a tensor of elements selected from either input or other, depending on condition.- Parameters:
condition
- a boolean tensor. When true (nonzero), yield input, otherwise yield other.input
- value selected at indices where condition is true.other
- value selected at indices where condition is false.- Returns:
- the output tensor.
-
matmul
Returns the matrix product of two tensors.- Parameters:
other
- another tensor.- Returns:
- the matrix product of two tensors.
-
outer
Returns the outer product of two tensors.- Parameters:
other
- another tensor.- Returns:
- the outer product of two tensors.
-
eq
Computes element-wise equality.- Parameters:
other
- the sclar to compare.- Returns:
- the output tensor.
-
eq
Computes element-wise equality.- Parameters:
other
- the scalar to compare.- Returns:
- the output tensor.
-
eq
Computes element-wise equality.- Parameters:
other
- the tensor to compare.- Returns:
- the output tensor.
-
ne
Computes element-wise inequality.- Parameters:
other
- the sclar to compare.- Returns:
- the output tensor.
-
ne
Computes element-wise inequality.- Parameters:
other
- the scalar to compare.- Returns:
- the output tensor.
-
ne
Computes element-wise inequality.- Parameters:
other
- the tensor to compare.- Returns:
- the output tensor.
-
lt
Computes element-wise less-than comparison.- Parameters:
other
- the scalar to compare.- Returns:
- the output tensor.
-
lt
Computes element-wise less-than comparison.- Parameters:
other
- the scalar to compare.- Returns:
- the output tensor.
-
lt
Computes element-wise less-than comparison.- Parameters:
other
- the tensor to compare.- Returns:
- the output tensor.
-
le
Computes element-wise less-than-or-equal-to comparison.- Parameters:
other
- the scalar to compare.- Returns:
- the output tensor.
-
le
Computes element-wise less-than-or-equal-to comparison.- Parameters:
other
- the scalar to compare.- Returns:
- the output tensor.
-
le
Computes element-wise less-than-or-equal-to comparison.- Parameters:
other
- the tensor to compare.- Returns:
- the output tensor.
-
gt
Computes element-wise greater-than comparison.- Parameters:
other
- the scalar to compare.- Returns:
- the output tensor.
-
gt
Computes element-wise greater-than comparison.- Parameters:
other
- the scalar to compare.- Returns:
- the output tensor.
-
gt
Computes element-wise greater-than comparison.- Parameters:
other
- the tensor to compare.- Returns:
- the output tensor.
-
ge
Computes element-wise greater-than-or-equal-to comparison.- Parameters:
other
- the scalar to compare.- Returns:
- the output tensor.
-
ge
Computes element-wise greater-than-or-equal-to comparison.- Parameters:
other
- the scalar to compare.- Returns:
- the output tensor.
-
ge
Computes element-wise greater-than-or-equal-to comparison.- Parameters:
other
- the tensor to compare.- Returns:
- the output tensor.
-
sum
Returns the sum of all elements in the tensor.- Returns:
- the sum of all elements.
-
mean
Returns the mean of all elements in the tensor.- Returns:
- the mean of all elements.
-
mean
Returns the mean along a dimension in the tensor.- Parameters:
dim
- the dimension to reduce.keepDim
- whether the output tensor has dim retained or not.- Returns:
- the output tensor.
-
rsqrt
Returns the reciprocal of the square-root of each of the elements in the tensor.- Returns:
- the output tensor.
-
rsqrt_
Returns the reciprocal of the square-root of each of the elements in the tensor.- Returns:
- this tensor.
-
exp
Returns the exponential of elements in the tensor.- Returns:
- the output tensor.
-
exp_
Returns the exponential of elements in the tensor in place.- Returns:
- this tensor.
-
scatterReduce
Writes all values from the tensor src into this tensor at the indices specified in the index tensor. For each value in src, its output index is specified by its index in src for dimension != dim and by the corresponding value in index for dimension = dim.This is the reverse operation of the manner described in gather().
- Parameters:
dim
- the axis along which to index.index
- the indices of elements to scatter, can be either empty or of the same dimensionality as src. When empty, the operation returns self unchanged.source
- the source elements to scatter and reduce.reduce
- the reduction operation to apply for non-unique indices ("sum", "prod", "mean", "amax", or "amin").- Returns:
- the output tensor.
-
scatterReduce_
Writes all values from the tensor src into this tensor at the indices specified in the index tensor. For each value in src, its output index is specified by its index in src for dimension != dim and by the corresponding value in index for dimension = dim.This is the reverse operation of the manner described in gather().
- Parameters:
dim
- the axis along which to index.index
- the indices of elements to scatter, can be either empty or of the same dimensionality as src. When empty, the operation returns self unchanged.source
- the source elements to scatter and reduce.reduce
- the reduction operation to apply for non-unique indices ("sum", "prod", "mean", "amax", or "amin").- Returns:
- this tensor.
-
gather
Gathers values along an axis specified by dim.- Parameters:
dim
- the axis along which to index.index
- the indices of elements to gather.- Returns:
- the output tensor.
-
add
Returns A + b.- Parameters:
other
- a scalar value.- Returns:
- the output tensor.
-
add_
Returns A += b.- Parameters:
other
- a scalar value.- Returns:
- this tensor.
-
add
Returns A + b.- Parameters:
other
- a scalar value.- Returns:
- the output tensor.
-
add_
Returns A += b.- Parameters:
other
- a scalar value.- Returns:
- this tensor.
-
add
Returns A + B.- Parameters:
other
- another tensor.- Returns:
- the output tensor.
-
add_
Returns A += B.- Parameters:
other
- another tensor.- Returns:
- this tensor.
-
add
Returns A + alpha * B.- Parameters:
other
- another tensor.alpha
- the scaling factor.- Returns:
- the output tensor.
-
add_
Returns A += alpha * B.- Parameters:
other
- another tensor.alpha
- the scaling factor.- Returns:
- this tensor.
-
sub
Returns A - b.- Parameters:
other
- a scalar value.- Returns:
- the output tensor.
-
sub_
Returns A - b.- Parameters:
other
- a scalar value.- Returns:
- the output tensor.
-
sub
Returns A -= b.- Parameters:
other
- a scalar value.- Returns:
- this tensor.
-
sub_
Returns A -= b.- Parameters:
other
- a scalar value.- Returns:
- this tensor.
-
sub
Returns A - B.- Parameters:
other
- another tensor.- Returns:
- the output tensor.
-
sub_
Returns A -= B.- Parameters:
other
- another tensor.- Returns:
- this tensor.
-
sub
Returns A - alpha * B.- Parameters:
other
- another tensor.alpha
- the scaling factor.- Returns:
- the output tensor.
-
sub_
Returns A -= alpha * B.- Parameters:
other
- another tensor.alpha
- the scaling factor.- Returns:
- this tensor.
-
mul
Returns A * b.- Parameters:
other
- a scalar value.- Returns:
- the output tensor.
-
mul_
Returns A *= b.- Parameters:
other
- a scalar value.- Returns:
- this tensor.
-
mul
Returns A * b.- Parameters:
other
- a scalar value.- Returns:
- the output tensor.
-
mul_
Returns A *= b.- Parameters:
other
- a scalar value.- Returns:
- this tensor.
-
mul
Returns A * B element wisely.- Parameters:
other
- another tensor.- Returns:
- the output tensor.
-
mul_
Returns A *= B element wisely.- Parameters:
other
- another tensor.- Returns:
- this tensor.
-
div
Returns A / b.- Parameters:
other
- a scalar value.- Returns:
- the output tensor.
-
div_
Returns A /= b.- Parameters:
other
- a scalar value.- Returns:
- this tensor.
-
div
Returns A / b.- Parameters:
other
- a scalar value.- Returns:
- the output tensor.
-
div_
Returns A /= b.- Parameters:
other
- a scalar value.- Returns:
- this tensor.
-
div
Returns A / B element wisely.- Parameters:
other
- another tensor.- Returns:
- the output tensor.
-
div_
Returns A /= B element wisely.- Parameters:
other
- another tensor.- Returns:
- this tensor.
-
pow
Returns a new tensor with the power of the elements of input.- Parameters:
exponent
- the exponent value.- Returns:
- a new tensor with the power of the elements of input.
-
pow_
Computes the power of the elements of input in place.- Parameters:
exponent
- the exponent value.- Returns:
- this tensor.
-
cos
Returns a new tensor with the cosine of the elements of input.- Returns:
- a new tensor with the cosine of the elements of input.
-
cos_
Computes the cosine of the elements of input in place.- Returns:
- this tensor.
-
sin
Returns a new tensor with the sine of the elements of input.- Returns:
- a new tensor with the sine of the elements of input.
-
sin_
Computes the sine of the elements of input in place.- Returns:
- this tensor.
-
acos
Returns a new tensor with the arccosine of the elements of input.- Returns:
- a new tensor with the arccosine of the elements of input.
-
acos_
Computes the arccosine of the elements of input in place.- Returns:
- this tensor.
-
asin
Returns a new tensor with the arcsine of the elements of input.- Returns:
- a new tensor with the arcsine of the elements of input.
-
asin_
Computes the arcsine of the elements of input in place.- Returns:
- this tensor.
-
isin
Tests if each element of this tensor is in other tensor. Returns a boolean tensor of the same shape.- Parameters:
other
- another tensor.- Returns:
- a boolean tensor.
-
all
public boolean all()Tests if all elements in the tensor are true.- Returns:
- the output tensor.
-
not
Returns logical NOT of this tensor.- Returns:
- a new tensor of logical not results.
-
not_
Returns logical NOT of this tensor.- Returns:
- a new tensor of logical not results.
-
and
Returns logical AND of two boolean tensors.- Parameters:
other
- another tensor.- Returns:
- a new tensor of logical and results.
-
and_
Returns logical AND of two boolean tensors.- Parameters:
other
- another tensor.- Returns:
- this tensor.
-
or
Returns logical OR of two boolean tensors.- Parameters:
other
- another tensor.- Returns:
- a new tensor of logical and results.
-
or_
Returns logical OR of two boolean tensors.- Parameters:
other
- another tensor.- Returns:
- this tensor.
-
softmax
Rescales a tensor so that the elements lie in the range [0,1] and sum to 1.- Parameters:
dim
- the dimension along which softmax will be computed.- Returns:
- this tensor.
-
dropout
Randomly zeroes some elements of the input tensor with probability p.- Parameters:
p
- the probability of an element to be zeroed.- Returns:
- a new tensor after random dropouts.
-
dropout_
Randomly zeroes some elements in place with probability p.- Parameters:
p
- the probability of an element to be zeroed.- Returns:
- this tensor.
-
newZeros
Returns a tensor filled with all zeros. The returned Tensor has the data type and device as this tensor.- Parameters:
shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
newOnes
Returns a tensor filled with all ones. The returned Tensor has the data type and device as this tensor.- Parameters:
shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
eye
Returns an identity matrix.- Parameters:
shape
- the dimension of the resulting matrix.- Returns:
- the created tensor.
-
eye
Returns an identity matrix.- Parameters:
options
- Tensor creation options.shape
- the dimension of the resulting matrix.- Returns:
- the created tensor.
-
full
Returns a tensor filled with the given value.- Parameters:
value
- the value to fill the output tensor with.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
full
Returns a tensor filled with the given value.- Parameters:
value
- the value to fill the output tensor with.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
empty
Returns a tensor with uninitialized data.- Parameters:
shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
empty
Returns a tensor with uninitialized data.- Parameters:
options
- Tensor creation options.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
zeros
Returns a tensor filled with all zeros.- Parameters:
shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
zeros
Returns a tensor filled with all zeros.- Parameters:
options
- Tensor creation options.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
ones
Returns a tensor filled with all ones.- Parameters:
shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
ones
Returns a tensor filled with all ones.- Parameters:
options
- Tensor creation options.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
rand
Returns a tensor filled with values drawn from a uniform distribution on [0, 1).- Parameters:
shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
rand
Returns a tensor filled with values drawn from a uniform distribution on [0, 1).- Parameters:
options
- Tensor creation options.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
randn
Returns a tensor filled with values drawn from a unit normal distribution.- Parameters:
shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
randn
Returns a tensor filled with values drawn from a unit normal distribution.- Parameters:
options
- Tensor creation options.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
arange
Returns a 1-D tensor of size (end - start) / step with values from the interval [start, end) taken with common difference step beginning from start.- Parameters:
start
- the starting value for the set of points.end
- the ending value for the set of points.step
- the gap between each pair of adjacent points.- Returns:
- a 1-D tensor.
-
of
Returns a tensor with given data and shape.- Parameters:
data
- the initialization data.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
of
Returns a tensor with given data and shape.- Parameters:
data
- the initialization data.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
of
Returns a tensor with given data and shape.- Parameters:
data
- the initialization data.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
of
Returns a tensor with given data and shape.- Parameters:
data
- the initialization data.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
of
Returns a tensor with given data and shape.- Parameters:
data
- the initialization data.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
of
Returns a tensor with given data and shape.- Parameters:
data
- the initialization data.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-
of
Returns a tensor with given data and shape.- Parameters:
data
- the initialization data.shape
- the dimensional shape of the resulting tensor.- Returns:
- the created tensor.
-