Shortcuts

torch

The torch package contains data structures for multi-dimensional 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

Tensors

is_tensor

Returns True if obj is a PyTorch tensor.

is_storage

Returns True if obj is a PyTorch storage object.

is_complex

Returns True if the data type of input is a complex data type i.e., one of torch.complex64, and torch.complex128.

is_floating_point

Returns True if the data type of input is a floating point data type i.e., one of torch.float64, torch.float32 and torch.float16.

is_nonzero

Returns True if the input is a single element tensor which is not equal to zero after type conversions.

set_default_dtype

Sets the default floating point dtype to d.

get_default_dtype

Get the current default floating point torch.dtype.

set_default_tensor_type

Sets the default torch.Tensor type to floating point tensor type t.

numel

Returns the total number of elements in the input tensor.

set_printoptions

Set options for printing.

set_flush_denormal

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 In-place random sampling methods to create torch.Tensor s with values sampled from a broader range of distributions.

tensor

Constructs a tensor with data.

sparse_coo_tensor

Constructs a sparse tensors in COO(rdinate) format with non-zero elements at the given indices with the given values.

as_tensor

Convert the data into a torch.Tensor.

as_strided

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

from_numpy

Creates a Tensor from a numpy.ndarray.

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 size 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 size as input.

arange

Returns a 1-D tensor of size endstartstep\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.

range

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

linspace

Returns a one-dimensional tensor of steps equally spaced points between start and end.

logspace

Returns a one-dimensional tensor of steps points logarithmically spaced with base base between basestart{\text{base}}^{\text{start}} and baseend{\text{base}}^{\text{end}} .

eye

Returns a 2-D 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 input.

empty_strided

Returns a tensor filled with uninitialized data.

full

Returns a tensor of size size filled with fill_value.

full_like

Returns a tensor with the same size as input filled with fill_value.

quantize_per_tensor

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

quantize_per_channel

Converts a float tensor to per-channel quantized tensor with given scales and zero points.

dequantize

Given a quantized Tensor, dequantize it and return an fp32 Tensor

Indexing, Slicing, Joining, Mutating Ops

cat

Concatenates the given sequence of seq tensors in the given dimension.

chunk

Splits a tensor into a specific number of chunks.

gather

Gathers values along an axis specified by dim.

index_select

Returns a new tensor which indexes the input tensor along dimension dim using the entries in index which is a LongTensor.

masked_select

Returns a new 1-D tensor which indexes the input tensor according to the boolean mask mask which is a BoolTensor.

narrow

Returns a new tensor that is a narrowed version of input tensor.

nonzero

reshape

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

split

Splits the tensor into chunks.

squeeze

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

stack

Concatenates sequence of tensors along a new dimension.

t

Expects input to be <= 2-D tensor and transposes dimensions 0 and 1.

take

Returns a new tensor with the elements of input at the given indices.

transpose

Returns a tensor that is a transposed version of input.

unbind

Removes a tensor dimension.

unsqueeze

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

where

Return a tensor of elements selected from either x or y, depending on condition.

Generators

Generator

Creates and returns a generator object which manages the state of the algorithm that produces pseudo random numbers.

Random sampling

seed

Sets the seed for generating random numbers to a non-deterministic random number.

manual_seed

Sets the seed for generating random numbers.

initial_seed

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

get_rng_state

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

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 num_samples indices sampled from the multinomial probability distribution located in the corresponding row of tensor input.

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 input with each element sampled from a Poisson distribution with rate parameter given by the corresponding element in input i.e.,

rand

