TensorPlay
API reference
latest (dev)
Copy
View Markdown

Latest development documentation · Updated 2026-10-15

tensorplay.functional API

Functions 605

#

abs

functionFull reference ↗
tensorplay.functional.abs(input: Tensor, *, out: Tensor | None) → Tensor[source]

Computes the absolute value of each element in input.

outi=∣inputi∣\text{out}_{i} = |\text{input}_{i}|
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.abs(tensorplay.tensor([-1, -2, 3]))
tensor([ 1,  2,  3])
#

acos

functionFull reference ↗
tensorplay.functional.acos(input: Tensor, *, out: Tensor | None) → Tensor[source]

Computes the inverse cosine of each element in input.

outi=cos⁡−1(inputi)\text{out}_{i} = \cos^{-1}(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([ 0.3348, -0.5889,  0.2005, -0.1584])
>>> tensorplay.acos(a)
tensor([ 1.2294,  2.2004,  1.3690,  1.7298])
#

acosh

functionFull reference ↗
tensorplay.functional.acosh(input: Tensor, *, out: Tensor | None) → Tensor[source]

Returns a new tensor with the inverse hyperbolic cosine of the elements of input.

outi=cosh⁡−1(inputi)\text{out}_{i} = \cosh^{-1}(\text{input}_{i})

Note

The domain of the inverse hyperbolic cosine is [1, inf) and values outside this range will be mapped to NaN, except for + INF for which the output is mapped to + INF.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4).uniform_(1, 2)
>>> a
tensor([ 1.3192, 1.9915, 1.9674, 1.7151 ])
>>> tensorplay.acosh(a)
tensor([ 0.7791, 1.3120, 1.2979, 1.1341 ])
#

add

functionFull reference ↗
tensorplay.functional.add(input, other, *, alpha=1, out=None) → Tensor[source]

Adds other, scaled by alpha, to input.

outi=inputi+alpha×otheri\text{{out}}_i = \text{{input}}_i + \text{{alpha}} \times \text{{other}}_i

Supports broadcasting to a common shape, type promotion, and integer, float, and complex inputs.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor or Number) – the tensor or number to add to input.

Keyword Arguments:
  • alpha (Number) – the multiplier for other.

  • out (Tensor, optional) – the output tensor.

Examples:

>>> a = tensorplay.randn(4)
>>> a
tensor([ 0.0202,  1.0985,  1.3506, -0.6056])
>>> tensorplay.add(a, 20)
tensor([ 20.0202,  21.0985,  21.3506,  19.3944])

>>> b = tensorplay.randn(4)
>>> b
tensor([-0.9732, -0.3497,  0.6245,  0.4022])
>>> c = tensorplay.randn(4, 1)
>>> c
tensor([[ 0.3743],
        [-1.7724],
        [-0.5811],
        [-0.8017]])
>>> tensorplay.add(b, c, alpha=10)
tensor([[  2.7695,   3.3930,   4.3672,   4.1450],
        [-18.6971, -18.0736, -17.0994, -17.3216],
        [ -6.7845,  -6.1610,  -5.1868,  -5.4090],
        [ -8.9902,  -8.3667,  -7.3925,  -7.6147]])
#

addbmm

functionFull reference ↗
tensorplay.functional.addbmm(input, batch1, batch2, *, beta=1, alpha=1, out=None) → Tensor[source]

Performs a batch matrix-matrix product of matrices stored in batch1 and batch2, with a reduced add step (all matrix multiplications get accumulated along the first dimension). input is added to the final result.

batch1 and batch2 must be 3-D tensors each containing the same number of matrices.

If batch1 is a (b×n×m)(b \times n \times m) tensor, batch2 is a (b×m×p)(b \times m \times p) tensor, input must be broadcastable with a (n×p)(n \times p) tensor and out will be a (n×p)(n \times p) tensor.

out = \beta\ \text{input} + \alpha\ (\sum_{i=0}^{b-1} \text{batch1}_i \mathbin{@} \text{batch2}_i)

If beta is 0, then the content of input will be ignored, and nan and inf in it will not be propagated.

For inputs of type FloatTensor or DoubleTensor, arguments beta and alpha must be real numbers, otherwise they should be integers.

This operator supports TensorFloat32.

On certain ROCm devices, when using float16 inputs this module will use different precision for backward.

Parameters:
  • input (Tensor) – matrix to be added

  • batch1 (Tensor) – the first batch of matrices to be multiplied

  • batch2 (Tensor) – the second batch of matrices to be multiplied

Keyword Arguments:
  • beta (Number, optional) – multiplier for input (β\beta)

  • alpha (Number, optional) – multiplier for batch1 @ batch2 (α\alpha)

  • out (Tensor, optional) – the output tensor.

Example:

>>> M = tensorplay.randn(3, 5)
>>> batch1 = tensorplay.randn(10, 3, 4)
>>> batch2 = tensorplay.randn(10, 4, 5)
>>> tensorplay.addbmm(M, batch1, batch2)
tensor([[  6.6311,   0.0503,   6.9768, -12.0362,  -2.1653],
        [ -4.8185,  -1.4255,  -6.6760,   8.9453,   2.5743],
        [ -3.8202,   4.3691,   1.0943,  -1.1109,   5.4730]])
#

addcdiv

functionFull reference ↗
tensorplay.functional.addcdiv(input, tensor1, tensor2, *, value=1, out=None) → Tensor[source]

Performs the element-wise division of tensor1 by tensor2, multiplies the result by the scalar value and adds it to input.

Warning

Integer division with addcdiv is no longer supported, and in a future release addcdiv will perform a true division of tensor1 and tensor2. The historic addcdiv behavior can be implemented as (input + value * tensorplay.trunc(tensor1 / tensor2)).to(input.dtype) for integer inputs and as (input + value * tensor1 / tensor2) for float inputs. The future addcdiv behavior is just the latter implementation: (input + value * tensor1 / tensor2), for all dtypes.

outi=inputi+value×tensor1itensor2i\text{out}_i = \text{input}_i + \text{value} \times \frac{\text{tensor1}_i}{\text{tensor2}_i}

The shapes of input, tensor1, and tensor2 must be broadcastable.

For inputs of type FloatTensor or DoubleTensor, value must be a real number, otherwise an integer.

Parameters:
  • input (Tensor) – the tensor to be added

  • tensor1 (Tensor) – the numerator tensor

  • tensor2 (Tensor) – the denominator tensor

Keyword Arguments:
  • value (Number, optional) – multiplier for tensor1/tensor2\text{tensor1} / \text{tensor2}

  • out (Tensor, optional) – the output tensor.

Example:

>>> t = tensorplay.randn(1, 3)
>>> t1 = tensorplay.randn(3, 1)
>>> t2 = tensorplay.randn(1, 3)
>>> tensorplay.addcdiv(t, t1, t2, value=0.1)
tensor([[-0.2312, -3.6496,  0.1312],
        [-1.0428,  3.4292, -0.1030],
        [-0.5369, -0.9829,  0.0430]])
#

addcmul

functionFull reference ↗
tensorplay.functional.addcmul(input, tensor1, tensor2, *, value=1, out=None) → Tensor[source]

Performs the element-wise multiplication of tensor1 by tensor2, multiplies the result by the scalar value and adds it to input.

outi=inputi+value×tensor1i×tensor2i\text{out}_i = \text{input}_i + \text{value} \times \text{tensor1}_i \times \text{tensor2}_i

The shapes of tensor, tensor1, and tensor2 must be broadcastable.

For inputs of type FloatTensor or DoubleTensor, value must be a real number, otherwise an integer.

Parameters:
  • input (Tensor) – the tensor to be added

  • tensor1 (Tensor) – the tensor to be multiplied

  • tensor2 (Tensor) – the tensor to be multiplied

Keyword Arguments:
  • value (Number, optional) – multiplier for tensor1.∗tensor2tensor1 .* tensor2

  • out (Tensor, optional) – the output tensor.

Example:

>>> t = tensorplay.randn(1, 3)
>>> t1 = tensorplay.randn(3, 1)
>>> t2 = tensorplay.randn(1, 3)
>>> tensorplay.addcmul(t, t1, t2, value=0.1)
tensor([[-0.8635, -0.6391,  1.6174],
        [-0.7617, -0.5879,  1.7388],
        [-0.8353, -0.6249,  1.6511]])
#

addmm

functionFull reference ↗
tensorplay.functional.addmm(input, mat1, mat2, out_dtype=None, *, beta=1, alpha=1, out=None) → Tensor[source]

Performs a matrix multiplication of the matrices mat1 and mat2. The matrix input is added to the final result.

If mat1 is a (n×m)(n \times m) tensor, mat2 is a (m×p)(m \times p) tensor, then input must be broadcastable with a (n×p)(n \times p) tensor and out will be a (n×p)(n \times p) tensor.

alpha and beta are scaling factors on matrix-vector product between mat1 and mat2 and the added matrix input respectively.

\text{out} = \beta\ \text{input} + \alpha\ (\text{mat1}_i \mathbin{@} \text{mat2}_i)

If beta is 0, then the content of input will be ignored, and nan and inf in it will not be propagated.

For inputs of type FloatTensor or DoubleTensor, arguments beta and alpha must be real numbers, otherwise they should be integers.

This operation has support for arguments with sparse layouts. If input is sparse the result will have the same layout and if out is provided it must have the same layout as input.

Warning

Sparse support is a beta feature and some layout(s)/dtype/device combinations may not be supported, or may not have autograd support. If you notice missing functionality please open a feature request.

This operator supports TensorFloat32.

On certain ROCm devices, when using float16 inputs this module will use different precision for backward.

Parameters:
  • input (Tensor) – matrix to be added

  • mat1 (Tensor) – the first matrix to be matrix multiplied

  • mat2 (Tensor) – the second matrix to be matrix multiplied

  • out_dtype (dtype, optional) – the dtype of the output tensor, Supported only on CUDA and for tensorplay.float32 given tensorplay.float16/tensorplay.bfloat16 input dtypes

Keyword Arguments:
  • beta (Number, optional) – multiplier for input (β\beta)

  • alpha (Number, optional) – multiplier for mat1 @ mat2 (α\alpha)

  • out (Tensor, optional) – the output tensor.

Example:

>>> M = tensorplay.randn(2, 3)
>>> mat1 = tensorplay.randn(2, 3)
>>> mat2 = tensorplay.randn(3, 3)
>>> tensorplay.addmm(M, mat1, mat2)
tensor([[-4.8716,  1.4671, -1.3746],
        [ 0.7573, -3.9555, -2.8681]])
#

addmv

functionFull reference ↗
tensorplay.functional.addmv(input, mat, vec, *, beta=1, alpha=1, out=None) → Tensor[source]

Performs a matrix-vector product of the matrix mat and the vector vec. The vector input is added to the final result.

If mat is a (n×m)(n \times m) tensor, vec is a 1-D tensor of size m, then input must be broadcastable with a 1-D tensor of size n and out will be 1-D tensor of size n.

alpha and beta are scaling factors on matrix-vector product between mat and vec and the added tensor input respectively.

\text{out} = \beta\ \text{input} + \alpha\ (\text{mat} \mathbin{@} \text{vec})

If beta is 0, then the content of input will be ignored, and nan and inf in it will not be propagated.

For inputs of type FloatTensor or DoubleTensor, arguments beta and alpha must be real numbers, otherwise they should be integers.

Parameters:
  • input (Tensor) – vector to be added

  • mat (Tensor) – matrix to be matrix multiplied

  • vec (Tensor) – vector to be matrix multiplied

Keyword Arguments:
  • beta (Number, optional) – multiplier for input (β\beta)

  • alpha (Number, optional) – multiplier for mat @ vec (α\alpha)

  • out (Tensor, optional) – the output tensor.

Example:

>>> M = tensorplay.randn(2)
>>> mat = tensorplay.randn(2, 3)
>>> vec = tensorplay.randn(3)
>>> tensorplay.addmv(M, mat, vec)
tensor([-0.3768, -5.5565])
#

addr

functionFull reference ↗
tensorplay.functional.addr(input, vec1, vec2, *, beta=1, alpha=1, out=None) → Tensor[source]

Performs the outer-product of vectors vec1 and vec2 and adds it to the matrix input.

Optional values beta and alpha are scaling factors on the outer product between vec1 and vec2 and the added matrix input respectively.

out=β input+α (vec1⊗vec2)\text{out} = \beta\ \text{input} + \alpha\ (\text{vec1} \otimes \text{vec2})

If beta is 0, then the content of input will be ignored, and nan and inf in it will not be propagated.

If vec1 is a vector of size n and vec2 is a vector of size m, then input must be broadcastable with a matrix of size (n×m)(n \times m) and out will be a matrix of size (n×m)(n \times m).

Parameters:
  • input (Tensor) – matrix to be added

  • vec1 (Tensor) – the first vector of the outer product

  • vec2 (Tensor) – the second vector of the outer product

Keyword Arguments:
  • beta (Number, optional) – multiplier for input (β\beta)

  • alpha (Number, optional) – multiplier for vec1⊗vec2\text{vec1} \otimes \text{vec2} (α\alpha)

  • out (Tensor, optional) – the output tensor.

Example:

>>> vec1 = tensorplay.arange(1., 4.)
>>> vec2 = tensorplay.arange(1., 3.)
>>> M = tensorplay.zeros(3, 2)
>>> tensorplay.addr(M, vec1, vec2)
tensor([[ 1.,  2.],
        [ 2.,  4.],
        [ 3.,  6.]])
#

adjoint

functionFull reference ↗
tensorplay.functional.adjoint(input: Tensor) → Tensor[source]

Returns a view of the tensor conjugated and with the last two dimensions transposed.

x.adjoint() is equivalent to x.transpose(-2, -1).conj() for complex tensors and to x.transpose(-2, -1) for real tensors.

Parameters:

{input}

Example:

>>> x = tensorplay.arange(4, dtype=tensorplay.float)
>>> A = tensorplay.complex(x, x).reshape(2, 2)
>>> A
tensor([[0.+0.j, 1.+1.j],
        [2.+2.j, 3.+3.j]])
>>> A.adjoint()
tensor([[0.-0.j, 2.-2.j],
        [1.-1.j, 3.-3.j]])
>>> (A.adjoint() == A.mH).all()
tensor(True)
#

all

functionFull reference ↗
tensorplay.functional.all(input: Tensor, *, out=None) → Tensor[source]

Tests if all elements in input evaluate to True.

Note

This function matches the behaviour of NumPy in returning output of dtype bool for all supported dtypes except uint8. For uint8 the dtype of output is uint8 itself.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.rand(1, 2).bool()
>>> a
tensor([[False, True]], dtype=tensorplay.bool)
>>> tensorplay.all(a)
tensor(False, dtype=tensorplay.bool)
>>> a = tensorplay.arange(0, 3)
>>> a
tensor([0, 1, 2])
>>> tensorplay.all(a)
tensor(False)
tensorplay.functional.all(input, dim, keepdim=False, *, out=None) → Tensor[source]

For each row of input in the given dimension dim, returns True if all elements in the row evaluate to True and False otherwise.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) –

    the dimension or dimensions to reduce. If None, all dimensions are reduced.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.rand(4, 2).bool()
>>> a
tensor([[True, True],
        [True, False],
        [True, True],
        [True, True]], dtype=tensorplay.bool)
>>> tensorplay.all(a, dim=1)
tensor([ True, False,  True,  True], dtype=tensorplay.bool)
>>> tensorplay.all(a, dim=0)
tensor([ True, False], dtype=tensorplay.bool)
#

allclose

functionFull reference ↗
tensorplay.functional.allclose(input: Tensor, other: Tensor, rtol: float = 1e-05, atol: float = 1e-08, equal_nan: bool = False) → bool[source]

This function checks if input and other satisfy the condition:

∣inputi−otheri∣≤atol+rtol×∣otheri∣\lvert \text{input}_i - \text{other}_i \rvert \leq \texttt{atol} + \texttt{rtol} \times \lvert \text{other}_i \rvert

elementwise, for all elements of input and other. The behaviour of this function is analogous to numpy.allclose

Parameters:
  • input (Tensor) – first tensor to compare

  • other (Tensor) – second tensor to compare

  • atol (float, optional) – absolute tolerance. Default: 1e-08

  • rtol (float, optional) – relative tolerance. Default: 1e-05

  • equal_nan (bool, optional) – if True, then two NaN s will be considered equal. Default: False

Example:

>>> tensorplay.allclose(tensorplay.tensor([10000., 1e-07]), tensorplay.tensor([10000.1, 1e-08]))
False
>>> tensorplay.allclose(tensorplay.tensor([10000., 1e-08]), tensorplay.tensor([10000.1, 1e-09]))
True
>>> tensorplay.allclose(tensorplay.tensor([1.0, float('nan')]), tensorplay.tensor([1.0, float('nan')]))
False
>>> tensorplay.allclose(tensorplay.tensor([1.0, float('nan')]), tensorplay.tensor([1.0, float('nan')]), equal_nan=True)
True
#

amax

functionFull reference ↗
tensorplay.functional.amax(input, dim, keepdim=False, *, out=None) → Tensor[source]

Returns the maximum value of each slice of the input tensor in the given dimension(s) dim.

Note

The difference between max/min and amax/amin is:
  • amax/amin supports reducing on multiple dimensions,

  • amax/amin does not return indices.

Both amax/amin evenly distribute gradients between equal values when there are multiple input elements with the same minimum or maximum value.

For max/min:
  • If reduce over all dimensions(no dim specified), gradients evenly distribute between equally max/min values.

  • If reduce over one specified axis, only propagate to the indexed element.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) –

    the dimension or dimensions to reduce. If None, all dimensions are reduced.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4, 4)
>>> a
tensor([[ 0.8177,  1.4878, -0.2491,  0.9130],
        [-0.7158,  1.1775,  2.0992,  0.4817],
        [-0.0053,  0.0164, -1.3738, -0.0507],
        [ 1.9700,  1.1106, -1.0318, -1.0816]])
>>> tensorplay.amax(a, 1)
tensor([1.4878, 2.0992, 0.0164, 1.9700])
#

amin

functionFull reference ↗
tensorplay.functional.amin(input, dim, keepdim=False, *, out=None) → Tensor[source]

Returns the minimum value of each slice of the input tensor in the given dimension(s) dim.

Note

The difference between max/min and amax/amin is:
  • amax/amin supports reducing on multiple dimensions,

  • amax/amin does not return indices.

Both amax/amin evenly distribute gradients between equal values when there are multiple input elements with the same minimum or maximum value.

For max/min:
  • If reduce over all dimensions(no dim specified), gradients evenly distribute between equally max/min values.

  • If reduce over one specified axis, only propagate to the indexed element.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) –

    the dimension or dimensions to reduce. If None, all dimensions are reduced.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4, 4)
>>> a
tensor([[ 0.6451, -0.4866,  0.2987, -1.3312],
        [-0.5744,  1.2980,  1.8397, -0.2713],
        [ 0.9128,  0.9214, -1.7268, -0.2995],
        [ 0.9023,  0.4853,  0.9075, -1.6165]])
>>> tensorplay.amin(a, 1)
tensor([-1.3312, -0.5744, -1.7268, -1.6165])
#

angle

functionFull reference ↗
tensorplay.functional.angle(input: Tensor, *, out: Tensor | None) → Tensor[source]

Computes the element-wise angle (in radians) of the given input tensor.

outi=angle(inputi)\text{out}_{i} = angle(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) –

the output tensor.

zero for non-negative real numbers, and propagates NaNs. Previously the function would return zero for all real numbers and not propagate floating-point NaNs.

Example:

>>> tensorplay.angle(tensorplay.tensor([-1 + 1j, -2 + 2j, 3 - 3j]))*180/3.14159
tensor([ 135.,  135,  -45])
#

any

functionFull reference ↗
tensorplay.functional.any(input: Tensor, *, out: Tensor | None) → Tensor[source]

Tests if any element in input evaluates to True.

Note

This function matches the behaviour of NumPy in returning output of dtype bool for all supported dtypes except uint8. For uint8 the dtype of output is uint8 itself.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.rand(1, 2).bool()
>>> a
tensor([[False, True]], dtype=tensorplay.bool)
>>> tensorplay.any(a)
tensor(True, dtype=tensorplay.bool)
>>> a = tensorplay.arange(0, 3)
>>> a
tensor([0, 1, 2])
>>> tensorplay.any(a)
tensor(True)
tensorplay.functional.any(input, dim, keepdim=False, *, out=None) → Tensor[source]

For each row of input in the given dimension dim, returns True if any element in the row evaluate to True and False otherwise.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) –

    the dimension or dimensions to reduce. If None, all dimensions are reduced.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4, 2) < 0
>>> a
tensor([[ True,  True],
        [False,  True],
        [ True,  True],
        [False, False]])
>>> tensorplay.any(a, 1)
tensor([ True,  True,  True, False])
>>> tensorplay.any(a, 0)
tensor([True, True])
#

arange

functionFull reference ↗
tensorplay.functional.arange(start=0, end, step=1, *, out=None, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False) → Tensor[source]

Returns a 1-D tensor of size ⌈end−startstep⌉\left\lceil \frac{\text{end} - \text{start}}{\text{step}} \right\rceil with values from the interval [start, end) taken with common difference step beginning from start.

Note: When using floating-point dtypes (especially reduced precision types like bfloat16), the results may be affected by floating-point rounding behavior. Some values in the sequence might not be exactly representable in certain floating-point formats, which can lead to repeated values or unexpected rounding. For precise sequences, it is recommended to use integer dtypes instead of floating-point dtypes.

Note that non-integer 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.

outi+1=outi+step\text{out}_{{i+1}} = \text{out}_{i} + \text{step}
Parameters:
  • start (Number, optional) – the starting value for the set of points. Default: 0.

  • end (Number) – the ending value for the set of points

  • step (Number, optional) – the gap between each pair of adjacent points. Default: 1.

Keyword Arguments:
  • out (Tensor, optional) – the output tensor.

  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()). If dtype is not given, infer the data type from the other input arguments. If any of start, end, or stop are floating-point, the dtype is inferred to be the default dtype, see get_default_dtype(). Otherwise, the dtype is inferred to be tensorplay.int64.

  • layout (tensorplay.layout, optional) – the desired layout of returned Tensor. Default: tensorplay.strided.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

>>> tensorplay.arange(5)
tensor([ 0,  1,  2,  3,  4])
>>> tensorplay.arange(1, 4)
tensor([ 1,  2,  3])
>>> tensorplay.arange(1, 2.5, 0.5)
tensor([ 1.0000,  1.5000,  2.0000])
#

argmax

functionFull reference ↗
tensorplay.functional.argmax(input) → LongTensor[source]

Returns the indices of the maximum value of all elements in the input tensor.

This is the second value returned by tensorplay.max(). See its documentation for the exact semantics of this method.

Note

If there are multiple maximal values then the indices of the first maximal value are returned.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> a = tensorplay.randn(4, 4)
>>> a
tensor([[ 1.3398,  0.2663, -0.2686,  0.2450],
        [-0.7401, -0.8805, -0.3402, -1.1936],
        [ 0.4907, -1.3948, -1.0691, -0.3132],
        [-1.6092,  0.5419, -0.2993,  0.3195]])
>>> tensorplay.argmax(a)
tensor(0)
tensorplay.functional.argmax(input, dim, keepdim=False) → LongTensor[source]

Returns the indices of the maximum values of a tensor across a dimension.

This is the second value returned by tensorplay.max(). See its documentation for the exact semantics of this method.

Parameters:
  • input (Tensor) – the input tensor.

  • dim – the dimension to reduce.

Example:

>>> a = tensorplay.randn(4, 4)
>>> a
tensor([[ 1.3398,  0.2663, -0.2686,  0.2450],
        [-0.7401, -0.8805, -0.3402, -1.1936],
        [ 0.4907, -1.3948, -1.0691, -0.3132],
        [-1.6092,  0.5419, -0.2993,  0.3195]])
>>> tensorplay.argmax(a, dim=1)
tensor([ 0,  2,  0,  1])
#

argmin

functionFull reference ↗
tensorplay.functional.argmin(input, dim=None, keepdim=False) → LongTensor[source]

Returns the indices of the minimum value(s) of the flattened tensor or along a dimension

This is the second value returned by tensorplay.min(). See its documentation for the exact semantics of this method.

Note

If there are multiple minimal values then the indices of the first minimal value are returned.

Parameters:
  • input (Tensor) – the input tensor.

  • dim – the dimension to reduce.

Example:

>>> a = tensorplay.randn(4, 4)
>>> a
tensor([[ 0.1139,  0.2254, -0.1381,  0.3687],
        [ 1.0100, -1.1975, -0.0102, -0.4732],
        [-0.9240,  0.1207, -0.7506, -1.0213],
        [ 1.7809, -1.2960,  0.9384,  0.1438]])
>>> tensorplay.argmin(a)
tensor(13)
>>> tensorplay.argmin(a, dim=1)
tensor([ 2,  1,  3,  1])
>>> tensorplay.argmin(a, dim=1, keepdim=True)
tensor([[2],
        [1],
        [3],
        [1]])
#

argsort

functionFull reference ↗
tensorplay.functional.argsort(input, dim=-1, descending=False, *, stable=False) → Tensor[source]

Returns the indices that sort a tensor along a given dimension in ascending order by value.

This is the second value returned by tensorplay.sort(). See its documentation for the exact semantics of this method.

If stable is True then the sorting routine becomes stable, preserving the order of equivalent elements. If False, the relative order of values which compare equal is not guaranteed. True is slower.

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int, optional) – the dimension to sort along

  • descending (bool, optional) – controls the sorting order (ascending or descending)

Keyword Arguments:

stable (bool, optional) – controls the relative order of equivalent elements

Example:

>>> a = tensorplay.randn(4, 4)
>>> a
tensor([[ 0.0785,  1.5267, -0.8521,  0.4065],
        [ 0.1598,  0.0788, -0.0745, -1.2700],
        [ 1.2208,  1.0722, -0.7064,  1.2564],
        [ 0.0669, -0.2318, -0.8229, -0.9280]])


>>> tensorplay.argsort(a, dim=1)
tensor([[2, 0, 3, 1],
        [3, 2, 1, 0],
        [2, 1, 0, 3],
        [3, 2, 1, 0]])
#

argwhere

functionFull reference ↗
tensorplay.functional.argwhere(input) → Tensor[source]

Returns a tensor containing the indices of all non-zero elements of input. Each row in the result contains the indices of a non-zero element in input. The result is sorted lexicographically, with the last index changing the fastest (C-style).

If input has nn dimensions, then the resulting indices tensor out is of size (z×n)(z \times n), where zz is the total number of non-zero elements in the input tensor.

Note

This function is similar to NumPy’s argwhere.

When input is on CUDA, this function causes host-device synchronization.

Parameters:

{input}

Example:

>>> t = tensorplay.tensor([1, 0, 1])
>>> tensorplay.argwhere(t)
tensor([[0],
        [2]])
>>> t = tensorplay.tensor([[1, 0, 1], [0, 1, 1]])
>>> tensorplay.argwhere(t)
tensor([[0, 0],
        [0, 2],
        [1, 1],
        [1, 2]])
#

as_strided_copy

functionFull reference ↗
tensorplay.functional.as_strided_copy(input, size, stride, storage_offset=None)[source]

Performs the same operation as tensorplay.as_strided(), but all output tensors are freshly created instead of aliasing the input.

#

as_strided_scatter

functionFull reference ↗
tensorplay.functional.as_strided_scatter(input, src, size, stride, storage_offset=None) → Tensor[source]

Embeds the values of the src tensor into input along the elements corresponding to the result of calling input.as_strided(size, stride, storage_offset).

This function returns a tensor with fresh storage; it does not return a view.

Parameters:
  • input (Tensor) – the input tensor.

  • size (tuple or ints) – the shape of the output tensor

  • stride (tuple or ints) – the stride of the output tensor

  • storage_offset (int, optional) – the offset in the underlying storage of the output tensor

Note

src must be of the proper size in order to be embedded into input. Specifically, it should have the same shape as tensorplay.as_strided(input, size, stride, storage_offset)

Example:

>>> a = tensorplay.arange(4).reshape(2, 2) + 1
>>> a
tensor([[1, 2],
        [3, 4]])
>>> b = tensorplay.zeros(3, 3)
>>> b
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
>>> tensorplay.as_strided_scatter(b, a, (2, 2), (1, 2))
tensor([[1., 3., 2.],
        [4., 0., 0.],
        [0., 0., 0.]])
#

as_strided

functionFull reference ↗
tensorplay.functional.as_strided(input, size, stride, storage_offset=None) → Tensor[source]

Create a view of an existing tensorplay.Tensor input with specified size, stride and storage_offset.

Warning

Prefer using other view functions, like tensorplay.Tensor.view() or tensorplay.Tensor.expand(), to setting a view’s strides manually with as_strided, as this function will throw an error on non-standard Pytensorplay backends (that do not have a concept of stride) and the result will depend on the current layout in memory. The constructed view must only refer to elements within the Tensor’s storage or a runtime error will be thrown. If the generated view is “overlapped” (with multiple indices referring to the same element in memory), the behavior of inplace operations on this view is undefined (and might not throw runtime errors).

Parameters:
  • input (Tensor) – the input tensor.

  • size (tuple or ints) – the shape of the output tensor

  • stride (tuple or ints) – the stride of the output tensor

  • storage_offset (int, optional) – the offset in the underlying storage of the output tensor. If None, the storage_offset of the output tensor will match the input tensor.

Example:

>>> x = tensorplay.randn(3, 3)
>>> x
tensor([[ 0.9039,  0.6291,  1.0795],
        [ 0.1586,  2.1939, -0.4900],
        [-0.1909, -0.7503,  1.9355]])
>>> t = tensorplay.as_strided(x, (2, 2), (1, 2))
>>> t
tensor([[0.9039, 1.0795],
        [0.6291, 0.1586]])
>>> t = tensorplay.as_strided(x, (2, 2), (1, 2), 1)
tensor([[0.6291, 0.1586],
        [1.0795, 2.1939]])
#

asin

functionFull reference ↗
tensorplay.functional.asin(input: Tensor, *, out: Tensor | None) → Tensor[source]

Returns a new tensor with the arcsine of the elements of input.

outi=sin⁡−1(inputi)\text{out}_{i} = \sin^{-1}(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([-0.5962,  1.4985, -0.4396,  1.4525])
>>> tensorplay.asin(a)
tensor([-0.6387,     nan, -0.4552,     nan])
#

asinh

functionFull reference ↗
tensorplay.functional.asinh(input: Tensor, *, out: Tensor | None) → Tensor[source]

Returns a new tensor with the inverse hyperbolic sine of the elements of input.

outi=sinh⁡−1(inputi)\text{out}_{i} = \sinh^{-1}(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([ 0.1606, -1.4267, -1.0899, -1.0250 ])
>>> tensorplay.asinh(a)
tensor([ 0.1599, -1.1534, -0.9435, -0.8990 ])
#

atan

functionFull reference ↗
tensorplay.functional.atan(input: Tensor, *, out: Tensor | None) → Tensor[source]

Returns a new tensor with the arctangent of the elements of input.

outi=tan⁡−1(inputi)\text{out}_{i} = \tan^{-1}(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([ 0.2341,  0.2539, -0.6256, -0.6448])
>>> tensorplay.atan(a)
tensor([ 0.2299,  0.2487, -0.5591, -0.5727])
#

atan2

functionFull reference ↗
tensorplay.functional.atan2(input: Tensor, other: Tensor, *, out: Tensor | None) → Tensor[source]

Element-wise arctangent of inputi/otheri\text{input}_{i} / \text{other}_{i} with consideration of the quadrant. Returns a new tensor with the signed angles in radians between vector (otheri,inputi)(\text{other}_{i}, \text{input}_{i}) and vector (1,0)(1, 0). (Note that otheri\text{other}_{i}, the second parameter, is the x-coordinate, while inputi\text{input}_{i}, the first parameter, is the y-coordinate.)

The shapes of input and other must be broadcastable.

Parameters:
  • input (Tensor) – the first input tensor

  • other (Tensor) – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([ 0.9041,  0.0196, -0.3108, -2.4423])
>>> tensorplay.atan2(a, tensorplay.randn(4))
tensor([ 0.9833,  0.0811, -1.9743, -1.4151])
#

atanh

functionFull reference ↗
tensorplay.functional.atanh(input: Tensor, *, out: Tensor | None) → Tensor[source]

Returns a new tensor with the inverse hyperbolic tangent of the elements of input.

Note

The domain of the inverse hyperbolic tangent is (-1, 1) and values outside this range will be mapped to NaN, except for the values 1 and -1 for which the output is mapped to +/-INF respectively.

outi=tanh⁡−1(inputi)\text{out}_{i} = \tanh^{-1}(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4).uniform_(-1, 1)
>>> a
tensor([ -0.9385, 0.2968, -0.8591, -0.1871 ])
>>> tensorplay.atanh(a)
tensor([ -1.7253, 0.3060, -1.2899, -0.1893 ])
#

avg_pool1d

functionFull reference ↗
tensorplay.functional.avg_pool1d(input, kernel_size, stride=[], padding=0, ceil_mode=False, count_include_pad=True)[source]
#

baddbmm

functionFull reference ↗
tensorplay.functional.baddbmm(input, batch1, batch2, out_dtype=None, *, beta=1, alpha=1, out=None) → Tensor[source]

Performs a batch matrix-matrix product of matrices in batch1 and batch2. input is added to the final result.

batch1 and batch2 must be 3-D tensors each containing the same number of matrices.

If batch1 is a (b×n×m)(b \times n \times m) tensor, batch2 is a (b×m×p)(b \times m \times p) tensor, then input must be broadcastable with a (b×n×p)(b \times n \times p) tensor and out will be a (b×n×p)(b \times n \times p) tensor. Both alpha and beta mean the same as the scaling factors used in tensorplay.addbmm().

\text{out}_i = \beta\ \text{input}_i + \alpha\ (\text{batch1}_i \mathbin{&#64;} \text{batch2}_i)

If beta is 0, then the content of input will be ignored, and nan and inf in it will not be propagated.

For inputs of type FloatTensor or DoubleTensor, arguments beta and alpha must be real numbers, otherwise they should be integers.

This operator supports TensorFloat32.

On certain ROCm devices, when using float16 inputs this module will use different precision for backward.

Parameters:
  • input (Tensor) – the tensor to be added

  • batch1 (Tensor) – the first batch of matrices to be multiplied

  • batch2 (Tensor) – the second batch of matrices to be multiplied

  • out_dtype (dtype, optional) – the dtype of the output tensor, Supported only on CUDA and for tensorplay.float32 given tensorplay.float16/tensorplay.bfloat16 input dtypes

Keyword Arguments:
  • beta (Number, optional) – multiplier for input (β\beta)

  • alpha (Number, optional) – multiplier for \text{batch1} \mathbin{&#64;} \text{batch2} (α\alpha)

  • out (Tensor, optional) – the output tensor.

Example:

>>> M = tensorplay.randn(10, 3, 5)
>>> batch1 = tensorplay.randn(10, 3, 4)
>>> batch2 = tensorplay.randn(10, 4, 5)
>>> tensorplay.baddbmm(M, batch1, batch2).size()
tensorplay.Size([10, 3, 5])
#

bartlett_window

functionFull reference ↗
tensorplay.functional.bartlett_window(window_length, periodic=True, *, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False) → Tensor[source]

Bartlett window function.

w[n]=1−∣2nN−1−1∣={2nN−1if 0≤n≤N−122−2nN−1if N−12<n<N,w[n] = 1 - \left| \frac{2n}{N-1} - 1 \right| = \begin{cases} \frac{2n}{N - 1} & \text{if } 0 \leq n \leq \frac{N - 1}{2} \\ 2 - \frac{2n}{N - 1} & \text{if } \frac{N - 1}{2} < n < N \\ \end{cases},

where NN is the full window size.

The input window_length is a positive integer controlling the returned window size. periodic flag determines whether the returned window trims off the last duplicate value from the symmetric window and is ready to be used as a periodic window with functions like tensorplay.stft(). Therefore, if periodic is true, the NN in above formula is in fact window_length+1\text{window\_length} + 1. Also, we always have tensorplay.bartlett_window(L, periodic=True) equal to tensorplay.bartlett_window(L + 1, periodic=False)[:-1]).

Note

If window_length =1=1, the returned window contains a single value 1.

Parameters:
  • window_length (int) – the size of returned window

  • periodic (bool, optional) – If True, returns a window to be used as periodic function. If False, return a symmetric window.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()). Only floating point types are supported.

  • layout (tensorplay.layout, optional) – the desired layout of returned window tensor. Only tensorplay.strided (dense layout) is supported.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Returns:

A 1-D tensor of size (window_length,)(\text{window\_length},) containing the window

Return type:

Tensor

#

batch_norm_backward_elemt

functionFull reference ↗
tensorplay.functional.batch_norm_backward_elemt(grad_out, input, mean, invstd, weight, sum_dy, sum_dy_xmu, count)[source]
#

batch_norm_backward_reduce

functionFull reference ↗
tensorplay.functional.batch_norm_backward_reduce(grad_out, input, mean, invstd, weight, input_g, weight_g, bias_g)[source]
#

batch_norm_elemt

functionFull reference ↗
tensorplay.functional.batch_norm_elemt(input, weight, bias, mean, invstd, eps, *, out=None)[source]
#

batch_norm_gather_stats_with_counts

functionFull reference ↗
tensorplay.functional.batch_norm_gather_stats_with_counts(input, mean, invstd, running_mean, running_var, momentum, eps, counts)[source]
#

batch_norm_gather_stats

functionFull reference ↗
tensorplay.functional.batch_norm_gather_stats(input, mean, invstd, running_mean, running_var, momentum, eps, count)[source]
#

batch_norm_update_stats

functionFull reference ↗
tensorplay.functional.batch_norm_update_stats(input, running_mean, running_var, momentum)[source]
#

bernoulli

functionFull reference ↗
tensorplay.functional.bernoulli(input: Tensor, *, generator: Generator | None, out: Tensor | None) → Tensor[source]

Draws binary random numbers (0 or 1) from a Bernoulli distribution.

The input tensor should be a tensor containing probabilities to be used for drawing the binary random number. Hence, all values in input have to be in the range: 0≤inputi≤10 \leq \text{input}_i \leq 1.

The ith\text{i}^{th} element of the output tensor will draw a value 11 according to the ith\text{i}^{th} probability value given in input.

outi∼Bernoulli(p=inputi)\text{out}_{i} \sim \mathrm{Bernoulli}(p = \text{input}_{i})

The returned out tensor only has values 0 or 1 and is of the same shape as input.

out can have integral dtype, but input must have floating point dtype.

Parameters:

input (Tensor) – the input tensor of probability values for the Bernoulli distribution

Keyword Arguments:
  • generator (tensorplay.Generator, optional) – a pseudorandom number generator for sampling

  • out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.empty(3, 3).uniform_(0, 1)  # generate a uniform random matrix with range [0, 1]
>>> a
tensor([[ 0.1737,  0.0950,  0.3609],
        [ 0.7148,  0.0289,  0.2676],
        [ 0.9456,  0.8937,  0.7202]])
>>> tensorplay.bernoulli(a)
tensor([[ 1.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 1.,  1.,  1.]])

>>> a = tensorplay.ones(3, 3) # probability of drawing "1" is 1
>>> tensorplay.bernoulli(a)
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])
>>> a = tensorplay.zeros(3, 3) # probability of drawing "1" is 0
>>> tensorplay.bernoulli(a)
tensor([[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]])
#

bincount

functionFull reference ↗
tensorplay.functional.bincount(input, weights=None, minlength=0) → Tensor[source]

Count the frequency of each value in an array of non-negative ints.

The number of bins (size 1) is one larger than the largest value in input unless input is empty, in which case the result is a tensor of size 0. If minlength is specified, the number of bins is at least minlength and if input is empty, then the result is tensor of size minlength filled with zeros. If n is the value at position i, out[n] += weights[i] if weights is specified else out[n] += 1.

Note

This operation may produce nondeterministic gradients when given tensors on a CUDA device. See Reproducibility for more information.

Parameters:
  • input (Tensor) – 1-d int tensor

  • weights (Tensor) – optional, weight for each value in the input tensor. Should be of same size as input tensor.

  • minlength (int) – optional, minimum number of bins. Should be non-negative.

Returns:

a tensor of shape Size([max(input) + 1]) if input is non-empty, else Size(0)

Return type:

output (Tensor)

Example:

>>> input = tensorplay.randint(0, 8, (5,), dtype=tensorplay.int64)
>>> weights = tensorplay.linspace(0, 1, steps=5)
>>> input, weights
(tensor([4, 3, 6, 3, 4]),
 tensor([ 0.0000,  0.2500,  0.5000,  0.7500,  1.0000])

>>> tensorplay.bincount(input)
tensor([0, 0, 0, 2, 2, 0, 1])

>>> input.bincount(weights)
tensor([0.0000, 0.0000, 0.0000, 1.0000, 1.0000, 0.0000, 0.5000])
#

bitwise_and

functionFull reference ↗
tensorplay.functional.bitwise_and(input, other, *, out=None) → Tensor[source]

Computes the bitwise AND of input and other. The input tensor must be of integral or Boolean types. For bool tensors, it computes the logical AND.

Parameters:
  • input – the first input tensor

  • other – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.bitwise_and(tensorplay.tensor([-1, -2, 3], dtype=tensorplay.int8), tensorplay.tensor([1, 0, 3], dtype=tensorplay.int8))
tensor([1, 0,  3], dtype=tensorplay.int8)
>>> tensorplay.bitwise_and(tensorplay.tensor([True, True, False]), tensorplay.tensor([False, True, False]))
tensor([ False, True, False])
#

bitwise_left_shift

functionFull reference ↗
tensorplay.functional.bitwise_left_shift(input, other, *, out=None) → Tensor[source]

Computes the left arithmetic shift of input by other bits. The input tensor must be of integral type. This operator supports broadcasting to a common shape and type promotion.

The operation applied is:

outi=inputi<<otheri\text{out}_i = \text{input}_i << \text{other}_i
Parameters:
Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.bitwise_left_shift(tensorplay.tensor([-1, -2, 3], dtype=tensorplay.int8), tensorplay.tensor([1, 0, 3], dtype=tensorplay.int8))
tensor([-2, -2, 24], dtype=tensorplay.int8)
#

bitwise_not

functionFull reference ↗
tensorplay.functional.bitwise_not(input, *, out=None) → Tensor[source]

Computes the bitwise NOT of the given input tensor. The input tensor must be of integral or Boolean types. For bool tensors, it computes the logical NOT.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.bitwise_not(tensorplay.tensor([-1, -2, 3], dtype=tensorplay.int8))
tensor([ 0,  1, -4], dtype=tensorplay.int8)
#

bitwise_or

functionFull reference ↗
tensorplay.functional.bitwise_or(input: Tensor, other: Tensor, *, out: Tensor | None) → Tensor[source]

Computes the bitwise OR of input and other. The input tensor must be of integral or Boolean types. For bool tensors, it computes the logical OR.

Parameters:
  • input – the first input tensor

  • other – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.bitwise_or(tensorplay.tensor([-1, -2, 3], dtype=tensorplay.int8), tensorplay.tensor([1, 0, 3], dtype=tensorplay.int8))
tensor([-1, -2,  3], dtype=tensorplay.int8)
>>> tensorplay.bitwise_or(tensorplay.tensor([True, True, False]), tensorplay.tensor([False, True, False]))
tensor([ True, True, False])
#

bitwise_right_shift

functionFull reference ↗
tensorplay.functional.bitwise_right_shift(input, other, *, out=None) → Tensor[source]

Computes the right arithmetic shift of input by other bits. The input tensor must be of integral type. This operator supports broadcasting to a common shape and type promotion. In any case, if the value of the right operand is negative or is greater or equal to the number of bits in the promoted left operand, the behavior is undefined.

The operation applied is:

outi=inputi>>otheri\text{out}_i = \text{input}_i >> \text{other}_i
Parameters:
Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.bitwise_right_shift(tensorplay.tensor([-2, -7, 31], dtype=tensorplay.int8), tensorplay.tensor([1, 0, 3], dtype=tensorplay.int8))
tensor([-1, -7,  3], dtype=tensorplay.int8)
#

bitwise_xor

functionFull reference ↗
tensorplay.functional.bitwise_xor(input, other, *, out=None) → Tensor[source]

Computes the bitwise XOR of input and other. The input tensor must be of integral or Boolean types. For bool tensors, it computes the logical XOR.

Parameters:
  • input – the first input tensor

  • other – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.bitwise_xor(tensorplay.tensor([-1, -2, 3], dtype=tensorplay.int8), tensorplay.tensor([1, 0, 3], dtype=tensorplay.int8))
tensor([-2, -2,  0], dtype=tensorplay.int8)
>>> tensorplay.bitwise_xor(tensorplay.tensor([True, True, False]), tensorplay.tensor([False, True, False]))
tensor([ True, False, False])
#

blackman_window

functionFull reference ↗
tensorplay.functional.blackman_window(window_length, periodic=True, *, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False) → Tensor[source]

Blackman window function.

w[n]=0.42−0.5cos⁡(2πnN−1)+0.08cos⁡(4πnN−1)w[n] = 0.42 - 0.5 \cos \left( \frac{2 \pi n}{N - 1} \right) + 0.08 \cos \left( \frac{4 \pi n}{N - 1} \right)

where NN is the full window size.

The input window_length is a positive integer controlling the returned window size. periodic flag determines whether the returned window trims off the last duplicate value from the symmetric window and is ready to be used as a periodic window with functions like tensorplay.stft(). Therefore, if periodic is true, the NN in above formula is in fact window_length+1\text{window\_length} + 1. Also, we always have tensorplay.blackman_window(L, periodic=True) equal to tensorplay.blackman_window(L + 1, periodic=False)[:-1].

Note

If window_length =1=1, the returned window contains a single value 1.

Parameters:
  • window_length (int) – the size of returned window

  • periodic (bool, optional) – If True, returns a window to be used as periodic function. If False, return a symmetric window.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()). Only floating point types are supported.

  • layout (tensorplay.layout, optional) – the desired layout of returned window tensor. Only tensorplay.strided (dense layout) is supported.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Returns:

A 1-D tensor of size (window_length,)(\text{window\_length},) containing the window

Return type:

Tensor

#

bmm

functionFull reference ↗
tensorplay.functional.bmm(input, mat2, out_dtype=None, *, out=None) → Tensor[source]

Performs a batch matrix-matrix product of matrices stored in input and mat2.

input and mat2 must be 3-D tensors each containing the same number of matrices.

If input is a (b×n×m)(b \times n \times m) tensor, mat2 is a (b×m×p)(b \times m \times p) tensor, out will be a (b×n×p)(b \times n \times p) tensor.

\text{out}_i = \text{input}_i \mathbin{&#64;} \text{mat2}_i

This operator supports TensorFloat32.

On certain ROCm devices, when using float16 inputs this module will use different precision for backward.

Note

This function does not broadcast. For broadcasting matrix products, see tensorplay.matmul().

Parameters:
  • input (Tensor) – the first batch of matrices to be multiplied

  • mat2 (Tensor) – the second batch of matrices to be multiplied

  • out_dtype (dtype, optional) – the dtype of the output tensor, Supported only on CUDA and for tensorplay.float32 given tensorplay.float16/tensorplay.bfloat16 input dtypes

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> input = tensorplay.randn(10, 3, 4)
>>> mat2 = tensorplay.randn(10, 4, 5)
>>> res = tensorplay.bmm(input, mat2)
>>> res.size()
tensorplay.Size([10, 3, 5])
#

broadcast_to

functionFull reference ↗
tensorplay.functional.broadcast_to(input, shape) → Tensor[source]

Broadcasts input to the shape shape. Equivalent to calling input.expand(shape). See expand() for details.

Parameters:

Example:

>>> x = tensorplay.tensor([1, 2, 3])
>>> tensorplay.broadcast_to(x, (3, 3))
tensor([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])
#

bucketize

functionFull reference ↗
tensorplay.functional.bucketize(input, boundaries, *, out_int32=False, right=False, out=None) → Tensor[source]

Returns the indices of the buckets to which each value in the input belongs, where the boundaries of the buckets are set by boundaries. Return a new tensor with the same size as input. If right is False (default), then the left boundary is open. Note that this behavior is opposite the behavior of numpy.digitize. More formally, the returned index satisfies the following rules:

right

returned index satisfies

False

boundaries[i-1] < input[m][n]...[l][x] <= boundaries[i]

True

boundaries[i-1] <= input[m][n]...[l][x] < boundaries[i]

Parameters:
  • input (Tensor or Scalar) – N-D tensor or a Scalar containing the search value(s).

  • boundaries (Tensor) – 1-D tensor, must contain a strictly increasing sequence, or the return value is undefined.

Keyword Arguments:
  • out_int32 (bool, optional) – indicate the output data type. tensorplay.int32 if True, tensorplay.int64 otherwise. Default value is False, i.e. default output data type is tensorplay.int64.

  • right (bool, optional) – determines the behavior for values in boundaries. See the table above.

  • out (Tensor, optional) – the output tensor, must be the same size as input if provided.

Example:

>>> boundaries = tensorplay.tensor([1, 3, 5, 7, 9])
>>> boundaries
tensor([1, 3, 5, 7, 9])
>>> v = tensorplay.tensor([[3, 6, 9], [3, 6, 9]])
>>> v
tensor([[3, 6, 9],
        [3, 6, 9]])
>>> tensorplay.bucketize(v, boundaries)
tensor([[1, 3, 4],
        [1, 3, 4]])
>>> tensorplay.bucketize(v, boundaries, right=True)
tensor([[2, 3, 5],
        [2, 3, 5]])
#

can_cast

functionFull reference ↗
tensorplay.functional.can_cast(from_, to) → bool[source]

described in the type promotion documentation.

Parameters:

Example:

>>> tensorplay.can_cast(tensorplay.double, tensorplay.float)
True
>>> tensorplay.can_cast(tensorplay.float, tensorplay.int)
False
#

cat

functionFull reference ↗
tensorplay.functional.cat(tensors, dim=0, *, out=None) → Tensor[source]

Concatenates the given sequence of tensors in tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be a 1-D empty tensor with size (0,).

tensorplay.cat() can be seen as an inverse operation for tensorplay.split() and tensorplay.chunk().

tensorplay.cat() can be best understood via examples.

See also

tensorplay.stack() concatenates the given sequence along a new dimension.

Parameters:
  • tensors (sequence of Tensors) – Non-empty tensors provided must have the same shape, except in the cat dimension.

  • dim (int, optional) – the dimension over which the tensors are concatenated

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> x = tensorplay.randn(2, 3)
>>> x
tensor([[ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497]])
>>> tensorplay.cat((x, x, x), 0)
tensor([[ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497],
        [ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497],
        [ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497]])
>>> tensorplay.cat((x, x, x), 1)
tensor([[ 0.6580, -1.0969, -0.4614,  0.6580, -1.0969, -0.4614,  0.6580,
         -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497, -0.1034, -0.5790,  0.1497, -0.1034,
         -0.5790,  0.1497]])
#

ceil

functionFull reference ↗
tensorplay.functional.ceil(input, *, out=None) → Tensor[source]

Returns a new tensor with the ceil of the elements of input, the smallest integer greater than or equal to each element.

For integer inputs, follows the array-api convention of returning a copy of the input tensor.

outi=⌈inputi⌉\text{out}_{i} = \left\lceil \text{input}_{i} \right\rceil
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([-0.6341, -1.4208, -1.0900,  0.5826])
>>> tensorplay.ceil(a)
tensor([-0., -1., -1.,  1.])
#

cholesky_inverse

functionFull reference ↗
tensorplay.functional.cholesky_inverse(L, upper=False, *, out=None) → Tensor[source]

Computes the inverse of a complex Hermitian or real symmetric positive-definite matrix given its Cholesky decomposition.

Let AA be a complex Hermitian or real symmetric positive-definite matrix, and LL its Cholesky decomposition such that:

A=LLHA = LL^{\text{H}}

where LHL^{\text{H}} is the conjugate transpose when LL is complex, and the transpose when LL is real-valued.

Computes the inverse matrix A−1A^{-1}.

Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if AA is a batch of matrices then the output has the same batch dimensions.

Parameters:
  • L (Tensor) – tensor of shape (*, n, n) where * is zero or more batch dimensions consisting of lower or upper triangular Cholesky decompositions of symmetric or Hermitian positive-definite matrices.

  • upper (bool, optional) – flag that indicates whether LL is lower triangular or upper triangular. Default: False

Keyword Arguments:

out (Tensor, optional) – output tensor. Ignored if None. Default: None.

Example:

>>> A = tensorplay.randn(3, 3)
>>> A = A @ A.T + tensorplay.eye(3) * 1e-3 # Creates a symmetric positive-definite matrix
>>> L = tensorplay.linalg.cholesky(A) # Extract Cholesky decomposition
>>> tensorplay.cholesky_inverse(L)
tensor([[ 1.9314,  1.2251, -0.0889],
        [ 1.2251,  2.4439,  0.2122],
        [-0.0889,  0.2122,  0.1412]])
>>> A.inverse()
tensor([[ 1.9314,  1.2251, -0.0889],
        [ 1.2251,  2.4439,  0.2122],
        [-0.0889,  0.2122,  0.1412]])

>>> A = tensorplay.randn(3, 2, 2, dtype=tensorplay.complex64)
>>> A = A @ A.mH + tensorplay.eye(2) * 1e-3 # Batch of Hermitian positive-definite matrices
>>> L = tensorplay.linalg.cholesky(A)
>>> tensorplay.dist(tensorplay.inverse(A), tensorplay.cholesky_inverse(L))
tensor(5.6358e-7)
#

cholesky_solve

functionFull reference ↗
tensorplay.functional.cholesky_solve(B, L, upper=False, *, out=None) → Tensor[source]

Computes the solution of a system of linear equations with complex Hermitian or real symmetric positive-definite lhs given its Cholesky decomposition.

Let AA be a complex Hermitian or real symmetric positive-definite matrix, and LL its Cholesky decomposition such that:

A=LLHA = LL^{\text{H}}

where LHL^{\text{H}} is the conjugate transpose when LL is complex, and the transpose when LL is real-valued.

Returns the solution XX of the following linear system:

AX=BAX = B

Supports inputs of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if AA or BB is a batch of matrices then the output has the same batch dimensions.

Parameters:
  • B (Tensor) – right-hand side tensor of shape (*, n, k) where ∗* is zero or more batch dimensions

  • L (Tensor) – tensor of shape (*, n, n) where * is zero or more batch dimensions consisting of lower or upper triangular Cholesky decompositions of symmetric or Hermitian positive-definite matrices.

  • upper (bool, optional) – flag that indicates whether LL is lower triangular or upper triangular. Default: False.

Keyword Arguments:

out (Tensor, optional) – output tensor. Ignored if None. Default: None.

Example:

>>> A = tensorplay.randn(3, 3)
>>> A = A @ A.T + tensorplay.eye(3) * 1e-3 # Creates a symmetric positive-definite matrix
>>> L = tensorplay.linalg.cholesky(A) # Extract Cholesky decomposition
>>> B = tensorplay.randn(3, 2)
>>> tensorplay.cholesky_solve(B, L)
tensor([[ -8.1625,  19.6097],
        [ -5.8398,  14.2387],
        [ -4.3771,  10.4173]])
>>> A.inverse() @  B
tensor([[ -8.1626,  19.6097],
        [ -5.8398,  14.2387],
        [ -4.3771,  10.4173]])

>>> A = tensorplay.randn(3, 2, 2, dtype=tensorplay.complex64)
>>> A = A @ A.mH + tensorplay.eye(2) * 1e-3 # Batch of Hermitian positive-definite matrices
>>> L = tensorplay.linalg.cholesky(A)
>>> B = tensorplay.randn(2, 1, dtype=tensorplay.complex64)
>>> X = tensorplay.cholesky_solve(B, L)
>>> tensorplay.dist(X, A.inverse() @ B)
tensor(1.6881e-5)
#

choose_qparams_optimized

functionFull reference ↗
tensorplay.functional.choose_qparams_optimized(input, numel, n_bins, ratio, bit_width)[source]
#

chunk

functionFull reference ↗
tensorplay.functional.chunk(input: Tensor, chunks: int, dim: int = 0) → Tuple[Tensor, ...][source]

Attempts to split a tensor into the specified number of chunks. Each chunk is a view of the input tensor.

Note

This function may return fewer than the specified number of chunks!

See also

tensorplay.tensor_split() a function that always returns exactly the specified number of chunks

If the tensor size along the given dimension dim is divisible by chunks, all returned chunks will be the same size. If the tensor size along the given dimension dim is not divisible by chunks, all returned chunks will be the same size, except the last one. If such division is not possible, this function may return fewer than the specified number of chunks.

Parameters:
  • input (Tensor) – the tensor to split

  • chunks (int) – number of chunks to return

  • dim (int) – dimension along which to split the tensor

Example

>>> tensorplay.arange(11).chunk(6)
(tensor([0, 1]),
 tensor([2, 3]),
 tensor([4, 5]),
 tensor([6, 7]),
 tensor([8, 9]),
 tensor([10]))
>>> tensorplay.arange(12).chunk(6)
(tensor([0, 1]),
 tensor([2, 3]),
 tensor([4, 5]),
 tensor([6, 7]),
 tensor([8, 9]),
 tensor([10, 11]))
>>> tensorplay.arange(13).chunk(6)
(tensor([0, 1, 2]),
 tensor([3, 4, 5]),
 tensor([6, 7, 8]),
 tensor([ 9, 10, 11]),
 tensor([12]))
#

clamp

functionFull reference ↗
tensorplay.functional.clamp(input, min=None, max=None, *, out=None) → Tensor[source]

Clamps all elements in input into the range [ min, max ]. Letting min_value and max_value be min and max, respectively, this returns:

yi=min⁡(max⁡(xi,min_valuei),max_valuei)y_i = \min(\max(x_i, \text{min\_value}_i), \text{max\_value}_i)

If min is None, there is no lower bound. Or, if max is None there is no upper bound.

Note

If min is greater than max tensorplay.clamp(..., min, max) sets all elements in input to the value of max.

Parameters:
  • input (Tensor) – the input tensor.

  • min (Number or Tensor, optional) – lower-bound of the range to be clamped to

  • max (Number or Tensor, optional) – upper-bound of the range to be clamped to

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([-1.7120,  0.1734, -0.0478, -0.0922])
>>> tensorplay.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000,  0.1734, -0.0478, -0.0922])

>>> min = tensorplay.linspace(-1, 1, steps=4)
>>> tensorplay.clamp(a, min=min)
tensor([-1.0000,  0.1734,  0.3333,  1.0000])
#

clone

functionFull reference ↗
tensorplay.functional.clone(input, *, memory_format=tensorplay.preserve_format) → Tensor[source]

Returns a copy of input.

Note

This function is differentiable, so gradients will flow back from the result of this operation to input. To create a tensor without an autograd relationship to input see detach().

In addition, when tensorplay.preserve_format is used: If the input tensor is dense (i.e., non-overlapping strided), its memory format (including strides) is retained. Otherwise (e.g., a non-dense view like a stepped slice), the output is converted to the dense (contiguous) format.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

memory_format (tensorplay.memory_format, optional) – the desired memory format of returned tensor. Default: tensorplay.preserve_format.

#

column_stack

functionFull reference ↗
tensorplay.functional.column_stack(tensors, *, out=None) → Tensor[source]

Creates a new tensor by horizontally stacking the tensors in tensors.

Equivalent to tensorplay.hstack(tensors), except each zero or one dimensional tensor t in tensors is first reshaped into a (t.numel(), 1) column before being stacked horizontally.

Parameters:

tensors (sequence of Tensors) – sequence of tensors to concatenate

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([1, 2, 3])
>>> b = tensorplay.tensor([4, 5, 6])
>>> tensorplay.column_stack((a, b))
tensor([[1, 4],
    [2, 5],
    [3, 6]])
>>> a = tensorplay.arange(5)
>>> b = tensorplay.arange(10).reshape(5, 2)
>>> tensorplay.column_stack((a, b, b))
tensor([[0, 0, 1, 0, 1],
        [1, 2, 3, 2, 3],
        [2, 4, 5, 4, 5],
        [3, 6, 7, 6, 7],
        [4, 8, 9, 8, 9]])
#

combinations

functionFull reference ↗
tensorplay.functional.combinations(input: Tensor, r: int = 2, with_replacement: bool = False) → seq[source]

Compute combinations of length rr of the given tensor. The behavior is similar to python’s itertools.combinations when with_replacement is set to False, and itertools.combinations_with_replacement when with_replacement is set to True.

Parameters:
  • input (Tensor) – 1D vector.

  • r (int, optional) – number of elements to combine

  • with_replacement (bool, optional) – whether to allow duplication in combination

Returns:

A tensor equivalent to converting all the input tensors into lists, do itertools.combinations or itertools.combinations_with_replacement on these lists, and finally convert the resulting list into tensor.

Return type:

Tensor

Example:

>>> a = [1, 2, 3]
>>> list(itertools.combinations(a, r=2))
[(1, 2), (1, 3), (2, 3)]
>>> list(itertools.combinations(a, r=3))
[(1, 2, 3)]
>>> list(itertools.combinations_with_replacement(a, r=2))
[(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]
>>> tensor_a = tensorplay.tensor(a)
>>> tensorplay.combinations(tensor_a)
tensor([[1, 2],
        [1, 3],
        [2, 3]])
>>> tensorplay.combinations(tensor_a, r=3)
tensor([[1, 2, 3]])
>>> tensorplay.combinations(tensor_a, with_replacement=True)
tensor([[1, 1],
        [1, 2],
        [1, 3],
        [2, 2],
        [2, 3],
        [3, 3]])
#

complex

functionFull reference ↗
tensorplay.functional.complex(real, imag, *, out=None) → Tensor[source]

Constructs a complex tensor with its real part equal to real and its imaginary part equal to imag.

Parameters:
  • real (Tensor) – The real part of the complex tensor. Must be half, float or double.

  • imag (Tensor) – The imaginary part of the complex tensor. Must be same dtype as real.

Keyword Arguments:

out (Tensor) – If the inputs are tensorplay.float32, must be tensorplay.complex64. If the inputs are tensorplay.float64, must be tensorplay.complex128.

Example:

>>> real = tensorplay.tensor([1, 2], dtype=tensorplay.float32)
>>> imag = tensorplay.tensor([3, 4], dtype=tensorplay.float32)
>>> z = tensorplay.complex(real, imag)
>>> z
tensor([(1.+3.j), (2.+4.j)])
>>> z.dtype
tensorplay.complex64
#

conj_physical

functionFull reference ↗
tensorplay.functional.conj_physical(input, *, out=None) → Tensor[source]

Computes the element-wise conjugate of the given input tensor. If input has a non-complex dtype, this function just returns input.

Note

This performs the conjugate operation regardless of the fact conjugate bit is set or not.

Warning

In the future, tensorplay.conj_physical() may return a non-writeable view for an input of non-complex dtype. It’s recommended that programs not modify the tensor returned by tensorplay.conj_physical() when input is of non-complex dtype to be compatible with this change.

outi=conj(inputi)\text{out}_{i} = conj(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.conj_physical(tensorplay.tensor([-1 + 1j, -2 + 2j, 3 - 3j]))
tensor([-1 - 1j, -2 - 2j, 3 + 3j])
#

conj

functionFull reference ↗
tensorplay.functional.conj(input) → Tensor[source]

Returns a view of input with a flipped conjugate bit. If input has a non-complex dtype, this function just returns input.

Note

tensorplay.conj() performs a lazy conjugation, but the actual conjugated tensor can be materialized at any time using tensorplay.resolve_conj().

Warning

In the future, tensorplay.conj() may return a non-writeable view for an input of non-complex dtype. It’s recommended that programs not modify the tensor returned by tensorplay.conj_physical() when input is of non-complex dtype to be compatible with this change.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> x = tensorplay.tensor([-1 + 1j, -2 + 2j, 3 - 3j])
>>> x.is_conj()
False
>>> y = tensorplay.conj(x)
>>> y.is_conj()
True
#

conv_transpose1d

functionFull reference ↗
tensorplay.functional.conv_transpose1d(input, weight, bias=None, stride=[], padding=[], output_padding=[], groups=1, dilation=[])[source]
#

conv_transpose2d

functionFull reference ↗
tensorplay.functional.conv_transpose2d(input, weight, bias=None, stride=[1, 1], padding=[0, 0], output_padding=[0, 0], groups=1, dilation=[1, 1])[source]
#

conv_transpose3d

functionFull reference ↗
tensorplay.functional.conv_transpose3d(input, weight, bias=None, stride=[1, 1, 1], padding=[0, 0, 0], output_padding=[0, 0, 0], groups=1, dilation=[1, 1, 1])[source]
#

conv1d

functionFull reference ↗
tensorplay.functional.conv1d(input, weight, bias=None, stride=[1], padding=[0], dilation=[1], groups=1)[source]
#

conv3d

functionFull reference ↗
tensorplay.functional.conv3d(input, weight, bias=None, stride=[1, 1, 1], padding=[0, 0, 0], dilation=[1, 1, 1], groups=1)[source]
#

convolution

functionFull reference ↗
tensorplay.functional.convolution(input, weight, bias, stride, padding, dilation, transposed, output_padding, groups)[source]
#

copysign

functionFull reference ↗
tensorplay.functional.copysign(input, other, *, out=None) → Tensor[source]

Create a new floating-point tensor with the magnitude of input and the sign of other, elementwise.

outi={−∣inputi∣if otheri≤−0.0∣inputi∣if otheri≥0.0\text{out}_{i} = \begin{cases} -|\text{input}_{i}| & \text{if } \text{other}_{i} \leq -0.0 \\ |\text{input}_{i}| & \text{if } \text{other}_{i} \geq 0.0 \\ \end{cases}

Supports broadcasting to a common shape, and integer and float inputs.

Parameters:
  • input (Tensor) – magnitudes.

  • other (Tensor or Number) – contains value(s) whose signbit(s) are applied to the magnitudes in input.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(5)
>>> a
tensor([-1.2557, -0.0026, -0.5387,  0.4740, -0.9244])
>>> tensorplay.copysign(a, 1)
tensor([1.2557, 0.0026, 0.5387, 0.4740, 0.9244])
>>> a = tensorplay.randn(4, 4)
>>> a
tensor([[ 0.7079,  0.2778, -1.0249,  0.5719],
        [-0.0059, -0.2600, -0.4475, -1.3948],
        [ 0.3667, -0.9567, -2.5757, -0.1751],
        [ 0.2046, -0.0742,  0.2998, -0.1054]])
>>> b = tensorplay.randn(4)
tensor([ 0.2373,  0.3120,  0.3190, -1.1128])
>>> tensorplay.copysign(a, b)
tensor([[ 0.7079,  0.2778,  1.0249, -0.5719],
        [ 0.0059,  0.2600,  0.4475, -1.3948],
        [ 0.3667,  0.9567,  2.5757, -0.1751],
        [ 0.2046,  0.0742,  0.2998, -0.1054]])
>>> a = tensorplay.tensor([1.])
>>> b = tensorplay.tensor([-0.])
>>> tensorplay.copysign(a, b)
tensor([-1.])

Note

copysign handles signed zeros. If the other argument has a negative zero (-0), the corresponding output value will be negative.

#

corrcoef

functionFull reference ↗
tensorplay.functional.corrcoef(input) → Tensor[source]

Estimates the Pearson product-moment correlation coefficient matrix of the variables given by the input matrix, where rows are the variables and columns are the observations.

Note

The correlation coefficient matrix R is computed using the covariance matrix C as given by Rij=CijCii∗CjjR_{ij} = \frac{ C_{ij} } { \sqrt{ C_{ii} * C_{jj} } }

Note

Due to floating point rounding, the resulting array may not be Hermitian and its diagonal elements may not be 1. The real and imaginary values are clipped to the interval [-1, 1] in an attempt to improve this situation.

Parameters:

input (Tensor) – A 2D matrix containing multiple variables and observations, or a Scalar or 1D vector representing a single variable.

Returns:

(Tensor) The correlation coefficient matrix of the variables.

See also

tensorplay.cov() covariance matrix.

Example:

>>> x = tensorplay.tensor([[0, 1, 2], [2, 1, 0]])
>>> tensorplay.corrcoef(x)
tensor([[ 1., -1.],
        [-1.,  1.]])
>>> x = tensorplay.randn(2, 4)
>>> x
tensor([[-0.2678, -0.0908, -0.3766,  0.2780],
        [-0.5812,  0.1535,  0.2387,  0.2350]])
>>> tensorplay.corrcoef(x)
tensor([[1.0000, 0.3582],
        [0.3582, 1.0000]])
>>> tensorplay.corrcoef(x[0])
tensor(1.)
#

cos

functionFull reference ↗
tensorplay.functional.cos(input, *, out=None) → Tensor[source]

Returns a new tensor with the cosine of the elements of input.

outi=cos⁡(inputi)\text{out}_{i} = \cos(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([ 1.4309,  1.2706, -0.8562,  0.9796])
>>> tensorplay.cos(a)
tensor([ 0.1395,  0.2957,  0.6553,  0.5574])
#

cosh

functionFull reference ↗
tensorplay.functional.cosh(input, *, out=None) → Tensor[source]

Returns a new tensor with the hyperbolic cosine of the elements of input.

outi=cosh⁡(inputi)\text{out}_{i} = \cosh(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([ 0.1632,  1.1835, -0.6979, -0.7325])
>>> tensorplay.cosh(a)
tensor([ 1.0133,  1.7860,  1.2536,  1.2805])

Note

When input is on the CPU, the implementation of tensorplay.cosh may use the Sleef library, which rounds very large results to infinity or negative infinity. See here for details.

#

count_nonzero

functionFull reference ↗
tensorplay.functional.count_nonzero(input, dim=None) → Tensor[source]

Counts the number of non-zero values in the tensor input along the given dim. If no dim is specified then all non-zeros in the tensor are counted.

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) – Dim or tuple of dims along which to count non-zeros.

Example:

>>> x = tensorplay.zeros(3,3)
>>> x[tensorplay.randn(3,3) > 0.5] = 1
>>> x
tensor([[0., 1., 1.],
        [0., 0., 0.],
        [0., 0., 1.]])
>>> tensorplay.count_nonzero(x)
tensor(3)
>>> tensorplay.count_nonzero(x, dim=0)
tensor([0, 1, 2])
#

cov

functionFull reference ↗
tensorplay.functional.cov(input, *, correction=1, fweights=None, aweights=None) → Tensor[source]

Estimates the covariance matrix of the variables given by the input matrix, where rows are the variables and columns are the observations.

A covariance matrix is a square matrix giving the covariance of each pair of variables. The diagonal contains the variance of each variable (covariance of a variable with itself). By definition, if input represents a single variable (Scalar or 1D) then its variance is returned.

The sample covariance of the variables xx and yy is given by:

cov(x,y)=∑i=1N(xi−xˉ)(yi−yˉ)max⁡(0, N − δN)\text{cov}(x,y) = \frac{\sum^{N}_{i = 1}(x_{i} - \bar{x})(y_{i} - \bar{y})}{\max(0,~N~-~\delta N)}

where xˉ\bar{x} and yˉ\bar{y} are the simple means of the xx and yy respectively, and δN\delta N is the correction.

If fweights and/or aweights are provided, the weighted covariance is calculated, which is given by:

covw(x,y)=∑i=1Nwi(xi−μx∗)(yi−μy∗)max⁡(0, ∑i=1Nwi − ∑i=1Nwiai∑i=1Nwi δN)\text{cov}_w(x,y) = \frac{\sum^{N}_{i = 1}w_i(x_{i} - \mu_x^*)(y_{i} - \mu_y^*)} {\max(0,~\sum^{N}_{i = 1}w_i~-~\frac{\sum^{N}_{i = 1}w_ia_i}{\sum^{N}_{i = 1}w_i}~\delta N)}

where ww denotes fweights or aweights (f and a for brevity) based on whichever is provided, or w=f×aw = f \times a if both are provided, and μx∗=∑i=1Nwixi∑i=1Nwi\mu_x^* = \frac{\sum^{N}_{i = 1}w_ix_{i} }{\sum^{N}_{i = 1}w_i} is the weighted mean of the variable. If not provided, f and/or a can be seen as a 1\mathbb{1} vector of appropriate size.

Parameters:

input (Tensor) – A 2D matrix containing multiple variables and observations, or a Scalar or 1D vector representing a single variable.

Keyword Arguments:
  • correction (int, optional) – difference between the sample size and sample degrees of freedom. Defaults to Bessel’s correction, correction = 1 which returns the unbiased estimate, even if both fweights and aweights are specified. correction = 0 will return the simple average. Defaults to 1.

  • fweights (tensor, optional) – A Scalar or 1D tensor of observation vector frequencies representing the number of times each observation should be repeated. Its numel must equal the number of columns of input. Must have integral dtype. Ignored if None. Defaults to None.

  • aweights (tensor, optional) – A Scalar or 1D array of observation vector weights. These relative weights are typically large for observations considered “important” and smaller for observations considered less “important”. Its numel must equal the number of columns of input. Must have floating point dtype. Ignored if None. Defaults to None.

Returns:

(Tensor) The covariance matrix of the variables.

See also

tensorplay.corrcoef() normalized covariance matrix.

Example:

>>> x = tensorplay.tensor([[0, 2], [1, 1], [2, 0]]).T
>>> x
tensor([[0, 1, 2],
        [2, 1, 0]])
>>> tensorplay.cov(x)
tensor([[ 1., -1.],
        [-1.,  1.]])
>>> tensorplay.cov(x, correction=0)
tensor([[ 0.6667, -0.6667],
        [-0.6667,  0.6667]])
>>> fw = tensorplay.randint(1, 10, (3,))
>>> fw
tensor([1, 6, 9])
>>> aw = tensorplay.rand(3)
>>> aw
tensor([0.4282, 0.0255, 0.4144])
>>> tensorplay.cov(x, fweights=fw, aweights=aw)
tensor([[ 0.4169, -0.4169],
        [-0.4169,  0.4169]])
#

cross

functionFull reference ↗
tensorplay.functional.cross(input, other, dim=None, *, out=None) → Tensor[source]

Returns the cross product of vectors in dimension dim of input and other.

Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of vectors, for which it computes the product along the dimension dim. In this case, the output has the same batch dimensions as the inputs.

Warning

If dim is not given, it defaults to the first dimension found with the size 3. Note that this might be unexpected.

This behavior is deprecated and will be changed to match that of tensorplay.linalg.cross() in a future release.

See also

tensorplay.linalg.cross() which has dim=-1 as default.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the second input tensor

  • dim (int, optional) – the dimension to take the cross-product in.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4, 3)
>>> a
tensor([[-0.3956,  1.1455,  1.6895],
        [-0.5849,  1.3672,  0.3599],
        [-1.1626,  0.7180, -0.0521],
        [-0.1339,  0.9902, -2.0225]])
>>> b = tensorplay.randn(4, 3)
>>> b
tensor([[-0.0257, -1.4725, -1.2251],
        [-1.1479, -0.7005, -1.9757],
        [-1.3904,  0.3726, -1.1836],
        [-0.9688, -0.7153,  0.2159]])
>>> tensorplay.cross(a, b, dim=1)
tensor([[ 1.0844, -0.5281,  0.6120],
        [-2.4490, -1.5687,  1.9792],
        [-0.8304, -1.3037,  0.5650],
        [-1.2329,  1.9883,  1.0551]])
>>> tensorplay.cross(a, b)
tensor([[ 1.0844, -0.5281,  0.6120],
        [-2.4490, -1.5687,  1.9792],
        [-0.8304, -1.3037,  0.5650],
        [-1.2329,  1.9883,  1.0551]])
#

ctc_loss

functionFull reference ↗
tensorplay.functional.ctc_loss(log_probs, targets, input_lengths, target_lengths, blank=0, reduction=1, zero_infinity=False)[source]
#

cudnn_batch_norm

functionFull reference ↗
tensorplay.functional.cudnn_batch_norm(input, weight, bias, running_mean, running_var, training, exponential_average_factor, epsilon, *, out=None)[source]
#

cudnn_convolution_add_relu

functionFull reference ↗
tensorplay.functional.cudnn_convolution_add_relu(input, weight, z, alpha, bias, stride, padding, dilation, groups)[source]
#

cudnn_convolution_relu

functionFull reference ↗
tensorplay.functional.cudnn_convolution_relu(input, weight, bias, stride, padding, dilation, groups)[source]
#

cudnn_convolution_transpose

functionFull reference ↗
tensorplay.functional.cudnn_convolution_transpose(input, weight, padding, output_padding, stride, dilation, groups, benchmark, deterministic, allow_tf32)[source]
#

cudnn_convolution

functionFull reference ↗
tensorplay.functional.cudnn_convolution(input, weight, padding, stride, dilation, groups, benchmark, deterministic, allow_tf32, *, out=None)[source]
#

cumprod

functionFull reference ↗
tensorplay.functional.cumprod(input, dim, *, dtype=None, out=None) → Tensor[source]

Returns the cumulative product of elements of input in the dimension dim.

For example, if input is a vector of size N, the result will also be a vector of size N, with elements.

yi=x1×x2×x3×⋯×xiy_i = x_1 \times x_2\times x_3\times \dots \times x_i
Parameters:
  • input (Tensor) – the input tensor.

  • dim (int) – the dimension to do the operation over

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

  • out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(10)
>>> a
tensor([ 0.6001,  0.2069, -0.1919,  0.9792,  0.6727,  1.0062,  0.4126,
        -0.2129, -0.4206,  0.1968])
>>> tensorplay.cumprod(a, dim=0)
tensor([ 0.6001,  0.1241, -0.0238, -0.0233, -0.0157, -0.0158, -0.0065,
         0.0014, -0.0006, -0.0001])

>>> a[5] = 0.0
>>> tensorplay.cumprod(a, dim=0)
tensor([ 0.6001,  0.1241, -0.0238, -0.0233, -0.0157, -0.0000, -0.0000,
         0.0000, -0.0000, -0.0000])
#

cumsum

functionFull reference ↗
tensorplay.functional.cumsum(input, dim, *, dtype=None, out=None) → Tensor[source]

Returns the cumulative sum of elements of input in the dimension dim.

For example, if input is a vector of size N, the result will also be a vector of size N, with elements.

yi=x1+x2+x3+⋯+xiy_i = x_1 + x_2 + x_3 + \dots + x_i
Parameters:
  • input (Tensor) – the input tensor.

  • dim (int) – the dimension to do the operation over

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

  • out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randint(1, 20, (10,))
>>> a
tensor([13,  7,  3, 10, 13,  3, 15, 10,  9, 10])
>>> tensorplay.cumsum(a, dim=0)
tensor([13, 20, 23, 33, 46, 49, 64, 74, 83, 93])
#

cumulative_trapezoid

functionFull reference ↗
tensorplay.functional.cumulative_trapezoid(y, x=None, *, dx=None, dim=-1) → Tensor[source]

Cumulatively computes the trapezoidal rule along dim. By default the spacing between elements is assumed to be 1, but dx can be used to specify a different constant spacing, and x can be used to specify arbitrary spacing along dim.

For more details, please read tensorplay.trapezoid(). The difference between tensorplay.trapezoid() and this function is that, tensorplay.trapezoid() returns a value for each integration, where as this function returns a cumulative value for every spacing within the integration. This is analogous to how .sum returns a value and .cumsum returns a cumulative sum.

Parameters:
  • y (Tensor) – Values to use when computing the trapezoidal rule.

  • x (Tensor) – If specified, defines spacing between values as specified above.

Keyword Arguments:
  • dx (float) – constant spacing between values. If neither x or dx are specified then this defaults to 1. Effectively multiplies the result by its value.

  • dim (int) – The dimension along which to compute the trapezoidal rule. The last (inner-most) dimension by default.

Examples:

>>> # Cumulatively computes the trapezoidal rule in 1D, spacing is implicitly 1.
>>> y = tensorplay.tensor([1, 5, 10])
>>> tensorplay.cumulative_trapezoid(y)
tensor([3., 10.5])

>>> # Computes the same trapezoidal rule directly up to each element to verify
>>> (1 + 5) / 2
3.0
>>> (1 + 10 + 10) / 2
10.5

>>> # Cumulatively computes the trapezoidal rule in 1D with constant spacing of 2
>>> # NOTE: the result is the same as before, but multiplied by 2
>>> tensorplay.cumulative_trapezoid(y, dx=2)
tensor([6., 21.])

>>> # Cumulatively computes the trapezoidal rule in 1D with arbitrary spacing
>>> x = tensorplay.tensor([1, 3, 6])
>>> tensorplay.cumulative_trapezoid(y, x)
tensor([6., 28.5])

>>> # Computes the same trapezoidal rule directly up to each element to verify
>>> ((3 - 1) * (1 + 5)) / 2
6.0
>>> ((3 - 1) * (1 + 5) + (6 - 3) * (5 + 10)) / 2
28.5

>>> # Cumulatively computes the trapezoidal rule for each row of a 3x3 matrix
>>> y = tensorplay.arange(9).reshape(3, 3)
tensor([[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]])
>>> tensorplay.cumulative_trapezoid(y)
tensor([[ 0.5,  2.],
        [ 3.5,  8.],
        [ 6.5, 14.]])

>>> # Cumulatively computes the trapezoidal rule for each column of the matrix
>>> tensorplay.cumulative_trapezoid(y, dim=0)
tensor([[ 1.5,  2.5,  3.5],
        [ 6.0,  8.0, 10.0]])

>>> # Cumulatively computes the trapezoidal rule for each row of a 3x3 ones matrix
>>> #   with the same arbitrary spacing
>>> y = tensorplay.ones(3, 3)
>>> x = tensorplay.tensor([1, 3, 6])
>>> tensorplay.cumulative_trapezoid(y, x)
tensor([[2., 5.],
        [2., 5.],
        [2., 5.]])

>>> # Cumulatively computes the trapezoidal rule for each row of a 3x3 ones matrix
>>> #   with different arbitrary spacing per row
>>> y = tensorplay.ones(3, 3)
>>> x = tensorplay.tensor([[1, 2, 3], [1, 3, 5], [1, 4, 7]])
>>> tensorplay.cumulative_trapezoid(y, x)
tensor([[1., 2.],
        [2., 4.],
        [3., 6.]])
#

deg2rad

functionFull reference ↗
tensorplay.functional.deg2rad(input, *, out=None) → Tensor[source]

Returns a new tensor with each of the elements of input converted from angles in degrees to radians.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([[180.0, -180.0], [360.0, -360.0], [90.0, -90.0]])
>>> tensorplay.deg2rad(a)
tensor([[ 3.1416, -3.1416],
        [ 6.2832, -6.2832],
        [ 1.5708, -1.5708]])
#

dequantize

functionFull reference ↗
tensorplay.functional.dequantize(tensor) → Tensor[source]

Returns an fp32 Tensor by dequantizing a quantized Tensor

Parameters:

tensor (Tensor) – A quantized Tensor

tensorplay.functional.dequantize(tensors) → sequence of Tensors[source]

Given a list of quantized Tensors, dequantize them and return a list of fp32 Tensors

Parameters:

tensors (sequence of Tensors) – A list of quantized Tensors

#

detach_copy

functionFull reference ↗
tensorplay.functional.detach_copy(input)[source]

Performs the same operation as tensorplay.detach(), but all output tensors are freshly created instead of aliasing the input.

#

diag_embed

functionFull reference ↗
tensorplay.functional.diag_embed(input, offset=0, dim1=-2, dim2=-1) → Tensor[source]

Creates a tensor whose diagonals of certain 2D planes (specified by dim1 and dim2) are filled by input. To facilitate creating batched diagonal matrices, the 2D planes formed by the last two dimensions of the returned tensor are chosen by default.

The argument offset controls which diagonal to consider:

  • If offset = 0, it is the main diagonal.

  • If offset > 0, it is above the main diagonal.

  • If offset < 0, it is below the main diagonal.

The size of the new matrix will be calculated to make the specified diagonal of the size of the last input dimension. Note that for offset other than 00, the order of dim1 and dim2 matters. Exchanging them is equivalent to changing the sign of offset.

Applying tensorplay.diagonal() to the output of this function with the same arguments yields a matrix identical to input. However, tensorplay.diagonal() has different default dimensions, so those need to be explicitly specified.

Parameters:
  • input (Tensor) – the input tensor. Must be at least 1-dimensional.

  • offset (int, optional) – which diagonal to consider. Default: 0 (main diagonal).

  • dim1 (int, optional) – first dimension with respect to which to take diagonal. Default: -2.

  • dim2 (int, optional) – second dimension with respect to which to take diagonal. Default: -1.

Example:

>>> a = tensorplay.randn(2, 3)
>>> tensorplay.diag_embed(a)
tensor([[[ 1.5410,  0.0000,  0.0000],
         [ 0.0000, -0.2934,  0.0000],
         [ 0.0000,  0.0000, -2.1788]],

        [[ 0.5684,  0.0000,  0.0000],
         [ 0.0000, -1.0845,  0.0000],
         [ 0.0000,  0.0000, -1.3986]]])

>>> tensorplay.diag_embed(a, offset=1, dim1=0, dim2=2)
tensor([[[ 0.0000,  1.5410,  0.0000,  0.0000],
         [ 0.0000,  0.5684,  0.0000,  0.0000]],

        [[ 0.0000,  0.0000, -0.2934,  0.0000],
         [ 0.0000,  0.0000, -1.0845,  0.0000]],

        [[ 0.0000,  0.0000,  0.0000, -2.1788],
         [ 0.0000,  0.0000,  0.0000, -1.3986]],

        [[ 0.0000,  0.0000,  0.0000,  0.0000],
         [ 0.0000,  0.0000,  0.0000,  0.0000]]])
#

diag

functionFull reference ↗
tensorplay.functional.diag(input, diagonal=0, *, out=None) → Tensor[source]
  • If input is a vector (1-D tensor), then returns a 2-D square tensor with the elements of input as the diagonal.

  • If input is a matrix (2-D tensor), then returns a 1-D tensor with the diagonal elements of input.

The argument diagonal controls which diagonal to consider:

  • If diagonal = 0, it is the main diagonal.

  • If diagonal > 0, it is above the main diagonal.

  • If diagonal < 0, it is below the main diagonal.

Parameters:
  • input (Tensor) – the input tensor.

  • diagonal (int, optional) – the diagonal to consider

Keyword Arguments:

out (Tensor, optional) – the output tensor.

See also

tensorplay.diagonal() always returns the diagonal of its input.

tensorplay.diagflat() always constructs a tensor with diagonal elements specified by the input.

Examples:

Get the square matrix where the input vector is the diagonal:

>>> a = tensorplay.randn(3)
>>> a
tensor([ 0.5950,-0.0872, 2.3298])
>>> tensorplay.diag(a)
tensor([[ 0.5950, 0.0000, 0.0000],
        [ 0.0000,-0.0872, 0.0000],
        [ 0.0000, 0.0000, 2.3298]])
>>> tensorplay.diag(a, 1)
tensor([[ 0.0000, 0.5950, 0.0000, 0.0000],
        [ 0.0000, 0.0000,-0.0872, 0.0000],
        [ 0.0000, 0.0000, 0.0000, 2.3298],
        [ 0.0000, 0.0000, 0.0000, 0.0000]])

Get the k-th diagonal of a given matrix:

>>> a = tensorplay.randn(3, 3)
>>> a
tensor([[-0.4264, 0.0255,-0.1064],
        [ 0.8795,-0.2429, 0.1374],
        [ 0.1029,-0.6482,-1.6300]])
>>> tensorplay.diag(a, 0)
tensor([-0.4264,-0.2429,-1.6300])
>>> tensorplay.diag(a, 1)
tensor([ 0.0255, 0.1374])
#

diagflat

functionFull reference ↗
tensorplay.functional.diagflat(input, offset=0) → Tensor[source]
  • If input is a vector (1-D tensor), then returns a 2-D square tensor with the elements of input as the diagonal.

  • If input is a tensor with more than one dimension, then returns a 2-D tensor with diagonal elements equal to a flattened input.

The argument offset controls which diagonal to consider:

  • If offset = 0, it is the main diagonal.

  • If offset > 0, it is above the main diagonal.

  • If offset < 0, it is below the main diagonal.

Parameters:
  • input (Tensor) – the input tensor.

  • offset (int, optional) – the diagonal to consider. Default: 0 (main diagonal).

Examples:

>>> a = tensorplay.randn(3)
>>> a
tensor([-0.2956, -0.9068,  0.1695])
>>> tensorplay.diagflat(a)
tensor([[-0.2956,  0.0000,  0.0000],
        [ 0.0000, -0.9068,  0.0000],
        [ 0.0000,  0.0000,  0.1695]])
>>> tensorplay.diagflat(a, 1)
tensor([[ 0.0000, -0.2956,  0.0000,  0.0000],
        [ 0.0000,  0.0000, -0.9068,  0.0000],
        [ 0.0000,  0.0000,  0.0000,  0.1695],
        [ 0.0000,  0.0000,  0.0000,  0.0000]])

>>> a = tensorplay.randn(2, 2)
>>> a
tensor([[ 0.2094, -0.3018],
        [-0.1516,  1.9342]])
>>> tensorplay.diagflat(a)
tensor([[ 0.2094,  0.0000,  0.0000,  0.0000],
        [ 0.0000, -0.3018,  0.0000,  0.0000],
        [ 0.0000,  0.0000, -0.1516,  0.0000],
        [ 0.0000,  0.0000,  0.0000,  1.9342]])
#

diagonal_copy

functionFull reference ↗
tensorplay.functional.diagonal_copy(input, offset=0, dim1=0, dim2=1)[source]

Performs the same operation as tensorplay.diagonal(), but all output tensors are freshly created instead of aliasing the input.

#

diagonal_scatter

functionFull reference ↗
tensorplay.functional.diagonal_scatter(input, src, offset=0, dim1=0, dim2=1) → Tensor[source]

Embeds the values of the src tensor into input along the diagonal elements of input, with respect to dim1 and dim2.

This function returns a tensor with fresh storage; it does not return a view.

The argument offset controls which diagonal to consider:

  • If offset = 0, it is the main diagonal.

  • If offset > 0, it is above the main diagonal.

  • If offset < 0, it is below the main diagonal.

Parameters:
  • input (Tensor) – the input tensor. Must be at least 2-dimensional.

  • src (Tensor) – the tensor to embed into input.

  • offset (int, optional) – which diagonal to consider. Default: 0 (main diagonal).

  • dim1 (int, optional) – first dimension with respect to which to take diagonal. Default: 0.

  • dim2 (int, optional) – second dimension with respect to which to take diagonal. Default: 1.

Note

src must be of the proper size in order to be embedded into input. Specifically, it should have the same shape as tensorplay.diagonal(input, offset, dim1, dim2)

Examples:

>>> a = tensorplay.zeros(3, 3)
>>> a
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])

>>> tensorplay.diagonal_scatter(a, tensorplay.ones(3), 0)
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

>>> tensorplay.diagonal_scatter(a, tensorplay.ones(2), 1)
tensor([[0., 1., 0.],
        [0., 0., 1.],
        [0., 0., 0.]])
#

diagonal

functionFull reference ↗
tensorplay.functional.diagonal(input, offset=0, dim1=0, dim2=1) → Tensor[source]

Returns a partial view of input with the its diagonal elements with respect to dim1 and dim2 appended as a dimension at the end of the shape.

The argument offset controls which diagonal to consider:

  • If offset = 0, it is the main diagonal.

  • If offset > 0, it is above the main diagonal.

  • If offset < 0, it is below the main diagonal.

Applying tensorplay.diag_embed() to the output of this function with the same arguments yields a diagonal matrix with the diagonal entries of the input. However, tensorplay.diag_embed() has different default dimensions, so those need to be explicitly specified.

Parameters:
  • input (Tensor) – the input tensor. Must be at least 2-dimensional.

  • offset (int, optional) – which diagonal to consider. Default: 0 (main diagonal).

  • dim1 (int, optional) – first dimension with respect to which to take diagonal. Default: 0.

  • dim2 (int, optional) – second dimension with respect to which to take diagonal. Default: 1.

Note

To take a batch diagonal, pass in dim1=-2, dim2=-1.

Examples:

>>> a = tensorplay.randn(3, 3)
>>> a
tensor([[-1.0854,  1.1431, -0.1752],
        [ 0.8536, -0.0905,  0.0360],
        [ 0.6927, -0.3735, -0.4945]])


>>> tensorplay.diagonal(a)
tensor([-1.0854, -0.0905, -0.4945])


>>> tensorplay.diagonal(a, 1)
tensor([ 1.1431,  0.0360])

>>> b = tensorplay.randn(2, 5)
>>> b
tensor([[-1.7948, -1.2731, -0.3181,  2.0200, -1.6745],
        [ 1.8262, -1.5049,  0.4114,  1.0704, -1.2607]])

>>> tensorplay.diagonal(b, 1, 1, 0)
tensor([1.8262])

>>> x = tensorplay.randn(2, 5, 4, 2)
>>> tensorplay.diagonal(x, offset=-1, dim1=1, dim2=2)
tensor([[[-1.2631,  0.3755, -1.5977, -1.8172],
         [-1.1065,  1.0401, -0.2235, -0.7938]],

        [[-1.7325, -0.3081,  0.6166,  0.2335],
         [ 1.0500,  0.7336, -0.3836, -1.1015]]])
#

diff

functionFull reference ↗
tensorplay.functional.diff(input, n=1, dim=-1, prepend=None, append=None) → Tensor[source]

Computes the n-th forward difference along the given dimension.

The first-order differences are given by out[i] = input[i + 1] - input[i]. Higher-order differences are calculated by using tensorplay.diff() recursively.

Parameters:
  • input (Tensor) – the tensor to compute the differences on

  • n (int, optional) – the number of times to recursively compute the difference

  • dim (int, optional) – the dimension to compute the difference along. Default is the last dimension.

  • prepend (Tensor, optional) – values to prepend or append to input along dim before computing the difference. Their dimensions must be equivalent to that of input, and their shapes must match input’s shape except on dim.

  • append (Tensor, optional) – values to prepend or append to input along dim before computing the difference. Their dimensions must be equivalent to that of input, and their shapes must match input’s shape except on dim.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([1, 3, 2])
>>> tensorplay.diff(a)
tensor([ 2, -1])
>>> b = tensorplay.tensor([4, 5])
>>> tensorplay.diff(a, append=b)
tensor([ 2, -1,  2,  1])
>>> c = tensorplay.tensor([[1, 2, 3], [3, 4, 5]])
>>> tensorplay.diff(c, dim=0)
tensor([[2, 2, 2]])
>>> tensorplay.diff(c, dim=1)
tensor([[1, 1],
        [1, 1]])
#

dist

functionFull reference ↗
tensorplay.functional.dist(input, other, p=2) → Tensor[source]

Returns the p-norm of (input - other)

The shapes of input and other must be broadcastable.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the Right-hand-side input tensor

  • p (float, optional) – the norm to be computed

Example:

>>> x = tensorplay.randn(4)
>>> x
tensor([-1.5393, -0.8675,  0.5916,  1.6321])
>>> y = tensorplay.randn(4)
>>> y
tensor([ 0.0967, -1.0511,  0.6295,  0.8360])
>>> tensorplay.dist(x, y, 3.5)
tensor(1.6727)
>>> tensorplay.dist(x, y, 3)
tensor(1.6973)
>>> tensorplay.dist(x, y, 0)
tensor(4.)
>>> tensorplay.dist(x, y, 1)
tensor(2.6537)
#

div

functionFull reference ↗
tensorplay.functional.div(input, other, *, rounding_mode=None, out=None) → Tensor[source]

Divides each element of the input input by the corresponding element of other.

outi=inputiotheri\text{out}_i = \frac{\text{input}_i}{\text{other}_i}

Note

By default, this performs a “true” division like Python 3. See the rounding_mode argument for floor division.

Supports broadcasting to a common shape, type promotion, and integer, float, and complex inputs. Always promotes integer types to the default scalar type.

Parameters:
  • input (Tensor) – the dividend

  • other (Tensor or Number) – the divisor

Keyword Arguments:
  • rounding_mode (str, optional) –

    Type of rounding applied to the result:

    • None - default behavior. Performs no rounding and, if both input and other are integer types, promotes the inputs to the default scalar type. Equivalent to true division in Python (the / operator) and NumPy’s np.true_divide.

    • "trunc" - rounds the results of the division towards zero. Equivalent to C-style integer division.

    • "floor" - rounds the results of the division down. Equivalent to floor division in Python (the // operator) and NumPy’s np.floor_divide.

  • out (Tensor, optional) – the output tensor.

Examples:

>>> x = tensorplay.tensor([ 0.3810,  1.2774, -0.2972, -0.3719,  0.4637])
>>> tensorplay.div(x, 0.5)
tensor([ 0.7620,  2.5548, -0.5944, -0.7438,  0.9274])

>>> a = tensorplay.tensor([[-0.3711, -1.9353, -0.4605, -0.2917],
...                   [ 0.1815, -1.0111,  0.9805, -1.5923],
...                   [ 0.1062,  1.4581,  0.7759, -1.2344],
...                   [-0.1830, -0.0313,  1.1908, -1.4757]])
>>> b = tensorplay.tensor([ 0.8032,  0.2930, -0.8113, -0.2308])
>>> tensorplay.div(a, b)
tensor([[-0.4620, -6.6051,  0.5676,  1.2639],
        [ 0.2260, -3.4509, -1.2086,  6.8990],
        [ 0.1322,  4.9764, -0.9564,  5.3484],
        [-0.2278, -0.1068, -1.4678,  6.3938]])

>>> tensorplay.div(a, b, rounding_mode='trunc')
tensor([[-0., -6.,  0.,  1.],
        [ 0., -3., -1.,  6.],
        [ 0.,  4., -0.,  5.],
        [-0., -0., -1.,  6.]])

>>> tensorplay.div(a, b, rounding_mode='floor')
tensor([[-1., -7.,  0.,  1.],
        [ 0., -4., -2.,  6.],
        [ 0.,  4., -1.,  5.],
        [-1., -1., -2.,  6.]])
#

dot

functionFull reference ↗
tensorplay.functional.dot(input, tensor, *, out=None) → Tensor[source]

Computes the dot product of two 1D tensors.

Note

Unlike NumPy’s dot, tensorplay.dot intentionally only supports computing the dot product of two 1D tensors with the same number of elements.

Parameters:
  • input (Tensor) – first tensor in the dot product, must be 1D.

  • tensor (Tensor) – second tensor in the dot product, must be 1D.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.dot(tensorplay.tensor([2, 3]), tensorplay.tensor([2, 1]))
tensor(7)

>>> t1, t2 = tensorplay.tensor([0, 1]), tensorplay.tensor([2, 3])
>>> tensorplay.dot(t1, t2)
tensor(3)
#

dsplit

functionFull reference ↗
tensorplay.functional.dsplit(input, indices_or_sections) → List of Tensors[source]

Splits input, a tensor with three or more dimensions, into multiple tensors depthwise according to indices_or_sections. Each split is a view of input.

This is equivalent to calling tensorplay.tensor_split(input, indices_or_sections, dim=2) (the split dimension is 2), except that if indices_or_sections is an integer it must evenly divide the split dimension or a runtime error will be thrown.

This function is based on NumPy’s numpy.dsplit().

Parameters:
  • input (Tensor) – tensor to split.

  • indices_or_sections (int or list or tuple of ints) – See argument in tensorplay.tensor_split().

Example:

>>> t = tensorplay.arange(16.0).reshape(2, 2, 4)
>>> t
tensor([[[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.]],
        [[ 8.,  9., 10., 11.],
         [12., 13., 14., 15.]]])
>>> tensorplay.dsplit(t, 2)
(tensor([[[ 0.,  1.],
        [ 4.,  5.]],
       [[ 8.,  9.],
        [12., 13.]]]),
 tensor([[[ 2.,  3.],
          [ 6.,  7.]],
         [[10., 11.],
          [14., 15.]]]))

>>> tensorplay.dsplit(t, [3, 6])
(tensor([[[ 0.,  1.,  2.],
          [ 4.,  5.,  6.]],
         [[ 8.,  9., 10.],
          [12., 13., 14.]]]),
 tensor([[[ 3.],
          [ 7.]],
         [[11.],
          [15.]]]),
 tensor([], size=(2, 2, 0)))
#

dstack

functionFull reference ↗
tensorplay.functional.dstack(tensors, *, out=None) → Tensor[source]

Stack tensors in sequence depthwise (along third axis).

This is equivalent to concatenation along the third axis after 1-D and 2-D tensors have been reshaped by tensorplay.atleast_3d().

Parameters:

tensors (sequence of Tensors) – sequence of tensors to concatenate

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([1, 2, 3])
>>> b = tensorplay.tensor([4, 5, 6])
>>> tensorplay.dstack((a,b))
tensor([[[1, 4],
         [2, 5],
         [3, 6]]])
>>> a = tensorplay.tensor([[1],[2],[3]])
>>> b = tensorplay.tensor([[4],[5],[6]])
>>> tensorplay.dstack((a,b))
tensor([[[1, 4]],
        [[2, 5]],
        [[3, 6]]])
#

embedding

functionFull reference ↗
tensorplay.functional.embedding(weight, indices, padding_idx=-1, scale_grad_by_freq=False, sparse=False)[source]
#

empty_like

functionFull reference ↗
tensorplay.functional.empty_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=tensorplay.preserve_format) → Tensor

Returns an uninitialized tensor with the same size as input. tensorplay.empty_like(input) is equivalent to tensorplay.empty(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).

Note

The returned storage is uninitialized, and tensorplay.use_deterministic_algorithms() does not change that. If the contents matter, allocate with tensorplay.zeros() or fill the tensor before reading it, otherwise the output becomes a source of nondeterminism. Floating point and complex tensors are filled with NaN, and integer tensors are filled with the maximum value.

When tensorplay.preserve_format is used: If the input tensor is dense (i.e., non-overlapping strided), its memory format (including strides) is retained. Otherwise (e.g., a non-dense view like a stepped slice), the output is converted to the dense format.

Parameters:

input (Tensor) – the size of input will determine size of the output tensor.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.

  • layout (tensorplay.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • memory_format (tensorplay.memory_format, optional) – the desired memory format of returned Tensor. Default: tensorplay.preserve_format.

Example:

>>> a=tensorplay.empty((2,3), dtype=tensorplay.int32, device = 'cuda')
>>> tensorplay.empty_like(a)
tensor([[0, 0, 0],
        [0, 0, 0]], device='cuda:0', dtype=tensorplay.int32)
#

empty_permuted

functionFull reference ↗
tensorplay.functional.empty_permuted(size, physical_layout, *, dtype=None, layout=None, device=None, requires_grad=False, pin_memory=False) → Tensor[source]

Creates an uninitialized, non-overlapping and dense tensor with the specified size, with physical_layout specifying how the dimensions are physically laid out in memory (each logical dimension is listed from outermost to innermost). physical_layout is a generalization of NCHW/NHWC notation: if each dimension is assigned a number according to what order they occur in size (N=0, C=1, H=2, W=3), then NCHW is (0, 1, 2, 3) while NHWC is (0, 2, 3, 1). Equivalently, the strides of the output tensor t are such that t.stride(physical_layout[i]) == contiguous_strides[i] (notably, this function is not equivalent to tensorplay.empty(size).permute(physical_layout)).

Unlike tensorplay.empty_strided(), this is guaranteed to produce a dense tensor with no overlaps. If possible, prefer using this function over tensorplay.empty_strided() or manual use of tensorplay.as_strided().

Note

The returned storage is uninitialized, and tensorplay.use_deterministic_algorithms() does not change that. If the contents matter, allocate with tensorplay.zeros() or fill the tensor before reading it, otherwise the output becomes a source of nondeterminism. Floating point and complex tensors are filled with NaN, and integer tensors are filled with the maximum value.

Parameters:
  • size (tuple of int) – the shape of the output tensor

  • physical_layout (tuple of int) – the ordering of dimensions physically in memory

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()).

  • layout (tensorplay.layout, optional) – the desired layout of returned Tensor. Default: tensorplay.strided.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

Examples

>>> tensorplay.empty((2, 3, 5, 7)).stride()
(105, 35, 7, 1)
>>> tensorplay.empty_permuted((2, 3, 5, 7), (0, 1, 2, 3)).stride()
(105, 35, 7, 1)
>>> tensorplay.empty((2, 3, 5, 7), memory_format=tensorplay.channels_last).stride()
(105, 1, 21, 3)
>>> tensorplay.empty_permuted((2, 3, 5, 7), (0, 2, 3, 1)).stride()
(105, 1, 21, 3)
>>> tensorplay.empty_permuted((2, 3, 5, 7), (0, 2, 3, 1)).dim_order()
(0, 2, 3, 1)
#

empty_quantized

functionFull reference ↗
tensorplay.functional.empty_quantized(size, qtensor, dtype=None, layout=None, device=None, pin_memory=None, memory_format=None)[source]
#

empty_strided

functionFull reference ↗
tensorplay.functional.empty_strided(size, stride, *, dtype=None, layout=None, device=None, requires_grad=False, pin_memory=False) → Tensor[source]

Creates a tensor with the specified size and stride and filled with undefined data.

Warning

If the constructed tensor is “overlapped” (with multiple indices referring to the same element in memory) its behavior is undefined.

Note

The returned storage is uninitialized, and tensorplay.use_deterministic_algorithms() does not change that. If the contents matter, allocate with tensorplay.zeros() or fill the tensor before reading it, otherwise the output becomes a source of nondeterminism. Floating point and complex tensors are filled with NaN, and integer tensors are filled with the maximum value.

Parameters:
  • size (tuple of int) – the shape of the output tensor

  • stride (tuple of int) – the strides of the output tensor

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()).

  • layout (tensorplay.layout, optional) – the desired layout of returned Tensor. Default: tensorplay.strided.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

Example:

>>> a = tensorplay.empty_strided((2, 3), (1, 2))
>>> a
tensor([[8.9683e-44, 4.4842e-44, 5.1239e+07],
        [0.0000e+00, 0.0000e+00, 3.0705e-41]])
>>> a.stride()
(1, 2)
>>> a.size()
tensorplay.Size([2, 3])
#

eq

functionFull reference ↗
tensorplay.functional.eq(input, other, *, out=None) → Tensor[source]

Computes element-wise equality

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:
  • input (Tensor) – the tensor to compare

  • other (Tensor or float) – the tensor or value to compare

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Returns:

A boolean tensor that is True where input is equal to other and False elsewhere

Example:

>>> tensorplay.eq(tensorplay.tensor([[1, 2], [3, 4]]), tensorplay.tensor([[1, 1], [4, 4]]))
tensor([[ True, False],
        [False, True]])
#

equal

functionFull reference ↗
tensorplay.functional.equal(input, other) → bool[source]

True if two tensors have the same size and elements, False otherwise.

Note

Tensors containing NaNs are never equal to each other. Additionally, this function does not differentiate between the data types of the tensors during comparison. For more thorough tensor checks, use tensorplay.testing.assert_close().

Example:

>>> tensorplay.equal(tensorplay.tensor([1, 2]), tensorplay.tensor([1, 2]))
True
>>> tensorplay.equal(tensorplay.tensor([3, tensorplay.nan]), tensorplay.tensor([3, tensorplay.nan]))
False
>>> tensorplay.equal(tensorplay.tensor([1, 2, 3], dtype=tensorplay.int32), tensorplay.tensor([1, 2, 3], dtype=tensorplay.float32))
True
#

exp

functionFull reference ↗
tensorplay.functional.exp(input, *, out=None) → Tensor[source]

Returns a new tensor with the exponential of the elements of the input tensor input.

yi=exiy_{i} = e^{x_{i}}
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.exp(tensorplay.tensor([0, math.log(2.)]))
tensor([ 1.,  2.])
#

expand_copy

functionFull reference ↗
tensorplay.functional.expand_copy(input, size, implicit=False)[source]

Performs the same operation as tensorplay.Tensor.expand(), but all output tensors are freshly created instead of aliasing the input.

#

eye

functionFull reference ↗
tensorplay.functional.eye(n, m=None, *, out=None, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False) → Tensor

Returns a 2-D tensor with ones on the diagonal and zeros elsewhere.

Parameters:
  • n (int) – the number of rows

  • m (int, optional) – the number of columns with default being n

Keyword Arguments:
  • out (Tensor, optional) – the output tensor.

  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()).

  • layout (tensorplay.layout, optional) – the desired layout of returned Tensor. Default: tensorplay.strided.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Returns:

A 2-D tensor with ones on the diagonal and zeros elsewhere

Return type:

Tensor

Example:

>>> tensorplay.eye(3)
tensor([[ 1.,  0.,  0.],
        [ 0.,  1.,  0.],
        [ 0.,  0.,  1.]])
#

fake_quantize_per_channel_affine

functionFull reference ↗
tensorplay.functional.fake_quantize_per_channel_affine(input, scale, zero_point, axis, quant_min, quant_max) → Tensor[source]

Returns a new tensor with the data in input fake quantized per channel using scale, zero_point, quant_min and quant_max, across the channel specified by axis.

output=(min(quant_max,max(quant_min,std::nearby_int(input/scale)+zero_point))−zero_point)×scale\text{output} = ( min( \text{quant\_max}, max( \text{quant\_min}, \text{std::nearby\_int}(\text{input} / \text{scale}) + \text{zero\_point} ) ) - \text{zero\_point} ) \times \text{scale}
Parameters:
  • input (Tensor) – the input value(s), in tensorplay.float32

  • scale (Tensor) – quantization scale, per channel in tensorplay.float32

  • zero_point (Tensor) – quantization zero_point, per channel in tensorplay.int32 or tensorplay.half or tensorplay.float32

  • axis (int32) – channel axis

  • quant_min (int64) – lower bound of the quantized domain

  • quant_max (int64) – upper bound of the quantized domain

Returns:

A newly fake_quantized per channel tensorplay.float32 tensor

Return type:

Tensor

Example:

>>> x = tensorplay.randn(2, 2, 2)
>>> x
tensor([[[-0.2525, -0.0466],
         [ 0.3491, -0.2168]],

        [[-0.5906,  1.6258],
         [ 0.6444, -0.0542]]])
>>> scales = (tensorplay.randn(2) + 1) * 0.05
>>> scales
tensor([0.0475, 0.0486])
>>> zero_points = tensorplay.zeros(2).to(tensorplay.int32)
>>> zero_points
tensor([0, 0])
>>> tensorplay.fake_quantize_per_channel_affine(x, scales, zero_points, 1, 0, 255)
tensor([[[0.0000, 0.0000],
         [0.3405, 0.0000]],

        [[0.0000, 1.6134],
        [0.6323, 0.0000]]])
#

fake_quantize_per_tensor_affine

functionFull reference ↗
tensorplay.functional.fake_quantize_per_tensor_affine(input, scale, zero_point, quant_min, quant_max) → Tensor[source]

Returns a new tensor with the data in input fake quantized using scale, zero_point, quant_min and quant_max.

output=(min(quant_max,max(quant_min,std::nearby_int(input/scale)+zero_point))−zero_point)×scale\text{output} = ( min( \text{quant\_max}, max( \text{quant\_min}, \text{std::nearby\_int}(\text{input} / \text{scale}) + \text{zero\_point} ) ) - \text{zero\_point} ) \times \text{scale}
Parameters:
  • input (Tensor) – the input value(s), tensorplay.float32 tensor

  • scale (double scalar or float32 Tensor) – quantization scale

  • zero_point (int64 scalar or int32 Tensor) – quantization zero_point

  • quant_min (int64) – lower bound of the quantized domain

  • quant_max (int64) – upper bound of the quantized domain

Returns:

A newly fake_quantized tensorplay.float32 tensor

Return type:

Tensor

Example:

>>> x = tensorplay.randn(4)
>>> x
tensor([ 0.0552,  0.9730,  0.3973, -1.0780])
>>> tensorplay.fake_quantize_per_tensor_affine(x, 0.1, 0, 0, 255)
tensor([0.1000, 1.0000, 0.4000, 0.0000])
>>> tensorplay.fake_quantize_per_tensor_affine(x, tensorplay.tensor(0.1), tensorplay.tensor(0), 0, 255)
tensor([0.1000, 1.0000, 0.4000, 0.0000])
#

fbgemm_linear_fp16_weight_fp32_activation

functionFull reference ↗
tensorplay.functional.fbgemm_linear_fp16_weight_fp32_activation(input, packed_weight, bias, *, out=None)[source]
#

fbgemm_linear_fp16_weight

functionFull reference ↗
tensorplay.functional.fbgemm_linear_fp16_weight(input, packed_weight, bias, *, out=None)[source]
#

fbgemm_linear_int8_weight_fp32_activation

functionFull reference ↗
tensorplay.functional.fbgemm_linear_int8_weight_fp32_activation(input, weight, packed, col_offsets, weight_scale, weight_zero_point, bias)[source]
#

fbgemm_linear_int8_weight

functionFull reference ↗
tensorplay.functional.fbgemm_linear_int8_weight(input, weight, packed, col_offsets, weight_scale, weight_zero_point, bias)[source]
#

fbgemm_pack_quantized_matrix

functionFull reference ↗
tensorplay.functional.fbgemm_pack_quantized_matrix(input, K=None, N=None)[source]
#

flatten

functionFull reference ↗
tensorplay.functional.flatten(input, start_dim=0, end_dim=-1) → Tensor[source]

Flattens input by reshaping it into a one-dimensional tensor. If start_dim or end_dim are passed, only dimensions starting with start_dim and ending with end_dim are flattened. The order of elements in input is unchanged.

Unlike NumPy’s flatten, which always copies input’s data, this function may return the original object, a view, or copy. If no dimensions are flattened, then the original object input is returned. Otherwise, if input can be viewed as the flattened shape, then that view is returned. Finally, only if the input cannot be viewed as the flattened shape is input’s data copied. See tensorplay.Tensor.view() for details on when a view will be returned.

Note

Flattening a zero-dimensional tensor will return a one-dimensional view.

Parameters:
  • input (Tensor) – the input tensor.

  • start_dim (int) – the first dim to flatten

  • end_dim (int) – the last dim to flatten

Example:

>>> t = tensorplay.tensor([[[1, 2],
...                    [3, 4]],
...                   [[5, 6],
...                    [7, 8]]])
>>> tensorplay.flatten(t)
tensor([1, 2, 3, 4, 5, 6, 7, 8])
>>> tensorplay.flatten(t, start_dim=1)
tensor([[1, 2, 3, 4],
        [5, 6, 7, 8]])
#

flip

functionFull reference ↗
tensorplay.functional.flip(input, dims) → Tensor[source]

Reverse the order of an n-D tensor along given axis in dims.

Note

tensorplay.flip makes a copy of input’s data. This is different from NumPy’s np.flip, which returns a view in constant time. Since copying a tensor’s data is more work than viewing that data, tensorplay.flip is expected to be slower than np.flip.

Parameters:
  • input (Tensor) – the input tensor.

  • dims (a list or tuple) – axis to flip on

Example:

>>> x = tensorplay.arange(8).view(2, 2, 2)
>>> x
tensor([[[ 0,  1],
         [ 2,  3]],

        [[ 4,  5],
         [ 6,  7]]])
>>> tensorplay.flip(x, [0, 1])
tensor([[[ 6,  7],
         [ 4,  5]],

        [[ 2,  3],
         [ 0,  1]]])
#

fliplr

functionFull reference ↗
tensorplay.functional.fliplr(input) → Tensor[source]

Flip tensor in the left/right direction, returning a new tensor.

Flip the entries in each row in the left/right direction. Columns are preserved, but appear in a different order than before.

Note

Requires the tensor to be at least 2-D.

Note

tensorplay.fliplr makes a copy of input’s data. This is different from NumPy’s np.fliplr, which returns a view in constant time. Since copying a tensor’s data is more work than viewing that data, tensorplay.fliplr is expected to be slower than np.fliplr.

Parameters:

input (Tensor) – Must be at least 2-dimensional.

Example:

>>> x = tensorplay.arange(4).view(2, 2)
>>> x
tensor([[0, 1],
        [2, 3]])
>>> tensorplay.fliplr(x)
tensor([[1, 0],
        [3, 2]])
#

flipud

functionFull reference ↗
tensorplay.functional.flipud(input) → Tensor[source]

Flip tensor in the up/down direction, returning a new tensor.

Flip the entries in each column in the up/down direction. Rows are preserved, but appear in a different order than before.

Note

Requires the tensor to be at least 1-D.

Note

tensorplay.flipud makes a copy of input’s data. This is different from NumPy’s np.flipud, which returns a view in constant time. Since copying a tensor’s data is more work than viewing that data, tensorplay.flipud is expected to be slower than np.flipud.

Parameters:

input (Tensor) – Must be at least 1-dimensional.

Example:

>>> x = tensorplay.arange(4).view(2, 2)
>>> x
tensor([[0, 1],
        [2, 3]])
>>> tensorplay.flipud(x)
tensor([[2, 3],
        [0, 1]])
#

float_power

functionFull reference ↗
tensorplay.functional.float_power(input, exponent, *, out=None) → Tensor[source]

Raises input to the power of exponent, elementwise, in double precision. If neither input is complex returns a tensorplay.float64 tensor, and if one or more inputs is complex returns a tensorplay.complex128 tensor.

Note

This function always computes in double precision, unlike tensorplay.pow(), which implements more typical type promotion. This is useful when the computation needs to be performed in a wider or more precise dtype, or the results of the computation may contain fractional values not representable in the input dtypes, like when an integer base is raised to a negative integer exponent.

Parameters:
  • input (Tensor or Number) – the base value(s)

  • exponent (Tensor or Number) – the exponent value(s)

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randint(10, (4,))
>>> a
tensor([6, 4, 7, 1])
>>> tensorplay.float_power(a, 2)
tensor([36., 16., 49.,  1.], dtype=tensorplay.float64)

>>> a = tensorplay.arange(1, 5)
>>> a
tensor([ 1,  2,  3,  4])
>>> exp = tensorplay.tensor([2, -3, 4, -5])
>>> exp
tensor([ 2, -3,  4, -5])
>>> tensorplay.float_power(a, exp)
tensor([1.0000e+00, 1.2500e-01, 8.1000e+01, 9.7656e-04], dtype=tensorplay.float64)
#

floor_divide

functionFull reference ↗
tensorplay.functional.floor_divide(input, other, *, out=None) → Tensor[source]

Note

truncation division. To restore the previous behavior use tensorplay.div() with rounding_mode='trunc'.

Computes input divided by other, elementwise, and floors the result.

outi=floor(inputiotheri)\text{{out}}_i = \text{floor} \left( \frac{{\text{{input}}_i}}{{\text{{other}}_i}} \right)

Supports broadcasting to a common shape, type promotion, and integer and float inputs.

Parameters:
  • input (Tensor or Number) – the dividend

  • other (Tensor or Number) – the divisor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([4.0, 3.0])
>>> b = tensorplay.tensor([2.0, 2.0])
>>> tensorplay.floor_divide(a, b)
tensor([2.0, 1.0])
>>> tensorplay.floor_divide(a, 1.4)
tensor([2.0, 2.0])
#

floor

functionFull reference ↗
tensorplay.functional.floor(input, *, out=None) → Tensor[source]

Returns a new tensor with the floor of the elements of input, the largest integer less than or equal to each element.

For integer inputs, follows the array-api convention of returning a copy of the input tensor.

outi=⌊inputi⌋\text{out}_{i} = \left\lfloor \text{input}_{i} \right\rfloor
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([-0.8166,  1.5308, -0.2530, -0.2091])
>>> tensorplay.floor(a)
tensor([-1.,  1., -1., -1.])
#

fmax

functionFull reference ↗
tensorplay.functional.fmax(input, other, *, out=None) → Tensor[source]

Computes the element-wise maximum of input and other.

This is like tensorplay.maximum() except it handles NaNs differently: if exactly one of the two elements being compared is a NaN then the non-NaN element is taken as the maximum. Only if both elements are NaN is NaN propagated.

This function is a wrapper around C++’s std::fmax and is similar to NumPy’s fmax function.

Supports broadcasting to a common shape, type promotion, and integer and floating-point inputs.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([9.7, float('nan'), 3.1, float('nan')])
>>> b = tensorplay.tensor([-2.2, 0.5, float('nan'), float('nan')])
>>> tensorplay.fmax(a, b)
tensor([9.7000, 0.5000, 3.1000,    nan])
#

fmin

functionFull reference ↗
tensorplay.functional.fmin(input, other, *, out=None) → Tensor[source]

Computes the element-wise minimum of input and other.

This is like tensorplay.minimum() except it handles NaNs differently: if exactly one of the two elements being compared is a NaN then the non-NaN element is taken as the minimum. Only if both elements are NaN is NaN propagated.

This function is a wrapper around C++’s std::fmin and is similar to NumPy’s fmin function.

Supports broadcasting to a common shape, type promotion, and integer and floating-point inputs.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([2.2, float('nan'), 2.1, float('nan')])
>>> b = tensorplay.tensor([-9.3, 0.1, float('nan'), float('nan')])
>>> tensorplay.fmin(a, b)
tensor([-9.3000, 0.1000, 2.1000,    nan])
#

fmod

functionFull reference ↗
tensorplay.functional.fmod(input, other, *, out=None) → Tensor[source]

Applies C++’s std::fmod entrywise. The result has the same sign as the dividend input and its absolute value is less than that of other.

This function may be defined in terms of tensorplay.div() as

tensorplay.fmod(a, b) == a - a.div(b, rounding_mode="trunc") * b

Supports broadcasting to a common shape, type promotion, and integer and float inputs.

Note

When the divisor is zero, returns NaN for floating point dtypes on both CPU and GPU; raises RuntimeError for integer division by zero on CPU; Integer division by zero on GPU may return any value.

Note

Complex inputs are not supported. In some cases, it is not mathematically possible to satisfy the definition of a modulo operation with complex numbers.

See also

tensorplay.remainder() which implements Python’s modulus operator. This one is defined using division rounding down the result.

Parameters:
Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.fmod(tensorplay.tensor([-3., -2, -1, 1, 2, 3]), 2)
tensor([-1., -0., -1.,  1.,  0.,  1.])
>>> tensorplay.fmod(tensorplay.tensor([1, 2, 3, 4, 5]), -1.5)
tensor([1.0000, 0.5000, 0.0000, 1.0000, 0.5000])
#

frac

functionFull reference ↗
tensorplay.functional.frac(input, *, out=None) → Tensor[source]

Computes the fractional portion of each element in input.

outi=inputi−⌊∣inputi∣⌋∗sgn⁡(inputi)\text{out}_{i} = \text{input}_{i} - \left\lfloor |\text{input}_{i}| \right\rfloor * \operatorname{sgn}(\text{input}_{i})

Example:

>>> tensorplay.frac(tensorplay.tensor([1, 2.5, -3.2]))
tensor([ 0.0000,  0.5000, -0.2000])
#

frexp

functionFull reference ↗
tensorplay.functional.frexp(input, *, out=None) -> (Tensor mantissa, Tensor exponent)[source]

Decomposes input into mantissa and exponent tensors such that input=mantissa×2exponent\text{input} = \text{mantissa} \times 2^{\text{exponent}}.

The range of mantissa is the open interval (-1, 1).

Supports float inputs.

Parameters:

input (Tensor) – the input tensor

Keyword Arguments:

out (tuple, optional) – the output tensors

Example:

>>> x = tensorplay.arange(9.)
>>> mantissa, exponent = tensorplay.frexp(x)
>>> mantissa
tensor([0.0000, 0.5000, 0.5000, 0.7500, 0.5000, 0.6250, 0.7500, 0.8750, 0.5000])
>>> exponent
tensor([0, 1, 2, 2, 3, 3, 3, 3, 4], dtype=tensorplay.int32)
>>> tensorplay.ldexp(mantissa, exponent)
tensor([0., 1., 2., 3., 4., 5., 6., 7., 8.])
#

from_file

functionFull reference ↗
tensorplay.functional.from_file(filename, shared=None, size=0, *, dtype=None, layout=None, device=None, pin_memory=False)[source]

Creates a CPU tensor with a storage backed by a memory-mapped file.

If shared is True, then memory is shared between processes. All changes are written to the file. If shared is False, then changes to the tensor do not affect the file.

size is the number of elements in the Tensor. If shared is False, then the file must contain at least size * sizeof(dtype) bytes. If shared is True the file will be created if needed.

Note

Only CPU tensors can be mapped to files.

Note

For now, tensors with storages backed by a memory-mapped file cannot be created in pinned memory.

Parameters:
  • filename (str) – file name to map

  • shared (bool) – whether to share memory (whether MAP_SHARED or MAP_PRIVATE is passed to the underlying mmap(2) call)

  • size (int) – number of elements in the tensor

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()).

  • layout (tensorplay.layout, optional) – the desired layout of returned Tensor. Default: tensorplay.strided.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

Example:

>>> t = tensorplay.randn(2, 5, dtype=tensorplay.float64)
>>> t.numpy().tofile('storage.pt')
>>> t_mapped = tensorplay.from_file('storage.pt', shared=False, size=10, dtype=tensorplay.float64)
#

full_like

functionFull reference ↗
tensorplay.functional.full_like(input, fill_value, \*, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False, memory_format=tensorplay.preserve_format) → Tensor

Returns a tensor with the same size as input filled with fill_value. tensorplay.full_like(input, fill_value) is equivalent to tensorplay.full(input.size(), fill_value, dtype=input.dtype, layout=input.layout, device=input.device).

Parameters:
  • input (Tensor) – the size of input will determine size of the output tensor.

  • fill_value – the number to fill the output tensor with.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.

  • layout (tensorplay.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • memory_format (tensorplay.memory_format, optional) – the desired memory format of returned Tensor. Default: tensorplay.preserve_format.

Example:

>>> x = tensorplay.ones(2, 3)
>>> tensorplay.full_like(x, 3.141592)
tensor([[ 3.1416,  3.1416,  3.1416],
        [ 3.1416,  3.1416,  3.1416]])
>>> tensorplay.full_like(x, 7)
tensor([[7., 7., 7.],
        [7., 7., 7.]])
>>> tensorplay.full_like(x, 0.5, dtype=tensorplay.int32)
tensor([[0, 0, 0],
        [0, 0, 0]], dtype=tensorplay.int32)
>>> y = tensorplay.randn(3, 4, dtype=tensorplay.float64)
>>> tensorplay.full_like(y, -1.0)
tensor([[-1., -1., -1., -1.],
        [-1., -1., -1., -1.],
        [-1., -1., -1., -1.]], dtype=tensorplay.float64)
#

fused_moving_avg_obs_fake_quant

functionFull reference ↗
tensorplay.functional.fused_moving_avg_obs_fake_quant(input, observer_on, fake_quant_on, running_min, running_max, scale, zero_point, averaging_const, quant_min, quant_max, ch_axis, per_row_fake_quant=False, symmetric_quant=False)[source]
#

gather

functionFull reference ↗
tensorplay.functional.gather(input, dim, index, *, sparse_grad=False, out=None) → Tensor[source]

Gathers values along an axis specified by dim.

For a 3-D tensor the output is specified by:

out[i][j][k] = input[index[i][j][k]][j][k]  # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k]  # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]]  # if dim == 2

input and index must have the same number of dimensions. It is also required that index.size(d) <= input.size(d) for all dimensions d != dim. out will have the same shape as index. Note that input and index do not broadcast against each other. When index is empty, we always return an empty output with the same shape without further error checking.

Parameters:
  • input (Tensor) – the source tensor

  • dim (int) – the axis along which to index

  • index (LongTensor) – the indices of elements to gather

Keyword Arguments:
  • sparse_grad (bool, optional) – If True, gradient w.r.t. input will be a sparse tensor.

  • out (Tensor, optional) – the destination tensor

Example:

>>> t = tensorplay.tensor([[1, 2], [3, 4]])
>>> tensorplay.gather(t, 1, tensorplay.tensor([[0, 0], [1, 0]]))
tensor([[ 1,  1],
        [ 4,  3]])
#

gcd

functionFull reference ↗
tensorplay.functional.gcd(input, other, *, out=None) → Tensor[source]

Computes the element-wise greatest common divisor (GCD) of input and other.

Both input and other must have integer types.

Note

This defines gcd(0,0)=0gcd(0, 0) = 0.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([5, 10, 15])
>>> b = tensorplay.tensor([3, 4, 5])
>>> tensorplay.gcd(a, b)
tensor([1, 2, 5])
>>> c = tensorplay.tensor([3])
>>> tensorplay.gcd(a, c)
tensor([1, 1, 3])
#

ge

functionFull reference ↗
tensorplay.functional.ge(input, other, *, out=None) → Tensor[source]

Computes input≥other\text{input} \geq \text{other} element-wise.

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:
  • input (Tensor) – the tensor to compare

  • other (Tensor or float) – the tensor or value to compare

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Returns:

A boolean tensor that is True where input is greater than or equal to other and False elsewhere

Example:

>>> tensorplay.ge(tensorplay.tensor([[1, 2], [3, 4]]), tensorplay.tensor([[1, 1], [4, 4]]))
tensor([[True, True], [False, True]])
#

geqrf

functionFull reference ↗
tensorplay.functional.geqrf(input, *, out=None)[source]

This is a low-level function for calling LAPACK’s geqrf directly. This function returns a namedtuple (a, tau) as defined in LAPACK documentation for geqrf .

Computes a QR decomposition of input. Both Q and R matrices are stored in the same output tensor a. The elements of R are stored on and above the diagonal. Elementary reflectors (or Householder vectors) implicitly defining matrix Q are stored below the diagonal. The results of this function can be used together with tensorplay.linalg.householder_product() to obtain the Q matrix or with tensorplay.ormqr(), which uses an implicit representation of the Q matrix, for an efficient matrix-matrix multiplication.

See LAPACK documentation for geqrf for further details.

Note

See also tensorplay.linalg.qr(), which computes Q and R matrices, and tensorplay.linalg.lstsq() with the driver="gels" option for a function that can solve matrix equations using a QR decomposition.

Parameters:

input (Tensor) – the input matrix

Keyword Arguments:

out (tuple, optional) – the output tuple of (Tensor, Tensor). Ignored if None. Default: None.

#

ger

functionFull reference ↗
tensorplay.functional.ger(input, vec2, *, out=None) → Tensor[source]

Alias of tensorplay.outer().

Warning

Use tensorplay.outer() instead.

#

grid_sampler_2d

functionFull reference ↗
tensorplay.functional.grid_sampler_2d(input, grid, interpolation_mode, padding_mode, align_corners)[source]
#

grid_sampler_3d

functionFull reference ↗
tensorplay.functional.grid_sampler_3d(input, grid, interpolation_mode, padding_mode, align_corners)[source]
#

gru

functionFull reference ↗
tensorplay.functional.gru(input, hx, params, has_biases=True, num_layers=1, dropout_p=0.0, training=False, bidirectional=False, batch_first=False)[source]
#

gt

functionFull reference ↗
tensorplay.functional.gt(input, other, *, out=None) → Tensor[source]

Computes input>other\text{input} > \text{other} element-wise.

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:
  • input (Tensor) – the tensor to compare

  • other (Tensor or float) – the tensor or value to compare

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Returns:

A boolean tensor that is True where input is greater than other and False elsewhere

Example:

>>> tensorplay.gt(tensorplay.tensor([[1, 2], [3, 4]]), tensorplay.tensor([[1, 1], [4, 4]]))
tensor([[False, True], [False, False]])
#

hamming_window

functionFull reference ↗
tensorplay.functional.hamming_window(window_length, *, dtype=None, layout=None, device=None, pin_memory=False, requires_grad=False) → Tensor[source]

Hamming window function.

w[n]=α−β cos⁡(2πnN−1),w[n] = \alpha - \beta\ \cos \left( \frac{2 \pi n}{N - 1} \right),

where NN is the full window size.

The input window_length is a positive integer controlling the returned window size. periodic flag determines whether the returned window trims off the last duplicate value from the symmetric window and is ready to be used as a periodic window with functions like tensorplay.stft(). Therefore, if periodic is true, the NN in above formula is in fact window_length+1\text{window\_length} + 1. Also, we always have tensorplay.hamming_window(L, periodic=True) equal to tensorplay.hamming_window(L + 1, periodic=False)[:-1]).

Note

If window_length =1=1, the returned window contains a single value 1.

Note

This is a generalized version of tensorplay.hann_window().

Parameters:

window_length (int) – the size of returned window

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()). Only floating point types are supported.

  • layout (tensorplay.layout, optional) – the desired layout of returned window tensor. Only tensorplay.strided (dense layout) is supported.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Returns:

A 1-D tensor of size (window_length,)(\text{window\_length},) containing the window.

Return type:

Tensor

tensorplay.functional.hamming_window(window_length, periodic, *, dtype=None, layout=None, device=None, pin_memory=False, requires_grad=False) → Tensor[source]

Hamming window function with periodic specified.

Parameters:
  • window_length (int) – the size of returned window

  • periodic (bool) – If True, returns a window to be used as periodic function. If False, return a symmetric window.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()). Only floating point types are supported.

  • layout (tensorplay.layout, optional) – the desired layout of returned window tensor. Only tensorplay.strided (dense layout) is supported.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Returns:

A 1-D tensor of size (window_length,)(\text{window\_length},) containing the window.

Return type:

Tensor

tensorplay.functional.hamming_window(window_length, periodic, float alpha, *, dtype=None, layout=None, device=None, pin_memory=False, requires_grad=False) → Tensor[source]

Hamming window function with periodic and alpha specified.

Parameters:
  • window_length (int) – the size of returned window

  • periodic (bool) – If True, returns a window to be used as periodic function. If False, return a symmetric window.

  • alpha (float) – The coefficient α\alpha in the equation above

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()). Only floating point types are supported.

  • layout (tensorplay.layout, optional) – the desired layout of returned window tensor. Only tensorplay.strided (dense layout) is supported.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Returns:

A 1-D tensor of size (window_length,)(\text{window\_length},) containing the window.

Return type:

Tensor

tensorplay.functional.hamming_window(window_length, periodic, float alpha, float beta, *, dtype=None, layout=None, device=None, pin_memory=False, requires_grad=False) → Tensor[source]

Hamming window function with periodic, alpha and beta specified.

Parameters:
  • window_length (int) – the size of returned window

  • periodic (bool) – If True, returns a window to be used as periodic function. If False, return a symmetric window.

  • alpha (float) – The coefficient α\alpha in the equation above

  • beta (float) – The coefficient β\beta in the equation above

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()). Only floating point types are supported.

  • layout (tensorplay.layout, optional) – the desired layout of returned window tensor. Only tensorplay.strided (dense layout) is supported.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Returns:

A 1-D tensor of size (window_length,)(\text{window\_length},) containing the window.

Return type:

Tensor

#

hann_window

functionFull reference ↗
tensorplay.functional.hann_window(window_length, periodic=True, *, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False) → Tensor[source]

Hann window function.

w[n]=12 [1−cos⁡(2πnN−1)]=sin⁡2(πnN−1),w[n] = \frac{1}{2}\ \left[1 - \cos \left( \frac{2 \pi n}{N - 1} \right)\right] = \sin^2 \left( \frac{\pi n}{N - 1} \right),

where NN is the full window size.

The input window_length is a positive integer controlling the returned window size. periodic flag determines whether the returned window trims off the last duplicate value from the symmetric window and is ready to be used as a periodic window with functions like tensorplay.stft(). Therefore, if periodic is true, the NN in above formula is in fact window_length+1\text{window\_length} + 1. Also, we always have tensorplay.hann_window(L, periodic=True) equal to tensorplay.hann_window(L + 1, periodic=False)[:-1]).

Note

If window_length =1=1, the returned window contains a single value 1.

Parameters:
  • window_length (int) – the size of returned window

  • periodic (bool, optional) – If True, returns a window to be used as periodic function. If False, return a symmetric window.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()). Only floating point types are supported.

  • layout (tensorplay.layout, optional) – the desired layout of returned window tensor. Only tensorplay.strided (dense layout) is supported.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Returns:

A 1-D tensor of size (window_length,)(\text{window\_length},) containing the window

Return type:

Tensor

#

hash_tensor

functionFull reference ↗
tensorplay.functional.hash_tensor(input, *, mode=0) → Tensor[source]

Returns a hash of all elements in the input tensor.

Currently only mode=0 (reduction via xor) is supported. The output will always be of type tensorplay.uint64. The elements of input are upcasted to their 64 bit float / integer equivalent and bitcasted to tensorplay.uint64 before reduction via xor.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

mode (int) – The hash to use. Default: 0 (xor_reduction)

Example:

>>> a = tensorplay.randn(1, 3)
>>> a
tensor([[ 1.1918, -1.1813,  0.3373]])
>>> tensorplay.hash_tensor(a)
tensor(13822780554648485888, dtype=tensorplay.uint64)
tensorplay.functional.hash_tensor(input, dim, *, keepdim=False, mode=0) → Tensor[source]

Returns the hash of each row of the input tensor in the given dimension dim given by mode. If dim is a list of dimensions, reduce over all of them.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) –

    the dimension or dimensions to reduce. If None, all dimensions are reduced.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

Keyword Arguments:

mode (int) – The hash to use. Default: 0 (xor_reduction)

Example:

>>> a = tensorplay.randn(2, 4)
>>> a
tensor([[ 0.1317, -0.5554, -1.4724, -1.1391],
        [ 0.0778, -0.6070,  0.6375,  0.1798]])
>>> tensorplay.hash_tensor(a, 1)
tensor([9233691267014066176, 9255993250844508160], dtype=tensorplay.uint64)
#

heaviside

functionFull reference ↗
tensorplay.functional.heaviside(input, values, *, out=None) → Tensor[source]

Computes the Heaviside step function for each element in input. The Heaviside step function is defined as:

heaviside(input,values)={0,if input < 0values,if input == 01,if input > 0\text{{heaviside}}(input, values) = \begin{cases} 0, & \text{if input < 0}\\ values, & \text{if input == 0}\\ 1, & \text{if input > 0} \end{cases}
Parameters:
  • input (Tensor) – the input tensor.

  • values (Tensor) – The values to use where input is zero.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> input = tensorplay.tensor([-1.5, 0, 2.0])
>>> values = tensorplay.tensor([0.5])
>>> tensorplay.heaviside(input, values)
tensor([0.0000, 0.5000, 1.0000])
>>> values = tensorplay.tensor([1.2, -2.0, 3.5])
>>> tensorplay.heaviside(input, values)
tensor([0., -2., 1.])
#

histc

functionFull reference ↗
tensorplay.functional.histc(input, bins=100, min=0, max=0, *, out=None) → Tensor[source]

Computes the histogram of a tensor.

The elements are sorted into equal width bins between min and max. If min and max are both zero, the minimum and maximum values of the data are used.

Elements lower than min and higher than max and NaN elements are ignored.

Parameters:
  • input (Tensor) – the input tensor.

  • bins (int) – number of histogram bins

  • min (Scalar) – lower end of the range (inclusive)

  • max (Scalar) – upper end of the range (inclusive)

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Returns:

Histogram represented as a tensor

Return type:

Tensor

Example:

>>> tensorplay.histc(tensorplay.tensor([1., 2, 1]), bins=4, min=0, max=3)
tensor([ 0.,  2.,  1.,  0.])
#

histogramdd

functionFull reference ↗
tensorplay.functional.histogramdd(input, bins, *, range=None, weight=None, density=False, out=None) -> (Tensor, Tensor[])

Computes a multi-dimensional histogram of the values in a tensor.

Interprets the elements of an input tensor whose innermost dimension has size N as a collection of N-dimensional points. Maps each of the points into a set of N-dimensional bins and returns the number of points (or total weight) in each bin.

input must be a tensor with at least 2 dimensions. If input has shape (M, N), each of its M rows defines a point in N-dimensional space. If input has three or more dimensions, all but the last dimension are flattened.

Each dimension is independently associated with its own strictly increasing sequence of bin edges. Bin edges may be specified explicitly by passing a sequence of 1D tensors. Alternatively, bin edges may be constructed automatically by passing a sequence of integers specifying the number of equal-width bins in each dimension.

For each N-dimensional point in input:
  • Each of its coordinates is binned independently among the bin edges

    corresponding to its dimension

  • Binning results are combined to identify the N-dimensional bin (if any)

    into which the point falls

  • If the point falls into a bin, the bin’s count (or total weight) is incremented

  • Points which do not fall into any bin do not contribute to the output

bins can be a sequence of N 1D tensors, a sequence of N ints, or a single int.

If bins is a sequence of N 1D tensors, it explicitly specifies the N sequences of bin edges. Each 1D tensor should contain a strictly increasing sequence with at least one element. A sequence of K bin edges defines K-1 bins, explicitly specifying the left and right edges of all bins. Every bin is inclusive of its left edge. Only the rightmost bin is inclusive of its right edge.

If bins is a sequence of N ints, it specifies the number of equal-width bins in each dimension. By default, the leftmost and rightmost bin edges in each dimension are determined by the minimum and maximum elements of the input tensor in the corresponding dimension. The range argument can be provided to manually specify the leftmost and rightmost bin edges in each dimension.

If bins is an int, it specifies the number of equal-width bins for all dimensions.

Note

See also tensorplay.histogram(), which specifically computes 1D histograms. While tensorplay.histogramdd() infers the dimensionality of its bins and binned values from the shape of input, tensorplay.histogram() accepts and flattens input of any shape.

Parameters:
  • input (Tensor) – the input tensor.

  • bins – Tensor[], int[], or int. If Tensor[], defines the sequences of bin edges. If int[], defines the number of equal-width bins in each dimension. If int, defines the number of equal-width bins for all dimensions.

Keyword Arguments:
  • range (sequence of float) – Defines the leftmost and rightmost bin edges in each dimension.

  • weight (Tensor) – By default, each value in the input has weight 1. If a weight tensor is passed, each N-dimensional coordinate in input contributes its associated weight towards its bin’s result. The weight tensor should have the same shape as the input tensor excluding its innermost dimension N.

  • density (bool) – If False (default), the result will contain the count (or total weight) in each bin. If True, each count (weight) is divided by the total count (total weight), then divided by the volume of its associated bin.

Returns:

N-dimensional Tensor containing the values of the histogram. bin_edges(Tensor[]): sequence of N 1D Tensors containing the bin edges.

Return type:

hist (Tensor)

Example:

>>> tensorplay.histogramdd(tensorplay.tensor([[0., 1.], [1., 0.], [2., 0.], [2., 2.]]), bins=[3, 3],
...                   weight=tensorplay.tensor([1., 2., 4., 8.]))
    tensorplay.return_types.histogramdd(
        hist=tensor([[0., 1., 0.],
                     [2., 0., 0.],
                     [4., 0., 8.]]),
        bin_edges=(tensor([0.0000, 0.6667, 1.3333, 2.0000]),
                   tensor([0.0000, 0.6667, 1.3333, 2.0000])))

>>> tensorplay.histogramdd(tensorplay.tensor([[0., 0.], [1., 1.], [2., 2.]]), bins=[2, 2],
...                   range=[0., 1., 0., 1.], density=True)
    tensorplay.return_types.histogramdd(
       hist=tensor([[2., 0.],
                    [0., 2.]]),
       bin_edges=(tensor([0.0000, 0.5000, 1.0000]),
                  tensor([0.0000, 0.5000, 1.0000])))
#

hsplit

functionFull reference ↗
tensorplay.functional.hsplit(input, indices_or_sections) → List of Tensors[source]

Splits input, a tensor with one or more dimensions, into multiple tensors horizontally according to indices_or_sections. Each split is a view of input.

If input is one dimensional this is equivalent to calling tensorplay.tensor_split(input, indices_or_sections, dim=0) (the split dimension is zero), and if input has two or more dimensions it’s equivalent to calling tensorplay.tensor_split(input, indices_or_sections, dim=1) (the split dimension is 1), except that if indices_or_sections is an integer it must evenly divide the split dimension or a runtime error will be thrown.

This function is based on NumPy’s numpy.hsplit().

Parameters:
  • input (Tensor) – tensor to split.

  • indices_or_sections (int or list or tuple of ints) – See argument in tensorplay.tensor_split().

Example:

>>> t = tensorplay.arange(16.0).reshape(4,4)
>>> t
tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.],
        [12., 13., 14., 15.]])
>>> tensorplay.hsplit(t, 2)
(tensor([[ 0.,  1.],
         [ 4.,  5.],
         [ 8.,  9.],
         [12., 13.]]),
 tensor([[ 2.,  3.],
         [ 6.,  7.],
         [10., 11.],
         [14., 15.]]))
>>> tensorplay.hsplit(t, [3, 6])
(tensor([[ 0.,  1.,  2.],
         [ 4.,  5.,  6.],
         [ 8.,  9., 10.],
         [12., 13., 14.]]),
 tensor([[ 3.],
         [ 7.],
         [11.],
         [15.]]),
 tensor([], size=(4, 0)))
#

hstack

functionFull reference ↗
tensorplay.functional.hstack(tensors, *, out=None) → Tensor[source]

Stack tensors in sequence horizontally (column wise).

This is equivalent to concatenation along the first axis for 1-D tensors, and along the second axis for all other tensors.

Parameters:

tensors (sequence of Tensors) – sequence of tensors to concatenate

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([1, 2, 3])
>>> b = tensorplay.tensor([4, 5, 6])
>>> tensorplay.hstack((a,b))
tensor([1, 2, 3, 4, 5, 6])
>>> a = tensorplay.tensor([[1],[2],[3]])
>>> b = tensorplay.tensor([[4],[5],[6]])
>>> tensorplay.hstack((a,b))
tensor([[1, 4],
        [2, 5],
        [3, 6]])
#

hypot

functionFull reference ↗
tensorplay.functional.hypot(input, other, *, out=None) → Tensor[source]

Given the legs of a right triangle, return its hypotenuse.

outi=inputi2+otheri2\text{out}_{i} = \sqrt{\text{input}_{i}^{2} + \text{other}_{i}^{2}}

The shapes of input and other must be broadcastable.

Parameters:
  • input (Tensor) – the first input tensor

  • other (Tensor) – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.hypot(tensorplay.tensor([4.0]), tensorplay.tensor([3.0, 4.0, 5.0]))
tensor([5.0000, 5.6569, 6.4031])
#

imag

functionFull reference ↗
tensorplay.functional.imag(input) → Tensor[source]

Returns a new tensor containing imaginary values of the self tensor. The returned tensor and self share the same underlying storage.

Warning

imag() is only supported for tensors with complex dtypes.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> x=tensorplay.randn(4, dtype=tensorplay.cfloat)
>>> x
tensor([(0.3100+0.3553j), (-0.5445-0.7896j), (-1.6492-0.0633j), (-0.0638-0.8119j)])
>>> x.imag
tensor([ 0.3553, -0.7896, -0.0633, -0.8119])
#

index_select

functionFull reference ↗
tensorplay.functional.index_select(input, dim, index, *, out=None) → Tensor[source]

Returns a new tensor which indexes the input tensor along dimension dim using the entries in index which is a LongTensor.

The returned tensor has the same number of dimensions as the original tensor (input). The dimth dimension has the same size as the length of index; other dimensions have the same size as in the original tensor.

Note

The returned tensor does not use the same storage as the original tensor. If out has a different shape than expected, we silently change it to the correct shape, reallocating the underlying storage if necessary.

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int) – the dimension in which we index

  • index (IntTensor or LongTensor) – the 1-D tensor containing the indices to index

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> x = tensorplay.randn(3, 4)
>>> x
tensor([[ 0.1427,  0.0231, -0.5414, -1.0009],
        [-0.4664,  0.2647, -0.1228, -1.1068],
        [-1.1734, -0.6571,  0.7230, -0.6004]])
>>> indices = tensorplay.tensor([0, 2])
>>> tensorplay.index_select(x, 0, indices)
tensor([[ 0.1427,  0.0231, -0.5414, -1.0009],
        [-1.1734, -0.6571,  0.7230, -0.6004]])
>>> tensorplay.index_select(x, 1, indices)
tensor([[ 0.1427, -0.5414],
        [-0.4664, -0.1228],
        [-1.1734,  0.7230]])
#

inner

functionFull reference ↗
tensorplay.functional.inner(input, other, *, out=None) → Tensor[source]

Computes the dot product for 1D tensors. For higher dimensions, sums the product of elements from input and other along their last dimension.

Note

If either input or other is a scalar, the result is equivalent to tensorplay.mul(input, other).

If both input and other are non-scalars, the size of their last dimension must match and the result is equivalent to tensorplay.tensordot(input, other, dims=([-1], [-1]))

Parameters:
  • input (Tensor) – First input tensor

  • other (Tensor) – Second input tensor

Keyword Arguments:

out (Tensor, optional) – Optional output tensor to write result into. The output shape is input.shape[:-1] + other.shape[:-1].

Example:

# Dot product
>>> tensorplay.inner(tensorplay.tensor([1, 2, 3]), tensorplay.tensor([0, 2, 1]))
tensor(7)

# Multidimensional input tensors
>>> a = tensorplay.randn(2, 3)
>>> a
tensor([[0.8173, 1.0874, 1.1784],
        [0.3279, 0.1234, 2.7894]])
>>> b = tensorplay.randn(2, 4, 3)
>>> b
tensor([[[-0.4682, -0.7159,  0.1506],
        [ 0.4034, -0.3657,  1.0387],
        [ 0.9892, -0.6684,  0.1774],
        [ 0.9482,  1.3261,  0.3917]],

        [[ 0.4537,  0.7493,  1.1724],
        [ 0.2291,  0.5749, -0.2267],
        [-0.7920,  0.3607, -0.3701],
        [ 1.3666, -0.5850, -1.7242]]])
>>> tensorplay.inner(a, b)
tensor([[[-0.9837,  1.1560,  0.2907,  2.6785],
        [ 2.5671,  0.5452, -0.6912, -1.5509]],

        [[ 0.1782,  2.9843,  0.7366,  1.5672],
        [ 3.5115, -0.4864, -1.2476, -4.4337]]])

# Scalar input
>>> tensorplay.inner(a, tensorplay.tensor(2))
tensor([[1.6347, 2.1748, 2.3567],
        [0.6558, 0.2469, 5.5787]])
#

instance_norm

functionFull reference ↗
tensorplay.functional.instance_norm(input, weight=None, bias=None, running_mean=None, running_var=None, use_input_stats=True, momentum=0.1, eps=1e-05)[source]
#

is_complex

functionFull reference ↗
tensorplay.functional.is_complex(input: Tensor) → bool[source]

Returns True if the data type of input is a complex data type i.e., one of tensorplay.complex64, and tensorplay.complex128.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> tensorplay.is_complex(tensorplay.tensor([1, 2, 3], dtype=tensorplay.complex64))
True
>>> tensorplay.is_complex(tensorplay.tensor([1, 2, 3], dtype=tensorplay.complex128))
True
>>> tensorplay.is_complex(tensorplay.tensor([1, 2, 3], dtype=tensorplay.int32))
False
>>> tensorplay.is_complex(tensorplay.tensor([1.0, 2.0, 3.0], dtype=tensorplay.float16))
False
#

is_conj

functionFull reference ↗
tensorplay.functional.is_conj(input)[source]

Returns True if the input is a conjugated tensor, i.e. its conjugate bit is set to True.

Parameters:

input (Tensor) – the input tensor.

#

is_floating_point

functionFull reference ↗
tensorplay.functional.is_floating_point(input: Tensor) → bool[source]

Returns True if the data type of input is a floating point data type i.e., one of tensorplay.float64, tensorplay.float32, tensorplay.float16, and tensorplay.bfloat16.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> tensorplay.is_floating_point(tensorplay.tensor([1.0, 2.0, 3.0]))
True
>>> tensorplay.is_floating_point(tensorplay.tensor([1, 2, 3], dtype=tensorplay.int32))
False
>>> tensorplay.is_floating_point(tensorplay.tensor([1.0, 2.0, 3.0], dtype=tensorplay.float16))
True
>>> tensorplay.is_floating_point(tensorplay.tensor([1, 2, 3], dtype=tensorplay.complex64))
False
#

is_inference

functionFull reference ↗
tensorplay.functional.is_inference(input)[source]

Returns True if input is an inference tensor.

A non-view tensor is an inference tensor if and only if it was allocated during inference mode. A view tensor is an inference tensor if and only if the tensor it is a view of is an inference tensor.

For details on inference mode please see Inference Mode.

Parameters:

input (Tensor) – the input tensor.

#

is_nonzero

functionFull reference ↗
tensorplay.functional.is_nonzero(input)[source]

Returns True if the input is a single element tensor which is not equal to zero after type conversions. i.e. not equal to tensorplay.tensor([0.]) or tensorplay.tensor([0]) or tensorplay.tensor([False]). Throws a RuntimeError if tensorplay.numel() != 1 (even in case of sparse tensors).

Parameters:

input (Tensor) – the input tensor.

Examples:

>>> tensorplay.is_nonzero(tensorplay.tensor([0.]))
False
>>> tensorplay.is_nonzero(tensorplay.tensor([1.5]))
True
>>> tensorplay.is_nonzero(tensorplay.tensor([False]))
False
>>> tensorplay.is_nonzero(tensorplay.tensor([3]))
True
>>> tensorplay.is_nonzero(tensorplay.tensor([1, 3, 5]))
Traceback (most recent call last):
...
RuntimeError: Boolean value of Tensor with more than one value is ambiguous
>>> tensorplay.is_nonzero(tensorplay.tensor([]))
Traceback (most recent call last):
...
RuntimeError: Boolean value of Tensor with no values is ambiguous
#

isclose

functionFull reference ↗
tensorplay.functional.isclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False) → Tensor[source]

Returns a new tensor with boolean elements representing if each element of input is “close” to the corresponding element of other. Closeness is defined as:

∣inputi−otheri∣≤rtol×∣otheri∣+atol\lvert \text{input}_i - \text{other}_i \rvert \leq \texttt{rtol} \times \lvert \text{other}_i \rvert + \texttt{atol}

where input and other are finite. Where input and/or other are nonfinite they are close if and only if they are equal, with NaNs being considered equal to each other when equal_nan is True.

Parameters:
  • input (Tensor) – first tensor to compare

  • other (Tensor) – second tensor to compare

  • rtol (float, optional) – relative tolerance. Default: 1e-05

  • atol (float, optional) – absolute tolerance. Default: 1e-08

  • equal_nan (bool, optional) – if True, then two NaN s will be considered equal. Default: False

Examples:

>>> tensorplay.isclose(tensorplay.tensor((1., 2, 3)), tensorplay.tensor((1 + 1e-10, 3, 4)))
tensor([ True, False, False])
>>> tensorplay.isclose(tensorplay.tensor((float('inf'), 4)), tensorplay.tensor((float('inf'), 6)), rtol=.5)
tensor([True, True])
#

isfinite

functionFull reference ↗
tensorplay.functional.isfinite(input) → Tensor[source]

Returns a new tensor with boolean elements representing if each element is finite or not.

Real values are finite when they are not NaN, negative infinity, or infinity. Complex values are finite when both their real and imaginary parts are finite.

Parameters:

input (Tensor) – the input tensor.

Returns:

A boolean tensor that is True where input is finite and False elsewhere

Example:

>>> tensorplay.isfinite(tensorplay.tensor([1, float('inf'), 2, float('-inf'), float('nan')]))
tensor([True,  False,  True,  False,  False])
#

isin

functionFull reference ↗
tensorplay.functional.isin(elements, test_elements, *, assume_unique=False, invert=False) → Tensor[source]

Tests if each element of elements is in test_elements. Returns a boolean tensor of the same shape as elements that is True for elements in test_elements and False otherwise.

Note

One of elements or test_elements can be a scalar, but not both.

Parameters:
  • elements (Tensor or Scalar) – Input elements

  • test_elements (Tensor or Scalar) – Values against which to test for each input element

  • assume_unique (bool, optional) – If True, assumes both elements and test_elements contain unique elements, which can speed up the calculation. Default: False

  • invert (bool, optional) – If True, inverts the boolean return tensor, resulting in True values for elements not in test_elements. Default: False

Returns:

A boolean tensor of the same shape as elements that is True for elements in test_elements and False otherwise

Example

>>> tensorplay.isin(tensorplay.tensor([[1, 2], [3, 4]]), tensorplay.tensor([2, 3]))
tensor([[False,  True],
        [ True, False]])
#

isinf

functionFull reference ↗
tensorplay.functional.isinf(input) → Tensor[source]

Tests if each element of input is infinite (positive or negative infinity) or not.

Note

Complex values are infinite when their real or imaginary part is infinite.

Parameters:

input (Tensor) – the input tensor.

Returns:

A boolean tensor that is True where input is infinite and False elsewhere

Example:

>>> tensorplay.isinf(tensorplay.tensor([1, float('inf'), 2, float('-inf'), float('nan')]))
tensor([False,  True,  False,  True,  False])
#

isnan

functionFull reference ↗
tensorplay.functional.isnan(input) → Tensor[source]

Returns a new tensor with boolean elements representing if each element of input is NaN or not. Complex values are considered NaN when either their real and/or imaginary part is NaN.

Parameters:

input (Tensor) – the input tensor.

Returns:

A boolean tensor that is True where input is NaN and False elsewhere

Example:

>>> tensorplay.isnan(tensorplay.tensor([1, float('nan'), 2]))
tensor([False, True, False])
#

isneginf

functionFull reference ↗
tensorplay.functional.isneginf(input, *, out=None) → Tensor[source]

Tests if each element of input is negative infinity or not.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([-float('inf'), float('inf'), 1.2])
>>> tensorplay.isneginf(a)
tensor([ True, False, False])
#

isposinf

functionFull reference ↗
tensorplay.functional.isposinf(input, *, out=None) → Tensor[source]

Tests if each element of input is positive infinity or not.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([-float('inf'), float('inf'), 1.2])
>>> tensorplay.isposinf(a)
tensor([False,  True, False])
#

isreal

functionFull reference ↗
tensorplay.functional.isreal(input) → Tensor[source]

Returns a new tensor with boolean elements representing if each element of input is real-valued or not. All real-valued types are considered real. Complex values are considered real when their imaginary part is 0.

Parameters:

input (Tensor) – the input tensor.

Returns:

A boolean tensor that is True where input is real and False elsewhere

Example:

>>> tensorplay.isreal(tensorplay.tensor([1, 1+1j, 2+0j]))
tensor([True, False, True])
#

istft

functionFull reference ↗
tensorplay.functional.istft(input, n_fft, hop_length=None, win_length=None, window=None, center=True, normalized=False, onesided=True, length=None, return_complex=False)[source]
#

kaiser_window

functionFull reference ↗
tensorplay.functional.kaiser_window(window_length, periodic=True, beta=12.0, *, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False) → Tensor[source]

Computes the Kaiser window with window length window_length and shape parameter beta.

Let I_0 be the zeroth order modified Bessel function of the first kind (see tensorplay.i0()) and N = L - 1 if periodic is False and L if periodic is True, where L is the window_length. This function computes:

outi=I0(β1−(i−N/2N/2)2)/I0(β)out_i = I_0 \left( \beta \sqrt{1 - \left( {\frac{i - N/2}{N/2}} \right) ^2 } \right) / I_0( \beta )

Calling tensorplay.kaiser_window(L, B, periodic=True) is equivalent to calling tensorplay.kaiser_window(L + 1, B, periodic=False)[:-1]). The periodic argument is intended as a helpful shorthand to produce a periodic window as input to functions like tensorplay.stft().

Note

If window_length is one, then the returned window is a single element tensor containing a one.

Parameters:
  • window_length (int) – length of the window.

  • periodic (bool, optional) – If True, returns a periodic window suitable for use in spectral analysis. If False, returns a symmetric window suitable for use in filter design.

  • beta (float, optional) – shape parameter for the window.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()).

  • layout (tensorplay.layout, optional) – the desired layout of returned window tensor. Only tensorplay.strided (dense layout) is supported.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

#

kron

functionFull reference ↗
tensorplay.functional.kron(input, other, *, out=None) → Tensor[source]

Computes the Kronecker product, denoted by ⊗\otimes, of input and other.

If input is a (a0×a1×⋯×an)(a_0 \times a_1 \times \dots \times a_n) tensor and other is a (b0×b1×⋯×bn)(b_0 \times b_1 \times \dots \times b_n) tensor, the result will be a (a0∗b0×a1∗b1×⋯×an∗bn)(a_0*b_0 \times a_1*b_1 \times \dots \times a_n*b_n) tensor with the following entries:

(input⊗other)k0,k1,…,kn=inputi0,i1,…,in∗otherj0,j1,…,jn,(\text{input} \otimes \text{other})_{k_0, k_1, \dots, k_n} = \text{input}_{i_0, i_1, \dots, i_n} * \text{other}_{j_0, j_1, \dots, j_n},

where kt=it∗bt+jtk_t = i_t * b_t + j_t for 0≤t≤n0 \leq t \leq n. If one tensor has fewer dimensions than the other it is unsqueezed until it has the same number of dimensions.

Supports real-valued and complex-valued inputs.

Note

This function generalizes the typical definition of the Kronecker product for two matrices to two tensors, as described above. When input is a (m×n)(m \times n) matrix and other is a (p×q)(p \times q) matrix, the result will be a (p∗m×q∗n)(p*m \times q*n) block matrix:

A⊗B=[a11B⋯a1nB⋮⋱⋮am1B⋯amnB]\mathbf{A} \otimes \mathbf{B}=\begin{bmatrix} a_{11} \mathbf{B} & \cdots & a_{1 n} \mathbf{B} \\ \vdots & \ddots & \vdots \\ a_{m 1} \mathbf{B} & \cdots & a_{m n} \mathbf{B} \end{bmatrix}

where input is A\mathbf{A} and other is B\mathbf{B}.

Parameters:
Keyword Arguments:

out (Tensor, optional) – The output tensor. Ignored if None. Default: None

Examples:

>>> mat1 = tensorplay.eye(2)
>>> mat2 = tensorplay.ones(2, 2)
>>> tensorplay.kron(mat1, mat2)
tensor([[1., 1., 0., 0.],
        [1., 1., 0., 0.],
        [0., 0., 1., 1.],
        [0., 0., 1., 1.]])

>>> mat1 = tensorplay.eye(2)
>>> mat2 = tensorplay.arange(1, 5).reshape(2, 2)
>>> tensorplay.kron(mat1, mat2)
tensor([[1., 2., 0., 0.],
        [3., 4., 0., 0.],
        [0., 0., 1., 2.],
        [0., 0., 3., 4.]])
#

lcm

functionFull reference ↗
tensorplay.functional.lcm(input, other, *, out=None) → Tensor[source]

Computes the element-wise least common multiple (LCM) of input and other.

Both input and other must have integer types.

Note

This defines lcm(0,0)=0lcm(0, 0) = 0 and lcm(0,a)=0lcm(0, a) = 0.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([5, 10, 15])
>>> b = tensorplay.tensor([3, 4, 5])
>>> tensorplay.lcm(a, b)
tensor([15, 20, 15])
>>> c = tensorplay.tensor([3])
>>> tensorplay.lcm(a, c)
tensor([15, 30, 15])
#

ldexp

functionFull reference ↗
tensorplay.functional.ldexp(input, other, *, out=None) → Tensor[source]

Multiplies input by 2 ** other.

outi=inputi∗2iother\text{{out}}_i = \text{{input}}_i * 2^\text{{other}}_i

Typically this function is used to construct floating point numbers by multiplying mantissas in input with integral powers of two created from the exponents in other.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – a tensor of exponents, typically integers.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.ldexp(tensorplay.tensor([1.]), tensorplay.tensor([1]))
tensor([2.])
>>> tensorplay.ldexp(tensorplay.tensor([1.0]), tensorplay.tensor([1, 2, 3, 4]))
tensor([ 2.,  4.,  8., 16.])
#

le

functionFull reference ↗
tensorplay.functional.le(input, other, *, out=None) → Tensor[source]

Computes input≤other\text{input} \leq \text{other} element-wise.

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:
  • input (Tensor) – the tensor to compare

  • other (Tensor or Scalar) – the tensor or value to compare

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Returns:

A boolean tensor that is True where input is less than or equal to other and False elsewhere

Example:

>>> tensorplay.le(tensorplay.tensor([[1, 2], [3, 4]]), tensorplay.tensor([[1, 1], [4, 4]]))
tensor([[True, False], [True, True]])
#

lerp

functionFull reference ↗
tensorplay.functional.lerp(input, end, weight, *, out=None)[source]

Does a linear interpolation of two tensors start (given by input) and end based on a scalar or tensor weight and returns the resulting out tensor.

outi=starti+weighti×(endi−starti)\text{out}_i = \text{start}_i + \text{weight}_i \times (\text{end}_i - \text{start}_i)

The shapes of start and end must be broadcastable. If weight is a tensor, then the shapes of weight, start, and end must be broadcastable.

Parameters:
  • input (Tensor) – the tensor with the starting points

  • end (Tensor) – the tensor with the ending points

  • weight (float or tensor) – the weight for the interpolation formula

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> start = tensorplay.arange(1., 5.)
>>> end = tensorplay.empty(4).fill_(10)
>>> start
tensor([ 1.,  2.,  3.,  4.])
>>> end
tensor([ 10.,  10.,  10.,  10.])
>>> tensorplay.lerp(start, end, 0.5)
tensor([ 5.5000,  6.0000,  6.5000,  7.0000])
>>> tensorplay.lerp(start, end, tensorplay.full_like(start, 0.5))
tensor([ 5.5000,  6.0000,  6.5000,  7.0000])
#

lgamma

functionFull reference ↗
tensorplay.functional.lgamma(input, *, out=None) → Tensor[source]

Computes the natural logarithm of the absolute value of the gamma function on input.

outi=ln⁡∣Γ(inputi)∣\text{out}_{i} = \ln |\Gamma(\text{input}_{i})|
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.arange(0.5, 2, 0.5)
>>> tensorplay.lgamma(a)
tensor([ 0.5724,  0.0000, -0.1208])
#

linspace

functionFull reference ↗
tensorplay.functional.linspace(start, end, steps, *, out=None, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False) → Tensor[source]

Creates a one-dimensional tensor of size steps whose values are evenly spaced from start to end, inclusive. That is, the value are:

(start,start+end−startsteps−1,…,start+(steps−2)∗end−startsteps−1,end)(\text{start}, \text{start} + \frac{\text{end} - \text{start}}{\text{steps} - 1}, \ldots, \text{start} + (\text{steps} - 2) * \frac{\text{end} - \text{start}}{\text{steps} - 1}, \text{end})
Parameters:
  • start (float or Tensor) – the starting value for the set of points. If Tensor, it must be 0-dimensional

  • end (float or Tensor) – the ending value for the set of points. If Tensor, it must be 0-dimensional

  • steps (int) – size of the constructed tensor

Keyword Arguments:
  • out (Tensor, optional) – the output tensor.

  • dtype (tensorplay.dtype, optional) – the data type to perform the computation in. Default: if None, uses the global default dtype (see tensorplay.get_default_dtype()) when both start and end are real, and corresponding complex dtype when either is complex.

  • layout (tensorplay.layout, optional) – the desired layout of returned Tensor. Default: tensorplay.strided.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

>>> tensorplay.linspace(3, 10, steps=5)
tensor([  3.0000,   4.7500,   6.5000,   8.2500,  10.0000])
>>> tensorplay.linspace(-10, 10, steps=5)
tensor([-10.,  -5.,   0.,   5.,  10.])
>>> tensorplay.linspace(start=-10, end=10, steps=5)
tensor([-10.,  -5.,   0.,   5.,  10.])
>>> tensorplay.linspace(start=-10, end=10, steps=1)
tensor([-10.])
#

log

functionFull reference ↗
tensorplay.functional.log(input, *, out=None) → Tensor[source]

Returns a new tensor with the natural logarithm of the elements of input.

yi=log⁡e(xi)y_{i} = \log_{e} (x_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.rand(5) * 5
>>> a
tensor([4.7767, 4.3234, 1.2156, 0.2411, 4.5739])
>>> tensorplay.log(a)
tensor([ 1.5637,  1.4640,  0.1952, -1.4226,  1.5204])
#

log10

functionFull reference ↗
tensorplay.functional.log10(input: Tensor, *, out: Tensor | None) → Tensor[source]

Returns a new tensor with the logarithm to the base 10 of the elements of input.

yi=log⁡10(xi)y_{i} = \log_{10} (x_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.rand(5)
>>> a
tensor([ 0.5224,  0.9354,  0.7257,  0.1301,  0.2251])


>>> tensorplay.log10(a)
tensor([-0.2820, -0.0290, -0.1392, -0.8857, -0.6476])
#

log1p

functionFull reference ↗
tensorplay.functional.log1p(input, *, out=None) → Tensor[source]

Returns a new tensor with the natural logarithm of (1 + input).

yi=log⁡e(xi+1)y_i = \log_{e} (x_i + 1)

Note

This function is more accurate than tensorplay.log() for small values of input

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(5)
>>> a
tensor([-1.0090, -0.9923,  1.0249, -0.5372,  0.2492])
>>> tensorplay.log1p(a)
tensor([    nan, -4.8653,  0.7055, -0.7705,  0.2225])
#

log2

functionFull reference ↗
tensorplay.functional.log2(input: Tensor, *, out: Tensor | None) → Tensor[source]

Returns a new tensor with the logarithm to the base 2 of the elements of input.

yi=log⁡2(xi)y_{i} = \log_{2} (x_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.rand(5)
>>> a
tensor([ 0.8419,  0.8003,  0.9971,  0.5287,  0.0490])


>>> tensorplay.log2(a)
tensor([-0.2483, -0.3213, -0.0042, -0.9196, -4.3504])
#

logaddexp

functionFull reference ↗
tensorplay.functional.logaddexp(input, other, *, out=None) → Tensor[source]

Logarithm of the sum of exponentiations of the inputs.

Calculates pointwise log⁡(ex+ey)\log\left(e^x + e^y\right). This function is useful in statistics where the calculated probabilities of events may be so small as to exceed the range of normal floating point numbers. In such cases the logarithm of the calculated probability is stored. This function allows adding probabilities stored in such a fashion.

This op should be disambiguated with tensorplay.logsumexp() which performs a reduction on a single tensor.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.logaddexp(tensorplay.tensor([-1.0]), tensorplay.tensor([-1.0, -2, -3]))
tensor([-0.3069, -0.6867, -0.8731])
>>> tensorplay.logaddexp(tensorplay.tensor([-100.0, -200, -300]), tensorplay.tensor([-1.0, -2, -3]))
tensor([-1., -2., -3.])
>>> tensorplay.logaddexp(tensorplay.tensor([1.0, 2000, 30000]), tensorplay.tensor([-1.0, -2, -3]))
tensor([1.1269e+00, 2.0000e+03, 3.0000e+04])
#

logaddexp2

functionFull reference ↗
tensorplay.functional.logaddexp2(input, other, *, out=None) → Tensor[source]

Logarithm of the sum of exponentiations of the inputs in base-2.

Calculates pointwise log⁡2(2x+2y)\log_2\left(2^x + 2^y\right). See tensorplay.logaddexp() for more details.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

#

logcumsumexp

functionFull reference ↗
tensorplay.functional.logcumsumexp(input, dim, *, out=None) → Tensor[source]

Returns the logarithm of the cumulative summation of the exponentiation of elements of input in the dimension dim.

For summation index jj given by dim and other indices ii, the result is

logcumsumexp(x)ij=log⁡∑k=0jexp⁡(xik)\text{logcumsumexp}(x)_{ij} = \log \sum\limits_{k=0}^{j} \exp(x_{ik})
Parameters:
  • input (Tensor) – the input tensor.

  • dim (int) – the dimension to do the operation over

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(10)
>>> tensorplay.logcumsumexp(a, dim=0)
tensor([-0.42296738, -0.04462666,  0.86278635,  0.94622083,  1.05277811,
         1.39202815,  1.83525007,  1.84492621,  2.06084887,  2.06844475]))
#

logdet

functionFull reference ↗
tensorplay.functional.logdet(input) → Tensor[source]

Calculates log determinant of a square matrix or batches of square matrices.

It returns -inf if the input has a determinant of zero, and NaN if it has a negative determinant.

Note

Backward through logdet() internally uses SVD results when input is not invertible. In this case, double backward through logdet() will be unstable in when input doesn’t have distinct singular values. See tensorplay.linalg.svd() for details.

See also

tensorplay.linalg.slogdet() computes the sign (resp. angle) and natural logarithm of the absolute value of the determinant of real-valued (resp. complex) square matrices.

Parameters:

input (Tensor) – the input tensor of size (*, n, n) where * is zero or more batch dimensions.

Example:

>>> A = tensorplay.randn(3, 3)
>>> tensorplay.det(A)
tensor(0.2611)
>>> tensorplay.logdet(A)
tensor(-1.3430)
>>> A
tensor([[[ 0.9254, -0.6213],
         [-0.5787,  1.6843]],

        [[ 0.3242, -0.9665],
         [ 0.4539, -0.0887]],

        [[ 1.1336, -0.4025],
         [-0.7089,  0.9032]]])
>>> A.det()
tensor([1.1990, 0.4099, 0.7386])
>>> A.det().log()
tensor([ 0.1815, -0.8917, -0.3031])
#

logical_and

functionFull reference ↗
tensorplay.functional.logical_and(input, other, *, out=None) → Tensor[source]

Computes the element-wise logical AND of the given input tensors. Zeros are treated as False and nonzeros are treated as True.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the tensor to compute AND with

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.logical_and(tensorplay.tensor([True, False, True]), tensorplay.tensor([True, False, False]))
tensor([ True, False, False])
>>> a = tensorplay.tensor([0, 1, 10, 0], dtype=tensorplay.int8)
>>> b = tensorplay.tensor([4, 0, 1, 0], dtype=tensorplay.int8)
>>> tensorplay.logical_and(a, b)
tensor([False, False,  True, False])
>>> tensorplay.logical_and(a.double(), b.double())
tensor([False, False,  True, False])
>>> tensorplay.logical_and(a.double(), b)
tensor([False, False,  True, False])
>>> tensorplay.logical_and(a, b, out=tensorplay.empty(4, dtype=tensorplay.bool))
tensor([False, False,  True, False])
#

logical_not

functionFull reference ↗
tensorplay.functional.logical_not(input, *, out=None) → Tensor[source]

Computes the element-wise logical NOT of the given input tensor. If not specified, the output tensor will have the bool dtype. If the input tensor is not a bool tensor, zeros are treated as False and non-zeros are treated as True.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.logical_not(tensorplay.tensor([True, False]))
tensor([False,  True])
>>> tensorplay.logical_not(tensorplay.tensor([0, 1, -10], dtype=tensorplay.int8))
tensor([ True, False, False])
>>> tensorplay.logical_not(tensorplay.tensor([0., 1.5, -10.], dtype=tensorplay.double))
tensor([ True, False, False])
>>> tensorplay.logical_not(tensorplay.tensor([0., 1., -10.], dtype=tensorplay.double), out=tensorplay.empty(3, dtype=tensorplay.int16))
tensor([1, 0, 0], dtype=tensorplay.int16)
#

logical_or

functionFull reference ↗
tensorplay.functional.logical_or(input, other, *, out=None) → Tensor[source]

Computes the element-wise logical OR of the given input tensors. Zeros are treated as False and nonzeros are treated as True.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the tensor to compute OR with

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.logical_or(tensorplay.tensor([True, False, True]), tensorplay.tensor([True, False, False]))
tensor([ True, False,  True])
>>> a = tensorplay.tensor([0, 1, 10, 0], dtype=tensorplay.int8)
>>> b = tensorplay.tensor([4, 0, 1, 0], dtype=tensorplay.int8)
>>> tensorplay.logical_or(a, b)
tensor([ True,  True,  True, False])
>>> tensorplay.logical_or(a.double(), b.double())
tensor([ True,  True,  True, False])
>>> tensorplay.logical_or(a.double(), b)
tensor([ True,  True,  True, False])
>>> tensorplay.logical_or(a, b, out=tensorplay.empty(4, dtype=tensorplay.bool))
tensor([ True,  True,  True, False])
#

logical_xor

functionFull reference ↗
tensorplay.functional.logical_xor(input: Tensor, other: Tensor, *, out: Tensor | None) → Tensor[source]

Computes the element-wise logical XOR of the given input tensors. Zeros are treated as False and nonzeros are treated as True.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the tensor to compute XOR with

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.logical_xor(tensorplay.tensor([True, False, True]), tensorplay.tensor([True, False, False]))
tensor([False, False,  True])
>>> a = tensorplay.tensor([0, 1, 10, 0], dtype=tensorplay.int8)
>>> b = tensorplay.tensor([4, 0, 1, 0], dtype=tensorplay.int8)
>>> tensorplay.logical_xor(a, b)
tensor([ True,  True, False, False])
>>> tensorplay.logical_xor(a.double(), b.double())
tensor([ True,  True, False, False])
>>> tensorplay.logical_xor(a.double(), b)
tensor([ True,  True, False, False])
>>> tensorplay.logical_xor(a, b, out=tensorplay.empty(4, dtype=tensorplay.bool))
tensor([ True,  True, False, False])
#

logspace

functionFull reference ↗
tensorplay.functional.logspace(start, end, steps, base=10.0, *, out=None, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False) → Tensor[source]

Creates a one-dimensional tensor of size steps whose values are evenly spaced from basestart{{\text{{base}}}}^{{\text{{start}}}} to baseend{{\text{{base}}}}^{{\text{{end}}}}, inclusive, on a logarithmic scale with base base. That is, the values are:

(basestart,base(start+end−startsteps−1),…,base(start+(steps−2)∗end−startsteps−1),baseend)(\text{base}^{\text{start}}, \text{base}^{(\text{start} + \frac{\text{end} - \text{start}}{ \text{steps} - 1})}, \ldots, \text{base}^{(\text{start} + (\text{steps} - 2) * \frac{\text{end} - \text{start}}{ \text{steps} - 1})}, \text{base}^{\text{end}})
Parameters:
  • start (float or Tensor) – the starting value for the set of points. If Tensor, it must be 0-dimensional

  • end (float or Tensor) – the ending value for the set of points. If Tensor, it must be 0-dimensional

  • steps (int) – size of the constructed tensor

  • base (float, optional) – base of the logarithm function. Default: 10.0.

Keyword Arguments:
  • out (Tensor, optional) – the output tensor.

  • dtype (tensorplay.dtype, optional) – the data type to perform the computation in. Default: if None, uses the global default dtype (see tensorplay.get_default_dtype()) when both start and end are real, and corresponding complex dtype when either is complex.

  • layout (tensorplay.layout, optional) – the desired layout of returned Tensor. Default: tensorplay.strided.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

>>> tensorplay.logspace(start=-10, end=10, steps=5)
tensor([ 1.0000e-10,  1.0000e-05,  1.0000e+00,  1.0000e+05,  1.0000e+10])
>>> tensorplay.logspace(start=0.1, end=1.0, steps=5)
tensor([  1.2589,   2.1135,   3.5481,   5.9566,  10.0000])
>>> tensorplay.logspace(start=0.1, end=1.0, steps=1)
tensor([1.2589])
>>> tensorplay.logspace(start=2, end=2, steps=1, base=2)
tensor([4.0])
#

logsumexp

functionFull reference ↗
tensorplay.functional.logsumexp(input, dim, keepdim=False, *, out=None)[source]

Returns the log of summed exponentials of each row of the input tensor in the given dimension dim. The computation is numerically stabilized.

For summation index jj given by dim and other indices ii, the result is

logsumexp(x)i=log⁡∑jexp⁡(xij)\text{logsumexp}(x)_{i} = \log \sum_j \exp(x_{ij})

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints) –

    the dimension or dimensions to reduce.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(3, 3)
>>> tensorplay.logsumexp(a, 1)
tensor([1.4907, 1.0593, 1.5696])
>>> tensorplay.dist(tensorplay.logsumexp(a, 1), tensorplay.log(tensorplay.sum(tensorplay.exp(a), 1)))
tensor(1.6859e-07)
#

lstm

functionFull reference ↗
tensorplay.functional.lstm(input, hx, params, has_biases=True, num_layers=1, dropout_p=0.0, training=False, bidirectional=False, batch_first=False)[source]
#

lt

functionFull reference ↗
tensorplay.functional.lt(input, other, *, out=None) → Tensor[source]

Computes input<other\text{input} < \text{other} element-wise.

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:
  • input (Tensor) – the tensor to compare

  • other (Tensor or float) – the tensor or value to compare

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Returns:

A boolean tensor that is True where input is less than other and False elsewhere

Example:

>>> tensorplay.lt(tensorplay.tensor([[1, 2], [3, 4]]), tensorplay.tensor([[1, 1], [4, 4]]))
tensor([[False, False], [True, False]])
#

lu_solve

functionFull reference ↗
tensorplay.functional.lu_solve(b, LU_data, LU_pivots, *, out=None) → Tensor[source]

Returns the LU solve of the linear system Ax=bAx = b using the partially pivoted LU factorization of A from lu_factor().

This function supports float, double, cfloat and cdouble dtypes for input.

Warning

tensorplay.lu_solve() is deprecated in favor of tensorplay.linalg.lu_solve(). X = tensorplay.lu_solve(B, LU, pivots) should be replaced with

X = linalg.lu_solve(LU, pivots, B)
Parameters:
  • b (Tensor) – the RHS tensor of size (∗,m,k)(*, m, k), where ∗* is zero or more batch dimensions.

  • LU_data (Tensor) – the pivoted LU factorization of A from lu_factor() of size (∗,m,m)(*, m, m), where ∗* is zero or more batch dimensions.

  • LU_pivots (IntTensor) – the pivots of the LU factorization from lu_factor() of size (∗,m)(*, m), where ∗* is zero or more batch dimensions. The batch dimensions of LU_pivots must be equal to the batch dimensions of LU_data.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> A = tensorplay.randn(2, 3, 3)
>>> b = tensorplay.randn(2, 3, 1)
>>> LU, pivots = tensorplay.linalg.lu_factor(A)
>>> x = tensorplay.lu_solve(b, LU, pivots)
>>> tensorplay.dist(A @ x, b)
tensor(1.00000e-07 *
       2.8312)
#

lu_unpack

functionFull reference ↗
tensorplay.functional.lu_unpack(LU_data, LU_pivots, unpack_data=True, unpack_pivots=True, *, out=None)[source]

Unpacks the LU decomposition returned by lu_factor() into the P, L, U matrices.

See also

lu() returns the matrices from the LU decomposition. Its gradient formula is more efficient than that of doing lu_factor() followed by lu_unpack().

Parameters:
  • LU_data (Tensor) – the packed LU factorization data

  • LU_pivots (Tensor) – the packed LU factorization pivots

  • unpack_data (bool) – flag indicating if the data should be unpacked. If False, then the returned L and U are empty tensors. Default: True

  • unpack_pivots (bool) – flag indicating if the pivots should be unpacked into a permutation matrix P. If False, then the returned P is an empty tensor. Default: True

Keyword Arguments:

out (tuple, optional) – output tuple of three tensors. Ignored if None.

Returns:

A namedtuple (P, L, U)

Examples:

>>> A = tensorplay.randn(2, 3, 3)
>>> LU, pivots = tensorplay.linalg.lu_factor(A)
>>> P, L, U = tensorplay.lu_unpack(LU, pivots)
>>> # We can recover A from the factorization
>>> A_ = P @ L @ U
>>> tensorplay.allclose(A, A_)
True

>>> # LU factorization of a rectangular matrix:
>>> A = tensorplay.randn(2, 3, 2)
>>> LU, pivots = tensorplay.linalg.lu_factor(A)
>>> P, L, U = tensorplay.lu_unpack(LU, pivots)
>>> # P, L, U are the same as returned by linalg.lu
>>> P_, L_, U_ = tensorplay.linalg.lu(A)
>>> tensorplay.allclose(P, P_) and tensorplay.allclose(L, L_) and tensorplay.allclose(U, U_)
True
#

masked_select

functionFull reference ↗
tensorplay.functional.masked_select(input, mask, *, out=None) → Tensor[source]

Returns a new 1-D tensor which indexes the input tensor according to the boolean mask mask which is a BoolTensor.

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable.

Note

The returned tensor does not use the same storage as the original tensor

Parameters:
  • input (Tensor) – the input tensor.

  • mask (BoolTensor) – the tensor containing the binary mask to index with

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> x = tensorplay.randn(3, 4)
>>> x
tensor([[ 0.3552, -2.3825, -0.8297,  0.3477],
        [-1.2035,  1.2252,  0.5002,  0.6248],
        [ 0.1307, -2.0608,  0.1244,  2.0139]])
>>> mask = x.ge(0.5)
>>> mask
tensor([[False, False, False, False],
        [False, True, True, True],
        [False, False, False, True]])
>>> tensorplay.masked_select(x, mask)
tensor([ 1.2252,  0.5002,  0.6248,  2.0139])
#

matmul

functionFull reference ↗
tensorplay.functional.matmul(input, other, *, out=None) → Tensor[source]

Matrix product of two tensors.

The behavior depends on the dimensionality of the tensors as follows:

  • If both tensors are 1-dimensional, the dot product (scalar) is returned.

  • If both arguments are 2-dimensional, the matrix-matrix product is returned.

  • If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed.

  • If the first argument is 2-dimensional and the second argument is 1-dimensional, the matrix-vector product is returned.

  • If both arguments are at least 1-dimensional and at least one argument is N-dimensional (where N > 2), then a batched matrix multiply is returned. If the first argument is 1-dimensional, a 1 is prepended to its dimension for the purpose of the batched matrix multiply and removed after. If the second argument is 1-dimensional, a 1 is appended to its dimension for the purpose of the batched matrix multiply and removed after.

    The first N-2 dimensions of each argument, the batch dimensions, are broadcast (and thus must be broadcastable). The last 2, the matrix dimensions, are handled as in the matrix-matrix product.

    For example, if input is a (j×1×n×m)(j \times 1 \times n \times m) tensor and other is a (k×m×p)(k \times m \times p) tensor, the batch dimensions are (j×1)(j \times 1) and (k)(k), and the matrix dimensions are (n×m)(n \times m) and (m×p)(m \times p). out will be a (j×k×n×p)(j \times k \times n \times p) tensor.

This operation has support for arguments with sparse layouts. In particular the matrix-matrix (both arguments 2-dimensional) supports sparse arguments with the same restrictions as tensorplay.mm()

Warning

Sparse support is a beta feature and some layout(s)/dtype/device combinations may not be supported, or may not have autograd support. If you notice missing functionality please open a feature request.

This operator supports TensorFloat32.

On certain ROCm devices, when using float16 inputs this module will use different precision for backward.

Note

The 1-dimensional dot product version of this function does not support an out parameter.

Parameters:
  • input (Tensor) – the first tensor to be multiplied

  • other (Tensor) – the second tensor to be multiplied

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> # vector x vector
>>> tensor1 = tensorplay.randn(3)
>>> tensor2 = tensorplay.randn(3)
>>> tensorplay.matmul(tensor1, tensor2).size()
tensorplay.Size([])
>>> # matrix x vector
>>> tensor1 = tensorplay.randn(3, 4)
>>> tensor2 = tensorplay.randn(4)
>>> tensorplay.matmul(tensor1, tensor2).size()
tensorplay.Size([3])
>>> # batched matrix x broadcasted vector
>>> tensor1 = tensorplay.randn(10, 3, 4)
>>> tensor2 = tensorplay.randn(4)
>>> tensorplay.matmul(tensor1, tensor2).size()
tensorplay.Size([10, 3])
>>> # batched matrix x batched matrix
>>> tensor1 = tensorplay.randn(10, 3, 4)
>>> tensor2 = tensorplay.randn(10, 4, 5)
>>> tensorplay.matmul(tensor1, tensor2).size()
tensorplay.Size([10, 3, 5])
>>> # batched matrix x broadcasted matrix
>>> tensor1 = tensorplay.randn(10, 3, 4)
>>> tensor2 = tensorplay.randn(4, 5)
>>> tensorplay.matmul(tensor1, tensor2).size()
tensorplay.Size([10, 3, 5])
#

max_pool1d

functionFull reference ↗
tensorplay.functional.max_pool1d(input, kernel_size, stride=[], padding=0, dilation=1, ceil_mode=False)[source]
#

max_pool3d

functionFull reference ↗
tensorplay.functional.max_pool3d(input, kernel_size, stride=[], padding=[0, 0, 0], dilation=[1, 1, 1], ceil_mode=False)[source]
#

maximum

functionFull reference ↗
tensorplay.functional.maximum(input, other, *, out=None) → Tensor[source]

Computes the element-wise maximum of input and other.

Note

If one of the elements being compared is a NaN, then that element is returned. maximum() is not supported for tensors with complex dtypes.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor((1, 2, -1))
>>> b = tensorplay.tensor((3, 0, 4))
>>> tensorplay.maximum(a, b)
tensor([3, 2, 4])
#

mean

functionFull reference ↗
tensorplay.functional.mean(input, *, dtype=None) → Tensor[source]

Note

If the input tensor is empty, tensorplay.mean() returns nan. This behavior is consistent with NumPy and follows the definition that the mean over an empty set is undefined.

Returns the mean value of all elements in the input tensor. Input must be floating point or complex.

Parameters:

input (Tensor) – the input tensor, either of floating point or complex dtype

Keyword Arguments:

dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

Example:

>>> a = tensorplay.randn(1, 3)
>>> a
tensor([[ 0.2294, -0.5481,  1.3288]])
>>> tensorplay.mean(a)
tensor(0.3367)
tensorplay.functional.mean(input, dim, keepdim=False, *, dtype=None, out=None) → Tensor[source]

Returns the mean value of each row of the input tensor in the given dimension dim. If dim is a list of dimensions, reduce over all of them.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) –

    the dimension or dimensions to reduce. If None, all dimensions are reduced.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

  • out (Tensor, optional) – the output tensor.

See also

tensorplay.nanmean() computes the mean value of non-NaN elements.

Example:

>>> a = tensorplay.randn(4, 4)
>>> a
tensor([[-0.3841,  0.6320,  0.4254, -0.7384],
        [-0.9644,  1.0131, -0.6549, -1.4279],
        [-0.2951, -1.3350, -0.7694,  0.5600],
        [ 1.0842, -0.9580,  0.3623,  0.2343]])
>>> tensorplay.mean(a, 1)
tensor([-0.0163, -0.5085, -0.4599,  0.1807])
>>> tensorplay.mean(a, 1, True)
tensor([[-0.0163],
        [-0.5085],
        [-0.4599],
        [ 0.1807]])
#

minimum

functionFull reference ↗
tensorplay.functional.minimum(input, other, *, out=None) → Tensor[source]

Computes the element-wise minimum of input and other.

Note

If one of the elements being compared is a NaN, then that element is returned. minimum() is not supported for tensors with complex dtypes.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor) – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor((1, 2, -1))
>>> b = tensorplay.tensor((3, 0, 4))
>>> tensorplay.minimum(a, b)
tensor([1, 0, -1])
#

miopen_batch_norm

functionFull reference ↗
tensorplay.functional.miopen_batch_norm(input, weight, bias, running_mean, running_var, training, exponential_average_factor, epsilon)[source]
#

miopen_convolution_add_relu

functionFull reference ↗
tensorplay.functional.miopen_convolution_add_relu(input, weight, z, alpha, bias, stride, padding, dilation, groups)[source]
#

miopen_convolution_relu

functionFull reference ↗
tensorplay.functional.miopen_convolution_relu(input, weight, bias, stride, padding, dilation, groups)[source]
#

miopen_convolution_transpose

functionFull reference ↗
tensorplay.functional.miopen_convolution_transpose(input, weight, bias, padding, output_padding, stride, dilation, groups, benchmark, deterministic)[source]
#

miopen_convolution

functionFull reference ↗
tensorplay.functional.miopen_convolution(input, weight, bias, padding, stride, dilation, groups, benchmark, deterministic)[source]
#

miopen_ctc_loss

functionFull reference ↗
tensorplay.functional.miopen_ctc_loss(log_probs, targets, input_lengths, target_lengths, blank, deterministic, zero_infinity)[source]
#

miopen_depthwise_convolution

functionFull reference ↗
tensorplay.functional.miopen_depthwise_convolution(input, weight, bias, padding, stride, dilation, groups, benchmark, deterministic)[source]
#

miopen_rnn

functionFull reference ↗
tensorplay.functional.miopen_rnn(input, weight, weight_stride0, hx, cx, mode, hidden_size, num_layers, batch_first, dropout, train, bidirectional, batch_sizes, dropout_state)[source]
#

mkldnn_adaptive_avg_pool2d

functionFull reference ↗
tensorplay.functional.mkldnn_adaptive_avg_pool2d(input, output_size, *, out=None)[source]
#

mkldnn_convolution

functionFull reference ↗
tensorplay.functional.mkldnn_convolution(input, weight, bias, padding, stride, dilation, groups)[source]
#

mkldnn_linear_backward_weights

functionFull reference ↗
tensorplay.functional.mkldnn_linear_backward_weights(grad_output, input, weight, bias_defined)[source]
#

mkldnn_max_pool2d

functionFull reference ↗
tensorplay.functional.mkldnn_max_pool2d(input, kernel_size, stride=[], padding=0, dilation=1, ceil_mode=False)[source]
#

mkldnn_max_pool3d

functionFull reference ↗
tensorplay.functional.mkldnn_max_pool3d(input, kernel_size, stride=[], padding=0, dilation=1, ceil_mode=False)[source]
#

mkldnn_rnn_layer

functionFull reference ↗
tensorplay.functional.mkldnn_rnn_layer(input, weight0, weight1, weight2, weight3, hx_, cx_, reverse, batch_sizes, mode, hidden_size, num_layers, has_biases, bidirectional, batch_first, train)[source]
#

mm

functionFull reference ↗
tensorplay.functional.mm(input, mat2, out_dtype=None, *, out=None) → Tensor[source]

Performs a matrix multiplication of the matrices input and mat2.

If input is a (n×m)(n \times m) tensor, mat2 is a (m×p)(m \times p) tensor, out will be a (n×p)(n \times p) tensor.

Note

This function does not broadcast. For broadcasting matrix products, see tensorplay.matmul().

Supports strided and sparse 2-D tensors as inputs, autograd with respect to strided inputs.

This operation has support for arguments with sparse layouts. If out is provided its layout will be used. Otherwise, the result layout will be deduced from that of input.

Warning

Sparse support is a beta feature and some layout(s)/dtype/device combinations may not be supported, or may not have autograd support. If you notice missing functionality please open a feature request.

This operator supports TensorFloat32.

On certain ROCm devices, when using float16 inputs this module will use different precision for backward.

Parameters:
  • input (Tensor) – the first matrix to be matrix multiplied

  • mat2 (Tensor) – the second matrix to be matrix multiplied

  • out_dtype (dtype, optional) – the dtype of the output tensor, Supported only on CUDA and for tensorplay.float32 given tensorplay.float16/tensorplay.bfloat16 input dtypes

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> mat1 = tensorplay.randn(2, 3)
>>> mat2 = tensorplay.randn(3, 3)
>>> tensorplay.mm(mat1, mat2)
tensor([[ 0.4851,  0.5037, -0.3633],
        [-0.0760, -3.6705,  2.4784]])
#

moveaxis

functionFull reference ↗
tensorplay.functional.moveaxis(input, source, destination) → Tensor[source]

Alias for tensorplay.movedim().

This function is equivalent to NumPy’s moveaxis function.

Examples:

>>> t = tensorplay.randn(3,2,1)
>>> t
tensor([[[-0.3362],
        [-0.8437]],

        [[-0.9627],
        [ 0.1727]],

        [[ 0.5173],
        [-0.1398]]])
>>> tensorplay.moveaxis(t, 1, 0).shape
tensorplay.Size([2, 3, 1])
>>> tensorplay.moveaxis(t, 1, 0)
tensor([[[-0.3362],
        [-0.9627],
        [ 0.5173]],

        [[-0.8437],
        [ 0.1727],
        [-0.1398]]])
>>> tensorplay.moveaxis(t, (1, 2), (0, 1)).shape
tensorplay.Size([2, 1, 3])
>>> tensorplay.moveaxis(t, (1, 2), (0, 1))
tensor([[[-0.3362, -0.9627,  0.5173]],

        [[-0.8437,  0.1727, -0.1398]]])
#

movedim

functionFull reference ↗
tensorplay.functional.movedim(input, source, destination) → Tensor[source]

Moves the dimension(s) of input at the position(s) in source to the position(s) in destination.

Other dimensions of input that are not explicitly moved remain in their original order and appear at the positions not specified in destination.

Parameters:
  • input (Tensor) – the input tensor.

  • source (int or tuple of ints) – Original positions of the dims to move. These must be unique.

  • destination (int or tuple of ints) – Destination positions for each of the original dims. These must also be unique.

Examples:

>>> t = tensorplay.randn(3,2,1)
>>> t
tensor([[[-0.3362],
        [-0.8437]],

        [[-0.9627],
        [ 0.1727]],

        [[ 0.5173],
        [-0.1398]]])
>>> tensorplay.movedim(t, 1, 0).shape
tensorplay.Size([2, 3, 1])
>>> tensorplay.movedim(t, 1, 0)
tensor([[[-0.3362],
        [-0.9627],
        [ 0.5173]],

        [[-0.8437],
        [ 0.1727],
        [-0.1398]]])
>>> tensorplay.movedim(t, (1, 2), (0, 1)).shape
tensorplay.Size([2, 1, 3])
>>> tensorplay.movedim(t, (1, 2), (0, 1))
tensor([[[-0.3362, -0.9627,  0.5173]],

        [[-0.8437,  0.1727, -0.1398]]])
#

msort

functionFull reference ↗
tensorplay.functional.msort(input: Tensor, *, out: Tensor | None) → Tensor[source]

Sorts the elements of the input tensor along its first dimension in ascending order by value.

Note

tensorplay.msort(t) is equivalent to tensorplay.sort(t, dim=0)[0]. See also tensorplay.sort().

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> t = tensorplay.randn(3, 4)
>>> t
tensor([[-0.1321,  0.4370, -1.2631, -1.1289],
        [-2.0527, -1.1250,  0.2275,  0.3077],
        [-0.0881, -0.1259, -0.5495,  1.0284]])
>>> tensorplay.msort(t)
tensor([[-2.0527, -1.1250, -1.2631, -1.1289],
        [-0.1321, -0.1259, -0.5495,  0.3077],
        [-0.0881,  0.4370,  0.2275,  1.0284]])
#

mul

functionFull reference ↗
tensorplay.functional.mul(input, other, *, out=None) → Tensor[source]

Multiplies input by other.

outi=inputi×otheri\text{out}_i = \text{input}_i \times \text{other}_i

Supports broadcasting to a common shape, type promotion, and integer, float, and complex inputs.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor or Number) – the tensor or number to multiply input by.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Examples:

>>> a = tensorplay.randn(3)
>>> a
tensor([ 0.2015, -0.4255,  2.6087])
>>> tensorplay.mul(a, 100)
tensor([  20.1494,  -42.5491,  260.8663])

>>> b = tensorplay.randn(4, 1)
>>> b
tensor([[ 1.1207],
        [-0.3137],
        [ 0.0700],
        [ 0.8378]])
>>> c = tensorplay.randn(1, 4)
>>> c
tensor([[ 0.5146,  0.1216, -0.5244,  2.2382]])
>>> tensorplay.mul(b, c)
tensor([[ 0.5767,  0.1363, -0.5877,  2.5083],
        [-0.1614, -0.0382,  0.1645, -0.7021],
        [ 0.0360,  0.0085, -0.0367,  0.1567],
        [ 0.4312,  0.1019, -0.4394,  1.8753]])
#

multinomial

functionFull reference ↗
tensorplay.functional.multinomial(input, num_samples, replacement=False, *, generator=None, out=None) → LongTensor[source]

Returns a tensor where each row contains num_samples indices sampled from the multinomial (a stricter definition would be multivariate, refer to tensorplay.distributions.multinomial.Multinomial for more details) probability distribution located in the corresponding row of tensor input.

Note

The rows of input do not need to sum to one (in which case we use the values as weights), but must be non-negative, finite and have a non-zero sum.

Indices are ordered from left to right according to when each was sampled (first samples are placed in first column).

If input is a vector, out is a vector of size num_samples.

If input is a matrix with m rows, out is an matrix of shape (m×num_samples)(m \times \text{num\_samples}).

If replacement is True, samples are drawn with replacement.

If not, they are drawn without replacement, which means that when a sample index is drawn for a row, it cannot be drawn again for that row.

Note

When drawn without replacement, num_samples must be lower than number of non-zero elements in input (or the min number of non-zero elements in each row of input if it is a matrix).

Parameters:
  • input (Tensor) – the input tensor containing probabilities

  • num_samples (int) – number of samples to draw

  • replacement (bool, optional) – whether to draw with replacement or not

Keyword Arguments:
  • generator (tensorplay.Generator, optional) – a pseudorandom number generator for sampling

  • out (Tensor, optional) – the output tensor.

Example:

>>> weights = tensorplay.tensor([0, 10, 3, 0], dtype=tensorplay.float) # create a tensor of weights
>>> tensorplay.multinomial(weights, 2)
tensor([1, 2])
>>> tensorplay.multinomial(weights, 5) # ERROR!
RuntimeError: cannot sample n_sample > prob_dist.size(-1) samples without replacement
>>> tensorplay.multinomial(weights, 4, replacement=True)
tensor([ 2,  1,  1,  1])
#

mv

functionFull reference ↗
tensorplay.functional.mv(input, vec, *, out=None) → Tensor[source]

Performs a matrix-vector product of the matrix input and the vector vec.

If input is a (n×m)(n \times m) tensor, vec is a 1-D tensor of size mm, out will be 1-D of size nn.

Note

This function does not broadcast.

Parameters:
  • input (Tensor) – matrix to be multiplied

  • vec (Tensor) – vector to be multiplied

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> mat = tensorplay.randn(2, 3)
>>> vec = tensorplay.randn(3)
>>> tensorplay.mv(mat, vec)
tensor([ 1.0404, -0.6361])
#

nan_to_num

functionFull reference ↗
tensorplay.functional.nan_to_num(input, nan=0.0, posinf=None, neginf=None, *, out=None) → Tensor[source]

Replaces NaN, positive infinity, and negative infinity values in input with the values specified by nan, posinf, and neginf, respectively. By default, NaNs are replaced with zero, positive infinity is replaced with the greatest finite value representable by input’s dtype, and negative infinity is replaced with the least finite value representable by input’s dtype.

Parameters:
  • input (Tensor) – the input tensor.

  • nan (Number, optional) – the value to replace NaNs with. Default is zero.

  • posinf (Number, optional) – if a Number, the value to replace positive infinity values with. If None, positive infinity values are replaced with the greatest finite value representable by input’s dtype. Default is None.

  • neginf (Number, optional) – if a Number, the value to replace negative infinity values with. If None, negative infinity values are replaced with the lowest finite value representable by input’s dtype. Default is None.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> x = tensorplay.tensor([float('nan'), float('inf'), -float('inf'), 3.14])
>>> tensorplay.nan_to_num(x)
tensor([ 0.0000e+00,  3.4028e+38, -3.4028e+38,  3.1400e+00])
>>> tensorplay.nan_to_num(x, nan=2.0)
tensor([ 2.0000e+00,  3.4028e+38, -3.4028e+38,  3.1400e+00])
>>> tensorplay.nan_to_num(x, nan=2.0, posinf=1.0)
tensor([ 2.0000e+00,  1.0000e+00, -3.4028e+38,  3.1400e+00])
#

nanmean

functionFull reference ↗
tensorplay.functional.nanmean(input, dim=None, keepdim=False, *, dtype=None, out=None) → Tensor[source]

Computes the mean of all non-NaN elements along the specified dimensions. Input must be floating point or complex.

This function is identical to tensorplay.mean() when there are no NaN values in the input tensor. In the presence of NaN, tensorplay.mean() will propagate the NaN to the output whereas tensorplay.nanmean() will ignore the NaN values (tensorplay.nanmean(a) is equivalent to tensorplay.mean(a[~a.isnan()])).

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor, either of floating point or complex dtype

  • dim (int or tuple of ints, optional) –

    the dimension or dimensions to reduce. If None, all dimensions are reduced.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

  • out (Tensor, optional) – the output tensor.

See also

tensorplay.mean() computes the mean value, propagating NaN.

Example:

>>> x = tensorplay.tensor([[tensorplay.nan, 1, 2], [1, 2, 3]])
>>> x.mean()
tensor(nan)
>>> x.nanmean()
tensor(1.8000)
>>> x.mean(dim=0)
tensor([   nan, 1.5000, 2.5000])
>>> x.nanmean(dim=0)
tensor([1.0000, 1.5000, 2.5000])

# If all elements in the reduced dimensions are NaN then the result is NaN
>>> tensorplay.tensor([tensorplay.nan]).nanmean()
tensor(nan)
#

nansum

functionFull reference ↗
tensorplay.functional.nansum(input, *, dtype=None) → Tensor[source]

Returns the sum of all elements, treating Not a Numbers (NaNs) as zero.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

Example:

>>> a = tensorplay.tensor([1., 2., float('nan'), 4.])
>>> tensorplay.nansum(a)
tensor(7.)
tensorplay.functional.nansum(input, dim, keepdim=False, *, dtype=None) → Tensor[source]

Returns the sum of each row of the input tensor in the given dimension dim, treating Not a Numbers (NaNs) as zero. If dim is a list of dimensions, reduce over all of them.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) –

    the dimension or dimensions to reduce. If None, all dimensions are reduced.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

Keyword Arguments:

dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

Example:

>>> tensorplay.nansum(tensorplay.tensor([1., float("nan")]))
tensor(1.)
>>> a = tensorplay.tensor([[1, 2], [3., float("nan")]])
>>> tensorplay.nansum(a)
tensor(6.)
>>> tensorplay.nansum(a, dim=0)
tensor([4., 2.])
>>> tensorplay.nansum(a, dim=1)
tensor([3., 3.])
#

narrow_copy

functionFull reference ↗
tensorplay.functional.narrow_copy(input, dim, start, length, *, out=None) → Tensor[source]

Same as Tensor.narrow() except this returns a copy rather than shared storage. This is primarily for sparse tensors, which do not have a shared-storage narrow method.

Parameters:
  • input (Tensor) – the tensor to narrow

  • dim (int) – the dimension along which to narrow

  • start (int) – index of the element to start the narrowed dimension from. Can be negative, which means indexing from the end of dim

  • length (int) – length of the narrowed dimension, must be weakly positive

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> x = tensorplay.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> tensorplay.narrow_copy(x, 0, 0, 2)
tensor([[ 1,  2,  3],
        [ 4,  5,  6]])
>>> tensorplay.narrow_copy(x, 1, 1, 2)
tensor([[ 2,  3],
        [ 5,  6],
        [ 8,  9]])
>>> s = tensorplay.arange(16).reshape(2, 2, 2, 2).to_sparse(2)
>>> tensorplay.narrow_copy(s, 0, 0, 1)
tensor(indices=tensor([[0, 0],
                       [0, 1]]),
       values=tensor([[[0, 1],
                       [2, 3]],

                      [[4, 5],
                       [6, 7]]]),
       size=(1, 2, 2, 2), nnz=2, layout=tensorplay.sparse_coo)

See also

tensorplay.narrow() for a non copy variant

#

narrow

functionFull reference ↗
tensorplay.functional.narrow(input, dim, start, length) → Tensor[source]

Returns a new tensor that is a narrowed version of input tensor. The dimension dim is input from start to start + length. The returned tensor and input tensor share the same underlying storage.

Parameters:
  • input (Tensor) – the tensor to narrow

  • dim (int) – the dimension along which to narrow

  • start (int or Tensor) – index of the element to start the narrowed dimension from. Can be negative, which means indexing from the end of dim. If Tensor, it must be an 0-dim integral Tensor (bools not allowed)

  • length (int) – length of the narrowed dimension, must be weakly positive

Example:

>>> x = tensorplay.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> tensorplay.narrow(x, 0, 0, 2)
tensor([[ 1,  2,  3],
        [ 4,  5,  6]])
>>> tensorplay.narrow(x, 1, 1, 2)
tensor([[ 2,  3],
        [ 5,  6],
        [ 8,  9]])
>>> tensorplay.narrow(x, -1, tensorplay.tensor(-1), 1)
tensor([[3],
        [6],
        [9]])
#

native_batch_norm

functionFull reference ↗
tensorplay.functional.native_batch_norm(input, weight, bias, running_mean, running_var, training, momentum, eps, *, out=None)[source]
#

native_group_norm

functionFull reference ↗
tensorplay.functional.native_group_norm(input, weight, bias, N, C, HxW, group, eps)[source]
#

native_layer_norm

functionFull reference ↗
tensorplay.functional.native_layer_norm(input, normalized_shape, weight, bias, eps)[source]
#

ne

functionFull reference ↗
tensorplay.functional.ne(input, other, *, out=None) → Tensor[source]

Computes input≠other\text{input} \neq \text{other} element-wise.

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters:
  • input (Tensor) – the tensor to compare

  • other (Tensor or float) – the tensor or value to compare

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Returns:

A boolean tensor that is True where input is not equal to other and False elsewhere

Example:

>>> tensorplay.ne(tensorplay.tensor([[1, 2], [3, 4]]), tensorplay.tensor([[1, 1], [4, 4]]))
tensor([[False, True], [True, False]])
#

neg

functionFull reference ↗
tensorplay.functional.neg(input, *, out=None) → Tensor[source]

Returns a new tensor with the negative of the elements of input.

out=−1×input\text{out} = -1 \times \text{input}
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(5)
>>> a
tensor([ 0.0090, -0.2262, -0.0682, -0.2866,  0.3940])
>>> tensorplay.neg(a)
tensor([-0.0090,  0.2262,  0.0682,  0.2866, -0.3940])
#

nextafter

functionFull reference ↗
tensorplay.functional.nextafter(input, other, *, out=None) → Tensor[source]

Return the next floating-point value after input towards other, elementwise.

The shapes of input and other must be broadcastable.

Parameters:
  • input (Tensor) – the first input tensor

  • other (Tensor) – the second input tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> eps = tensorplay.finfo(tensorplay.float32).eps
>>> tensorplay.nextafter(tensorplay.tensor([1.0, 2.0]), tensorplay.tensor([2.0, 1.0])) == tensorplay.tensor([eps + 1, 2 - eps])
tensor([True, True])
#

nonzero

functionFull reference ↗
tensorplay.functional.nonzero(input, *, out=None, as_tuple=False) → LongTensor or tuple of LongTensors[source]

Note

tensorplay.nonzero(..., as_tuple=False) (default) returns a 2-D tensor where each row is the index for a nonzero value.

tensorplay.nonzero(..., as_tuple=True) returns a tuple of 1-D index tensors, allowing for advanced indexing, so x[x.nonzero(as_tuple=True)] gives all nonzero values of tensor x. Of the returned tuple, each index tensor contains nonzero indices for a certain dimension.

See below for more details on the two behaviors.

When input is on CUDA, tensorplay.nonzero() causes host-device synchronization.

When as_tuple is False (default):

Returns a tensor containing the indices of all non-zero elements of input. Each row in the result contains the indices of a non-zero element in input. The result is sorted lexicographically, with the last index changing the fastest (C-style).

If input has nn dimensions, then the resulting indices tensor out is of size (z×n)(z \times n), where zz is the total number of non-zero elements in the input tensor.

When as_tuple is True:

Returns a tuple of 1-D tensors, one for each dimension in input, each containing the indices (in that dimension) of all non-zero elements of input .

If input has nn dimensions, then the resulting tuple contains nn tensors of size zz, where zz is the total number of non-zero elements in the input tensor.

As a special case, when input has zero dimensions and a nonzero scalar value, it is treated as a one-dimensional tensor with one element.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (LongTensor, optional) – the output tensor containing indices

Returns:

If as_tuple is False, the output tensor containing indices. If as_tuple is True, one 1-D tensor for each dimension, containing the indices of each nonzero element along that dimension.

Return type:

LongTensor or tuple of LongTensor

Example:

>>> tensorplay.nonzero(tensorplay.tensor([1, 1, 1, 0, 1]))
tensor([[ 0],
        [ 1],
        [ 2],
        [ 4]])
>>> tensorplay.nonzero(tensorplay.tensor([[0.6, 0.0, 0.0, 0.0],
...                             [0.0, 0.4, 0.0, 0.0],
...                             [0.0, 0.0, 1.2, 0.0],
...                             [0.0, 0.0, 0.0,-0.4]]))
tensor([[ 0,  0],
        [ 1,  1],
        [ 2,  2],
        [ 3,  3]])
>>> tensorplay.nonzero(tensorplay.tensor([1, 1, 1, 0, 1]), as_tuple=True)
(tensor([0, 1, 2, 4]),)
>>> tensorplay.nonzero(tensorplay.tensor([[0.6, 0.0, 0.0, 0.0],
...                             [0.0, 0.4, 0.0, 0.0],
...                             [0.0, 0.0, 1.2, 0.0],
...                             [0.0, 0.0, 0.0,-0.4]]), as_tuple=True)
(tensor([0, 1, 2, 3]), tensor([0, 1, 2, 3]))
>>> tensorplay.nonzero(tensorplay.tensor(5), as_tuple=True)
(tensor([0]),)
#

normal

functionFull reference ↗
tensorplay.functional.normal(mean, std, *, generator=None, out=None) → Tensor[source]

Returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given.

The mean is a tensor with the mean of each output element’s normal distribution

The std is a tensor with the standard deviation of each output element’s normal distribution

The shapes of mean and std don’t need to match, but the total number of elements in each tensor need to be the same.

Note

When the shapes do not match, the shape of mean is used as the shape for the returned output tensor

Note

When std is a CUDA tensor, this function synchronizes its device with the CPU.

Parameters:
  • mean (Tensor) – the tensor of per-element means

  • std (Tensor) – the tensor of per-element standard deviations

Keyword Arguments:
  • generator (tensorplay.Generator, optional) – a pseudorandom number generator for sampling

  • out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.normal(mean=tensorplay.arange(1., 11.), std=tensorplay.arange(1, 0, -0.1))
tensor([  1.0425,   3.5672,   2.7969,   4.2925,   4.7229,   6.2134,
          8.0505,   8.1408,   9.0563,  10.0566])
tensorplay.functional.normal(mean=0.0, std, *, out=None) → Tensor[source]

Similar to the function above, but the means are shared among all drawn elements.

Parameters:
  • mean (float, optional) – the mean for all distributions

  • std (Tensor) – the tensor of per-element standard deviations

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.normal(mean=0.5, std=tensorplay.arange(1., 6.))
tensor([-1.2793, -1.0732, -2.0687,  5.1177, -1.2303])
tensorplay.functional.normal(mean, std=1.0, *, out=None) → Tensor[source]

Similar to the function above, but the standard deviations are shared among all drawn elements.

Parameters:
  • mean (Tensor) – the tensor of per-element means

  • std (float, optional) – the standard deviation for all distributions

Keyword Arguments:

out (Tensor, optional) – the output tensor

Example:

>>> tensorplay.normal(mean=tensorplay.arange(1., 6.))
tensor([ 1.1552,  2.6148,  2.6535,  5.8318,  4.2361])
tensorplay.functional.normal(mean, std, size, *, out=None) → Tensor[source]

Similar to the function above, but the means and standard deviations are shared among all drawn elements. The resulting tensor has size given by size.

Parameters:
  • mean (float) – the mean for all distributions

  • std (float) – the standard deviation for all distributions

  • size (int...) – a sequence of integers defining the shape of the output tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.normal(2, 3, size=(1, 4))
tensor([[-1.3987, -1.9544,  3.6048,  0.7909]])
#

numel

functionFull reference ↗
tensorplay.functional.numel(input: Tensor) → int[source]

Returns the total number of elements in the input tensor.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> a = tensorplay.randn(1, 2, 3, 4, 5)
>>> tensorplay.numel(a)
120
>>> a = tensorplay.zeros(4,4)
>>> tensorplay.numel(a)
16
#

ones_like

functionFull reference ↗
tensorplay.functional.ones_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=tensorplay.preserve_format) → Tensor

Returns a tensor filled with the scalar value 1, with the same size as input. tensorplay.ones_like(input) is equivalent to tensorplay.ones(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).

Warning

As of 0.4, this function does not support an out keyword. As an alternative, the old tensorplay.ones_like(input, out=output) is equivalent to tensorplay.ones(input.size(), out=output).

Parameters:

input (Tensor) – the size of input will determine size of the output tensor.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.

  • layout (tensorplay.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • memory_format (tensorplay.memory_format, optional) – the desired memory format of returned Tensor. Default: tensorplay.preserve_format.

Example:

>>> input = tensorplay.empty(2, 3)
>>> tensorplay.ones_like(input)
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])
#

ormqr

functionFull reference ↗
tensorplay.functional.ormqr(input, tau, other, left=True, transpose=False, *, out=None) → Tensor[source]

Computes the matrix-matrix multiplication of a product of Householder matrices with a general matrix.

Multiplies a m×nm \times n matrix C (given by other) with a matrix Q, where Q is represented using Householder reflectors (input, tau). See Representation of Orthogonal or Unitary Matrices for further details.

If left is True then op(Q) times C is computed, otherwise the result is C times op(Q). When left is True, the implicit matrix Q has size m×mm \times m. It has size n×nn \times n otherwise. If transpose is True then op is the conjugate transpose operation, otherwise it’s a no-op.

Supports inputs of float, double, cfloat and cdouble dtypes. Also supports batched inputs, and, if the input is batched, the output is batched with the same dimensions.

See also

tensorplay.geqrf() can be used to form the Householder representation (input, tau) of matrix Q from the QR decomposition.

Note

This function supports backward but it is only fast when (input, tau) do not require gradients and/or tau.size(-1) is very small. ``

Parameters:
  • input (Tensor) – tensor of shape (*, mn, k) where * is zero or more batch dimensions and mn equals to m or n depending on the left.

  • tau (Tensor) – tensor of shape (*, min(mn, k)) where * is zero or more batch dimensions.

  • other (Tensor) – tensor of shape (*, m, n) where * is zero or more batch dimensions.

  • left (bool) – controls the order of multiplication.

  • transpose (bool) – controls whether the matrix Q is conjugate transposed or not.

Keyword Arguments:

out (Tensor, optional) – the output Tensor. Ignored if None. Default: None.

#

outer

functionFull reference ↗
tensorplay.functional.outer(input, vec2, *, out=None) → Tensor[source]

Outer product of input and vec2. If input is a vector of size nn and vec2 is a vector of size mm, then out must be a matrix of size (n×m)(n \times m).

Note

This function does not broadcast.

Parameters:
  • input (Tensor) – 1-D input vector

  • vec2 (Tensor) – 1-D input vector

Keyword Arguments:

out (Tensor, optional) – optional output matrix

Example:

>>> v1 = tensorplay.arange(1., 5.)
>>> v2 = tensorplay.arange(1., 4.)
>>> tensorplay.outer(v1, v2)
tensor([[  1.,   2.,   3.],
        [  2.,   4.,   6.],
        [  3.,   6.,   9.],
        [  4.,   8.,  12.]])
#

permute_copy

functionFull reference ↗
tensorplay.functional.permute_copy(input, dims)[source]

Performs the same operation as tensorplay.permute(), but all output tensors are freshly created instead of aliasing the input.

#

permute

functionFull reference ↗
tensorplay.functional.permute(input, dims) → Tensor[source]

Returns a view of the original tensor input with its dimensions permuted.

Parameters:
  • input (Tensor) – the input tensor.

  • dims (tuple of int) – The desired ordering of dimensions

Example

>>> x = tensorplay.randn(2, 3, 5)
>>> x.size()
tensorplay.Size([2, 3, 5])
>>> tensorplay.permute(x, (2, 0, 1)).size()
tensorplay.Size([5, 2, 3])
#

poisson_nll_loss

functionFull reference ↗
tensorplay.functional.poisson_nll_loss(input, target, log_input=True, full=False, eps=1e-08)[source]
#

poisson

functionFull reference ↗
tensorplay.functional.poisson(input, generator=None) → Tensor[source]

Returns a tensor of the same size as input with each element sampled from a Poisson distribution with rate parameter given by the corresponding element in input i.e.,

outi∼Poisson(inputi)\text{out}_i \sim \text{Poisson}(\text{input}_i)

input must be non-negative.

Parameters:

input (Tensor) – the input tensor containing the rates of the Poisson distribution

Keyword Arguments:

generator (tensorplay.Generator, optional) – a pseudorandom number generator for sampling

Example:

>>> rates = tensorplay.rand(4, 4) * 5  # rate parameter between 0 and 5
>>> tensorplay.poisson(rates)
tensor([[9., 1., 3., 5.],
        [8., 6., 6., 0.],
        [0., 4., 5., 3.],
        [2., 1., 4., 2.]])
#

polar

functionFull reference ↗
tensorplay.functional.polar(abs, angle, *, out=None) → Tensor[source]

Constructs a complex tensor whose elements are Cartesian coordinates corresponding to the polar coordinates with absolute value abs and angle angle.

out=abs⋅cos⁡(angle)+abs⋅sin⁡(angle)⋅j\text{out} = \text{abs} \cdot \cos(\text{angle}) + \text{abs} \cdot \sin(\text{angle}) \cdot j

Note

tensorplay.polar is similar to std::polar and does not compute the polar decomposition of a complex tensor like Python’s cmath.polar and SciPy’s linalg.polar do. The behavior of this function is undefined if abs is negative or NaN, or if angle is infinite.

Parameters:
  • abs (Tensor) – The absolute value the complex tensor. Must be float or double.

  • angle (Tensor) – The angle of the complex tensor. Must be same dtype as abs.

Keyword Arguments:

out (Tensor) – If the inputs are tensorplay.float32, must be tensorplay.complex64. If the inputs are tensorplay.float64, must be tensorplay.complex128.

Example:

>>> import numpy as np
>>> abs = tensorplay.tensor([1, 2], dtype=tensorplay.float64)
>>> angle = tensorplay.tensor([np.pi / 2, 5 * np.pi / 4], dtype=tensorplay.float64)
>>> z = tensorplay.polar(abs, angle)
>>> z
tensor([(0.0000+1.0000j), (-1.4142-1.4142j)], dtype=tensorplay.complex128)
#

positive

functionFull reference ↗
tensorplay.functional.positive(input) → Tensor[source]

Returns input. Throws a runtime error if input is a bool tensor.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> t = tensorplay.randn(5)
>>> t
tensor([ 0.0090, -0.2262, -0.0682, -0.2866,  0.3940])
>>> tensorplay.positive(t)
tensor([ 0.0090, -0.2262, -0.0682, -0.2866,  0.3940])
#

pow

functionFull reference ↗
tensorplay.functional.pow(input, exponent, *, out=None) → Tensor[source]

Takes the power of each element in input with exponent and returns a tensor with the result.

exponent can be either a single float number or a Tensor with the same number of elements as input.

When exponent is a scalar value, the operation applied is:

outi=xiexponent\text{out}_i = x_i ^ \text{exponent}

When exponent is a tensor, the operation applied is:

outi=xiexponenti\text{out}_i = x_i ^ {\text{exponent}_i}

When exponent is a tensor, the shapes of input and exponent must be broadcastable.

Parameters:
  • input (Tensor) – the input tensor.

  • exponent (float or tensor) – the exponent value

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([ 0.4331,  1.2475,  0.6834, -0.2791])
>>> tensorplay.pow(a, 2)
tensor([ 0.1875,  1.5561,  0.4670,  0.0779])
>>> exp = tensorplay.arange(1., 5.)

>>> a = tensorplay.arange(1., 5.)
>>> a
tensor([ 1.,  2.,  3.,  4.])
>>> exp
tensor([ 1.,  2.,  3.,  4.])
>>> tensorplay.pow(a, exp)
tensor([   1.,    4.,   27.,  256.])
tensorplay.functional.pow(self, exponent, *, out=None) → Tensor[source]

self is a scalar float value, and exponent is a tensor. The returned tensor out is of the same shape as exponent

The operation applied is:

outi=selfexponenti\text{out}_i = \text{self} ^ {\text{exponent}_i}
Parameters:
  • self (float) – the scalar base value for the power operation

  • exponent (Tensor) – the exponent tensor

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> exp = tensorplay.arange(1., 5.)
>>> base = 2
>>> tensorplay.pow(base, exp)
tensor([  2.,   4.,   8.,  16.])
#

prod

functionFull reference ↗
tensorplay.functional.prod(input: Tensor, *, dtype: _dtype | None) → Tensor[source]

Returns the product of all elements in the input tensor.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

Example:

>>> a = tensorplay.randn(1, 3)
>>> a
tensor([[-0.8020,  0.5428, -1.5854]])
>>> tensorplay.prod(a)
tensor(0.6902)
tensorplay.functional.prod(input, dim, keepdim=False, *, dtype=None) → Tensor[source]

Returns the product of each row of the input tensor in the given dimension dim.

If keepdim is True, the output tensor is of the same size as input except in the dimension dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 fewer dimension than input.

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int, optional) –

    the dimension to reduce. If None, all dimensions are reduced.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

Keyword Arguments:

dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

Example:

>>> a = tensorplay.randn(4, 2)
>>> a
tensor([[ 0.5261, -0.3837],
        [ 1.1857, -0.2498],
        [-1.1646,  0.0705],
        [ 1.1131, -1.0629]])
>>> tensorplay.prod(a, 1)
tensor([-0.2018, -0.2962, -0.0821, -1.1831])
#

promote_types

functionFull reference ↗
tensorplay.functional.promote_types(type1, type2) → dtype[source]

Returns the tensorplay.dtype with the smallest size and scalar kind that is not smaller nor of lower kind than either type1 or type2. See type promotion documentation for more information on the type promotion logic.

Parameters:

Example:

>>> tensorplay.promote_types(tensorplay.int32, tensorplay.float32)
tensorplay.float32
>>> tensorplay.promote_types(tensorplay.uint8, tensorplay.long)
tensorplay.long
#

quantize_per_channel

functionFull reference ↗
tensorplay.functional.quantize_per_channel(input, scales, zero_points, axis, dtype) → Tensor[source]

Converts a float tensor to a per-channel quantized tensor with given scales and zero points.

Parameters:
  • input (Tensor) – float tensor to quantize

  • scales (Tensor) – float 1D tensor of scales to use, size should match input.size(axis)

  • zero_points (int) – integer 1D tensor of offset to use, size should match input.size(axis)

  • axis (int) – dimension on which apply per-channel quantization

  • dtype (tensorplay.dtype) – the desired data type of returned tensor. Has to be one of the quantized dtypes: tensorplay.quint8, tensorplay.qint8, tensorplay.qint32

Returns:

A newly quantized tensor

Return type:

Tensor

Example:

>>> x = tensorplay.tensor([[-1.0, 0.0], [1.0, 2.0]])
>>> tensorplay.quantize_per_channel(x, tensorplay.tensor([0.1, 0.01]), tensorplay.tensor([10, 0]), 0, tensorplay.quint8)
tensor([[-1.,  0.],
        [ 1.,  2.]], size=(2, 2), dtype=tensorplay.quint8,
       quantization_scheme=tensorplay.per_channel_affine,
       scale=tensor([0.1000, 0.0100], dtype=tensorplay.float64),
       zero_point=tensor([10,  0]), axis=0)
>>> tensorplay.quantize_per_channel(x, tensorplay.tensor([0.1, 0.01]), tensorplay.tensor([10, 0]), 0, tensorplay.quint8).int_repr()
tensor([[  0,  10],
        [100, 200]], dtype=tensorplay.uint8)
#

quantize_per_tensor_dynamic

functionFull reference ↗
tensorplay.functional.quantize_per_tensor_dynamic(input, dtype, reduce_range) → Tensor[source]

Converts a float tensor to a quantized tensor with scale and zero_point calculated dynamically based on the input.

Parameters:
  • input (Tensor) – float tensor or list of tensors to quantize

  • dtype (tensorplay.dtype) – the desired data type of returned tensor. Has to be one of the quantized dtypes: tensorplay.quint8, tensorplay.qint8

  • reduce_range (bool) – a flag to indicate whether to reduce the range of quantized

  • bit (data by 1)

  • hardwares (it's required to avoid instruction overflow for some)

Returns:

A newly (dynamically) quantized tensor

Return type:

Tensor

Example:

>>> t = tensorplay.quantize_per_tensor_dynamic(tensorplay.tensor([-1.0, 0.0, 1.0, 2.0]), tensorplay.quint8, False)
>>> print(t)
tensor([-1.,  0.,  1.,  2.], size=(4,), dtype=tensorplay.quint8,
       quantization_scheme=tensorplay.per_tensor_affine, scale=0.011764705882352941,
       zero_point=85)
>>> t.int_repr()
tensor([  0,  85, 170, 255], dtype=tensorplay.uint8)
#

quantize_per_tensor

functionFull reference ↗
tensorplay.functional.quantize_per_tensor(input, scale, zero_point, dtype) → Tensor[source]

Converts a float tensor to a quantized tensor with given scale and zero point.

Parameters:
  • input (Tensor) – float tensor or list of tensors to quantize

  • scale (float or Tensor) – scale to apply in quantization formula

  • zero_point (int or Tensor) – offset in integer value that maps to float zero

  • dtype (tensorplay.dtype) – the desired data type of returned tensor. Has to be one of the quantized dtypes: tensorplay.quint8, tensorplay.qint8, tensorplay.qint32

Returns:

A newly quantized tensor or list of quantized tensors.

Return type:

Tensor

Example:

>>> tensorplay.quantize_per_tensor(tensorplay.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, tensorplay.quint8)
tensor([-1.,  0.,  1.,  2.], size=(4,), dtype=tensorplay.quint8,
       quantization_scheme=tensorplay.per_tensor_affine, scale=0.1, zero_point=10)
>>> tensorplay.quantize_per_tensor(tensorplay.tensor([-1.0, 0.0, 1.0, 2.0]), 0.1, 10, tensorplay.quint8).int_repr()
tensor([ 0, 10, 20, 30], dtype=tensorplay.uint8)
>>> tensorplay.quantize_per_tensor([tensorplay.tensor([-1.0, 0.0]), tensorplay.tensor([-2.0, 2.0])],
>>> tensorplay.tensor([0.1, 0.2]), tensorplay.tensor([10, 20]), tensorplay.quint8)
(tensor([-1.,  0.], size=(2,), dtype=tensorplay.quint8,
    quantization_scheme=tensorplay.per_tensor_affine, scale=0.1, zero_point=10),
    tensor([-2.,  2.], size=(2,), dtype=tensorplay.quint8,
    quantization_scheme=tensorplay.per_tensor_affine, scale=0.2, zero_point=20))
>>> tensorplay.quantize_per_tensor(tensorplay.tensor([-1.0, 0.0, 1.0, 2.0]), tensorplay.tensor(0.1), tensorplay.tensor(10), tensorplay.quint8)
tensor([-1.,  0.,  1.,  2.], size=(4,), dtype=tensorplay.quint8,
   quantization_scheme=tensorplay.per_tensor_affine, scale=0.10, zero_point=10)
#

quantized_batch_norm

functionFull reference ↗
tensorplay.functional.quantized_batch_norm(input, weight=None, bias=None, mean, var, eps, output_scale, output_zero_point) → Tensor[source]

Applies batch normalization on a 4D (NCHW) quantized tensor.

y=x−E[x]Var[x]+ϵ∗γ+βy = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta
Parameters:
  • input (Tensor) – quantized tensor

  • weight (Tensor) – float tensor that corresponds to the gamma, size C

  • bias (Tensor) – float tensor that corresponds to the beta, size C

  • mean (Tensor) – float mean value in batch normalization, size C

  • var (Tensor) – float tensor for variance, size C

  • eps (float) – a value added to the denominator for numerical stability.

  • output_scale (float) – output quantized tensor scale

  • output_zero_point (int) – output quantized tensor zero_point

Returns:

A quantized tensor with batch normalization applied.

Return type:

Tensor

Example:

>>> qx = tensorplay.quantize_per_tensor(tensorplay.rand(2, 2, 2, 2), 1.5, 3, tensorplay.quint8)
>>> tensorplay.quantized_batch_norm(qx, tensorplay.ones(2), tensorplay.zeros(2), tensorplay.rand(2), tensorplay.rand(2), 0.00001, 0.2, 2)
tensor([[[[-0.2000, -0.2000],
      [ 1.6000, -0.2000]],

     [[-0.4000, -0.4000],
      [-0.4000,  0.6000]]],


    [[[-0.2000, -0.2000],
      [-0.2000, -0.2000]],

     [[ 0.6000, -0.4000],
      [ 0.6000, -0.4000]]]], size=(2, 2, 2, 2), dtype=tensorplay.quint8,
   quantization_scheme=tensorplay.per_tensor_affine, scale=0.2, zero_point=2)
#

quantized_gru_cell

functionFull reference ↗
tensorplay.functional.quantized_gru_cell(input, hx, w_ih, w_hh, b_ih, b_hh, packed_ih, packed_hh, col_offsets_ih, col_offsets_hh, scale_ih, scale_hh, zero_point_ih, zero_point_hh)[source]
#

quantized_lstm_cell

functionFull reference ↗
tensorplay.functional.quantized_lstm_cell(input, hx, w_ih, w_hh, b_ih, b_hh, packed_ih, packed_hh, col_offsets_ih, col_offsets_hh, scale_ih, scale_hh, zero_point_ih, zero_point_hh)[source]
#

quantized_max_pool1d

functionFull reference ↗
tensorplay.functional.quantized_max_pool1d(input, kernel_size, stride=[], padding=0, dilation=1, ceil_mode=False) → Tensor[source]

Applies a 1D max pooling over an input quantized tensor composed of several input planes.

Parameters:
  • input (Tensor) – quantized tensor

  • kernel_size (list of int) – the size of the sliding window

  • stride (list of int, optional) – the stride of the sliding window

  • padding (list of int, optional) – padding to be added on both sides, must be >= 0 and <= kernel_size / 2

  • dilation (list of int, optional) – The stride between elements within a sliding window, must be > 0. Default 1

  • ceil_mode (bool, optional) – If True, will use ceil instead of floor to compute the output shape. Defaults to False.

Returns:

A quantized tensor with max_pool1d applied.

Return type:

Tensor

Example:

>>> qx = tensorplay.quantize_per_tensor(tensorplay.rand(2, 2), 1.5, 3, tensorplay.quint8)
>>> tensorplay.quantized_max_pool1d(qx, [2])
tensor([[0.0000],
        [1.5000]], size=(2, 1), dtype=tensorplay.quint8,
    quantization_scheme=tensorplay.per_tensor_affine, scale=1.5, zero_point=3)
#

quantized_max_pool2d

functionFull reference ↗
tensorplay.functional.quantized_max_pool2d(input, kernel_size, stride=[], padding=0, dilation=1, ceil_mode=False) → Tensor[source]

Applies a 2D max pooling over an input quantized tensor composed of several input planes.

Parameters:
  • input (Tensor) – quantized tensor

  • kernel_size (list of int) – the size of the sliding window

  • stride (list of int, optional) – the stride of the sliding window

  • padding (list of int, optional) – padding to be added on both sides, must be >= 0 and <= kernel_size / 2

  • dilation (list of int, optional) – The stride between elements within a sliding window, must be > 0. Default 1

  • ceil_mode (bool, optional) – If True, will use ceil instead of floor to compute the output shape. Defaults to False.

Returns:

A quantized tensor with max_pool2d applied.

Return type:

Tensor

Example:

>>> qx = tensorplay.quantize_per_tensor(tensorplay.rand(2, 2, 2, 2), 1.5, 3, tensorplay.quint8)
>>> tensorplay.quantized_max_pool2d(qx, [2,2])
tensor([[[[1.5000]],

        [[1.5000]]],


        [[[0.0000]],

        [[0.0000]]]], size=(2, 2, 1, 1), dtype=tensorplay.quint8,
    quantization_scheme=tensorplay.per_tensor_affine, scale=1.5, zero_point=3)
#

quantized_max_pool3d

functionFull reference ↗
tensorplay.functional.quantized_max_pool3d(input, kernel_size, stride=[], padding=0, dilation=1, ceil_mode=False)[source]
#

quantized_rnn_relu_cell

functionFull reference ↗
tensorplay.functional.quantized_rnn_relu_cell(input, hx, w_ih, w_hh, b_ih, b_hh, packed_ih, packed_hh, col_offsets_ih, col_offsets_hh, scale_ih, scale_hh, zero_point_ih, zero_point_hh)[source]
#

quantized_rnn_tanh_cell

functionFull reference ↗
tensorplay.functional.quantized_rnn_tanh_cell(input, hx, w_ih, w_hh, b_ih, b_hh, packed_ih, packed_hh, col_offsets_ih, col_offsets_hh, scale_ih, scale_hh, zero_point_ih, zero_point_hh)[source]
#

rad2deg

functionFull reference ↗
tensorplay.functional.rad2deg(input: Tensor, *, out: Tensor | None) → Tensor[source]

Returns a new tensor with each of the elements of input converted from angles in radians to degrees.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([[3.142, -3.142], [6.283, -6.283], [1.570, -1.570]])
>>> tensorplay.rad2deg(a)
tensor([[ 180.0233, -180.0233],
        [ 359.9894, -359.9894],
        [  89.9544,  -89.9544]])
#

rand_like

functionFull reference ↗
tensorplay.functional.rand_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=tensorplay.preserve_format) → Tensor[source]

Returns a tensor with the same size as input that is filled with random numbers from a uniform distribution on the interval [0,1)[0, 1). tensorplay.rand_like(input) is equivalent to tensorplay.rand(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).

Parameters:

input (Tensor) – the size of input will determine size of the output tensor.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.

  • layout (tensorplay.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • memory_format (tensorplay.memory_format, optional) – the desired memory format of returned Tensor. Default: tensorplay.preserve_format.

#

randint_like

functionFull reference ↗
tensorplay.functional.randint_like(input, low=0, high, \*, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False, memory_format=tensorplay.preserve_format) → Tensor[source]

Returns a tensor with the same shape as Tensor input filled with random integers generated uniformly between low (inclusive) and high (exclusive).

Parameters:
  • input (Tensor) – the size of input will determine size of the output tensor.

  • low (int, optional) – Lowest integer to be drawn from the distribution. Default: 0.

  • high (int) – One above the highest integer to be drawn from the distribution.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.

  • layout (tensorplay.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • memory_format (tensorplay.memory_format, optional) – the desired memory format of returned Tensor. Default: tensorplay.preserve_format.

#

randint

functionFull reference ↗
tensorplay.functional.randint(low=0, high, size, \*, generator=None, out=None, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False) → Tensor[source]

Returns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive).

The shape of the tensor is defined by the variable argument size.

Note

With the global dtype default (tensorplay.float32), this function returns a tensor with dtype tensorplay.int64.

Parameters:
  • low (int, optional) – Lowest integer to be drawn from the distribution. Default: 0.

  • high (int) – One above the highest integer to be drawn from the distribution.

  • size (tuple) – a tuple defining the shape of the output tensor.

Keyword Arguments:
  • generator (tensorplay.Generator, optional) – a pseudorandom number generator for sampling

  • out (Tensor, optional) – the output tensor.

  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, this function returns a tensor with dtype tensorplay.int64.

  • layout (tensorplay.layout, optional) – the desired layout of returned Tensor. Default: tensorplay.strided.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

>>> tensorplay.randint(3, 5, (3,))
tensor([4, 3, 4])


>>> tensorplay.randint(10, (2, 2))
tensor([[0, 2],
        [5, 5]])


>>> tensorplay.randint(3, 10, (2, 2))
tensor([[4, 5],
        [6, 7]])
#

randn_like

functionFull reference ↗
tensorplay.functional.randn_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=tensorplay.preserve_format) → Tensor[source]

Returns a tensor with the same size as input that is filled with random numbers from a normal distribution with mean 0 and variance 1. Please refer to tensorplay.randn() for the sampling process of complex dtypes. tensorplay.randn_like(input) is equivalent to tensorplay.randn(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).

Parameters:

input (Tensor) – the size of input will determine size of the output tensor.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.

  • layout (tensorplay.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • memory_format (tensorplay.memory_format, optional) – the desired memory format of returned Tensor. Default: tensorplay.preserve_format.

#

randperm

functionFull reference ↗
tensorplay.functional.randperm(n, *, generator=None, out=None, dtype=tensorplay.int64, layout=tensorplay.strided, device=None, requires_grad=False, pin_memory=False) → Tensor[source]

Returns a random permutation of integers from 0 to n - 1.

Parameters:

n (int) – the upper bound (exclusive)

Keyword Arguments:
  • generator (tensorplay.Generator, optional) – a pseudorandom number generator for sampling

  • out (Tensor, optional) – the output tensor.

  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: tensorplay.int64.

  • layout (tensorplay.layout, optional) – the desired layout of returned Tensor. Default: tensorplay.strided.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

Example:

>>> tensorplay.randperm(4)
tensor([2, 1, 0, 3])
#

range

functionFull reference ↗
tensorplay.functional.range(start=0, end, step=1, *, out=None, dtype=None, layout=tensorplay.strided, device=None, requires_grad=False) → Tensor[source]

Returns a 1-D tensor of size ⌊end−startstep⌋+1\left\lfloor \frac{\text{end} - \text{start}}{\text{step}} \right\rfloor + 1 with values from start to end with step step. Step is the gap between two values in the tensor.

outi+1=outi+step.\text{out}_{i+1} = \text{out}_i + \text{step}.

Warning

This function is deprecated and will be removed in a future release because its behavior is inconsistent with Python’s range builtin. Instead, use tensorplay.arange(), which produces values in [start, end).

Parameters:
  • start (float, optional) – the starting value for the set of points. Default: 0.

  • end (float) – the ending value for the set of points

  • step (float, optional) – the gap between each pair of adjacent points. Default: 1.

Keyword Arguments:
  • out (Tensor, optional) – the output tensor.

  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see tensorplay.set_default_dtype()). If dtype is not given, infer the data type from the other input arguments. If any of start, end, or step are floating-point, the dtype is inferred to be the default dtype, see get_default_dtype(). Otherwise, the dtype is inferred to be tensorplay.int64.

  • layout (tensorplay.layout, optional) – the desired layout of returned Tensor. Default: tensorplay.strided.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

>>> tensorplay.range(1, 4)
tensor([ 1.,  2.,  3.,  4.])
>>> tensorplay.range(1, 4, 0.5)
tensor([ 1.0000,  1.5000,  2.0000,  2.5000,  3.0000,  3.5000,  4.0000])
#

ravel

functionFull reference ↗
tensorplay.functional.ravel(input) → Tensor[source]

Return a contiguous flattened tensor. A copy is made only if needed.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> t = tensorplay.tensor([[[1, 2],
...                    [3, 4]],
...                   [[5, 6],
...                    [7, 8]]])
>>> tensorplay.ravel(t)
tensor([1, 2, 3, 4, 5, 6, 7, 8])
#

real

functionFull reference ↗
tensorplay.functional.real(input) → Tensor[source]

Returns a new tensor containing real values of the self tensor. The returned tensor and self share the same underlying storage.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> x=tensorplay.randn(4, dtype=tensorplay.cfloat)
>>> x
tensor([(0.3100+0.3553j), (-0.5445-0.7896j), (-1.6492-0.0633j), (-0.0638-0.8119j)])
>>> x.real
tensor([ 0.3100, -0.5445, -1.6492, -0.0638])
#

reciprocal

functionFull reference ↗
tensorplay.functional.reciprocal(input, *, out=None) → Tensor[source]

Returns a new tensor with the reciprocal of the elements of input

outi=1inputi\text{out}_{i} = \frac{1}{\text{input}_{i}}

Note

Unlike NumPy’s reciprocal, tensorplay.reciprocal supports integral inputs. Integral inputs to reciprocal are automatically promoted to the default scalar type.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([-0.4595, -2.1219, -1.4314,  0.7298])
>>> tensorplay.reciprocal(a)
tensor([-2.1763, -0.4713, -0.6986,  1.3702])
#

remainder

functionFull reference ↗
tensorplay.functional.remainder(input, other, *, out=None) → Tensor[source]

Computes Python’s modulus operation entrywise. The result has the same sign as the divisor other and its absolute value is less than that of other.

It may also be defined in terms of tensorplay.div() as

tensorplay.remainder(a, b) == a - a.div(b, rounding_mode="floor") * b

Supports broadcasting to a common shape, type promotion, and integer and float inputs.

Note

Complex inputs are not supported. In some cases, it is not mathematically possible to satisfy the definition of a modulo operation with complex numbers. See tensorplay.fmod() for how division by zero is handled.

See also

tensorplay.fmod() which implements C++’s std::fmod. This one is defined in terms of division rounding towards zero.

Parameters:
Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.remainder(tensorplay.tensor([-3., -2, -1, 1, 2, 3]), 2)
tensor([ 1.,  0.,  1.,  1.,  0.,  1.])
>>> tensorplay.remainder(tensorplay.tensor([1, 2, 3, 4, 5]), -1.5)
tensor([ -0.5000, -1.0000,  0.0000, -0.5000, -1.0000 ])
#

renorm

functionFull reference ↗
tensorplay.functional.renorm(input, p, dim, maxnorm, *, out=None) → Tensor[source]

Returns a tensor where each sub-tensor of input along dimension dim is normalized such that the p-norm of the sub-tensor is lower than the value maxnorm

Note

If the norm of a row is lower than maxnorm, the row is unchanged

Parameters:
  • input (Tensor) – the input tensor.

  • p (float) – the power for the norm computation

  • dim (int) – the dimension to slice over to get the sub-tensors

  • maxnorm (float) – the maximum norm to keep each sub-tensor under

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> x = tensorplay.ones(3, 3)
>>> x[1].fill_(2)
tensor([ 2.,  2.,  2.])
>>> x[2].fill_(3)
tensor([ 3.,  3.,  3.])
>>> x
tensor([[ 1.,  1.,  1.],
        [ 2.,  2.,  2.],
        [ 3.,  3.,  3.]])
>>> tensorplay.renorm(x, 1, 0, 5)
tensor([[ 1.0000,  1.0000,  1.0000],
        [ 1.6667,  1.6667,  1.6667],
        [ 1.6667,  1.6667,  1.6667]])
#

repeat_interleave

functionFull reference ↗
tensorplay.functional.repeat_interleave(input, repeats, dim=None, *, output_size=None) → Tensor[source]

Repeat elements of a tensor.

Warning

This is different from tensorplay.Tensor.repeat() but similar to numpy.repeat.

Parameters:
  • input (Tensor) – the input tensor.

  • repeats (Tensor or int) – The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.

  • dim (int, optional) – The dimension along which to repeat values. By default, use the flattened input array, and return a flat output array.

Keyword Arguments:

output_size (int, optional) – Total output size for the given axis ( e.g. sum of repeats). If given, it will avoid stream synchronization needed to calculate output shape of the tensor.

Returns:

Repeated tensor which has the same shape as input, except along the given axis.

Return type:

Tensor

Example:

>>> x = tensorplay.tensor([1, 2, 3])
>>> x.repeat_interleave(2)
tensor([1, 1, 2, 2, 3, 3])
>>> y = tensorplay.tensor([[1, 2], [3, 4]])
>>> tensorplay.repeat_interleave(y, 2)
tensor([1, 1, 2, 2, 3, 3, 4, 4])
>>> tensorplay.repeat_interleave(y, 3, dim=1)
tensor([[1, 1, 1, 2, 2, 2],
        [3, 3, 3, 4, 4, 4]])
>>> tensorplay.repeat_interleave(y, tensorplay.tensor([1, 2]), dim=0)
tensor([[1, 2],
        [3, 4],
        [3, 4]])
>>> tensorplay.repeat_interleave(y, tensorplay.tensor([1, 2]), dim=0, output_size=3)
tensor([[1, 2],
        [3, 4],
        [3, 4]])

If the repeats is tensor([n1, n2, n3, …]), then the output will be tensor([0, 0, …, 1, 1, …, 2, 2, …, …]) where 0 appears n1 times, 1 appears n2 times, 2 appears n3 times, etc.

tensorplay.functional.repeat_interleave(repeats, *) → Tensor[source]

Repeats 0 repeats[0] times, 1 repeats[1] times, 2 repeats[2] times, etc.

Parameters:

repeats (Tensor) – The number of repetitions for each element.

Returns:

Repeated tensor of size sum(repeats).

Return type:

Tensor

Example:

>>> tensorplay.repeat_interleave(tensorplay.tensor([1, 2, 3]))
tensor([0, 1, 1, 2, 2, 2])
#

reshape

functionFull reference ↗
tensorplay.functional.reshape(input, shape) → Tensor[source]

Returns a tensor with the same data and number of elements as input, but with the specified shape. When possible, the returned tensor will be a view of input. Otherwise, it will be a copy. Contiguous inputs and inputs with compatible strides can be reshaped without copying, but you should not depend on the copying vs. viewing behavior.

See tensorplay.Tensor.view() on when it is possible to return a view.

A single dimension may be -1, in which case it’s inferred from the remaining dimensions and the number of elements in input.

Parameters:
  • input (Tensor) – the tensor to be reshaped

  • shape (tuple of int) – the new shape

Example:

>>> a = tensorplay.arange(4.)
>>> tensorplay.reshape(a, (2, 2))
tensor([[ 0.,  1.],
        [ 2.,  3.]])
>>> b = tensorplay.tensor([[0, 1], [2, 3]])
>>> tensorplay.reshape(b, (-1,))
tensor([ 0,  1,  2,  3])
#

resolve_conj

functionFull reference ↗
tensorplay.functional.resolve_conj(input) → Tensor[source]

Returns a new tensor with materialized conjugation if input’s conjugate bit is set to True, else returns input. The output tensor will always have its conjugate bit set to False.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> x = tensorplay.tensor([-1 + 1j, -2 + 2j, 3 - 3j])
>>> y = x.conj()
>>> y.is_conj()
True
>>> z = y.resolve_conj()
>>> z
tensor([-1 - 1j, -2 - 2j, 3 + 3j])
>>> z.is_conj()
False
#

resolve_neg

functionFull reference ↗
tensorplay.functional.resolve_neg(input) → Tensor[source]

Returns a new tensor with materialized negation if input’s negative bit is set to True, else returns input. The output tensor will always have its negative bit set to False.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> x = tensorplay.tensor([-1 + 1j, -2 + 2j, 3 - 3j])
>>> y = x.conj()
>>> z = y.imag
>>> z.is_neg()
True
>>> out = z.resolve_neg()
>>> out
tensor([-1., -2., 3.])
>>> out.is_neg()
False
#

result_type

functionFull reference ↗
tensorplay.functional.result_type(tensor1, tensor2) → dtype[source]

Returns the tensorplay.dtype that would result from performing an arithmetic operation on the provided input tensors. See type promotion documentation for more information on the type promotion logic.

Parameters:
  • tensor1 (Tensor or Number) – an input tensor or number

  • tensor2 (Tensor or Number) – an input tensor or number

Example:

>>> tensorplay.result_type(tensorplay.tensor([1, 2], dtype=tensorplay.int), 1.0)
tensorplay.float32
>>> tensorplay.result_type(tensorplay.tensor([1, 2], dtype=tensorplay.uint8), tensorplay.tensor(1))
tensorplay.uint8
#

rnn_relu

functionFull reference ↗
tensorplay.functional.rnn_relu(input, hx, params, has_biases=True, num_layers=1, dropout_p=0.0, training=False, bidirectional=False, batch_first=False)[source]
#

rnn_tanh

functionFull reference ↗
tensorplay.functional.rnn_tanh(input, hx, params, has_biases=True, num_layers=1, dropout_p=0.0, training=False, bidirectional=False, batch_first=False)[source]
#

roll

functionFull reference ↗
tensorplay.functional.roll(input, shifts, dims=None) → Tensor[source]

Roll the tensor input along the given dimension(s). Elements that are shifted beyond the last position are re-introduced at the first position. If dims is None, the tensor will be flattened before rolling and then restored to the original shape.

Parameters:
  • input (Tensor) – the input tensor.

  • shifts (int or tuple of ints) – The number of places by which the elements of the tensor are shifted. If shifts is a tuple, dims must be a tuple of the same size, and each dimension will be rolled by the corresponding value

  • dims (int or tuple of ints) – Axis along which to roll

Example:

>>> x = tensorplay.tensor([1, 2, 3, 4, 5, 6, 7, 8]).view(4, 2)
>>> x
tensor([[1, 2],
        [3, 4],
        [5, 6],
        [7, 8]])
>>> tensorplay.roll(x, 1)
tensor([[8, 1],
        [2, 3],
        [4, 5],
        [6, 7]])
>>> tensorplay.roll(x, 1, 0)
tensor([[7, 8],
        [1, 2],
        [3, 4],
        [5, 6]])
>>> tensorplay.roll(x, -1, 0)
tensor([[3, 4],
        [5, 6],
        [7, 8],
        [1, 2]])
>>> tensorplay.roll(x, shifts=(2, 1), dims=(0, 1))
tensor([[6, 5],
        [8, 7],
        [2, 1],
        [4, 3]])
#

rot90

functionFull reference ↗
tensorplay.functional.rot90(input, k=1, dims=(0, 1)) → Tensor[source]

Rotate an n-D tensor by 90 degrees in the plane specified by dims axis. Rotation direction is from the first towards the second axis if k > 0, and from the second towards the first for k < 0.

Parameters:
  • input (Tensor) – the input tensor.

  • k (int) – number of times to rotate. Default value is 1

  • dims (a list or tuple) – axis to rotate. Default value is [0, 1]

Example:

>>> x = tensorplay.arange(4).view(2, 2)
>>> x
tensor([[0, 1],
        [2, 3]])
>>> tensorplay.rot90(x, 1, [0, 1])
tensor([[1, 3],
        [0, 2]])

>>> x = tensorplay.arange(8).view(2, 2, 2)
>>> x
tensor([[[0, 1],
         [2, 3]],

        [[4, 5],
         [6, 7]]])
>>> tensorplay.rot90(x, 1, [1, 2])
tensor([[[1, 3],
         [0, 2]],

        [[5, 7],
         [4, 6]]])
#

round

functionFull reference ↗
tensorplay.functional.round(input, *, decimals=0, out=None) → Tensor[source]

Rounds elements of input to the nearest integer.

For integer inputs, follows the array-api convention of returning a copy of the input tensor. The return type of output is same as that of input’s dtype.

Note

This function implements the “round half to even” to break ties when a number is equidistant from two integers (e.g. round(2.5) is 2).

When the :attr:`decimals` argument is specified the algorithm used is similar to NumPy’s around. This algorithm is fast but inexact and it can easily overflow for low precision dtypes. Eg. round(tensor([10000], dtype=tensorplay.float16), decimals=3) is inf.

See also

tensorplay.ceil(), which rounds up. tensorplay.floor(), which rounds down. tensorplay.trunc(), which rounds towards zero.

Parameters:
  • input (Tensor) – the input tensor.

  • decimals (int) – Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> tensorplay.round(tensorplay.tensor((4.7, -2.3, 9.1, -7.7)))
tensor([ 5.,  -2.,  9., -8.])

>>> # Values equidistant from two integers are rounded towards the
>>> #   the nearest even value (zero is treated as even)
>>> tensorplay.round(tensorplay.tensor([-0.5, 0.5, 1.5, 2.5]))
tensor([-0., 0., 2., 2.])

>>> # A positive decimals argument rounds to the to that decimal place
>>> tensorplay.round(tensorplay.tensor([0.1234567]), decimals=3)
tensor([0.1230])

>>> # A negative decimals argument rounds to the left of the decimal
>>> tensorplay.round(tensorplay.tensor([1200.1234567]), decimals=-3)
tensor([1000.])
#

rrelu_

functionFull reference ↗
tensorplay.functional.rrelu_(input, lower=0.125, upper=0.3333333333333333, training=False, generator=None)[source]
#

rrelu

functionFull reference ↗
tensorplay.functional.rrelu(input, lower=0.125, upper=0.3333333333333333, training=False, generator=None)[source]
#

rsqrt

functionFull reference ↗
tensorplay.functional.rsqrt(input, *, out=None) → Tensor[source]

Returns a new tensor with the reciprocal of the square-root of each of the elements of input.

outi=1inputi\text{out}_{i} = \frac{1}{\sqrt{\text{input}_{i}}}
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([-0.0370,  0.2970,  1.5420, -0.9105])
>>> tensorplay.rsqrt(a)
tensor([    nan,  1.8351,  0.8053,     nan])
#

scatter_add

functionFull reference ↗
tensorplay.functional.scatter_add(input, dim, index, src) → Tensor[source]

Out-of-place version of tensorplay.Tensor.scatter_add_()

#

scatter_reduce

functionFull reference ↗
tensorplay.functional.scatter_reduce(input, dim, index, src, reduce, *, include_self=True) → Tensor[source]

Out-of-place version of tensorplay.Tensor.scatter_reduce_()

#

searchsorted

functionFull reference ↗
tensorplay.functional.searchsorted(sorted_sequence, values, *, out_int32=False, right=False, side=None, out=None, sorter=None) → Tensor[source]

Find the indices from the innermost dimension of sorted_sequence such that, if the corresponding values in values were inserted before the indices, when sorted, the order of the corresponding innermost dimension within sorted_sequence would be preserved. Return a new tensor with the same size as values. More formally, the returned index satisfies the following rules:

sorted_sequence

right

returned index satisfies

1-D

False

sorted_sequence[i-1] < values[m][n]...[l][x] <= sorted_sequence[i]

1-D

True

sorted_sequence[i-1] <= values[m][n]...[l][x] < sorted_sequence[i]

N-D

False

sorted_sequence[m][n]...[l][i-1] < values[m][n]...[l][x] <= sorted_sequence[m][n]...[l][i]

N-D

True

sorted_sequence[m][n]...[l][i-1] <= values[m][n]...[l][x] < sorted_sequence[m][n]...[l][i]

Parameters:
  • sorted_sequence (Tensor) – N-D or 1-D tensor, containing monotonically increasing sequence on the innermost dimension unless sorter is provided, in which case the sequence does not need to be sorted

  • values (Tensor or Scalar) – N-D tensor or a Scalar containing the search value(s).

Keyword Arguments:
  • out_int32 (bool, optional) – indicate the output data type. tensorplay.int32 if True, tensorplay.int64 otherwise. Default value is False, i.e. default output data type is tensorplay.int64.

  • right (bool, optional) – if False, return the first suitable location that is found. If True, return the last such index. If no suitable index found, return 0 for non-numerical value (eg. nan, inf) or the size of innermost dimension within sorted_sequence (one pass the last index of the innermost dimension). In other words, if False, gets the lower bound index for each value in values on the corresponding innermost dimension of the sorted_sequence. If True, gets the upper bound index instead. Default value is False. side does the same and is preferred. It will error if side is set to “left” while this is True.

  • side (str, optional) – the same as right but preferred. “left” corresponds to False for right and “right” corresponds to True for right. It will error if this is set to “left” while right is True. Default value is None.

  • out (Tensor, optional) – the output tensor, must be the same size as values if provided.

  • sorter (LongTensor, optional) – if provided, a tensor matching the shape of the unsorted sorted_sequence containing a sequence of indices that sort it in the ascending order on the innermost dimension

Example:

>>> sorted_sequence = tensorplay.tensor([[1, 3, 5, 7, 9], [2, 4, 6, 8, 10]])
>>> sorted_sequence
tensor([[ 1,  3,  5,  7,  9],
        [ 2,  4,  6,  8, 10]])
>>> values = tensorplay.tensor([[3, 6, 9], [3, 6, 9]])
>>> values
tensor([[3, 6, 9],
        [3, 6, 9]])
>>> tensorplay.searchsorted(sorted_sequence, values)
tensor([[1, 3, 4],
        [1, 2, 4]])
>>> tensorplay.searchsorted(sorted_sequence, values, side='right')
tensor([[2, 3, 5],
        [1, 3, 4]])

>>> sorted_sequence_1d = tensorplay.tensor([1, 3, 5, 7, 9])
>>> sorted_sequence_1d
tensor([1, 3, 5, 7, 9])
>>> tensorplay.searchsorted(sorted_sequence_1d, values)
tensor([[1, 3, 4],
        [1, 3, 4]])
#

segment_reduce

functionFull reference ↗
tensorplay.functional.segment_reduce(data: Tensor, reduce: str, *, lengths: Tensor | None = None, indices: Tensor | None = None, offsets: Tensor | None = None, axis: _int = 0, unsafe: _bool = False, initial: Number | _complex | None = None) → Tensor[source]

Perform a segment reduction operation on the input tensor along the specified axis.

Parameters:
  • data (Tensor) – The input tensor on which the segment reduction operation will be performed.

  • reduce (str) – The type of reduction operation. Supported values are sum, mean, max, min, prod.

Keyword Arguments:
  • lengths (Tensor, optional) – Length of each segment. Default: None.

  • offsets (Tensor, optional) – Offset of each segment. Default: None.

  • axis (int, optional) – The axis perform reduction. Default: 0.

  • unsafe (bool, optional) – Skip validation If True. Default: False.

  • initial (Number, optional) – The initial value for the reduction operation. Default: None.

Example:

>>> data = tensorplay.tensor([[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]], dtype=tensorplay.float32, device='cuda')
>>> lengths = tensorplay.tensor([2, 1], device='cuda')
>>> tensorplay.segment_reduce(data, 'max', lengths=lengths)
tensor([[ 5.,  6.,  7.,  8.],
        [ 9., 10., 11., 12.]], device='cuda:0')
#

select_copy

functionFull reference ↗
tensorplay.functional.select_copy(input, dim, index)[source]

Performs the same operation as tensorplay.select(), but all output tensors are freshly created instead of aliasing the input.

#

select_scatter

functionFull reference ↗
tensorplay.functional.select_scatter(input, src, dim, index) → Tensor[source]

Embeds the values of the src tensor into input at the given index. This function returns a tensor with fresh storage; it does not create a view.

Parameters:
  • input (Tensor) – the input tensor.

  • src (Tensor) – The tensor to embed into input

  • dim (int) – the dimension to insert the slice into.

  • index (int) – the index to select with

Note

src must be of the proper size in order to be embedded into input. Specifically, it should have the same shape as tensorplay.select(input, dim, index)

Example:

>>> a = tensorplay.zeros(2, 2)
>>> b = tensorplay.ones(2)
>>> a.select_scatter(b, 0, 0)
tensor([[1., 1.],
        [0., 0.]])
#

select

functionFull reference ↗
tensorplay.functional.select(input, dim, index) → Tensor[source]

Slices the input tensor along the selected dimension at the given index. This function returns a view of the original tensor with the given dimension removed.

Note

If input is a sparse tensor and returning a view of the tensor is not possible, a RuntimeError exception is raised. In this is the case, consider using tensorplay.select_copy() function.

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int) – the dimension to slice

  • index (int) – the index to select with

Note

select() is equivalent to slicing. For example, tensor.select(0, index) is equivalent to tensor[index] and tensor.select(2, index) is equivalent to tensor[:,:,index].

#

sgn

functionFull reference ↗
tensorplay.functional.sgn(input, *, out=None) → Tensor[source]

This function is an extension of tensorplay.sign() to complex tensors. It computes a new tensor whose elements have the same angles as the corresponding elements of input and absolute values (i.e. magnitudes) of one for complex tensors and is equivalent to tensorplay.sign() for non-complex tensors.

outi={0∣inputi∣==0inputi∣inputi∣otherwise\text{out}_{i} = \begin{cases} 0 & |\text{{input}}_i| == 0 \\ \frac{{\text{{input}}_i}}{|{\text{{input}}_i}|} & \text{otherwise} \end{cases}
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> t = tensorplay.tensor([3+4j, 7-24j, 0, 1+2j])
>>> t.sgn()
tensor([0.6000+0.8000j, 0.2800-0.9600j, 0.0000+0.0000j, 0.4472+0.8944j])
#

sign

functionFull reference ↗
tensorplay.functional.sign(input, *, out=None) → Tensor[source]

Returns a new tensor with the signs of the elements of input.

outi=sgn⁡(inputi)\text{out}_{i} = \operatorname{sgn}(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([0.7, -1.2, 0., 2.3])
>>> a
tensor([ 0.7000, -1.2000,  0.0000,  2.3000])
>>> tensorplay.sign(a)
tensor([ 1., -1.,  0.,  1.])
#

signbit

functionFull reference ↗
tensorplay.functional.signbit(input, *, out=None) → Tensor[source]

Tests if each element of input has its sign bit set or not.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([0.7, -1.2, 0., 2.3])
>>> tensorplay.signbit(a)
tensor([ False, True,  False,  False])
>>> a = tensorplay.tensor([-0.0, 0.0])
>>> tensorplay.signbit(a)
tensor([ True,  False])

Note

signbit handles signed zeros, so negative zero (-0) returns True.

#

sin

functionFull reference ↗
tensorplay.functional.sin(input, *, out=None) → Tensor[source]

Returns a new tensor with the sine of the elements of input.

outi=sin⁡(inputi)\text{out}_{i} = \sin(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([-0.5461,  0.1347, -2.7266, -0.2746])
>>> tensorplay.sin(a)
tensor([-0.5194,  0.1343, -0.4032, -0.2711])
#

sinh

functionFull reference ↗
tensorplay.functional.sinh(input, *, out=None) → Tensor[source]

Returns a new tensor with the hyperbolic sine of the elements of input.

outi=sinh⁡(inputi)\text{out}_{i} = \sinh(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([ 0.5380, -0.8632, -0.1265,  0.9399])
>>> tensorplay.sinh(a)
tensor([ 0.5644, -0.9744, -0.1268,  1.0845])

Note

When input is on the CPU, the implementation of tensorplay.sinh may use the Sleef library, which rounds very large results to infinity or negative infinity. See here for details.

#

slice_copy

functionFull reference ↗
tensorplay.functional.slice_copy(input, dim=0, start=None, end=None, step=1)[source]

Performs the same operation as tensorplay.slice(), but all output tensors are freshly created instead of aliasing the input.

#

slice_scatter

functionFull reference ↗
tensorplay.functional.slice_scatter(input, src, dim=0, start=None, end=None, step=1) → Tensor[source]

Embeds the values of the src tensor into input at the given dimension. This function returns a tensor with fresh storage; it does not create a view.

Parameters:
  • input (Tensor) – the input tensor.

  • src (Tensor) – The tensor to embed into input

  • dim (int) – the dimension to insert the slice into

  • start (Optional[int]) – the start index of where to insert the slice

  • end (Optional[int]) – the end index of where to insert the slice

  • step (int) – the how many elements to skip in

Example:

>>> a = tensorplay.zeros(8, 8)
>>> b = tensorplay.ones(2, 8)
>>> a.slice_scatter(b, start=6)
tensor([[0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0., 0., 0., 0.],
        [1., 1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1., 1.]])

>>> b = tensorplay.ones(8, 2)
>>> a.slice_scatter(b, dim=1, start=2, end=6, step=2)
tensor([[0., 0., 1., 0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 1., 0., 0., 0.]])
#

sort

functionFull reference ↗
tensorplay.functional.sort(input, dim=-1, descending=False, *, stable=<object object>, out=None)[source]
#

sparse_bsc_tensor

functionFull reference ↗
tensorplay.functional.sparse_bsc_tensor(ccol_indices, row_indices, values, size=None, *, dtype=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None) → Tensor[source]

Constructs a sparse tensor in BSC (Block Compressed Sparse Column)) with specified 2-dimensional blocks at the given ccol_indices and row_indices. Sparse matrix multiplication operations in BSC format are typically faster than that for sparse tensors in COO format. Make you have a look at the note on the data type of the indices.

Note

If the device argument is not specified the device of the given values and indices tensor(s) must match. If, however, the argument is specified the input Tensors will be converted to the given device and in turn determine the device of the constructed sparse tensor.

Parameters:
  • ccol_indices (array_like) – (B+1)-dimensional array of size (*batchsize, ncolblocks + 1). The last element of each batch is the number of non-zeros. This tensor encodes the index in values and row_indices depending on where the given column starts. Each successive number in the tensor subtracted by the number before it denotes the number of elements in a given column.

  • row_indices (array_like) – Row block coordinates of each block in values. (B+1)-dimensional tensor with the same length as values.

  • values (array_list) – Initial blocks for the tensor. Can be a list, tuple, NumPy ndarray, and other types that represents a (1 + 2 + K)-dimensional tensor where K is the number of dense dimensions.

  • size (list, tuple, tensorplay.Size, optional) – Size of the sparse tensor: (*batchsize, nrows * blocksize[0], ncols * blocksize[1], *densesize) If not provided, the size will be inferred as the minimum size big enough to hold all non-zero blocks.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, infers data type from values.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • check_invariants (bool, optional) – If sparse tensor invariants are checked. Default: as returned by tensorplay.sparse.check_sparse_tensor_invariants.is_enabled(), initially False.

Example:

>>> ccol_indices = [0, 1, 2]
>>> row_indices = [0, 1]
>>> values = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
>>> tensorplay.sparse_bsc_tensor(tensorplay.tensor(ccol_indices, dtype=tensorplay.int64),
...                         tensorplay.tensor(row_indices, dtype=tensorplay.int64),
...                         tensorplay.tensor(values), dtype=tensorplay.double)
tensor(ccol_indices=tensor([0, 1, 2]),
       row_indices=tensor([0, 1]),
       values=tensor([[[1., 2.],
                       [3., 4.]],
                      [[5., 6.],
                       [7., 8.]]]), size=(2, 2), nnz=2, dtype=tensorplay.float64,
       layout=tensorplay.sparse_bsc)
#

sparse_bsr_tensor

functionFull reference ↗
tensorplay.functional.sparse_bsr_tensor(crow_indices, col_indices, values, size=None, *, dtype=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None) → Tensor[source]

Constructs a sparse tensor in BSR (Block Compressed Sparse Row)) with specified 2-dimensional blocks at the given crow_indices and col_indices. Sparse matrix multiplication operations in BSR format are typically faster than that for sparse tensors in COO format. Make you have a look at the note on the data type of the indices.

Note

If the device argument is not specified the device of the given values and indices tensor(s) must match. If, however, the argument is specified the input Tensors will be converted to the given device and in turn determine the device of the constructed sparse tensor.

Parameters:
  • crow_indices (array_like) – (B+1)-dimensional array of size (*batchsize, nrowblocks + 1). The last element of each batch is the number of non-zeros. This tensor encodes the block index in values and col_indices depending on where the given row block starts. Each successive number in the tensor subtracted by the number before it denotes the number of blocks in a given row.

  • col_indices (array_like) – Column block coordinates of each block in values. (B+1)-dimensional tensor with the same length as values.

  • values (array_list) – Initial values for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types that represents a (1 + 2 + K)-dimensional tensor where K is the number of dense dimensions.

  • size (list, tuple, tensorplay.Size, optional) – Size of the sparse tensor: (*batchsize, nrows * blocksize[0], ncols * blocksize[1], *densesize) where blocksize == values.shape[1:3]. If not provided, the size will be inferred as the minimum size big enough to hold all non-zero blocks.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, infers data type from values.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • check_invariants (bool, optional) – If sparse tensor invariants are checked. Default: as returned by tensorplay.sparse.check_sparse_tensor_invariants.is_enabled(), initially False.

Example:

>>> crow_indices = [0, 1, 2]
>>> col_indices = [0, 1]
>>> values = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
>>> tensorplay.sparse_bsr_tensor(tensorplay.tensor(crow_indices, dtype=tensorplay.int64),
...                         tensorplay.tensor(col_indices, dtype=tensorplay.int64),
...                         tensorplay.tensor(values), dtype=tensorplay.double)
tensor(crow_indices=tensor([0, 1, 2]),
       col_indices=tensor([0, 1]),
       values=tensor([[[1., 2.],
                       [3., 4.]],
                      [[5., 6.],
                       [7., 8.]]]), size=(2, 2), nnz=2, dtype=tensorplay.float64,
       layout=tensorplay.sparse_bsr)
#

sparse_coo_tensor

functionFull reference ↗
tensorplay.functional.sparse_coo_tensor(indices, values, size=None, *, dtype=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None, is_coalesced=None) → Tensor[source]

Constructs a sparse tensor in COO(rdinate) format with specified values at the given indices.

Note

This function returns an uncoalesced tensor when is_coalesced is unspecified or None.

Note

If the device argument is not specified the device of the given values and indices tensor(s) must match. If, however, the argument is specified the input Tensors will be converted to the given device and in turn determine the device of the constructed sparse tensor.

Parameters:
  • indices (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types. Will be cast to a tensorplay.LongTensor internally. The indices are the coordinates of the non-zero values in the matrix, and thus should be two-dimensional where the first dimension is the number of tensor dimensions and the second dimension is the number of non-zero values.

  • values (array_like) – Initial values for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types.

  • size (list, tuple, or tensorplay.Size, optional) – Size of the sparse tensor. If not provided the size will be inferred as the minimum size big enough to hold all non-zero elements.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, infers data type from values.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • check_invariants (bool, optional) – If sparse tensor invariants are checked. Default: as returned by tensorplay.sparse.check_sparse_tensor_invariants.is_enabled(), initially False.

  • is_coalesced (bool, optional) – When``True``, the caller is responsible for providing tensor indices that correspond to a coalesced tensor. If the check_invariants flag is False, no error will be raised if the prerequisites are not met and this will lead to silently incorrect results. To force coalescion please use coalesce() on the resulting Tensor. Default: None: except for trivial cases (e.g. nnz < 2) the resulting Tensor has is_coalesced set to False`.

Example:

>>> i = tensorplay.tensor([[0, 1, 1],
...                   [2, 0, 2]])
>>> v = tensorplay.tensor([3, 4, 5], dtype=tensorplay.float32)
>>> tensorplay.sparse_coo_tensor(i, v, [2, 4])
tensor(indices=tensor([[0, 1, 1],
                       [2, 0, 2]]),
       values=tensor([3., 4., 5.]),
       size=(2, 4), nnz=3, layout=tensorplay.sparse_coo)

>>> tensorplay.sparse_coo_tensor(i, v)  # Shape inference
tensor(indices=tensor([[0, 1, 1],
                       [2, 0, 2]]),
       values=tensor([3., 4., 5.]),
       size=(2, 3), nnz=3, layout=tensorplay.sparse_coo)

>>> tensorplay.sparse_coo_tensor(i, v, [2, 4],
...                         dtype=tensorplay.float64,
...                         device=tensorplay.device('cuda:0'))
tensor(indices=tensor([[0, 1, 1],
                       [2, 0, 2]]),
       values=tensor([3., 4., 5.]),
       device='cuda:0', size=(2, 4), nnz=3, dtype=tensorplay.float64,
       layout=tensorplay.sparse_coo)

# Create an empty sparse tensor with the following invariants:
#   1. sparse_dim + dense_dim = len(SparseTensor.shape)
#   2. SparseTensor._indices().shape = (sparse_dim, nnz)
#   3. SparseTensor._values().shape = (nnz, SparseTensor.shape[sparse_dim:])
#
# For instance, to create an empty sparse tensor with nnz = 0, dense_dim = 0 and
# sparse_dim = 1 (hence indices is a 2D tensor of shape = (1, 0))
>>> S = tensorplay.sparse_coo_tensor(tensorplay.empty([1, 0]), [], [1])
tensor(indices=tensor([], size=(1, 0)),
       values=tensor([], size=(0,)),
       size=(1,), nnz=0, layout=tensorplay.sparse_coo)

# and to create an empty sparse tensor with nnz = 0, dense_dim = 1 and
# sparse_dim = 1
>>> S = tensorplay.sparse_coo_tensor(tensorplay.empty([1, 0]), tensorplay.empty([0, 2]), [1, 2])
tensor(indices=tensor([], size=(1, 0)),
       values=tensor([], size=(0, 2)),
       size=(1, 2), nnz=0, layout=tensorplay.sparse_coo)
#

sparse_csc_tensor

functionFull reference ↗
tensorplay.functional.sparse_csc_tensor(ccol_indices, row_indices, values, size=None, *, dtype=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None) → Tensor[source]

Constructs a sparse tensor in CSC (Compressed Sparse Column) with specified values at the given ccol_indices and row_indices. Sparse matrix multiplication operations in CSC format are typically faster than that for sparse tensors in COO format. Make you have a look at the note on the data type of the indices.

Note

If the device argument is not specified the device of the given values and indices tensor(s) must match. If, however, the argument is specified the input Tensors will be converted to the given device and in turn determine the device of the constructed sparse tensor.

Parameters:
  • ccol_indices (array_like) – (B+1)-dimensional array of size (*batchsize, ncols + 1). The last element of each batch is the number of non-zeros. This tensor encodes the index in values and row_indices depending on where the given column starts. Each successive number in the tensor subtracted by the number before it denotes the number of elements in a given column.

  • row_indices (array_like) – Row coordinates of each element in values. (B+1)-dimensional tensor with the same length as values.

  • values (array_list) – Initial values for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types that represents a (1+K)-dimensional tensor where K is the number of dense dimensions.

  • size (list, tuple, tensorplay.Size, optional) – Size of the sparse tensor: (*batchsize, nrows, ncols, *densesize). If not provided, the size will be inferred as the minimum size big enough to hold all non-zero elements.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, infers data type from values.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • check_invariants (bool, optional) – If sparse tensor invariants are checked. Default: as returned by tensorplay.sparse.check_sparse_tensor_invariants.is_enabled(), initially False.

Example:

>>> ccol_indices = [0, 2, 4]
>>> row_indices = [0, 1, 0, 1]
>>> values = [1, 2, 3, 4]
>>> tensorplay.sparse_csc_tensor(tensorplay.tensor(ccol_indices, dtype=tensorplay.int64),
...                         tensorplay.tensor(row_indices, dtype=tensorplay.int64),
...                         tensorplay.tensor(values), dtype=tensorplay.double)
tensor(ccol_indices=tensor([0, 2, 4]),
       row_indices=tensor([0, 1, 0, 1]),
       values=tensor([1., 2., 3., 4.]), size=(2, 2), nnz=4,
       dtype=tensorplay.float64, layout=tensorplay.sparse_csc)
#

sparse_csr_tensor

functionFull reference ↗
tensorplay.functional.sparse_csr_tensor(crow_indices, col_indices, values, size=None, *, dtype=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None) → Tensor[source]

Constructs a sparse tensor in CSR (Compressed Sparse Row) with specified values at the given crow_indices and col_indices. Sparse matrix multiplication operations in CSR format are typically faster than that for sparse tensors in COO format. Make you have a look at the note on the data type of the indices.

Note

If the device argument is not specified the device of the given values and indices tensor(s) must match. If, however, the argument is specified the input Tensors will be converted to the given device and in turn determine the device of the constructed sparse tensor.

Parameters:
  • crow_indices (array_like) – (B+1)-dimensional array of size (*batchsize, nrows + 1). The last element of each batch is the number of non-zeros. This tensor encodes the index in values and col_indices depending on where the given row starts. Each successive number in the tensor subtracted by the number before it denotes the number of elements in a given row.

  • col_indices (array_like) – Column coordinates of each element in values. (B+1)-dimensional tensor with the same length as values.

  • values (array_list) – Initial values for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types that represents a (1+K)-dimensional tensor where K is the number of dense dimensions.

  • size (list, tuple, tensorplay.Size, optional) – Size of the sparse tensor: (*batchsize, nrows, ncols, *densesize). If not provided, the size will be inferred as the minimum size big enough to hold all non-zero elements.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. Default: if None, infers data type from values.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • check_invariants (bool, optional) – If sparse tensor invariants are checked. Default: as returned by tensorplay.sparse.check_sparse_tensor_invariants.is_enabled(), initially False.

Example:

>>> crow_indices = [0, 2, 4]
>>> col_indices = [0, 1, 0, 1]
>>> values = [1, 2, 3, 4]
>>> tensorplay.sparse_csr_tensor(tensorplay.tensor(crow_indices, dtype=tensorplay.int64),
...                         tensorplay.tensor(col_indices, dtype=tensorplay.int64),
...                         tensorplay.tensor(values), dtype=tensorplay.double)
tensor(crow_indices=tensor([0, 2, 4]),
       col_indices=tensor([0, 1, 0, 1]),
       values=tensor([1., 2., 3., 4.]), size=(2, 2), nnz=4,
       dtype=tensorplay.float64, layout=tensorplay.sparse_csr)
#

split_copy

functionFull reference ↗
tensorplay.functional.split_copy(input, split_size, dim=0, *, out=None)[source]

Performs the same operation as tensorplay.split(), but all output tensors are freshly created instead of aliasing the input.

#

sqrt

functionFull reference ↗
tensorplay.functional.sqrt(input, *, out=None) → Tensor[source]

Returns a new tensor with the square-root of the elements of input.

outi=inputi\text{out}_{i} = \sqrt{\text{input}_{i}}
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([-2.0755,  1.0226,  0.0831,  0.4806])
>>> tensorplay.sqrt(a)
tensor([    nan,  1.0112,  0.2883,  0.6933])
#

square

functionFull reference ↗
tensorplay.functional.square(input: Tensor, *, out: Tensor | None) → Tensor[source]

Returns a new tensor with the square of the elements of input.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([-2.0755,  1.0226,  0.0831,  0.4806])
>>> tensorplay.square(a)
tensor([ 4.3077,  1.0457,  0.0069,  0.2310])
#

squeeze_copy

functionFull reference ↗
tensorplay.functional.squeeze_copy(input, dim=None)[source]

Performs the same operation as tensorplay.squeeze(), but all output tensors are freshly created instead of aliasing the input.

#

squeeze

functionFull reference ↗
tensorplay.functional.squeeze(input: Tensor, dim: int | List[int] | None) → Tensor[source]

Returns a tensor with all specified dimensions of input of size 1 removed.

For example, if input is of shape: (A×1×B×C×1×D)(A \times 1 \times B \times C \times 1 \times D) then the input.squeeze() will be of shape: (A×B×C×D)(A \times B \times C \times D).

When dim is given, a squeeze operation is done only in the given dimension(s). If input is of shape: (A×1×B)(A \times 1 \times B), squeeze(input, 0) leaves the tensor unchanged, but squeeze(input, 1) will squeeze the tensor to the shape (A×B)(A \times B).

Note

The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other.

Warning

If the tensor has a batch dimension of size 1, then squeeze(input) will also remove the batch dimension, which can lead to unexpected errors. Consider specifying only the dims you wish to be squeezed.

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) –

    if given, the input will be squeezed

    only in the specified dimensions.

    Changed in version 2.0: dim now accepts tuples of dimensions.

Example:

>>> x = tensorplay.zeros(2, 1, 2, 1, 2)
>>> x.size()
tensorplay.Size([2, 1, 2, 1, 2])
>>> y = tensorplay.squeeze(x)
>>> y.size()
tensorplay.Size([2, 2, 2])
>>> y = tensorplay.squeeze(x, 0)
>>> y.size()
tensorplay.Size([2, 1, 2, 1, 2])
>>> y = tensorplay.squeeze(x, 1)
>>> y.size()
tensorplay.Size([2, 2, 1, 2])
>>> y = tensorplay.squeeze(x, (1, 2, 3))
tensorplay.Size([2, 2, 2])
#

stack

functionFull reference ↗
tensorplay.functional.stack(tensors, dim=0, *, out=None) → Tensor[source]

Concatenates a sequence of tensors along a new dimension.

All tensors need to be of the same size.

See also

tensorplay.cat() concatenates the given sequence along an existing dimension.

Parameters:
  • tensors (sequence of Tensors) – sequence of tensors to concatenate

  • dim (int, optional) – dimension to insert. Has to be between 0 and the number of dimensions of concatenated tensors (inclusive). Default: 0

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> x = tensorplay.randn(2, 3)
>>> x
tensor([[ 0.3367,  0.1288,  0.2345],
        [ 0.2303, -1.1229, -0.1863]])
>>> tensorplay.stack((x, x)) # same as tensorplay.stack((x, x), dim=0)
tensor([[[ 0.3367,  0.1288,  0.2345],
         [ 0.2303, -1.1229, -0.1863]],

        [[ 0.3367,  0.1288,  0.2345],
         [ 0.2303, -1.1229, -0.1863]]])
>>> tensorplay.stack((x, x)).size()
tensorplay.Size([2, 2, 3])
>>> tensorplay.stack((x, x), dim=1)
tensor([[[ 0.3367,  0.1288,  0.2345],
         [ 0.3367,  0.1288,  0.2345]],

        [[ 0.2303, -1.1229, -0.1863],
         [ 0.2303, -1.1229, -0.1863]]])
>>> tensorplay.stack((x, x), dim=2)
tensor([[[ 0.3367,  0.3367],
         [ 0.1288,  0.1288],
         [ 0.2345,  0.2345]],

        [[ 0.2303,  0.2303],
         [-1.1229, -1.1229],
         [-0.1863, -0.1863]]])
>>> tensorplay.stack((x, x), dim=-1)
tensor([[[ 0.3367,  0.3367],
         [ 0.1288,  0.1288],
         [ 0.2345,  0.2345]],

        [[ 0.2303,  0.2303],
         [-1.1229, -1.1229],
         [-0.1863, -0.1863]]])
#

std_mean

functionFull reference ↗
tensorplay.functional.std_mean(input, dim=None, *, correction=1, keepdim=False, out=None)[source]

Calculates the standard deviation and mean over the dimensions specified by dim. dim can be a single dimension, list of dimensions, or None to reduce over all dimensions.

The standard deviation (σ\sigma) is calculated as

σ=1max⁡(0, N−δN)∑i=0N−1(xi−xˉ)2\sigma = \sqrt{\frac{1}{\max(0,~N - \delta N)}\sum_{i=0}^{N-1}(x_i-\bar{x})^2}

where xx is the sample set of elements, xˉ\bar{x} is the sample mean, NN is the number of samples and δN\delta N is the correction.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) – the dimension or dimensions to reduce. If None, all dimensions are reduced.

Keyword Arguments:
  • correction (int) –

    difference between the sample size and sample degrees of freedom. Defaults to Bessel’s correction, correction=1.

    Changed in version 2.0: Previously this argument was called unbiased and was a boolean with True corresponding to correction=1 and False being correction=0.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

  • out (Tensor, optional) – the output tensor.

Returns:

A tuple (std, mean) containing the standard deviation and mean.

Example

>>> a = tensorplay.tensor(
...     [[ 0.2035,  1.2959,  1.8101, -0.4644],
...      [ 1.5027, -0.3270,  0.5905,  0.6538],
...      [-1.5745,  1.3330, -0.5596, -0.6548],
...      [ 0.1264, -0.5080,  1.6420,  0.1992]]
... )  # fmt: skip
>>> tensorplay.std_mean(a, dim=0, keepdim=True)
(tensor([[1.2620, 1.0028, 1.0957, 0.6038]]),
 tensor([[ 0.0645,  0.4485,  0.8707, -0.0665]]))
#

std

functionFull reference ↗
tensorplay.functional.std(input, dim=None, *, correction=1, keepdim=False, out=None) → Tensor[source]

Calculates the standard deviation over the dimensions specified by dim. dim can be a single dimension, list of dimensions, or None to reduce over all dimensions.

The standard deviation (σ\sigma) is calculated as

σ=1max⁡(0, N−δN)∑i=0N−1(xi−xˉ)2\sigma = \sqrt{\frac{1}{\max(0,~N - \delta N)}\sum_{i=0}^{N-1}(x_i-\bar{x})^2}

where xx is the sample set of elements, xˉ\bar{x} is the sample mean, NN is the number of samples and δN\delta N is the correction.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) – the dimension or dimensions to reduce. If None, all dimensions are reduced.

Keyword Arguments:
  • correction (int) –

    difference between the sample size and sample degrees of freedom. Defaults to Bessel’s correction, correction=1.

    Changed in version 2.0: Previously this argument was called unbiased and was a boolean with True corresponding to correction=1 and False being correction=0.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

  • out (Tensor, optional) – the output tensor.

Example

>>> a = tensorplay.tensor(
...     [[ 0.2035,  1.2959,  1.8101, -0.4644],
...      [ 1.5027, -0.3270,  0.5905,  0.6538],
...      [-1.5745,  1.3330, -0.5596, -0.6548],
...      [ 0.1264, -0.5080,  1.6420,  0.1992]]
... )  # fmt: skip
>>> tensorplay.std(a, dim=1, keepdim=True)
tensor([[1.0311],
        [0.7477],
        [1.2204],
        [0.9087]])
#

stft

functionFull reference ↗
tensorplay.functional.stft(input, n_fft, hop_length=None, win_length=None, window=None, center=True, pad_mode='reflect', normalized=False, onesided=True, return_complex=True)[source]
#

sub

functionFull reference ↗
tensorplay.functional.sub(input, other, *, alpha=1, out=None) → Tensor[source]

Subtracts other, scaled by alpha, from input.

outi=inputi−alpha×otheri\text{{out}}_i = \text{{input}}_i - \text{{alpha}} \times \text{{other}}_i

Supports broadcasting to a common shape, type promotion, and integer, float, and complex inputs.

Parameters:
  • input (Tensor) – the input tensor.

  • other (Tensor or Number) – the tensor or number to subtract from input.

Keyword Arguments:
  • alpha (Number) – the multiplier for other.

  • out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor((1, 2))
>>> b = tensorplay.tensor((0, 1))
>>> tensorplay.sub(a, b, alpha=2)
tensor([1, 0])
#

sum

functionFull reference ↗
tensorplay.functional.sum(input, *, dtype=None) → Tensor[source]

Returns the sum of all elements in the input tensor.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

Note

Use the dtype argument if you need the result in a specific tensor type. Otherwise, the result type may be automatically promoted (e.g., from tensorplay.int32 to tensorplay.int64).

Example:

>>> a = tensorplay.randn(1, 3)
>>> a
tensor([[ 0.1133, -0.9567,  0.2958]])
>>> tensorplay.sum(a)
tensor(-0.5475)
tensorplay.functional.sum(input, dim, keepdim=False, *, dtype=None) → Tensor[source]

Returns the sum of each row of the input tensor in the given dimension dim. If dim is a list of dimensions, reduce over all of them.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) –

    the dimension or dimensions to reduce. If None, all dimensions are reduced.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

Keyword Arguments:

dtype (tensorplay.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

Example:

>>> a = tensorplay.randn(4, 4)
>>> a
tensor([[ 0.0569, -0.2475,  0.0737, -0.3429],
        [-0.2993,  0.9138,  0.9337, -1.6864],
        [ 0.1132,  0.7892, -0.1003,  0.5688],
        [ 0.3637, -0.9906, -0.4752, -1.5197]])
>>> tensorplay.sum(a, 1)
tensor([-0.4598, -0.1381,  1.3708, -2.6217])
>>> b = tensorplay.arange(4 * 5 * 6).view(4, 5, 6)
>>> tensorplay.sum(b, (2, 1))
tensor([  435.,  1335.,  2235.,  3135.])
#

svd

functionFull reference ↗
tensorplay.functional.svd(input, some=True, compute_uv=True, *, out=None)[source]

Computes the singular value decomposition of either a matrix or batch of matrices input. The singular value decomposition is represented as a namedtuple (U, S, V), such that input =Udiag(S)VH= U \text{diag}(S) V^{\text{H}}. where VHV^{\text{H}} is the transpose of V for real inputs, and the conjugate transpose of V for complex inputs. If input is a batch of matrices, then U, S, and V are also batched with the same batch dimensions as input.

If some is True (default), the method returns the reduced singular value decomposition. In this case, if the last two dimensions of input are m and n, then the returned U and V matrices will contain only min(n, m) orthonormal columns.

If compute_uv is False, the returned U and V will be zero-filled matrices of shape (m, m) and (n, n) respectively, and the same device as input. The argument some has no effect when compute_uv is False.

Supports input of float, double, cfloat and cdouble data types. The dtypes of U and V are the same as input’s. S will always be real-valued, even if input is complex.

Warning

tensorplay.svd() is deprecated in favor of tensorplay.linalg.svd()

U, S, V = tensorplay.svd(A, some=some, compute_uv=True) (default) should be replaced with

U, S, Vh = tensorplay.linalg.svd(A, full_matrices=not some)
V = Vh.mH

_, S, _ = tensorplay.svd(A, some=some, compute_uv=False) should be replaced with

S = tensorplay.linalg.svdvals(A)

Note

Differences with tensorplay.linalg.svd():

  • some is the opposite of tensorplay.linalg.svd()’s full_matrices. Note that default value for both is True, so the default behavior is effectively the opposite.

  • tensorplay.svd() returns V, whereas tensorplay.linalg.svd() returns Vh, that is, VHV^{\text{H}}.

  • If compute_uv is False, tensorplay.svd() returns zero-filled tensors for U and Vh, whereas tensorplay.linalg.svd() returns empty tensors.

Note

The singular values are returned in descending order. If input is a batch of matrices, then the singular values of each matrix in the batch are returned in descending order.

Note

The S tensor can only be used to compute gradients if compute_uv is True.

Note

When some is False, the gradients on U[…, :, min(m, n):] and V[…, :, min(m, n):] will be ignored in the backward pass, as those vectors can be arbitrary bases of the corresponding subspaces.

Note

The implementation of tensorplay.linalg.svd() on CPU uses LAPACK’s routine ?gesdd (a divide-and-conquer algorithm) instead of ?gesvd for speed. Analogously, on GPU, it uses cuSOLVER’s routines gesvdj and gesvdjBatched on CUDA 10.1.243 and later, and MAGMA’s routine gesdd on earlier versions of CUDA.

Note

The returned U will not be contiguous. The matrix (or batch of matrices) will be represented as a column-major matrix (i.e. Fortran-contiguous).

Warning

The gradients with respect to U and V will only be finite when the input does not have zero nor repeated singular values.

Warning

If the distance between any two singular values is close to zero, the gradients with respect to U and V will be numerically unstable, as they depends on 1min⁡i≠jσi2−σj2\frac{1}{\min_{i \neq j} \sigma_i^2 - \sigma_j^2}. The same happens when the matrix has small singular values, as these gradients also depend on S^{-1}.

Warning

For complex-valued input the singular value decomposition is not unique, as U and V may be multiplied by an arbitrary phase factor eiϕe^{i \phi} on every column. The same happens when input has repeated singular values, where one may multiply the columns of the spanning subspace in U and V by a rotation matrix and the resulting vectors will span the same subspace. Different platforms, like NumPy, or inputs on different device types, may produce different U and V tensors.

Parameters:
  • input (Tensor) – the input tensor of size (*, m, n) where * is zero or more batch dimensions consisting of (m, n) matrices.

  • some (bool, optional) – controls whether to compute the reduced or full decomposition, and consequently, the shape of returned U and V. Default: True.

  • compute_uv (bool, optional) – controls whether to compute U and V. Default: True.

Keyword Arguments:

out (tuple, optional) – the output tuple of tensors

Example:

>>> a = tensorplay.randn(5, 3)
>>> a
tensor([[ 0.2364, -0.7752,  0.6372],
        [ 1.7201,  0.7394, -0.0504],
        [-0.3371, -1.0584,  0.5296],
        [ 0.3550, -0.4022,  1.5569],
        [ 0.2445, -0.0158,  1.1414]])
>>> u, s, v = tensorplay.svd(a)
>>> u
tensor([[ 0.4027,  0.0287,  0.5434],
        [-0.1946,  0.8833,  0.3679],
        [ 0.4296, -0.2890,  0.5261],
        [ 0.6604,  0.2717, -0.2618],
        [ 0.4234,  0.2481, -0.4733]])
>>> s
tensor([2.3289, 2.0315, 0.7806])
>>> v
tensor([[-0.0199,  0.8766,  0.4809],
        [-0.5080,  0.4054, -0.7600],
        [ 0.8611,  0.2594, -0.4373]])
>>> tensorplay.dist(a, tensorplay.mm(tensorplay.mm(u, tensorplay.diag(s)), v.t()))
tensor(8.6531e-07)
>>> a_big = tensorplay.randn(7, 5, 3)
>>> u, s, v = tensorplay.svd(a_big)
>>> tensorplay.dist(a_big, tensorplay.matmul(tensorplay.matmul(u, tensorplay.diag_embed(s)), v.mT))
tensor(2.6503e-06)
#

swapaxes

functionFull reference ↗
tensorplay.functional.swapaxes(input, axis0, axis1) → Tensor[source]

Alias for tensorplay.transpose().

This function is equivalent to NumPy’s swapaxes function.

Examples:

>>> x = tensorplay.tensor([[[0,1],[2,3]],[[4,5],[6,7]]])
>>> x
tensor([[[0, 1],
        [2, 3]],

        [[4, 5],
        [6, 7]]])
>>> tensorplay.swapaxes(x, 0, 1)
tensor([[[0, 1],
        [4, 5]],

        [[2, 3],
        [6, 7]]])
>>> tensorplay.swapaxes(x, 0, 2)
tensor([[[0, 4],
        [2, 6]],

        [[1, 5],
        [3, 7]]])
#

swapdims

functionFull reference ↗
tensorplay.functional.swapdims(input, dim0, dim1) → Tensor[source]

Alias for tensorplay.transpose().

This function is equivalent to NumPy’s swapaxes function.

Examples:

>>> x = tensorplay.tensor([[[0,1],[2,3]],[[4,5],[6,7]]])
>>> x
tensor([[[0, 1],
        [2, 3]],

        [[4, 5],
        [6, 7]]])
>>> tensorplay.swapdims(x, 0, 1)
tensor([[[0, 1],
        [4, 5]],

        [[2, 3],
        [6, 7]]])
>>> tensorplay.swapdims(x, 0, 2)
tensor([[[0, 4],
        [2, 6]],

        [[1, 5],
        [3, 7]]])
#

sym_constrain_range_for_size

functionFull reference ↗
tensorplay.functional.sym_constrain_range_for_size(size, min=None, max=None)[source]
#

t_copy

functionFull reference ↗
tensorplay.functional.t_copy(input)[source]

Performs the same operation as tensorplay.t(), but all output tensors are freshly created instead of aliasing the input.

#

t

functionFull reference ↗
tensorplay.functional.t(input) → Tensor[source]

Expects input to be <= 2-D tensor and transposes dimensions 0 and 1.

0-D and 1-D tensors are returned as is. When input is a 2-D tensor this is equivalent to transpose(input, 0, 1).

Parameters:

input (Tensor) – the input tensor.

Example:

>>> x = tensorplay.randn(())
>>> x
tensor(0.1995)
>>> tensorplay.t(x)
tensor(0.1995)
>>> x = tensorplay.randn(3)
>>> x
tensor([ 2.4320, -0.4608,  0.7702])
>>> tensorplay.t(x)
tensor([ 2.4320, -0.4608,  0.7702])
>>> x = tensorplay.randn(2, 3)
>>> x
tensor([[ 0.4875,  0.9158, -0.5872],
        [ 0.3938, -0.6929,  0.6932]])
>>> tensorplay.t(x)
tensor([[ 0.4875,  0.3938],
        [ 0.9158, -0.6929],
        [-0.5872,  0.6932]])

See also tensorplay.transpose().

#

take_along_dim

functionFull reference ↗
tensorplay.functional.take_along_dim(input, indices, dim=None, *, out=None) → Tensor[source]

Selects values from input at the 1-dimensional indices from indices along the given dim.

If dim is None, the input array is treated as if it has been flattened to 1d.

Functions that return indices along a dimension, like tensorplay.argmax() and tensorplay.argsort(), are designed to work with this function. See the examples below.

Note

This function is similar to NumPy’s take_along_axis. See also tensorplay.gather().

Parameters:
  • input (Tensor) – the input tensor.

  • indices (LongTensor) – the indices into input. Must have long dtype.

  • dim (int, optional) – dimension to select along. Default: 0

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> t = tensorplay.tensor([[10, 30, 20], [60, 40, 50]])
>>> max_idx = tensorplay.argmax(t)
>>> tensorplay.take_along_dim(t, max_idx)
tensor([60])
>>> sorted_idx = tensorplay.argsort(t, dim=1)
>>> tensorplay.take_along_dim(t, sorted_idx, dim=1)
tensor([[10, 20, 30],
        [40, 50, 60]])
#

take

functionFull reference ↗
tensorplay.functional.take(input, index) → Tensor[source]

Returns a new tensor with the elements of input at the given indices. The input tensor is treated as if it were viewed as a 1-D tensor. The result takes the same shape as the indices.

Parameters:
  • input (Tensor) – the input tensor.

  • index (LongTensor) – the indices into tensor

Example:

>>> src = tensorplay.tensor([[4, 3, 5],
...                     [6, 7, 8]])
>>> tensorplay.take(src, tensorplay.tensor([0, 2, 5]))
tensor([ 4,  5,  8])
#

tan

functionFull reference ↗
tensorplay.functional.tan(input, *, out=None) → Tensor[source]

Returns a new tensor with the tangent of the elements of input.

outi=tan⁡(inputi)\text{out}_{i} = \tan(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([-1.2027, -1.7687,  0.4412, -1.3856])
>>> tensorplay.tan(a)
tensor([-2.5930,  4.9859,  0.4722, -5.3366])
#

tanh

functionFull reference ↗
tensorplay.functional.tanh(input, *, out=None) → Tensor[source]

Returns a new tensor with the hyperbolic tangent of the elements of input.

outi=tanh⁡(inputi)\text{out}_{i} = \tanh(\text{input}_{i})
Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([ 0.8986, -0.7279,  1.1745,  0.2611])
>>> tensorplay.tanh(a)
tensor([ 0.7156, -0.6218,  0.8257,  0.2553])
#

tensor_split

functionFull reference ↗
tensorplay.functional.tensor_split(input, indices_or_sections, dim=0) → List of Tensors[source]

Splits a tensor into multiple sub-tensors, all of which are views of input, along dimension dim according to the indices or number of sections specified by indices_or_sections. This function is based on NumPy’s numpy.array_split().

Parameters:
  • input (Tensor) – the tensor to split

  • indices_or_sections (Tensor, int or list or tuple of ints) –

    If indices_or_sections is an integer n or a zero dimensional long tensor with value n, input is split into n sections along dimension dim. If input is divisible by n along dimension dim, each section will be of equal size, input.size(dim) / n. If input is not divisible by n, the sizes of the first int(input.size(dim) % n) sections will have size int(input.size(dim) / n) + 1, and the rest will have size int(input.size(dim) / n).

    If indices_or_sections is a list or tuple of ints, or a one-dimensional long tensor, then input is split along dimension dim at each of the indices in the list, tuple or tensor. For instance, indices_or_sections=[2, 3] and dim=0 would result in the tensors input[:2], input[2:3], and input[3:].

    If indices_or_sections is a tensor, it must be a zero-dimensional or one-dimensional long tensor on the CPU.

  • dim (int, optional) – dimension along which to split the tensor. Default: 0

Example:

>>> x = tensorplay.arange(8)
>>> tensorplay.tensor_split(x, 3)
(tensor([0, 1, 2]), tensor([3, 4, 5]), tensor([6, 7]))

>>> x = tensorplay.arange(7)
>>> tensorplay.tensor_split(x, 3)
(tensor([0, 1, 2]), tensor([3, 4]), tensor([5, 6]))
>>> tensorplay.tensor_split(x, (1, 6))
(tensor([0]), tensor([1, 2, 3, 4, 5]), tensor([6]))

>>> x = tensorplay.arange(14).reshape(2, 7)
>>> x
tensor([[ 0,  1,  2,  3,  4,  5,  6],
        [ 7,  8,  9, 10, 11, 12, 13]])
>>> tensorplay.tensor_split(x, 3, dim=1)
(tensor([[0, 1, 2],
        [7, 8, 9]]),
 tensor([[ 3,  4],
        [10, 11]]),
 tensor([[ 5,  6],
        [12, 13]]))
>>> tensorplay.tensor_split(x, (1, 6), dim=1)
(tensor([[0],
        [7]]),
 tensor([[ 1,  2,  3,  4,  5],
        [ 8,  9, 10, 11, 12]]),
 tensor([[ 6],
        [13]]))
#

tile

functionFull reference ↗
tensorplay.functional.tile(input, dims) → Tensor[source]

Constructs a tensor by repeating the elements of input. The dims argument specifies the number of repetitions in each dimension.

If dims specifies fewer dimensions than input has, then ones are prepended to dims until all dimensions are specified. For example, if input has shape (8, 6, 4, 2) and dims is (2, 2), then dims is treated as (1, 1, 2, 2).

Analogously, if input has fewer dimensions than dims specifies, then input is treated as if it were unsqueezed at dimension zero until it has as many dimensions as dims specifies. For example, if input has shape (4, 2) and dims is (3, 3, 2, 2), then input is treated as if it had the shape (1, 1, 4, 2).

Note

This function is similar to NumPy’s tile function.

Parameters:
  • input (Tensor) – the tensor whose elements to repeat.

  • dims (tuple) – the number of repetitions per dimension.

Example:

>>> x = tensorplay.tensor([1, 2, 3])
>>> x.tile((2,))
tensor([1, 2, 3, 1, 2, 3])
>>> y = tensorplay.tensor([[1, 2], [3, 4]])
>>> tensorplay.tile(y, (2, 2))
tensor([[1, 2, 1, 2],
        [3, 4, 3, 4],
        [1, 2, 1, 2],
        [3, 4, 3, 4]])
#

trace

functionFull reference ↗
tensorplay.functional.trace(input) → Tensor[source]

Returns the sum of the elements of the diagonal of the input 2-D matrix.

Example:

>>> x = tensorplay.arange(1., 10.).view(3, 3)
>>> x
tensor([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.],
        [ 7.,  8.,  9.]])
>>> tensorplay.trace(x)
tensor(15.)
#

transpose_copy

functionFull reference ↗
tensorplay.functional.transpose_copy(input, dim0, dim1)[source]

Performs the same operation as tensorplay.transpose(), but all output tensors are freshly created instead of aliasing the input.

#

transpose

functionFull reference ↗
tensorplay.functional.transpose(input, dim0, dim1) → Tensor[source]

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 the relevant documentation then the resulting out tensor does not share the underlying storage with the input tensor.

If input is a the relevant documentation 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.

Note

Transpositions which interchange the sparse dimensions of a SparseCSR or SparseCSC layout tensor will result in the layout changing between the two options. Transposition of the sparse dimensions of a ` SparseBSR` or SparseBSC layout tensor will likewise generate a result with the opposite layout.

Parameters:
  • input (Tensor) – the input tensor.

  • dim0 (int) – the first dimension to be transposed

  • dim1 (int) – the second dimension to be transposed

Example:

>>> x = tensorplay.randn(2, 3)
>>> x
tensor([[ 1.0028, -0.9893,  0.5809],
        [-0.1669,  0.7299,  0.4942]])
>>> tensorplay.transpose(x, 0, 1)
tensor([[ 1.0028, -0.1669],
        [-0.9893,  0.7299],
        [ 0.5809,  0.4942]])

See also tensorplay.t().

#

trapezoid

functionFull reference ↗
tensorplay.functional.trapezoid(y, x=None, *, dx=None, dim=-1) → Tensor[source]

Computes the trapezoidal rule along dim. By default the spacing between elements is assumed to be 1, but dx can be used to specify a different constant spacing, and x can be used to specify arbitrary spacing along dim. Only one of x or dx should be specified.

Assuming y is a one-dimensional tensor with elements y0,y1,...,yn{y_0, y_1, ..., y_n}, the default computation is

∑i=1n12(yi+yi−1)\begin{aligned} \sum_{i = 1}^{n} \frac{1}{2} (y_i + y_{i-1}) \end{aligned}

When dx is specified the computation becomes

∑i=1nΔx2(yi+yi−1)\begin{aligned} \sum_{i = 1}^{n} \frac{\Delta x}{2} (y_i + y_{i-1}) \end{aligned}

effectively multiplying the result by dx. When x is specified, assuming x is also a one-dimensional tensor with elements x0,x1,...,xn{x_0, x_1, ..., x_n}, the computation becomes

∑i=1n(xi−xi−1)2(yi+yi−1)\begin{aligned} \sum_{i = 1}^{n} \frac{(x_i - x_{i-1})}{2} (y_i + y_{i-1}) \end{aligned}

When x and y have the same size, the computation is as described above and no broadcasting is needed. The broadcasting behavior of this function is as follows when their sizes are different. For both x and y, the function computes the difference between consecutive elements along dimension dim. This effectively creates two tensors, x_diff and y_diff, that have the same shape as the original tensors except their lengths along the dimension dim is reduced by 1. After that, those two tensors are broadcast together to compute final output as part of the trapezoidal rule. See the examples below for details.

Note

The trapezoidal rule is a technique for approximating the definite integral of a function by averaging its left and right Riemann sums. The approximation becomes more accurate as the resolution of the partition increases.

Parameters:
  • y (Tensor) – Values to use when computing the trapezoidal rule.

  • x (Tensor) – If specified, defines spacing between values as specified above.

Keyword Arguments:
  • dx (float) – constant spacing between values. If neither x or dx are specified then this defaults to 1. Effectively multiplies the result by its value.

  • dim (int) – The dimension along which to compute the trapezoidal rule. The last (inner-most) dimension by default.

Examples:

>>> # Computes the trapezoidal rule in 1D, spacing is implicitly 1
>>> y = tensorplay.tensor([1, 5, 10])
>>> tensorplay.trapezoid(y)
tensor(10.5)

>>> # Computes the same trapezoidal rule directly to verify
>>> (1 + 10 + 10) / 2
10.5

>>> # Computes the trapezoidal rule in 1D with constant spacing of 2
>>> # NOTE: the result is the same as before, but multiplied by 2
>>> tensorplay.trapezoid(y, dx=2)
21.0

>>> # Computes the trapezoidal rule in 1D with arbitrary spacing
>>> x = tensorplay.tensor([1, 3, 6])
>>> tensorplay.trapezoid(y, x)
28.5

>>> # Computes the same trapezoidal rule directly to verify
>>> ((3 - 1) * (1 + 5) + (6 - 3) * (5 + 10)) / 2
28.5

>>> # Computes the trapezoidal rule for each row of a 3x3 matrix
>>> y = tensorplay.arange(9).reshape(3, 3)
tensor([[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]])
>>> tensorplay.trapezoid(y)
tensor([ 2., 8., 14.])

>>> # Computes the trapezoidal rule for each column of the matrix
>>> tensorplay.trapezoid(y, dim=0)
tensor([ 6., 8., 10.])

>>> # Computes the trapezoidal rule for each row of a 3x3 ones matrix
>>> #   with the same arbitrary spacing
>>> y = tensorplay.ones(3, 3)
>>> x = tensorplay.tensor([1, 3, 6])
>>> tensorplay.trapezoid(y, x)
array([5., 5., 5.])

>>> # Computes the trapezoidal rule for each row of a 3x3 ones matrix
>>> #   with different arbitrary spacing per row
>>> y = tensorplay.ones(3, 3)
>>> x = tensorplay.tensor([[1, 2, 3], [1, 3, 5], [1, 4, 7]])
>>> tensorplay.trapezoid(y, x)
array([2., 4., 6.])
#

triangular_solve

functionFull reference ↗
tensorplay.functional.triangular_solve(b, A, upper=True, transpose=False, unitriangular=False, *, out=None)[source]

Solves a system of equations with a square upper or lower triangular invertible matrix AA and multiple right-hand sides bb.

In symbols, it solves AX=bAX = b and assumes AA is square upper-triangular (or lower-triangular if upper= False) and does not have zeros on the diagonal.

tensorplay.triangular_solve(b, A) can take in 2D inputs b, A or inputs that are batches of 2D matrices. If the inputs are batches, then returns batched outputs X

If the diagonal of A contains zeros or elements that are very close to zero and unitriangular= False (default) or if the input matrix is badly conditioned, the result may contain NaN s.

Supports input of float, double, cfloat and cdouble data types.

Warning

tensorplay.triangular_solve() is deprecated in favor of tensorplay.linalg.solve_triangular() tensorplay.linalg.solve_triangular() has its arguments reversed and does not return a copy of one of the inputs.

X = tensorplay.triangular_solve(B, A).solution should be replaced with

X = tensorplay.linalg.solve_triangular(A, B)
Parameters:
  • b (Tensor) – multiple right-hand sides of size (∗,m,k)(*, m, k) where ∗* is zero of more batch dimensions

  • A (Tensor) – the input triangular coefficient matrix of size (∗,m,m)(*, m, m) where ∗* is zero or more batch dimensions

  • upper (bool, optional) – whether AA is upper or lower triangular. Default: True.

  • transpose (bool, optional) – solves op(A)X = b where op(A) = A^T if this flag is True, and op(A) = A if it is False. Default: False.

  • unitriangular (bool, optional) – whether AA is unit triangular. If True, the diagonal elements of AA are assumed to be 1 and not referenced from AA. Default: False.

Keyword Arguments:

out ((Tensor, Tensor), optional) – tuple of two tensors to write the output to. Ignored if None. Default: None.

Returns:

A namedtuple (solution, cloned_coefficient) where cloned_coefficient is a clone of AA and solution is the solution XX to AX=bAX = b (or whatever variant of the system of equations, depending on the keyword arguments.)

Examples:

>>> A = tensorplay.randn(2, 2).triu()
>>> A
tensor([[ 1.1527, -1.0753],
        [ 0.0000,  0.7986]])
>>> b = tensorplay.randn(2, 3)
>>> b
tensor([[-0.0210,  2.3513, -1.5492],
        [ 1.5429,  0.7403, -1.0243]])
>>> tensorplay.triangular_solve(b, A)
tensorplay.return_types.triangular_solve(
solution=tensor([[ 1.7841,  2.9046, -2.5405],
        [ 1.9320,  0.9270, -1.2826]]),
cloned_coefficient=tensor([[ 1.1527, -1.0753],
        [ 0.0000,  0.7986]]))
#

tril_indices

functionFull reference ↗
tensorplay.functional.tril_indices(row, col, offset=0, *, dtype=tensorplay.long, device='cpu', layout=tensorplay.strided) → Tensor[source]

Returns the indices of the lower triangular part of a row-by- col matrix in a 2-by-N Tensor, where the first row contains row coordinates of all indices and the second row contains column coordinates. Indices are ordered based on rows and then columns.

The lower triangular part of the matrix is defined as the elements on and below the diagonal.

The argument offset controls which diagonal to consider. If offset = 0, all elements on and below the main diagonal are retained. A positive value includes just as many diagonals above the main diagonal, and similarly a negative value excludes just as many diagonals below the main diagonal. The main diagonal are the set of indices {(i,i)}\lbrace (i, i) \rbrace for i∈[0,min⁡{d1,d2}−1]i \in [0, \min\{d_{1}, d_{2}\} - 1] where d1,d2d_{1}, d_{2} are the dimensions of the matrix.

Note

When running on CUDA, row * col must be less than 2592^{59} to prevent overflow during calculation.

Parameters:
  • row (int) – number of rows in the 2-D matrix.

  • col (int) – number of columns in the 2-D matrix.

  • offset (int) – diagonal offset from the main diagonal. Default: if not provided, 0.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor, only support tensorplay.int, tensorplay.long. Default: if None, tensorplay.long.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • layout (tensorplay.layout, optional) – currently only support tensorplay.strided.

Example:

>>> a = tensorplay.tril_indices(3, 3)
>>> a
tensor([[0, 1, 1, 2, 2, 2],
        [0, 0, 1, 0, 1, 2]])

>>> a = tensorplay.tril_indices(4, 3, -1)
>>> a
tensor([[1, 2, 2, 3, 3, 3],
        [0, 0, 1, 0, 1, 2]])

>>> a = tensorplay.tril_indices(4, 3, 1)
>>> a
tensor([[0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3],
        [0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 2]])
#

tril

functionFull reference ↗
tensorplay.functional.tril(input, diagonal=0, *, out=None) → Tensor[source]

Returns the lower triangular part of the matrix (2-D tensor) or batch of matrices input, the other elements of the result tensor out are set to 0.

The lower triangular part of the matrix is defined as the elements on and below the diagonal.

The argument diagonal controls which diagonal to consider. If diagonal = 0, all elements on and below the main diagonal are retained. A positive value includes just as many diagonals above the main diagonal, and similarly a negative value excludes just as many diagonals below the main diagonal. The main diagonal are the set of indices {(i,i)}\lbrace (i, i) \rbrace for i∈[0,min⁡{d1,d2}−1]i \in [0, \min\{d_{1}, d_{2}\} - 1] where d1,d2d_{1}, d_{2} are the dimensions of the matrix.

Parameters:
  • input (Tensor) – the input tensor.

  • diagonal (int, optional) – the diagonal to consider

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(3, 3)
>>> a
tensor([[-1.0813, -0.8619,  0.7105],
        [ 0.0935,  0.1380,  2.2112],
        [-0.3409, -0.9828,  0.0289]])
>>> tensorplay.tril(a)
tensor([[-1.0813,  0.0000,  0.0000],
        [ 0.0935,  0.1380,  0.0000],
        [-0.3409, -0.9828,  0.0289]])

>>> b = tensorplay.randn(4, 6)
>>> b
tensor([[ 1.2219,  0.5653, -0.2521, -0.2345,  1.2544,  0.3461],
        [ 0.4785, -0.4477,  0.6049,  0.6368,  0.8775,  0.7145],
        [ 1.1502,  3.2716, -1.1243, -0.5413,  0.3615,  0.6864],
        [-0.0614, -0.7344, -1.3164, -0.7648, -1.4024,  0.0978]])
>>> tensorplay.tril(b, diagonal=1)
tensor([[ 1.2219,  0.5653,  0.0000,  0.0000,  0.0000,  0.0000],
        [ 0.4785, -0.4477,  0.6049,  0.0000,  0.0000,  0.0000],
        [ 1.1502,  3.2716, -1.1243, -0.5413,  0.0000,  0.0000],
        [-0.0614, -0.7344, -1.3164, -0.7648, -1.4024,  0.0000]])
>>> tensorplay.tril(b, diagonal=-1)
tensor([[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
        [ 0.4785,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
        [ 1.1502,  3.2716,  0.0000,  0.0000,  0.0000,  0.0000],
        [-0.0614, -0.7344, -1.3164,  0.0000,  0.0000,  0.0000]])
#

triplet_margin_loss

functionFull reference ↗
tensorplay.functional.triplet_margin_loss(anchor, positive, negative, margin=1.0, p=2.0)[source]
#

triu_indices

functionFull reference ↗
tensorplay.functional.triu_indices(row, col, offset=0, *, dtype=tensorplay.long, device='cpu', layout=tensorplay.strided) → Tensor[source]

Returns the indices of the upper triangular part of a row by col matrix in a 2-by-N Tensor, where the first row contains row coordinates of all indices and the second row contains column coordinates. Indices are ordered based on rows and then columns.

The upper triangular part of the matrix is defined as the elements on and above the diagonal.

The argument offset controls which diagonal to consider. If offset = 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. The main diagonal are the set of indices {(i,i)}\lbrace (i, i) \rbrace for i∈[0,min⁡{d1,d2}−1]i \in [0, \min\{d_{1}, d_{2}\} - 1] where d1,d2d_{1}, d_{2} are the dimensions of the matrix.

Note

When running on CUDA, row * col must be less than 2592^{59} to prevent overflow during calculation.

Parameters:
  • row (int) – number of rows in the 2-D matrix.

  • col (int) – number of columns in the 2-D matrix.

  • offset (int) – diagonal offset from the main diagonal. Default: if not provided, 0.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned tensor, only support tensorplay.int, tensorplay.long. Default: if None, tensorplay.long.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see tensorplay.set_default_device()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

  • layout (tensorplay.layout, optional) – currently only support tensorplay.strided.

Example:

>>> a = tensorplay.triu_indices(3, 3)
>>> a
tensor([[0, 0, 0, 1, 1, 2],
        [0, 1, 2, 1, 2, 2]])

>>> a = tensorplay.triu_indices(4, 3, -1)
>>> a
tensor([[0, 0, 0, 1, 1, 1, 2, 2, 3],
        [0, 1, 2, 0, 1, 2, 1, 2, 2]])

>>> a = tensorplay.triu_indices(4, 3, 1)
>>> a
tensor([[0, 0, 1],
        [1, 2, 2]])
#

triu

functionFull reference ↗
tensorplay.functional.triu(input, diagonal=0, *, out=None) → Tensor[source]

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.

The upper triangular part of the matrix is defined as the elements on and above the diagonal.

The argument 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. The main diagonal are the set of indices {(i,i)}\lbrace (i, i) \rbrace for i∈[0,min⁡{d1,d2}−1]i \in [0, \min\{d_{1}, d_{2}\} - 1] where d1,d2d_{1}, d_{2} are the dimensions of the matrix.

Parameters:
  • input (Tensor) – the input tensor.

  • diagonal (int, optional) – the diagonal to consider

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(3, 3)
>>> a
tensor([[ 0.2309,  0.5207,  2.0049],
        [ 0.2072, -1.0680,  0.6602],
        [ 0.3480, -0.5211, -0.4573]])
>>> tensorplay.triu(a)
tensor([[ 0.2309,  0.5207,  2.0049],
        [ 0.0000, -1.0680,  0.6602],
        [ 0.0000,  0.0000, -0.4573]])
>>> tensorplay.triu(a, diagonal=1)
tensor([[ 0.0000,  0.5207,  2.0049],
        [ 0.0000,  0.0000,  0.6602],
        [ 0.0000,  0.0000,  0.0000]])
>>> tensorplay.triu(a, diagonal=-1)
tensor([[ 0.2309,  0.5207,  2.0049],
        [ 0.2072, -1.0680,  0.6602],
        [ 0.0000, -0.5211, -0.4573]])

>>> b = tensorplay.randn(4, 6)
>>> b
tensor([[ 0.5876, -0.0794, -1.8373,  0.6654,  0.2604,  1.5235],
        [-0.2447,  0.9556, -1.2919,  1.3378, -0.1768, -1.0857],
        [ 0.4333,  0.3146,  0.6576, -1.0432,  0.9348, -0.4410],
        [-0.9888,  1.0679, -1.3337, -1.6556,  0.4798,  0.2830]])
>>> tensorplay.triu(b, diagonal=1)
tensor([[ 0.0000, -0.0794, -1.8373,  0.6654,  0.2604,  1.5235],
        [ 0.0000,  0.0000, -1.2919,  1.3378, -0.1768, -1.0857],
        [ 0.0000,  0.0000,  0.0000, -1.0432,  0.9348, -0.4410],
        [ 0.0000,  0.0000,  0.0000,  0.0000,  0.4798,  0.2830]])
>>> tensorplay.triu(b, diagonal=-1)
tensor([[ 0.5876, -0.0794, -1.8373,  0.6654,  0.2604,  1.5235],
        [-0.2447,  0.9556, -1.2919,  1.3378, -0.1768, -1.0857],
        [ 0.0000,  0.3146,  0.6576, -1.0432,  0.9348, -0.4410],
        [ 0.0000,  0.0000, -1.3337, -1.6556,  0.4798,  0.2830]])
#

true_divide

functionFull reference ↗
tensorplay.functional.true_divide(dividend, divisor, *, out) → Tensor[source]

Alias for tensorplay.div() with rounding_mode=None.

#

trunc

functionFull reference ↗
tensorplay.functional.trunc(input, *, out=None) → Tensor[source]

Returns a new tensor with the truncated integer values of the elements of input.

For integer inputs, follows the array-api convention of returning a copy of the input tensor.

Parameters:

input (Tensor) – the input tensor.

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.randn(4)
>>> a
tensor([ 3.4742,  0.5466, -0.8008, -0.9079])
>>> tensorplay.trunc(a)
tensor([ 3.,  0., -0., -0.])
#

unbind_copy

functionFull reference ↗
tensorplay.functional.unbind_copy(input, dim=0, *, out=None)[source]

Performs the same operation as tensorplay.unbind(), but all output tensors are freshly created instead of aliasing the input.

#

unbind

functionFull reference ↗
tensorplay.functional.unbind(input, dim=0) → seq[source]

Removes a tensor dimension.

Returns a tuple of all slices along a given dimension, already without it.

Parameters:
  • input (Tensor) – the tensor to unbind

  • dim (int) – dimension to remove

Example:

>>> tensorplay.unbind(tensorplay.tensor([[1, 2, 3],
>>>                            [4, 5, 6],
>>>                            [7, 8, 9]]))
(tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([7, 8, 9]))
#

unflatten

functionFull reference ↗
tensorplay.functional.unflatten(input, dim, sizes) → Tensor[source]

Expands a dimension of the input tensor over multiple dimensions.

See also

tensorplay.flatten() the inverse of this function. It coalesces several dimensions into one.

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int) – Dimension to be unflattened, specified as an index into input.shape.

  • sizes (Tuple[int]) – New shape of the unflattened dimension. One of its elements can be -1 in which case the corresponding output dimension is inferred. Otherwise, the product of sizes must equal input.shape[dim].

Returns:

A View of input with the specified dimension unflattened.

Examples::
>>> tensorplay.unflatten(tensorplay.randn(3, 4, 1), 1, (2, 2)).shape
tensorplay.Size([3, 2, 2, 1])
>>> tensorplay.unflatten(tensorplay.randn(3, 4, 1), 1, (-1, 2)).shape
tensorplay.Size([3, 2, 2, 1])
>>> tensorplay.unflatten(tensorplay.randn(5, 12, 3), -2, (2, 2, 3, 1, 1)).shape
tensorplay.Size([5, 2, 2, 3, 1, 1, 3])
#

unfold_copy

functionFull reference ↗
tensorplay.functional.unfold_copy(input, dimension, size, step)[source]

Performs the same operation as tensorplay.unfold(), but all output tensors are freshly created instead of aliasing the input.

#

unique_consecutive

functionFull reference ↗
tensorplay.functional.unique_consecutive(input, return_inverse=False, return_counts=False, dim=None)[source]
#

unique

functionFull reference ↗
tensorplay.functional.unique(input, sorted=True, return_inverse=False, return_counts=False, dim=None)[source]
#

unsqueeze_copy

functionFull reference ↗
tensorplay.functional.unsqueeze_copy(input, dim)[source]

Performs the same operation as tensorplay.unsqueeze(), but all output tensors are freshly created instead of aliasing the input.

#

unsqueeze

functionFull reference ↗
tensorplay.functional.unsqueeze(input, dim) → Tensor[source]

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:
  • input (Tensor) – the input tensor.

  • dim (int) – the index at which to insert the singleton dimension

Example:

>>> x = tensorplay.tensor([1, 2, 3, 4])
>>> tensorplay.unsqueeze(x, 0)
tensor([[ 1,  2,  3,  4]])
>>> tensorplay.unsqueeze(x, 1)
tensor([[ 1],
        [ 2],
        [ 3],
        [ 4]])
#

vander

functionFull reference ↗
tensorplay.functional.vander(x, N=None, increasing=False) → Tensor[source]

Generates a Vandermonde matrix.

The columns of the output matrix are elementwise powers of the input vector x(N−1),x(N−2),...,x0x^{(N-1)}, x^{(N-2)}, ..., x^0. If increasing is True, the order of the columns is reversed x0,x1,...,x(N−1)x^0, x^1, ..., x^{(N-1)}. Such a matrix with a geometric progression in each row is named for Alexandre-Theophile Vandermonde.

Parameters:
  • x (Tensor) – 1-D input tensor.

  • N (int, optional) – Number of columns in the output. If N is not specified, a square array is returned (N=len(x))(N = len(x)).

  • increasing (bool, optional) – Order of the powers of the columns. If True, the powers increase from left to right, if False (the default) they are reversed.

Returns:

Vandermonde matrix. If increasing is False, the first column is x(N−1)x^{(N-1)}, the second x(N−2)x^{(N-2)} and so forth. If increasing is True, the columns are x0,x1,...,x(N−1)x^0, x^1, ..., x^{(N-1)}.

Return type:

Tensor

Example:

>>> x = tensorplay.tensor([1, 2, 3, 5])
>>> tensorplay.vander(x)
tensor([[  1,   1,   1,   1],
        [  8,   4,   2,   1],
        [ 27,   9,   3,   1],
        [125,  25,   5,   1]])
>>> tensorplay.vander(x, N=3)
tensor([[ 1,  1,  1],
        [ 4,  2,  1],
        [ 9,  3,  1],
        [25,  5,  1]])
>>> tensorplay.vander(x, N=3, increasing=True)
tensor([[ 1,  1,  1],
        [ 1,  2,  4],
        [ 1,  3,  9],
        [ 1,  5, 25]])
#

var_mean

functionFull reference ↗
tensorplay.functional.var_mean(input, dim=None, *, correction=1, keepdim=False, out=None)[source]

Calculates the variance and mean over the dimensions specified by dim. dim can be a single dimension, list of dimensions, or None to reduce over all dimensions.

The variance (σ2\sigma^2) is calculated as

σ2=1max⁡(0, N−δN)∑i=0N−1(xi−xˉ)2\sigma^2 = \frac{1}{\max(0,~N - \delta N)}\sum_{i=0}^{N-1}(x_i-\bar{x})^2

where xx is the sample set of elements, xˉ\bar{x} is the sample mean, NN is the number of samples and δN\delta N is the correction.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) – the dimension or dimensions to reduce. If None, all dimensions are reduced.

Keyword Arguments:
  • correction (int) –

    difference between the sample size and sample degrees of freedom. Defaults to Bessel’s correction, correction=1.

    Changed in version 2.0: Previously this argument was called unbiased and was a boolean with True corresponding to correction=1 and False being correction=0.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

  • out (Tensor, optional) – the output tensor.

Returns:

A tuple (var, mean) containing the variance and mean.

Example

>>> a = tensorplay.tensor(
...     [[ 0.2035,  1.2959,  1.8101, -0.4644],
...      [ 1.5027, -0.3270,  0.5905,  0.6538],
...      [-1.5745,  1.3330, -0.5596, -0.6548],
...      [ 0.1264, -0.5080,  1.6420,  0.1992]]
... )  # fmt: skip
>>> tensorplay.var_mean(a, dim=0, keepdim=True)
(tensor([[1.5926, 1.0056, 1.2005, 0.3646]]),
 tensor([[ 0.0645,  0.4485,  0.8707, -0.0665]]))
#

var

functionFull reference ↗
tensorplay.functional.var(input, dim=None, *, correction=1, keepdim=False, out=None) → Tensor[source]

Calculates the variance over the dimensions specified by dim. dim can be a single dimension, list of dimensions, or None to reduce over all dimensions.

The variance (σ2\sigma^2) is calculated as

σ2=1max⁡(0, N−δN)∑i=0N−1(xi−xˉ)2\sigma^2 = \frac{1}{\max(0,~N - \delta N)}\sum_{i=0}^{N-1}(x_i-\bar{x})^2

where xx is the sample set of elements, xˉ\bar{x} is the sample mean, NN is the number of samples and δN\delta N is the correction.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see tensorplay.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters:
  • input (Tensor) – the input tensor.

  • dim (int or tuple of ints, optional) – the dimension or dimensions to reduce. If None, all dimensions are reduced.

Keyword Arguments:
  • correction (int) –

    difference between the sample size and sample degrees of freedom. Defaults to Bessel’s correction, correction=1.

    Changed in version 2.0: Previously this argument was called unbiased and was a boolean with True corresponding to correction=1 and False being correction=0.

    keepdim (bool, optional): whether the output tensor has dim retained or not. Default: False.

  • out (Tensor, optional) – the output tensor.

Example

>>> a = tensorplay.tensor(
...     [[ 0.2035,  1.2959,  1.8101, -0.4644],
...      [ 1.5027, -0.3270,  0.5905,  0.6538],
...      [-1.5745,  1.3330, -0.5596, -0.6548],
...      [ 0.1264, -0.5080,  1.6420,  0.1992]]
... )  # fmt: skip
>>> tensorplay.var(a, dim=1, keepdim=True)
tensor([[1.0631],
        [0.5590],
        [1.4893],
        [0.8258]])
#

vdot

functionFull reference ↗
tensorplay.functional.vdot(input, other, *, out=None) → Tensor[source]

Computes the dot product of two 1D vectors along a dimension.

In symbols, this function computes

∑i=1nxi‾yi.\sum_{i=1}^n \overline{x_i}y_i.

where xi‾\overline{x_i} denotes the conjugate for complex vectors, and it is the identity for real vectors.

Note

Unlike NumPy’s vdot, tensorplay.vdot intentionally only supports computing the dot product of two 1D tensors with the same number of elements.

See also

tensorplay.linalg.vecdot() computes the dot product of two batches of vectors along a dimension.

Parameters:
  • input (Tensor) – first tensor in the dot product, must be 1D. Its conjugate is used if it’s complex.

  • other (Tensor) – second tensor in the dot product, must be 1D.

Keyword args:

Note

out (Tensor, optional): the output tensor.

Example:

>>> tensorplay.vdot(tensorplay.tensor([2, 3]), tensorplay.tensor([2, 1]))
tensor(7)
>>> a = tensorplay.tensor((1 +2j, 3 - 1j))
>>> b = tensorplay.tensor((2 +1j, 4 - 0j))
>>> tensorplay.vdot(a, b)
tensor([16.+1.j])
>>> tensorplay.vdot(b, a)
tensor([16.-1.j])
#

view_as_complex_copy

functionFull reference ↗
tensorplay.functional.view_as_complex_copy(input)[source]

Performs the same operation as tensorplay.view_as_complex(), but all output tensors are freshly created instead of aliasing the input.

#

view_as_complex

functionFull reference ↗
tensorplay.functional.view_as_complex(input) → Tensor[source]

Returns a view of input as a complex tensor. For an input complex tensor of size m1,m2,…,mi,2m1, m2, \dots, mi, 2, this function returns a new complex tensor of size m1,m2,…,mim1, m2, \dots, mi where the last dimension of the input tensor is expected to represent the real and imaginary components of complex numbers.

Warning

view_as_complex() is only supported for tensors with tensorplay.dtype tensorplay.float64 and tensorplay.float32. The input is expected to have the last dimension of size 2. In addition, the tensor must have a stride of 1 for its last dimension. The strides of all other dimensions must be even numbers.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> x=tensorplay.randn(4, 2)
>>> x
tensor([[ 1.6116, -0.5772],
        [-1.4606, -0.9120],
        [ 0.0786, -1.7497],
        [-0.6561, -1.6623]])
>>> tensorplay.view_as_complex(x)
tensor([(1.6116-0.5772j), (-1.4606-0.9120j), (0.0786-1.7497j), (-0.6561-1.6623j)])
#

view_as_real_copy

functionFull reference ↗
tensorplay.functional.view_as_real_copy(input)[source]

Performs the same operation as tensorplay.view_as_real(), but all output tensors are freshly created instead of aliasing the input.

#

view_as_real

functionFull reference ↗
tensorplay.functional.view_as_real(input) → Tensor[source]

Returns a view of input as a real tensor. For an input complex tensor of size m1,m2,…,mim1, m2, \dots, mi, this function returns a new real tensor of size m1,m2,…,mi,2m1, m2, \dots, mi, 2, where the last dimension of size 2 represents the real and imaginary components of complex numbers.

Warning

view_as_real() is only supported for tensors with complex dtypes.

Parameters:

input (Tensor) – the input tensor.

Example:

>>> x=tensorplay.randn(4, dtype=tensorplay.cfloat)
>>> x
tensor([(0.4737-0.3839j), (-0.2098-0.6699j), (0.3470-0.9451j), (-0.5174-1.3136j)])
>>> tensorplay.view_as_real(x)
tensor([[ 0.4737, -0.3839],
        [-0.2098, -0.6699],
        [ 0.3470, -0.9451],
        [-0.5174, -1.3136]])
#

vsplit

functionFull reference ↗
tensorplay.functional.vsplit(input, indices_or_sections) → List of Tensors[source]

Splits input, a tensor with two or more dimensions, into multiple tensors vertically according to indices_or_sections. Each split is a view of input.

This is equivalent to calling tensorplay.tensor_split(input, indices_or_sections, dim=0) (the split dimension is 0), except that if indices_or_sections is an integer it must evenly divide the split dimension or a runtime error will be thrown.

This function is based on NumPy’s numpy.vsplit().

Parameters:
  • input (Tensor) – tensor to split.

  • indices_or_sections (int or list or tuple of ints) – See argument in tensorplay.tensor_split().

Example:

>>> t = tensorplay.arange(16.0).reshape(4,4)
>>> t
tensor([[ 0.,  1.,  2.,  3.],
        [ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.],
        [12., 13., 14., 15.]])
>>> tensorplay.vsplit(t, 2)
(tensor([[0., 1., 2., 3.],
         [4., 5., 6., 7.]]),
 tensor([[ 8.,  9., 10., 11.],
         [12., 13., 14., 15.]]))
>>> tensorplay.vsplit(t, [3, 6])
(tensor([[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.]]),
 tensor([[12., 13., 14., 15.]]),
 tensor([], size=(0, 4)))
#

vstack

functionFull reference ↗
tensorplay.functional.vstack(tensors, *, out=None) → Tensor[source]

Stack tensors in sequence vertically (row wise).

This is equivalent to concatenation along the first axis after all 1-D tensors have been reshaped by tensorplay.atleast_2d().

Parameters:

tensors (sequence of Tensors) – sequence of tensors to concatenate

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Example:

>>> a = tensorplay.tensor([1, 2, 3])
>>> b = tensorplay.tensor([4, 5, 6])
>>> tensorplay.vstack((a,b))
tensor([[1, 2, 3],
        [4, 5, 6]])
>>> a = tensorplay.tensor([[1],[2],[3]])
>>> b = tensorplay.tensor([[4],[5],[6]])
>>> tensorplay.vstack((a,b))
tensor([[1],
        [2],
        [3],
        [4],
        [5],
        [6]])
#

where

functionFull reference ↗
tensorplay.functional.where(condition, input, other, *, out=None) → Tensor[source]

Return a tensor of elements selected from either input or other, depending on condition.

The operation is defined as:

outi={inputiif conditioniotheriotherwise\text{out}_i = \begin{cases} \text{input}_i & \text{if } \text{condition}_i \\ \text{other}_i & \text{otherwise} \\ \end{cases}

Note

The tensors condition, input, other must be broadcastable.

Parameters:
  • condition (BoolTensor) – When True (nonzero), yield input, otherwise yield other

  • input (Tensor or Scalar) – value (if input is a scalar) or values selected at indices where condition is True

  • other (Tensor or Scalar) – value (if other is a scalar) or values selected at indices where condition is False

Keyword Arguments:

out (Tensor, optional) – the output tensor.

Returns:

A tensor of shape equal to the broadcasted shape of condition, input, other

Return type:

Tensor

Example:

>>> x = tensorplay.randn(3, 2)
>>> y = tensorplay.ones(3, 2)
>>> x
tensor([[-0.4620,  0.3139],
        [ 0.3898, -0.7197],
        [ 0.0478, -0.1657]])
>>> tensorplay.where(x > 0, 1.0, 0.0)
tensor([[0., 1.],
        [1., 0.],
        [1., 0.]])
>>> tensorplay.where(x > 0, x, y)
tensor([[ 1.0000,  0.3139],
        [ 0.3898,  1.0000],
        [ 0.0478,  1.0000]])
>>> x = tensorplay.randn(2, 2, dtype=tensorplay.double)
>>> x
tensor([[ 1.0779,  0.0383],
        [-0.8785, -1.1089]], dtype=tensorplay.float64)
>>> tensorplay.where(x > 0, x, 0.)
tensor([[1.0779, 0.0383],
        [0.0000, 0.0000]], dtype=tensorplay.float64)
tensorplay.functional.where(condition) → tuple of LongTensor[source]

tensorplay.where(condition) is identical to tensorplay.nonzero(condition, as_tuple=True).

Note

See also tensorplay.nonzero().

#

zeros_like

functionFull reference ↗
tensorplay.functional.zeros_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=tensorplay.preserve_format) → Tensor

Returns a tensor filled with the scalar value 0, with the same size as input. tensorplay.zeros_like(input) is equivalent to tensorplay.zeros(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).

Warning

As of 0.4, this function does not support an out keyword. As an alternative, the old tensorplay.zeros_like(input, out=output) is equivalent to tensorplay.zeros(input.size(), out=output).

Parameters:

input (Tensor) – the size of input will determine size of the output tensor.

Keyword Arguments:
  • dtype (tensorplay.dtype, optional) – the desired data type of returned Tensor. Default: if None, defaults to the dtype of input.

  • layout (tensorplay.layout, optional) – the desired layout of returned tensor. Default: if None, defaults to the layout of input.

  • device (tensorplay.device, optional) – the desired device of returned tensor. Default: if None, defaults to the device of input.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

  • memory_format (tensorplay.memory_format, optional) – the desired memory format of returned Tensor. Default: tensorplay.preserve_format.

Example:

>>> input = tensorplay.empty(2, 3)
>>> tensorplay.zeros_like(input)
tensor([[ 0.,  0.,  0.],
        [ 0.,  0.,  0.]])

On this page

Functions 605abs_absabsoluteacos_acosacosh_acoshadaptive_avg_pool1dadaptive_max_pool1daddaddbmmaddcdivaddcmuladdmmaddmv_addmvaddradjointaffine_grid_generatorairy_aialias_copyallallclosealpha_dropout_alpha_dropoutamaxaminaminmaxangleanyarangearccos_arccosarccosh_arccosharcsin_arcsinarcsinh_arcsinharctan_arctanarctan2arctanh_arctanhargmaxargminargsortargwhereas_strided_as_strided_copyas_strided_scatteras_stridedasin_asinasinh_asinhatan_atanatan2atanh_atanhatleast_1datleast_2datleast_3davg_pool1dbaddbmmbartlett_windowbatch_norm_backward_elemtbatch_norm_backward_reducebatch_norm_elemtbatch_norm_gather_stats_with_countsbatch_norm_gather_statsbatch_norm_statsbatch_norm_update_statsbernoullibessel_j0bessel_j1bessel_y0bessel_y1bilinearbincountbinomialbitwise_andbitwise_left_shiftbitwise_notbitwise_orbitwise_right_shiftbitwise_xorblackman_windowbmmbroadcast_tobucketizecan_castcartesian_prodcatccol_indices_copycdistceil_ceilcelu_chain_matmulchannel_shufflechebyshev_polynomial_tchebyshev_polynomial_uchebyshev_polynomial_vchebyshev_polynomial_wcholesky_inversecholesky_solvechoose_qparams_optimizedchunkclamp_clamp_max_clamp_min_clampclip_clipclonecol_indices_copycolumn_stackcombinationscomplexconcatconcatenateconj_physical_conj_physicalconjconv_tbcconv_transpose1dconv_transpose2dconv_transpose3dconv1dconv3dconvolutioncopysigncorrcoefcos_coscosh_coshcosine_embedding_losscosine_similaritycount_nonzerocovcrosscrow_indices_copyctc_losscudnn_affine_grid_generatorcudnn_batch_normcudnn_convolution_add_relucudnn_convolution_relucudnn_convolution_transposecudnn_convolutioncudnn_grid_samplercudnn_is_acceptablecummaxcummincumprodcumsumcumulative_trapezoiddeg2rad_deg2raddequantizedetdetach_detach_copydetachdiag_embeddiagdiagflatdiagonal_copydiagonal_scatterdiagonaldiffdigammadistdivdividedotdropout_dsplitdstackembedding_renorm_embeddingempty_likeempty_permutedempty_quantizedempty_stridedentreqequalerf_erferfc_erfcerfcxerfinvexp_expexp2_exp2expand_copyexpm1_expm1eyefake_quantize_per_channel_affinefake_quantize_per_tensor_affinefbgemm_linear_fp16_weight_fp32_activationfbgemm_linear_fp16_weightfbgemm_linear_int8_weight_fp32_activationfbgemm_linear_int8_weightfbgemm_linear_quantize_weightfbgemm_pack_gemm_matrix_fp16fbgemm_pack_quantized_matrixfeature_alpha_dropout_feature_alpha_dropoutfeature_dropout_feature_dropoutfill_fillfix_fixflattenflipfliplrflipudfloat_powerfloor_floor_dividefloorfmaxfminfmodfrac_fracfrexpfrom_filefull_likefused_moving_avg_obs_fake_quantgammaincgammainccgathergcd_gcdgegeqrfgerget_devicegreater_equalgreatergrid_sampler_2dgrid_sampler_3dgroup_normgru_cellgrugthamming_windowhann_windowhardshrinkhash_tensorheavisidehermite_polynomial_hhermite_polynomial_hehinge_embedding_losshistchistogramddhsplithstackhypoti0_i0i0ei1i1eigammaigammacimagindex_addindex_copyindex_put_index_reduceindex_selectindices_copyinnerinstance_normint_reprinverseis_complexis_conjis_distributedis_floating_pointis_inferenceis_negis_nonzerois_same_sizeis_signedis_vulkan_availableiscloseisfiniteisinisinfisnanisneginfisposinfisrealistftkaiser_windowkl_divkronkthvaluelaguerre_polynomial_llcm_lcmldexp_ldexplelegendre_polynomial_plerpless_equallesslgammalinspacelog_log_ndtrloglog10_log10log1p_log1plog2_log2logaddexplogaddexp2logcumsumexplogdetlogical_andlogical_notlogical_orlogical_xorlogit_logitlogspacelogsumexplstm_celllstmltlu_solvelu_unpackmargin_ranking_lossmasked_fillmasked_selectmatmulmatrix_expmatrix_powermax_pool1dmax_pool3dmaximummeanmedianmeshgridminimummiopen_batch_normmiopen_convolution_add_relumiopen_convolution_relumiopen_convolution_transposemiopen_convolutionmiopen_ctc_lossmiopen_depthwise_convolutionmiopen_rnnmkldnn_adaptive_avg_pool2dmkldnn_convolutionmkldnn_linear_backward_weightsmkldnn_max_pool2dmkldnn_max_pool3dmkldnn_rnn_layermmmodemodified_bessel_i0modified_bessel_i1modified_bessel_k0modified_bessel_k1moveaxismovedimmsortmulmultinomialmultiplymvmvlgammanan_to_num_nan_to_numnanmeannanmediannansumnarrow_copynarrownative_batch_normnative_channel_shufflenative_group_normnative_layer_normnative_normndtrndtrineneg_negnegative_negativenextafternonzero_staticnonzeronorm_except_dimnormnormalnot_equalnuclear_normnumelones_likeorgqrormqrouterpairwise_distancepdistpermute_copypermutepinversepixel_unshufflepoisson_nll_losspoissonpolarpolygammapositivepowpreluprodpromote_typesputq_per_channel_axisq_per_channel_scalesq_per_channel_zero_pointsq_scaleq_zero_pointquantize_per_channelquantize_per_tensor_dynamicquantize_per_tensorquantized_batch_normquantized_gru_cellquantized_lstm_cellquantized_max_pool1dquantized_max_pool2dquantized_max_pool3dquantized_rnn_relu_cellquantized_rnn_tanh_cellrad2deg_rad2degrand_likerandint_likerandintrandn_likerandpermrangeravelrealreciprocal_reciprocalrelu_remainderrenormrepeat_interleavereshaperesize_as_resize_as_sparse_resolve_conjresolve_negresult_typerms_normrnn_relu_cellrnn_relurnn_tanh_cellrnn_tanhrollrot90round_roundrow_indices_copyrow_stackrrelu_rrelursqrt_rsqrtrsubscalar_tensorscaled_modified_bessel_k0scaled_modified_bessel_k1scatter_addscatter_reducescattersearchsortedsegment_reduceselect_copyselect_scatterselectselu_selusgnshifted_chebyshev_polynomial_tshifted_chebyshev_polynomial_ushifted_chebyshev_polynomial_vshifted_chebyshev_polynomial_wsigmoid_sigmoidsignsignbitsin_sinsinc_sincsinh_sinhslice_copyslice_inverseslice_scatterslogdetsoftmaxsortsparse_bsc_tensorsparse_bsr_tensorsparse_coo_tensorsparse_csc_tensorsparse_csr_tensorspherical_bessel_j0split_copysplit_with_sizes_copysplitsqrt_sqrtsquare_squaresqueeze_copysqueezestackstd_meanstdstftsubsubtractsumsvdswapaxesswapdimssym_constrain_range_for_sizesym_constrain_ranget_copyttake_along_dimtaketan_tantanh_tanhtensor_splitthreshold_thresholdtiletopktracetranspose_copytransposetrapezoidtrapztriangular_solvetril_indicestriltriplet_margin_losstriu_indicestriutrue_dividetrunc_truncunbind_copyunbindunflattenunfold_copyunique_consecutiveuniqueunsqueeze_copyunsqueezevalues_copyvandervar_meanvarvdotview_as_complex_copyview_as_complexview_as_real_copyview_as_realview_copyvsplitvstackwherexlog1pyxlogy_xlogyzero_zeros_likezeta
Ask DeepWiki