mirror of
https://github.com/ml-explore/mlx.git
synced 2025-06-25 01:41:17 +08:00
parent
ef7b8756c0
commit
600db7d754
@ -18,7 +18,7 @@ struct Remainder {
|
|||||||
template <typename T> T operator()(T x, T y) { return x % y; }
|
template <typename T> T operator()(T x, T y) { return x % y; }
|
||||||
template <> float operator()(float x, float y) { return fmod(x, y); }
|
template <> float operator()(float x, float y) { return fmod(x, y); }
|
||||||
template <> half operator()(half x, half y) { return fmod(x, y); }
|
template <> half operator()(half x, half y) { return fmod(x, y); }
|
||||||
template <> bfloat operator()(bfloat x, bfloat y) { return fmod(x, y); }
|
template <> bfloat16_t operator()(bfloat16_t x, bfloat16_t y) { return fmod(x, y); }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Equal {
|
struct Equal {
|
||||||
|
@ -112,5 +112,7 @@ constexpr complex64_t operator*(complex64_t a, complex64_t b) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
constexpr complex64_t operator%(complex64_t a, complex64_t b) {
|
constexpr complex64_t operator%(complex64_t a, complex64_t b) {
|
||||||
return {fmod(a.real, b.real), fmod(a.imag, b.imag)};
|
auto real = a.real - (b.real * static_cast<int64_t>(a.real / b.real));
|
||||||
|
auto imag = a.imag - (b.imag * static_cast<int64_t>(a.imag / b.imag));
|
||||||
|
return {real, imag};
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import mlx.core as mx
|
import mlx.core as mx
|
||||||
|
|
||||||
|
|
||||||
def cross_entropy(
|
def cross_entropy(
|
||||||
logits: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none"
|
logits: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none"
|
||||||
) -> mx.array:
|
) -> mx.array:
|
||||||
@ -24,7 +25,9 @@ def cross_entropy(
|
|||||||
return _reduce(loss, reduction)
|
return _reduce(loss, reduction)
|
||||||
|
|
||||||
|
|
||||||
def l1_loss(predictions: mx.array, targets: mx.array, reduction: str = "none") -> mx.array:
|
def l1_loss(
|
||||||
|
predictions: mx.array, targets: mx.array, reduction: str = "none"
|
||||||
|
) -> mx.array:
|
||||||
"""
|
"""
|
||||||
Computes the L1 loss between predictions and targets.
|
Computes the L1 loss between predictions and targets.
|
||||||
|
|
||||||
@ -42,7 +45,9 @@ def l1_loss(predictions: mx.array, targets: mx.array, reduction: str = "none") -
|
|||||||
return _reduce(loss, reduction)
|
return _reduce(loss, reduction)
|
||||||
|
|
||||||
|
|
||||||
def mse_loss(predictions: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none") -> mx.array:
|
def mse_loss(
|
||||||
|
predictions: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none"
|
||||||
|
) -> mx.array:
|
||||||
"""
|
"""
|
||||||
Computes the mean squared error loss between predictions and targets.
|
Computes the mean squared error loss between predictions and targets.
|
||||||
|
|
||||||
@ -61,7 +66,9 @@ def mse_loss(predictions: mx.array, targets: mx.array, axis: int = -1, reduction
|
|||||||
return _reduce(loss, reduction)
|
return _reduce(loss, reduction)
|
||||||
|
|
||||||
|
|
||||||
def nll_loss(logits: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none") -> mx.array:
|
def nll_loss(
|
||||||
|
logits: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none"
|
||||||
|
) -> mx.array:
|
||||||
"""
|
"""
|
||||||
Computes the negative log likelihood loss between logits and targets.
|
Computes the negative log likelihood loss between logits and targets.
|
||||||
|
|
||||||
@ -80,7 +87,9 @@ def nll_loss(logits: mx.array, targets: mx.array, axis: int = -1, reduction: str
|
|||||||
return _reduce(loss, reduction)
|
return _reduce(loss, reduction)
|
||||||
|
|
||||||
|
|
||||||
def kl_div_loss(logits: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none") -> mx.array:
|
def kl_div_loss(
|
||||||
|
logits: mx.array, targets: mx.array, axis: int = -1, reduction: str = "none"
|
||||||
|
) -> mx.array:
|
||||||
"""
|
"""
|
||||||
Computes the Kullback-Leibler divergence loss between logits and targets.
|
Computes the Kullback-Leibler divergence loss between logits and targets.
|
||||||
|
|
||||||
@ -98,7 +107,8 @@ def kl_div_loss(logits: mx.array, targets: mx.array, axis: int = -1, reduction:
|
|||||||
|
|
||||||
return _reduce(loss, reduction)
|
return _reduce(loss, reduction)
|
||||||
|
|
||||||
def _reduce(loss: mx.array, reduction: str = 'none'):
|
|
||||||
|
def _reduce(loss: mx.array, reduction: str = "none"):
|
||||||
if reduction == "mean":
|
if reduction == "mean":
|
||||||
return mx.mean(loss)
|
return mx.mean(loss)
|
||||||
elif reduction == "sum":
|
elif reduction == "sum":
|
||||||
|
@ -62,7 +62,6 @@ class TestNN(mlx_tests.MLXTestCase):
|
|||||||
expected_sum = mx.sum(expected_none)
|
expected_sum = mx.sum(expected_none)
|
||||||
self.assertEqual(losses_sum, expected_sum)
|
self.assertEqual(losses_sum, expected_sum)
|
||||||
|
|
||||||
|
|
||||||
def test_nll_loss(self):
|
def test_nll_loss(self):
|
||||||
logits = mx.array([[0.0, -float("inf")], [-float("inf"), 0.0]])
|
logits = mx.array([[0.0, -float("inf")], [-float("inf"), 0.0]])
|
||||||
targets = mx.array([0, 1])
|
targets = mx.array([0, 1])
|
||||||
@ -82,7 +81,6 @@ class TestNN(mlx_tests.MLXTestCase):
|
|||||||
expected_sum = mx.sum(expected_none)
|
expected_sum = mx.sum(expected_none)
|
||||||
self.assertEqual(losses_sum, expected_sum)
|
self.assertEqual(losses_sum, expected_sum)
|
||||||
|
|
||||||
|
|
||||||
def test_kl_div_loss(self):
|
def test_kl_div_loss(self):
|
||||||
p_logits = mx.array([[1.0, 2.0], [0.5, 1.0]])
|
p_logits = mx.array([[1.0, 2.0], [0.5, 1.0]])
|
||||||
q_logits = mx.array([[0.8, 1.5], [0.4, 1.2]])
|
q_logits = mx.array([[0.8, 1.5], [0.4, 1.2]])
|
||||||
|
Loading…
Reference in New Issue
Block a user