chainer.functions.hinge

chainer.functions.hinge(x, t, norm='L1', reduce='mean')[source]

Computes the hinge loss for a one-of-many classification task.

\[L = \frac{1}{N} \sum_{n=1}^N \sum_{k=1}^K \left[ \max(0, 1 - \delta\{t_n = k\} x_{nk}) \right]^p\]

where \(N\) denotes the batch size and \(K\) is the number of classes of interest,

\[\begin{split}\delta \{ {\rm condition} \} = \left \{ \begin{array}{cc} 1 & {\rm if~condition\ is\ true} \\ -1 & {\rm otherwise,} \end{array} \right.\end{split}\]

and

\[\begin{split}p = \left \{ \begin{array}{cc} 1 & {\rm if~norm} = {\rm L1} \\ 2 & {\rm if~norm} = {\rm L2.} \end{array} \right.\end{split}\]

The output is a variable whose value depends on the value of the option reduce. If it is 'no', it holds the elementwise loss values. If it is 'mean', it takes the mean of loss values.

Parameters:
  • x (Variable) – Input variable. The shape of x should be (\(N\), \(K\)).
  • t (Variable) – The \(N\)-dimensional label vector with values \(t_n \in \{0, 1, 2, \dots, K-1\}\). The shape of t should be (\(N\),).
  • norm (string) – Specifies norm type. Either 'L1' or 'L2' is acceptable.
  • reduce (str) – Reduction option. Its value must be either 'mean' or 'no'. Otherwise, ValueError is raised.
Returns:

A variable object holding a scalar array of the hinge loss \(L\). If reduce is 'no', the output variable holds array whose shape is same as one of (hence both of) input variables. If it is 'mean', the output variable holds a scalar value.

Return type:

Variable