torch¶
The torch package contains data structures for multidimensional tensors and defines mathematical operations over these tensors. Additionally, it provides many utilities for efficient serializing of Tensors and arbitrary types, and other useful utilities.
It has a CUDA counterpart, that enables you to run your tensor computations on an NVIDIA GPU with compute capability >= 3.0
Tensors¶
Returns True if obj is a PyTorch tensor. 

Returns True if obj is a PyTorch storage object. 

Returns True if the data type of 

Returns True if the data type of 

Returns True if the 

Sets the default floating point dtype to 

Get the current default floating point 

Sets the default 

Returns the total number of elements in the 

Set options for printing. 

Disables denormal floating numbers on CPU. 
Creation Ops¶
Note
Random sampling creation ops are listed under Random sampling and
include:
torch.rand()
torch.rand_like()
torch.randn()
torch.randn_like()
torch.randint()
torch.randint_like()
torch.randperm()
You may also use torch.empty()
with the Inplace random sampling
methods to create torch.Tensor
s with values sampled from a broader
range of distributions.
Constructs a tensor with 

Constructs a sparse tensor in COO(rdinate) format with specified values at the given 

Convert the data into a torch.Tensor. 

Create a view of an existing torch.Tensor 

Creates a 

Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument 

Returns a tensor filled with the scalar value 0, with the same size as 

Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument 

Returns a tensor filled with the scalar value 1, with the same size as 

Returns a 1D tensor of size $\left\lceil \frac{\text{end}  \text{start}}{\text{step}} \right\rceil$
with values from the interval 

Returns a 1D tensor of size $\left\lfloor \frac{\text{end}  \text{start}}{\text{step}} \right\rfloor + 1$
with values from 

Creates a onedimensional tensor of size 

Creates a onedimensional tensor of size 

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

Returns a tensor filled with uninitialized data. 

Returns an uninitialized tensor with the same size as 

Returns a tensor filled with uninitialized data. 

Creates a tensor of size 

Returns a tensor with the same size as 

Converts a float tensor to a quantized tensor with given scale and zero point. 

Converts a float tensor to a perchannel quantized tensor with given scales and zero points. 

Returns an fp32 Tensor by dequantizing a quantized Tensor 

Constructs a complex tensor with its real part equal to 

Constructs a complex tensor whose elements are Cartesian coordinates corresponding to the polar coordinates with absolute value 

Computes the Heaviside step function for each element in 
Indexing, Slicing, Joining, Mutating Ops¶
Concatenates the given sequence of 

Splits a tensor into a specific number of chunks. 

Creates a new tensor by horizontally stacking the tensors in 

Stack tensors in sequence depthwise (along third axis). 

Gathers values along an axis specified by dim. 

Stack tensors in sequence horizontally (column wise). 

Returns a new tensor which indexes the 

Returns a new 1D tensor which indexes the 

Moves the dimension(s) of 

Alias for 

Returns a new tensor that is a narrowed version of 

Returns a tensor with the same data and number of elements as 

Alias of 

Outofplace version of 

Outofplace version of 

Splits the tensor into chunks. 

Returns a tensor with all the dimensions of 

Concatenates a sequence of tensors along a new dimension. 

Alias for 

Alias for 

Expects 

Returns a new tensor with the elements of 

Splits a tensor into multiple subtensors, all of which are views of 

Constructs a tensor by repeating the elements of 

Returns a tensor that is a transposed version of 

Removes a tensor dimension. 

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

Stack tensors in sequence vertically (row wise). 

Return a tensor of elements selected from either 
Generators¶
Creates and returns a generator object that manages the state of the algorithm which produces pseudo random numbers. 
Random sampling¶
Sets the seed for generating random numbers to a nondeterministic random number. 

Sets the seed for generating random numbers. 

Returns the initial seed for generating random numbers as a Python long. 

Returns the random number generator state as a torch.ByteTensor. 

Sets the random number generator state. 

torch.
default_generator
Returns the default CPU torch.Generator¶
Draws binary random numbers (0 or 1) from a Bernoulli distribution. 

Returns a tensor where each row contains 

Returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given. 

Returns a tensor of the same size as 

Returns a tensor filled with random numbers from a uniform distribution on the interval $[0, 1)$ 

Returns a tensor with the same size as 

Returns a tensor filled with random integers generated uniformly between 

Returns a tensor with the same shape as Tensor 

Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution). 

Returns a tensor with the same size as 

Returns a random permutation of integers from 
Inplace random sampling¶
There are a few more inplace random sampling functions defined on Tensors as well. Click through to refer to their documentation:
torch.Tensor.bernoulli_()
 inplace version oftorch.bernoulli()
