Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mla/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def logloss(actual, predicted):


def hinge(actual, predicted):
return np.mean(np.max(1.0 - actual * predicted, 0.0))
return np.mean(np.maximum(1.0 - actual * predicted, 0.0))


def binary_crossentropy(actual, predicted):
Expand Down
22 changes: 22 additions & 0 deletions mla/metrics/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,25 @@ def test_multiclass_logloss():
assert_almost_equal(f([1], [1]), 0)
assert_almost_equal(f([1, 1], [1, 1]), 0)
assert_almost_equal(f([1], [0.5]), -np.log(0.5))


@pytest.mark.parametrize(
"actual, predicted, expected",
[
([1, -1], [2.0, -2.0], 0.0),
([1, -1], [0.0, 0.0], 1.0),
([1, -1], [-1.0, 1.0], 2.0),
([1, -1, 1], [2.0, -0.5, -1.0], 2.5 / 3),
],
)
def test_hinge(actual, predicted, expected):
assert_almost_equal(metric("hinge")(actual, predicted), expected)


def test_hinge_gradient():
from autograd import grad

actual = np.array([1.0, -1.0, 1.0])
predicted = np.array([2.0, -0.5, -1.0])
gradient = grad(lambda scores: get_metric("hinge")(actual, scores))
np.testing.assert_allclose(gradient(predicted), [0.0, 1.0 / 3, -1.0 / 3])
Loading