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=1NN∑n=1K∑k=1[max(0,1−δ{tn=k}xnk)]pwhere N denotes the batch size and K is the number of classes of interest,
δ{condition}={1if condition is true−1otherwise,and
p={1if norm=L12if norm=L2.Let the hinge loss function l(x,δ) be [max(0,1−δx)]p. When x and δ have the same sign (meaning x predicts the proper score for classification) and |x|≥1, the hinge loss l(x,δ)=0, but when they have opposite sign, l(x,δ) increases linearly with x.
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
or N-dimensional array) – Input variable. The shape ofx
should be (N, K).t (
Variable
or N-dimensional array) – The N-dimensional label vector with values tn∈{0,1,2,…,K−1}. The shape oft
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
Example
In this case, the batch size
N
is 2 and the number of classesK
is 3.>>> x = np.array([[-2.0, 3.0, 0.5], ... [5.0, 2.0, -0.5]]).astype(np.float32) >>> x array([[-2. , 3. , 0.5], [ 5. , 2. , -0.5]], dtype=float32) >>> t = np.array([1, 0]).astype(np.int32) >>> t array([1, 0], dtype=int32) >>> F.hinge(x, t) variable(2.5) >>> F.hinge(x, t, reduce='no') variable([[0. , 0. , 1.5], [0. , 3. , 0.5]]) >>> F.hinge(x, t, norm='L2') variable(5.75)