Class Tensor

java.lang.Object
smile.deep.tensor.Tensor
All Implemented Interfaces:
AutoCloseable

public class Tensor extends Object implements AutoCloseable
A Tensor is a multidimensional array containing elements of a single data type.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    A class that encapsulates the construction axes of a Tensor.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Tensor(org.bytedeco.pytorch.Tensor tensor)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a new tensor with the arccosine of the elements of input.
    Computes the arccosine of the elements of input in place.
    add(double other)
    Returns A + b.
    add(float other)
    Returns A + b.
    add(Tensor other)
    Returns A + B.
    add(Tensor other, double alpha)
    Returns A + alpha * B.
    add_(double other)
    Returns A += b.
    add_(float other)
    Returns A += b.
    add_(Tensor other)
    Returns A += B.
    add_(Tensor other, double alpha)
    Returns A += alpha * B.
    and(Tensor other)
    Returns logical AND of two boolean tensors.
    and_(Tensor other)
    Returns logical AND of two boolean tensors.
    static Tensor
    arange(double start, double end, double 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.
    static Tensor
    arange(float start, float end, float 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.
    static Tensor
    arange(int start, int end, int 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.
    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.
    Returns a new tensor with the arcsine of the elements of input.
    Computes the arcsine of the elements of input in place.
    org.bytedeco.pytorch.Tensor
    Returns the PyTorch tensor object.
    void
    Computes the gradients.
    bernoulli_(double p)
    Draws binary random numbers (0 or 1) from a Bernoulli distribution.
    int
    Returns the byte value when the tensor holds a single value.
     
    void
     
    cos()
    Returns a new tensor with the cosine of the elements of input.
    Computes the cosine of the elements of input in place.
    Returns a new tensor, detached from the current graph.
    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.
    div(Tensor other)
    Returns A / B element wisely.
    div_(double other)
    Returns A /= b.
    div_(float other)
    Returns A /= b.
    div_(Tensor other)
    Returns A /= B element wisely.
    double
    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.
    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.
    eq(Tensor other)
    Computes element-wise equality.
    boolean
    equals(Object other)
     
    exp()
    Returns the exponential of elements in the tensor.
    Returns the exponential of elements in the tensor in place.
    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.
    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 value when the tensor holds a single value.
    gather(int dim, Tensor index)
    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.
    ge(Tensor other)
    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.
    get(Index... indices)
    Returns a portion of tensor given the indices.
    get(Tensor index)
    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.
    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.
    gt(Tensor other)
    Computes element-wise greater-than comparison.
    int
    Returns the int value when the tensor holds a single value.
    le(double other)
    Computes element-wise less-than-or-equal-to comparison.
    le(int other)
    Computes element-wise less-than-or-equal-to comparison.
    le(Tensor other)
    Computes element-wise less-than-or-equal-to comparison.
    long
    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.
    lt(Tensor other)
    Computes element-wise less-than comparison.
    matmul(Tensor other)
    Returns the matrix product of two tensors.
    Returns the mean of all elements in the tensor.
    mul(double other)
    Returns A * b.
    mul(float other)
    Returns A * b.
    mul(Tensor other)
    Returns A * B element wisely.
    mul_(double other)
    Returns A *= b.
    mul_(float other)
    Returns A *= b.
    mul_(Tensor other)
    Returns A *= B element wisely.
    ne(double other)
    Computes element-wise inequality.
    ne(int other)
    Computes element-wise inequality.
    ne(Tensor other)
    Computes element-wise inequality.
    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.
    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.
    or(Tensor other)
    Returns logical OR of two boolean tensors.
    or_(Tensor other)
    Returns logical OR of two boolean tensors.
    permute(long... dims)
    Returns a view of the original tensor input with its dimensions permuted.
    void
    Prints the tensor on the standard output.
    put(Tensor source, Index... indices)
    Updates a portion of tensor.
    put(Tensor source, Tensor index)
    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.
    put_(Tensor source, Index... indices)
    Updates a portion of tensor in place.
    put_(Tensor source, Tensor index)
    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.
    boolean
    Returns true if autograd should record operations on this tensor.
    requireGrad(boolean required)
    Sets if autograd should record operations on this tensor.
    reshape(long... shape)
    Returns a tensor with the same data and number of elements but with the specified shape.
    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.
    long[]
    Returns the shape of the tensor.
    int
    Returns the short value when the tensor holds a single value.
    sin()
    Returns a new tensor with the sine of the elements of input.
    Computes the sine of the elements of input in place.
    long
    size(int dim)
    Returns the size of given dimension.
    sub(double other)
    Returns A -= b.
    sub(float other)
    Returns A - b.
    sub(Tensor other)
    Returns A - B.
    sub(Tensor other, double alpha)
    Returns A - alpha * B.
    sub_(double other)
    Returns A -= b.
    sub_(float other)
    Returns A - b.
    sub_(Tensor other)
    Returns A -= B.
    sub_(Tensor other, double alpha)
    Returns A -= alpha * B.
    sum()
    Returns the sum of all elements in the tensor.
    to(Device device)
    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.
     
    transpose(long dim0, long dim1)
    Returns a tensor that is a transposed version of input.
    unsqueeze(long dim)
    Returns a new tensor with a dimension of size one inserted at the specified position.
    where(Tensor condition, double input, double other)
    Returns a tensor of elements selected from either input or other, depending on condition.
    where(Tensor condition, int input, int other)
    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.

    Methods inherited from class java.lang.Object

    finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Tensor

      public Tensor(org.bytedeco.pytorch.Tensor tensor)
      Constructor.
      Parameters:
      tensor - PyTorch Tensor object.
  • Method Details

    • print

      public void print()
      Prints the tensor on the standard output.
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • clone

      public Tensor clone()
      Overrides:
      clone in class Object
    • asTorch

      public org.bytedeco.pytorch.Tensor asTorch()
      Returns the PyTorch tensor object.
      Returns:
      the PyTorch tensor object.
    • requireGrad

      public Tensor requireGrad(boolean required)
      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.
    • requireGrad

      public boolean requireGrad()
      Returns true if autograd should record operations on this tensor.
      Returns:
      true if autograd should record operations on this tensor.
    • 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.
    • detach

      public 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

      public Tensor to(ScalarType dtype)
      Clone the tensor with a different data type.
      Parameters:
      dtype - the element data type of new tensor.
      Returns:
      The cloned tensor.
    • to

      public Tensor to(Device device)
      Clone the tensor to a device.
      Parameters:
      device - the compute device of new tensor.
      Returns:
      The cloned tensor.
    • to

      public Tensor to(Device device, ScalarType dtype)
      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

      public ScalarType dtype()
      Returns the element data type.
      Returns:
      the element data type.
    • device

      public Device 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.
    • reshape

      public Tensor reshape(long... shape)
      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

      public Tensor 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

      public Tensor flatten(int startDim)
      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

      public Tensor flatten(int startDim, int endDim)
      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_

      public Tensor fill_(int x)
      Fills this tensor with the specified value.
      Parameters:
      x - the value.
      Returns:
      this tensor.
    • fill_

      public Tensor fill_(double x)
      Fills this tensor with the specified value.
      Parameters:
      x - the value.
      Returns:
      this tensor.
    • bernoulli_

      public Tensor bernoulli_(double p)
      Draws binary random numbers (0 or 1) from a Bernoulli distribution.
      Parameters:
      p - Bernoulli probability.
      Returns:
      this tensor.
    • permute

      public Tensor permute(long... dims)
      Returns a view of the original tensor input with its dimensions permuted.
      Parameters:
      dims - The desired ordering of dimensions.
      Returns:
      the permuted tensor.
    • put

      public Tensor put(Tensor source, Index... indices)
      Updates a portion of tensor.
      Parameters:
      source - the new sub-tensor values.
      indices - the indices along the dimensions.
      Returns:
      the output tensor.
    • put

      public Tensor put(Tensor source, Tensor index)
      Updates a portion of tensor.
      Parameters:
      source - the new sub-tensor value.
      index - the sub-tensor index.
      Returns:
      the output tensor.
    • put_

      public Tensor put_(Tensor source, Index... indices)
      Updates a portion of tensor in place.
      Parameters:
      source - the new sub-tensor values.
      indices - the indices along the dimensions.
      Returns:
      this tensor.
    • put_

      public Tensor put_(Tensor source, Tensor index)
      Updates a portion of tensor in place.
      Parameters:
      source - the new sub-tensor value.
      index - the sub-tensor index.
      Returns:
      this tensor.
    • put_

      public Tensor put_(byte x, int... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • put_

      public Tensor put_(byte x, long... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • put_

      public Tensor put_(short x, int... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • put_

      public Tensor put_(short x, long... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • put_

      public Tensor put_(int x, int... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • put_

      public Tensor put_(int x, long... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • put_

      public Tensor put_(long x, int... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • put_

      public Tensor put_(long x, long... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • put_

      public Tensor put_(float x, int... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • put_

      public Tensor put_(float x, long... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • put_

      public Tensor put_(double x, int... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • put_

      public Tensor put_(double x, long... indices)
      Updates an element in place.
      Parameters:
      x - the new element value.
      indices - the element indices.
      Returns:
      this tensor.
    • get

      public Tensor get(int... indices)
      Returns a portion of tensor given the indices.
      Parameters:
      indices - the indices along the dimensions.
      Returns:
      the sub-tensor.
    • get

      public Tensor get(long... indices)
      Returns a portion of tensor given the indices.
      Parameters:
      indices - the indices along the dimensions.
      Returns:
      the sub-tensor.
    • get

      public Tensor get(Index... indices)
      Returns a portion of tensor given the indices.
      Parameters:
      indices - the indices along the dimensions.
      Returns:
      the sub-tensor.
    • get

      public Tensor get(Tensor index)
      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.
    • byteValue

      public int 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 int 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.
    • unsqueeze

      public Tensor unsqueeze(long dim)
      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

      public Tensor transpose(long dim0, long dim1)
      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.
    • argmax

      public Tensor argmax(int dim, boolean keepDim)
      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

      public Tuple2<Tensor,Tensor> topk(int k)
      Returns the k largest elements.
      Parameters:
      k - the number of largest elements.
      Returns:
      the values and indices of the largest k elements.
    • topk

      public Tuple2<Tensor,Tensor> topk(int k, int dim, boolean largest, boolean sorted)
      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.
    • where

      public Tensor where(Tensor condition, int input, int other)
      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

      public Tensor where(Tensor condition, double input, double other)
      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

      public Tensor matmul(Tensor other)
      Returns the matrix product of two tensors.
      Parameters:
      other - another tensor.
      Returns:
      the matrix product of two tensors.
    • eq

      public Tensor eq(int other)
      Computes element-wise equality.
      Parameters:
      other - the sclar to compare.
      Returns:
      the output tensor.
    • eq

      public Tensor eq(double other)
      Computes element-wise equality.
      Parameters:
      other - the scalar to compare.
      Returns:
      the output tensor.
    • eq

      public Tensor eq(Tensor other)
      Computes element-wise equality.
      Parameters:
      other - the tensor to compare.
      Returns:
      the output tensor.
    • ne

      public Tensor ne(int other)
      Computes element-wise inequality.
      Parameters:
      other - the sclar to compare.
      Returns:
      the output tensor.
    • ne

      public Tensor ne(double other)
      Computes element-wise inequality.
      Parameters:
      other - the scalar to compare.
      Returns:
      the output tensor.
    • ne

      public Tensor ne(Tensor other)
      Computes element-wise inequality.
      Parameters:
      other - the tensor to compare.
      Returns:
      the output tensor.
    • lt

      public Tensor lt(double other)
      Computes element-wise less-than comparison.
      Parameters:
      other - the scalar to compare.
      Returns:
      the output tensor.
    • lt

      public Tensor lt(int other)
      Computes element-wise less-than comparison.
      Parameters:
      other - the scalar to compare.
      Returns:
      the output tensor.
    • lt

      public Tensor lt(Tensor other)
      Computes element-wise less-than comparison.
      Parameters:
      other - the tensor to compare.
      Returns:
      the output tensor.
    • le

      public Tensor le(int other)
      Computes element-wise less-than-or-equal-to comparison.
      Parameters:
      other - the scalar to compare.
      Returns:
      the output tensor.
    • le

      public Tensor le(double other)
      Computes element-wise less-than-or-equal-to comparison.
      Parameters:
      other - the scalar to compare.
      Returns:
      the output tensor.
    • le

      public Tensor le(Tensor other)
      Computes element-wise less-than-or-equal-to comparison.
      Parameters:
      other - the tensor to compare.
      Returns:
      the output tensor.
    • gt

      public Tensor gt(int other)
      Computes element-wise greater-than comparison.
      Parameters:
      other - the scalar to compare.
      Returns:
      the output tensor.
    • gt

      public Tensor gt(double other)
      Computes element-wise greater-than comparison.
      Parameters:
      other - the scalar to compare.
      Returns:
      the output tensor.
    • gt

      public Tensor gt(Tensor other)
      Computes element-wise greater-than comparison.
      Parameters:
      other - the tensor to compare.
      Returns:
      the output tensor.
    • ge

      public Tensor ge(int other)
      Computes element-wise greater-than-or-equal-to comparison.
      Parameters:
      other - the scalar to compare.
      Returns:
      the output tensor.
    • ge

      public Tensor ge(double other)
      Computes element-wise greater-than-or-equal-to comparison.
      Parameters:
      other - the scalar to compare.
      Returns:
      the output tensor.
    • ge

      public Tensor ge(Tensor other)
      Computes element-wise greater-than-or-equal-to comparison.
      Parameters:
      other - the tensor to compare.
      Returns:
      the output tensor.
    • sum

      public Tensor sum()
      Returns the sum of all elements in the tensor.
      Returns:
      the sum of all elements.
    • mean

      public Tensor mean()
      Returns the mean of all elements in the tensor.
      Returns:
      the mean of all elements.
    • exp

      public Tensor exp()
      Returns the exponential of elements in the tensor.
      Returns:
      the output tensor.
    • exp_

      public Tensor exp_()
      Returns the exponential of elements in the tensor in place.
      Returns:
      this tensor.
    • scatterReduce

      public 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. 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_

      public 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. 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

      public Tensor gather(int dim, Tensor index)
      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

      public Tensor add(float other)
      Returns A + b.
      Parameters:
      other - a scalar value.
      Returns:
      the output tensor.
    • add_

      public Tensor add_(float other)
      Returns A += b.
      Parameters:
      other - a scalar value.
      Returns:
      this tensor.
    • add

      public Tensor add(double other)
      Returns A + b.
      Parameters:
      other - a scalar value.
      Returns:
      the output tensor.
    • add_

      public Tensor add_(double other)
      Returns A += b.
      Parameters:
      other - a scalar value.
      Returns:
      this tensor.
    • add

      public Tensor add(Tensor other)
      Returns A + B.
      Parameters:
      other - another tensor.
      Returns:
      the output tensor.
    • add_

      public Tensor add_(Tensor other)
      Returns A += B.
      Parameters:
      other - another tensor.
      Returns:
      this tensor.
    • add

      public Tensor add(Tensor other, double alpha)
      Returns A + alpha * B.
      Parameters:
      other - another tensor.
      alpha - the scaling factor.
      Returns:
      the output tensor.
    • add_

      public Tensor add_(Tensor other, double alpha)
      Returns A += alpha * B.
      Parameters:
      other - another tensor.
      alpha - the scaling factor.
      Returns:
      this tensor.
    • sub

      public Tensor sub(float other)
      Returns A - b.
      Parameters:
      other - a scalar value.
      Returns:
      the output tensor.
    • sub_

      public Tensor sub_(float other)
      Returns A - b.
      Parameters:
      other - a scalar value.
      Returns:
      the output tensor.
    • sub

      public Tensor sub(double other)
      Returns A -= b.
      Parameters:
      other - a scalar value.
      Returns:
      this tensor.
    • sub_

      public Tensor sub_(double other)
      Returns A -= b.
      Parameters:
      other - a scalar value.
      Returns:
      this tensor.
    • sub

      public Tensor sub(Tensor other)
      Returns A - B.
      Parameters:
      other - another tensor.
      Returns:
      the output tensor.
    • sub_

      public Tensor sub_(Tensor other)
      Returns A -= B.
      Parameters:
      other - another tensor.
      Returns:
      this tensor.
    • sub

      public Tensor sub(Tensor other, double alpha)
      Returns A - alpha * B.
      Parameters:
      other - another tensor.
      alpha - the scaling factor.
      Returns:
      the output tensor.
    • sub_

      public Tensor sub_(Tensor other, double alpha)
      Returns A -= alpha * B.
      Parameters:
      other - another tensor.
      alpha - the scaling factor.
      Returns:
      this tensor.
    • mul

      public Tensor mul(float other)
      Returns A * b.
      Parameters:
      other - a scalar value.
      Returns:
      the output tensor.
    • mul_

      public Tensor mul_(float other)
      Returns A *= b.
      Parameters:
      other - a scalar value.
      Returns:
      this tensor.
    • mul

      public Tensor mul(double other)
      Returns A * b.
      Parameters:
      other - a scalar value.
      Returns:
      the output tensor.
    • mul_

      public Tensor mul_(double other)
      Returns A *= b.
      Parameters:
      other - a scalar value.
      Returns:
      this tensor.
    • mul

      public Tensor mul(Tensor other)
      Returns A * B element wisely.
      Parameters:
      other - another tensor.
      Returns:
      the output tensor.
    • mul_

      public Tensor mul_(Tensor other)
      Returns A *= B element wisely.
      Parameters:
      other - another tensor.
      Returns:
      this tensor.
    • div

      public Tensor div(float other)
      Returns A / b.
      Parameters:
      other - a scalar value.
      Returns:
      the output tensor.
    • div_

      public Tensor div_(float other)
      Returns A /= b.
      Parameters:
      other - a scalar value.
      Returns:
      this tensor.
    • div

      public Tensor div(double other)
      Returns A / b.
      Parameters:
      other - a scalar value.
      Returns:
      the output tensor.
    • div_

      public Tensor div_(double other)
      Returns A /= b.
      Parameters:
      other - a scalar value.
      Returns:
      this tensor.
    • div

      public Tensor div(Tensor other)
      Returns A / B element wisely.
      Parameters:
      other - another tensor.
      Returns:
      the output tensor.
    • div_

      public Tensor div_(Tensor other)
      Returns A /= B element wisely.
      Parameters:
      other - another tensor.
      Returns:
      this tensor.
    • cos

      public 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_

      public Tensor cos_()
      Computes the cosine of the elements of input in place.
      Returns:
      this tensor.
    • sin

      public 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_

      public Tensor sin_()
      Computes the sine of the elements of input in place.
      Returns:
      this tensor.
    • acos

      public 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_

      public Tensor acos_()
      Computes the arccosine of the elements of input in place.
      Returns:
      this tensor.
    • asin

      public 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_

      public Tensor asin_()
      Computes the arcsine of the elements of input in place.
      Returns:
      this tensor.
    • and

      public Tensor and(Tensor other)
      Returns logical AND of two boolean tensors.
      Parameters:
      other - another tensor.
      Returns:
      a new tensor of logical and results.
    • and_

      public Tensor and_(Tensor other)
      Returns logical AND of two boolean tensors.
      Parameters:
      other - another tensor.
      Returns:
      this tensor.
    • or

      public Tensor or(Tensor other)
      Returns logical OR of two boolean tensors.
      Parameters:
      other - another tensor.
      Returns:
      a new tensor of logical and results.
    • or_

      public Tensor or_(Tensor other)
      Returns logical OR of two boolean tensors.
      Parameters:
      other - another tensor.
      Returns:
      this tensor.
    • dropout

      public Tensor dropout(double p)
      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_

      public Tensor dropout_(double p)
      Randomly zeroes some elements in place with probability p.
      Parameters:
      p - the probability of an element to be zeroed.
      Returns:
      this tensor.
    • newZeros

      public Tensor newZeros(long... shape)
      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

      public Tensor newOnes(long... shape)
      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

      public static Tensor eye(long shape)
      Returns an identity matrix.
      Parameters:
      shape - the dimension of the resulting matrix.
      Returns:
      the created tensor.
    • eye

      public static Tensor eye(Tensor.Options options, long shape)
      Returns an identity matrix.
      Parameters:
      options - Tensor creation options.
      shape - the dimension of the resulting matrix.
      Returns:
      the created tensor.
    • empty

      public static Tensor empty(long... shape)
      Returns a tensor with uninitialized data.
      Parameters:
      shape - the dimensional shape of the resulting tensor.
      Returns:
      the created tensor.
    • empty

      public static Tensor empty(Tensor.Options options, long... shape)
      Returns a tensor with uninitialized data.
      Parameters:
      options - Tensor creation options.
      shape - the dimensional shape of the resulting tensor.
      Returns:
      the created tensor.
    • zeros

      public static Tensor zeros(long... shape)
      Returns a tensor filled with all zeros.
      Parameters:
      shape - the dimensional shape of the resulting tensor.
      Returns:
      the created tensor.
    • zeros

      public static Tensor zeros(Tensor.Options options, long... shape)
      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

      public static Tensor ones(long... shape)
      Returns a tensor filled with all ones.
      Parameters:
      shape - the dimensional shape of the resulting tensor.
      Returns:
      the created tensor.
    • ones

      public static Tensor ones(Tensor.Options options, long... shape)
      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

      public static Tensor rand(long... shape)
      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

      public static Tensor rand(Tensor.Options options, long... shape)
      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

      public static Tensor randn(long... shape)
      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

      public static Tensor randn(Tensor.Options options, long... shape)
      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

      public static Tensor arange(int start, int end, int 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.
      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.
    • arange

      public 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.
      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.
    • arange

      public static Tensor arange(float start, float end, float 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.

      Note that step is subject to floating point rounding errors when comparing against end. To avoid inconsistency, we advise subtracting a small epsilon from end in such cases.

      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.
    • arange

      public static Tensor arange(double start, double end, double 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.

      Note that step is subject to floating point rounding errors when comparing against end. To avoid inconsistency, we advise subtracting a small epsilon from end in such cases.

      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

      public static Tensor of(byte[] data, long... shape)
      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

      public static Tensor of(short[] data, long... shape)
      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

      public static Tensor of(int[] data, long... shape)
      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

      public static Tensor of(long[] data, long... shape)
      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

      public static Tensor of(float[] data, long... shape)
      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

      public static Tensor of(double[] data, long... shape)
      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.