Model.eval () turns off BN and dropout. If you want to turn on dropout separately, you can use the following code:
Import torch import torch. Nn as nn def apply_dropout(m): """ "" if type(m) == nn. dropout: m.train() class Net(nn.Module): def __init__(self): Super (Net, self).__init__() self.fc = nn.linear (2, 2) self.dropout = nn.dropout (0.5) def forward(self, x): x = self.dropout(x) x = self.fc(x) x = self.dropout(x) return x net = Net() x = torch.FloatTensor([1] * 2) net.train() y = net(x) print('train mode result 1: ', y) y = net(x) print('train mode result 2: ', y) with torch.no_grad(): net.eval() y = net(x) print('eval mode result 1: ', y) print('eval mode result 2: ', y) net.eval() net.apply(apply_dropout) y = net(x) print('dropout eval mode result 1: ', y) y = net(x) print('dropout eval mode result 2: ', y) y = net(x) print('dropout eval mode result 3: ', y)Copy the code