深度学习之网络模型构造
一、继承Module类构造模型
Module
类是nn
模块里提供的一个模型构造类,是所有神经网络模块的基类,我们可以继承它来定义我们想要的模型。下面继承Module
类构造本节开头提到的多层感知机。这里定义的MLP
类重载了Module
类的__init__
函数和forward
函数。它们分别用于创建模型参数和定义前向计算。前向计算也即正向传播。
import torch
from torch import nn
class MLP(nn.Module):
# 声明带有模型参数的层,这里声明了两个全连接层
def __init__(self, **kwargs):
# 调用MLP父类Module的构造函数来进行必要的初始化。这样在构造实例时还可以指定其他函数
# 参数,如“模型参数的访问、初始化和共享”一节将介绍的模型参数params
super(MLP, self).__init__(**kwargs)
self.hidden = nn.Linear(784, 256) # 隐藏层
self.act = nn.ReLU()
self.output = nn.Linear(256, 10) # 输出层
# 定义模型的前向计算,即如何根据输入x计算返回所需要的模型输出
def forward(self, x):
a = self.act(self.hidden(x))
return self.output(a)
可以实例化MLP
类得到模型变量net
。下面的代码初始化net
并传入输入数据X
做一次前向计算。其中,net(X)
会调用MLP
继承自Module
类的__call__
函数,这个函数将调用MLP
类定义的forward
函数来完成前向计算。
X = torch.rand(2, 784)
net = MLP()
print(net)
net(X)
二、Module子类
1、Sequential类
当模型的前向计算为简单串联各个层的计算时,Sequential
类可以通过更加简单的方式定义模型。这正是Sequential
类的目的:它可以接收一个子模块的有序字典(OrderedDict)或者一系列子模块作为参数来逐一添加Module
的实例,而模型的前向计算就是将这些实例按添加的顺序逐一计算。例如下面的AlexNet构造实例:
class AlexNet(nn.Module): # 训练 ALexNet
'''
5层卷积,3层全连接
'''
def __init__(self):
super(AlexNet, self).__init__()
# 五个卷积层 输入 32 * 32 * 3
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=1), # (32-3+2)/1+1 = 32
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0) # (32-2)/2+1 = 16
)
self.conv2 = nn.Sequential( # 输入 16 * 16 * 6
nn.Conv2d(in_channels=6, out_channels=16, kernel_size=3, stride=1, padding=1), # (16-3+2)/1+1 = 16
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0) # (16-2)/2+1 = 8
)
self.conv3 = nn.Sequential( # 输入 8 * 8 * 16
nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, stride=1, padding=1), # (8-3+2)/1+1 = 8
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0) # (8-2)/2+1 = 4
)
self.conv4 = nn.Sequential( # 输入 4 * 4 * 64
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1), # (4-3+2)/1+1 = 4
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0) # (4-2)/2+1 = 2
)
self.conv5 = nn.Sequential( # 输入 2 * 2 * 128
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1), # (2-3+2)/1+1 = 2
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2, padding=0), # (2-2)/2+1 = 1
# nn.Flatten()
) # 最后一层卷积层,输出 1 * 1 * 128
# 全连接层
self.dense = nn.Sequential(
nn.Linear(128, 120),
nn.ReLU(),
# nn.Dropout(),
nn.Linear(120, 84),
nn.ReLU(),
# nn.Dropout(),
nn.Linear(84, 10),
# nn.ReLU(),
# nn.Softmax()
)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = x.view(x.size()[0], -1)
x = self.dense(x)
return x
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
nn.init.constant_(m.bias, 0)
2、ModuleList类
ModuleList
接收一个子模块的列表作为输入,然后也可以类似List那样进行append和extend操作:
net = nn.ModuleList([nn.Linear(784, 256), nn.ReLU()])
net.append(nn.Linear(256, 10)) # # 类似List的append操作
print(net[-1]) # 类似List的索引访问
print(net)
输出为:
Linear(in_features=256, out_features=10, bias=True)
ModuleList(
(0): Linear(in_features=784, out_features=256, bias=True)
(1): ReLU()
(2): Linear(in_features=256, out_features=10, bias=True)
)
Sequential和ModuleList二者区别:
①
ModuleList
仅仅是一个储存各种模块的列表,这些模块之间没有联系也没有顺序(所以不用保证相邻层的输入输出维度匹配,而且没有实现forward功能需要自己实现,所以上面执行net(torch.zeros(1, 784))
会报`NotImplementedError。②
Sequential
内的模块需要按照顺序排列,要保证相邻层的输入输出大小相匹配,内部forward
功能已经实现。
a、ModuleList灵活运用例一
ModuleList
的出现只是让网络定义前向传播时更加灵活
class MyModule(nn.Module):
def __init__(self):
super(MyModule, self).__init__()
self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)])
def forward(self, x):
# ModuleList can act as an iterable, or be indexed using ints
for i, l in enumerate(self.linears):
x = self.linears[i // 2](x) + l(x)
return x
b、ModuleList灵活运用例二
ModuleList
不同于一般的Python的list
,加入到ModuleList
里面的所有模块的参数会被自动添加到整个网络中
class Module_ModuleList(nn.Module):
def __init__(self):
super(Module_ModuleList, self).__init__()
self.linears = nn.ModuleList([nn.Linear(10, 10)])
class Module_List(nn.Module):
def __init__(self):
super(Module_List, self).__init__()
self.linears = [nn.Linear(10, 10)]
net1 = Module_ModuleList()
net2 = Module_List()
print("net1:")
for p in net1.parameters():
print(p.size())
print("net2:")
for p in net2.parameters():
print(p)
输出为:
net1:
torch.Size([10, 10])
torch.Size([10])
net2:
3、ModuleDict类
ModuleDict
接收一个子模块的字典作为输入, 然后也可以类似字典那样进行添加访问操作:
net = nn.ModuleDict({
'linear': nn.Linear(784, 256),
'act': nn.ReLU(),
})
net['output'] = nn.Linear(256, 10) # 添加
print(net['linear']) # 访问
print(net.output)
print(net)
输出为:
Linear(in_features=784, out_features=256, bias=True)
Linear(in_features=256, out_features=10, bias=True)
ModuleDict(
(act): ReLU()
(linear): Linear(in_features=784, out_features=256, bias=True)
(output): Linear(in_features=256, out_features=10, bias=True)
)
ModuleDict与ModuleList的相同点
①:和
ModuleList
一样,ModuleDict
实例仅仅是存放了一些模块的字典,并没有定义forward
函数,需要自己定义。②:
ModuleDict
也与Python的Dict
有所不同,ModuleDict
里的所有模块的参数会被自动添加到整个网络中。
三、自己构造复杂的model
1、构造自己的模型FancyMLP
下面构造一个稍微复杂点的网络FancyMLP
。在这个网络中,我们通过get_constant
函数创建训练中不被迭代的参数,即常数参数。在前向计算中,除了使用创建的常数参数外,我们还使用Tensor
的函数和Python的控制流,并多次调用相同的层。
class FancyMLP(nn.Module):
def __init__(self, **kwargs):
super(FancyMLP, self).__init__(**kwargs)
self.rand_weight = torch.rand((20, 20), requires_grad=False) # 不可训练参数(常数参数)
self.linear = nn.Linear(20, 20)
def forward(self, x):
x = self.linear(x)
# 使用创建的常数参数,以及nn.functional中的relu函数和mm函数
x = nn.functional.relu(torch.mm(x, self.rand_weight.data) + 1)
# 复用全连接层。等价于两个全连接层共享参数
x = self.linear(x)
# 控制流,这里我们需要调用item函数来返回标量进行比较
while x.norm().item() > 1: #norm()是用来求二范数的;item()作用:取出单元素张量的元素值并返回该值,保持原元素类型不变。
x /= 2
if x.norm().item() < 0.8:
x *= 10
return x.sum()
X = torch.rand(2, 20)
net = FancyMLP()
print(net)
net(X)
输出为:
FancyMLP(
(linear): Linear(in_features=20, out_features=20, bias=True)
)
tensor(0.8432, grad_fn=<SumBackward0>)
2、使用上述构造的fancyMLP嵌套构造新网络NestMLP
因为FancyMLP
和Sequential
类都是Module
类的子类,所以可以嵌套调用它们。
class NestMLP(nn.Module):
def __init__(self, **kwargs):
super(NestMLP, self).__init__(**kwargs)
self.net = nn.Sequential(nn.Linear(40, 30), nn.ReLU())
def forward(self, x):
return self.net(x)
net = nn.Sequential(NestMLP(), nn.Linear(30, 20), FancyMLP())
X = torch.rand(2, 40)
print(net)
net(X)
输出:
Sequential(
(0): NestMLP(
(net): Sequential(
(0): Linear(in_features=40, out_features=30, bias=True)
(1): ReLU()
)
)
(1): Linear(in_features=30, out_features=20, bias=True)
(2): FancyMLP(
(linear): Linear(in_features=20, out_features=20, bias=True)
)
)
tensor(14.4908, grad_fn=<SumBackward0>)
小结
- 可以通过继承
Module
类来构造模型。 Sequential
、ModuleList
、ModuleDict
类都继承自Module
类。- 与
Sequential
不同,ModuleList
和ModuleDict
并没有定义一个完整的网络,它们只是将不同的模块存放在一起,需要自己定义forward
函数。 - 虽然
Sequential
等类可以使模型构造更加简单,但直接继承Module
类可以极大地拓展模型构造的灵活性。