Nirias
Nirias

Reputation: 23

Use tf.metrics.mean_per_class_accuracy in Keras

I am trying to find a way of using tf.metrics.mean_per_class_accuracy as Keras metric.

My classes are binary and the metric I want to perform is : this one. So it perfectly corresponds to mean_per_class accuracy from TF.

I could actually code it but I don't master tensorflow tensors

binary_accuracy and categorical_accuracyin keras do not precisely perform the metric I want.

Thank you very much

Upvotes: 2

Views: 814

Answers (1)

Pradip Gupta
Pradip Gupta

Reputation: 589

You can use the below function:

def mean_per_class_acc(y_true, y_pred):
    score, up_opt = tf.metrics.mean_per_class_accuracy(y_true, y_pred, num_classes=4)
    K.get_session().run(tf.local_variables_initializer())
    with tf.control_dependencies([up_opt]):
        score = tf.identity(score)       
    return score

Use it in your model as:

model.compile(loss= loss, optimizer=optimizer, metrics=[mean_per_class_acc])

Upvotes: 1

Related Questions