[GH-ISSUE #146] Why the results of cosine similarities between the corresponding image and text features may appear negative numbers and NaN? #128

Closed
opened 2026-10-31 21:11:29 +00:00 by bsakaguchi · 1 comment
Owner

Originally created by @lr19960813 on GitHub (Aug 30, 2021).
Original GitHub issue: https://github.com/openai/CLIP/issues/146

I input a image and some texts in one list, then use model.(image,text) to calculate their values. Follow the instructions, the values are cosine similarities between the corresponding image and text features, times 100.

They should all be positive numbers, but when I fine tuned clip and use the fine tuned clip model for calculations, some results are negative numbers and even all results are NaN. I don‘t know why this happened?

Originally created by @lr19960813 on GitHub (Aug 30, 2021). Original GitHub issue: https://github.com/openai/CLIP/issues/146 I input a image and some texts in one list, then use model.(image,text) to calculate their values. Follow the instructions, the values are cosine similarities between the corresponding image and text features, times 100. They should all be positive numbers, but when I fine tuned clip and use the fine tuned clip model for calculations, some results are negative numbers and even all results are NaN. I don‘t know why this happened?
Author
Owner

@jongwook commented on GitHub (Sep 24, 2021):

While we usually observe positive cosine similarities for typical text and image inputs, they can have negative values, because it's a cosine function.

See #40, #63, #81, and #144 for tips for avoiding NaNs during fine-tuning. In short, converting everything to FP32 will mostly solve the problem.

<!-- gh-comment-id:926325937 --> @jongwook commented on GitHub (Sep 24, 2021): While we usually observe positive cosine similarities for typical text and image inputs, they can have negative values, because it's a cosine function. See #40, #63, #81, and #144 for tips for avoiding `NaN`s during fine-tuning. In short, converting everything to FP32 will mostly solve the problem.
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#128
No description provided.