From 95e335db7b92fb3302e0456842975619dc8ccd9d Mon Sep 17 00:00:00 2001 From: jiyzhang Date: Thu, 20 Mar 2025 11:19:02 +0800 Subject: [PATCH] Update smooth_l1_loss in losses.py (#1974) According the definition of smooth_l1_loss, the line diff = predictions - targets Should be updated to diff = mx.abs(predictions - targets) After the modification, the result is consistent with PyTorch smooth_l1_loss --- python/mlx/nn/losses.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/mlx/nn/losses.py b/python/mlx/nn/losses.py index bccf45b16..58232363a 100644 --- a/python/mlx/nn/losses.py +++ b/python/mlx/nn/losses.py @@ -373,7 +373,7 @@ def smooth_l1_loss( f"targets shape {targets.shape}." ) - diff = predictions - targets + diff = mx.abs(predictions - targets) loss = mx.where( diff < beta, 0.5 * mx.square(diff) / beta, mx.abs(diff) - 0.5 * beta )