Deterministic strategies for generating text: always pick the most likely token (greedy) or keep several candidate sequences and choose the best overall (beam search).
Greedy decoding picks the most likely token at each step. It amounts to setting the
temperature to zero and always yields the same output for the same input
(Alammar & Grootendorst, 2024, pp. 79–80). Its flaw is short-sightedness: the best choice at each step is not
the best overall. An example with made-up probabilities:
first step: "the" 0.5 · "once" 0.4
after "the": "house" 0.4 (the rest spread out) → "the house": 0.5 × 0.4 = 0.20
after "once": "upon" 0.9 → "once upon": 0.4 × 0.9 = 0.36
Greedy decoding commits to "the" and never sees the more likely sequence. Beam search
keeps the k best partial sequences in parallel (the beam width), extends them all, and again
keeps the k best. With k = 2 it finds "once upon."
Beam search was the standard in neural machine translation: Sutskever et al. (2014) found that a
beam of size 2 already gave most of the benefit (2014, sec. 3.2), and the original
Transformer used a beam of 4 with a length penalty (Vaswani et al., 2017, sec. 6.1), as did
GPT-3 on free-form tasks (Brown et al., 2020, sec. 2.4). In conversation and open-ended writing,
however, searching for the most likely sequence produces bland, repetitive text, which is why
sampling is preferred (Holtzman et al., 2020).
For a psycholinguist, greedy short-sightedness has a name: garden-path sentences, such as
Bever's (1970) classic "The horse raced past the barn fell," where the reader commits to the
most likely parse and has to backtrack. An autoregressive model cannot backtrack: what is
written stays written. Beam search is a limited way of keeping several readings open at once.
Sources
- Alammar, J., & Grootendorst, M. (2024). Hands-on large language models: Language understanding and generation. O’Reilly Media. §
- Brown, T. B., Mann, B., Ryder, N., Subbiah, M., Kaplan, J., Dhariwal, P., Neelakantan, A., Shyam, P., Sastry, G., Askell, A., Agarwal, S., Herbert-Voss, A., Krueger, G., Henighan, T., Child, R., Ramesh, A., Ziegler, D. M., Wu, J., Winter, C., . . . Amodei, D. (2020). Language models are few-shot learners. In Advances in Neural Information Processing Systems (Vol. 33, pp. 1877–1901). Curran Associates. https://proceedings.neurips.cc/paper_files/paper/2020/hash/1457c0d6bfcb4967418bfb8ac142f64a-Abstract.html §
- Holtzman, A., Buys, J., Du, L., Forbes, M., & Choi, Y. (2020). The curious case of neural text degeneration. In 8th International Conference on Learning Representations (ICLR 2020). https://openreview.net/forum?id=rygGQyrFvH §
- Sutskever, I., Vinyals, O., & Le, Q. V. (2014). Sequence to sequence learning with neural networks. In Advances in Neural Information Processing Systems (Vol. 27). Curran Associates. https://proceedings.neurips.cc/paper_files/paper/2014/hash/5a18e133cbf9f257297f410bb7eca942-Abstract.html §
- Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention is all you need. In Advances in Neural Information Processing Systems (Vol. 30). Curran Associates. https://proceedings.neurips.cc/paper_files/paper/2017/hash/3f5ee243547dee91fbd053c1c4a845aa-Abstract.html §