thaumoctopus
thaumoctopus

Reputation: 213

Getting alignment/attention during translation in OpenNMT-py

Does anyone know how to get the alignments weights when translating in Opennmt-py? Usually the only output are the resulting sentences and I have tried to find a debugging flag or similar for the attention weights. So far, I have been unsuccessful.

Upvotes: 0

Views: 733

Answers (2)

abhirut
abhirut

Reputation: 21

I'm not sure if this is a new feature, since I did not come across this when looking for alignments a few months back, but onmt seems to have added a flag -report_align to output word alignments along with the translation.

https://opennmt.net/OpenNMT-py/FAQ.html#raw-alignments-from-averaging-transformer-attention-heads

Excerpt from opennnmt.net -

Currently, we support producing word alignment while translating for Transformer based models. Using -report_align when calling translate.py will output the inferred alignments in Pharaoh format. Those alignments are computed from an argmax on the average of the attention heads of the second to last decoder layer.

Upvotes: 2

Jindřich
Jindřich

Reputation: 11220

You can get the attention matrices. Note that it is not the same as alignment which is a term from statistical (not neural) machine translation.

There is a thread on github discussing it. Here is a snippet from the discussion. When you get the translations from the mode, the attentions are in the attn field.

import onmt
import onmt.io
import onmt.translate
import onmt.ModelConstructor
from collections import namedtuple

# Load the model.
Opt = namedtuple('Opt', ['model', 'data_type', 'reuse_copy_attn', "gpu"])

opt = Opt("PATH_TO_SAVED_MODEL", "text", False, 0)
fields, model, model_opt =  onmt.ModelConstructor.load_test_model(
    opt, {"reuse_copy_attn" : False})

# Test data
data = onmt.io.build_dataset(
    fields, "text", "PATH_TO_DATA", None, use_filter_pred=False)
data_iter = onmt.io.OrderedIterator(
    dataset=data, device=0,
    batch_size=1, train=False, sort=False,
    sort_within_batch=True, shuffle=False)

# Translator
translator = onmt.translate.Translator(
    model, fields, beam_size=5, n_best=1,
    global_scorer=None, cuda=True)

builder = onmt.translate.TranslationBuilder(
   data, translator.fields, 1, False, None)

batch = next(data_iter)
batch_data = translator.translate_batch(batch, data)
translations = builder.from_batch(batch_data)
translations[0].attn # <--- here are the attentions

Upvotes: 0

Related Questions