Compute triplet loss using squared Euclidean distance:
d(x,y)=j=1∑D(xj−yj)2For each anchor, positive, and negative triplet:
Li=max(0,d(ai,pi)−d(ai,ni)+m)Here, D is embedding width and m is margin. Support one triplet with shape (D,) or a batch with shape (N,D). Return the mean loss as a Python float.
Input: anchor = [[1, 0]], positive = [[2, 0]], negative = [[5, 0]], margin = 1.0
Output: 0.0
Explanation: The positive squared distance is 1 and the negative squared distance is 16, so the margin is already satisfied.
Input: anchor = [[0, 0]], positive = [[3, 0]], negative = [[1, 0]], margin = 1.0
Output: 9.0
Input: anchor = [1, 0], positive = [2, 0], negative = [5, 0], margin = 1.0
Output: 0.0
Use np.atleast_2d so one vector and a batch share the same computation.
Sum squared coordinate differences along axis=1.
Apply np.maximum(0.0, positive_distance - negative_distance + margin).
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Compute triplet loss using squared Euclidean distance:
d(x,y)=j=1∑D(xj−yj)2For each anchor, positive, and negative triplet:
Li=max(0,d(ai,pi)−d(ai,ni)+m)Here, D is embedding width and m is margin. Support one triplet with shape (D,) or a batch with shape (N,D). Return the mean loss as a Python float.
Input: anchor = [[1, 0]], positive = [[2, 0]], negative = [[5, 0]], margin = 1.0
Output: 0.0
Explanation: The positive squared distance is 1 and the negative squared distance is 16, so the margin is already satisfied.
Input: anchor = [[0, 0]], positive = [[3, 0]], negative = [[1, 0]], margin = 1.0
Output: 9.0
Input: anchor = [1, 0], positive = [2, 0], negative = [5, 0], margin = 1.0
Output: 0.0
Use np.atleast_2d so one vector and a batch share the same computation.
Sum squared coordinate differences along axis=1.
Apply np.maximum(0.0, positive_distance - negative_distance + margin).
Sign in to take notes on this problem
Accepts: array
Accepts: array
Accepts: array
Accepts: number