TF 2.0 is out! Get hands-on practice at TF World, Oct 28-31. Use code TF20 for 20% off select passes. Register now

tfa.losses.contrastive_loss

View source on GitHub

Computes the contrastive loss between y_true and y_pred.

Aliases:

tfa.losses.contrastive_loss(
    y_true,
    y_pred,
    margin=1.0
)

This loss encourages the embedding to be close to each other for the samples of the same label and the embedding to be far apart at least by the margin constant for the samples of different labels.

The euclidean distances y_pred between two embedding matrices a and b with shape [batch_size, hidden_size] can be computed as follows:

# y_pred = \sqrt (\sum_i (a[:, i] - b[:, i])^2)
y_pred = tf.linalg.norm(a - b, axis=1)

See: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf

Args:

  • y_true: 1-D integer Tensor with shape [batch_size] of binary labels indicating positive vs negative pair.
  • y_pred: 1-D float Tensor with shape [batch_size] of distances between two embedding matrices.
  • margin: margin term in the loss definition.

Returns:

  • contrastive_loss: 1-D float Tensor with shape [batch_size].