Language translation with pretrained PyTorch model
Your team at PyBooks is working on an AI project that involves translation from one language to another. They want to leverage pre-trained models for this task, which can save a lot of training time and resources. The task for this exercise is to set up a translation model from HuggingFace's Transformers library, specifically the T5 (Text-To-Text Transfer Transformer) model, and use it to translate an English phrase to French.
T5Tokenizer, T5ForConditionalGeneration have been loaded for you.
Deze oefening maakt deel uit van de cursus
Deep Learning for Text with PyTorch
Oefeninstructies
- Initialize the
tokenizerandmodelfrom the pretrained"t5-small"model. - Encode the input prompt using the tokenizer, making sure to return PyTorch tensors.
- Translate the input prompt using
modeland generate the translated output.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# Initalize tokenizer and model
tokenizer = ____.from_pretrained("t5-small")
model = ____.from_pretrained("t5-small")
input_prompt = "translate English to French: 'Hello, how are you?'"
# Encode the input prompt using the tokenizer
input_ids = ____.____(input_prompt, return_tensors="____")
# Generate the translated ouput
output = model.____(input_ids, max_length=50)
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
print("Generated text:",generated_text)