Skip to content

Commit

Permalink
fix graykode#30 vocab_size to seq_len
Browse files Browse the repository at this point in the history
  • Loading branch information
graykode committed Jul 8, 2019
1 parent 3b3a80d commit 6e171b9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 2 additions & 2 deletions 5-1.Transformer/Transformer(Greedy_decoder)-Torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class Encoder(nn.Module):
def __init__(self):
super(Encoder, self).__init__()
self.src_emb = nn.Embedding(src_vocab_size, d_model)
self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(src_vocab_size, d_model),freeze=True)
self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(src_len+1, d_model),freeze=True)
self.layers = nn.ModuleList([EncoderLayer() for _ in range(n_layers)])

def forward(self, enc_inputs): # enc_inputs : [batch_size x source_len]
Expand All @@ -154,7 +154,7 @@ class Decoder(nn.Module):
def __init__(self):
super(Decoder, self).__init__()
self.tgt_emb = nn.Embedding(tgt_vocab_size, d_model)
self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(tgt_vocab_size, d_model),freeze=True)
self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(tgt_len+1, d_model),freeze=True)
self.layers = nn.ModuleList([DecoderLayer() for _ in range(n_layers)])

def forward(self, dec_inputs, enc_inputs, enc_outputs): # dec_inputs : [batch_size x target_len]
Expand Down
23 changes: 19 additions & 4 deletions 5-1.Transformer/Transformer(Greedy_decoder)_Torch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
}
],
"source": [
"'''\n",
" code by Tae Hwan Jung(Jeff Jung) @graykode, Derek Miller @dmmiller612\n",
" Reference : https://github.com/jadore801120/attention-is-all-you-need-pytorch\n",
" https://github.com/JayParks/transformer\n",
"'''\n",
"'''\n",
" code by Tae Hwan Jung(Jeff Jung) @graykode, Derek Miller @dmmiller612\n",
" Reference : https://github.com/jadore801120/attention-is-all-you-need-pytorch\n",
Expand Down Expand Up @@ -233,7 +238,7 @@
" def __init__(self):\n",
" super(Encoder, self).__init__()\n",
" self.src_emb = nn.Embedding(src_vocab_size, d_model)\n",
" self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(src_vocab_size, d_model),freeze=True)\n",
" self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(src_len+1, d_model),freeze=True)\n",
" self.layers = nn.ModuleList([EncoderLayer() for _ in range(n_layers)])\n",
"\n",
" def forward(self, enc_inputs): # enc_inputs : [batch_size x source_len]\n",
Expand All @@ -249,7 +254,7 @@
" def __init__(self):\n",
" super(Decoder, self).__init__()\n",
" self.tgt_emb = nn.Embedding(tgt_vocab_size, d_model)\n",
" self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(tgt_vocab_size, d_model),freeze=True)\n",
" self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(tgt_len+1, d_model),freeze=True)\n",
" self.layers = nn.ModuleList([DecoderLayer() for _ in range(n_layers)])\n",
"\n",
" def forward(self, dec_inputs, enc_inputs, enc_outputs): # dec_inputs : [batch_size x target_len]\n",
Expand Down Expand Up @@ -351,10 +356,20 @@
},
"kernelspec": {
"name": "python3",
"language": "python",
"display_name": "Python 3"
},
"accelerator": "GPU"
"accelerator": "GPU",
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"source": [],
"metadata": {
"collapsed": false
}
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}
}
4 changes: 2 additions & 2 deletions 5-1.Transformer/Transformer-Torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Encoder(nn.Module):
def __init__(self):
super(Encoder, self).__init__()
self.src_emb = nn.Embedding(src_vocab_size, d_model)
self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(src_vocab_size, d_model),freeze=True)
self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(src_len+1, d_model),freeze=True)
self.layers = nn.ModuleList([EncoderLayer() for _ in range(n_layers)])

def forward(self, enc_inputs): # enc_inputs : [batch_size x source_len]
Expand All @@ -153,7 +153,7 @@ class Decoder(nn.Module):
def __init__(self):
super(Decoder, self).__init__()
self.tgt_emb = nn.Embedding(tgt_vocab_size, d_model)
self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(tgt_vocab_size, d_model),freeze=True)
self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(tgt_len+1, d_model),freeze=True)
self.layers = nn.ModuleList([DecoderLayer() for _ in range(n_layers)])

def forward(self, dec_inputs, enc_inputs, enc_outputs): # dec_inputs : [batch_size x target_len]
Expand Down
18 changes: 14 additions & 4 deletions 5-1.Transformer/Transformer_Torch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@
" def __init__(self):\n",
" super(Encoder, self).__init__()\n",
" self.src_emb = nn.Embedding(src_vocab_size, d_model)\n",
" self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(src_vocab_size, d_model),freeze=True)\n",
" self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(src_len+1, d_model),freeze=True)\n",
" self.layers = nn.ModuleList([EncoderLayer() for _ in range(n_layers)])\n",
"\n",
" def forward(self, enc_inputs): # enc_inputs : [batch_size x source_len]\n",
Expand All @@ -266,7 +266,7 @@
" def __init__(self):\n",
" super(Decoder, self).__init__()\n",
" self.tgt_emb = nn.Embedding(tgt_vocab_size, d_model)\n",
" self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(tgt_vocab_size, d_model),freeze=True)\n",
" self.pos_emb = nn.Embedding.from_pretrained(get_sinusoid_encoding_table(tgt_len+1, d_model),freeze=True)\n",
" self.layers = nn.ModuleList([DecoderLayer() for _ in range(n_layers)])\n",
"\n",
" def forward(self, dec_inputs, enc_inputs, enc_outputs): # dec_inputs : [batch_size x target_len]\n",
Expand Down Expand Up @@ -345,10 +345,20 @@
},
"kernelspec": {
"name": "python3",
"language": "python",
"display_name": "Python 3"
},
"accelerator": "GPU"
"accelerator": "GPU",
"pycharm": {
"stem_cell": {
"cell_type": "raw",
"source": [],
"metadata": {
"collapsed": false
}
}
}
},
"nbformat": 4,
"nbformat_minor": 0
}
}

0 comments on commit 6e171b9

Please sign in to comment.