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¶
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 In-place random sampling
methods to create torch.Tensor
s with values sampled from a broader
range of distributions.
Constructs a tensor with |
|
Constructs a sparse tensors in COO(rdinate) format with non-zero elements 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 1-D tensor of size
with values from the interval |
|
Returns a 1-D tensor of size
with values from |
|
Returns a one-dimensional tensor of |
|
Returns a one-dimensional tensor of |
|
Returns a 2-D 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. |
|
Returns a tensor of size |
|
Returns a tensor with the same size as |
|
Converts a float tensor to quantized tensor with given scale and zero point. |
|
Converts a float tensor to per-channel quantized tensor with given scales and zero points. |
|
Given a quantized Tensor, dequantize it and return an fp32 Tensor |
Indexing, Slicing, Joining, Mutating Ops¶
Concatenates the given sequence of |
|
Splits a tensor into a specific number of chunks. |
|
Gathers values along an axis specified by dim. |
|
Returns a new tensor which indexes the |
|
Returns a new 1-D tensor which indexes the |
|
Returns a new tensor that is a narrowed version of |
|
Returns a tensor with the same data and number of elements as |
|
Splits the tensor into chunks. |
|
Returns a tensor with all the dimensions of |
|
Concatenates sequence of tensors along a new dimension. |
|
Expects |
|
Returns a new tensor with 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. |
|
Return a tensor of elements selected from either |
Generators¶
Creates and returns a generator object which manages the state of the algorithm that produces pseudo random numbers. |
Random sampling¶
Sets the seed for generating random numbers to a non-deterministic 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 |
|
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 |
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:
torch.Tensor.bernoulli_()
- in-place 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 log-normal distributiontorch.Tensor.normal_()
- in-place version oftorch.normal()
torch.Tensor.random_()
- numbers sampled from the discrete uniform distributiontorch.Tensor.uniform_()
- numbers sampled from the continuous uniform distribution
Quasi-random 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 inter-op 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
Context-manager that disabled gradient calculation. |
|
Context-manager that enables gradient calculation. |
|
Context-manager that sets gradient calculation to on or off. |
Math operations¶
Pointwise Ops¶
Computes the element-wise absolute value of the given |
|
Alias for |
|
Returns a new tensor with the arccosine of the elements of |
|
Returns a new tensor with the inverse hyperbolic cosine of the elements of |
|
Adds the scalar |
|
Performs the element-wise division of |
|
Performs the element-wise multiplication of |
|
Computes the element-wise angle (in radians) of the given |
|
Returns a new tensor with the arcsine of the elements of |
|
Returns a new tensor with the inverse hyperbolic sine of the elements of |
|
Returns a new tensor with the arctangent of the elements of |
|
Returns a new tensor with the inverse hyperbolic tangent of the elements of |
|
Element-wise arctangent of 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 |
|
Clamp all elements in |
|
Computes the element-wise conjugate of the given |
|
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 |
|
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 |
|
Returns a new tensor with the exponential of the elements minus 1 of |
|
Returns a new tensor with the floor of the elements of |
|
Return the division of the inputs rounded down to the nearest integer. |
|
Computes the element-wise remainder of division. |
|
Computes the fractional portion of each element in |
|
Returns a new tensor containing imaginary values of the |
|
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 base-2. |
|
Computes the element-wise logical AND of the given input tensors. |
|
Computes the element-wise logical NOT of the given input tensor. |
|
Computes the element-wise logical OR of the given input tensors. |
|
Computes the element-wise logical XOR of the given input tensors. |
|
Multiplies each element of the input |
|
Computes the multivariate log-gamma function) with dimension element-wise, given by |
|
Returns a new tensor with the negative of the elements of |
|
Computes the
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 element-wise remainder of division. |
|
Returns a new tensor with each of the elements of |
|
Returns a new tensor with the reciprocal of the square-root 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 |
|
Returns a new tensor with the sine of the elements of |
|
Returns a new tensor with the hyperbolic sine of the elements of |
|
Returns a new tensor with the square-root of the elements of |
|
Returns a new tensor with the square of the elements of |
|
Returns a new tensor with the tangent of the elements of |
|
Returns a new tensor with the hyperbolic tangent of the elements of |
|
Performs “true division” that always computes the division in floating point. |
|
Returns a new tensor with the truncated integer values of the elements of |
Reduction Ops¶
Returns the indices of the maximum value of all elements in the |
|
Returns the indices of the minimum value of all elements in the |
|
Returns the p-norm of ( |
|
Returns the log of summed exponentials of each row of the |
|
Returns the mean value of all elements in the |
|
Returns the median value of all elements in the |
|
Returns a namedtuple |
|
Returns the matrix norm or vector norm of a given tensor. |
|
Returns the product of all elements in the |
|
Returns the standard-deviation of all elements in the |
|
Returns the standard-deviation 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 non-zero 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 element-wise equality |
|
|
|
Computes element-wise. |
|
Computes element-wise. |
|
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. |
|
Returns a new tensor with boolean elements representing if each element is +/-INF or not. |
|
Returns a new tensor with boolean elements representing if each element is NaN or not. |
|
Returns a namedtuple |
|
Computes element-wise. |
|
Computes element-wise. |
|
Returns the maximum value of all elements in the |
|
Returns the minimum value of all elements in the |
|
Computes element-wise. |
|
Sorts the elements of the |
|
Returns the |
Spectral Ops¶
Complex-to-complex Discrete Fourier Transform |
|
Complex-to-complex Inverse Discrete Fourier Transform |
|
Real-to-complex Discrete Fourier Transform |
|
Complex-to-real Inverse Discrete Fourier Transform |
|
Short-time Fourier transform (STFT). |
|
Inverse short time Fourier Transform. |
|
Bartlett window function. |
|
Blackman window function. |
|
Hamming window function. |
|
Hann window function. |
Other Operations¶
Count the frequency of each value in an array of non-negative ints. |
|
Create a block diagonal matrix from provided tensors. |
|
Broadcasts the given tensors according to Broadcasting semantics. |
|
Returns the indices of the buckets to which each value in the |
|
Do cartesian product of the given sequence of tensors. |
|
Computes batched the p-norm distance between each pair of the two collections of row vectors. |
|
Compute combinations of length 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 |
|
This function provides a way of computing multilinear expressions (i.e. |
|
Flattens a contiguous range of dims in a tensor. |
|
Reverse the order of a n-D tensor along given axis in dims. |
|
Flip array in the left/right direction, returning a new tensor. |
|
Flip array in the up/down direction, returning a new tensor. |
|
Rotate a n-D tensor by 90 degrees in the plane specified by dims axis. |
|
Computes the histogram of a tensor. |
|
Take tensors, each of which can be either scalar or 1-dimensional vector, and create N-dimensional grids, where the th grid is defined by expanding the th input over dimensions defined by other inputs. |
|
Returns the logarithm of the cumulative summation of the exponentiation of elements of |
|
Returns a tensor where each sub-tensor 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 2-D matrix. |
|
Returns the lower triangular part of the matrix (2-D tensor) or batch of matrices |
|
Returns the indices of the lower triangular part of a |
|
Returns the upper triangular part of a matrix (2-D 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 matrix-matrix product of matrices stored in |
|
Performs a matrix multiplication of the matrices |
|
Performs a matrix-vector product of the matrix |
|
Performs the outer-product of vectors |
|
Performs a batch matrix-matrix product of matrices in |
|
Performs a batch matrix-matrix product of matrices stored in |
|
Returns the matrix product of the 2-D tensors. |
|
Computes the Cholesky decomposition of a symmetric positive-definite matrix or for batches of symmetric positive-definite matrices. |
|
Computes the inverse of a symmetric positive-definite matrix
using its Cholesky factor
: returns matrix |
|
Solves a linear system of equations with a positive semidefinite matrix to be inverted given its Cholesky factor matrix . |
|
Computes the dot product (inner product) of two tensors. |
|
Computes the eigenvalues and eigenvectors of a real square matrix. |
|
This is a low-level function for calling LAPACK directly. |
|
Outer product of |
|
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 of size and a matrix of size . |
|
Computes the LU factorization of a matrix or batches of matrices |
|
Returns the LU solve of the linear system
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 2-D tensor. |
|
Performs a matrix multiplication of the matrices |
|
Performs a matrix-vector product of the matrix |
|
Computes the orthogonal matrix Q of a QR factorization, from the (input, input2) tuple returned by |
|
Multiplies mat (given by |
|
Calculates the pseudo-inverse (also known as the Moore-Penrose 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 and the LU factorization of A, in order as a namedtuple solution, LU. |
|
This function returns a namedtuple |
|
Return the singular value decomposition |
|
Performs linear Principal Component Analysis (PCA) on a low-rank 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 matrix-free LOBPCG methods. |
|
Estimate along dim, using the trapezoid rule. |
|
Solves a system of equations with a triangular coefficient matrix and multiple right-hand sides . |
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 |