File size: 383 Bytes
c4bc1f2
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
import torch


class ContrastiveLoss(torch.nn.Module):
    def __init__(self, margin=1.0):
        super(ContrastiveLoss, self).__init__()
        self.margin = margin

    def forward(self, output1, output2):  # noqa
        euclidean_distance = torch.nn.functional.pairwise_distance(output1, output2)
        loss = torch.mean(torch.pow(euclidean_distance, 2))
        return loss