What does BERT stand for and what is its main purpose?
BERT stands for Bidirectional Encoder Representations from Transformers. It is a model designed to understand the context of words in a sentence by looking at both left and right sides, helping machines understand language better.
Click to reveal answer
beginner
Why is BERT called 'bidirectional'?
Because BERT reads the text from both directions (left to right and right to left) at the same time, it understands the full context of a word based on all surrounding words.
Click to reveal answer
intermediate
What is the role of the [CLS] token in BERT for text classification?
The [CLS] token is added at the start of every input sentence. After processing, its output embedding is used as a summary representation of the whole sentence for classification tasks.
Click to reveal answer
intermediate
How do you fine-tune BERT for a text classification task?
You add a simple classification layer on top of BERT's output (usually on the [CLS] token embedding) and train the whole model on your labeled data, adjusting weights to improve prediction accuracy.
Click to reveal answer
beginner
What metric is commonly used to evaluate BERT's performance on text classification?
Accuracy is commonly used to measure how many texts BERT correctly classifies out of all examples. Other metrics like F1-score can also be used for imbalanced data.
Click to reveal answer
What does the [SEP] token do in BERT input?
ARepresents unknown words
BSeparates two sentences in input
CIndicates padding tokens
DMarks the start of the sentence
✗ Incorrect
The [SEP] token is used to separate two sentences or segments in BERT input, helping the model understand sentence boundaries.
Which part of BERT's output is used for classification tasks?
AOutput embedding of the [CLS] token
BOutput embedding of the last word
CSum of all token embeddings
DInput token embeddings
✗ Incorrect
The [CLS] token's output embedding is used as a summary representation for classification.
What is the main advantage of fine-tuning BERT instead of training from scratch?
AOnly works for images
BNeeds more data and time
CDoes not improve accuracy
DRequires less data and time
✗ Incorrect
Fine-tuning uses pre-trained knowledge, so it needs less data and training time to perform well.
Which optimizer is commonly used when fine-tuning BERT?
ARMSProp
BSGD
CAdamW
DAdagrad
✗ Incorrect
AdamW optimizer is commonly used because it handles weight decay properly during fine-tuning.
What does 'tokenization' mean in BERT preprocessing?
ASplitting text into smaller pieces called tokens
BConverting text to uppercase
CRemoving punctuation only
DTranslating text to another language
✗ Incorrect
Tokenization splits text into tokens (words or subwords) that BERT can understand.
Explain how BERT processes input text for classification, including tokenization, special tokens, and output usage.
Think about how BERT reads and prepares sentences before predicting.
You got /3 concepts.
Describe the steps to fine-tune a pre-trained BERT model on a new text classification dataset.
Focus on model modification, data preparation, training, and evaluation.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using BERT for text classification compared to traditional bag-of-words models?
easy
A. BERT understands the context of words in a sentence.
B. BERT uses fewer parameters than bag-of-words models.
C. BERT requires no preprocessing of text data.
D. BERT only works with short texts.
Solution
Step 1: Understand BERT's design
BERT reads words in context, meaning it looks at surrounding words to understand meaning better.
Step 2: Compare with bag-of-words
Bag-of-words treats words independently, ignoring order and context, which limits understanding.
Final Answer:
BERT understands the context of words in a sentence. -> Option A
Quick Check:
Contextual understanding = A [OK]
Hint: Remember BERT reads words with context, unlike bag-of-words [OK]
Common Mistakes:
Thinking BERT uses fewer parameters
Assuming BERT needs no preprocessing
Believing BERT only works on short texts
2. Which of the following is the correct way to load a pretrained BERT model for sequence classification in PyTorch?
easy
A. model = torch.load('bert-base-uncased')
B. model = BertTokenizer.from_pretrained('bert-base-uncased')
C. model = BertForSequenceClassification.from_pretrained('bert-base-uncased')
D. model = nn.Sequential('bert-base-uncased')
Solution
Step 1: Identify correct class for model loading
BertForSequenceClassification is the class to load pretrained BERT models for classification tasks.
Step 2: Check method usage
from_pretrained is the method to load pretrained weights by model name.
Final Answer:
model = BertForSequenceClassification.from_pretrained('bert-base-uncased') -> Option C
Quick Check:
Use BertForSequenceClassification.from_pretrained = A [OK]
Hint: Use from_pretrained with BertForSequenceClassification for classification models [OK]
Common Mistakes:
Using BertTokenizer to load model weights
Trying to load model with torch.load without saving first
Using nn.Sequential incorrectly for BERT
3. Given the following code snippet, what will be the shape of the logits output?
from transformers import BertTokenizer, BertForSequenceClassification
import torch
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=3)
inputs = tokenizer(['Hello world!', 'BERT is great.'], padding=True, return_tensors='pt')
outputs = model(**inputs)
logits = outputs.logits
print(logits.shape)
medium
A. torch.Size([3, 2])
B. torch.Size([2, 3])
C. torch.Size([2, 768])
D. torch.Size([1, 3])
Solution
Step 1: Understand batch size and labels
Input has 2 sentences, so batch size is 2. num_labels=3 means 3 classes.
Step 2: Check output logits shape
Logits shape is (batch_size, num_labels), so (2, 3).
Final Answer:
torch.Size([2, 3]) -> Option B
Quick Check:
Batch size 2 and 3 labels = (2, 3) [OK]
Hint: Logits shape = (batch size, number of classes) [OK]
Common Mistakes:
Confusing batch size and label dimension order
Assuming output shape matches hidden size (768)
Ignoring batch size in output shape
4. You run this code but get an error: TypeError: forward() got an unexpected keyword argument 'input_ids'. What is the likely cause?