Bird
Raised Fist0
NLPml~5 mins

T5 for text-to-text tasks in NLP

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
T5 turns all language tasks into a simple text input and text output format. This makes it easy to teach a model to do many things like translation, summarization, or answering questions.
You want to translate sentences from one language to another.
You need to summarize a long article into a short paragraph.
You want to answer questions based on a given text.
You want to convert one style of text into another, like changing formal text to casual.
You want a single model that can handle many different language tasks.
Syntax
NLP
from transformers import T5Tokenizer, T5ForConditionalGeneration

# Load model and tokenizer
model = T5ForConditionalGeneration.from_pretrained('t5-small')
tokenizer = T5Tokenizer.from_pretrained('t5-small')

# Prepare input text with task prefix
input_text = 'translate English to German: The house is wonderful.'
input_ids = tokenizer(input_text, return_tensors='pt').input_ids

# Generate output
outputs = model.generate(input_ids)

# Decode output
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
T5 uses a prefix in the input text to tell the model what task to do, like 'translate English to German:'.
The model generates text output, so all tasks are treated as text-to-text.
Examples
This input tells T5 to summarize the given sentence.
NLP
input_text = 'summarize: Machine learning is a method of teaching computers to learn from data.'
This input tells T5 to translate the English sentence into French.
NLP
input_text = 'translate English to French: How are you today?'
This input asks T5 to answer a question using the given context.
NLP
input_text = 'question: What is the capital of France? context: France is a country in Europe. Its capital is Paris.'
Sample Model
This program shows how to use T5 for two tasks: translating English to German and summarizing a sentence. It loads the model, prepares inputs with task prefixes, generates outputs, and prints the results.
NLP
from transformers import T5Tokenizer, T5ForConditionalGeneration

# Load the small T5 model and tokenizer
model = T5ForConditionalGeneration.from_pretrained('t5-small')
tokenizer = T5Tokenizer.from_pretrained('t5-small')

# Example input: translate English to German
input_text = 'translate English to German: The house is wonderful.'
input_ids = tokenizer(input_text, return_tensors='pt').input_ids

# Generate translation
outputs = model.generate(input_ids, max_length=40)

# Decode and print result
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
print('Translation:', result)

# Example input: summarize text
input_text2 = 'summarize: Machine learning helps computers learn from data to make decisions.'
input_ids2 = tokenizer(input_text2, return_tensors='pt').input_ids
outputs2 = model.generate(input_ids2, max_length=20)
summary = tokenizer.decode(outputs2[0], skip_special_tokens=True)
print('Summary:', summary)
OutputSuccess
Important Notes
Always add a clear task prefix in the input text so T5 knows what to do.
Use the tokenizer to convert text to tokens and back to text after generation.
The 't5-small' model is good for learning and small tasks; bigger models exist for better results.
Summary
T5 treats all language tasks as text input and text output.
You tell T5 what to do by adding a task prefix in the input text.
T5 can do many tasks like translation, summarization, and question answering with one model.

Practice

(1/5)
1. What is the main idea behind the T5 model in NLP?
easy
A. It treats all language tasks as text input and text output.
B. It uses images as input and text as output.
C. It only works for translation tasks.
D. It requires separate models for each task.

Solution

  1. Step 1: Understand T5's approach to tasks

    T5 converts every language task into a text-to-text format, meaning both input and output are text.
  2. Step 2: Compare options with this approach

    Only It treats all language tasks as text input and text output. correctly states this main idea; others describe different or incorrect approaches.
  3. Final Answer:

    It treats all language tasks as text input and text output. -> Option A
  4. Quick Check:

    T5 text-to-text = text input and text output [OK]
Hint: Remember: T5 always uses text input and output [OK]
Common Mistakes:
  • Thinking T5 uses images as input
  • Believing T5 only does translation
  • Assuming T5 needs multiple models
