Skip to content
Back to log

What do temperature and top-p do?

Chainabit's Chao · · 7 min read

In short

Temperature affects randomness in token selection. Top-p determines the set of candidates to choose from based on their cumulative probability. Explore how the two settings work and their limits.

Token selection and sampling

When a language model generates text, it builds the continuation token by token. In the Nucleus Sampling study by Holtzman and colleagues, the preceding sequence of tokens is used as context for each new token. The probabilities the model assigns to possible continuations affect which tokens can be selected during sampling.

The researchers define open-ended text generation as the task of producing a coherent continuation of a given context. In this type of generation, the context constrains the range of suitable continuations; however, there may still be multiple plausible options for what comes next. The token selection examined in the study is part of the process of producing that continuation.

Google Cloud states that parameter values in calls sent to the model affect how the response is generated. Different values can lead to different results. The available parameters can also vary by model.

Temperature and top-p affect different aspects of token selection. According to Google Cloud's explanation, temperature controls the degree of randomness in selection. Top-p changes how the candidate tokens for selection are determined. Both settings are related to sampling during response generation.

What does temperature change?

According to Google Cloud, low temperature values are suitable for prompts that require a less open-ended or less creative response. Higher values can lead to more varied or creative results. This does not mean that a high value will produce a particular result in every response.

Google Cloud states that when the temperature value is 0, the highest-probability tokens are always selected. Responses to the same prompt are largely deterministic in this case. Small variations may still occur.

Holtzman and colleagues describe temperature as a method for shaping the probability distribution. In the study, the calculation recomputes the softmax distribution using the model's logits values and the temperature value. This process changes the distribution of probabilities used for sampling.

According to the study, temperature values between zero and one shift the distribution toward high-probability events. This process also reduces the total probability in the tail of the distribution made up of low-probability tokens. The researchers also refer to studies in which the distribution is shaped with low temperature before top-k sampling.

Google Cloud recommends increasing the temperature value when the response is too generic or short, or when the model gives a fallback response. Raising the temperature value to at least 0.1 when the model enters infinite generation may also improve results. These are recommendations specific to the situations described by Google Cloud.

How does top-p determine candidates?

In the method Holtzman and colleagues call Nucleus Sampling, the set of tokens eligible for selection is determined by the shape of the probability distribution. The top-p set is the smallest set of tokens whose cumulative probability reaches the specified p threshold. The process builds this set starting with the highest-probability tokens.

In the study, after the set is determined, the original distribution is rescaled and the next token is sampled from this new distribution. Tokens excluded from the set have zero probability in the new distribution. The threshold determines which tokens participate in sampling based on cumulative probability.

In Google Cloud's example, the probabilities of tokens A, B, and C are given as 0.3, 0.2, and 0.1, respectively. When the top-p value is 0.5, the combined probability of A and B reaches that value. The next token is selected from A or B using temperature; C is not included in the candidate set.

In this example, top-p does not mean that the selected token has a probability of 0.5 on its own. The threshold is met by the sum of the probabilities of A and B. In the process described by Google Cloud, whether the next token is A or B is determined by selection from this candidate set using temperature.

Google Cloud states that a lower top-p value should be used for less random responses and a higher value for more random responses. Top-p concerns the cumulative probability of the candidates to be selected at each step. In Google Cloud's example, temperature is used during sampling from these candidates.

Why does the number of candidates change?

Turn this insight into action.

Use the mobile app to continue with guided workflows.

According to the Nucleus Sampling study, the size of the top-p set changes according to the shape of the distribution at each token generation step. In some contexts, the probability for the next word may be spread across many plausible candidates. In other contexts, a large portion of the total probability may be concentrated in one or a few tokens.

The researchers explain that in the first case, the distribution is relatively flat and many candidates can have similar probabilities. In the other case, the distribution is more peaked. Different numbers of tokens may be needed in these two cases to reach the same cumulative probability threshold.

The researchers explain that the number of candidates is kept fixed in the top-k sampling method. A small k carries the risk of producing generic or bland text in contexts where probability is spread across many plausible options. A large k, on the other hand, can include unsuitable candidates in the set. Rescaling can increase the probability of selecting these candidates.

In the top-p method, the number of candidates increases or decreases with changes in the confidence region over the model's vocabulary. As the researchers explain it, the threshold does not directly fix the number of selected tokens; it defines the set through cumulative probability.

According to the study, both top-k and Nucleus Sampling perform sampling by truncating the language model's distribution. The difference between them is the method used to determine where to truncate. Top-k retains k candidates at each step, while Nucleus Sampling uses the set that meets the cumulative probability threshold.

Temperature and top-p together

Google Cloud states that when topP and topK are applied during response generation, temperature is used for sampling. According to the sequence it describes, at each token selection step, the top-k tokens with the highest probabilities are taken first. The candidates are then filtered further using top-p. The final token is selected through temperature sampling.

In this process, the number given for top-k and the top-p threshold express different things. In Google Cloud's example, when the top-k value is 3, a selection is made from the three highest-probability tokens using temperature. In the top-p example, the candidate set is determined by the sum of the probabilities.

Google Cloud also states that when the top-k value is 1, the highest-probability token in the model's vocabulary is selected. This process is also called greedy decoding. In the example with a top-k value of 3, the selection is made from the three highest-probability candidates.

According to Google Cloud, changing parameter settings, including temperature, can cause changes in the response even when the same seed is used. With a fixed seed, the model makes an effort to give the same response to repeated requests; deterministic output is still not guaranteed. Small variations are also possible when temperature is 0.

Limits and research findings

Holtzman and colleagues observed text that was weakly related to the context and incoherent with pure sampling directly from the probabilities predicted by the model. The researchers associate this with the unreliable tail of the distribution. This tail consists of candidate tokens that individually have low probabilities but carry more weight when considered collectively.

According to the same study, temperature may not sufficiently suppress the unreliable tail while changing the shape of the distribution. The researchers propose truncating this tail in the top-p approach and sampling from a dynamic set that contains most of the total probability.

The study also found that using low temperature can lead to problems with diversity and repetition. More repetition was observed in texts generated with temperature sampling when very high temperature values were not used. The researchers state that very high values negatively affected coherence in their evaluation.

The researchers also examine the problem of repetition across different sampling methods. They state that when tuning parameters are set too low, these methods can truncate the distribution excessively and approach greedy search behavior. This finding is not an observation concerning the temperature value alone.

Nucleus Sampling does not mean error-free output either. In one example from the study, while continuing a text about whales, the model confused whales with birds and began writing about birds. This observation belongs to the study's example output.

Ready to start?

Download the app and continue from your phone.

End of Entry