Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to load a pretrained Hugging Face model.
PyTorch
from transformers import AutoModel model = AutoModel.[1]('bert-base-uncased')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'load_model' which is not a Hugging Face method.
Using 'init_model' which does not exist in transformers.
Using 'create' which is not a valid method.
✗ Incorrect
The method to load a pretrained model from Hugging Face is 'from_pretrained'.
2fill in blank
mediumComplete the code to tokenize input text using a Hugging Face tokenizer.
PyTorch
from transformers import AutoTokenizer tokenizer = AutoTokenizer.[1]('bert-base-uncased') tokens = tokenizer('Hello world!')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'load' which is not a valid method.
Using 'init' which does not exist.
Using 'create_tokenizer' which is not a transformers method.
✗ Incorrect
Tokenizers are loaded with 'from_pretrained' method to match the model.
3fill in blank
hardFix the error in the code to get model outputs from tokenized inputs.
PyTorch
outputs = model([1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the tokenizer object instead of tokenized inputs.
Passing raw tokens without converting to tensors.
Passing the tokenizer call directly without saving to a variable.
✗ Incorrect
The model expects a dictionary of inputs, usually named 'inputs' after tokenization with return_tensors.
4fill in blank
hardFill both blanks to tokenize text with padding and convert to tensors.
PyTorch
inputs = tokenizer('Hello Hugging Face!', [1]=True, [2]='pt')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'truncation' instead of 'padding' for padding.
Using 'max_length' without padding.
Using 'pt' as a boolean instead of a string for return_tensors.
✗ Incorrect
To pad sequences and get PyTorch tensors, use padding=True and return_tensors='pt'.
5fill in blank
hardFill all three blanks to extract the last hidden state from model outputs.
PyTorch
outputs = model(inputs) last_hidden_state = outputs.[1] print(last_hidden_state.[2]) print(last_hidden_state.shape[[3]])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'hidden_states' which is not directly returned unless configured.
Printing tensor without detach causing gradient tracking.
Using wrong shape index.
✗ Incorrect
The model output has 'last_hidden_state' attribute. Use detach() to get tensor without gradient. Shape index 0 is batch size.