Complete the code to import the GPT model from the transformers library.
from transformers import [1]
The GPT family models are accessed via GPT2Model in the transformers library.
Complete the code to load the GPT-2 tokenizer.
from transformers import GPT2Tokenizer tokenizer = GPT2Tokenizer.from_pretrained([1])
The GPT-2 tokenizer is loaded using the model name 'gpt2'.
Fix the error in the code to generate text using GPT-2.
from transformers import GPT2LMHeadModel, GPT2Tokenizer tokenizer = GPT2Tokenizer.from_pretrained('gpt2') model = GPT2LMHeadModel.from_pretrained('gpt2') input_ids = tokenizer.encode('Hello, how are you?', return_tensors=[1]) outputs = model.generate(input_ids, max_length=20) print(tokenizer.decode(outputs[0], skip_special_tokens=True))
The return_tensors argument expects 'pt' for PyTorch tensors when using GPT-2 with transformers.
Fill both blanks to create a dictionary comprehension that maps GPT model names to their sizes in millions of parameters.
gpt_sizes = { [1] : [2] for [1] in ['gpt2', 'gpt2-medium', 'gpt2-large'] }The variable model is used as the key, and the dictionary with sizes is used as the value for each key in the comprehension.
Fill both blanks to create a function that returns the number of tokens in a text using GPT-2 tokenizer.
def count_tokens(text): tokenizer = GPT2Tokenizer.from_pretrained([1]) tokens = tokenizer.[2](text) return len(tokens) # number of tokens print(count_tokens('Hello world!'))
The GPT-2 tokenizer is loaded with 'gpt2'. The method to convert text to tokens is encode. The function returns the length of the token list.