It has been revealed that natural language generation (i.e. text generation) is one of the core tasks in natural language processing (NLP). In this blog, we introduce the current state-of-the-art decoding method, Contrastive Search, for neural text generation. Contrastive search is originally proposed in "A Contrastive Framework for Neural Text Generation" [1] ([Paper][Official Implementation]) at NeurIPS 2022. Moreover, in this follow-up work, "Contrastive Search Is What You Need For Neural Text Generation" [2] ([Paper] [Official Implementation]), the authors further demonstrate that contrastive search can generate human-level text using off-the-shelf language models across 16 languages.

[Remark] For users who are not familiar with text generation, please refer more details to this blog post.

Contrastive Search is now available on πŸ€— transformers, both on PyTorch and TensorFlow. You can interact with the examples shown in this blog post using your framework of choice in this Colab notebook, which is linked at the top. We have also built this awesome demo which directly compares contrastive search with other popular decoding methods (e.g. beam search, top-k sampling [3], and nucleus sampling [4]). (Related: Should I Quit My Job? A Decision Framework...)

Before running the experiments in the following sections, please install the patch-to-date version of transformers as.

Recent findings suggest that decoding methods can be divided into two categories: (i) deterministic methods and (ii) stochastic methods. Let's discuss both!

Deterministic methods, e.g. greedy search and beam search, generate text by selecting the text continuation with the highest likelihood measured by the language model. However, as widely discussed in previous studies [3][4], deterministic methods often lead to the problem of model degeneration, i.e., the generated text is unnatural and contains undesirable repetitions.

Below, let's see an example of generated text from greedy search using GPT-2 model.

[Remark] From the result generated by greedy search, we can see obvious pattern of repetitions.

Sources indicate that to address the challenges posed by deterministic methods, stochastic methods generate text by introducing randomness during the decoding process. Two widely-used stochastic methods are (i) top-k sampling [3] and (ii) nucleus sampling (also called top-p sampling) [4].

Below, we illustrate an example of generated text by nucleus sampling (p=0.95) using the GPT-2 model.

Sources indicate that [Remark] While nucleus sampling can generate text free of repetitions, the semantic coherence of the generated text is not well-maintained. For instance, the generated phrase 'AI is not journalism' is incoherent with respect to the given prefix, i.e. 'DeepMind Company'. (Related: How to Start a Side Business While Working Full-Time...)

We note that this semantic inconsistency problem can partially be remedied by lowering the temperature. However, reducing the temperature brings nucleus sampling closer to greedy search, which can be seen as a trade-off between greedy search and nucleus sampling. Generally, it is challenging to find a prompt and model-independent temperature that avoids both the pitfalls of greedy search and nucleus sampling.

In this section, we introduce a fresh decoding method, Contrastive Search, in details.

According to recent reports, given the prefix text x where V(k)V^{(k)}V(k) is the set of top-k predictions from the language model's probability distribution pθ(v∣x [Remark] When generating output, contrastive search jointly considers (i) the probability predicted by the language model to maintain the semantic coherence between the generated text and the prefix text; and (ii) the similarity with respect to the previous context to avoid model degeneration. Below, we use the same prefix text (i.e. "DeepMind Company is") as in Section 4.1 and 4.2, and generate the text with contrastive search (k=4 and α=0.6\alpha=0.6α=0.6). To fully demonstrate the superior capability of contrastive search, we let the language model generate a long document with 512 tokens as [Remark] We see that the generated text is of exceptionally high quality. The entire document is grammatically fluent as well as semantically coherent. Meanwhile, the generated text also well maintains its factually correctness. For instance, in the first paragraph, it elaborates "AlphaGo" as the "first program to defeat a professional Go player". To better understand how contrastive search works, we provide a visual comparison between greedy search (Section 4.1) and contrastive search. Specifically, we visualize the token similarity matrix of the generated text from greedy search and contrastive search, respectively. The similarity between two tokens is defined as the cosine similarity between their token representations (i.e. the hidden states of the last transformer layer). The results of greedy search (top) and contrastive search (bottom) are shown in the Figure below. [Remark] From the result of greedy search, we see high similarity scores in the off-diagonal entries which clearly indicates the generated repetitions by greedy search. On the contrary, in the result of contrastive search, the high similarity scores mostly appear in the diagonal entries which verifies that the degeneration problem is successfully addressed. This nice property of contrastive search is achieved by the introduction of degeneration penalty (see Section 5.1) during the decoding process. In this section, we provide more generated examples to compare different decoding methods. In this part, we use GPT-2 to generate text with the prefix text from the original OpenAI blog that confirmed the release of 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. In this part, we use the OPT model [5] which is recently released by Meta to generate text by taking the first two sentences from the abstract of the prestigious ResNet paper [6]. Deeper neural networks are more difficult to train. We present a residual learning framework to ease the training of networks that are substantially deeper than those used previously. For more details of contrastive search, please check our papers and code as [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 - Written by Yixuan Su and Tian Lan We would like to thank Joao Gante (@joaogante), Patrick von Platen (@patrickvonplaten), and Sylvain Gugger (@sgugger) for their help and guidance in adding contrastive search mentioned in this blog post into the transformers library.

