A Siamese network embeds two inputs and learns that similar pairs are close and dissimilar pairs are at least a margin apart (Hadsell et al.):
where and for similar pairs. Implement contrastive_loss(A, B, y, margin=1.0) and contrastive_grad(A, B, y, margin=1.0) returning the gradients (dA, dB) of the mean loss.