The torch package contains data structures for multidimensional tensors and mathematical operations over these are defined. 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
is_tensor
 Returns True if 
is_storage
 Returns True if 
is_complex
 Returns True if the data type of 
is_floating_point
 Returns True if the data type of 
is_nonzero
 Returns True if the 
set_default_dtype
 Sets the default floating point dtype to 
get_default_dtype
 Get the current default floating point 
set_default_tensor_type
 Sets the default 
numel
 Returns the total number of elements in the 
set_printoptions
 Set options for printing. 
set_flush_denormal
 Disables denormal floating numbers on CPU. 
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.
tensor
 Constructs a tensor with 
sparse_coo_tensor
 Constructs a sparse tensors in COO(rdinate) format with nonzero elements at the given 
as_tensor
 Convert the data into a 
as_strided
 Create a view of an existing 
from_numpy
 Creates a 
zeros
 Returns a tensor filled with the scalar value 
zeros_like
 Returns a tensor filled with the scalar value 
ones
 Returns a tensor filled with the scalar value 
ones_like
 Returns a tensor filled with the scalar value 
arange
 Returns a 1D tensor of size $\left\lceil \frac{\text{end}  \text{start}}{\text{step}} \right\rceil$ with values from the interval 
range
 Returns a 1D tensor of size $\left\lfloor \frac{\text{end}  \text{start}}{\text{step}} \right\rfloor + 1$ with values from 
linspace
 Creates a onedimensional tensor of size 
logspace
 Creates a onedimensional tensor of size 
eye
 Returns a 2D tensor with ones on the diagonal and zeros elsewhere. 
empty
 Returns a tensor filled with uninitialized data. 
empty_like
 Returns an uninitialized tensor with the same size as 
empty_strided
 Returns a tensor filled with uninitialized data. 
full
 Creates a tensor of size 
full_like
 Returns a tensor with the same size as 
quantize_per_tensor
 Converts a float tensor to a quantized tensor with given scale and zero point. 
quantize_per_channel
 Converts a float tensor to a perchannel quantized tensor with given scales and zero points. 
dequantize
 Returns an fp32 Tensor by dequantizing a quantized Tensor 
complex
 Constructs a complex tensor with its real part equal to 
polar
 Constructs a complex tensor whose elements are Cartesian coordinates corresponding to the polar coordinates with absolute value 
heaviside
 Computes the Heaviside step function for each element in 
cat
 Concatenates the given sequence of 
chunk
 Splits a tensor into a specific number of chunks. 
dstack
 Stack tensors in sequence depthwise (along third axis). 
gather
 Gathers values along an axis specified by 
hstack
 Stack tensors in sequence horizontally (column wise). 
index_select
 Returns a new tensor which indexes the 
masked_select
 Returns a new 1D tensor which indexes the 
movedim
 Moves the dimension(s) of 
narrow
 Returns a new tensor that is a narrowed version of 
nonzero
 
reshape
 Returns a tensor with the same data and number of elements as 
split
 Splits the tensor into chunks. 
squeeze
 Returns a tensor with all the dimensions of 
stack
 Concatenates a sequence of tensors along a new dimension. 
t
 Expects 
take
 Returns a new tensor with the elements of 
transpose
 Returns a tensor that is a transposed version of 
unbind
 Removes a tensor dimension. 
unsqueeze
 Returns a new tensor with a dimension of size one inserted at the specified position. 
vstack
 Stack tensors in sequence vertically (row wise). 
where
 Return a tensor of elements selected from either 
Generator
 Creates and returns a generator object that manages the state of the algorithm which produces pseudo random numbers. 
seed
 Sets the seed for generating random numbers to a nondeterministic random number. 
manual_seed
 Sets the seed for generating random numbers. 
initial_seed
 Returns the initial seed for generating random numbers as a Python 
get_rng_state
 Returns the random number generator state as a 
set_rng_state
 Sets the random number generator state. 
torch.default_generator Returns the default CPU torch.Generator
bernoulli
 Draws binary random numbers (0 or 1) from a Bernoulli distribution. 
