PyTorch - How To Deactivate Dropout In Evaluation Mode
Solution 1:
You have to define your nn.Dropout
layer in your __init__
and assign it to your model to be responsive for calling eval()
.
So changing your model like this should work for you:
class mylstm(nn.Module):
def __init__(self,input_dim, output_dim, hidden_dim,linear_dim,p):
super(mylstm, self).__init__()
self.hidden_dim=hidden_dim
self.lstm=nn.LSTMCell(input_dim,self.hidden_dim)
self.linear1=nn.Linear(hidden_dim,linear_dim)
self.linear2=nn.Linear(linear_dim,output_dim)
# define dropout layer in __init__
self.drop_layer = nn.Dropout(p=p)
def forward(self, input):
out,_= self.lstm(input)
# apply model dropout, responsive to eval()
out= self.drop_layer(out)
out= self.linear1(out)
# apply model dropout, responsive to eval()
out= self.drop_layer(out)
out= self.linear2(out)
return out
If you change it like this dropout will be inactive as soon as you call eval()
.
NOTE: If you want to continue training afterwards you need to call train()
on your model to leave evaluation mode.
You can also find a small working example for dropout with eval()
for evaluation mode here:
nn.Dropout vs. F.dropout pyTorch
Solution 2:
I add this answer just because I'm facing now the same issue while trying to reproduce Deep Bayesian active learning through dropout disagreement. If you need to keep dropout active (for example to bootstrap a set of different predictions for the same test instances) you just need to leave the model in training mode, there is no need to define your own dropout layer.
- Does Applying A Dropout Layer After The Embedding Layer Have The Same Effect As Applying The Dropout Through The Lstm Dropout Parameter?
- On Training Lstms Efficiently But Well, Parallelism Vs Training Regime
- Why The Model Is Training On Only 1875 Training Set Images If There Are 60000 Images In The Mnist Dataset?
Since in pytorch you need to define your own prediction function, you can just add a parameter to it like this:
def predict_class(model, test_instance, active_dropout=False):
if active_dropout:
model.train()
else:
model.eval()
Solution 3:
As the other answers said, the dropout layer is desired to be defined in your model's __init__
method, so that your model can keep track of all information of each pre-defined layer. When the model's state is changed, it would notify all layers and do some relevant work. For instance, while calling model.eval()
your model would deactivate the dropout layers but directly pass all activations. In general, if you wanna deactivate your dropout layers, you'd better define the dropout layers in __init__
method using nn.Dropout
module.
Post a Comment for "PyTorch - How To Deactivate Dropout In Evaluation Mode"