tf.keras.ops.top_k
Stay organized with collections
Save and categorize content based on your preferences.
Finds the top-k values and their indices in a tensor.
tf.keras.ops.top_k(
x, k, sorted=True
)
Args |
x
|
Input tensor.
|
k
|
An integer representing the number of top elements to retrieve.
|
sorted
|
A boolean indicating whether to sort the output in
descending order. Defaults toTrue .
|
Returns |
A tuple containing two tensors. The first tensor contains the
top-k values, and the second tensor contains the indices of the
top-k values in the input tensor.
|
Example:
x = keras.ops.convert_to_tensor([5, 2, 7, 1, 9, 3])
values, indices = top_k(x, k=3)
print(values)
array([9 7 5], shape=(3,), dtype=int32)
print(indices)
array([4 2 0], shape=(3,), dtype=int32)
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 2024-06-07 UTC.
[null,null,["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.ops.top_k\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/ops/math.py#L136-L163) |\n\nFinds the top-k values and their indices in a tensor. \n\n tf.keras.ops.top_k(\n x, k, sorted=True\n )\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------|-----------------------------------------------------------------------------------------|\n| `x` | Input tensor. |\n| `k` | An integer representing the number of top elements to retrieve. |\n| `sorted` | A boolean indicating whether to sort the output in descending order. Defaults to`True`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A tuple containing two tensors. The first tensor contains the top-k values, and the second tensor contains the indices of the top-k values in the input tensor. ||\n\n\u003cbr /\u003e\n\n#### Example:\n\n x = keras.ops.convert_to_tensor([5, 2, 7, 1, 9, 3])\n values, indices = top_k(x, k=3)\n print(values)\n array([9 7 5], shape=(3,), dtype=int32)\n print(indices)\n array([4 2 0], shape=(3,), dtype=int32)"]]