6 Generating text
Now that we can train models, the last piece we need is the ability to generate text from our model.
Recall that a language model takes in a (possibly batched) integer sequence of length sequence_length
and produces a matrix of size (sequence_length, vocab_size), where each element of the sequence is a
probability distribution predicting the next token after that position. We will now write a few functions
to turn this into a sampling scheme for new sequences.
Softmax
By standard convention, the language model output is the output of the final linear layer (the “logits”)
and so we have to turn this into a normalized probability via the softmax operation, which we saw earlier
in Equation 10.
Decoding
To generate text (decode) from our model, we will provide the model with a sequence of prefix tokens
(the “prompt”), and ask it to produce a probability distribution over the vocabulary that predicts the
next token in the sequence. Then, we will sample from this distribution over the vocabulary items to
determine the next output token.
Concretely, one step of the decoding process should take in a sequence 𝑥1…𝑡 and return a token 𝑥𝑡+1 via
the following equation,
exp(𝑣𝑖 )
𝑃 (𝑥𝑡+1 = 𝑖 | 𝑥1…𝑡 ) = (21)
∑𝑗 exp(𝑣𝑗 )
𝑣 = TransformerLM(𝑥1…𝑡 )𝑡 ∈ ℝvocab_size (22)
where TransformerLM is our model which takes as input a sequence of length sequence_length and
produces a matrix of size (sequence_length, vocab_size), and we take the last element of this matrix, as
we are looking for the next token prediction at the 𝑡-th position.
This gives us a basic decoder by repeatedly sampling from these one-step conditionals (appending our
previously-generated output token to the input of the next decoding timestep) until we generate the end-
of-sequence token <|endoftext|> (or a user-specified maximum number of tokens to generate).
Decoder tricks
We will be experimenting with small models, and small models can sometimes generate very low-quality
texts. Two simple decoder tricks can help fix these issues. First, in temperature scaling we modify our
softmax with a temperature parameter 𝜏 , where the new softmax is
exp(𝑣𝑖 /𝜏 )
softmax(𝑣, 𝜏 )𝑖 = vocab_size
. (23)
∑𝑗=1 exp(𝑣𝑗 /𝜏 )
Note how setting 𝜏 → 0 makes it so that the largest element of 𝑣 dominates, and the output of the
softmax becomes a one-hot vector concentrated at this maximal element.
9
wandb.ai
Second, another trick is nucleus or top-p sampling, where we modify the sampling distribution by
truncating low-probability tokens. Let 𝑞 be a probability distribution that we get from a (temperature-
scaled) softmax of size vocab_size. Nucleus sampling with hyperparameter 𝑝 produces the next token
according to the equation
𝑞𝑖
∑𝑗∈𝑉 (𝑝) 𝑞𝑗 if 𝑖 ∈ 𝑉 (𝑝)
𝑃 (𝑥𝑡+1 = 𝑖 | 𝑞) = { (24)
0 otherwise
where 𝑉 (𝑝) is the smallest set of indices such that ∑𝑗∈𝑉 (𝑝) 𝑞𝑗 ≥ 𝑝. You can compute this quantity easily
by first sorting the probability distribution 𝑞 by magnitude, and selecting the largest vocabulary elements
until you reach the target level of 𝑝.
Problem (decoding): Decoding (3 points)
Deliverable: Implement a function to decode from your language model. We recommend that
you support the following features:
• Generate completions for a user-provided prompt (i.e., take in some 𝑥1…𝑡 and sample a
completion until you hit an <|endoftext|> token).
• Allow the user to control the maximum number of generated tokens.
• Given a desired temperature value, apply softmax temperature scaling to the predicted next-
token distributions before sampling.
• Top-𝑝 sampling ([A. Holtzman et al., 2020] also referred to as nucleus sampling), given a user-
specified threshold value.
本章讨论
在 GitHub 查看记录 ↗