You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/losses.md
+21Lines changed: 21 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -787,6 +787,27 @@ This is also known as InfoNCE, and is a generalization of the [NPairsLoss](losse
787
787
-[Representation Learning with Contrastive Predictive Coding](https://arxiv.org/pdf/1807.03748.pdf){target=_blank}
788
788
-[Momentum Contrast for Unsupervised Visual Representation Learning](https://arxiv.org/pdf/1911.05722.pdf){target=_blank}
789
789
-[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).
0 commit comments