tf.argsort
Stay organized with collections
Save and categorize content based on your preferences.
Returns the indices of a tensor that give its sorted order along an axis.
tf.argsort(
values, axis=-1, direction='ASCENDING', stable=False, name=None
)
Used in the notebooks
values = [1, 10, 26.9, 2.8, 166.32, 62.3]
sort_order = tf.argsort(values)
sort_order.numpy()
array([0, 3, 1, 2, 5, 4], dtype=int32)
For a 1D tensor:
sorted = tf.gather(values, sort_order)
assert tf.reduce_all(sorted == tf.sort(values))
For higher dimensions, the output has the same shape as
values
, but along the given axis, values represent the index of the sorted
element in that slice of the tensor at the given position.
mat = [[30,20,10],
[20,10,30],
[10,30,20]]
indices = tf.argsort(mat)
indices.numpy()
array([[2, 1, 0],
[1, 0, 2],
[0, 2, 1]], dtype=int32)
If axis=-1
these indices can be used to apply a sort using tf.gather
:
tf.gather(mat, indices, batch_dims=-1).numpy()
array([[10, 20, 30],
[10, 20, 30],
[10, 20, 30]], dtype=int32)
See also |
tf.sort : Sort along an axis.
tf.math.top_k : A partial sort that returns a fixed number of top values
and corresponding indices.
|
Args |
values
|
1-D or higher numeric Tensor .
|
axis
|
The axis along which to sort. The default is -1, which sorts the last
axis.
|
direction
|
The direction in which to sort the values ('ASCENDING' or
'DESCENDING' ).
|
stable
|
If True, equal elements in the original tensor will not be
re-ordered in the returned order. Unstable sort is not yet implemented,
but will eventually be the default for performance reasons. If you require
a stable order, pass stable=True for forwards compatibility.
|
name
|
Optional name for the operation.
|
Returns |
An int32 Tensor with the same shape as values . The indices that would
sort each slice of the given values along the given axis .
|
Raises |
ValueError
|
If axis is not a constant scalar, or the direction is invalid.
|
tf.errors.InvalidArgumentError
|
If the values.dtype is not a float or
int type.
|
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-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.argsort\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/sort_ops.py#L86-L150) |\n\nReturns the indices of a tensor that give its sorted order along an axis.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.argsort`](https://www.tensorflow.org/api_docs/python/tf/argsort)\n\n\u003cbr /\u003e\n\n tf.argsort(\n values, axis=-1, direction='ASCENDING', stable=False, name=None\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [MoViNet for streaming action recognition](https://www.tensorflow.org/hub/tutorials/movinet) - [Image Classification with TensorFlow Hub](https://www.tensorflow.org/hub/tutorials/image_classification) - [Client-efficient large-model federated learning via \\`federated_select\\` and sparse aggregation](https://www.tensorflow.org/federated/tutorials/sparse_federated_learning) |\n\n values = [1, 10, 26.9, 2.8, 166.32, 62.3]\n sort_order = tf.argsort(values)\n sort_order.numpy()\n array([0, 3, 1, 2, 5, 4], dtype=int32)\n\n#### For a 1D tensor:\n\n sorted = tf.gather(values, sort_order)\n assert tf.reduce_all(sorted == tf.sort(values))\n\nFor higher dimensions, the output has the same shape as\n`values`, but along the given axis, values represent the index of the sorted\nelement in that slice of the tensor at the given position. \n\n mat = [[30,20,10],\n [20,10,30],\n [10,30,20]]\n indices = tf.argsort(mat)\n indices.numpy()\n array([[2, 1, 0],\n [1, 0, 2],\n [0, 2, 1]], dtype=int32)\n\nIf `axis=-1` these indices can be used to apply a sort using `tf.gather`: \n\n tf.gather(mat, indices, batch_dims=-1).numpy()\n array([[10, 20, 30],\n [10, 20, 30],\n [10, 20, 30]], dtype=int32)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| See also -------- ||\n|---|---|\n| \u003cbr /\u003e - [`tf.sort`](../tf/sort): Sort along an axis. - [`tf.math.top_k`](../tf/math/top_k): A partial sort that returns a fixed number of top values and corresponding indices. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `values` | 1-D or higher **numeric** `Tensor`. |\n| `axis` | The axis along which to sort. The default is -1, which sorts the last axis. |\n| `direction` | The direction in which to sort the values (`'ASCENDING'` or `'DESCENDING'`). |\n| `stable` | If True, equal elements in the original tensor will not be re-ordered in the returned order. Unstable sort is not yet implemented, but will eventually be the default for performance reasons. If you require a stable order, pass `stable=True` for forwards compatibility. |\n| `name` | Optional name for the operation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| An int32 `Tensor` with the same shape as `values`. The indices that would sort each slice of the given `values` along the given `axis`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|---------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------|\n| `ValueError` | If axis is not a constant scalar, or the direction is invalid. |\n| [`tf.errors.InvalidArgumentError`](https://www.tensorflow.org/api_docs/python/tf/errors/InvalidArgumentError) | If the `values.dtype` is not a `float` or `int` type. |\n\n\u003cbr /\u003e"]]