目录
3--Model.train()函数和Model.eval()函数
1-- torch.nn.Sequential( )函数
作用:通过顺序序列创建神经网络结构
示例:调用Block函数时,将顺序执行括号内的Function。
import torch
x = torch.randn(1, 1, 64, 64)
Block = torch.nn.Sequential(
torch.nn.Conv2d(in_channels = 1, out_channels = 32, kernel_size = 3, stride = 1),
torch.nn.ReLU(),
torch.nn.Conv2d(in_channels = 32, out_channels = 32, kernel_size = 3, stride = 1),
torch.nn.ReLU()
)
result = Block(x)
print(result)
上述代码等同于:
import torch
Conv1 = torch.nn.Conv2d(in_channels = 1, out_channels = 32, kernel_size = 3, stride = 1)
Conv2 = torch.nn.Conv2d(in_channels = 32, out_channels = 32, kernel_size = 3, stride = 1),
Relu = torch.nn.ReLU()
x = torch.randn(1, 1, 64, 64)
print(x)
x = Conv1(x)
x = Relu(x)
x = Conv2(x)
result = Relu(x)
print(result)
############################
2--torch.flatten()函数
作用:按维度进行拼接;torch.flatten(input, start_dim = 0, end_dim = -1);input为输入tensor数据,start_dim为拼接的起始维度,end_dim为拼接的终止维度。
示例:
import torch
x = torch.randn(2, 3, 3)
print(x)
result1 = torch.flatten(x, start_dim = 0, end_dim = 2)
print(result1)
result2 = torch.flatten(x, start_dim = 0, end_dim = 1)
print(result2)
result3 = torch.flatten(x, start_dim = 1, end_dim = 2)
print(result3)
结果:
tensor([[[ 0.3546, -0.8551, 2.3490],
[-0.0920, 0.0773, -0.4556],
[-1.6943, 1.4517, -0.0767]],
[[-0.6950, 0.4382, -1.2691],
[-0.0252, -0.4980, -0.5994],
[-0.2581, -0.2544, -0.6787]]]) #X
tensor([ 0.3546, -0.8551, 2.3490, -0.0920, 0.0773, -0.4556, -1.6943, 1.4517,
-0.0767, -0.6950, 0.4382, -1.2691, -0.0252, -0.4980, -0.5994, -0.2581,
-0.2544, -0.6787]) #result1
tensor([[ 0.3546, -0.8551, 2.3490],
[-0.0920, 0.0773, -0.4556],
[-1.6943, 1.4517, -0.0767],
[-0.6950, 0.4382, -1.2691],
[-0.0252, -0.4980, -0.5994],
[-0.2581, -0.2544, -0.6787]]) #result2
tensor([[ 0.3546, -0.8551, 2.3490, -0.0920, 0.0773, -0.4556, -1.6943, 1.4517,
-0.0767],
[-0.6950, 0.4382, -1.2691, -0.0252, -0.4980, -0.5994, -0.2581, -0.2544,
-0.6787]]) #result3
############################
3--Model.train()函数和Model.eval()函数
Model.train()函数用于模型的训练状态,将模型置于训练模式;
Model.eval()函数用于模型的测试状态,将模型置于测试模式;
模型处于训练和测试这两种不同的模式,有一些函数所起的作用是不同的,如:Batch Normalization 和 Dropout(训练模式时将启用,测试模式时则禁用);
如果模型中有BN层(Batch Normalization)和Dropout,需要在训练时添加model.train()。model.train()是保证BN层能够用到每一批数据的均值和方差。对于Dropout,model.train()是随机取一部分网络连接来训练更新参数。
如果模型中有BN层(Batch Normalization)和Dropout,在测试时添加model.eval()。model.eval()是保证BN层能够用全部训练数据的均值和方差,即测试过程中要保证BN层的均值和方差不变。对于Dropout,model.eval()是利用到了所有网络连接,即不进行随机舍弃神经元。
训练完train样本后,生成的模型model要用来测试样本。在model(test)之前,需要加上model.eval(),否则的话,有输入数据,即使不训练,它也会改变权值。这是model中含有BN层和Dropout所带来的的性质。
参考链接