Returns a tensor filled with random numbers from a uniform distribution on the interval [0,1)[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)[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 Tensor input 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 random permutation of integers from 0 to n - 1.

In-place random sampling

There are a few more in-place random sampling functions defined on Tensors as well. Click through to refer to their documentation:

Quasi-random sampling

quasirandom.SobolEngine

The torch.quasirandom.SobolEngine is an engine for generating (scrambled) Sobol sequences.

Serialization

save

Saves an object to a disk file.

load

Loads an object saved with torch.save() from a file.

Parallelism

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 inter-op parallelism on CPU (e.g.

set_num_interop_threads

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

no_grad

Context-manager that disabled gradient calculation.

enable_grad

Context-manager that enables gradient calculation.

set_grad_enabled

Context-manager that sets gradient calculation to on or off.

Math operations

Pointwise Ops

abs

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

absolute

Alias for torch.abs()

acos

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

acosh

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

add

Adds the scalar other to each element of the input input and returns a new resulting tensor.

addcdiv

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

addcmul

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

angle

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

asin

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

asinh

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

atan

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

atanh

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

atan2

Element-wise arctangent of inputi/otheri\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 input and other.

bitwise_or

Computes the bitwise OR of input and other.

bitwise_xor

Computes the bitwise XOR of input and other.

ceil

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

clamp

Clamp all elements in input into the range [ min, max ] and return a resulting tensor:

conj

Computes the element-wise conjugate of the given input tensor.

cos

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

cosh

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

deg2rad

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

div

Divides each element of the input input with the scalar other and returns a new resulting tensor.

digamma

Computes the logarithmic derivative of the gamma function on input.

erf

Computes the error function of each element.

erfc

Computes the complementary error function of each element of input.

erfinv

Computes the inverse error function of each element of input.

exp

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

expm1

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

floor

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

floor_divide

Return the division of the inputs rounded down to the nearest integer.

fmod

Computes the element-wise remainder of division.

frac

Computes the fractional portion of each element in input.

imag

Returns a new tensor containing imaginary values of the self tensor.

lerp

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.

lgamma

Computes the logarithm of the gamma function on input.

log

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

log10

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

log1p

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

log2

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

logaddexp

Logarithm of the sum of exponentiations of the inputs.

logaddexp2

Logarithm of the sum of exponentiations of the inputs in base-2.

logical_and

Computes the element-wise logical AND of the given input tensors.

logical_not

Computes the element-wise logical NOT of the given input tensor.

logical_or

Computes the element-wise logical OR of the given input tensors.

logical_xor

Computes the element-wise logical XOR of the given input tensors.

mul

Multiplies each element of the input input with the scalar other and returns a new resulting tensor.

mvlgamma

Computes the multivariate log-gamma function) with dimension pp element-wise, given by

neg

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

polygamma

Computes the nthn^{th} derivative of the digamma function on input.

pow

Takes the power of each element in input with exponent and returns a tensor with the result.

rad2deg

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

real

Returns a new tensor containing real values of the self tensor.

reciprocal

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

remainder

Computes the element-wise remainder of division.

round

Returns a new tensor with each of the elements of input rounded to the closest integer.

rsqrt

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

sigmoid

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

sign

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

sin

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

sinh

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

sqrt

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

square

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

tan

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

tanh

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

true_divide

Performs “true division” that always computes the division in floating point.

trunc

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

Reduction Ops

argmax

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

argmin

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

dist

Returns the p-norm of (input - other)

logsumexp

Returns the log of summed exponentials of each row of the input tensor in the given dimension dim.

mean

Returns the mean value of all elements in the input tensor.

median

Returns the median value of all elements in the input tensor.

mode

Returns a namedtuple (values, indices) where values is the mode value of each row of the input tensor in the given dimension dim, i.e.

norm

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

prod

Returns the product of all elements in the input tensor.

std

Returns the standard-deviation of all elements in the input tensor.

std_mean

Returns the standard-deviation and mean of all elements in the input tensor.

sum

Returns the sum of all elements in the input tensor.

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 input tensor.

var_mean

Returns the variance and mean of all elements in the input tensor.

count_nonzero

Counts the number of non-zero values in the tensor input along the given dim.

Comparison Ops

allclose

This function checks if all input and other satisfy the condition:

argsort

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

eq

Computes element-wise equality

equal

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

ge

Computes inputother\text{input} \geq \text{other} element-wise.

gt

Computes input>other\text{input} > \text{other} element-wise.

isclose

Returns a new tensor with boolean elements representing if each element of input is “close” to the corresponding element of other.

isfinite

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

isinf

Returns a new tensor with boolean elements representing if each element is +/-INF or not.

isnan

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

kthvalue

Returns a namedtuple (values, indices) where values is the k th smallest element of each row of the input tensor in the given dimension dim.

le

Computes inputother\text{input} \leq \text{other} element-wise.

lt

Computes input<other\text{input} < \text{other} element-wise.

max

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

min

Returns the minimum value of all elements in the input tensor.

ne

Computes inputotherinput \neq other element-wise.

sort

Sorts the elements of the input tensor along a given dimension in ascending order by value.

topk

Returns the k largest elements of the given input tensor along a given dimension.

Spectral Ops

fft

Complex-to-complex Discrete Fourier Transform

ifft

Complex-to-complex Inverse Discrete Fourier Transform

rfft

Real-to-complex Discrete Fourier Transform

irfft

Complex-to-real Inverse Discrete Fourier Transform

stft

Short-time 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.

Other Operations

bincount

Count the frequency of each value in an array of non-negative 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 input belongs, where the boundaries of the buckets are set by boundaries.

cartesian_prod

Do cartesian product of the given sequence of tensors.

cdist

Computes batched the p-norm distance between each pair of the two collections of row vectors.

combinations

Compute combinations of length rr of the given tensor.

cross

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

cummax

Returns a namedtuple (values, indices) where values is the cumulative maximum of elements of input in the dimension dim.

cummin

Returns a namedtuple (values, indices) where values is the cumulative minimum of elements of input in the dimension dim.

cumprod

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

cumsum

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

diag

  • If input is a vector (1-D tensor), then returns a 2-D square tensor

diag_embed

Creates a tensor whose diagonals of certain 2D planes (specified by dim1 and dim2) are filled by input.

diagflat

  • If input is a vector (1-D tensor), then returns a 2-D square tensor

diagonal

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.

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 n-D 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 n-D tensor by 90 degrees in the plane specified by dims axis.

histc

Computes the histogram of a tensor.

meshgrid

Take NN tensors, each of which can be either scalar or 1-dimensional vector, and create NN N-dimensional grids, where the ii th grid is defined by expanding the ii th input over dimensions defined by other inputs.

logcumsumexp

Returns the logarithm of the cumulative summation of the exponentiation of elements of input in the dimension dim.

renorm

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

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 sorted_sequence such that, if the corresponding values in values were inserted before the indices, the order of the corresponding innermost dimension within sorted_sequence would be preserved.

tensordot

Returns a contraction of a and b over multiple dimensions.

trace

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

tril

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.

tril_indices

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.

triu

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.

triu_indices

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.

vander

Generates a Vandermonde matrix.

view_as_real

Returns a view of input as a real tensor.

view_as_complex

Returns a view of input as a complex tensor.

BLAS and LAPACK Operations

addbmm

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).

