Here are the steps to calculate perplexity at the sentence level using Hugging Face language models:
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("gpt2")
model = AutoModelForCausalLM.from_pretrained("gpt2")
input_sentence = "The cat sat on the mat."
input_ids = tokenizer.encode(input_sentence, return_tensors="pt")
next_token_logits = model(input_ids)[0][:, -1, :]
CrossEntropyLoss
function from PyTorch.from torch.nn.functional import cross_entropy
next_token = torch.tensor(tokenizer.encode(" ")[0])
cross_entropy_loss = cross_entropy(next_token_logits, next_token)
perplexity = torch.exp(cross_entropy_loss)
The perplexity value indicates how well the language model is able to predict the next token in the sentence given the input tokens. A lower perplexity value means better performance.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-09 17:08:26 +0000
Seen: 12 times
Last updated: Jun 09 '23
What is the method to calculate the total of two whole numbers in the programming language C?
In C programming language, can you explain the purpose and function of (int)(unsigned char)(x)?
What is the meaning of "Firebase Error: Unexpected error while deploying database rules"?
What is the process to determine the remainder in bash programming language?
How can the language of a table be modified in Dataverse using PowerApps?
How do you specify the language of a document using EvoPDF?
Which programming language can function as a service without any external runtime and without a GUI?