关于Pytorch的MLP模块实现方式

yipeiwu_com6年前Python基础

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模块实现方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python实现列表的排序方法分享

python实现列表的排序方法分享

这次代码主要是实现列表的排序,使用sort函数实现,sort函数是对列表中的元素按照特定顺序进行排序,默认reverse,为false,从小到大排序, 如果指定reverse=...

pyqt5简介及安装方法介绍

pyqt5简介及安装方法介绍

本文研究的主要是pyqt5简介及安装方法介绍的有关内容,具体如下。 pyqt5介绍 pyqt5是一套Python绑定Digia QT5应用的框架。它可用于Python 2和3。本教程使用...

对python3中, print横向输出的方法详解

Python 2 : print打印的时候,如果结尾有逗号,打出来时候不会换行。但是在python3里面就不行了。 Python3 : 3.0的print最后加个参数end=""就可以了...

Python使用filetype精确判断文件类型

filetype.py Small and dependency free Python package to infer file type and MIME type checkin...

Python迭代用法实例教程

本文实例讲述了Python中迭代的用法,是一个非常实用的技巧。分享给大家供大家参考借鉴之用。具体分析如下: 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或t...