4
$\begingroup$

I have wanted to find AUC metric for my Keras model. Keras doesn't have any inbuilt function to measure AUC metric. So I found that write a function which calculates AUC metric and call this function while compiling Keras model like:

from sklearn import metrics
from keras import backend as K

def auc(y_true, y_pred):
    return metrics.roc_auc_score(K.eval(y_true), K.eval(y_pred))     
model.compile(loss="binary_crossentropy", optimizer='adam',metrics=['auc'])

But this doesn't work in my case. Please help me to figure out this query.

thanks

$\endgroup$

2 Answers 2

3
$\begingroup$

There is now a built-in function to compute (an approximation of) the AUC. See tf.keras.metrics.AUC. Apparently, you just need to do the following

...
model = tf.keras.Model(inputs, outputs)
model.compile('sgd', loss='mse', metrics=[tf.keras.metrics.AUC()])
$\endgroup$
1
  • $\begingroup$ Getting InvalidArgumentError: while trying to find AUC as mentioned in your answer. Check this $\endgroup$ Feb 15, 2021 at 12:36
1
$\begingroup$

I solve this query by myself by updating the AUC function.

def auc(y_true, y_pred):
    auc = tf.metrics.auc(y_true, y_pred)[1]
    K.get_session().run(tf.local_variables_initializer())
    return auc

Now, this work perfectly fine for me.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.