パープレキシティの評価
テキスト生成やパープレクシティスコアの評価を試してみてください。
文の冒頭として「現在の傾向から、2030年までに」というinput_textが与えられています。
LLMを使って文の残りを生成してください。
AutoModelForCausalLMモデルとそのトークナイザーはmodel変数とtokenizer変数として読み込まれています。
この演習はコースの一部です
Python で学ぶ LLM の入門
演習の手順
input_textをエンコードし、提供されたテキスト生成モデルに渡します。- 生成されたテキストの
mean_perplexityスコアを読み込み、計算します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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'])