開始使用免費開始

用 RLHF 進行文字生成

在這個練習中,你將使用一個經 RLHF 預先訓練的模型 lvwerra/gpt2-imdb-pos-v2。這個練習讓你複習如何建立 Hugging Face 的 pipeline,並用它來測試 RLHF 訓練模型的其中一個情境:生成電影評論。

pipeline, AutoModelForCausalLM, and AutoTokenizer 物件已從 transformers 預先匯入。tokenizer 已經預先載入。

本練習屬於課程

Reinforcement Learning from Human Feedback(RLHF)

檢視課程

練習說明

  • 將模型名稱設定為 lvwerra/gpt2-imdb-pos-v2,也就是 RLHF 預訓練的模型。
  • 使用 pipeline 函式建立一個 text-generation 的 pipeline。
  • 使用這個文字生成 pipeline,為提供的評論產生續寫內容。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Set the model name
model_name = ____
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Create a text generation pipeline
text_generator = pipeline(____, model=model, tokenizer=tokenizer)

review_prompt = "Surprisingly, the film"

# Generate a continuation of the review
generated_text = text_generator(____, max_length=10)
print(f"Generated Review Continuation: {generated_text[0]['generated_text']}")
編輯並執行程式碼