Text generation
Text generation using a neural network works autoregressively: the model produces one token at a time, and each newly generated token is appended to the in...
Autoregressive Generation and the Token-by-Token Process
Text generation using a neural network works autoregressively: the model produces one token at a time, and each newly generated token is appended to the input before generating the next. The model is trained to predict the probability distribution over the vocabulary for the next token given all preceding tokens, and at inference time it samples from or greedily selects from this distribution repeatedly until a stopping condition is reached.
An example makes this concrete. Given the prompt the weather today is, the model assigns probabilities to every word in its vocabulary as a potential next token. It might assign high probability to sunny, cold, rainy, and uncertain and low probability to unrelated words. After selecting a token, it extends the sequence to the weather today is sunny and repeats the process to generate the remainder of the sentence.
Exam tip: Understand the difference between greedy decoding, beam search, and sampling with temperature. Greedy decoding always picks the most likely token and is fast but often produces repetitive or suboptimal text. Beam search maintains multiple candidate sequences simultaneously. Temperature scaling adjusts how peaked the sampling distribution is, with low temperature producing more deterministic output and high temperature more diverse but less coherent output.
Training Objectives for Language Generation
Language models for text generation are typically trained with a causal language modeling objective, also called next-token prediction. The model processes a sequence of tokens and is trained to maximize the log probability of each token given the tokens that precede it in the sequence. This objective requires no external labels beyond the text itself, making it possible to train on enormous corpora of raw text from the internet, books, and code.
The loss is averaged over all token positions in the training batch, which means longer documents contribute proportionally more gradient signal than shorter ones. To handle variable-length sequences efficiently, training batches often pack multiple shorter documents end-to-end with special separator tokens, filling a fixed context window.
Modern large language models are trained on trillions of tokens using this objective, and the resulting models acquire broad language understanding and generation capability as an emergent result of optimizing the prediction loss at scale.
Controlling Output Quality and Safety
Raw autoregressive generation can produce factually incorrect, repetitive, or harmful text. Several post-training techniques improve output quality. Reinforcement learning from human feedback trains the model to produce outputs that human evaluators rate highly, aligning generation toward helpful, accurate, and safe responses. Constitutional AI refines this approach by having the model critique and revise its own outputs according to a set of stated principles, reducing the need for as many human annotations.
Sampling strategies also affect quality significantly. Top-k sampling restricts sampling to only the k most probable tokens, preventing unlikely but grammatically valid tokens from being chosen. Nucleus sampling, also called top-p, samples from the smallest set of tokens whose cumulative probability exceeds a threshold p, adapting the effective vocabulary size dynamically based on how peaked the distribution is.
For an interview discussion of text generation, connecting the technical mechanism of autoregressive sampling to its practical effects on output diversity and safety demonstrates the ability to reason from fundamentals to user-facing consequences.