1、三大容器
- nn.Sequential:按次序包装多个网络层
- nn.ModuleList:像 Python 中的 list 一样包装多个网络层
- nn.ModuleDict:像 Python 中的 dict 一样包装多个网络层
1.1 Sequential
1.1.1 概念
nn.Sequential 是 nn.Module 的容器,用于按次序包装一组网络层
1.1.2 特征
- 次序性:各网络层之间严格按次序构建
- 自带 forward():自带的 forward 里,通过 for 循环依次执行向前传播运算
1.3 代码框架
- 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
复制代码
- LeNetSequentialOrderDict()
- 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
复制代码 1.2 ModuleList
1.2.1 概念
nn.ModuleList 是 nn.module 的容器,用于包装一组网络层,以索引方式调用网络层
1.2.2 重要方法
- append():在 ModuleList 背面添加网络层
- extend():拼接两个 ModuleList
- insert():指定在 ModuleList 中某个位置插入网络层
1.2.3 代码框架
- class ModuleList(nn.Module):
- def __init__(self):
- super(ModuleList, self).__init__()
- self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(20)])
- def forward(self, x):
- for i, linear in enumerate(self.linears):
- x = linear(x)
- return x
复制代码 1.3 ModuleDict
1.3.1 概念
nn.ModuleDict 是 nn.module 的容器,用于包装一组网络层,以索引方式调用网络层
1.3.2 重要方法
- clear():清空 ModuleDict
- items():返回可迭代的键值对(key-value pairs)
- keys():返回字典的键(key)
- values():返回字典的值(value)
- pop():返回一组键值对并从字典中删除
1.3.3 代码框架
- 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
复制代码 1.4 小结
- nn.Sequential:次序性,各网络层之间严格按照次序执行,常用于 block 构建
- nn.ModuleList:迭代性,常用于大量重复网络构建,通过 for 循环实现重复构建
- nn.ModuleDict:字典性,冲用于可选择的网络层构建
2、AlexNet
2.1 配景介绍
2021年 AlextNet 以高出第二名10多个百分点的准确率得到 ImageNet 分类任务冠军,开创了卷积神经网络的新时代
2.2 特征
- 接纳 ReLU 激活函数:替换了 sigmoid 函数,减轻梯度消散的问题
- 接纳 LRN(Local Response Normalization):对数据进行归一化,抑制其对输出的影响,减轻梯度消散的问题
- 接纳 Dropout:提高全毗连层的鲁棒性,增加网络的泛化本领
- 接纳 Data Augmentation:TenCrop 策略,色彩修改
2.3 代码框架
- class AlexNet(nn.Module):
- def __init__(self, num_classes: int = 1000, dropout: float = 0.5) -> None:
- super().__init__()
- _log_api_usage_once(self)
- self.features = nn.Sequential(
- 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(
- nn.Dropout(p=dropout),
- nn.Linear(256 * 6 * 6, 4096),
- nn.ReLU(inplace=True),
- nn.Dropout(p=dropout),
- nn.Linear(4096, 4096),
- nn.ReLU(inplace=True),
- nn.Linear(4096, num_classes),
- )
- def forward(self, x: torch.Tensor) -> torch.Tensor:
- x = self.features(x)
- x = self.avgpool(x)
- x = torch.flatten(x, 1)
- x = self.classifier(x)
- return x
复制代码 微语录:熬过无人问津的日子,才能拥抱诗和远方。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |