1、模型容器Containers
pytorch的Containers中有三个常用的模块:
- nn.Sequential:按顺序包装多个网络层
- nn.ModuleList:像python的list一样包装多个网络层
- nn.ModuleDict:像python的dict一样包装多个网络层
1.1 容器之Sequential
nn.Sequential是nn.module的容器,用于按顺序包装一组网络层,下面通过代码观察怎么用Sequential包装LeNet网络。
class LeNetSequential(nn.Module):
def __init__(self, classes):
super(LeNetSequential, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 6, 5),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Conv2d(6, 16, 5),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),)
self.classifier = nn.Sequential(
nn.Linear(16*5*5, 120),
nn.ReLU(),
nn.Linear(120, 84),
nn.ReLU(),
nn.Linear(84, classes),)
def forward(self, x):
x = self.features(x)
x = x.view(x.size()[0], -1) # 展开,形状变换
x = self.classifier(x)
return x
在__init__()模块中,采用sequential()对卷积层和池化层进行包装,把sequential类属性赋予feature,然后对三个全连接层进行sequential包装,赋值为classifier类属性,这就完成了模型构建的第一步。foward构建了前向传播过程,只有三行,非常简洁。
通过以下代码进行调试,进入LeNetSequential()进行观察,观察nn.Sequential的构建:
net = LeNetSequential(classes=2)
点击步进进入container.py文件当中:
class Sequential(Module):
def __init__(self, *args):
super(Sequential, self).__init__()
if len(args) == 1 and isinstance(args[0], OrderedDict):
for key, module in args[0].items():
self.add_module(key, module)
else:
for idx, module in enumerate(args):
self.add_module(str(idx), module)
super(Sequential, self).init()代码的作用是产生8个属性字典,接着对传入的网络层添加到sequential()当中。代码if len(args) == 1 and isinstance(args[0], OrderedDict):会做一个判断,判断输入的参数的数据类型是不是一个有序字典,因为输入不是一个有序字典,所以会跳转到else中,else的代码是进行一个for循环,对输入的参数循环地取出每一个网络层,然后采用model的类方法add_module将网络层添加到sequential当中。这样就通过容器sequential对网络层进行包装。
现在看一下forward()前向传播过程的具体实现。
def forward(self, x):
x = self.features(x)
x = x.view(x.size()[0], -1) # 展开,形状变换
x = self.classifier(x)
return x
进入self.feature(x)看看具体的运作原理,进入container.py的sequential类的forward()函数当中:
class Sequential(Module):
def forward(self, input):
for module in self._modules.values():
input = module(input)
return input
代码进行一个for循环,对网络层进行遍历,不断运行每个网络层,由于当前层的output是下一层的input,数据形状不能出错。这样就完成了forward的操作。
我们把LeNet网络层打印出来,如下所示:
我们用sequential构建LeNet,LeNet中有一个features,类型为sequential,sequential中有六个网络层,以序号(0)-(5)命名;还有一个classifier,一样是sequential。这里存在一个问题,这里的网络层是没有名字的,是通过序号索引的,如果在一个上千层的网络中,很难采用序号去进行索引每一个网络层。这时候可以对网络层进行命名,这就是第二种sequential的方法,对sequential输入一个头有序的字典,以这种方式构建网络,代码如下所示。
class LeNetSequentialOrderDict(nn.Module):
def __init__(self, classes):
super(LeNetSequentialOrderDict, self).__init__()
self.features = nn.Sequential(OrderedDict({
'conv1': nn.Conv2d(3, 6, 5),
'relu1': nn.ReLU(inplace=True),
'pool1': nn.MaxPool2d(kernel_size=2, stride=2),
'conv2': nn.Conv2d(6, 16, 5),
'relu2': nn.ReLU(inplace=True),
'pool2': nn.MaxPool2d(kernel_size=2, stride=2),
}))
self.classifier = nn.Sequential(OrderedDict({
'fc1': nn.Linear(16*5*5, 120),
'relu3': nn.ReLU(),
'fc2': nn.Linear(120, 84),
'relu4': nn.ReLU(inplace=True),
'fc3': nn.Linear(84, classes),
}))
def forward(self, x):
x = self.features(x)
x = x.view(x.size()[0], -1)
x = self.classifier(x)
return x
下面通过单步调试看看sequential是怎么命名的。创建以下代码调用LeNetSequentialOrderDict类:
net = LeNetSequentialOrderDict(classes=2)
通过单步调试进入类sequential的init()函数,代码如下所示:
class Sequential(Module):
def __init__(self, *args):
super(Sequential, self).__init__()
if len(args) == 1 and isinstance(args[0], OrderedDict):
for key, module in args[0].items():
self.add_module(key, module)
else:
for idx, module in enumerate(args):
self.add_module(str(idx), module)
if len(args) == 1 and isinstance(args[0], OrderedDict):会对输入参数进行判断,判断其长度是否为1,同时判断是否是一个有序字典。由于我们传入的是一个字典,所以会进入该条件下的for循环语句:
if len(args) == 1 and isinstance(args[0], OrderedDict):
for key, module in args[0].items():
self.add_module(key, module)
代码中的key就是我们设置的网络层名字,module就是对应的网络层。通过不断传入到sequential当中,就得到了有命名的网络层。这样网络层就有名称了,可以通过名称调用网络层。
1.1.1总结
nn.Sequential是nn.module的容器,用于按顺序包装一组网络层:
- 顺序性:各网络层之间严格按照顺序构建;
- 自带forward():自带的forward里,通过for循环依次执行前向传播运算;
1.2 容器之ModuleList
nn.ModuleList是nn.module的容器,用于包装一组网络层,以迭代的方式调用网络层。
主要方法:
- append():在ModuleList后面添加网络层;
- extend():拼接两个ModuleList;
- insert():指定在ModuleList中位置插入网络层;
通过代码查看具体的应用方式:
class ModuleList(nn.Module):
def __init__(self):
super(ModuleList, self).__init__()
self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(20)]) # 列表生成式,采用for循环
def forward(self, x):
for i, linear in enumerate(self.linears):
x = linear(x)
return x
net = ModuleList()
通过单步调试进入nn.ModuleList()类当中:
class ModuleList(Module):
def __init__(self, modules=None):
super(ModuleList, self).__init__()
if modules is not None:
self += modules
通过单步调试,进入了nn.ModuleList的init()函数当中,通过代码可以发现,如果mpdules不是空的话,则会不断进行叠加,得到linear。
可以看到,通过nn.ModuleList可以简便地建立一个二十层的全连接网络模型。
1.3 容器之ModuleLDict
nn.ModuleLDict是nn.module的容器,用于包装一组网络层,以索引方式调用网络层。
主要方法:
- clear():清空ModuleDict
- items():返回可迭代的键值对(key-value)
- keys():返回字典的键(key)
- values():返回字典的值(value)
- pop():返回一对键值,并从字典中删除
下面通过代码观察ModuleLDict的使用方法:
class ModuleDict(nn.Module):
def __init__(self):
super(ModuleDict, self).__init__()
self.choices = nn.ModuleDict({
'conv': nn.Conv2d(10, 10, 3),
'pool': nn.MaxPool2d(3)
})
self.activations = nn.ModuleDict({
'relu': nn.ReLU(),
'prelu': nn.PReLU()
})
def forward(self, x, choice, act):
x = self.choices[choice](x)
x = self.activations[act](x)
return x
net = ModuleDict()
fake_img = torch.randn((4, 10, 32, 32))
output = net(fake_img, 'conv', 'relu')
1.4 容器总结
- nn.Sequential:顺序性:各网络层之间严格按顺序执行,常用于block构建;
- nn.ModuleList:迭代性,常用于大量重复网的构建,通过for循环实现重复构建;
- nn.ModuleDict:索引性,常用于可选择的网络层;
2、AlexNet构建
AlexNet:2012年以高出第二名10多个百分点的准确率获得ImageNet分类任务冠军,开创了卷积神经网络的新时代;
AlexNet特点如下:
- 常用ReLU:替换饱和激活函数,减轻梯度消失;
- 采用LRN(Local Response Normalization):对数据进行归一化,减轻梯度消失;
- Dropout:提高全连接层的鲁棒性,增加网络的泛化能力;
- Data Augmentation:TenCrop,色彩修改;
下面观察pytorch中AlexNet网络的实现,通过以下代码查看:
alexnet = torchvision.models.AlexNet()
其整体结构如下所示:
class AlexNet(nn.Module):
def __init__(self, num_classes=1000):
super(AlexNet, self).__init__()
self.features = nn.Sequential( # sequential,将卷积池化的一系列操作打包构成一个feature提取
nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(64, 192, kernel_size=5, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(192, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
)
self.avgpool = nn.AdaptiveAvgPool2d((6, 6)) # 构建一个池化层
self.classifier = nn.Sequential( #采用sequential构建一个分类器
nn.Dropout(),
nn.Linear(256 * 6 * 6, 4096),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, num_classes),
)
def forward(self, x):
x = self.features(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.classifier(x)
return x