关于Pytorch的MLP模块实现方式

时间:2021-05-22

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模块实现方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章