mirror of
https://github.com/ml-explore/mlx.git
synced 2025-09-01 04:24:36 +08:00
Style fix for loss functions (#91)
* MLE and L1 loss functions * logsoftmax change and tests * subtract max logit for numerical stability * l1 name change * cross entropy reduction + unit tests * docstrings * l1 test name change * old loss impl + default none * style
This commit is contained in:
@@ -10,6 +10,7 @@ import mlx_tests
|
||||
import numpy as np
|
||||
from mlx.utils import tree_flatten, tree_map, tree_unflatten
|
||||
|
||||
|
||||
class TestNN(mlx_tests.MLXTestCase):
|
||||
def test_linear(self):
|
||||
inputs = mx.zeros((10, 4))
|
||||
@@ -22,17 +23,17 @@ class TestNN(mlx_tests.MLXTestCase):
|
||||
targets = mx.array([0, 1])
|
||||
|
||||
# Test with reduction 'none'
|
||||
losses_none = nn.losses.cross_entropy(logits, targets, reduction='none')
|
||||
losses_none = nn.losses.cross_entropy(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.cross_entropy(logits, targets, reduction='mean')
|
||||
losses_mean = nn.losses.cross_entropy(logits, targets, reduction="mean")
|
||||
expected_mean = mx.mean(expected_none)
|
||||
self.assertEqual(losses_mean, expected_mean)
|
||||
|
||||
# Test with reduction 'sum'
|
||||
losses_sum = nn.losses.cross_entropy(logits, targets, reduction='sum')
|
||||
losses_sum = nn.losses.cross_entropy(logits, targets, reduction="sum")
|
||||
expected_sum = mx.sum(expected_none)
|
||||
self.assertEqual(losses_sum, expected_sum)
|
||||
|
||||
|
Reference in New Issue
Block a user