0
0
PyTorchml~10 mins

Hugging Face integration basics in PyTorch - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Acreate
Bfrom_pretrained
Cinit_model
Dload_model
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.
2fill in blank
medium

Complete 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'
Aload
Binit
Cfrom_pretrained
Dcreate_tokenizer
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.
3fill in blank
hard

Fix 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'
Ainputs
Btokenizer
Ctokens
Dtokenizer('Hello world!')
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.
4fill in blank
hard

Fill 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'
Apadding
Btruncation
Creturn_tensors
Dmax_length
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.
5fill in blank
hard

Fill 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'
Alast_hidden_state
Bdetach()
C0
Dhidden_states
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.