torch.nn
- nn.Parameter:张量子类表示可学习参数,如weight, bias
- nn.Module:所有网络层基类,管理网络属性
- nn.functional:函数具体实现,如卷积、池化、激活函数等
- nn.init:参数初始化方法
nn.Module中重要属性
- parameters:存储管理nn.Parameter类
- modules:存储管理nn.Module类
- buffers :存储管理缓冲属性,如BN层中的running_ mean
- ***_hooks:存储管理钩子函数
self._parameters = OrderedDict()
self._buffers = OrderedDict()
self._backward_hooks = OrderedDict()
self._forward_hooks = OrderedDict()
self._forward_pre_hooks = OrderedDict()
self._state_dict_hooks = OrderedDict()
self._load_state_dict_pre_hooks = OrderedDict()
self._modules = OrderedDict()
nn.Module总结
- 一个module可以包含多个子module
- 一个module相当于一个运算,必须实现forward()函数
- 每个module都有8个字典管理它的属性
Containers容器(实现模型搭建的3种方式)
nn.Sequential:按顺序包装多个网络层
顺序性:各网络层之间严格按照顺序构建
自带forward():自带的forward里,通过for循环依次进行前向传播运算。
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
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
nn.ModuleList:像python的list一样包装多个网络层,以迭代方式调用网络层
主要方法:
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)])
def forward(self, x):
for i, linear in enumerate(self.linears):
x = linear(x)
return x
nn.ModuleDict:像python的dict一样包装多个网络层,以索引的方式调用网络层
主要方法:
clear() :清空ModuleDict
items() :返回可迭代的键值对(key-value pairs)
keys() :返回字典的键(key)
values() :返回字典的值(value)
pop() :返回一对键值,并从字典中删除
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
Containers总结
nn. Sequentialo顺序性:各网络层之间严格按顺序执行,常用于block构建
nn.ModuleList :迭代性:常用于大量重复网构建,通过for循环实现重复构建
nn. ModuleDict :索引性:常用于可选择的网络层