開始使用免費開始

評估困惑度(perplexity)

試著產生文本並評估困惑度分數。

我們提供了句子開頭的 input_text:"Current trends show that by 2030 ".

使用一個 LLM 來生成該句子的後半段。

一個 AutoModelForCausalLM 模型及其 tokenizer 已載入為 modeltokenizer 變數供你使用。

本練習屬於課程

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'])
編輯並執行程式碼