事前学習済みLLMの活用

Pythonで学ぶ LLM 入門

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

言語理解

テキスト分類、要約、感情分析、Q&Aを示すタスクに囲まれた机で作業する人

Pythonで学ぶ LLM 入門

言語生成

テキスト生成と翻訳のタスクに囲まれた机で作業する人

Pythonで学ぶ LLM 入門

テキスト生成

generator = pipeline(task="text-generation", model="distilgpt2")

prompt = "The Gion neighborhood in Kyoto is famous for"

output = generator(prompt, max_length=100, pad_token_id=generator.tokenizer.eos_token_id)
  • 一貫性がある
  • 意味が通る
  • 人間らしい文
  • eos_token_id: 文章終端トークンID
Pythonで学ぶ LLM 入門

テキスト生成

2つのシーケンス「we should go」「i really like to travel」を例に、トークンID、パディング、EOS位置を示す図

  • pad_token_id: max_lengthまで空きを埋める
  • Padding: トークンを追加
  • generator.tokenizer.eos_token_idに設定すると、学習で得た意味の終端を示す
  • 生成はmax_lengthまたはpad_token_idまで
  • truncation = True
Pythonで学ぶ LLM 入門

テキスト生成

generator = pipeline(task="text-generation", model="distilgpt2")

prompt = "The Gion neighborhood in Kyoto is famous for"

output = generator(prompt, max_length=100, pad_token_id=generator.tokenizer.eos_token_id)

print(output[0]["generated_text"])
The Gion neighborhood in Kyoto is famous for its many colorful green forests, such as the 
Red Hill, the Red River and the Red River. The Gion neighborhood is home to the world's 
tallest trees.
  • プロンプトが曖昧だと出力は最適でないことがある
Pythonで学ぶ LLM 入門

出力の誘導

generator = pipeline(task="text-generation", model="distilgpt2")


review = "This book was great. I enjoyed the plot twist in Chapter 10." response = "Dear reader, thank you for your review." prompt = f"Book review:\n{review}\n\nBook shop response to the review:\n{response}"
output = generator(prompt, max_length=100, pad_token_id=generator.tokenizer.eos_token_id) print(output[0]["generated_text"])
Dear reader, thank you for your review. We'd like to thank you for your reading!
Pythonで学ぶ LLM 入門

言語翻訳

  • Hugging Face には翻訳タスクとモデルの一覧があります
translator = pipeline(task="translation_en_to_es", model="Helsinki-NLP/opus-mt-en-es")

text = "Walking amid Gion's Machiya wooden houses was a mesmerizing experience."
output = translator(text, clean_up_tokenization_spaces=True)
print(output[0]["translation_text"])
Caminar entre las casas de madera Machiya de Gion fue una experiencia fascinante.
Pythonで学ぶ LLM 入門

演習に進みましょう!

Pythonで学ぶ LLM 入門

Preparing Video For Download...