This notebook regroups the code sample of the video below, which is a part of the Hugging Face course.
#@title
from IPython.display import HTML
HTML('<iframe width="560" height="315" src="https://www.youtube.com/embed/NURcDHhYe98?rel=0&controls=0&showinfo=0" frameborder="0" allowfullscreen></iframe>')
Install the Transformers and Datasets libraries to run this notebook.
! pip install datasets transformers[sentencepiece]
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
model = AutoModelForCausalLM.from_pretrained("gpt2")
tokenizer = AutoTokenizer.from_pretrained("gpt2")
inputs = tokenizer("Hugging Face is a startup based in New York City and Paris",
return_tensors="pt")
loss = model(input_ids=inputs["input_ids"],
labels=inputs["input_ids"]).loss
ppl = torch.exp(loss)
print(f"Perplexity: {ppl.item():.2f}")