[GH-ISSUE #48] Two questions regarding model implementation: #43

Closed
opened 2026-10-31 21:10:11 +00:00 by bsakaguchi · 7 comments
Owner

Originally created by @vinsis on GitHub (Feb 24, 2021).
Original GitHub issue: https://github.com/openai/CLIP/issues/48

First of all thank you for providing the code and pretrained model. The results look stunning.

I was looking into the source code and there are two parts I am struggling to understand:

  1. What is the purpose of logit_scale in the model here? It seems to simply scale the dot products between two representations.
  2. In the implementation of VisualTransformer what is the role of class_embedding defined here.

Thanks!

Originally created by @vinsis on GitHub (Feb 24, 2021). Original GitHub issue: https://github.com/openai/CLIP/issues/48 First of all thank you for providing the code and pretrained model. The results look stunning. I was looking into the source code and there are two parts I am struggling to understand: 1. What is the purpose of `logit_scale` in the model [here](https://github.com/openai/CLIP/blob/main/clip/model.py#L362)? It seems to simply scale the dot products between two representations. 2. In the implementation of `VisualTransformer` what is the role of `class_embedding` defined [here](https://github.com/openai/CLIP/blob/main/clip/model.py#L209). Thanks!
Author
Owner

@PaulLerner commented on GitHub (Mar 3, 2021):

2. In the implementation of `VisualTransformer` what is the role of `class_embedding` defined [here](https://github.com/openai/CLIP/blob/main/clip/model.py#L209).

This is the embedding used for the final representation, see here they only keep the first "token" of each sequence.
See http://arxiv.org/abs/2010.11929 for more details about Vision Transformer :)

<!-- gh-comment-id:789935477 --> @PaulLerner commented on GitHub (Mar 3, 2021): > 2. In the implementation of `VisualTransformer` what is the role of `class_embedding` defined [here](https://github.com/openai/CLIP/blob/main/clip/model.py#L209). This is the embedding used for the final representation, see [here](https://github.com/openai/CLIP/blob/main/clip/model.py#L230) they only keep the first "token" of each sequence. See http://arxiv.org/abs/2010.11929 for more details about Vision Transformer :)
Author
Owner

@vinsis commented on GitHub (Mar 4, 2021):

Got it, thanks.

The answer to the first question still remains elusive.

<!-- gh-comment-id:790260388 --> @vinsis commented on GitHub (Mar 4, 2021): Got it, thanks. The answer to the first question still remains elusive.
Author
Owner

@jongwook commented on GitHub (Mar 4, 2021):

During contrastive learning, we interpret the dot products as logits that are fed to the cross-entropy loss for each text and image example. Because we normalize the feature vectors, the dot products are capped between [-1, 1], which might not have an enough dynamic range and limit the categorical probability distributions that the logits can express. For this reason, we scale the logits to have larger differences. To avoid numerical instability, we clipped the scale value at 100, and for all models that we trained, it reached 100. This comment explains how to clip the value.

<!-- gh-comment-id:790265298 --> @jongwook commented on GitHub (Mar 4, 2021): During contrastive learning, we interpret the dot products as logits that are fed to the cross-entropy loss for each text and image example. Because we normalize the feature vectors, the dot products are capped between [-1, 1], which might not have an enough dynamic range and limit the categorical probability distributions that the logits can express. For this reason, we scale the logits to have larger differences. To avoid numerical instability, we clipped the scale value at 100, and for all models that we trained, it reached 100. [This comment](https://github.com/openai/CLIP/issues/46#issuecomment-782558799) explains how to clip the value.
Author
Owner

@vinsis commented on GitHub (Mar 4, 2021):

Ok thanks. That explains it.

<!-- gh-comment-id:790376916 --> @vinsis commented on GitHub (Mar 4, 2021): Ok thanks. That explains it.
Author
Owner

@Horizon2333 commented on GitHub (Oct 17, 2021):

During contrastive learning, we interpret the dot products as logits that are fed to the cross-entropy loss for each text and image example. Because we normalize the feature vectors, the dot products are capped between [-1, 1], which might not have an enough dynamic range and limit the categorical probability distributions that the logits can express. For this reason, we scale the logits to have larger differences. To avoid numerical instability, we clipped the scale value at 100, and for all models that we trained, it reached 100. This comment explains how to clip the value.

So why don't you directly initialize temperature parameter to 100 ? What's the purpose of let it grow from 1/0.07 to 100? Does it bring enough performance improvement?

<!-- gh-comment-id:945062805 --> @Horizon2333 commented on GitHub (Oct 17, 2021): > During contrastive learning, we interpret the dot products as logits that are fed to the cross-entropy loss for each text and image example. Because we normalize the feature vectors, the dot products are capped between [-1, 1], which might not have an enough dynamic range and limit the categorical probability distributions that the logits can express. For this reason, we scale the logits to have larger differences. To avoid numerical instability, we clipped the scale value at 100, and for all models that we trained, it reached 100. [This comment](https://github.com/openai/CLIP/issues/46#issuecomment-782558799) explains how to clip the value. So why don't you directly initialize temperature parameter to 100 ? What's the purpose of let it grow from 1/0.07 to 100? Does it bring enough performance improvement?
Author
Owner

@justStarG commented on GitHub (May 30, 2024):

During contrastive learning, we interpret the dot products as logits that are fed to the cross-entropy loss for each text and image example. Because we normalize the feature vectors, the dot products are capped between [-1, 1], which might not have an enough dynamic range and limit the categorical probability distributions that the logits can express. For this reason, we scale the logits to have larger differences. To avoid numerical instability, we clipped the scale value at 100, and for all models that we trained, it reached 100. This comment explains how to clip the value.

So why don't you directly initialize temperature parameter to 100 ? What's the purpose of let it grow from 1/0.07 to 100? Does it bring enough performance improvement?

Yes, I think initialize to 100 will converge faster. But why not set to 100?

<!-- gh-comment-id:2138525159 --> @justStarG commented on GitHub (May 30, 2024): > > During contrastive learning, we interpret the dot products as logits that are fed to the cross-entropy loss for each text and image example. Because we normalize the feature vectors, the dot products are capped between [-1, 1], which might not have an enough dynamic range and limit the categorical probability distributions that the logits can express. For this reason, we scale the logits to have larger differences. To avoid numerical instability, we clipped the scale value at 100, and for all models that we trained, it reached 100. [This comment](https://github.com/openai/CLIP/issues/46#issuecomment-782558799) explains how to clip the value. > > So why don't you directly initialize temperature parameter to 100 ? What's the purpose of let it grow from 1/0.07 to 100? Does it bring enough performance improvement? Yes, I think initialize to 100 will converge faster. But why not set to 100?
Author
Owner

@yakorch commented on GitHub (Sep 8, 2025):

@Horizon2333 @thesby
I am late to the party, but perhaps this is still useful.
In Temperature-Free Loss Function for Contrastive Learning, the authors analyze the properties of the InfoNCE loss, showing that for high temperature values (in the paper notation that I cite), the total loss is non-zero even for correct model predictions (C -> 1).

However, my intuition is that at the start of the training, the model is poor, resulting in very few "C->1" predictions, so the optimization process does not experience this drawback of the chosen loss function. And this comes with a neat bonus of a strong gradient magnitude for non-optimal C, speeding up training.

After some training, hopefully the model is better, and "C->1" predictions are common, and one can no longer ignore the non-zero InfoNCE loss. Yet it is no longer a problem thanks to the decreasing temperature as a learnable parameter.

<!-- gh-comment-id:3265056137 --> @yakorch commented on GitHub (Sep 8, 2025): @Horizon2333 @thesby I am late to the party, but perhaps this is still useful. In [Temperature-Free Loss Function for Contrastive Learning](https://arxiv.org/pdf/2501.17683v1), the authors analyze the properties of the InfoNCE loss, showing that for high temperature values (in the paper notation that I cite), the total loss is non-zero even for correct model predictions (C -> 1). However, my intuition is that at the start of the training, the model is poor, resulting in very few "C->1" predictions, so the optimization process does not experience this drawback of the chosen loss function. And this comes with a neat bonus of a strong gradient magnitude for non-optimal C, speeding up training. After some training, hopefully the model is better, and "C->1" predictions are common, and one can no longer ignore the non-zero InfoNCE loss. Yet it is no longer a problem thanks to the decreasing temperature as a learnable parameter.
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openai/CLIP#43
No description provided.