Warning: This project is deprecated. TensorFlow Addons has stopped development,
The project will only be providing minimal maintenance releases until May 2024. See the full
announcement here or on
github.
tfa.losses.contrastive_loss
Stay organized with collections
Save and categorize content based on your preferences.
Computes the contrastive loss between y_true
and y_pred
.
@tf.function
tfa.losses.contrastive_loss(
y_true: tfa.types.TensorLike
,
y_pred: tfa.types.TensorLike
,
margin: tfa.types.Number
= 1.0
) -> tf.Tensor
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:
a = tf.constant([[1, 2],
[3, 4],
[5, 6]], dtype=tf.float16)
b = tf.constant([[5, 9],
[3, 6],
[1, 8]], dtype=tf.float16)
y_pred = tf.linalg.norm(a - b, axis=1)
y_pred
<tf.Tensor: shape=(3,), dtype=float16, numpy=array([8.06 , 2. , 4.473],
dtype=float16)>
<... Note: constants a & b have been used purely for
example purposes and have no significant value ...>
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] .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-05-25 UTC.
[null,null,["Last updated 2023-05-25 UTC."],[],[],null,["# tfa.losses.contrastive_loss\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/addons/blob/v0.20.0/tensorflow_addons/losses/contrastive.py#L24-L69) |\n\nComputes the contrastive loss between `y_true` and `y_pred`. \n\n @tf.function\n tfa.losses.contrastive_loss(\n y_true: ../../tfa/types/TensorLike,\n y_pred: ../../tfa/types/TensorLike,\n margin: ../../tfa/types/Number = 1.0\n ) -\u003e tf.Tensor\n\nThis loss encourages the embedding to be close to each other for\nthe samples of the same label and the embedding to be far apart at least\nby the margin constant for the samples of different labels.\n\nThe euclidean distances `y_pred` between two embedding matrices\n`a` and `b` with shape `[batch_size, hidden_size]` can be computed\nas follows: \n\n a = tf.constant([[1, 2],\n [3, 4],\n [5, 6]], dtype=tf.float16)\n b = tf.constant([[5, 9],\n [3, 6],\n [1, 8]], dtype=tf.float16)\n y_pred = tf.linalg.norm(a - b, axis=1)\n y_pred\n \u003ctf.Tensor: shape=(3,), dtype=float16, numpy=array([8.06 , 2. , 4.473],\n dtype=float16)\u003e\n\n\\\u003c... Note: constants a \\& b have been used purely for\nexample purposes and have no significant value ...\\\u003e\n\nSee: \u003chttp://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|-------------------------------------------------------------------------------------------------------|\n| `y_true` | 1-D integer `Tensor` with shape `[batch_size]` of binary labels indicating positive vs negative pair. |\n| `y_pred` | 1-D float `Tensor` with shape `[batch_size]` of distances between two embedding matrices. |\n| `margin` | margin term in the loss definition. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|--------------------|-----------------------------------------------|\n| `contrastive_loss` | 1-D float `Tensor` with shape `[batch_size]`. |\n\n\u003cbr /\u003e"]]