1.torch.nn
是专门为深度学习设计的模块,所以一般进行网络设计时都必须先import torch.nn
下面是torch.nn中包含的一些子模块
接下来介绍其中比较重要的几个
2.Containers
顾名思义,容器,都是一些装东西的类
1)Module
module是神经网络的基类,我们自己设计的网络都需要继承自这个基类,module也可以包含其他module(关于模型嵌套文档里讲的比较多,以后再深入研究)。
一般定义网络时都是这句
class Net(nn.Module):
2)Sequential
这是一个序列模型,就是按照顺序排列的一个Module
# 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())
]))
3)ModuleList
用一个list来储存submodules,我们这里先不看submodule
4)ModuleDict
用一个dict来储存submodules,我们这里先不看submodule
5)ParameterList
ParameterList
可以像一个常规的python链表一样被索引
6)ParameterDict
同上