关于Pytorch的MLP模块实现方式

Fronde ·
更新时间:2024-09-21
· 800 次阅读

MLP分类效果一般好于线性分类器,即将特征输入MLP中再经过softmax来进行分类。

具体实现为将原先线性分类模块:

self.classifier = nn.Linear(config.hidden_size, num_labels)

替换为:

self.classifier = MLP(config.hidden_size, num_labels)

并且添加MLP模块:

class MLP(nn.Module): def __init__(self, input_size, common_size): super(MLP, self).__init__() self.linear = nn.Sequential( nn.Linear(input_size, input_size // 2), nn.ReLU(inplace=True), nn.Linear(input_size // 2, input_size // 4), nn.ReLU(inplace=True), nn.Linear(input_size // 4, common_size) ) def forward(self, x): out = self.linear(x) return out

看一下模块结构:

mlp = MLP(1000,3) print(mlp)

以上这篇关于Pytorch的MLP模块实现方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。

您可能感兴趣的文章:pytorch中的transforms模块实例详解Pytorch之parameters的使用pytorch载入预训练模型后,实现训练指定层pytorch构建网络模型的4种方法



mlp pytorch

需要 登录 后方可回复, 如果你还没有账号请 注册新账号