序列生成任务

Python 中的自然语言处理(NLP)

Fouad Trad

Machine Learning Engineer

序列生成

  • 基于输入生成新文本
  • 包含任务:
    • 文本摘要
    • 文本翻译
    • 语言建模

一支铅笔在纸上写句子的动图。

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)

文本翻译

  • 将文本从一种语言转换为另一种
  • 多语言应用中至关重要:
    • 国际化网站
    • 客服工具

文本从英语(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)

让我们来练习!

Python 中的自然语言处理(NLP)

Preparing Video For Download...