Bird
Raised Fist0
NLPml~20 mins

Why different transformers serve different tasks in NLP - Challenge Your Understanding

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
Challenge - 5 Problems
🎖️
Transformer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do transformers have different architectures for different tasks?

Transformers are used in many tasks like translation, text classification, and question answering. Why do we need different transformer models for these tasks?

ABecause transformers only work for translation tasks and cannot be used for other tasks without changing the language.
BBecause transformers use different programming languages for different tasks.
CBecause transformers are slow and need to be replaced by different models for each task.
DBecause each task requires a different way to process input and output, so transformer models are designed with specific layers or heads to handle those needs.
Attempts:
2 left
💡 Hint

Think about how the output of a model changes depending on the task.

Model Choice
intermediate
2:00remaining
Choosing the right transformer for sentiment analysis

You want to build a model to classify movie reviews as positive or negative. Which transformer model is best suited for this task?

AGPT-3 used as a language generator without fine-tuning
BA convolutional neural network without transformers
CBERT with a classification head on top
DTransformer model designed only for machine translation
Attempts:
2 left
💡 Hint

Think about which model is designed to understand sentence meaning and output labels.

Predict Output
advanced
2:00remaining
Output shape of transformer model for question answering

Consider a transformer model fine-tuned for question answering. The input is a batch of 2 sequences, each with 10 tokens. The model outputs start and end logits for answer spans. What is the shape of the output logits?

NLP
import torch
batch_size = 2
seq_len = 10
start_logits = torch.randn(batch_size, seq_len)
end_logits = torch.randn(batch_size, seq_len)
print(start_logits.shape, end_logits.shape)
A(2, 10) (2, 10)
B(10, 2) (10, 2)
C(2, 1) (2, 1)
D(20,) (20,)
Attempts:
2 left
💡 Hint

Think about how logits correspond to tokens in each sequence for each batch item.

Hyperparameter
advanced
2:00remaining
Effect of number of attention heads in transformer models

What is the main effect of increasing the number of attention heads in a transformer model?

AIt allows the model to focus on different parts of the input simultaneously, improving its ability to capture diverse relationships.
BIt reduces the model size and speeds up training by using fewer parameters.
CIt disables the self-attention mechanism and uses only feed-forward layers.
DIt changes the output format from text to images.
Attempts:
2 left
💡 Hint

Think about what multiple attention heads do in the transformer.

🔧 Debug
expert
3:00remaining
Why does this transformer model output all zeros?

You fine-tuned a transformer for text classification, but the model always outputs zeros for predictions. What is the most likely cause?

import torch
from transformers import BertForSequenceClassification, BertTokenizer

model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

inputs = tokenizer('Hello world', return_tensors='pt')
outputs = model(**inputs)
print(outputs.logits)
AThe tokenizer is incorrect and returns empty input tensors.
BThe model is in evaluation mode but was never trained, so outputs are near zero logits.
CThe model architecture is wrong and does not produce logits.
DThe input text is too short, so the model outputs zeros.
Attempts:
2 left
💡 Hint

Think about what happens if you use a pretrained model without fine-tuning for classification.

Practice

(1/5)
1. Why do different transformer models exist for different NLP tasks?
easy
A. Because transformers do not use any training data
B. Because transformers are only designed for image processing
C. Because all transformers work exactly the same for every task
D. Because each task requires a special way to process and understand language

Solution

  1. Step 1: Understand the role of transformers in NLP tasks

    Transformers are designed to handle language data, but different tasks like translation or classification need different ways to process inputs and outputs.
  2. Step 2: Recognize why task-specific models exist

    Because tasks differ, models are fine-tuned or designed to best fit each task's needs, improving performance.
  3. Final Answer:

    Because each task requires a special way to process and understand language -> Option D
  4. Quick Check:

    Task needs shape model choice = A [OK]
Hint: Different tasks need different processing methods [OK]
Common Mistakes:
  • Thinking all transformers are the same
  • Believing transformers only work for images
  • Ignoring the role of training data
