Evaluating perplexity
Try your had at generating text and evaluating the perplexity score.
You've been provided some input_text that is the start of a sentence: "Current trends show that by 2030 ".
Use an LLM to generate the rest of the sentence.
An AutoModelForCausalLM model and its tokenizer have been loaded for you as model and tokenizer variables.
This exercise is part of the course
Introduction to LLMs in Python
Exercise instructions
- Encode the
input_textand pass it to the provided text generation model. - Load and compute the
mean_perplexityscore on the generated text.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Encode the input text, generate and decode it
input_text_ids = ____(input_text, return_tensors="pt")
output = ____(input_text_ids, max_length=20)
generated_text = ____(output[0], skip_special_tokens=True)
print("Generated Text: ", generated_text)
# Load and compute the perplexity score
perplexity = ____("perplexity", module_type="metric")
results = ____(model_id="gpt2", predictions=____)
print("Perplexity: ", results['mean_perplexity'])