文章目录
一、模型容器
PyTorch提供的模型容器中有三个非常常用的,这三个容器如下:
-
nn.Sequetial
功能:按顺序的将一组网络层包装起来其特性总结如下:
- 顺序性:各网络层之间严格按照顺序构建
- 自带forward():自带的forward里,通过for循环依次执行前向传播运算
下面使用Sequetial来包装
LeNet
模型class LeNetSequential(nn.Module): def __init__(self, classes): super(LeNetSequential, self).__init__() # 将卷积层与池化层包装成features网络层 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),) # 将全连接网络包装成classifier层 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
其源码为:
class Sequential(Module): r"""A sequential container. Modules will be added to it in the order they are passed in the constructor. Alternatively, an ordered dict of modules can also be passed in. To make it easier to understand, here is a small example:: # Example of using Sequential model = nn.Sequential( nn.Conv2d(1,20,5), nn.ReLU(), nn.Conv2d(20,64,5), nn.ReLU() ) # Example of using Sequential with OrderedDict model = nn.Sequential(OrderedDict([ ('conv1', nn.Conv2d(1,20,5)), ('relu1', nn.ReLU()), ('conv2', nn.Conv2d(20,64,5)), ('relu2', nn.ReLU()) ])) """ def __init__(self, *args): super(Sequential, self).__init__() # 将传入的网络层添加到Sequential中 # 判断输入的参数是否为有序字典 if len(args) == 1 and isinstance(args[0], OrderedDict): for key, module in args[0].items(): self.add_module(key, module) else: # 如果不是的话,就直接将传入的网络层添加到Sequential中 for idx, module in enumerate(args): self.add_module(str(idx), module) def _get_item_by_idx(self, iterator, idx): """Get the idx-th item of the iterator""" size = len(self) idx = operator.index(idx) if not -size <= idx < size: raise IndexError('index {} is out of range'.format(idx)) idx %= size return next(islice(iterator,