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 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
38 changes: 22 additions & 16 deletions onmt/translate/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,12 @@ def __init__(
self.report_score = report_score
self.logger = logger

self.use_filter_pred = False

# for debugging
self.beam_trace = self.dump_beam != ""
self.beam_accum = None
if self.beam_trace:
if self.dump_beam != "":
self.beam_accum = {
"predicted_ids": [],
"beam_parent_ids": [],
"scores": [],
"log_probs": []}
"predicted_ids": [], "beam_parent_ids": [], "scores": []
}
else:
self.beam_accum = None

set_random_seed(opt.seed, self.cuda)

Expand Down Expand Up @@ -185,7 +180,7 @@ def translate(
window_size=self.window_size,
window_stride=self.window_stride,
window=self.window,
use_filter_pred=self.use_filter_pred,
use_filter_pred=False,
image_channel_size=self.image_channel_size,
)

Expand Down Expand Up @@ -303,8 +298,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):
Expand Down Expand Up @@ -561,7 +556,6 @@ def _fast_translate_batch(
):
# TODO: support these blacklisted features.
assert not self.dump_beam
assert not self.use_filter_pred
assert self.block_ngram_repeat == 0
assert self.global_scorer.beta == 0

Expand Down Expand Up @@ -761,6 +755,8 @@ def _translate_batch(self, batch, data):
# And helper method for reducing verbosity.
beam_size = self.beam_size
batch_size = batch.batch_size
assert self.beam_accum is None or batch_size == 1, \
"Beam visualization currently only works with batch_size == 1"
tgt_field = self.fields['tgt'][0][1].base_field
vocab = tgt_field.vocab

Expand Down Expand Up @@ -833,8 +829,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)
Expand All @@ -854,6 +849,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):
Expand Down