Join us at TensorFlow World, Oct 28-31. Use code TF20 for 20% off select passes. Register now


View source on GitHub

Adds Ops for computing the multiclass hinge loss.


The implementation is based on the following paper: On the Algorithmic Implementation of Multiclass Kernel-based Vector Machines by Crammer and Singer. link:

This is a generalization of standard (binary) hinge loss. For a given instance with correct label c, the loss is given by:

$$loss = max_{c != c} logitsc - logits{c} + 1.\( </div> or equivalently <div> \)loss = max_c { logitsc - logits{c} + I{c != c*} }$$
where \(I{c != c} = 1\ \text{if}\ c != c\) and 0 otherwise.


  • labels: Tensor of shape [batch_size] or [batch_size, 1]. Corresponds to the ground truth. Each entry must be an index in [0, num_classes).
  • logits: Tensor of shape [batch_size, num_classes] corresponding to the unscaled logits. Its dtype should be either float32 or float64.
  • weights: Optional (python) scalar or Tensor. If a non-scalar Tensor, its rank should be either 1 ([batch_size]) or 2 ([batch_size, 1]).
  • scope: The scope for the operations performed in computing the loss.
  • loss_collection: collection to which the loss will be added.
  • reduction: Type of reduction to apply to loss.


Weighted loss float Tensor. If reduction is NONE, this has the same shape as labels; otherwise, it is a scalar.


  • ValueError: If logits, labels or weights have invalid or inconsistent shapes.
  • ValueError: If labels tensor has invalid dtype.