torch.nn¶
These are the basic building blocks for graphs:
torch.nn
A kind of Tensor that is to be considered a module parameter. 

A parameter that is not initialized. 

A buffer that is not initialized. 
Containers¶
Base class for all neural network modules. 

A sequential container. 

Holds submodules in a list. 

Holds submodules in a dictionary. 

Holds parameters in a list. 

Holds parameters in a dictionary. 
Global Hooks For Module
Register a forward prehook common to all modules. 

Register a global forward hook for all the modules. 

Register a backward hook common to all the modules. 

Register a backward prehook common to all the modules. 

Register a backward hook common to all the modules. 

Register a buffer registration hook common to all modules. 

Register a module registration hook common to all modules. 

Register a parameter registration hook common to all modules. 
Convolution Layers¶
Applies a 1D convolution over an input signal composed of several input planes. 

Applies a 2D convolution over an input signal composed of several input planes. 

Applies a 3D convolution over an input signal composed of several input planes. 

Applies a 1D transposed convolution operator over an input image composed of several input planes. 

Applies a 2D transposed convolution operator over an input image composed of several input planes. 

Applies a 3D transposed convolution operator over an input image composed of several input planes. 

A 

A 

A 

A 

A 

A 

Extracts sliding local blocks from a batched input tensor. 

Combines an array of sliding local blocks into a large containing tensor. 
Pooling layers¶
Applies a 1D max pooling over an input signal composed of several input planes. 

Applies a 2D max pooling over an input signal composed of several input planes. 

Applies a 3D max pooling over an input signal composed of several input planes. 

Computes a partial inverse of 

Computes a partial inverse of 

Computes a partial inverse of 

Applies a 1D average pooling over an input signal composed of several input planes. 

Applies a 2D average pooling over an input signal composed of several input planes. 

Applies a 3D average pooling over an input signal composed of several input planes. 

Applies a 2D fractional max pooling over an input signal composed of several input planes. 

Applies a 3D fractional max pooling over an input signal composed of several input planes. 

Applies a 1D poweraverage pooling over an input signal composed of several input planes. 

Applies a 2D poweraverage pooling over an input signal composed of several input planes. 

Applies a 3D poweraverage pooling over an input signal composed of several input planes. 

Applies a 1D adaptive max pooling over an input signal composed of several input planes. 

Applies a 2D adaptive max pooling over an input signal composed of several input planes. 

Applies a 3D adaptive max pooling over an input signal composed of several input planes. 

Applies a 1D adaptive average pooling over an input signal composed of several input planes. 

Applies a 2D adaptive average pooling over an input signal composed of several input planes. 

Applies a 3D adaptive average pooling over an input signal composed of several input planes. 
Padding Layers¶
Pads the input tensor using the reflection of the input boundary. 

Pads the input tensor using the reflection of the input boundary. 

Pads the input tensor using the reflection of the input boundary. 

Pads the input tensor using replication of the input boundary. 

Pads the input tensor using replication of the input boundary. 

Pads the input tensor using replication of the input boundary. 

Pads the input tensor boundaries with zero. 

Pads the input tensor boundaries with zero. 

Pads the input tensor boundaries with zero. 

Pads the input tensor boundaries with a constant value. 

Pads the input tensor boundaries with a constant value. 

Pads the input tensor boundaries with a constant value. 

Pads the input tensor using circular padding of the input boundary. 

Pads the input tensor using circular padding of the input boundary. 

Pads the input tensor using circular padding of the input boundary. 
Nonlinear Activations (weighted sum, nonlinearity)¶
Applies the Exponential Linear Unit (ELU) function, elementwise. 

Applies the Hard Shrinkage (Hardshrink) function elementwise. 

Applies the Hardsigmoid function elementwise. 

Applies the HardTanh function elementwise. 

Applies the Hardswish function, elementwise. 

Applies the LeakyReLU function elementwise. 

Applies the Logsigmoid function elementwise. 

Allows the model to jointly attend to information from different representation subspaces. 

Applies the elementwise PReLU function. 

Applies the rectified linear unit function elementwise. 

Applies the ReLU6 function elementwise. 

Applies the randomized leaky rectified linear unit function, elementwise. 

Applies the SELU function elementwise. 

Applies the CELU function elementwise. 

Applies the Gaussian Error Linear Units function. 

Applies the Sigmoid function elementwise. 

Applies the Sigmoid Linear Unit (SiLU) function, elementwise. 

Applies the Mish function, elementwise. 

Applies the Softplus function elementwise. 

Applies the soft shrinkage function elementwise. 

Applies the elementwise Softsign function. 

Applies the Hyperbolic Tangent (Tanh) function elementwise. 

Applies the elementwise Tanhshrink function. 

Thresholds each element of the input Tensor. 

Applies the gated linear unit function. 
Nonlinear Activations (other)¶
Applies the Softmin function to an ndimensional input Tensor. 

Applies the Softmax function to an ndimensional input Tensor. 

Applies SoftMax over features to each spatial location. 

Applies the $\log(\text{Softmax}(x))$ function to an ndimensional input Tensor. 

Efficient softmax approximation. 
Normalization Layers¶
Applies Batch Normalization over a 2D or 3D input. 

Applies Batch Normalization over a 4D input. 

Applies Batch Normalization over a 5D input. 

A 

A 

A 

Applies Group Normalization over a minibatch of inputs. 

Applies Batch Normalization over a NDimensional input. 

Applies Instance Normalization. 

Applies Instance Normalization. 

Applies Instance Normalization. 

A 

A 

A 

Applies Layer Normalization over a minibatch of inputs. 

Applies local response normalization over an input signal. 

Applies Root Mean Square Layer Normalization over a minibatch of inputs. 
Recurrent Layers¶
Base class for RNN modules (RNN, LSTM, GRU). 

Apply a multilayer Elman RNN with $\tanh$ or $\text{ReLU}$ nonlinearity to an input sequence. 

Apply a multilayer long shortterm memory (LSTM) RNN to an input sequence. 

Apply a multilayer gated recurrent unit (GRU) RNN to an input sequence. 

An Elman RNN cell with tanh or ReLU nonlinearity. 

A long shortterm memory (LSTM) cell. 

A gated recurrent unit (GRU) cell. 
Transformer Layers¶
A transformer model. 

TransformerEncoder is a stack of N encoder layers. 

TransformerDecoder is a stack of N decoder layers. 

TransformerEncoderLayer is made up of selfattn and feedforward network. 

TransformerDecoderLayer is made up of selfattn, multiheadattn and feedforward network. 
Linear Layers¶
A placeholder identity operator that is argumentinsensitive. 

Applies an affine linear transformation to the incoming data: $y = xA^T + b$. 

Applies a bilinear transformation to the incoming data: $y = x_1^T A x_2 + b$. 

A 
Dropout Layers¶
During training, randomly zeroes some of the elements of the input tensor with probability 

Randomly zero out entire channels. 

Randomly zero out entire channels. 

Randomly zero out entire channels. 

Applies Alpha Dropout over the input. 

Randomly masks out entire channels. 
Sparse Layers¶
A simple lookup table that stores embeddings of a fixed dictionary and size. 

Compute sums or means of 'bags' of embeddings, without instantiating the intermediate embeddings. 
Distance Functions¶
Returns cosine similarity between $x_1$ and $x_2$, computed along dim. 

Computes the pairwise distance between input vectors, or between columns of input matrices. 
Loss Functions¶
Creates a criterion that measures the mean absolute error (MAE) between each element in the input $x$ and target $y$. 

Creates a criterion that measures the mean squared error (squared L2 norm) between each element in the input $x$ and target $y$. 

This criterion computes the cross entropy loss between input logits and target. 

The Connectionist Temporal Classification loss. 

The negative log likelihood loss. 

Negative log likelihood loss with Poisson distribution of target. 

Gaussian negative log likelihood loss. 

The KullbackLeibler divergence loss. 

Creates a criterion that measures the Binary Cross Entropy between the target and the input probabilities: 

This loss combines a Sigmoid layer and the BCELoss in one single class. 

Creates a criterion that measures the loss given inputs $x1$, $x2$, two 1D minibatch or 0D Tensors, and a label 1D minibatch or 0D Tensor $y$ (containing 1 or 1). 

Measures the loss given an input tensor $x$ and a labels tensor $y$ (containing 1 or 1). 

Creates a criterion that optimizes a multiclass multiclassification hinge loss (marginbased loss) between input $x$ (a 2D minibatch Tensor) and output $y$ (which is a 2D Tensor of target class indices). 

Creates a criterion that uses a squared term if the absolute elementwise error falls below delta and a deltascaled L1 term otherwise. 

Creates a criterion that uses a squared term if the absolute elementwise error falls below beta and an L1 term otherwise. 

Creates a criterion that optimizes a twoclass classification logistic loss between input tensor $x$ and target tensor $y$ (containing 1 or 1). 

Creates a criterion that optimizes a multilabel oneversusall loss based on maxentropy, between input $x$ and target $y$ of size $(N, C)$. 

Creates a criterion that measures the loss given input tensors $x_1$, $x_2$ and a Tensor label $y$ with values 1 or 1. 

Creates a criterion that optimizes a multiclass classification hinge loss (marginbased loss) between input $x$ (a 2D minibatch Tensor) and output $y$ (which is a 1D tensor of target class indices, $0 \leq y \leq \text{x.size}(1)1$): 

Creates a criterion that measures the triplet loss given an input tensors $x1$, $x2$, $x3$ and a margin with a value greater than $0$. 

