Sub topic 10
The Learned Temperature (Logit Scale)
Prerequisites and purpose
This chapter assumes you know vector normalization, dot products, softmax, and cross-entropy loss. It also assumes the basic CLIP setup: an image encoder and a text encoder produce one vector for each input, and matching image-text pairs should have similar vectors.
CLIP usually normalizes both vectors to unit length. Their dot product is then cosine similarity, with values from -1 to 1. These similarities are useful for measuring alignment, but they may not be separated enough for classification. CLIP therefore multiplies them by a learned logit scale before applying cross-entropy.
From similarity to logits
For a batch of N matching image-text pairs, let v_i be an image embedding and t_j be a text embedding. After normalization, the similarity matrix is:
S[i, j] = v_i · t_jThe diagonal entries S[i, i] represent matching pairs. The other entries are in-batch negatives: they are not the known match for that image.
CLIP converts similarities into logits with:
L[i, j] = exp(s) × S[i, j]Here, s is a learned scalar parameter called logit_scale. The model stores its logarithm so that exp(s) is always positive. This is equivalent to dividing by a positive temperature τ:
L[i, j] = S[i, j] / τ
τ = 1 / exp(s)A small temperature produces large logit differences and a sharper softmax distribution. A large temperature produces smaller differences and a softer distribution.
Why learn the scale?
The encoders determine which pairs are close. The scale determines how confidently the loss interprets those similarities.
Suppose the correct pair has similarity 0.70, while a negative pair has similarity 0.60. The difference is only 0.10. Without scaling, softmax may assign substantial probability to both pairs. A larger scale magnifies the difference, making the correct pair more likely.
The scale also affects gradients. If it becomes too large, the softmax becomes nearly one-hot. Most examples then produce very small gradients, and training can become unstable. If it is too small, predictions remain nearly uniform, so the model receives weak pressure to distinguish matches.
Implementing the scale
The following loss uses one learned log-scale parameter. It applies cross-entropy in both directions: image-to-text and text-to-image.
import torch
import torch.nn.functional as F
def clip_loss(image_emb, text_emb, logit_scale):
# Each row in the two batches is a matching pair.
image_emb = F.normalize(image_emb, dim=-1)
text_emb = F.normalize(text_emb, dim=-1)
scale = logit_scale.exp()
logits = scale * image_emb @ text_emb.t()
labels = torch.arange(logits.size(0), device=logits.device)
image_to_text = F.cross_entropy(logits, labels)
text_to_image = F.cross_entropy(logits.t(), labels)
return (image_to_text + text_to_image) / 2In a training model, logit_scale is typically created as a trainable scalar, such as torch.nn.Parameter. Implementations may also clamp the resulting scale to prevent an extreme value.
Practical checks
When debugging CLIP training, inspect the scale during training. A rapidly growing scale can indicate overconfident logits or poor regularization. A scale that remains too small can indicate weak separation between positive and negative pairs.
The scale is shared across the batch and both retrieval directions. It is not a separate temperature for each image, caption, or class.
Exercise
If exp(logit_scale) increases while the similarity matrix stays the same, what changes?
Show answer
The logits become farther apart, so softmax becomes sharper and the model expresses more confidence in the highest-similarity matches.