Make accuracy metrics work with masked outputs
* Make accuracy metrics work with masked outputs
Several accuracy metrics end in a call to `K.equal()`, which gives a tensor with dtype `bool`.  The multiplication with a float mask then crashes.  This fixes that crash.
* Move cast to metrics
@@ -21,13 +21,15 @@ def binary_accuracy(y_true, y_pred):
def categorical_accuracy(y_true, y_pred):
- return K.equal(K.argmax(y_true, axis=-1),
- K.argmax(y_pred, axis=-1))
+ return K.cast(K.equal(K.argmax(y_true, axis=-1),
+ K.argmax(y_pred, axis=-1)),
+ K.floatx())
def sparse_categorical_accuracy(y_true, y_pred):
- return K.equal(K.max(y_true, axis=-1),
- K.cast(K.argmax(y_pred, axis=-1), K.floatx()))
+ return K.cast(K.equal(K.max(y_true, axis=-1),
+ K.cast(K.argmax(y_pred, axis=-1), K.floatx())),
+ K.floatx())
def top_k_categorical_accuracy(y_true, y_pred, k=5):
