Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load a pre-trained NLP model using Hugging Face Transformers.
NLP
from transformers import AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the model name.
Using a variable name instead of a string.
✗ Incorrect
The model name must be a string, so it needs to be in quotes.
2fill in blank
mediumComplete the code to tokenize input text for the NLP model.
NLP
from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") inputs = tokenizer([1], return_tensors="pt")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name without defining it.
Passing a list instead of a string.
✗ Incorrect
The tokenizer expects a string input, so the text must be in quotes.
3fill in blank
hardFix the error in the code to get model predictions from tokenized inputs.
NLP
outputs = model([1]) predictions = outputs.logits.argmax(dim=1)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the whole inputs dictionary instead of input_ids.
Using a wrong key like 'tokens'.
✗ Incorrect
The model expects input_ids as a keyword argument, so we pass inputs['input_ids'].
4fill in blank
hardFill both blanks to create a function that preprocesses text and returns model predictions.
NLP
def predict(text): inputs = tokenizer([1], return_tensors=[2]) outputs = model(inputs['input_ids']) return outputs.logits.argmax(dim=1).item()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong variable or forgetting quotes around 'pt'.
Using inconsistent variable names.
✗ Incorrect
The function takes text as input and tokenizer needs return_tensors='pt' for PyTorch tensors.
5fill in blank
hardFill all three blanks to add batch processing and return a list of predictions.
NLP
def batch_predict(texts): inputs = tokenizer([1], padding=True, truncation=True, return_tensors=[2]) outputs = model(inputs['input_ids']) preds = outputs.logits.argmax(dim=[3]) return preds.tolist()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dim=0 which would give wrong axis for batch predictions.
Passing a single text instead of a list.
✗ Incorrect
We pass the list of texts, use 'pt' for PyTorch tensors, and argmax over dim=1 for batch dimension.