Hinge loss in PyTorch

I was wondering if there is an equivalent for tf.compat.v1.losses.hinge_loss in PyTorch? Is torch.nn.HingeEmbeddingLoss the equivalent function? Thanks!

I implemented the Hinge Loss function from the definition as below:

class HingeLoss(torch.nn.Module):

    def __init__(self):
        super(HingeLoss, self).__init__()
        self.relu = nn.ReLU()

    def forward(self, output, target):
        all_ones = torch.ones_like(target)
        labels = 2 * target - all_ones
        losses = all_ones - torch.mul(output.squeeze(1), labels)

        return torch.norm(self.relu(losses))

However, this gives a different value than torch.nn.HingeEmbeddingLoss, as I have explained in this example.

1 Like