其实这二者在功能上并没有本质的区别,如果我们看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()函数中直接使用。
举个简单的例子,例子来自stackoverflow
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更好,理由如下: