Implement triplet margin loss for metric learning. Given anchor, positive, and negative embeddings, compute the loss that encourages the anchor to be closer to the positive than the negative by at least a specified margin.
import numpy as np
def triplet_margin_loss(anchor: np.ndarray, positive: np.ndarray, negative: np.ndarray, margin: float = 1.0) -> float:
# anchor, positive, negative: shape (batch_size, embedding_dim)
dist_pos = np.sqrt(np.sum((anchor - positive) ** 2, axis=1))
dist_neg = np.sqrt(np.sum((anchor - negative) ** 2, axis=1))
losses = np.maximum(dist_pos - dist_neg + margin, 0.0)
return float(np.mean(losses))max(d_pos - d_neg + margin, 0). This is zero when the negative is already farther than the positive by at least the margin.