I am trying to play around with the Fairseq machine translation model using
en2de = torch.hub.load('pytorch/fairseq', 'transformer.wmt19.en-de',
checkpoint_file='model1.pt:model2.pt:model3.pt:model4.pt',
tokenizer='moses', bpe='fastbpe')
When i use en2de.generate(....), i wanna know what are the return values of this function.
This function is defined in the hub_utils.py file of fairseq model
I tried debugging the code, but didnt get anywhere. I need a better understanding of its return types.
Most probably the code snippet you're looking at came from https://github.com/facebookresearch/fairseq/blob/main/examples/wmt19/README.md
The example code there looks like this:
But most probably you'll meet some environmental setup issues because fairseq isn't easily useable "off-the-shelf". So, you'll have to do something like this:
After setting up the environment, now you can try this again:
[out]:
If we do some code digging, it points to https://github.com/facebookresearch/fairseq/blob/main/fairseq/hub_utils.py#L97
And if we look at the
translate()function, it goes to https://github.com/facebookresearch/fairseq/blob/main/fairseq/hub_utils.py#LL133C1-L145C76So
.translate()returns a list of stringsAnd if we dig deeper into the rabbit hole, we see the
.generate()function from https://github.com/facebookresearch/fairseq/blob/main/fairseq/hub_utils.py#L170 which returnsAnd if you use the model with
.generate(),[out]:
.generate()returns a list of list of dict, where keys are names and values are tensorThe outer most list is the sentences' result. If you have one sentence, the result for the sentence is:
[out]:
You'll see that each sentence has 5 translation results. This is because the beam size is set to 5 by default. Each dictionary in the inner list corresponds to the translations from each beam.
[out]:
And to get the best translation:
[out]:
And to get the string representation, we fetch the tokens and decode them:
[out]:
Here's the working code for the above examples, https://www.kaggle.com/alvations/how-to-use-fairseq-wmt19-models