Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix len greedy decode #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions AnnotatedTransformer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2497,7 +2497,7 @@
"def greedy_decode(model, src, src_mask, max_len, start_symbol):\n",
" memory = model.encode(src, src_mask)\n",
" ys = torch.zeros(1, 1).fill_(start_symbol).type_as(src.data)\n",
" for i in range(max_len - 1):\n",
" for i in range(max_len):\n",
" out = model.decode(\n",
" memory, src_mask, ys, subsequent_mask(ys.size(1)).type_as(src.data)\n",
" )\n",
Expand All @@ -2507,7 +2507,9 @@
" ys = torch.cat(\n",
" [ys, torch.zeros(1, 1).type_as(src.data).fill_(next_word)], dim=1\n",
" )\n",
" return ys"
" \n",
" # Return the target sequence without the start symbol\n",
" return ys[:, 1:]"
]
},
{
Expand Down
6 changes: 4 additions & 2 deletions the_annotated_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ def __call__(self, x, y, norm):
def greedy_decode(model, src, src_mask, max_len, start_symbol):
memory = model.encode(src, src_mask)
ys = torch.zeros(1, 1).fill_(start_symbol).type_as(src.data)
for i in range(max_len - 1):
for i in range(max_len):
out = model.decode(
memory, src_mask, ys, subsequent_mask(ys.size(1)).type_as(src.data)
)
Expand All @@ -1323,7 +1323,9 @@ def greedy_decode(model, src, src_mask, max_len, start_symbol):
ys = torch.cat(
[ys, torch.zeros(1, 1).type_as(src.data).fill_(next_word)], dim=1
)
return ys

# Return the target sequence without the start symbol
return ys[:, 1:]


# %% id="qgIZ2yEtdYwe" tags=[]
Expand Down