0
0
NLPml~20 mins

BERT fine-tuning for classification in NLP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BERT Fine-tuning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Model Choice
intermediate
2:00remaining
Choosing the correct BERT model for fine-tuning

You want to fine-tune a BERT model for a text classification task with 3 classes. Which pretrained model is the best starting point?

Abert-base-uncased
Bgpt-2
Cbert-large-cased
Dresnet50
Attempts:
2 left
💡 Hint

Pick a model designed for language understanding and classification.

Predict Output
intermediate
2:00remaining
Output shape after BERT forward pass

Given this code snippet, what is the shape of outputs.logits?

NLP
from transformers import BertForSequenceClassification, BertTokenizer
import torch

model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=4)
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
inputs = tokenizer(['Hello world', 'Test sentence'], padding=True, return_tensors='pt')
outputs = model(**inputs)
logits_shape = outputs.logits.shape
A(768, 4)
B(4, 2)
C(2, 768)
D(2, 4)
Attempts:
2 left
💡 Hint

Check batch size and number of labels.

Hyperparameter
advanced
2:00remaining
Choosing learning rate for BERT fine-tuning

You are fine-tuning BERT for classification. Which learning rate is most appropriate to start with?

A5e-5
B0.1
C1.0
D0.000001
Attempts:
2 left
💡 Hint

Typical fine-tuning learning rates are small but not too tiny.

Metrics
advanced
2:00remaining
Evaluating BERT classification performance

After fine-tuning BERT on a 3-class classification task, you get these predictions and true labels:

preds = [0, 2, 1, 1, 0]
labels = [0, 1, 1, 2, 0]

What is the accuracy?

A0.8
B0.4
C0.6
D0.2
Attempts:
2 left
💡 Hint

Count how many predictions match labels exactly.

🔧 Debug
expert
2:00remaining
Identifying error in BERT fine-tuning code

What error does this code raise?

from transformers import BertForSequenceClassification
model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)
inputs = {'input_ids': [[101, 2054, 2003, 1996, 2562, 102]], 'attention_mask': [[1, 1, 1, 1, 1, 1]]}
outputs = model(**inputs)
loss = outputs.loss
AAttributeError: 'SequenceClassifierOutput' object has no attribute 'loss'
BRuntimeError: Expected tensor for input_ids, got list
CTypeError: forward() missing required positional argument 'labels'
DNo error, code runs successfully
Attempts:
2 left
💡 Hint

Check the type/format of input_ids and attention_mask.