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

[WIP] Reimplement beam visualization #1240

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
reimplement beam_accum, but with a bug
bpopeters committed Jan 30, 2019
commit acf06b12d28aadd19daa3e2bdbbf8877177ee265
18 changes: 14 additions & 4 deletions onmt/translate/translator.py
Original file line number Diff line number Diff line change
@@ -303,8 +303,8 @@ def translate(

if self.dump_beam:
import json
json.dump(self.translator.beam_accum,
codecs.open(self.dump_beam, 'w', 'utf-8'))
with codecs.open(self.dump_beam, 'w', 'utf-8') as f:
json.dump(self.beam_accum, f)
return all_scores, all_predictions

def sample_with_temperature(self, logits, sampling_temp, keep_topk):
@@ -833,8 +833,7 @@ def _translate_batch(self, batch, data):
select_indices_array = []
# Loop over the batch_size number of beam
for j, b in enumerate(beam):
b.advance(out[j, :],
beam_attn.data[j, :, :memory_lengths[j]])
b.advance(out[j, :], beam_attn.data[j, :, :memory_lengths[j]])
select_indices_array.append(
b.get_current_origin() + j * beam_size)
select_indices = torch.cat(select_indices_array)
@@ -854,6 +853,17 @@ def _translate_batch(self, batch, data):
results["scores"].append(scores)
results["attention"].append(attn)

if self.beam_accum is not None:
self.beam_accum["beam_parent_ids"].append(
[t.tolist() for t in b.prev_ks])
self.beam_accum["scores"].append([
["%4f" % s for s in t.tolist()]
for t in b.all_scores][1:])
# ok, so what was the tgt_dict?
self.beam_accum["predicted_ids"].append(
[[vocab.itos[i] for i in t.tolist()]
for t in b.next_ys][1:])

return results

def _score_target(self, batch, memory_bank, src_lengths, data, src_map):