I want to calculate the contrastive loss for hyperspectral image classification. I first have to derive the input needed for the contrastive loss based on the output logit[1,num_classes,h,w] of the hyperspectral classification and the superpixel segmentation slic result segments[h,w]. The usual input for contrastive loss is a two-by-two pair of positive samples, with negative samples between pairs. In thi scase, positive samples come from the logit values of 2 randomly selected pixels in each superpixels. If the segments are [1-N] numbers, the logit values are randomly drawn from two of the superpixelsin the segments. Thus, the comparison loss is two inputs, emb_i and emb_j, both of size [N,num_classes], emb_i and emb_j are positive samples in the first dimension, and negative samples between pairs. My question is that: Is there any way to prepare inputs for contrastive loss without loop? Thanks for any help!
I asked chatgpt for the answer, but it always had loop. I am frustrated.