The torch hub provides pretrained models, such as: https://pytorch.org/hub/pytorch_fairseq_translation/
These models can be used in python, or interactively with a CLI.
With the CLI it is possible to get alignments, with the --print-alignment
flag. The following code works in a terminal, after installing fairseq (and pytorch)
curl https://dl.fbaipublicfiles.com/fairseq/models/wmt14.v2.en-fr.fconv-py.tar.bz2 | tar xvjf -
MODEL_DIR=wmt14.en-fr.fconv-py
fairseq-interactive \
--path $MODEL_DIR/model.pt $MODEL_DIR \
--beam 5 --source-lang en --target-lang fr \
--tokenizer moses \
--bpe subword_nmt --bpe-codes $MODEL_DIR/bpecodes \
--print-alignment
In python it is possible to specify the keyword args verbose
and print_alignment
:
import torch
en2fr = torch.hub.load('pytorch/fairseq', 'transformer.wmt14.en-fr', tokenizer='moses', bpe='subword_nmt')
fr = en2fr.translate('Hello world!', beam=5, verbose=True, print_alignment=True)
However, this will only output the alignment as a logging message. And for fairseq 0.9 it seems to be broken and results in an error message (issue).
Is there a way to access alignment information (or possibly even the full attention matrix) from python code?