multinomial
 Returns a tensor where each row contains 
normal
 Returns a tensor of random numbers drawn from separate normal distributions whose mean and standard deviation are given. 
poisson
 Returns a tensor of the same size as 
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 
randint
 Returns a tensor filled with random integers generated uniformly between 
randint_like
 Returns a tensor with the same shape as Tensor 
randn
 Returns a tensor filled with random numbers from a normal distribution with mean 
randn_like
 Returns a tensor with the same size as 
randperm
 Returns a random permutation of integers from 
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 of torch.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 of torch.normal()
torch.Tensor.random_()
 numbers sampled from the discrete uniform distributiontorch.Tensor.uniform_()
 numbers sampled from the continuous uniform distributionThe 
save
 Saves an object to a disk file. 
load
 Loads an object saved with 
get_num_threads
 Returns the number of threads used for parallelizing CPU operations 
set_num_threads
 Sets the number of threads used for intraop parallelism on CPU. 
get_num_interop_threads
 Returns the number of threads used for interop parallelism on CPU (e.g. 
set_num_interop_threads
 Sets the number of threads used for interop parallelism (e.g. 
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
no_grad
 Contextmanager that disabled gradient calculation. 
enable_grad
 Contextmanager that enables gradient calculation. 
set_grad_enabled
 Contextmanager that sets gradient calculation to on or off. 
abs
 Computes the absolute value of each element in 
absolute
 Alias for 
acos
 Computes the inverse cosine of each element in 
arccos
 Alias for 
acosh
 Returns a new tensor with the inverse hyperbolic cosine of the elements of 
arccosh
 Alias for 
add
 Adds the scalar 
addcdiv
 Performs the elementwise division of 
addcmul
 Performs the elementwise multiplication of 
angle
 Computes the elementwise angle (in radians) of the given 
asin
 Returns a new tensor with the arcsine of the elements of 
arcsin
 Alias for 
asinh
 Returns a new tensor with the inverse hyperbolic sine of the elements of 
arcsinh
 Alias for 
atan
 Returns a new tensor with the arctangent of the elements of 
arctan
 Alias for 
atanh
 Returns a new tensor with the inverse hyperbolic tangent of the elements of 
arctanh
 Alias for 
atan2
 Elementwise arctangent of $\text{input}_{i} / \text{other}_{i}$ with consideration of the quadrant. 
bitwise_not
 Computes the bitwise NOT of the given input tensor. 
bitwise_and
 Computes the bitwise AND of 
bitwise_or
 Computes the bitwise OR of 
bitwise_xor
 Computes the bitwise XOR of 
ceil
 Returns a new tensor with the ceil of the elements of 
clamp
 Clamp all elements in 
clip
 Alias for 
conj
 Computes the elementwise conjugate of the given 
cos
 Returns a new tensor with the cosine of the elements of 
cosh
 Returns a new tensor with the hyperbolic cosine of the elements of 
deg2rad
 Returns a new tensor with each of the elements of 
div
 Divides each element of the input 
divide
 Alias for 
digamma
 Computes the logarithmic derivative of the gamma function on 
erf
 Computes the error function of each element. 
erfc
 Computes the complementary error function of each element of 
erfinv
 Computes the inverse error function of each element of 
exp
 Returns a new tensor with the exponential of the elements of the input tensor 
exp2
 Computes the base two exponential function of 
expm1
 Returns a new tensor with the exponential of the elements minus 1 of 
fix
 Alias for 
floor
 Returns a new tensor with the floor of the elements of 
floor_divide
 
fmod
 Computes the elementwise remainder of division. 
frac
 Computes the fractional portion of each element in 
imag
 Returns a new tensor containing imaginary values of the 
lerp
 Does a linear interpolation of two tensors 
lgamma
 Computes the logarithm of the gamma function on 
log
 Returns a new tensor with the natural logarithm of the elements of 
log10
 Returns a new tensor with the logarithm to the base 10 of the elements of 
log1p
 Returns a new tensor with the natural logarithm of (1 + 
log2
 Returns a new tensor with the logarithm to the base 2 of the elements of 
logaddexp
 Logarithm of the sum of exponentiations of the inputs. 
logaddexp2
 Logarithm of the sum of exponentiations of the inputs in base2. 
logical_and
 Computes the elementwise logical AND of the given input tensors. 
logical_not
 Computes the elementwise logical NOT of the given input tensor. 
logical_or
 Computes the elementwise logical OR of the given input tensors. 
logical_xor
 Computes the elementwise logical XOR of the given input tensors. 
logit
 Returns a new tensor with the logit of the elements of 
hypot
 Given the legs of a right triangle, return its hypotenuse. 
i0
 Computes the zeroth order modified Bessel function of the first kind for each element of 
mul
 Multiplies each element of the input 
multiply
 Alias for 
mvlgamma
 Computes the multivariate loggamma function) with dimension $p$ elementwise, given by 
neg
 Returns a new tensor with the negative of the elements of 
negative
 Alias for 
nextafter
 Return the next floatingpoint value after 
polygamma
 Computes the $n^{th}$ derivative of the digamma function on 
pow
 Takes the power of each element in 
rad2deg
 Returns a new tensor with each of the elements of 
real
 Returns a new tensor containing real values of the 
reciprocal
 Returns a new tensor with the reciprocal of the elements of 
remainder
 Computes the elementwise remainder of division. 
round
 Returns a new tensor with each of the elements of 
rsqrt
 Returns a new tensor with the reciprocal of the squareroot of each of the elements of 
sigmoid
 Returns a new tensor with the sigmoid of the elements of 
sign
 Returns a new tensor with the signs of the elements of 
signbit
 Tests if each element of 
sin
 Returns a new tensor with the sine of the elements of 
sinh
 Returns a new tensor with the hyperbolic sine of the elements of 
sqrt
 Returns a new tensor with the squareroot of the elements of 
square
 Returns a new tensor with the square of the elements of 
sub
 Subtracts 
subtract
 Alias for 
tan
 Returns a new tensor with the tangent of the elements of 
tanh
 Returns a new tensor with the hyperbolic tangent of the elements of 
true_divide
 Alias for 
trunc
 Returns a new tensor with the truncated integer values of the elements of 
argmax
 Returns the indices of the maximum value of all elements in the 
argmin
 Returns the indices of the minimum value of all elements in the 
amax
 Returns the maximum value of each slice of the 
amin
 Returns the minimum value of each slice of the 
max
 Returns the maximum value of all elements in the 
min
 Returns the minimum value of all elements in the 
dist
 Returns the pnorm of ( 
logsumexp
 Returns the log of summed exponentials of each row of the 
mean
 Returns the mean value of all elements in the 
median
 Returns the median value of all elements in the 
mode
 Returns a namedtuple 
norm
 Returns the matrix norm or vector norm of a given tensor. 
nansum
 Returns the sum of all elements, treating Not a Numbers (NaNs) as zero. 
prod
 Returns the product of all elements in the 
quantile
 Returns the qth quantiles of all elements in the 
nanquantile
 This is a variant of 
std
 Returns the standarddeviation of all elements in the 
std_mean
 Returns the standarddeviation and mean of all elements in the 
sum
 Returns the sum of all elements in the 
unique
 Returns the unique elements of the input tensor. 
unique_consecutive
 Eliminates all but the first element from every consecutive group of equivalent elements. 
var
 Returns the variance of all elements in the 
var_mean
 Returns the variance and mean of all elements in the 
count_nonzero
 Counts the number of nonzero values in the tensor 
allclose
 This function checks if all 
argsort
 Returns the indices that sort a tensor along a given dimension in ascending order by value. 
eq
 Computes elementwise equality 
equal


ge
 Computes $\text{input} \geq \text{other}$ elementwise. 
greater_equal
 Alias for 
gt
 Computes $\text{input} > \text{other}$ elementwise. 
greater
 Alias for 
isclose
 Returns a new tensor with boolean elements representing if each element of 
isfinite
 Returns a new tensor with boolean elements representing if each element is 
isinf
 Tests if each element of 
isposinf
 Tests if each element of 
isneginf
 Tests if each element of 
isnan
 Returns a new tensor with boolean elements representing if each element of 
isreal
 Returns a new tensor with boolean elements representing if each element of 
kthvalue
 Returns a namedtuple 
le
 Computes $\text{input} \leq \text{other}$ elementwise. 
less_equal
 Alias for 
lt
 Computes $\text{input} < \text{other}$ elementwise. 
less
 Alias for 
maximum
 Computes the elementwise maximum of 
minimum
 Computes the elementwise minimum of 
ne
 Computes $\text{input} \neq \text{other}$ elementwise. 
not_equal
 Alias for 
sort
 Sorts the elements of the 
topk
 Returns the 
fft
 Complextocomplex Discrete Fourier Transform. 
ifft
 Complextocomplex Inverse Discrete Fourier Transform. 
rfft
 Realtocomplex Discrete Fourier Transform. 
irfft
 Complextoreal Inverse Discrete Fourier Transform. 
stft
 Shorttime Fourier transform (STFT). 
istft
 Inverse short time Fourier Transform. 
bartlett_window
 Bartlett window function. 
blackman_window
 Blackman window function. 
hamming_window
 Hamming window function. 
hann_window
 Hann window function. 
kaiser_window
 Computes the Kaiser window with window length 
atleast_1d
 Returns a 1dimensional view of each input tensor with zero dimensions. 
atleast_2d
 Returns a 2dimensional view of each each input tensor with zero dimensions. 
atleast_3d
 Returns a 3dimensional view of each each input tensor with zero dimensions. 
bincount
 Count the frequency of each value in an array of nonnegative ints. 
block_diag
 Create a block diagonal matrix from provided tensors. 
broadcast_tensors
 Broadcasts the given tensors according to Broadcasting semantics. 
bucketize
 Returns the indices of the buckets to which each value in the 
cartesian_prod
 Do cartesian product of the given sequence of tensors. 
cdist
 Computes batched the pnorm distance between each pair of the two collections of row vectors. 
clone
 Returns a copy of 
combinations
 Compute combinations of length $r$ of the given tensor. 
cross
 Returns the cross product of vectors in dimension 
cummax
 Returns a namedtuple 
cummin
 Returns a namedtuple 
cumprod
 Returns the cumulative product of elements of 
cumsum
 Returns the cumulative sum of elements of 
diag


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


diagonal
 Returns a partial view of 
einsum
 This function provides a way of computing multilinear expressions (i.e. 
flatten
 Flattens a contiguous range of dims in a tensor. 
flip
 Reverse the order of a nD tensor along given axis in dims. 
fliplr
 Flip array in the left/right direction, returning a new tensor. 
flipud
 Flip array in the up/down direction, returning a new tensor. 
rot90
 Rotate a nD tensor by 90 degrees in the plane specified by dims axis. 
gcd
 Computes the elementwise greatest common divisor (GCD) of 
histc
 Computes the histogram of a tensor. 
meshgrid
 
lcm
 Computes the elementwise least common multiple (LCM) of 
logcumsumexp
 Returns the logarithm of the cumulative summation of the exponentiation of elements of 
renorm
 Returns a tensor where each subtensor of 
repeat_interleave
 Repeat elements of a tensor. 
roll
 Roll the tensor along the given dimension(s). 
searchsorted
 Find the indices from the innermost dimension of 
tensordot
 Returns a contraction of a and b over multiple dimensions. 
trace
 Returns the sum of the elements of the diagonal of the input 2D matrix. 
tril
 Returns the lower triangular part of the matrix (2D tensor) or batch of matrices 
tril_indices
 Returns the indices of the lower triangular part of a 
triu
 Returns the upper triangular part of a matrix (2D tensor) or batch of matrices 
triu_indices
 Returns the indices of the upper triangular part of a 
vander
 Generates a Vandermonde matrix. 
view_as_real
 Returns a view of 
view_as_complex
 Returns a view of 
addbmm
 Performs a batch matrixmatrix product of matrices stored in 
addmm
 Performs a matrix multiplication of the matrices 
addmv
 Performs a matrixvector product of the matrix 
addr
 Performs the outerproduct of vectors 
baddbmm
 Performs a batch matrixmatrix product of matrices in 
bmm
 Performs a batch matrixmatrix product of matrices stored in 
chain_matmul
 Returns the matrix product of the $N$ 2D tensors. 
cholesky
 Computes the Cholesky decomposition of a symmetric positivedefinite matrix $A$ or for batches of symmetric positivedefinite matrices. 
cholesky_inverse
 Computes the inverse of a symmetric positivedefinite matrix $A$ using its Cholesky factor $u$ : returns matrix 
cholesky_solve
 Solves a linear system of equations with a positive semidefinite matrix to be inverted given its Cholesky factor matrix $u$ . 
dot
 Computes the dot product (inner product) of two tensors. 
eig
 Computes the eigenvalues and eigenvectors of a real square matrix. 
geqrf
 This is a lowlevel function for calling LAPACK directly. 
ger
 Alias of 
inverse
 Takes the inverse of the square matrix 
det
 Calculates determinant of a square matrix or batches of square matrices. 
logdet
 Calculates log determinant of a square matrix or batches of square matrices. 
slogdet
 Calculates the sign and log absolute value of the determinant(s) of a square matrix or batches of square matrices. 
lstsq
 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)$ . 
lu
 Computes the LU factorization of a matrix or batches of matrices 
lu_solve
 Returns the LU solve of the linear system $Ax = b$ using the partially pivoted LU factorization of A from 
lu_unpack
 Unpacks the data and pivots from a LU factorization of a tensor. 
matmul
 Matrix product of two tensors. 
matrix_power
 Returns the matrix raised to the power 
matrix_rank
 Returns the numerical rank of a 2D tensor. 
matrix_exp
 matrix_power(input) > Tensor 
mm
 Performs a matrix multiplication of the matrices 
mv
 Performs a matrixvector product of the matrix 
orgqr
 Computes the orthogonal matrix 
ormqr
 Multiplies 
outer
 Outer product of 
pinverse
 Calculates the pseudoinverse (also known as the MoorePenrose inverse) of a 2D tensor. 
qr
 Computes the QR decomposition of a matrix or a batch of matrices 
solve
 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 
svd
 This function returns a namedtuple 
svd_lowrank
 Return the singular value decomposition 
pca_lowrank
 Performs linear Principal Component Analysis (PCA) on a lowrank matrix, batches of such matrices, or sparse matrix. 
symeig
 This function returns eigenvalues and eigenvectors of a real symmetric matrix 
lobpcg
 Find the k largest (or smallest) eigenvalues and the corresponding eigenvectors of a symmetric positive defined generalized eigenvalue problem using matrixfree LOBPCG methods. 
trapz
 Estimate $\int y\,dx$ along 
triangular_solve
 Solves a system of equations with a triangular coefficient matrix $A$ and multiple righthand sides $b$ . 
vdot
 Computes the dot product (inner product) of two tensors. 
compiled_with_cxx11_abi
 Returns whether PyTorch was built with _GLIBCXX_USE_CXX11_ABI=1 
result_type
 Returns the 
can_cast
 Determines if a type conversion is allowed under PyTorch casting rules described in the type promotion documentation. 
promote_types
 Returns the 
set_deterministic
 Sets whether PyTorch operations must use “deterministic” algorithms. 
is_deterministic
 Returns True if the global deterministic flag is turned on. 
Assert
 A wrapper around Python’s assert which is symbolically traceable. 
© 2019 Torch Contributors
Licensed under the 3clause BSD License.
https://pytorch.org/docs/1.7.0/torch.html