PyTorch中的层简介

创建日期:2025-03-12
更新日期:2025-03-12

示例代码

x = torch.randn(10, 24)

fc = nn.Linear(in_features=24, out_features=48)
y = fc(x)
print(y)
print(y.shape)

容器(Containers)

说明
ModuleBase class for all neural network modules.
SequentialA sequential container.
ModuleListHolds submodules in a list.
ModuleDictHolds submodules in a dictionary.
ParameterListHolds parameters in a list.
ParameterDictHolds parameters in a dictionary.

示例1

import torch
import torch.nn as nn

class LinearModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(784, 1000)
        self.relu = nn.ReLU()
        self.dropout = nn.Dropout()
        self.fc2 = nn.Linear(1000, 10)

    def forward(self, x):
        out = self.fc1(x)
        out = self.relu(out)
        out = self.dropout(out)
        out = self.fc2(out)
        return out

model = LinearModel()

x = torch.randn(100, 784)
y = model(x)
print(y.shape)

示例2

import torch
import torch.nn as nn

class LinearModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.seq = nn.Sequential(
            nn.Linear(784, 1000), nn.ReLU(), nn.Dropout(), nn.Linear(1000, 10)
        )

    def forward(self, x):
        out = self.seq(x)
        return out

model = LinearModel()

x = torch.randn(100, 784)
y = model(x)
print(y.shape)

卷积层(Convolution Layers)

说明
nn.Conv1dApplies a 1D convolution over an input signal composed of several input planes.
nn.Conv2dApplies a 2D convolution over an input signal composed of several input planes.
nn.Conv3dApplies a 3D convolution over an input signal composed of several input planes.
nn.ConvTranspose1dApplies a 1D transposed convolution operator over an input image composed of several input planes.
nn.ConvTranspose2dApplies a 2D transposed convolution operator over an input image composed of several input planes.
nn.ConvTranspose3dApplies a 3D transposed convolution operator over an input image composed of several input planes.
nn.LazyConv1dA torch.nn.Conv1d module with lazy initialization of the in_channels argument.
nn.LazyConv2dA torch.nn.Conv2d module with lazy initialization of the in_channels argument.
nn.LazyConv3dA torch.nn.Conv3d module with lazy initialization of the in_channels argument.
nn.LazyConvTranspose1dA torch.nn.ConvTranspose1d module with lazy initialization of the in_channels argument.
nn.LazyConvTranspose2dA torch.nn.ConvTranspose2d module with lazy initialization of the in_channels argument.
nn.LazyConvTranspose3dA torch.nn.ConvTranspose3d module with lazy initialization of the in_channels argument.
nn.UnfoldExtracts sliding local blocks from a batched input tensor.
nn.FoldCombines an array of sliding local blocks into a large containing tensor.

池化层(Pooling layers)

说明
nn.MaxPool1dApplies a 1D max pooling over an input signal composed of several input planes.
nn.MaxPool2dApplies a 2D max pooling over an input signal composed of several input planes.
nn.MaxPool3dApplies a 3D max pooling over an input signal composed of several input planes.
nn.MaxUnpool1dComputes a partial inverse of MaxPool1d.
nn.MaxUnpool2dComputes a partial inverse of MaxPool2d.
nn.MaxUnpool3dComputes a partial inverse of MaxPool3d.
nn.AvgPool1dApplies a 1D average pooling over an input signal composed of several input planes.
nn.AvgPool2dApplies a 2D average pooling over an input signal composed of several input planes.
nn.AvgPool3dApplies a 3D average pooling over an input signal composed of several input planes.
nn.FractionalMaxPool2dApplies a 2D fractional max pooling over an input signal composed of several input planes.
nn.FractionalMaxPool3dApplies a 3D fractional max pooling over an input signal composed of several input planes.
nn.LPPool1dApplies a 1D power-average pooling over an input signal composed of several input planes.
nn.LPPool2dApplies a 2D power-average pooling over an input signal composed of several input planes.
nn.LPPool3dApplies a 3D power-average pooling over an input signal composed of several input planes.
nn.AdaptiveMaxPool1dApplies a 1D adaptive max pooling over an input signal composed of several input planes.
nn.AdaptiveMaxPool2dApplies a 2D adaptive max pooling over an input signal composed of several input planes.
nn.AdaptiveMaxPool3dApplies a 3D adaptive max pooling over an input signal composed of several input planes.
nn.AdaptiveAvgPool1dApplies a 1D adaptive average pooling over an input signal composed of several input planes.
nn.AdaptiveAvgPool2dApplies a 2D adaptive average pooling over an input signal composed of several input planes.
nn.AdaptiveAvgPool3dApplies a 3D adaptive average pooling over an input signal composed of several input planes.

填充层(Padding Layers)