addmm

Performs a matrix multiplication of the matrices mat1 and mat2.

addmv

Performs a matrix-vector product of the matrix mat and the vector vec.

addr

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

baddbmm

Performs a batch matrix-matrix product of matrices in batch1 and batch2.

bmm

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

chain_matmul

Returns the matrix product of the NN 2-D tensors.

cholesky

Computes the Cholesky decomposition of a symmetric positive-definite matrix AA or for batches of symmetric positive-definite matrices.

cholesky_inverse

Computes the inverse of a symmetric positive-definite matrix AA using its Cholesky factor uu : returns matrix inv.

cholesky_solve

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

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 low-level function for calling LAPACK directly.

ger

Outer product of input and vec2.

inverse

Takes the inverse of the square matrix input.

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 AA of size (m×n)(m \times n) and a matrix BB of size (m×k)(m \times k) .

lu

Computes the LU factorization of a matrix or batches of matrices A.

lu_solve

Returns the LU solve of the linear system Ax=bAx = b using the partially pivoted LU factorization of A from torch.lu().

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 n for square matrices.

matrix_rank

Returns the numerical rank of a 2-D tensor.

mm

Performs a matrix multiplication of the matrices input and mat2.

mv

Performs a matrix-vector product of the matrix input and the vector vec.

orgqr

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

ormqr

Multiplies mat (given by input3) by the orthogonal Q matrix of the QR factorization formed by torch.geqrf() that is represented by (a, tau) (given by (input, input2)).

pinverse

Calculates the pseudo-inverse (also known as the Moore-Penrose inverse) of a 2D tensor.

qr

Computes the QR decomposition of a matrix or a batch of matrices input, and returns a namedtuple (Q, R) of tensors such that input=QR\text{input} = Q R with QQ being an orthogonal matrix or batch of orthogonal matrices and RR being an upper triangular matrix or batch of upper triangular matrices.

solve

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

svd

This function returns a namedtuple (U, S, V) which is the singular value decomposition of a input real matrix or batches of real matrices input such that input=U×diag(S)×VTinput = U \times diag(S) \times V^T .

svd_lowrank

Return the singular value decomposition (U, S, V) of a matrix, batches of matrices, or a sparse matrix AA such that AUdiag(S)VTA \approx U diag(S) V^T .

pca_lowrank

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

symeig

This function returns eigenvalues and eigenvectors of a real symmetric matrix input or a batch of real symmetric matrices, represented by a namedtuple (eigenvalues, eigenvectors).

lobpcg

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

trapz

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

triangular_solve

Solves a system of equations with a triangular coefficient matrix AA and multiple right-hand sides bb .

Utilities

compiled_with_cxx11_abi

Returns whether PyTorch was built with _GLIBCXX_USE_CXX11_ABI=1

result_type

Returns the torch.dtype that would result from performing an arithmetic operation on the provided input tensors.

can_cast

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

promote_types

Returns the torch.dtype with the smallest size and scalar kind that is not smaller nor of lower kind than either type1 or type2.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources