目录
1. PyTorch模型定义的方式
- 基于nn.Module,我们可以通过Sequential,ModuleList,ModuleDict三种方式来定义Pytorch模型。
- torch.nn.Module是所有网络的基类,在Pytorch实现的Model都要继承该类。而且,Module是可以包含其他的Module的,以树形的结构来表示一个网络结构
1.1 torch.nn.Sequential
- torch.nn.Sequential 类是 torch.nn 中的一种序列容器,通过在容器中嵌套各种实现神经网络中具体功能相关的类,来完成对神经网络模型的搭建,最主要的是,参数会按照我们定义好的序列自动传递下去。
- 我们可以将嵌套在容器中的各个部分看作是不同的模块,这些模块可以自由组合。
方式一:直接排列
import torch.nn as nn
hidden_layor = 256
input_data = 784
output_data = 10
net = nn.Sequential(
nn.Linear(input_data,hidden_layor), #完成从输入层到隐藏层的线性变换
nn.ReLU(), #激活层
nn.Linear(hidden_layor,output_data), #隐藏层到输出层的线性变换
)
print(net)
方式二:使用OrderDict有序字典进行传入搭建的模型
hidden_layor = 256
input_data = 784
output_data = 10
import collections
import torch.nn as nn
net2 = nn.Sequential(collections.OrderedDict
([
('Line1',nn.Linear(input_data,hidden_layor)),
('Relu1',nn.ReLU()),
('Line2',nn.Linear(hidden_layor,output_data))
]))
print(net2)
以上两种方式的唯一区别在于:
- 使用第二种方式搭建的模型,每一个模块都可以自定义名字
- 第一个方式搭建的模块是默认从0开始的数字序号作为每一个模块的名字
1.2 torch.nn.ModuleList
- ModuleList是以list的形式保存sub-modules或者网络层,这样就可以先将网络需要的layer构建好保存到一个list,然后通过ModuleList方法添加到网络中。
import torch.nn as nn
hidden_layor = 256
input_data = 784
output_data = 10
net = nn.ModuleList([nn.Linear(input_data,hidden_layor),nn.ReLU()])
net.append(nn.Linear(hidden_layor,output_data))
print(net) # 类似List的索引访问
print(net[0])
nn.ModuleList 并没有定义一个网络,它只是将不同的模块储存在一起。ModuleList中元素的先后顺序并不代表其在网络中的真实位置顺序,需要经过forward函数指定各个层的先后顺序后才算完成了模型的定义。如下:
class model(nn.Module):
def __init__(self):
super(model,self).__init__()
# 构建layer的list
self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)])
def forward(self,x):
# 正向传播,使用遍历每个Layer
for i, l in enumerate(self.linears):
x = self.linears[i // 2](x) + l(x)
return x
1.3 torch.nn.ModuleDict
- ModuleDict和ModuleList的作用类似,只是ModuleDict能够更方便地为神经网络的层添加名称。
import torch.nn as nn
hidden_layor = 256
input_data = 784
output_data = 10
net = nn.ModuleDict({
'Line1':nn.Linear(input_data,hidden_layor),
'ReLu':nn.ReLU()})
net['Line2'] = nn.Linear(hidden_layor,output_data)
print(net)
print(net['Line2'])
print(net.Line2)
1.4 实战
import torch
import torch.nn as nn
from torch.autograd import Variable
batch_n = 100 #? 一个批次中输入数据的数量,意味着在一个批次中输入100个数据
hidden_layer = 100
input_data = 1000 #? 1000个特征
output_data = 10
#! 输入输出
x = Variable(torch.randn(batch_n,input_data),requires