说明
nn.ReflectionPad1dPads the input tensor using the reflection of the input boundary.
nn.ReflectionPad2dPads the input tensor using the reflection of the input boundary.
nn.ReflectionPad3dPads the input tensor using the reflection of the input boundary.
nn.ReplicationPad1dPads the input tensor using replication of the input boundary.
nn.ReplicationPad2dPads the input tensor using replication of the input boundary.
nn.ReplicationPad3dPads the input tensor using replication of the input boundary.
nn.ZeroPad1dPads the input tensor boundaries with zero.
nn.ZeroPad2dPads the input tensor boundaries with zero.
nn.ZeroPad3dPads the input tensor boundaries with zero.
nn.ConstantPad1dPads the input tensor boundaries with a constant value.
nn.ConstantPad2dPads the input tensor boundaries with a constant value.
nn.ConstantPad3dPads the input tensor boundaries with a constant value.
nn.CircularPad1dPads the input tensor using circular padding of the input boundary.
nn.CircularPad2dPads the input tensor using circular padding of the input boundary.
nn.CircularPad3dPads the input tensor using circular padding of the input boundary.

正则化层(Normalization Layers)

说明
nn.BatchNorm1dApplies Batch Normalization over a 2D or 3D input.
nn.BatchNorm2dApplies Batch Normalization over a 4D input.
nn.BatchNorm3dApplies Batch Normalization over a 5D input.
nn.LazyBatchNorm1dA torch.nn.BatchNorm1d module with lazy initialization.
nn.LazyBatchNorm2dA torch.nn.BatchNorm2d module with lazy initialization.
nn.LazyBatchNorm3dA torch.nn.BatchNorm3d module with lazy initialization.
nn.GroupNormApplies Group Normalization over a mini-batch of inputs.
nn.SyncBatchNormApplies Batch Normalization over a N-Dimensional input.
nn.InstanceNorm1dApplies Instance Normalization.
nn.InstanceNorm2dApplies Instance Normalization.
nn.InstanceNorm3dApplies Instance Normalization.
nn.LazyInstanceNorm1dA torch.nn.InstanceNorm1d module with lazy initialization of the num_features argument.
nn.LazyInstanceNorm2dA torch.nn.InstanceNorm2d module with lazy initialization of the num_features argument.
nn.LazyInstanceNorm3dA torch.nn.InstanceNorm3d module with lazy initialization of the num_features argument.
nn.LayerNormApplies Layer Normalization over a mini-batch of inputs.
nn.LocalResponseNormApplies local response normalization over an input signal.
nn.RMSNormApplies Root Mean Square Layer Normalization over a mini-batch of inputs.

循环层(Recurrent Layers)

说明
nn.RNNBaseBase class for RNN modules (RNN, LSTM, GRU).
nn.RNNApply a multi-layer Elman RNN with tanh⁡tanh or ReLUReLU non-linearity to an input sequence.
nn.LSTMApply a multi-layer long short-term memory (LSTM) RNN to an input sequence.
nn.GRUApply a multi-layer gated recurrent unit (GRU) RNN to an input sequence.
nn.RNNCellAn Elman RNN cell with tanh or ReLU non-linearity.
nn.LSTMCellA long short-term memory (LSTM) cell.
nn.GRUCellA gated recurrent unit (GRU) cell.

Transformer层(Transformer Layers)

说明
nn.TransformerA transformer model.
nn.TransformerEncoderTransformerEncoder is a stack of N encoder layers.
nn.TransformerDecoderTransformerDecoder is a stack of N decoder layers.
nn.TransformerEncoderLayerTransformerEncoderLayer is made up of self-attn and feedforward network.
nn.TransformerDecoderLayerTransformerDecoderLayer is made up of self-attn, multi-head-attn and feedforward network.

线性层(Linear Layers)

说明
nn.IdentityA placeholder identity operator that is argument-insensitive.
nn.LinearApplies an affine linear transformation to the incoming data: y=xA^T+b, //y//=//xA^T//+//b//.
nn.BilinearApplies a bilinear transformation to the incoming data: y=x1^TAx2+b, //y//=//x//1^//T//​//Ax//2​+//b//.
nn.LazyLinearA torch.nn.Linear module where //in_features// is inferred.

Dropout层(Dropout Layers)

说明
nn.DropoutDuring training, randomly zeroes some of the elements of the input tensor with probability p.
nn.Dropout1dRandomly zero out entire channels.
nn.Dropout2dRandomly zero out entire channels.
nn.Dropout3dRandomly zero out entire channels.
nn.AlphaDropoutApplies Alpha Dropout over the input.
nn.FeatureAlphaDropoutRandomly masks out entire channels.

稀疏层(Sparse Layers)

说明
nn.EmbeddingA simple lookup table that stores embeddings of a fixed dictionary and size.
nn.EmbeddingBagCompute sums or means of 'bags' of embeddings, without instantiating the intermediate embeddings.

视觉层(Vision Layers)

说明
nn.PixelShuffleRearrange elements in a tensor according to an upscaling factor.
nn.PixelUnshuffleReverse the PixelShuffle operation.
nn.UpsampleUpsamples a given multi-channel 1D (temporal), 2D (spatial) or 3D (volumetric) data.
nn.UpsamplingNearest2dApplies a 2D nearest neighbor upsampling to an input signal composed of several input channels.
nn.UpsamplingBilinear2dApplies a 2D bilinear upsampling to an input signal composed of several input channels.

Shuffle Layers

说明
nn.ChannelShuffleDivides and rearranges the channels in a tensor.

DataParallel Layers (multi-GPU, distributed)

说明
nn.DataParallelImplements data parallelism at the module level.
nn.parallel.DistributedDataParallelImplement distributed data parallelism based on torch.distributed at module level.