Files
Castor/lstm_regularization/locked_dropout.py
T
Ashutosh-Adhikari 91ed6261db Add regularization modules for LSTM baseline (#156)
* Add Regularization Modules for LSTM

* Update Reuters Trainer and Evalueator for regularization

* Remove unnecessary comments

* Comply with PEP8

* Comply import order with PEP8

* Fix typos in README.md

* Comply with PEP8

* Add BSD 3-Clause Licence

* Remove deprecated call to Variable for PyTorch 0.4

* Update dataset selection in main

* Remove block comments
2018-11-06 10:48:28 -05:00

16 lines
423 B
Python

import torch
import torch.nn as nn
from torch.autograd import Variable
class LockedDropout(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x, dropout=0.5):
if not self.training or not dropout:
return x
m = x.data.new(1, x.size(1), x.size(2)).bernoulli_(1 - dropout)
mask = m / (1 - dropout)
mask = mask.expand_as(x)
return mask * x