### Siamese Neural Network

**Learning Goal**: Force the distance between Anchor and Positive to be small and that between Anchor and Negative to be large: \(d(A, P) + \alpha \leq d(A, N)\).

**Triplet Loss Function**:

where \(A, P, N\) refer to Anchor, Positive, Negative; \(\alpha\) is the margin that defines the threshold distance. Overall loss is \(L = \sum_{i=1}^m L_i\).

**Problem**: If triplets are randomly chosen, the inequality is easily satisfied.

**Solution**: Change the objective to

where \(d_i\) is the normalized distance for \(i\)th sample, \(y_i=1\) indicates \(P\), \(y_i=0\) indicates \(N\). The margin \(\alpha\) here will force \(d_i\) to be more negative in order to minimize the overall loss.