### 1、排序和排序索引
```py
a=tf.random.shuffle(tf.range(5))
# 降序排列数据, 不写DESC是默认升序
tf.sort(a, direction='DESCENDING')
# 降序排列索引
a_sort_index=tf.argsort(a, direction='DESCENDING')
tf.gather(a, a_sort_index)
# 多维排序,只对多维度里面的一个维度进行排序,一般对最后的维度进行排序
a=tf.random.uniform([3,3], maxval=10,dtype=tf.int32)
a=tf.sort(a)
```
### 2、返回最大的前几个 Top_k
**使用场景:在预测结果中,取前top的数据,进行计算模型的准确性**
```py
# 返回a最大的前两个
b = tf.math.top_k(a, 2)
# 返回索引
b.indices
# 返回值
b.values
```
**取前top计算模型的准确性,然后验证的数据也必须是前top,然后验证两组数据是否相等**
**有一个结果预测对就算对**
```py
[[2,1], [[2,0], FALSE
[1,0], =》 [2,0], => TRUE
[0,2]] [2,0]] TRUE
```
**逻辑代码**
```py
# 创建一个预测数据和一个测试数据
pred = tf.constant[[0.1,0.2,0.7],[0.2,0.7,0.1]])
test = tf.constant([2,0])
# 取top的数据
a_top_index=tf.math.top_k(a, 3).indices
# 对数据进行转置
a_pred = tf.transpose(a_top, [1,0])
test= tf.broadcase_to(test, [3,2])
# 然后对比数据a_pred 和 test的结果
top_3_result=2/3 (2个对的,一共3个top数据)
```
**具体代码**
```py
```

Tensorflow : 排序