tf.keras.metrics.sparse_top_k_categorical_accuracy
Stay organized with collections
Save and categorize content based on your preferences.
Computes how often integer targets are in the top K predictions.
tf.keras.metrics.sparse_top_k_categorical_accuracy(
y_true, y_pred, k=5, from_sorted_ids=False
)
Args |
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).
|
Returns |
A tensor with the same shape as y_true containing ones where y_true
is in the top k and zeros elsewhere.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2026-10-31 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-10-31 UTC."],[],[]]