# torchreid.losses

## Softmax

class `torchreid.losses.cross_entropy_loss.``CrossEntropyLoss`(num_classes, epsilon=0.1, use_gpu=True, label_smooth=True)

  Cross entropy loss with label smoothing regularizer.

  Reference:
    Szegedy et al. Rethinking the Inception Architecture for Computer Vision. CVPR 2016.

  With label smoothing, the label \(y\) for a class is computed by

  \[\begin{equation} (1 - \epsilon) \times y + \frac{\epsilon}{K}, \end{equation}\]

  where \(K\) denotes the number of classes and \(\epsilon\) is a weight. When \(\epsilon = 0\), the loss function reduces to the normal cross entropy.

  Parameters:

  - num_classes (int) – number of classes.
  - epsilon (float, optional) – weight. Default is 0.1.
  - use_gpu (bool, optional) – whether to use gpu devices. Default is True.
  - label_smooth (bool, optional) – whether to apply label smoothing. Default is True.

  `forward`(inputs, targets)

    Parameters:

    - inputs (torch.Tensor) – prediction matrix (before softmax) with shape (batch_size, num_classes).
    - targets (torch.LongTensor) – ground truth labels with shape (num_classes).

## Triplet

class `torchreid.losses.hard_mine_triplet_loss.``TripletLoss`(margin=0.3)

  Triplet loss with hard positive/negative mining.

  Reference:
    Hermans et al. In Defense of the Triplet Loss for Person Re-Identification. arXiv:1703.07737.

  Imported from https://github.com/Cysu/open-reid/blob/master/reid/loss/triplet.py.

  Parameters: | margin (float, optional) – margin for triplet. Default is 0.3.

  `forward`(inputs, targets)

    Parameters:

    - inputs (torch.Tensor) – feature matrix with shape (batch_size, feat_dim).
    - targets (torch.LongTensor) – ground truth labels with shape (num_classes).