2. Which of the following is the correct way to tell T5 to perform a summarization task?
easy
A. Add the prefix generate image: before the input text.
B. Add the prefix translate English to French: before the input text.
C. Add the prefix classify sentiment: before the input text.
D. Add the prefix summarize: before the input text.

Solution

  1. Step 1: Identify the task prefix for summarization

    T5 uses specific prefixes to indicate tasks; for summarization, the prefix is "summarize:".
  2. Step 2: Match prefixes to tasks

    Add the prefix summarize: before the input text. correctly uses "summarize:"; others are for different tasks or invalid.
  3. Final Answer:

    Add the prefix summarize: before the input text. -> Option D
  4. Quick Check:

    Summarization prefix = summarize: [OK]
Hint: Use task name as prefix, e.g., summarize: for summaries [OK]
Common Mistakes:
  • Using wrong prefixes like translate for summarization
  • Confusing classification prefix with summarization
  • Adding unrelated prefixes like generate image
3. Given the input to T5: translate English to German: The cat is on the mat. What is the expected output?
medium
A. Die Katze liegt auf der Matte.
B. Le chat est sur le tapis.
C. The cat is on the mat.
D. El gato estΓ‘ en la alfombra.

Solution

  1. Step 1: Identify the task from the prefix

    The prefix "translate English to German:" tells T5 to translate the English sentence into German.
  2. Step 2: Match the correct German translation

    Die Katze liegt auf der Matte. is the correct German translation of "The cat is on the mat." Others are French, English, and Spanish translations.
  3. Final Answer:

    Die Katze liegt auf der Matte. -> Option A
  4. Quick Check:

    English to German translation = Die Katze liegt auf der Matte. [OK]
Hint: Match prefix language to output language translation [OK]
Common Mistakes:
  • Choosing output in wrong language
  • Ignoring the prefix and returning input
  • Confusing similar languages like French and German
4. You wrote this input for T5: summarize The quick brown fox jumps over the lazy dog. but the output is not a summary. What is the likely error?
medium
A. The input text is too short for summarization.
B. You forgot to add a colon after the prefix 'summarize'.
C. T5 cannot summarize sentences with animals.
D. You need to add 'translate:' prefix instead.

Solution

  1. Step 1: Check the prefix syntax

    T5 requires the task prefix to end with a colon, e.g., "summarize:" not "summarize".
  2. Step 2: Understand impact of missing colon

    Without the colon, T5 treats the whole input as text, not as a task instruction, so it won't summarize.
  3. Final Answer:

    You forgot to add a colon after the prefix 'summarize'. -> Option B
  4. Quick Check:

    Prefix colon missing = You forgot to add a colon after the prefix 'summarize'. [OK]
Hint: Always end task prefix with a colon ':' [OK]
Common Mistakes:
  • Ignoring colon after prefix
  • Thinking T5 can't summarize short text
  • Using wrong prefix like translate for summarization
5. You want T5 to answer questions based on a paragraph. Which input format correctly uses T5's text-to-text approach?
hard
A. What is the capital of France? Paris is the capital city of France.
B. translate English to French: What is the capital of France?
C. answer question: What is the capital of France? Context: Paris is the capital city of France.
D. summarize: Paris is the capital city of France.

Solution

  1. Step 1: Identify the task prefix for question answering

    T5 uses prefixes like "answer question:" to specify question answering tasks.
  2. Step 2: Check input format includes question and context

    answer question: What is the capital of France? Context: Paris is the capital city of France. correctly includes the question and context with the proper prefix. Others either miss the prefix or use wrong tasks.
  3. Final Answer:

    answer question: What is the capital of France? Context: Paris is the capital city of France. -> Option C
  4. Quick Check:

    QA prefix with context = answer question: What is the capital of France? Context: Paris is the capital city of France. [OK]
Hint: Use 'answer question:' prefix plus context for QA tasks [OK]
Common Mistakes:
  • Omitting task prefix for question answering
  • Using translation or summarization prefix wrongly
  • Not providing context with the question