SIMCLRLOSS#

Author: Yonglong Tian (yonglong@mit.edu) Date: May 07, 2020 Source: https://github.com/HobbitLong/SupContrast/blob/master/losses.py

Classes#

class utils.simclrloss.SupConLoss(temperature=0.07, contrast_mode='all', base_temperature=0.07, reduction='mean')[source]#

Bases: Module

Supervised Contrastive Learning: https://arxiv.org/pdf/2004.11362.pdf. It also supports the unsupervised contrastive loss in SimCLR

forward(features, labels=None, mask=None)[source]#

Compute loss for model. If both labels and mask are None, it degenerates to SimCLR unsupervised loss: https://arxiv.org/pdf/2002.05709.pdf .

Parameters:
  • features – hidden vector of shape [bsz, n_views, …].

  • labels – ground truth of shape [bsz].

  • mask – contrastive mask of shape [bsz, bsz], mask_{i,j}=1 if sample j has the same class as sample i. Can be asymmetric.

Returns:

A loss scalar.