3
$\begingroup$

What does the temperature parameter mean when talking about the GPT models?

I know that a higher temperature value means more randomness, but I want to know how randomness is introduced.

Does temperature mean we add noise to the weights/activations or do we add randomness when choosing a token in the softmax layer?

$\endgroup$
6
$\begingroup$

In sequence generating models, for vocabulary of size $N$ (number of words, parts of words, any other kind of token), one predicts the next token from distribution of the form: $$ \mathrm{softmax} (x_i/T) \quad i = 1, \ldots N, $$ Here $T$ is the temperature. The output of the softmax is the probability that the next token will be the $i$-th word in the vocabulary.

The temperature determines how greedy the generative model is.

If the temperature is low, the probabilities to sample other but the class with the highest log probability will be small, and the model will probably output the most correct text, but rather boring, with small variation.

If the temperature is high, the model can output, with rather high probability, other words than those with the highest probability. The generated text will be more diverse, but there is a higher possibility of grammar mistakes and generation of nonsense.

enter image description here

The difference between the low-temperature case (left) and the high-temperature case for the categorical distribution is illustrated in the picture above, where the heights of the bars correspond to probabilities.

Example

A good sample is provided in the Deep Learning with Python by François Chollet in chapter 12. An extract from the tutorial, refer to this notebook.

import numpy as np

tokens_index = dict(enumerate(text_vectorization.get_vocabulary()))

def sample_next(predictions, temperature=1.0):
    predictions = np.asarray(predictions).astype("float64")
    predictions = np.log(predictions) / temperature
    exp_preds = np.exp(predictions)
    predictions = exp_preds / np.sum(exp_preds)
    probas = np.random.multinomial(1, predictions, 1)
    return np.argmax(probas)
$\endgroup$
4
  • $\begingroup$ What is the range of the parameter $T$? $\endgroup$
    – nbro
    Nov 21 '21 at 18:46
  • $\begingroup$ @nbro $T$ can be any positive number $T \in (0, \infty)$. Reasonable choice dependents on the particular model since different model produce logits with different magnitude. I added the reference to the popular book on DL. Seems like the idea itself is pretty old. $\endgroup$ Nov 21 '21 at 19:06
  • 1
    $\begingroup$ Yes, I guess it's an old idea. The concept of a "temperature" also pops up in other contexts, like simulated annealing, but I am not sure they have a similar role in both cases. Although you don't mention GPT-3, I suspect that your definition of temperature is what is being used also in that context. It might be worth checking. $\endgroup$
    – nbro
    Nov 21 '21 at 19:16
  • $\begingroup$ I guess this is what NovelAI labels as "randomness" $\endgroup$
    – user253751
    Nov 22 '21 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.