torch
Top-level functions in the torch namespace.
Contents
Tensors
| Function | Description |
|---|---|
is_tensor | Returns True if obj is a PyTorch tensor. |
is_nonzero | Returns True if the input is a single element tensor which is not equal to zero after type conversions. |
numel | Returns the total number of elements in the input tensor. |
Creation Ops
| Function | Description |
|---|---|
tensor | Create a tensor from data. |
zeros | Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size. |
zeros_like | Returns a tensor filled with the scalar value 0, with the same shape as input |
ones | Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size. |
ones_like | Returns a tensor filled with the scalar value 1, with the same shape as input |
empty | Returns an uninitialized tensor. |
empty_like | Returns an uninitialized tensor with the same shape as input |
full | Returns a tensor filled with the scalar value, with the shape defined by the variable argument size. |
full_like | Returns a tensor filled with the scalar value, with the same shape as input |
arange | Returns a 1-D tensor of size $$ \lceil \frac{\text{end} - \text{start}}{\text{step}} \rceil $$ with values from start to end with step step. |
linspace | Creates a one-dimensional tensor of size steps whose values are evenly spaced from start to end, inclusive. |
Random sampling
| Function | Description |
|---|---|
seed | Sets the seed for generating random numbers to a non-deterministic random number. |
manual_seed | Sets the seed for generating random numbers. |
rand | Returns a tensor filled with random numbers from a uniform distribution on the interval $$ [0, 1) $$. |
rand_like | Returns a tensor with the same size as input that is filled with random numbers from a uniform distribution on the interval $$ [0, 1) $$. |
randint | Returns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive). |
randint_like | Returns a tensor with the same shape as input that is filled with random integers generated uniformly between low (inclusive) and high (exclusive). |
randn | Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution). |
randn_like | 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. |
randperm | Returns 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.
>>> x = torch.zeros(1, requires_grad=True)
>>> with torch.no_grad():
... y = x * 2
>>> y.requires_grad
False| Function | Description |
|---|---|
no_grad | Context manager that disables gradient computation. |
is_grad_enabled | Returns True if grad mode is currently enabled. |
Math operations
| Function | Description |
|---|---|
abs | Computes the absolute value of each element in input. |
add | Adds other to input. |
sub | Subtracts other from input. |
mul | Multiplies input by other. |
div | Divides input by other. |
pow | Takes the power of each element in input with other. |
matmul | Matrix product of two tensors. |
sum | Returns the sum of all elements in the input tensor. |
mean | Returns the mean value of all elements in the input tensor. |
max | Returns the maximum value of all elements in the input tensor. |
min | Returns the minimum value of all elements in the input tensor. |
sin | Returns a new tensor with the sine of the elements of input. |
cos | Returns a new tensor with the cosine of the elements of input. |
tan | Returns a new tensor with the tangent of the elements of input. |
exp | Returns a new tensor with the exponential of the elements of the input tensor. |
log | Returns a new tensor with the natural logarithm of the elements of input. |
sqrt | Returns a new tensor with the square-root of the elements of input. |
maximum | Computes the element-wise maximum of input and other. |
minimum | Computes the element-wise minimum of input and other. |
allclose | This function checks if all input and other satisfy the condition. |
cat | Concatenates the given sequence of seq tensors in the given dimension. |
Operations
torch.reshape PyTorch↗
torch.reshape(input, *args) -> TensorReturns a tensor with the same data and number of elements as input but with the specified shape.
>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.reshape(x, (2, 2))
tensor([[1, 2], [3, 4]])torch.squeeze PyTorch↗
torch.squeeze(input, dim=None) -> TensorReturns a tensor with all specified dimensions of input of size 1 removed.
>>> x = torch.tensor([[[1], [2]]])
>>> torch.squeeze(x)
tensor([1, 2])torch.unsqueeze PyTorch↗
torch.unsqueeze(input, dim) -> TensorReturns a new tensor with a dimension of size one inserted at the specified position.
>>> x = torch.tensor([1, 2])
>>> torch.unsqueeze(x, 0)
tensor([[1, 2]])torch.transpose PyTorch↗
torch.transpose(input, dim0, dim1) -> TensorReturns a tensor that is a transposed version of input.
>>> x = torch.tensor([[1, 2], [3, 4]])
>>> torch.transpose(x, 0, 1)
tensor([[1, 3], [2, 4]])torch.flatten PyTorch↗
torch.flatten(input, start_dim=0, end_dim=-1) -> TensorFlattens input tensor by reshaping it into a one-dimensional tensor.
>>> x = torch.tensor([[1, 2], [3, 4]])
>>> torch.flatten(x)
tensor([1, 2, 3, 4])torch.sum PyTorch↗
torch.sum(input, dim=None, keepdim=False) -> TensorReturns the sum of all elements in the tensor.
>>> x = torch.tensor([1., 2.])
>>> torch.sum(x)
tensor(3)torch.mean PyTorch↗
torch.mean(input, dim=None, keepdim=False) -> TensorReturns the mean value of all elements in the tensor.
>>> x = torch.tensor([1., 2.])
>>> torch.mean(x)
tensor(1.5)torch.max PyTorch↗
torch.max(input, dim=None, keepdim=False) -> TensorReturns the maximum value of all elements in the tensor.
>>> x = torch.tensor([1., 2.])
>>> torch.max(x)
tensor(2)torch.min PyTorch↗
torch.min(input, dim=None, keepdim=False) -> TensorReturns the minimum value of all elements in the tensor.
>>> x = torch.tensor([1., 2.])
>>> torch.min(x)
tensor(1)torch.add PyTorch↗
torch.add(input, other) -> TensorAdds other to input.
>>> x = torch.tensor([1, 2])
>>> torch.add(x, 3)
tensor([4, 5])torch.sub PyTorch↗
torch.sub(input, other) -> TensorSubtracts other from input.
>>> x = torch.tensor([1, 2])
>>> torch.sub(x, 3)
tensor([-2, -1])torch.mul PyTorch↗
torch.mul(input, other) -> TensorMultiplies input by other.
>>> x = torch.tensor([1, 2])
>>> torch.mul(x, 3)
tensor([3, 6])torch.div PyTorch↗
torch.div(input, other) -> TensorDivides input by other.
>>> x = torch.tensor([1., 2.])
>>> torch.div(x, 2)
tensor([0.5, 1])torch.pow PyTorch↗
torch.pow(input, other) -> TensorTakes the power of each element in input with other.
>>> x = torch.tensor([2., 3.])
>>> torch.pow(x, 2)
tensor([4, 9])torch.matmul PyTorch↗
torch.matmul(input, other) -> TensorMatrix product of two tensors.
>>> x = torch.tensor([[1, 2]])
>>> y = torch.tensor([[3], [4]])
>>> torch.matmul(x, y)
tensor([[11]])torch.neg PyTorch↗
torch.neg(input) -> TensorReturns a new tensor with the negative of the elements of input.
>>> x = torch.tensor([1, -2])
>>> torch.neg(x)
tensor([-1, 2])torch.abs PyTorch↗
torch.abs(input) -> TensorComputes the element-wise absolute value of the given input tensor.
>>> x = torch.tensor([1, -2])
>>> torch.abs(x)
tensor([1, 2])torch.log PyTorch↗
torch.log(input) -> TensorReturns a new tensor with the natural logarithm of the elements of input.
>>> x = torch.tensor([1., 2.])
>>> torch.log(x)
tensor([0, 0.6931471805599453])torch.exp PyTorch↗
torch.exp(input) -> TensorReturns a new tensor with the exponential of the elements of the input tensor.
>>> x = torch.tensor([1., 2.])
>>> torch.exp(x)
tensor([2.718281828459045, 7.38905609893065])torch.sqrt PyTorch↗
torch.sqrt(input) -> TensorReturns a new tensor with the square-root of the elements of input.
>>> x = torch.tensor([1., 4.])
>>> torch.sqrt(x)
tensor([1, 2])torch.square PyTorch↗
torch.square(input) -> TensorReturns a new tensor with the square of the elements of input.
>>> x = torch.tensor([1., 4.])
>>> torch.square(x)
tensor([1, 16])torch.sin PyTorch↗
torch.sin(input) -> TensorReturns a new tensor with the sine of the elements of input.
>>> x = torch.tensor([0., 3.14])
>>> torch.sin(x)
tensor([0, 0.0015926529164868282])torch.cos PyTorch↗
torch.cos(input) -> TensorReturns a new tensor with the cosine of the elements of input.
>>> x = torch.tensor([0., 3.14])
>>> torch.cos(x)
tensor([1, -0.9999987317275395])torch.tan PyTorch↗
torch.tan(input) -> TensorReturns a new tensor with the tangent of the elements of input.
>>> x = torch.tensor([0., 3.14])
>>> torch.tan(x)
tensor([0, -0.001592654936407223])torch.sigmoid PyTorch↗
torch.sigmoid(input) -> TensorApplies the sigmoid function element-wise.
>>> x = torch.tensor([0., 1.])
>>> torch.sigmoid(x)
tensor([0.5, 0.7310585786300049])torch.relu PyTorch↗
torch.relu(input) -> TensorApplies the rectified linear unit function element-wise: max(0, x).
>>> x = torch.tensor([-1., 0., 1.])
>>> torch.relu(x)
tensor([0, 0, 1])torch.sign PyTorch↗
torch.sign(input) -> TensorReturns a new tensor with the sign of the elements of input.
>>> x = torch.tensor([-1., 0., 1.])
>>> torch.sign(x)
tensor([-1, 0, 1])torch.reciprocal PyTorch↗
torch.reciprocal(input) -> TensorReturns a new tensor with the reciprocal of the elements of input.
>>> x = torch.tensor([2., 4.])
>>> torch.reciprocal(x)
tensor([0.5, 0.25])torch.nan_to_num PyTorch↗
torch.nan_to_num(input) -> TensorReplaces NaN, positive infinity, and negative infinity values in input with the corresponding replacement values.
>>> x = torch.tensor([1., float('nan')])
>>> torch.nan_to_num(x)
tensor([1, 0])torch.lt PyTorch↗
torch.lt(input, other) -> TensorComputes self < other element-wise.
>>> x = torch.tensor([1, 2])
>>> torch.lt(x, 2)
tensor([1, 0])torch.gt PyTorch↗
torch.gt(input, other) -> TensorComputes self > other element-wise.
>>> x = torch.tensor([1, 2])
>>> torch.gt(x, 1)
tensor([0, 1])torch.le PyTorch↗
torch.le(input, other) -> TensorComputes self <= other element-wise.
>>> x = torch.tensor([1, 2])
>>> torch.le(x, 1)
tensor([1, 0])torch.ge PyTorch↗
torch.ge(input, other) -> TensorComputes self >= other element-wise.
>>> x = torch.tensor([1, 2])
>>> torch.ge(x, 2)
tensor([0, 1])torch.eq PyTorch↗
torch.eq(input, other) -> TensorComputes self == other element-wise.
>>> x = torch.tensor([1, 2])
>>> torch.eq(x, 2)
tensor([0, 1])torch.ne PyTorch↗
torch.ne(input, other) -> TensorComputes self != other element-wise.
>>> x = torch.tensor([1, 2])
>>> torch.ne(x, 2)
tensor([1, 0])torch.allclose PyTorch↗
torch.allclose(input, other, rtol=1e-5, atol=1e-8, equal_nan=False) -> boolThis function checks if input and other satisfy the condition.
>>> x = torch.tensor([1., 2.])
>>> y = torch.tensor([1.00001, 2.])
>>> torch.allclose(x, y)
True