本文主要是介绍【DeepLearning-3】前馈(feed-forward)神经网络层,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
类定义
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)
这篇关于【DeepLearning-3】前馈(feed-forward)神经网络层的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!