torch.nn¶
These are the basic building blocks for graphs:
torch.nn
Parameter |
A kind of Tensor that is to be considered a module parameter. |
UninitializedParameter |
A parameter that is not initialized. |
UninitializedBuffer |
A buffer that is not initialized. |
Containers¶
Module |
Base class for all neural network modules. |
Sequential |
A sequential container. |
ModuleList |
Holds submodules in a list. |
ModuleDict |
Holds submodules in a dictionary. |
ParameterList |
Holds parameters in a list. |
ParameterDict |
Holds parameters in a dictionary. |
Global Hooks For Module
register_module_forward_pre_hook |
Register a forward pre-hook common to all modules. |
register_module_forward_hook |
Register a global forward hook for all the modules. |
register_module_backward_hook |
Register a backward hook common to all the modules. |
register_module_full_backward_pre_hook |
Register a backward pre-hook common to all the modules. |
register_module_full_backward_hook |
Register a backward hook common to all the modules. |
register_module_buffer_registration_hook |
Register a buffer registration hook common to all modules. |
register_module_module_registration_hook |
Register a module registration hook common to all modules. |
register_module_parameter_registration_hook |
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 power-average pooling over an input signal composed of several input planes. |
|
Applies a 2D power-average pooling over an input signal composed of several input planes. |
|
Applies a 3D power-average 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. |
Non-linear Activations (weighted sum, nonlinearity)¶
Applies the Exponential Linear Unit (ELU) function, element-wise. |
|
Applies the Hard Shrinkage (Hardshrink) function element-wise. |
|
Applies the Hardsigmoid function element-wise. |
|
Applies the HardTanh function element-wise. |
|
Applies the Hardswish function, element-wise. |
|
Applies the LeakyReLU function element-wise. |
|
Applies the Logsigmoid function element-wise. |
|
Allows the model to jointly attend to information from different representation subspaces. |
|
Applies the element-wise PReLU function. |
|
Applies the rectified linear unit function element-wise. |
|
Applies the ReLU6 function element-wise. |
|
Applies the randomized leaky rectified linear unit function, element-wise. |
|
Applies the SELU function element-wise. |
|
Applies the CELU function element-wise. |
|
Applies the Gaussian Error Linear Units function. |
|
Applies the Sigmoid function element-wise. |
|
Applies the Sigmoid Linear Unit (SiLU) function, element-wise. |
|
Applies the Mish function, element-wise. |
|
Applies the Softplus function element-wise. |
|
Applies the soft shrinkage function element-wise. |
|
Applies the element-wise Softsign function. |
|
Applies the Hyperbolic Tangent (Tanh) function element-wise. |
|
Applies the element-wise Tanhshrink function. |
|
Thresholds each element of the input Tensor. |
|
Applies the gated linear unit function. |
Non-linear Activations (other)¶
Applies the Softmin function to an n-dimensional input Tensor. |
|
Applies the Softmax function to an n-dimensional input Tensor. |
|
Applies SoftMax over features to each spatial location. |
|
Applies the function to an n-dimensional 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 mini-batch of inputs. |
|
Applies Batch Normalization over a N-Dimensional input. |
|
Applies Instance Normalization. |
|
Applies Instance Normalization. |
|
Applies Instance Normalization. |
|
A |
|
A |
|
A |
|
Applies Layer Normalization over a mini-batch of inputs. |
|
Applies local response normalization over an input signal. |
|
Applies Root Mean Square Layer Normalization over a mini-batch of inputs. |
Recurrent Layers¶
Base class for RNN modules (RNN, LSTM, GRU). |
|
Apply a multi-layer Elman RNN with or non-linearity to an input sequence. |
|
Apply a multi-layer long short-term memory (LSTM) RNN to an input sequence. |
|
Apply a multi-layer gated recurrent unit (GRU) RNN to an input sequence. |
|
An Elman RNN cell with tanh or ReLU non-linearity. |
|
A long short-term 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 self-attn and feedforward network. |
|
TransformerDecoderLayer is made up of self-attn, multi-head-attn and feedforward network. |
Linear Layers¶
A placeholder identity operator that is argument-insensitive. |
|
Applies an affine linear transformation to the incoming data: . |
|
Applies a bilinear transformation to the incoming data: . |
|
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 and , 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 and target . |
|
Creates a criterion that measures the mean squared error (squared L2 norm) between each element in the input and target . |
|
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 Kullback-Leibler 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 , , two 1D mini-batch or 0D Tensors, and a label 1D mini-batch or 0D Tensor (containing 1 or -1). |
|
Measures the loss given an input tensor and a labels tensor (containing 1 or -1). |
|
Creates a criterion that optimizes a multi-class multi-classification hinge loss (margin-based loss) between input (a 2D mini-batch Tensor) and output (which is a 2D Tensor of target class indices). |
|
Creates a criterion that uses a squared term if the absolute element-wise error falls below delta and a delta-scaled L1 term otherwise. |
|
Creates a criterion that uses a squared term if the absolute element-wise error falls below beta and an L1 term otherwise. |
|
Creates a criterion that optimizes a two-class classification logistic loss between input tensor and target tensor (containing 1 or -1). |
|
Creates a criterion that optimizes a multi-label one-versus-all loss based on max-entropy, between input and target of size . |
|
Creates a criterion that measures the loss given input tensors , and a Tensor label with values 1 or -1. |
|
Creates a criterion that optimizes a multi-class classification hinge loss (margin-based loss) between input (a 2D mini-batch Tensor) and output (which is a 1D tensor of target class indices, ): |
|
Creates a criterion that measures the triplet loss given an input tensors , , and a margin with a value greater than . |
|
Creates a criterion that measures the triplet loss given input tensors , , and (representing anchor, positive, and negative examples, respectively), and a nonnegative, real-valued 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 multi-channel 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 (multi-GPU, 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_grad_norm_ |
Clip the gradient norm of an iterable of parameters. |
clip_grad_norm |
Clip the gradient norm of an iterable of parameters. |
clip_grad_value_ |
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.
parameters_to_vector |
Flatten an iterable of parameters into a single vector. |
vector_to_parameters |
Copy slices of a vector into an iterable of parameters. |
Utility functions to fuse Modules with BatchNorm modules.
fuse_conv_bn_eval |
Fuse a convolutional module and a BatchNorm module into a single, new convolutional module. |
fuse_conv_bn_weights |
Fuse convolutional module parameters and BatchNorm module parameters into new convolutional module parameters. |
fuse_linear_bn_eval |
Fuse a linear module and a BatchNorm module into a single, new linear module. |
fuse_linear_bn_weights |
Fuse linear module parameters and BatchNorm module parameters into new linear module parameters. |
Utility functions to convert Module parameter memory formats.
convert_conv2d_weight_memory_format |
Convert |
convert_conv3d_weight_memory_format |
Convert |
Utility functions to apply and remove weight normalization from Module parameters.
weight_norm |
Apply weight normalization to a parameter in the given module. |
remove_weight_norm |
Remove the weight normalization reparameterization from a module. |
spectral_norm |
Apply spectral normalization to a parameter in the given module. |
remove_spectral_norm |
Remove the spectral normalization reparameterization from a module. |
Utility functions for initializing Module parameters.
skip_init |
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 L1-norm. |
|
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 L1-norm. |
|
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 pre-hooks. |
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". |