Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in sparse_categorical_accuracy #11373

Merged
merged 20 commits into from
Oct 13, 2018
Merged
Show file tree
Hide file tree
Changes from 10 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
11 changes: 7 additions & 4 deletions keras/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ def categorical_accuracy(y_true, y_pred):


def sparse_categorical_accuracy(y_true, y_pred):
# flatten y_true in case it's in shape (num_samples, 1) instead of (num_samples,)
return K.cast(K.equal(K.flatten(y_true),
K.cast(K.argmax(y_pred, axis=-1), K.floatx())),
K.floatx())
# reshape in case it's in shape (num_samples, 1) instead of (num_samples,)
if K.ndim(y_true) == K.ndim(y_pred):
y_true = K.squeeze(y_true, -1)
gabrieldemarmiesse marked this conversation as resolved.
Show resolved Hide resolved
# convert dense predictions to labels
y_pred_labels = K.argmax(y_pred, axis=-1)
y_pred_labels = K.cast(y_pred_labels, K.floatx())
return K.cast(K.equal(y_true, y_pred_labels), K.floatx())


def top_k_categorical_accuracy(y_true, y_pred, k=5):
Expand Down
19 changes: 11 additions & 8 deletions tests/keras/metrics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ def test_sparse_metrics():


def test_sparse_categorical_accuracy_correctness():
y_a = K.variable(np.random.randint(0, 7, (6,)), dtype=K.floatx())
y_b = K.variable(np.random.random((6, 7)), dtype=K.floatx())
# use one_hot embedding to convert sparse labels to equivalent dense labels
y_a_dense_labels = K.cast(K.one_hot(K.cast(y_a, dtype='int32'), num_classes=7),
dtype=K.floatx())
sparse_categorical_acc = metrics.sparse_categorical_accuracy(y_a, y_b)
categorical_acc = metrics.categorical_accuracy(y_a_dense_labels, y_b)
assert np.allclose(K.eval(sparse_categorical_acc), K.eval(categorical_acc))
input_shapes = [(6,), (6, 3)]
for shape in input_shapes:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a better error reporting, we could use pytest's parametrization instead of a for loop.

y_a = K.variable(np.random.randint(0, 7, shape), dtype=K.floatx())
y_b_shape = shape + (7,)
y_b = K.variable(np.random.random(y_b_shape), dtype=K.floatx())
# use one_hot embedding to convert sparse labels to equivalent dense labels
y_a_dense_labels = K.cast(K.one_hot(K.cast(y_a, dtype='int32'), 7),
dtype=K.floatx())
sparse_categorical_acc = metrics.sparse_categorical_accuracy(y_a, y_b)
categorical_acc = metrics.categorical_accuracy(y_a_dense_labels, y_b)
assert np.allclose(K.eval(sparse_categorical_acc), K.eval(categorical_acc))


def test_serialize():
Expand Down