Create jaccard_test.py

This commit is contained in:
Mike Clark
2017-09-24 09:50:32 +08:00
committed by GitHub
parent b1f92a6ed3
commit 3a3a250a8f
@@ -0,0 +1,31 @@
from keras_contrib.losses import jaccard_distance
import numpy as np
def test_jaccard_distance():
# all_right, almost_right, half_right, all_wrong
y_true = np.array([[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0],
[0, 0, 1., 0.]])
y_pred = np.array([[0, 0, 1, 0], [0, 0, 0.9, 0], [0, 0, 0.1, 0],
[1, 1, 0.1, 1.]])
r = jaccard_distance(
K.variable(y_true),
K.variable(y_pred), )
all_right, almost_right, half_right, all_wrong = K.eval(r)
assert r.shape == (4, )
assert all_right == 0, 'should converge on zero'
assert all_right < almost_right
assert almost_right < half_right
assert half_right < all_wrong
def test_jaccard_distance_shapes_3d():
y_a = K.variable(np.random.random((5, 6, 7)))
y_b = K.variable(np.random.random((5, 6, 7)))
objective_output = jaccard_distance(y_a, y_b)
assert K.eval(objective_output).shape == (5, 6)
def test_jaccard_distance_shapes_2d():
y_a = K.variable(np.random.random((6, 7)))
y_b = K.variable(np.random.random((6, 7)))
objective_output = jaccard_distance(y_a, y_b)
assert K.eval(objective_output).shape == (6, )