# tf.contrib.losses.metric_learning.triplet_semihard_loss

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/losses/python/metric_learning/metric_loss_ops.py#L160-L239) |

Computes the triplet loss with semi-hard negative mining.

    tf.contrib.losses.metric_learning.triplet_semihard_loss(
        labels, embeddings, margin=1.0
    )

The loss encourages the positive distances (between a pair of embeddings with
the same labels) to be smaller than the minimum negative distance among
which are at least greater than the positive distance plus the margin constant
(called semi-hard negative) in the mini-batch. If no such negative exists,
uses the largest negative distance instead.
See: <https://arxiv.org/abs/1503.03832>

<br />

<br />

<br />

| ## Args ||
|---|---|
| `labels` | 1-D tf.int32 `Tensor` with shape \[batch_size\] of multiclass integer labels. |
| `embeddings` | 2-D float `Tensor` of embedding vectors. Embeddings should be l2 normalized. |
| `margin` | Float, margin term in the loss definition. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| `triplet_loss` | tf.float32 scalar. |

<br />