1039.159.252 / 1039_159_252.py
antitheft159's picture
Update 1039_159_252.py
1efd21a verified
raw
history blame contribute delete
564 Bytes
import torch
!pip install torch
from transformers import AutoModelForCausalLM, AutoTokenizer
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
loss = model(input_ids=inputs["input_ids"],
labels=inputs["input_ids"]).loss
ppl = torch.exp(loss)
print(f"Perplexity: {ppl.item(): 2f}")