backprop.utils.losses

backprop.utils.losses.triplet_loss

class TripletLoss(device)[source]

Bases: torch.nn.modules.module.Module

forward(input, target, **kwargs)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
TripletSemiHardLoss(y_true, y_pred, device, margin=1.0)[source]

Computes the triplet loss_functions with semi-hard negative mining. The loss_functions 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. We expect labels y_true to be provided as 1-D integer Tensor with shape [batch_size] of multi-class integer labels. And embeddings y_pred must be 2-D float Tensor of l2 normalized embedding vectors. :param margin: Float, margin term in the loss_functions definition. Default value is 1.0. :param name: Optional name for the op.

pairwise_distance_torch(embeddings, device)[source]

Computes the pairwise distance matrix with numerical stability. output[i, j] = || feature[i, :] - feature[j, :] ||_2 :param embeddings: 2-D Tensor of size [number of data, feature dimension].

Returns

2-D Tensor of size [number of data, number of data].

Return type

pairwise_distances