Reputation: 51
I want to be able to know the rounded accuracy of my neural network when the prediction is above or below a certain threshold. For example, I want it to only calculate accuracy when the prediction is above 0.55 or below 0.45 in order to filter out near 50/50 cases.
I tried using the soft_acc function on stackoverflow and adding an if else to the beginning to filter out the near 50/50s.
def soft_acc(y_true, y_pred):
if y_pred > 0.55 or y_pred < 0.45:
return K.mean(K.equal(K.round(y_true), K.round(y_pred)))
I received the following error message.
TypeError: Using a tf.Tensor
as a Python bool
is not allowed. Use if t is not None:
instead of if t:
to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor.
Upvotes: 4
Views: 2035
Reputation: 402553
Use tf.boolean_mask
to filter out values at indices that don't meet the required threshold.
# remove values from `X` in interval (lo, hi)
mask = tf.math.logical_or(tf.lesser(X, lo), tf.greater(X, hi))
X = tf.boolean_mask(X, mask)
In your case, you would define soft_acc
as
def soft_acc(y_true, y_pred):
mask = tf.math.logical_or(tf.greater(y_pred, 0.55), tf.lesser(y_pred, 0.45))
y_true2 = tf.boolean_mask(y_true, mask)
y_pred2 = tf.boolean_mask(y_pred, mask)
return K.mean(K.equal(K.round(y_true2), K.round(y_pred2)))
Upvotes: 4