Skip to content

Commit

Permalink
Update awd_lstm.py (fastai#2517)
Browse files Browse the repository at this point in the history
When the weights in an embedding layer are initialised all of the weights are updated, including those set to zero at the padding index. This means that the weights at the padding idx are now non zero, which I don't think can be desirable. The change I am proposing just reinstates the zeros at the padding index.
  • Loading branch information
garethmd authored Mar 10, 2020
1 parent 9a9d8a4 commit f15ecfc
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion fastai/text/models/awd_lstm.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def __init__(self, vocab_sz:int, emb_sz:int, n_hid:int, n_layers:int, pad_token:
self.rnns = [WeightDropout(rnn, weight_p) for rnn in self.rnns]
self.rnns = nn.ModuleList(self.rnns)
self.encoder.weight.data.uniform_(-self.initrange, self.initrange)
if self.encoder.padding_idx is not None:
self.encoder.weight.data[self.encoder.padding_idx] = 0.
self.input_dp = RNNDropout(input_p)
self.hidden_dps = nn.ModuleList([RNNDropout(hidden_p) for l in range(n_layers)])

Expand Down Expand Up @@ -197,4 +199,4 @@ def show_piece_attn(*args, **kwargs):
def _eval_dropouts(mod):
module_name = mod.__class__.__name__
if 'Dropout' in module_name or 'BatchNorm' in module_name: mod.training = False
for module in mod.children(): _eval_dropouts(module)
for module in mod.children(): _eval_dropouts(module)

0 comments on commit f15ecfc

Please sign in to comment.