Creates a criterion that measures the triplet loss given input tensors $a$, $p$, and $n$ (representing anchor, positive, and negative examples, respectively), and a nonnegative, realvalued function ("distance function") used to compute the relationship between the anchor and positive example ("positive distance") and the anchor and negative example ("negative distance"). 
Vision Layers¶
Rearrange elements in a tensor according to an upscaling factor. 

Reverse the PixelShuffle operation. 

Upsamples a given multichannel 1D (temporal), 2D (spatial) or 3D (volumetric) data. 

Applies a 2D nearest neighbor upsampling to an input signal composed of several input channels. 

Applies a 2D bilinear upsampling to an input signal composed of several input channels. 
Shuffle Layers¶
Divides and rearranges the channels in a tensor. 
DataParallel Layers (multiGPU, distributed)¶
Implements data parallelism at the module level. 

Implement distributed data parallelism based on 
Utilities¶
From the torch.nn.utils
module:
Utility functions to clip parameter gradients.
Clip the gradient norm of an iterable of parameters. 

Clip the gradient norm of an iterable of parameters. 

Clip the gradients of an iterable of parameters at specified value. 
Utility functions to flatten and unflatten Module parameters to and from a single vector.
Flatten an iterable of parameters into a single vector. 

Copy slices of a vector into an iterable of parameters. 
Utility functions to fuse Modules with BatchNorm modules.
Fuse a convolutional module and a BatchNorm module into a single, new convolutional module. 

Fuse convolutional module parameters and BatchNorm module parameters into new convolutional module parameters. 

Fuse a linear module and a BatchNorm module into a single, new linear module. 

Fuse linear module parameters and BatchNorm module parameters into new linear module parameters. 
Utility functions to convert Module parameter memory formats.
Convert 

Convert 
Utility functions to apply and remove weight normalization from Module parameters.
Apply weight normalization to a parameter in the given module. 

Remove the weight normalization reparameterization from a module. 

Apply spectral normalization to a parameter in the given module. 

Remove the spectral normalization reparameterization from a module. 
Utility functions for initializing Module parameters.
Given a module class object and args / kwargs, instantiate the module without initializing parameters / buffers. 
Utility classes and functions for pruning Module parameters.
Abstract base class for creation of new pruning techniques. 

Container holding a sequence of pruning methods for iterative pruning. 

Utility pruning method that does not prune any units but generates the pruning parametrization with a mask of ones. 

Prune (currently unpruned) units in a tensor at random. 

Prune (currently unpruned) units in a tensor by zeroing out the ones with the lowest L1norm. 

Prune entire (currently unpruned) channels in a tensor at random. 

Prune entire (currently unpruned) channels in a tensor based on their L 

Apply pruning reparametrization without pruning any units. 

Prune tensor by removing random (currently unpruned) units. 

Prune tensor by removing units with the lowest L1norm. 

Prune tensor by removing random channels along the specified dimension. 

Prune tensor by removing channels with the lowest L 

Globally prunes tensors corresponding to all parameters in 

Prune tensor corresponding to parameter called 

Remove the pruning reparameterization from a module and the pruning method from the forward hook. 

Check if a module is pruned by looking for pruning prehooks. 
Parametrizations implemented using the new parametrization functionality
in torch.nn.utils.parameterize.register_parametrization()
.
Apply an orthogonal or unitary parametrization to a matrix or a batch of matrices. 

Apply weight normalization to a parameter in the given module. 

Apply spectral normalization to a parameter in the given module. 
Utility functions to parametrize Tensors on existing Modules. Note that these functions can be used to parametrize a given Parameter or Buffer given a specific function that maps from an input space to the parametrized space. They are not parameterizations that would transform an object into a parameter. See the Parametrizations tutorial for more information on how to implement your own parametrizations.
Register a parametrization to a tensor in a module. 

Remove the parametrizations on a tensor in a module. 

Context manager that enables the caching system within parametrizations registered with 

Determine if a module has a parametrization. 
A sequential container that holds and manages the original parameters or buffers of a parametrized 
Utility functions to call a given Module in a stateless manner.
Perform a functional call on the module by replacing the module parameters and buffers with the provided ones. 
Utility functions in other modules
Holds the data and list of 

Packs a Tensor containing padded sequences of variable length. 

Pad a packed batch of variable length sequences. 

Pad a list of variable length Tensors with 

Packs a list of variable length Tensors. 

Unpack PackedSequence into a list of variable length Tensors. 

Unpad padded Tensor into a list of variable length Tensors. 
Flattens a contiguous range of dims into a tensor. 

Unflattens a tensor dim expanding it to a desired shape. 
Quantized Functions¶
Quantization refers to techniques for performing computations and storing tensors at lower bitwidths than floating point precision. PyTorch supports both per tensor and per channel asymmetric linear quantization. To learn more how to use quantized functions in PyTorch, please refer to the Quantization documentation.
Lazy Modules Initialization¶
A mixin for modules that lazily initialize parameters, also known as "lazy modules". 