シーケンス生成タスク

Pythonで学ぶ自然言語処理(NLP)

Fouad Trad

Machine Learning Engineer

シーケンス生成

  • 入力に基づいて新しいテキストを生成
  • 含まれるタスク:
    • 要約
    • 翻訳
    • 言語モデル

紙に文を書く鉛筆のGIF。

Pythonで学ぶ自然言語処理(NLP)

テキスト要約

  • 長文を要点を押さえた短い文に要約
  • 次の場面で有用:
    • 長いニュース記事
    • 論文
    • レポート
    • メール

多数の本の山から要約文書が生成される図。

Pythonで学ぶ自然言語処理(NLP)

テキスト要約パイプライン

from transformers import pipeline

summarizer = pipeline(task="summarization", model="cnicu/t5-small-booksum")
text = """The Amazon rainforest, often referred to as the "lungs of the Earth," is one of the most biologically diverse regions in the world. Spanning over nine countries in South America, the majority of the forest lies in Brazil. It is home to an estimated 390 billion individual trees, divided into 16,000 different species. The rainforest plays a critical role in regulating the global climate by absorbing vast amounts of carbon dioxide and producing oxygen."""
result = summarizer(text)
print(result)
[{'summary_text': 'the Amazon rainforest is one of the most biologically diverse regions in the world. 
The majority of the forest lies in Brazil. The rainforest plays a critical role in regulating the 
global climate by absorbing vast amounts of carbon dioxide and producing oxygen.'}]
Pythonで学ぶ自然言語処理(NLP)

テキスト翻訳

  • テキストを他言語へ変換
  • 多言語アプリで重要:
    • 国際的なWebサイト
    • カスタマーサポートツール

英語(Good morning)からフランス語(Bonjour)への翻訳例。

Pythonで学ぶ自然言語処理(NLP)

テキスト翻訳パイプライン

translator = pipeline(task="translation", model="Helsinki-NLP/opus-mt-en-fr")

sentence = "The rainforest helps regulate the Earth's climate."
result = translator(sentence)
print(result)
[{'translation_text': 'La forêt tropicale aide à réguler le climat de la Terre.'}]
Pythonで学ぶ自然言語処理(NLP)

言語モデル

  • 与えたプロンプトに基づき次の語を予測
  • 多くの応用の基盤:
    • オートコンプリート
    • 物語生成
    • チャットボット応答

プロンプトを与えると言語モデルがテキストを生成する様子。

Pythonで学ぶ自然言語処理(NLP)

言語モデルのパイプライン

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


prompt = "Once upon a time,"
result = generator(prompt, max_length=30, num_return_sequences=3)
print(result)
[{'generated_text': "Once upon a time, my life wasn't so good, I kept my things tidy. The 
                     more time I spend with my children the more ..."}, 
 {'generated_text': 'Once upon a time, we began a process of finding the right answers to 
                     some big questions," said Jim Pelterer, a lecturer at the 
                     University...'}, 
 {'generated_text': 'Once upon a time, a man came along and took in the city, and found out 
                     that a strange woman had just walked in and was dancing about...'}]
Pythonで学ぶ自然言語処理(NLP)

Ayo berlatih!

Pythonで学ぶ自然言語処理(NLP)

Preparing Video For Download...