序列生成任務

Python 的 Natural Language Processing(NLP)

Fouad Trad

Machine Learning Engineer

序列生成

  • 依輸入產生新文本
  • 常見任務:
    • 文本摘要
    • 文本翻譯
    • 語言建模

鉛筆在紙上寫句子的 GIF。

Python 的 Natural Language Processing(NLP)

文本摘要

  • 將長文濃縮為重點摘要
  • 適用於:
    • 冗長新聞
    • 研究論文
    • 報告
    • 電子郵件

一疊書籍與其產生的摘要文件。

Python 的 Natural Language Processing(NLP)

文本摘要流程(pipeline)

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 的 Natural Language Processing(NLP)

文本翻譯

  • 將文本從一種語言轉為另一種
  • 多語系應用很關鍵:
    • 國際化網站
    • 客服工具

文字從英文(Good morning)翻譯成法文(Bonjour)的示意圖。

Python 的 Natural Language Processing(NLP)

文本翻譯流程(pipeline)

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 的 Natural Language Processing(NLP)

語言建模

  • 依提示預測下一些詞
  • 許多應用的基礎:
    • 自動補全
    • 故事生成
    • 聊天機器人回覆

語言建模示意:輸入提示,機器產生文本。

Python 的 Natural Language Processing(NLP)

語言建模流程(pipeline)

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 的 Natural Language Processing(NLP)

一起來練習吧!

Python 的 Natural Language Processing(NLP)

Preparing Video For Download...