目录
2. torch.flatten(input, start_dim=0, end_dim=-1)
7.pytorch中model eval和torch no grad()的区别
1. nn.Sequential
顺序容器。模块将按照在构造函数中传递的顺序添加到它。或者,也可以传入模块的有序字典。
# 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())
]))
2. torch.flatten(input, start_dim=0, end_dim=-1)
input: 一个 tensor,也就是要被展平 tensor。
start_dim: 起始维度。
end_dim: 结束维度。
t = torch.tensor([[[1, 2, 2, 1], [3, 4, 4, 3], [1, 2, 3, 4]], [[5, 6, 6, 5], [7, 8, 8, 7], [5, 6, 7, 8]]]) print(t, t.shape) 运行后: tensor([[[1, 2, 2, 1], [3, 4, 4, 3], [1, 2, 3, 4]], [[5, 6, 6, 5], [7, 8, 8, 7], [5, 6, 7, 8]]]) torch.Size([2, 3, 4]) x = torch.flatten(t, start_dim=1) # 第1维到最后一维展平 print(x, x.shape) y = torch.flatten(t, start_dim=0, end_dim=1) # 第0维到第1维合并 print(y, y.shape) 运行后: tensor([[1, 2, 2, 1, 3, 4, 4, 3, 1, 2, 3, 4], [5, 6, 6, 5, 7, 8, 8, 7, 5, 6, 7, 8]]) torch.Size([2, 12]) tensor([[1, 2, 2, 1], [3, 4, 4, 3], [1, 2, 3, 4], [5, 6, 6, 5], [7, 8, 8, 7], [5, 6, 7, 8]]) torch.Size([6, 4])
3.model.train()和model.eval()
分别在训练和测试中都要写,它们的作用如下:
(1). model.train()
启用 BatchNormalization 和 Dropout,将BatchNormalization和Dropout置为True
(2). model.eval()
不启用 BatchNormalization 和 Dropout,将BatchNormalization和Dropout置为False总结
(1)在训练模块中千万不要忘了写model.train()
(2)在评估(或测试)模块千万不要忘了写model.eval()
(3)在没有涉及到BN与Dropout的网络,这两个函数存在对于网络性能的影响,好坏不定
4.optimizer优化器