这二者在功能上并没有本质的区别,如果我们看nn.dropout的代码,我们会发现
class Dropout(_DropoutNd):
........
@weak_script_method
def forward(self, input):
return F.dropout(input, self.p, self.training, self.inplace)
也就是说其实nn.dropout是调用的F.dropout的函数实现的。
而它们在使用的时候是有区别的。
nn.Dropout派生自nn.Module,通过这样,我们可以把nn.Dropout定义为模型中的一层。所以nn.dropout在模型类的__init__()函数中被定义为一层,而F.dropout在forward()函数中直接使用。
举个简单的例子:
import torch
import torch.nn as nn
class Model1(nn.Module):
# Model 1 using functional dropout
def __init__(self, p=0.0):
super().__init__()
self.p = p
def forward(self, inputs):
return nn.functional.dropout(inputs, p=self.p, training=True)
class Model2(nn.Module):
# Model 2 using dropout module
def __init__(self, p=0.0):
super().__init__()
self.drop_layer = nn.Dropout(p=p)
def forward(self, inputs):
return self.drop_layer(inputs)
model1 = Model1(p=0.5) # functional dropout
model2 = Model2(p=0.5) # dropout module
# creating inputs
inputs = torch.rand(10)
print("inputs", inputs)
# forwarding inputs in train mode
print('Normal (train) model:')
print('Model 1', model1(inputs))
print('Model 2', model2(inputs))
print()
# switching to eval mode
model1.eval()
model2.eval()
# forwarding inputs in evaluation mode
print('Evaluation mode:')
print('Model 1', model1(inputs))
print('Model 2', model2(inputs))
# show model summary
print('Print summary:')
print(model1)
print(model2)
结果
inputs tensor([0.6773, 0.4399, 0.0696, 0.6631, 0.7012, 0.9417, 0.5610, 0.3946, 0.5692,
0.8952])
Normal (train) model:
Model 1 tensor([0.0000, 0.0000, 0.0000, 0.0000, 1.4023, 1.8834, 0.0000, 0.7891, 1.1385,
0.0000])
Model 2 tensor([1.3545, 0.8799, 0.0000, 1.3261, 0.0000, 1.8834, 0.0000, 0.7891, 0.0000,
1.7904])
Evaluation mode:
Model 1 tensor([0.0000, 0.8799, 0.0000, 0.0000, 1.4023, 0.0000, 1.1219, 0.7891, 1.1385,
0.0000])
Model 2 tensor([0.6773, 0.4399, 0.0696, 0.6631, 0.7012, 0.9417, 0.5610, 0.3946, 0.5692,
0.8952])
Print summary:
Model1()
Model2(
(drop_layer): Dropout(p=0.5)
)
可以看到,如果使用的是F.dropout,那么在model.eval的时候如果没有对dropout函数进行调整的话,还是会对输入进行dropout的操作,但是nn.dropout就帮你完成了这一步。这里可以这么使用F.dropout,就跟nn.dropout效果一样。
nn.functional.dropout(inputs, p=self.p, training=self.training)
所以这里其实就根据个人喜好来使用nn.dropout或者F.dropout,有一些观点认为nn.dropout更好,理由如下:
(1)Dropout被设计为只在训练中使用,所以当你对模型进行预测或评估时,你需要关闭Dropout。nn.dropout可以方便地处理这个问题,在模型进入eval时立即关闭Dropout,而F.dropout并care你是什么模式。
(2)分配给模型的所有模块都在模型中注册。所以模型类跟踪它们,这就是为什么可以通过调用eval()关闭dropout模块。当使用F.dropout时,您的模型并不知道它,所以模型的summary中也不会出现dropout模块。
[1]pytorch中torch.nn.dropout和torch.nn.F.dropout区别