对比搜索演示:神经网络文本生成的最新解码方法

释放双眼,带上耳机,听听看~!
了解神经网络文本生成领域最先进的对比搜索解码方法,探索🤗 transformers的支持,并使用贪心搜索生成文本的演示。

1. 引言

自然语言生成 (即文本生成) 是自然语言处理 (NLP) 的核心任务之一。本文将介绍神经网络文本生成领域当前最先进的解码方法 对比搜索 (Contrastive Search)。提出该方法的论文 “A Contrastive Framework for Neural Text Generation” 最初发表于 NeurIPS 2022 ([论文][官方实现])。此后, “Contrastive Search Is What You Need For Neural Text Generation” 的作者又进一步证明了对比搜索可以用 现有的 语言模型在 16 种语言上生成可媲美人类水平的文本 ([论文][官方实现])。

[备注] 对于不熟悉文本生成的用户,请参阅 此博文 了解更多详情。

2. Hugging Face 🤗 对比搜索演示

目前,🤗 transformers 的 PyTorch 和 TensorFlow 后端均支持对比搜索。你可以在 该 Colab notebook 中根据不同的后端选择相应的部分来探索该方法,文章顶部也有该 notebook 链接。我们还构建了这个不错的 演示应用,用它可以直观地比较对比搜索与其他流行的解码方法 (例如波束搜索、top-k 采样 OpenAI 发布 GPT-2 的博客文章

In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English.

加载语言模型并准备前缀文本:
import torch
from transformers import AutoTokenizer, GPT2LMHeadModel

tokenizer = AutoTokenizer.from_pretrained('gpt2-large')
model = GPT2LMHeadModel.from_pretrained('gpt2-large')

prefix_text = r"In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English."
input_ids = tokenizer(prefix_text, return_tensors='pt').input_ids

6.1.1. 使用贪心搜索生成文本
代码: [点击展开]
output = model.generate(input_ids, max_length=512)
print("Output:n" + 100 *'-')
print(tokenizer.decode(output[0], skip_special_tokens=True))
print("" + 100 *'-')
模型输出: [点击展开]
Output:
----------------------------------------------------------------------------------------------------
In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English.

The researchers, led by Dr. David R. Williams of the University of California, Santa Cruz, discovered the unicorns in the Andes Mountains of Peru. The area is known for its unique geology and is home to a number of rare species of animals.

The researchers found the unicorns in the Andes Mountains of Peru.

"We were surprised to find that the unicorns were able to communicate with each other," Williams said. "We were also surprised to find that they were able to communicate in English."

The researchers believe that the unicorns are descendants of the ancient Incas, who lived in the area around 2,000 years ago.

"The Incas were the first people to use the Andes Mountains as a refuge from the harsh climate of the Andes," Williams said. "They were also the first people to use the Andes Mountains as a place to hunt and gather food."

The researchers believe that the unicorns are descendants of the ancient Incas, who lived in the area around 2,000 years ago.

"The Incas were the first people to use the Andes Mountains as a refuge from the harsh climate of the Andes," Williams said. "They were also the first people to use the Andes Mountains as a place to hunt and gather food."

The researchers believe that the unicorns are descendants of the ancient Incas, who lived in the area around 2,000 years ago.

"The Incas were the first people to use the Andes Mountains as a refuge from the harsh climate of the Andes," Williams said. "They were also the first people to use the Andes Mountains as a place to hunt and gather food."

The researchers believe that the unicorns are descendants of the ancient Incas, who lived in the area around 2,000 years ago.

"The Incas were the first people to use the Andes Mountains as a refuge from the harsh climate of the Andes," Williams said. "They were also the first people to use the Andes Mountains as a place to hunt and gather food."

The researchers believe that the unicorns are descendants of the ancient Incas, who lived in the area around 2,000 years ago
----------------------------------------------------------------------------------------------------

6.1.2. 使用核采样生成文本
代码: [点击展开]
torch.manual_seed(0.)
output = model.generate(input_ids, do_sample=True, max_length=512, top_p=0.95, top_k=0)
print("Output:n" + 100 *'-')
print(tokenizer.decode(output[0], skip_special_tokens=True))
print("" + 100 *'-')
模型输出: [点击展开]
Output:
----------------------------------------------------------------------------------------------------
In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English. The study was published in the Journal of Zoology in March 2016.

Polygynous mammals such as unicorns have remained largely unknown to science. Professor Gustavo Giacota, from the University of Oxford who led the study, said that they had been documented as far as Eastern Siberia in Russia, but had only been seen a handful of times in the Gobi Desert.

Tiny animals with pale and shiny coats live in the presence of human beings and are hardly likely to be victims of any cruelty. However, there is some evidence of the condition occurring in both humans and animals in remote regions, which might have similarities to "black moles" that coexist on the skin.

It is thought that Unicorns could be inside themselves, that they have different scents depending on their current environment, or just fall out and there are plenty of legends of how they have survived. Experts speculate that the moths and other animals could be remnants of the Yezidi Isis and Charon, which literally is both the word which means great bird, and the Greek word for sound. It is said that the Isis and Charon taught their young the use of voice in the form of calling out to others.

The scientists think that it could be ancient folklore that has survived and is no longer attributed to a real entity
----------------------------------------------------------------------------------------------------

6.1.3. 使用对比搜索生成文本
代码:
output = model.generate(input_ids, max_length=512, penalty_alpha=0.6, top_k=4)
print("Output:n" + 100 *'-')
print(tokenizer.decode(output[0], skip_special_tokens=True))
print("" + 100 *'-')
模型输出:
Output:
----------------------------------------------------------------------------------------------------
In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English.

According to the BBC, a team of scientists led by Dr David MacKay, from the University of Bristol, spent two years searching for the unicorn herd, which they discovered during a survey of the area.

"It's a very rare find," MacKay told the BBC. "There are a few in the Himalayas, but this is the first time we've been able to find one in such a remote area."

The team was surprised to find a herd of unicorns living in a region that has been known to be a hotbed of poaching, with many of the animals poached for their horns, which are used in traditional Chinese medicine to treat everything from rheumatism to cancer.

"We knew that the area was rich in rhino horn, but we had no idea how many there were, or what they were doing there," MacKay said. "This is an area of high poaching pressure, and we wanted to find out what was going on."

In order to do so, the team used GPS collars to track the animals as they moved around the mountain and the surrounding area. The GPS data was then compared with information gathered from local villagers, who had a wealth of information about the animals' movements, including where they were eating, what they were doing at night, and how much time they spent in the mountains each day.

After analyzing the data, the team determined that the herd consisted of at least three species of unicorns, including a male and two females. One of the females was the mother of the male, and the other two were her daughters. All three had the same horn color, which is believed to be a sign of purity in the animal kingdom.

While the discovery is exciting, it's not the first time scientists have discovered an animal that speaks English. Last year, scientists discovered a species of porcupine that can be heard by humans, and has been dubbed "Porcupine Man" for his ability to converse with the human race.
----------------------------------------------------------------------------------------------------

6.2. 示例二: OPT

本节中,我们使用 Meta 最近发布的 OPT 模型 论文官方实现

  • Contrastive Search Is What You Need For Neural Text Generation: 论文官方实现
  • 8. 引用

    @inproceedings{su2022a,
       title={A Contrastive Framework for Neural Text Generation},
       author={Yixuan Su and Tian Lan and Yan Wang and Dani Yogatama and Lingpeng Kong and Nigel Collier},
       booktitle={Advances in Neural Information Processing Systems},
       editor={Alice H. Oh and Alekh Agarwal and Danielle Belgrave and Kyunghyun Cho},
       year={2022},
       url={https://openreview.net/forum?id=V88BafmH9Pj}
    }
    
    @article{su2022contrastiveiswhatyouneed,
      title={Contrastive Search Is What You Need For Neural Text Generation},
      author={Su, Yixuan and Collier, Nigel},
      journal={arXiv preprint arXiv:2210.14140},
      year={2022}
    }
    

    参考文献

    [1] Su et al., 2022 “A Contrastive Framework for Neural Text Generation”, NeurIPS 2022

    [2] Su and Collier, 2022 “Contrastive Search Is What You Need For Neural Text Generation”, Arxiv 2022

    [3] Fan et al., 2018 “Hierarchical Neural Story Generation”, ACL 2018

    [4] Holtzman et al., 2020 “The Curious Case of Neural Text Degeneration”, ICLR 2020

    [5] Zhang et al., 2022 “OPT: Open Pre-trained Transformer Language Models”, Arxiv 2022

    [6] He et al., 2016 “Deep Residual Learning for Image Recognition”, CVPR 2016

    – 本文由 Yixuan Su 和 Tian Lan 撰写

    致谢

    我们要感谢 Joao Gante (@joaogante)、Patrick von Platen (@patrickvonplaten) 和 Sylvain Gugger (@sgugger),感谢他们在我们将本文中的对比搜索集成进 transformers 库的过程中给予的帮助和指导。

    英文原文: hf.co/blog/introd…

    原文作者: Tian Lan

    译者: Matrix Yao (姚伟峰),英特尔深度学习工程师,工作方向为 transformer-family 模型在各模态数据上的应用及大规模模型的训练推理。

    审校/排版: zhongdongy (阿东)

    本网站的内容主要来自互联网上的各种资源,仅供参考和信息分享之用,不代表本网站拥有相关版权或知识产权。如您认为内容侵犯您的权益,请联系我们,我们将尽快采取行动,包括删除或更正。
    AI教程

    OpenAI插件:ChatGPT连接第三方应用程序功能介绍与使用教程

    2023-12-6 22:03:14

    AI教程

    SmartGPT:增强大型语言模型 (LLM) 的实验性程序

    2023-12-6 23:03:14

    个人中心
    购物车
    优惠劵
    今日签到
    有新私信 私信列表
    搜索