简体   繁体   English

如何从张量中获取前 3 个最大数字

[英]How do I get the first 3 max numbers from tensor

我如何从y_classe = tf.argmax(preds, axis=1, output_type=tf.int32)获得前 3 个最大数字?

You can sort and take the first 3:您可以排序并取前 3 个:

import tensorflow as tf

a = [1, 10, 26.9, 2.8, 166.32, 62.3]
sorted_a = tf.sort(a,direction='DESCENDING')
max_3 = tf.gather(sorted_a, [0,1,2])
print(max_3)

You can use tf.math.top_k :您可以使用tf.math.top_k

import tensorflow as tf

y_pred = [[-18.6, 0.51, 2.94, -12.8]]

max_entries = 3

values, indices = tf.math.top_k(y_pred, k=max_entries)
print(values)
print(indices)
tf.Tensor([[  2.94   0.51 -12.8 ]], shape=(1, 3), dtype=float32)
tf.Tensor([[2 1 3]], shape=(1, 3), dtype=int32)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM