Skip to content

torch

Top-level functions in the torch namespace.

Contents

Tensors

FunctionDescription
is_tensorReturns True if obj is a PyTorch tensor.
is_nonzeroReturns True if the input is a single element tensor which is not equal to zero after type conversions.
numelReturns the total number of elements in the input tensor.

Creation Ops

FunctionDescription
tensorCreate a tensor from data.
zerosReturns a tensor filled with the scalar value 0, with the shape defined by the variable argument size.
zeros_likeReturns a tensor filled with the scalar value 0, with the same shape as input
onesReturns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.
ones_likeReturns a tensor filled with the scalar value 1, with the same shape as input
emptyReturns an uninitialized tensor.
empty_likeReturns an uninitialized tensor with the same shape as input
fullReturns a tensor filled with the scalar value, with the shape defined by the variable argument size.
full_likeReturns a tensor filled with the scalar value, with the same shape as input
arangeReturns a 1-D tensor of size $$ \lceil \frac{\text{end} - \text{start}}{\text{step}} \rceil $$ with values from start to end with step step.
linspaceCreates a one-dimensional tensor of size steps whose values are evenly spaced from start to end, inclusive.

Random sampling

FunctionDescription
seedSets the seed for generating random numbers to a non-deterministic random number.
manual_seedSets the seed for generating random numbers.
randReturns a tensor filled with random numbers from a uniform distribution on the interval $$ [0, 1) $$.
rand_likeReturns a tensor with the same size as input that is filled with random numbers from a uniform distribution on the interval $$ [0, 1) $$.
randintReturns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive).
randint_likeReturns a tensor with the same shape as input that is filled with random integers generated uniformly between low (inclusive) and high (exclusive).
randnReturns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution).
randn_likeReturns a tensor with the same size as input that is filled with random numbers from a normal distribution with mean 0 and variance 1.
randpermReturns a tensor containing a random permutation of integers in the interval $$ [0, n) $$.

Locally disabling gradient computation

The context manager torch.no_grad() is helpful for locally disabling and enabling gradient computation.

python
>>> x = torch.zeros(1, requires_grad=True)
>>> with torch.no_grad():
...     y = x * 2
>>> y.requires_grad
False
FunctionDescription
no_gradContext manager that disables gradient computation.
is_grad_enabledReturns True if grad mode is currently enabled.

Math operations

FunctionDescription
absComputes the absolute value of each element in input.
addAdds other to input.
subSubtracts other from input.
mulMultiplies input by other.
divDivides input by other.
powTakes the power of each element in input with other.
matmulMatrix product of two tensors.
sumReturns the sum of all elements in the input tensor.
meanReturns the mean value of all elements in the input tensor.
maxReturns the maximum value of all elements in the input tensor.
minReturns the minimum value of all elements in the input tensor.
sinReturns a new tensor with the sine of the elements of input.
cosReturns a new tensor with the cosine of the elements of input.
tanReturns a new tensor with the tangent of the elements of input.
expReturns a new tensor with the exponential of the elements of the input tensor.
logReturns a new tensor with the natural logarithm of the elements of input.
sqrtReturns a new tensor with the square-root of the elements of input.
maximumComputes the element-wise maximum of input and other.
minimumComputes the element-wise minimum of input and other.
allcloseThis function checks if all input and other satisfy the condition.
catConcatenates the given sequence of seq tensors in the given dimension.

Operations

torch.reshape PyTorch

python
torch.reshape(input, *args) -> Tensor

Returns a tensor with the same data and number of elements as input but with the specified shape.

python
>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.reshape(x, (2, 2))
tensor([[1, 2], [3, 4]])

torch.squeeze PyTorch

python
torch.squeeze(input, dim=None) -> Tensor

Returns a tensor with all specified dimensions of input of size 1 removed.

python
>>> x = torch.tensor([[[1], [2]]])
>>> torch.squeeze(x)
tensor([1, 2])

torch.unsqueeze PyTorch

python
torch.unsqueeze(input, dim) -> Tensor

