mlx.nn.losses.smooth_l1_loss

mlx.nn.losses.smooth_l1_loss#

class smooth_l1_loss(predictions: array, targets: array, beta: float = 1.0, reduction: Literal['none', 'mean', 'sum'] = 'mean')#

计算 smooth L1 损失。

smooth L1 损失是 L1 损失的一个变体,当绝对差值小于 beta 时,它用平方差代替绝对差值。

smooth L1 损失的公式为

\[\begin{split}l = \begin{cases} 0.5 (x - y)^2 / \beta, & \text{如果 } |x - y| < \beta \\ |x - y| - 0.5 \beta, & \text{否则} \end{cases}\end{split}\]
参数:
  • predictions (array) – 预测值。

  • targets (array) – 真实值。

  • beta (float, 可选) – 损失从平方差变为绝对差的阈值。默认值: 1.0

  • reduction (str, 可选) – 指定应用于输出的归约方式: 'none' | 'mean' | 'sum'。默认值: 'mean'

返回:

计算出的 smooth L1 损失。

返回类型:

array