<p>You can use <code>keras.metrics.top_k_categorical_accuracy</code> for calculating accuracy.<br><br> But this one is accuracy metric. I don't think there is any inbuilt top_k loss function in TensorFlow or Keras as of now.<br> A loss function should be differentiable to work with gradient based learning methods. While <code>top_k</code> is not a differentiable function. Just like accuracy metric.<br> Hence it can be used as accuracy metric but not as learning objective. So you won't find any inbuilt method for this, however there are other research papers aiming to solve this problems. You might want to have a look at <a href="https://arxiv.org/abs/1802.07595" rel="nofollow noreferrer">Learning with Average Top-k Loss</a> and <a href="https://arxiv.org/abs/1705.08826" rel="nofollow noreferrer">Smooth Loss Functions for Deep Top-k Classification</a>.</p>
I want to define loss function for 3-best classification
<p>I'm design a classification model. </p>

<p>I have a problem, there are many categories which has similar features. 
I think best options is re-generate category hierarchy, but those are fixed.</p>

<p>So, I focused on 3-best accuracy, instead of 1-best accuracy. </p>

<p>I want to defined a loss function for 3-best accuracy.</p>

<p>I don't care where is the answer in position 1 - 3.  </p>

<p>Is there any good loss function for that? of How can I define it?   </p>