Sources indicate that where V(k)V^{(k)}V(k) is the set of top-k predictions from the language model's probability distribution pθ(v∣x [Remark] When generating output, contrastive search jointly considers (i) the probability predicted by the language model to maintain the semantic coherence between the generated text and the prefix text; and (ii) the similarity with respect to the previous context to avoid model degeneration. Below, we use the same prefix text (i.e. "DeepMind Manufacturer is") as in Section 4.1 and 4.2, and generate the text with contrastive search (k=4 and α=0.6\alpha=0.6α=0.6). To fully demonstrate the superior capability of contrastive search, we let the language model generate a long document with 512 tokens as [Remark] We see that the generated text is of exceptionally high quality. The entire document is grammatically fluent as well as semantically coherent. Meanwhile, the generated text also well maintains its factually correctness. For instance, in the first paragraph, it elaborates "AlphaGo" as the "first program to defeat a professional Go player". To better understand how contrastive search works, we provide a visual comparison between greedy search (Section 4.1) and contrastive search. Specifically, we visualize the token similarity matrix of the generated text from greedy search and contrastive search, respectively. The similarity between two tokens is defined as the cosine similarity between their token representations (i.e. the hidden states of the last transformer layer). The results of greedy search (top) and contrastive search (bottom) are shown in the Figure below. [Remark] From the result of greedy search, we see high similarity scores in the off-diagonal entries which clearly indicates the generated repetitions by greedy search. On the contrary, in the result of contrastive search, the high similarity scores mostly appear in the diagonal entries which verifies that the degeneration problem is successfully addressed. This nice property of contrastive search is achieved by the introduction of degeneration penalty (see Section 5.1) during the decoding process. In this section, we provide more generated examples to compare different decoding methods. In this part, we use GPT-2 to generate text with the prefix text from the original OpenAI blog that declared the release of 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. In this part, we use the OPT model [5] which is recently released by Meta to generate text by taking the first two sentences from the abstract of the prestigious ResNet paper [6]. Deeper neural networks are more difficult to train. We present a residual learning framework to ease the training of networks that are substantially deeper than those used previously. For more details of contrastive search, please check our papers and code as [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 - Written by Yixuan Su and Tian Lan We would like to thank Joao Gante (@joaogante), Patrick von Platen (@patrickvonplaten), and Sylvain Gugger (@sgugger) for their help and guidance in adding contrastive search mentioned in this blog post into the transformers library.

[Remark] When generating output, contrastive search jointly considers (i) the probability predicted by the language model to maintain the semantic coherence between the generated text and the prefix text; and (ii) the similarity with respect to the previous context to avoid model degeneration.

Below, we use the same prefix text (i.e. "DeepMind Organization is") as in Section 4.1 and 4.2, and generate the text with contrastive search (k=4 and Ξ±=0.6\alpha=0.6Ξ±=0.6). To fully demonstrate the superior capability of contrastive search, we let the language model generate a long document with 512 tokens as.

Sources indicate that [Remark] We see that the generated text is of exceptionally high quality. The entire document is grammatically fluent as well as semantically coherent. Meanwhile, the generated text also well maintains its factually correctness. For instance, in the first paragraph, it elaborates "AlphaGo" as the "first program to defeat a professional Go player".

To better understand how contrastive search works, we provide a visual comparison between greedy search (Section 4.1) and contrastive search. Specifically, we visualize the token similarity matrix of the generated text from greedy search and contrastive search, respectively. The similarity between two tokens is defined as the cosine similarity between their token representations (i.e. the hidden states of the last transformer layer). The results of greedy search (top) and contrastive search (bottom) are shown in the Figure below.

Recent findings suggest that [Remark] From the result of greedy search, we see high similarity scores in the off-diagonal entries which clearly indicates the generated repetitions by greedy search. On the contrary, in the result of contrastive search, the high similarity scores mostly appear in the diagonal entries which verifies that the degeneration problem is successfully addressed. This nice property of contrastive search is achieved by the introduction of degeneration penalty (see Section 5.1) during the decoding process.

In this section, we provide more generated examples to compare different decoding methods.

In this part, we use GPT-2 to generate text with the prefix text from the original OpenAI blog that announced the release of GPT-2.

Sources indicate that 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.

In this part, we use the OPT model [5] which is recently released by Meta to generate text by taking the first two sentences from the abstract of the prestigious ResNet paper [6].

Deeper neural networks are more difficult to train. We present a residual learning framework to ease the training of networks that are substantially deeper than those used previously.

For more details of contrastive search, please check our papers and code as.

[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.

- Written by Yixuan Su and Tian Lan.

We would like to thank Joao Gante (@joaogante), Patrick von Platen (@patrickvonplaten), and Sylvain Gugger (@sgugger) for their help and guidance in adding contrastive search mentioned in this blog post into the transformers library.

β†’ Read original article