<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/keras-team/keras/tree/v3.15.1/keras/src/metrics/accuracy_metrics.py#L382-L436) |

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
    )

<br />

<br />

<br />

| ## 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). |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A tensor with the same shape as `y_true` containing ones where `y_true` is in the top `k` and zeros elsewhere. ||

<br />