What is Hugging Face in the context of machine learning?
Hugging Face is a platform and library that provides easy access to pre-trained models and tools for natural language processing and other AI tasks.
Click to reveal answer
beginner
What is the purpose of the 'transformers' library in Hugging Face?
The 'transformers' library allows you to load and use pre-trained models for tasks like text classification, translation, and question answering with simple code.
Click to reveal answer
beginner
How do you load a pre-trained model and tokenizer from Hugging Face in PyTorch?
You use `AutoTokenizer.from_pretrained('model_name')` and `AutoModel.from_pretrained('model_name')` to load the tokenizer and model respectively.
Click to reveal answer
beginner
Why do we use a tokenizer before feeding text into a Hugging Face model?
A tokenizer converts raw text into numbers (tokens) that the model can understand and process.
Click to reveal answer
beginner
What is the typical output of a Hugging Face transformer model in PyTorch?
The output is usually a tensor containing predictions like class scores, embeddings, or generated tokens depending on the task.
Click to reveal answer
Which Hugging Face class is used to load a pre-trained tokenizer?
AAutoTokenizer
BAutoModel
CTokenizerLoader
DModelTokenizer
✗ Incorrect
AutoTokenizer is the class used to load pre-trained tokenizers easily.
What does the tokenizer do to the input text?
AGenerates new text
BConverts text to tokens (numbers)
CTrains the model
DEvaluates model accuracy
✗ Incorrect
Tokenizers convert raw text into tokens (numbers) that models can process.
Which library do you import to use Hugging Face models in PyTorch?
Atensorflow
Btorchvision
Csklearn
Dtransformers
✗ Incorrect
The 'transformers' library provides Hugging Face models for PyTorch.
What is the first step to use a Hugging Face model on new text?
ATrain the model
BEvaluate the model
CTokenize the text
DSave the model
✗ Incorrect
You must tokenize the text before feeding it to the model.
What kind of tasks can Hugging Face models perform?
The tokenizer returns input IDs with shape [batch_size, sequence_length]. Here batch_size=1 and sequence_length=5 (tokens: [CLS], 'hello', 'world', '!', [SEP]).
Step 2: Understand model output shape
The model outputs last_hidden_state with shape [batch_size, sequence_length, hidden_size]. For BERT base, hidden_size=768.
Hint: Output shape = batch x tokens x hidden size [OK]
Common Mistakes:
Confusing sequence length with batch size
Ignoring hidden size dimension
Assuming output shape is 2D instead of 3D
4. Identify the error in this Hugging Face PyTorch code snippet:
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
model = AutoModel.from_pretrained('bert-base-uncased')
inputs = tokenizer('Test input', return_tensors='pt')
outputs = model(inputs)
print(outputs)
medium
A. Missing .to(device) to move inputs to GPU
B. AutoModel cannot be used with BERT models
C. Tokenizer should use return_tensors='tf' instead of 'pt'
D. Model should be called with unpacked inputs: model(**inputs)
Solution
Step 1: Check how model is called
The model expects keyword arguments, so inputs must be unpacked with ** to pass input_ids and attention_mask correctly.
Step 2: Identify the fix
Change model(inputs) to model(**inputs) to avoid errors.
Final Answer:
Model should be called with unpacked inputs: model(**inputs) -> Option D
Quick Check:
Use **inputs when calling model [OK]
Hint: Call model with model(**inputs) not model(inputs) [OK]
Common Mistakes:
Passing inputs as a single dict argument
Confusing PyTorch and TensorFlow tensor types
Thinking AutoModel can't load BERT
5. You want to classify text into positive or negative sentiment using a Hugging Face model in PyTorch. After tokenizing and running the model, which step correctly converts raw model outputs into class probabilities?
hard
A. Apply torch.softmax on the model's logits output
B. Use torch.argmax directly on tokenized inputs
C. Call model.eval() to get probabilities
D. Normalize input tokens before feeding to the model
Solution
Step 1: Understand model output for classification
Models output raw scores called logits for each class, which are not probabilities.
Step 2: Convert logits to probabilities
Applying torch.softmax on logits converts them into probabilities summing to 1.
Final Answer:
Apply torch.softmax on the model's logits output -> Option A
Quick Check:
Softmax = logits to probabilities [OK]
Hint: Use softmax on logits to get probabilities [OK]
Common Mistakes:
Using argmax on inputs instead of outputs
Confusing model.eval() with probability conversion
Normalizing inputs instead of outputs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.