tf.sort
Stay organized with collections
Save and categorize content based on your preferences.
Sorts a tensor.
tf.sort(
values, axis=-1, direction='ASCENDING', name=None
)
Used in the notebooks
Usage:
a = [1, 10, 26.9, 2.8, 166.32, 62.3]
tf.sort(a).numpy()
array([ 1. , 2.8 , 10. , 26.9 , 62.3 , 166.32], dtype=float32)
tf.sort(a, direction='DESCENDING').numpy()
array([166.32, 62.3 , 26.9 , 10. , 2.8 , 1. ], dtype=float32)
For multidimensional inputs you can control which axis the sort is applied
along. The default axis=-1
sorts the innermost axis.
mat = [[3,2,1],
[2,1,3],
[1,3,2]]
tf.sort(mat, axis=-1).numpy()
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3]], dtype=int32)
tf.sort(mat, axis=0).numpy()
array([[1, 1, 1],
[2, 2, 2],
[3, 3, 3]], dtype=int32)
See also |
tf.argsort : Like sort, but it returns the sort indices.
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' ).
|
name
|
Optional name for the operation.
|
Returns |
A Tensor with the same dtype and shape as values , with the elements
sorted along the given axis .
|
Raises |
tf.errors.InvalidArgumentError
|
If the values.dtype is not a float or
int type.
|
ValueError
|
If axis is not a constant scalar, or the direction is invalid.
|
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.
[[["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 2024-04-26 UTC."],[],[],null,["# tf.sort\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#L29-L83) |\n\nSorts a tensor.\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.sort`](https://www.tensorflow.org/api_docs/python/tf/sort)\n\n\u003cbr /\u003e\n\n tf.sort(\n values, axis=-1, direction='ASCENDING', name=None\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|----------------------------------------------------------------------------------------------------------------------|\n| - [TFP Release Notes notebook (0.11.0)](https://www.tensorflow.org/probability/examples/TFP_Release_Notebook_0_11_0) |\n\n#### Usage:\n\n a = [1, 10, 26.9, 2.8, 166.32, 62.3]\n tf.sort(a).numpy()\n array([ 1. , 2.8 , 10. , 26.9 , 62.3 , 166.32], dtype=float32)\n\n tf.sort(a, direction='DESCENDING').numpy()\n array([166.32, 62.3 , 26.9 , 10. , 2.8 , 1. ], dtype=float32)\n\nFor multidimensional inputs you can control which axis the sort is applied\nalong. The default `axis=-1` sorts the innermost axis. \n\n mat = [[3,2,1],\n [2,1,3],\n [1,3,2]]\n tf.sort(mat, axis=-1).numpy()\n array([[1, 2, 3],\n [1, 2, 3],\n [1, 2, 3]], dtype=int32)\n tf.sort(mat, axis=0).numpy()\n array([[1, 1, 1],\n [2, 2, 2],\n [3, 3, 3]], dtype=int32)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| See also -------- ||\n|---|---|\n| \u003cbr /\u003e - [`tf.argsort`](../tf/argsort): Like sort, but it returns the sort indices. - [`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| `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| A `Tensor` with the same dtype and shape as `values`, with the elements sorted 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| [`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| `ValueError` | If axis is not a constant scalar, or the direction is invalid. |\n\n\u003cbr /\u003e"]]