Hinge losses for "maximum-margin" classification
- 원본 링크 : https://keras.io/api/losses/hinge_losses/
- 최종 확인 : 2024-11-25
Hinge
class
keras.losses.Hinge(reduction="sum_over_batch_size", name="hinge", dtype=None)
Computes the hinge loss between y_true
& y_pred
.
Formula:
loss = maximum(1 - y_true * y_pred, 0)
y_true
values are expected to be -1 or 1. If binary (0 or 1) labels are
provided we will convert them to -1 or 1.
Arguments
- reduction: Type of reduction to apply to the loss. In almost all cases
this should be
"sum_over_batch_size"
. Supported options are"sum"
,"sum_over_batch_size"
orNone
. - name: Optional name for the loss instance.
- dtype: The dtype of the loss’s computations. Defaults to
None
, which means usingkeras.backend.floatx()
.keras.backend.floatx()
is a"float32"
unless set to different value (viakeras.backend.set_floatx()
). If akeras.DTypePolicy
is provided, then thecompute_dtype
will be utilized.
SquaredHinge
class
keras.losses.SquaredHinge(
reduction="sum_over_batch_size", name="squared_hinge", dtype=None
)
Computes the squared hinge loss between y_true
& y_pred
.
Formula:
loss = square(maximum(1 - y_true * y_pred, 0))
y_true
values are expected to be -1 or 1. If binary (0 or 1) labels are
provided we will convert them to -1 or 1.
Arguments
- reduction: Type of reduction to apply to the loss. In almost all cases
this should be
"sum_over_batch_size"
. Supported options are"sum"
,"sum_over_batch_size"
orNone
. - name: Optional name for the loss instance.
- dtype: The dtype of the loss’s computations. Defaults to
None
, which means usingkeras.backend.floatx()
.keras.backend.floatx()
is a"float32"
unless set to different value (viakeras.backend.set_floatx()
). If akeras.DTypePolicy
is provided, then thecompute_dtype
will be utilized.
CategoricalHinge
class
keras.losses.CategoricalHinge(
reduction="sum_over_batch_size", name="categorical_hinge", dtype=None
)
Computes the categorical hinge loss between y_true
& y_pred
.
Formula:
loss = maximum(neg - pos + 1, 0)
where neg=maximum((1-y_true)*y_pred)
and pos=sum(y_true*y_pred)
Arguments
- reduction: Type of reduction to apply to the loss. In almost all cases
this should be
"sum_over_batch_size"
. Supported options are"sum"
,"sum_over_batch_size"
orNone
. - name: Optional name for the loss instance.
- dtype: The dtype of the loss’s computations. Defaults to
None
, which means usingkeras.backend.floatx()
.keras.backend.floatx()
is a"float32"
unless set to different value (viakeras.backend.set_floatx()
). If akeras.DTypePolicy
is provided, then thecompute_dtype
will be utilized.
hinge
function
keras.losses.hinge(y_true, y_pred)
Computes the hinge loss between y_true
& y_pred
.
Formula:
loss = mean(maximum(1 - y_true * y_pred, 0), axis=-1)
Arguments
- y_true: The ground truth values.
y_true
values are expected to be -1 or 1. If binary (0 or 1) labels are provided they will be converted to -1 or 1 with shape =[batch_size, d0, .. dN]
. - y_pred: The predicted values with shape =
[batch_size, d0, .. dN]
.
Returns
Hinge loss values with shape = [batch_size, d0, .. dN-1]
.
Example
>>> y_true = np.random.choice([-1, 1], size=(2, 3))
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = keras.losses.hinge(y_true, y_pred)
squared_hinge
function
keras.losses.squared_hinge(y_true, y_pred)
Computes the squared hinge loss between y_true
& y_pred
.
Formula:
loss = mean(square(maximum(1 - y_true * y_pred, 0)), axis=-1)
Arguments
- y_true: The ground truth values.
y_true
values are expected to be -1 or 1. If binary (0 or 1) labels are provided we will convert them to -1 or 1 with shape =[batch_size, d0, .. dN]
. - y_pred: The predicted values with shape =
[batch_size, d0, .. dN]
.
Returns
Squared hinge loss values with shape = [batch_size, d0, .. dN-1]
.
Example
>>> y_true = np.random.choice([-1, 1], size=(2, 3))
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = keras.losses.squared_hinge(y_true, y_pred)
categorical_hinge
function
keras.losses.categorical_hinge(y_true, y_pred)
Computes the categorical hinge loss between y_true
& y_pred
.
Formula:
loss = maximum(neg - pos + 1, 0)
where neg=maximum((1-y_true)*y_pred)
and pos=sum(y_true*y_pred)
Arguments
- y_true: The ground truth values.
y_true
values are expected to be either{-1, +1}
or{0, 1}
(i.e. a one-hot-encoded tensor) with shape =[batch_size, d0, .. dN]
. - y_pred: The predicted values with shape =
[batch_size, d0, .. dN]
.
Returns
Categorical hinge loss values with shape = [batch_size, d0, .. dN-1]
.
Example
>>> y_true = np.random.randint(0, 3, size=(2,))
>>> y_true = np.eye(np.max(y_true) + 1)[y_true]
>>> y_pred = np.random.random(size=(2, 3))
>>> loss = keras.losses.categorical_hinge(y_true, y_pred)