Returns a new tensor with a dimension of size one inserted at the specified position.

python
>>> x = torch.tensor([1, 2])
>>> torch.unsqueeze(x, 0)
tensor([[1, 2]])

torch.transpose PyTorch

python
torch.transpose(input, dim0, dim1) -> Tensor

Returns a tensor that is a transposed version of input.

python
>>> x = torch.tensor([[1, 2], [3, 4]])
>>> torch.transpose(x, 0, 1)
tensor([[1, 3], [2, 4]])

torch.flatten PyTorch

python
torch.flatten(input, start_dim=0, end_dim=-1) -> Tensor

Flattens input tensor by reshaping it into a one-dimensional tensor.

python
>>> x = torch.tensor([[1, 2], [3, 4]])
>>> torch.flatten(x)
tensor([1, 2, 3, 4])

torch.sum PyTorch

python
torch.sum(input, dim=None, keepdim=False) -> Tensor

Returns the sum of all elements in the tensor.

python
>>> x = torch.tensor([1., 2.])
>>> torch.sum(x)
tensor(3)

torch.mean PyTorch

python
torch.mean(input, dim=None, keepdim=False) -> Tensor

Returns the mean value of all elements in the tensor.

python
>>> x = torch.tensor([1., 2.])
>>> torch.mean(x)
tensor(1.5)

torch.max PyTorch

python
torch.max(input, dim=None, keepdim=False) -> Tensor

Returns the maximum value of all elements in the tensor.

python
>>> x = torch.tensor([1., 2.])
>>> torch.max(x)
tensor(2)

torch.min PyTorch

python
torch.min(input, dim=None, keepdim=False) -> Tensor

Returns the minimum value of all elements in the tensor.

python
>>> x = torch.tensor([1., 2.])
>>> torch.min(x)
tensor(1)

torch.add PyTorch

python
torch.add(input, other) -> Tensor

Adds other to input.

python
>>> x = torch.tensor([1, 2])
>>> torch.add(x, 3)
tensor([4, 5])

torch.sub PyTorch

python
torch.sub(input, other) -> Tensor

Subtracts other from input.

python
>>> x = torch.tensor([1, 2])
>>> torch.sub(x, 3)
tensor([-2, -1])

torch.mul PyTorch

python
torch.mul(input, other) -> Tensor

Multiplies input by other.

python
>>> x = torch.tensor([1, 2])
>>> torch.mul(x, 3)
tensor([3, 6])

torch.div PyTorch

python
torch.div(input, other) -> Tensor

Divides input by other.

python
>>> x = torch.tensor([1., 2.])
>>> torch.div(x, 2)
tensor([0.5, 1])

torch.pow PyTorch

python
torch.pow(input, other) -> Tensor

Takes the power of each element in input with other.

python
>>> x = torch.tensor([2., 3.])
>>> torch.pow(x, 2)
tensor([4, 9])

torch.matmul PyTorch

python
torch.matmul(input, other) -> Tensor

Matrix product of two tensors.

python
>>> x = torch.tensor([[1, 2]])
>>> y = torch.tensor([[3], [4]])
>>> torch.matmul(x, y)
tensor([[11]])

torch.neg PyTorch

python
torch.neg(input) -> Tensor

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

python
>>> x = torch.tensor([1, -2])
>>> torch.neg(x)
tensor([-1, 2])

torch.abs PyTorch

python
torch.abs(input) -> Tensor

Computes the element-wise absolute value of the given input tensor.

python
>>> x = torch.tensor([1, -2])
>>> torch.abs(x)
tensor([1, 2])

torch.log PyTorch

python
torch.log(input) -> Tensor

Returns a new tensor with the natural logarithm of the elements of input.

python
>>> x = torch.tensor([1., 2.])
>>> torch.log(x)
tensor([0, 0.6931471805599453])

torch.exp PyTorch

python
torch.exp(input) -> Tensor

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

python
>>> x = torch.tensor([1., 2.])
>>> torch.exp(x)
tensor([2.718281828459045, 7.38905609893065])

torch.sqrt PyTorch

python
torch.sqrt(input) -> Tensor

Returns a new tensor with the square-root of the elements of input.

python
>>> x = torch.tensor([1., 4.])
>>> torch.sqrt(x)
tensor([1, 2])

torch.square PyTorch

python
torch.square(input) -> Tensor

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

python
>>> x = torch.tensor([1., 4.])
>>> torch.square(x)
tensor([1, 16])

torch.sin PyTorch

python
torch.sin(input) -> Tensor

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

python
>>> x = torch.tensor([0., 3.14])
>>> torch.sin(x)
tensor([0, 0.0015926529164868282])

torch.cos PyTorch

python
torch.cos(input) -> Tensor

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

python
>>> x = torch.tensor([0., 3.14])
>>> torch.cos(x)
tensor([1, -0.9999987317275395])

torch.tan PyTorch

python
torch.tan(input) -> Tensor

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

python
>>> x = torch.tensor([0., 3.14])
>>> torch.tan(x)
tensor([0, -0.001592654936407223])

torch.sigmoid PyTorch

python
torch.sigmoid(input) -> Tensor

Applies the sigmoid function element-wise.

python
>>> x = torch.tensor([0., 1.])
>>> torch.sigmoid(x)
tensor([0.5, 0.7310585786300049])

torch.relu PyTorch

python
torch.relu(input) -> Tensor

Applies the rectified linear unit function element-wise: max(0, x).

python
>>> x = torch.tensor([-1., 0., 1.])
>>> torch.relu(x)
tensor([0, 0, 1])

torch.sign PyTorch

python
torch.sign(input) -> Tensor

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

python
>>> x = torch.tensor([-1., 0., 1.])
>>> torch.sign(x)
tensor([-1, 0, 1])

torch.reciprocal PyTorch

python
torch.reciprocal(input) -> Tensor

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

python
>>> x = torch.tensor([2., 4.])
>>> torch.reciprocal(x)
tensor([0.5, 0.25])

torch.nan_to_num PyTorch

python
torch.nan_to_num(input) -> Tensor

Replaces NaN, positive infinity, and negative infinity values in input with the corresponding replacement values.

python
>>> x = torch.tensor([1., float('nan')])
>>> torch.nan_to_num(x)
tensor([1, 0])

torch.lt PyTorch

python
torch.lt(input, other) -> Tensor

Computes self < other element-wise.

python
>>> x = torch.tensor([1, 2])
>>> torch.lt(x, 2)
tensor([1, 0])

torch.gt PyTorch

python
torch.gt(input, other) -> Tensor

Computes self > other element-wise.

python
>>> x = torch.tensor([1, 2])
>>> torch.gt(x, 1)
tensor([0, 1])

torch.le PyTorch

python
torch.le(input, other) -> Tensor

Computes self <= other element-wise.

python
>>> x = torch.tensor([1, 2])
>>> torch.le(x, 1)
tensor([1, 0])

torch.ge PyTorch

python
torch.ge(input, other) -> Tensor

Computes self >= other element-wise.

python
>>> x = torch.tensor([1, 2])
>>> torch.ge(x, 2)
tensor([0, 1])

torch.eq PyTorch

python
torch.eq(input, other) -> Tensor

Computes self == other element-wise.

python
>>> x = torch.tensor([1, 2])
>>> torch.eq(x, 2)
tensor([0, 1])

torch.ne PyTorch

python
torch.ne(input, other) -> Tensor

Computes self != other element-wise.

python
>>> x = torch.tensor([1, 2])
>>> torch.ne(x, 2)
tensor([1, 0])

torch.allclose PyTorch

python
torch.allclose(input, other, rtol=1e-5, atol=1e-8, equal_nan=False) -> bool

This function checks if input and other satisfy the condition.

python
>>> x = torch.tensor([1., 2.])
>>> y = torch.tensor([1.00001, 2.])
>>> torch.allclose(x, y)
True