类定义
class FeedForward(nn.Module):
FeedForward 类继承自 nn.Module,nn.Module是PyTorch中所有神经网络模块的基类。nn.Module允许您创建自己的神经网络层。
nn.Module 是构建所有神经网络层和模型的基类。当创建一个类继承自 nn.Module时,意味着你在创建一个自定义的神经网络层或整个神经网络模型。
构造函数 __init__
def __init__(self, dim, hidden_dim, dropout=0.):
- 构造函数用于初始化
FeedForward 类的实例。 dim: 输入和输出的维度。hidden_dim: 隐藏层的维度。dropout: Dropout率,用于正则化以防止过拟合,默认值为0。
网络结构
self.net = nn.Sequential( nn.Linear(dim, hidden_dim), nn.SiLU(), nn.Dropout(dropout), nn.Linear(hidden_dim, dim), nn.Dropout(dropout) )
- 使用
nn.Sequential 来定义一个简单的序列模型。 nn.Linear(dim, hidden_dim): 第一个线性层,将输入的维度从 dim 转换到 hidden_dim。nn.SiLU(): 激活函数,SiLU(Sigmoid Linear Unit),也称为Swish激活函数。nn.Dropout(dropout): 应用dropout正则化。nn.Linear(hidden_dim, dim): 第二个线性层,将维度从 hidden_dim 转换回 dim。nn.Dropout(dropout): 再次应用dropout正则化。
前向传播函数 forward
def forward(self, x): return self.net(x)
forward 函数定义了数据通过网络的方式。x 是输入数据。self.net(x) 通过之前定义的序列模型(即网络结构)处理 x,然后返回结果。
完整代码:
class FeedForward(nn.Module):def __init__(self, dim, hidden_dim, dropout=0.):super().__init__()self.net = nn.Sequential(nn.Linear(dim, hidden_dim),nn.SiLU(),nn.Dropout(dropout),nn.Linear(hidden_dim, dim),nn.Dropout(dropout))def forward(self, x):return self.net(x)