mirror of
https://github.com/ml-explore/mlx.git
synced 2025-10-18 15:28:16 +08:00
added mse_loss, nll_loss and kl_div_loss (#98)
* added mse_loss, nll_loss and kl_div_loss * fixed axis not defined error in nll_loss * fixed axis not defined in kl_div_loss * added tests for mse, nll and kl_div * modified docstrings and added reduce helper func * updated docstring in kl_div_loss and moved helper func * added new kl divergence implementation * added reduction to test * updated docstring of kl_div_loss with correct spelling * added losses to nn.rst in docs
This commit is contained in:
@@ -40,8 +40,67 @@ class TestNN(mlx_tests.MLXTestCase):
|
||||
def test_l1_loss(self):
|
||||
predictions = mx.array([0.5, 0.2, 0.9, 0.0])
|
||||
targets = mx.array([0.5, 0.2, 0.9, 0.0])
|
||||
losses = nn.losses.l1_loss(predictions, targets)
|
||||
losses = nn.losses.l1_loss(predictions, targets, reduction="none")
|
||||
self.assertEqual(losses, 0.0)
|
||||
|
||||
def test_mse_loss(self):
|
||||
predictions = mx.array([0.5, 0.2, 0.9, 0.0])
|
||||
targets = mx.array([0.7, 0.1, 0.8, 0.2])
|
||||
|
||||
# Test with reduction 'none'
|
||||
losses_none = nn.losses.mse_loss(predictions, targets, reduction="none")
|
||||
expected_none = mx.array([0.04, 0.01, 0.01, 0.04])
|
||||
self.assertTrue(mx.array_equal(losses_none, expected_none))
|
||||
|
||||
# Test with reduction 'mean'
|
||||
losses_mean = nn.losses.mse_loss(predictions, targets, reduction="mean")
|
||||
expected_mean = mx.mean(expected_none)
|
||||
self.assertEqual(losses_mean, expected_mean)
|
||||
|
||||
# Test with reduction 'sum'
|
||||
losses_sum = nn.losses.mse_loss(predictions, targets, reduction="sum")
|
||||
expected_sum = mx.sum(expected_none)
|
||||
self.assertEqual(losses_sum, expected_sum)
|
||||
|
||||
|
||||
def test_nll_loss(self):
|
||||
logits = mx.array([[0.0, -float("inf")], [-float("inf"), 0.0]])
|
||||
targets = mx.array([0, 1])
|
||||
|
||||
# Test with reduction 'none'
|
||||
losses_none = nn.losses.nll_loss(logits, targets, reduction="none")
|
||||
expected_none = mx.array([0.0, 0.0])
|
||||
self.assertTrue(mx.array_equal(losses_none, expected_none))
|
||||
|
||||
# Test with reduction 'mean'
|
||||
losses_mean = nn.losses.nll_loss(logits, targets, reduction="mean")
|
||||
expected_mean = mx.mean(expected_none)
|
||||
self.assertEqual(losses_mean, expected_mean)
|
||||
|
||||
# Test with reduction 'sum'
|
||||
losses_sum = nn.losses.nll_loss(logits, targets, reduction="sum")
|
||||
expected_sum = mx.sum(expected_none)
|
||||
self.assertEqual(losses_sum, expected_sum)
|
||||
|
||||
|
||||
def test_kl_div_loss(self):
|
||||
p_logits = mx.array([[1.0, 2.0], [0.5, 1.0]])
|
||||
q_logits = mx.array([[0.8, 1.5], [0.4, 1.2]])
|
||||
|
||||
# Test with reduction 'none'
|
||||
losses_none = nn.losses.kl_div_loss(p_logits, q_logits, reduction="none")
|
||||
expected_none = mx.array([0.22314353, 0.09966799])
|
||||
self.assertTrue(mx.array_equal(losses_none, expected_none))
|
||||
|
||||
# Test with reduction 'mean'
|
||||
losses_mean = nn.losses.kl_div_loss(p_logits, q_logits, reduction="mean")
|
||||
expected_mean = mx.mean(expected_none)
|
||||
self.assertEqual(losses_mean, expected_mean)
|
||||
|
||||
# Test with reduction 'sum'
|
||||
losses_sum = nn.losses.kl_div_loss(p_logits, q_logits, reduction="sum")
|
||||
expected_sum = mx.sum(expected_none)
|
||||
self.assertEqual(losses_sum, expected_sum)
|
||||
|
||||
def test_gelu(self):
|
||||
inputs = [1.15286231, -0.81037411, 0.35816911, 0.77484438, 0.66276414]
|
||||
|
Reference in New Issue
Block a user