Skip to content

Commit 9764110

Browse files
Merge pull request #650 from stompsjo/ntxentloss-docs
Adding details to NTXentLoss documentation
2 parents 6b4d4d1 + fac0fe4 commit 9764110

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

docs/losses.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,27 @@ This is also known as InfoNCE, and is a generalization of the [NPairsLoss](losse
787787
- [Representation Learning with Contrastive Predictive Coding](https://arxiv.org/pdf/1807.03748.pdf){target=_blank}
788788
- [Momentum Contrast for Unsupervised Visual Representation Learning](https://arxiv.org/pdf/1911.05722.pdf){target=_blank}
789789
- [A Simple Framework for Contrastive Learning of Visual Representations](https://arxiv.org/pdf/2002.05709.pdf){target=_blank}
790+
791+
??? "How exactly is the NTXentLoss computed?"
792+
793+
In the equation below, a loss is computed for each positive pair (`k_+`) in a batch, normalized by all positive and negative pairs in the batch that have the same "anchor" embedding (`k_i in K`).
794+
795+
- What does "anchor" mean? Let's say we have 3 pairs specified by batch indices: (0, 1), (0, 2), (1, 0). The first two pairs start with 0, so they have the same anchor. The third pair has the same indices as the first pair, but the order is different, so it does not have the same anchor.
796+
797+
Given `embeddings` with corresponding `labels`, positive pairs `(embeddings[i], embeddings[j])` are defined when `labels[i] == labels[j]`. Now let's look at an example loss calculation:
798+
799+
Consider `labels = [0, 0, 1, 2]`. Two losses will be computed:
800+
801+
* A positive pair of indices `[0, 1]`, with negative pairs of indices `[0, 2], [0, 3]`.
802+
803+
* A positive pair of indices `[1, 0]`, with negative pairs of indices `[1, 2], [1, 3]`.
804+
805+
Labels `1`, and `2` do not have positive pairs, and therefore the negative pair of indices `[2, 3]` will not be used.
806+
807+
Note that an anchor can belong to multiple positive pairs if its label is present multiple times in `labels`.
808+
809+
Are you trying to use `NTXentLoss` for self-supervised learning? Specifically, do you have two sets of embeddings which are derived from data that are augmented versions of each other? If so, you can skip the step of creating the `labels` array, by wrapping `NTXentLoss` with [`SelfSupervisedLoss`](losses.md#selfsupervisedloss).
810+
790811
```python
791812
losses.NTXentLoss(temperature=0.07, **kwargs)
792813
```

0 commit comments

Comments
 (0)