how to use top_k_categorical_accuracy metric?

5,656 views
Skip to first unread message

cxy...@gmail.com

unread,
Nov 5, 2016, 3:26:46 PM11/5/16
to Keras-users
Hi all,

From the Keras documentations, there's a metric called "top_k_categorical_accuracy", I'm having trouble knowing how to actually use that in the code, because almost everywhere is using the example of

model.compile (..., metrics=['accuracy'])

so i tried   model.compile(..., metrics=['top_k_categorical_accuracy']) but it doesn't work, tried a lot of other stuff and nothing worked either.

in addition, how do I change the k from the default of 5 to, say, 3?

Thanks in advance!

Chen-Ping Yu

unread,
Nov 5, 2016, 3:31:18 PM11/5/16
to Keras-users
it's for tensorflow backend by the way.

l...@lqu.me

unread,
Jan 11, 2017, 5:11:51 PM1/11/17
to Keras-users
I had the same problem. You need to import it.

 from keras.metrics import top_k_categorical_accuracy

For top 3, create a new function with different default.

 inTop3 = lambda x, y: top_k_categorical_accuracy(x, y, k=3)

 model.compile(..., metrics=[inTop3])

Daπid

unread,
Jan 12, 2017, 8:18:32 AM1/12/17
to l...@lqu.me, Keras-users


On 11 Jan 2017 23:11, <l...@lqu.me> wrote:
I had the same problem. You need to import it.

 from keras.metrics import top_k_categorical_accuracy

For top 3, create a new function with different default.

 inTop3 = lambda x, y: top_k_categorical_accuracy(x, y, k=3)

Or use functools.partial


 model.compile(..., metrics=[inTop3])



On Saturday, November 5, 2016 at 12:26:46 PM UTC-7, Chen-Ping Yu wrote:
Hi all,

From the Keras documentations, there's a metric called "top_k_categorical_accuracy", I'm having trouble knowing how to actually use that in the code, because almost everywhere is using the example of

model.compile (..., metrics=['accuracy'])

so i tried   model.compile(..., metrics=['top_k_categorical_accuracy']) but it doesn't work, tried a lot of other stuff and nothing worked either.

in addition, how do I change the k from the default of 5 to, say, 3?

Thanks in advance!

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/2ea488ed-5d76-4452-9c53-2eb9cf6cdba7%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

ZuZu

unread,
Feb 17, 2017, 10:38:53 AM2/17/17
to Keras-users
For top-1 (which is the same as 'accuracy' metric, use:

def top_1_categorical_accuracy(y_true, y_pred):
    return top_k_categorical_accuracy(y_true, y_pred, k=1) 

and then something like...
model.compile(optimizer='adadelta', 
              loss='categorical_crossentropy', 
              metrics=['accuracy', top_1_categorical_accuracy])

f201...@goa.bits-pilani.ac.in

unread,
Nov 21, 2017, 3:21:49 AM11/21/17
to Keras-users
saved so much effort! thank you so much!

marco.mo...@i-m3d.com

unread,
May 4, 2018, 3:53:54 AM5/4/18
to Keras-users
For me the solution using a lambda was not working. It gave me a weird error saying that "<lambda> is not a valid scope". To me this worked: https://stackoverflow.com/questions/46858016/keras-custom-loss-function-to-pass-arguments-other-than-y-true-and-y-pred
Reply all
Reply to author
Forward
0 new messages