LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Deep Learning for Text with PyTorch

Kurs anzeigen

Anleitung zur Übung

  • Initialize the tokenizer and model from the pretrained "t5-small" model.
  • Encode the input prompt using the tokenizer, making sure to return PyTorch tensors.
  • Translate the input prompt using model and generate the translated output.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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)
Code bearbeiten und ausführen