Learned Temperature Collapse in Contrastive Loss
In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity. This causes `CrossEntropyLoss` (which computes exp) to produce infinities, yielding NaNs in the backward pass.
In contrastive learning (like CLIP), logits are scaled by `1 / temperature`.
- Root cause
- In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity.
- Recommended fix
- Clamp the Temperature Parameter self.temperature.data = torch.clamp(self.temperature.data, min=1e-4, max=100.0) Clamping the temperature strictly prevents it from approaching zero, ensuring logits remain safely bounded.
- How Denpex helps
- Denpex investigates Learned Temperature Collapse in Contrastive Loss using the evidence you provide or your connected workload collects. Earlier rank, host or application evidence is needed to distinguish an initiating failure from a downstream report.
What this failure is
Learned Temperature Collapse in Contrastive Loss is a Model failure seen during ML training runs. In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity. This causes `CrossEntropyLoss` (which computes exp) to produce infinities, yielding NaNs in the backward pass. Common tags: Loss Divergence.
Is this what broke your run? Paste your log.
You're reading about Learned Temperature Collapse in Contrastive Loss. Paste your own traceback and relevant evidence for an investigation of your workload, with a next action or a specific missing fact. A reference entry does not establish your cause. No account or card for the free diagnosis. Review data handling before submitting sensitive logs.
Before uploading, review cloud data handling and local options.
Upload your log file. Large files are processed in your browser. Only selected evidence is submitted when you run diagnosis.
Why it happens (the mechanism)
The loss seems to be improving wonderfully right before it blows up, tricking researchers into thinking the model was learning exceptionally well before a 'random' numerical glitch.
What you'll observe
- Loss drops suddenly, then goes to NaN
- Gradient norm explodes
Common symptoms and what they mean
| Symptom | Why it happens |
|---|---|
| The contrastive loss initially decreases rapidly but suddenly explodes to NaN. | In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity. This causes `CrossEntropyLoss` (which computes exp) to produce infinities, yielding NaNs in the backward pass. |
| The learned temperature parameter converges towards zero. | In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity. This causes `CrossEntropyLoss` (which computes exp) to produce infinities, yielding NaNs in the backward pass. |
Which systems are affected
- PyTorch
- CLIP
- Contrastive Learning
How to confirm this is the problem
Use this checklist to test the hypothesis against a small reproduction. No single line proves the root cause, so preserve the preceding events and compare one variable at a time.
- ✓Track the value of the temperature parameter on Weights & Biases or TensorBoard.
- ✓Log the max logit value prior to CrossEntropyLoss.
Root cause
- In contrastive learning (like CLIP), logits are scaled by `1 / temperature`. If the temperature parameter is learned and not constrained, the optimizer may push it towards zero to artificially increase confidence and lower the loss. As temperature approaches 0, logits approach infinity. This causes `CrossEntropyLoss` (which computes exp) to produce infinities, yielding NaNs in the backward pass.
The fix and how to prevent it
The fix and the prevention pattern
The root cause is on this page and stays free. A free account adds the exact remediation steps, saved history, and the fix on every entry in the encyclopedia.
Sign up free. Unlock the full analysisNo credit card. Daily allowance follows verified trust tier. Instant access.
Diagnose this failure in VS Code
Select the traceback or open the failed terminal, then run Denpex locally to see the initiating rank, collateral failures, exact fix, and verification command without uploading the log.
Install the free VS Code extensionReferences
Don't just read the fix, diagnose your run
The encyclopedia tells you what went wrong. Denpex tells you what went wrong in YOUR training run. With your logs, your config, and your stack.