torch.Tensor.cauchy_()
 numbers drawn from the Cauchy distributiontorch.Tensor.exponential_()
 numbers drawn from the exponential distributiontorch.Tensor.geometric_()
 elements drawn from the geometric distributiontorch.Tensor.log_normal_()
 samples from the lognormal distributiontorch.Tensor.normal_()
 inplace version oftorch.normal()
torch.Tensor.random_()
 numbers sampled from the discrete uniform distributiontorch.Tensor.uniform_()
 numbers sampled from the continuous uniform distribution
Quasirandom sampling¶
The 
Serialization¶
Saves an object to a disk file. 

Loads an object saved with 
Parallelism¶
Returns the number of threads used for parallelizing CPU operations 

Sets the number of threads used for intraop parallelism on CPU. 

Returns the number of threads used for interop parallelism on CPU (e.g. 

Sets the number of threads used for interop parallelism (e.g. 
Locally disabling gradient computation¶
The context managers torch.no_grad()
, torch.enable_grad()
, and
torch.set_grad_enabled()
are helpful for locally disabling and enabling
gradient computation. See Locally disabling gradient computation for more details on
their usage. These context managers are thread local, so they won’t
work if you send work to another thread using the threading
module, etc.
Examples:
>>> x = torch.zeros(1, requires_grad=True)
>>> with torch.no_grad():
... y = x * 2
>>> y.requires_grad
False
>>> is_train = False
>>> with torch.set_grad_enabled(is_train):
... y = x * 2
>>> y.requires_grad
False
>>> torch.set_grad_enabled(True) # this can also be used as a function
>>> y = x * 2
>>> y.requires_grad
True
>>> torch.set_grad_enabled(False)
>>> y = x * 2
>>> y.requires_grad
False
Contextmanager that disabled gradient calculation. 

Contextmanager that enables gradient calculation. 

Contextmanager that sets gradient calculation to on or off. 
Math operations¶
Pointwise Ops¶
Computes the absolute value of each element in 

Alias for 

Computes the inverse cosine of each element in 

Alias for 

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

Alias for 

Adds the scalar 

Performs the elementwise division of 

Performs the elementwise multiplication of 

Computes the elementwise angle (in radians) of the given 

Returns a new tensor with the arcsine of the elements of 

Alias for 

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

Alias for 

Returns a new tensor with the arctangent of the elements of 

Alias for 

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

Alias for 

Elementwise arctangent of $\text{input}_{i} / \text{other}_{i}$ with consideration of the quadrant. 

Computes the bitwise NOT of the given input tensor. 

Computes the bitwise AND of 

Computes the bitwise OR of 

Computes the bitwise XOR of 

Returns a new tensor with the ceil of the elements of 

Alias for 

Computes the elementwise conjugate of the given 

Create a new floatingpoint tensor with the magnitude of 

Returns a new tensor with the cosine of the elements of 

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

Returns a new tensor with each of the elements of 

Divides each element of the input 

Alias for 

Computes the logarithmic derivative of the gamma function on input. 

Computes the error function of each element. 

Computes the complementary error function of each element of 

Computes the inverse error function of each element of 

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

Computes the base two exponential function of 

Returns a new tensor with the exponential of the elements minus 1 of 

Alias for 

Raises 

Returns a new tensor with the floor of the elements of 

Computes the elementwise remainder of division. 

Computes the fractional portion of each element in 

Returns a new tensor containing imaginary values of the 

Multiplies 

Does a linear interpolation of two tensors 

Computes the logarithm of the gamma function on 

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

Returns a new tensor with the logarithm to the base 10 of the elements of 

Returns a new tensor with the natural logarithm of (1 + 

Returns a new tensor with the logarithm to the base 2 of the elements of 

Logarithm of the sum of exponentiations of the inputs. 

Logarithm of the sum of exponentiations of the inputs in base2. 

Computes the elementwise logical AND of the given input tensors. 

Computes the elementwise logical NOT of the given input tensor. 

Computes the elementwise logical OR of the given input tensors. 

Computes the elementwise logical XOR of the given input tensors. 

Returns a new tensor with the logit of the elements of 

Given the legs of a right triangle, return its hypotenuse. 

Computes the zeroth order modified Bessel function of the first kind for each element of 

Computes the regularized lower incomplete gamma function: 

Computes the regularized upper incomplete gamma function: 

Multiplies each element of the input 

Alias for 

Computes the multivariate loggamma function) with dimension $p$ elementwise, given by 

Replaces 

Returns a new tensor with the negative of the elements of 

Alias for 

Return the next floatingpoint value after 

Computes the $n^{th}$
derivative of the digamma function on 

Takes the power of each element in 

Returns a new tensor with each of the elements of 

Returns a new tensor containing real values of the 

Returns a new tensor with the reciprocal of the elements of 

Computes the elementwise remainder of division. 

Returns a new tensor with each of the elements of 

Returns a new tensor with the reciprocal of the squareroot of each of the elements of 

Returns a new tensor with the sigmoid of the elements of 

Returns a new tensor with the signs of the elements of 

Tests if each element of 

Returns a new tensor with the sine of the elements of 

Computes the normalized sinc of 

Returns a new tensor with the hyperbolic sine of the elements of 

Returns a new tensor with the squareroot of the elements of 

Returns a new tensor with the square of the elements of 

Subtracts 

Alias for 

Returns a new tensor with the tangent of the elements of 

Returns a new tensor with the hyperbolic tangent of the elements of 

Alias for 

Returns a new tensor with the truncated integer values of the elements of 

Computes 
Reduction Ops¶
Returns the indices of the maximum value of all elements in the 

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

Returns the maximum value of each slice of the 

Returns the minimum value of each slice of the 

Tests if all elements in 



Returns the maximum value of all elements in the 

Returns the minimum value of all elements in the 

Returns the pnorm of ( 

Returns the log of summed exponentials of each row of the 

Returns the mean value of all elements in the 

Returns the median of the values in 

Returns the median of the values in 

Returns a namedtuple 

Returns the matrix norm or vector norm of a given tensor. 

Returns the sum of all elements, treating Not a Numbers (NaNs) as zero. 

Returns the product of all elements in the 

Returns the qth quantiles of all elements in the 

This is a variant of 

Returns the standarddeviation of all elements in the 

Returns the standarddeviation and mean of all elements in the 

Returns the sum of all elements in the 

Returns the unique elements of the input tensor. 

Eliminates all but the first element from every consecutive group of equivalent elements. 

Returns the variance of all elements in the 

Returns the variance and mean of all elements in the 

Counts the number of nonzero values in the tensor 
Comparison Ops¶
This function checks if all 

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

Computes elementwise equality 



Computes $\text{input} \geq \text{other}$ elementwise. 

Alias for 

Computes $\text{input} > \text{other}$ elementwise. 

Alias for 

Returns a new tensor with boolean elements representing if each element of 

Returns a new tensor with boolean elements representing if each element is finite or not. 

Tests if each element of 

Tests if each element of 

Tests if each element of 

Returns a new tensor with boolean elements representing if each element of 

Returns a new tensor with boolean elements representing if each element of 

Returns a namedtuple 

Computes $\text{input} \leq \text{other}$ elementwise. 

Alias for 

Computes $\text{input} < \text{other}$ elementwise. 

Alias for 

Computes the elementwise maximum of 

Computes the elementwise minimum of 

Computes $\text{input} \neq \text{other}$ elementwise. 

Alias for 

Sorts the elements of the 

Returns the 

Sorts the elements of the 
Spectral Ops¶
Shorttime Fourier transform (STFT). 

Inverse short time Fourier Transform. 

Bartlett window function. 

Blackman window function. 

Hamming window function. 

Hann window function. 

Computes the Kaiser window with window length 
Other Operations¶
Returns a 1dimensional view of each input tensor with zero dimensions. 

Returns a 2dimensional view of each each input tensor with zero dimensions. 

Returns a 3dimensional view of each each input tensor with zero dimensions. 

Count the frequency of each value in an array of nonnegative ints. 

Create a block diagonal matrix from provided tensors. 

Broadcasts the given tensors according to Broadcasting semantics. 

Broadcasts 

Similar to 

Returns the indices of the buckets to which each value in the 

Do cartesian product of the given sequence of tensors. 

Computes batched the pnorm distance between each pair of the two collections of row vectors. 

Returns a copy of 

Compute combinations of length $r$ of the given tensor. 

Returns the cross product of vectors in dimension 

Returns a namedtuple 

Returns a namedtuple 

Returns the cumulative product of elements of 

Returns the cumulative sum of elements of 



Creates a tensor whose diagonals of certain 2D planes (specified by 



Returns a partial view of 

Sums the product of the elements of the input 

Flattens 

Reverse the order of a nD tensor along given axis in dims. 

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

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

Computes the Kronecker product, denoted by $\otimes$
, of 

Rotate a nD tensor by 90 degrees in the plane specified by dims axis. 

Computes the elementwise greatest common divisor (GCD) of 

Computes the histogram of a tensor. 

Take $N$ tensors, each of which can be either scalar or 1dimensional vector, and create $N$ Ndimensional grids, where the $i$ ^{th} grid is defined by expanding the $i$ ^{th} input over dimensions defined by other inputs. 

Computes the elementwise least common multiple (LCM) of 

Returns the logarithm of the cumulative summation of the exponentiation of elements of 

Return a contiguous flattened tensor. 

Returns a tensor where each subtensor of 

Repeat elements of a tensor. 

Roll the tensor along the given dimension(s). 

Find the indices from the innermost dimension of 

Returns a contraction of a and b over multiple dimensions. 

Returns the sum of the elements of the diagonal of the input 2D matrix. 

Returns the lower triangular part of the matrix (2D tensor) or batch of matrices 

Returns the indices of the lower triangular part of a 

Returns the upper triangular part of a matrix (2D tensor) or batch of matrices 

Returns the indices of the upper triangular part of a 

Generates a Vandermonde matrix. 

Returns a view of 

Returns a view of 
BLAS and LAPACK Operations¶
Performs a batch matrixmatrix product of matrices stored in 

Performs a matrix multiplication of the matrices 

Performs a matrixvector product of the matrix 

Performs the outerproduct of vectors 

Performs a batch matrixmatrix product of matrices in 

Performs a batch matrixmatrix product of matrices stored in 

Returns the matrix product of the $N$ 2D tensors. 

Computes the Cholesky decomposition of a symmetric positivedefinite matrix $A$ or for batches of symmetric positivedefinite matrices. 

Computes the inverse of a symmetric positivedefinite matrix $A$
using its Cholesky factor $u$
: returns matrix 

Solves a linear system of equations with a positive semidefinite matrix to be inverted given its Cholesky factor matrix $u$ . 

Computes the dot product of two 1D tensors. 

Computes the eigenvalues and eigenvectors of a real square matrix. 

This is a lowlevel function for calling LAPACK directly. 

Alias of 

Computes the dot product for 1D tensors. 

Takes the inverse of the square matrix 

Calculates determinant of a square matrix or batches of square matrices. 

Calculates log determinant of a square matrix or batches of square matrices. 

Calculates the sign and log absolute value of the determinant(s) of a square matrix or batches of square matrices. 

Computes the solution to the least squares and least norm problems for a full rank matrix $A$ of size $(m \times n)$ and a matrix $B$ of size $(m \times k)$ . 

Computes the LU factorization of a matrix or batches of matrices 

Returns the LU solve of the linear system $Ax = b$
using the partially pivoted LU factorization of A from 

Unpacks the data and pivots from a LU factorization of a tensor. 

Matrix product of two tensors. 

Returns the matrix raised to the power 

Returns the numerical rank of a 2D tensor. 

Returns the matrix exponential. 

Performs a matrix multiplication of the matrices 

Performs a matrixvector product of the matrix 

Computes the orthogonal matrix Q of a QR factorization, from the (input, input2) tuple returned by 

Multiplies mat (given by 

Outer product of 

Calculates the pseudoinverse (also known as the MoorePenrose inverse) of a 2D tensor. 

Computes the QR decomposition of a matrix or a batch of matrices 

This function returns the solution to the system of linear equations represented by $AX = B$ and the LU factorization of A, in order as a namedtuple solution, LU. 

Computes the singular value decomposition of either a matrix or batch of matrices 

Return the singular value decomposition 

Performs linear Principal Component Analysis (PCA) on a lowrank matrix, batches of such matrices, or sparse matrix. 

This function returns eigenvalues and eigenvectors of a real symmetric matrix 

Find the k largest (or smallest) eigenvalues and the corresponding eigenvectors of a symmetric positive defined generalized eigenvalue problem using matrixfree LOBPCG methods. 

Estimate $\int y\,dx$ along dim, using the trapezoid rule. 

Solves a system of equations with a triangular coefficient matrix $A$ and multiple righthand sides $b$ . 

Computes the dot product of two 1D tensors. 
Utilities¶
Returns whether PyTorch was built with _GLIBCXX_USE_CXX11_ABI=1 

Returns the 

Determines if a type conversion is allowed under PyTorch casting rules described in the type promotion documentation. 

Returns the 

Sets whether PyTorch operations must use “deterministic” algorithms. 

Returns True if the global deterministic flag is turned on. 

vmap is the vectorizing map. 

A wrapper around Python’s assert which is symbolically traceable. 