Random Translation Generation with Fairseq Transformer Model

  • Share this:

Code introduction


This code defines a function that uses a Transformer model from the Fairseq library to generate random translations from source tokens to target tokens. The function accepts lists of source and target tokens, as well as the path to the pretrained model. It loads the model, converts the tokens to tensors, and then uses the model to perform the translation.


Technology Stack : Fairseq, TransformerModel, collate_tokens

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import random
import torch
from fairseq.models.transformer import TransformerModel
from fairseq.data.data_utils import collate_tokens

def generate_random_translation(source_tokens, target_tokens, model_path, beam_size=5):
    """
    Generate a random translation from source tokens to target tokens using a Fairseq Transformer model.
    """
    # Load the model
    model = TransformerModel.from_pretrained(model_path)

    # Convert source and target tokens to tensor
    source_tensors = torch.tensor([source_tokens])
    target_tensors = torch.tensor([target_tokens])

    # Collate the tensors
    source_collated = collate_tokens([source_tensors], pad_to_length=True, batch_first=True)
    target_collated = collate_tokens([target_tensors], pad_to_length=True, batch_first=True)

    # Generate translation
    with torch.no_grad():
        translations = model.translate(source_collated, beam=beam_size, max_len_a=50, max_len_b=50)

    # Extract the first translation (randomly chosen)
    translation = translations[0][0].cpu().numpy()

    return translation

# Example usage:
# source_tokens = [1, 2, 3, 4, 5]
# target_tokens = [5, 4, 3, 2, 1]
# model_path = 'path_to_pretrained_model'
# translation = generate_random_translation(source_tokens, target_tokens, model_path)
# print(translation)