Skip to content
DeepForgeby svx
All problems
EasyML Fundamentalsml-170

Triplet Loss

Problem

Compute the mean triplet loss over pairs of [d_ap, d_an]: max(0, d_ap - d_an + margin).

Empty input returns 0.0.

Hint

The anchor-negative distance must exceed the anchor-positive distance by at least the margin.

Test cases

case 1
in: [[[0.2,0.8],[1,0.5]],0.3]
expected: 0.4
case 2
in: [[[0.1,0.2],[0.5,0.1]],1]
expected: 1.15
case 3
in: [[[0,0]],0.5]
expected: 0.5
case 4
in: [[],0.5]
expected: 0
Starter codePython
def triplet_loss(pairs, margin):
    # Your code here
    pass

4 test cases · run your code against them in the editor