Question 1: Short answer
mediumAI-checkedTransfer LearningA SimCLR loss builds a cosine-similarity matrix sim for images, each seen in two augmented views
(rows and are the two views of image ):
pos = torch.roll(torch.eye(2 * N), N, 1) # 1 where the column is the other view
neg = torch.ones(2 * N) - torch.eye(2 * N) # 1 everywhere except the diagonal
numerator = torch.exp(sim / T)[pos.bool()]
denominator = torch.sum(torch.exp(sim / T) * neg, dim=1)
loss = torch.mean(-torch.log(numerator / denominator))
With a batch of images, how many terms are summed in each row of denominator?