2. Which of the following is the correct way to load a pretrained transformer model for text classification using the Hugging Face library?
easy
A. model = AutoTokenizer.from_pretrained('bert-base-uncased')
B. model = AutoModel.from_pretrained('bert-base-uncased')
C. model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased')
D. model = AutoModelForImageClassification.from_pretrained('bert-base-uncased')

Solution

  1. Step 1: Identify the correct class for text classification

    For text classification, the correct class is AutoModelForSequenceClassification.
  2. Step 2: Check the pretrained model name and method

    'bert-base-uncased' is a common pretrained model, and from_pretrained loads it properly.
  3. Final Answer:

    model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased') -> Option C
  4. Quick Check:

    Text classification model loading = A [OK]
Hint: Use AutoModelForSequenceClassification for classification tasks [OK]
Common Mistakes:
  • Using AutoModel instead of AutoModelForSequenceClassification
  • Confusing tokenizer loading with model loading
  • Using image classification model for text
3. Given this code snippet using a transformer for question answering, what will be the output type of outputs?
from transformers import AutoModelForQuestionAnswering, AutoTokenizer
model = AutoModelForQuestionAnswering.from_pretrained('distilbert-base-uncased-distilled-squad')
tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased-distilled-squad')
inputs = tokenizer('Who is the president of the USA?', return_tensors='pt')
outputs = model(**inputs)
medium
A. A single number representing sentiment score
B. A tuple containing start and end logits for answer span
C. A sequence of translated text tokens
D. A classification label like 'positive' or 'negative'

Solution

  1. Step 1: Identify the model type and task

    The model is AutoModelForQuestionAnswering, designed to find answer spans in text.
  2. Step 2: Understand the output format for question answering models

    These models output start and end logits indicating where the answer begins and ends in the input.
  3. Final Answer:

    A tuple containing start and end logits for answer span -> Option B
  4. Quick Check:

    Question answering output = start/end logits = D [OK]
Hint: Question answering outputs start/end logits tuple [OK]
Common Mistakes:
  • Expecting classification labels from QA models
  • Confusing translation output with QA output
  • Thinking output is a single sentiment score
4. You tried to use AutoModelForSeq2SeqLM for a text classification task but got wrong results. What is the likely error?
medium
A. Using a sequence-to-sequence model instead of a classification model
B. Not tokenizing the input text
C. Using the wrong optimizer
D. Loading the model without pretrained weights

Solution

  1. Step 1: Understand model purpose

    AutoModelForSeq2SeqLM is for tasks like translation or summarization, not classification.
  2. Step 2: Identify mismatch with task

    Using a seq2seq model for classification leads to wrong outputs because the model expects different input-output formats.
  3. Final Answer:

    Using a sequence-to-sequence model instead of a classification model -> Option A
  4. Quick Check:

    Model-task mismatch = seq2seq used for classification = C [OK]
Hint: Match model type to task type carefully [OK]
Common Mistakes:
  • Ignoring model-task compatibility
  • Forgetting to tokenize input
  • Assuming optimizer causes output errors
5. You want to build a chatbot that answers questions based on a knowledge base. Which transformer model type should you choose and why?
hard
A. AutoModelForQuestionAnswering, because it finds answer spans in text
B. AutoModelForSequenceClassification, because it classifies sentiment
C. AutoModelForMaskedLM, because it predicts missing words
D. AutoModelForSeq2SeqLM, because it translates languages

Solution

  1. Step 1: Understand chatbot task

    The chatbot needs to answer questions by finding relevant text spans in a knowledge base.
  2. Step 2: Match model type to task

    AutoModelForQuestionAnswering is designed to locate answer spans, making it ideal for this chatbot.
  3. Step 3: Exclude other options

    SequenceClassification is for sentiment, MaskedLM predicts missing words, Seq2SeqLM is for translation, so they don't fit the task.
  4. Final Answer:

    AutoModelForQuestionAnswering, because it finds answer spans in text -> Option A
  5. Quick Check:

    Chatbot answering needs QA model = B [OK]
Hint: Use QA models for answer span tasks like chatbots [OK]
Common Mistakes:
  • Choosing classification or translation models incorrectly
  • Confusing masked language models with QA models
  • Not matching model to chatbot needs