Keras 3 API文档 / 损失函数 / "最大间隔"分类的Hinge损失函数

用于“最大间隔”分类的 Hinge 损失函数

[源代码]

Hinge

keras.losses.Hinge(reduction="sum_over_batch_size", name="hinge", dtype=None)

计算y_truey_pred 之间的 Hinge 损失。

公式

loss = maximum(1 - y_true * y_pred, 0)

y_true 值应为 -1 或 1。如果提供二元(0 或 1)标签,我们将将其转换为 -1 或 1。

参数

  • reduction:要应用于损失的归约类型。几乎所有情况下,这都应该是 "sum_over_batch_size"。支持的选项有 "sum""sum_over_batch_size""mean""mean_with_sample_weight"None"sum" 对损失求和,"sum_over_batch_size""mean" 对损失求和并除以样本大小,而 "mean_with_sample_weight" 对损失求和并除以样本权重的总和。"none"None 不执行聚合。默认为 "sum_over_batch_size"
  • name: 损失实例的可选名称。
  • dtype:损失计算的数据类型。默认为 None,这意味着使用 keras.backend.floatx()keras.backend.floatx()"float32",除非将其设置为不同值(通过 keras.backend.set_floatx())。如果提供了 keras.DTypePolicy,则将使用 compute_dtype

[源代码]

SquaredHinge

keras.losses.SquaredHinge(
    reduction="sum_over_batch_size", name="squared_hinge", dtype=None
)

计算y_truey_pred 之间的平方 Hinge 损失。

公式

loss = square(maximum(1 - y_true * y_pred, 0))

y_true 值应为 -1 或 1。如果提供二元(0 或 1)标签,我们将将其转换为 -1 或 1。

参数

  • reduction:要应用于损失的归约类型。几乎所有情况下,这都应该是 "sum_over_batch_size"。支持的选项有 "sum""sum_over_batch_size""mean""mean_with_sample_weight"None"sum" 对损失求和,"sum_over_batch_size""mean" 对损失求和并除以样本大小,而 "mean_with_sample_weight" 对损失求和并除以样本权重的总和。"none"None 不执行聚合。默认为 "sum_over_batch_size"
  • name: 损失实例的可选名称。
  • dtype:损失计算的数据类型。默认为 None,这意味着使用 keras.backend.floatx()keras.backend.floatx()"float32",除非将其设置为不同值(通过 keras.backend.set_floatx())。如果提供了 keras.DTypePolicy,则将使用 compute_dtype

[源代码]

CategoricalHinge

keras.losses.CategoricalHinge(
    reduction="sum_over_batch_size", name="categorical_hinge", dtype=None
)

计算y_truey_pred 之间的分类 Hinge 损失。

公式

loss = maximum(neg - pos + 1, 0)

其中 neg=maximum((1-y_true)*y_pred)pos=sum(y_true*y_pred)

参数

  • reduction:要应用于损失的归约类型。几乎所有情况下,这都应该是 "sum_over_batch_size"。支持的选项有 "sum""sum_over_batch_size""mean""mean_with_sample_weight"None"sum" 对损失求和,"sum_over_batch_size""mean" 对损失求和并除以样本大小,而 "mean_with_sample_weight" 对损失求和并除以样本权重的总和。"none"None 不执行聚合。默认为 "sum_over_batch_size"
  • name: 损失实例的可选名称。
  • dtype:损失计算的数据类型。默认为 None,这意味着使用 keras.backend.floatx()keras.backend.floatx()"float32",除非将其设置为不同值(通过 keras.backend.set_floatx())。如果提供了 keras.DTypePolicy,则将使用 compute_dtype

[源代码]

hinge 函数

keras.losses.hinge(y_true, y_pred)

计算y_truey_pred 之间的 Hinge 损失。

公式

loss = mean(maximum(1 - y_true * y_pred, 0), axis=-1)

参数

  • y_true: 真实标签值。y_true 的值预期为 -1 或 1。如果提供了二元 (0 或 1) 标签,它们将被转换为 -1 或 1,形状为 [batch_size, d0, .. dN]
  • y_pred: 预测值,形状为 [batch_size, d0, .. dN]

返回

Hinge 损失值,形状为 [batch_size, d0, .. dN-1]

示例

>>> 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 函数

keras.losses.squared_hinge(y_true, y_pred)

计算y_truey_pred 之间的平方 Hinge 损失。

公式

loss = mean(square(maximum(1 - y_true * y_pred, 0)), axis=-1)

参数

  • y_true: 真实标签值。y_true 的值预期为 -1 或 1。如果提供了二元 (0 或 1) 标签,我们将把它们转换为 -1 或 1,形状为 [batch_size, d0, .. dN]
  • y_pred: 预测值,形状为 [batch_size, d0, .. dN]

返回

平方 Hinge 损失值,形状为 [batch_size, d0, .. dN-1]

示例

>>> 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 函数

keras.losses.categorical_hinge(y_true, y_pred)

计算y_truey_pred 之间的分类 Hinge 损失。

公式

loss = maximum(neg - pos + 1, 0)

其中 neg=maximum((1-y_true)*y_pred)pos=sum(y_true*y_pred)

参数

  • y_true: 真实标签值。y_true 的值预期为 {-1, +1}{0, 1} (即独热编码张量),形状为 [batch_size, d0, .. dN]
  • y_pred: 预测值,形状为 [batch_size, d0, .. dN]

返回

分类 Hinge 损失值,形状为 [batch_size, d0, .. dN-1]

示例

>>> 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)