tf.keras.metrics.sparse_top_k_categorical_accuracy

Computes how often integer targets are in the top K predictions.

y_true A tensor of shape (batch_size) representing indices or IDs of true categories.
y_pred If from_sorted_ids=False, a tensor of shape (batch_size, num_categories) containing the scores for each sample for all possible categories. If from_sorted_ids=True, a tensor of shape (batch_size, N) containing indices or IDs of the top N categories in order from highest score to lowest score.
k (Optional) Number of top elements to look at for computing accuracy. Defaults to 5.
from_sorted_ids (Optional) Whether y_pred is sorted category IDs or scores for all categories (the default).

A tensor with the same shape as y_true containing ones where y_true is in the top k and zeros elsewhere.