MCPcopy Create free account
hub / github.com/MotrixLab/MotionDiffuse / ContrastiveLoss

Class ContrastiveLoss

text2motion/datasets/evaluator_models.py:11–24  ·  view source on GitHub ↗

Contrastive loss function. Based on: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf

Source from the content-addressed store, hash-verified

9
10
11class ContrastiveLoss(torch.nn.Module):
12 """
13 Contrastive loss function.
14 Based on: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
15 """
16 def __init__(self, margin=3.0):
17 super(ContrastiveLoss, self).__init__()
18 self.margin = margin
19
20 def forward(self, output1, output2, label):
21 euclidean_distance = F.pairwise_distance(output1, output2, keepdim=True)
22 loss_contrastive = torch.mean((1-label) * torch.pow(euclidean_distance, 2) +
23 (label) * torch.pow(torch.clamp(self.margin - euclidean_distance, min=0.0), 2))
24 return loss_contrastive
25
26
27def init_weight(m):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected