📜  Python – tensorflow.math.top_k()

📅  最后修改于: 2022-05-13 01:54:28.639000             🧑  作者: Mango

Python – tensorflow.math.top_k()

TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

top_k()用于查找最后一个维度的前 k 个最大条目(沿矩阵的每一行)。

示例 1:

Python3
# importing the library
import tensorflow as tf
  
# Initializing the input tensor
a = tf.constant([7, 2, 3, 9, 5], dtype = tf.float64)
  
# Printing the input tensor
print('a: ', a)
  
# Calculating result
res = tf.math.top_k(a)
  
# Printing the result
print('Result: ', res)


Python3
# importing the library
import tensorflow as tf
  
# Initializing the input tensor
a = tf.constant([[7, 2, 3], [ 9, 5, 7]], dtype = tf.float64)
  
# Printing the input tensor
print('a: ', a)
  
# Calculating result
res = tf.math.top_k(a, k = 2)
  
# Printing the result
print('Result: ', res)


输出:

a:  tf.Tensor([7. 2. 3. 9. 5.], shape=(5, ), dtype=float64)
Result:  TopKV2(values=, 
    indices=)
    
    
    

示例 2:

Python3

# importing the library
import tensorflow as tf
  
# Initializing the input tensor
a = tf.constant([[7, 2, 3], [ 9, 5, 7]], dtype = tf.float64)
  
# Printing the input tensor
print('a: ', a)
  
# Calculating result
res = tf.math.top_k(a, k = 2)
  
# Printing the result
print('Result: ', res)

输出:

a:  tf.Tensor(
[[7. 2. 3.]
 [9. 5. 7.]], shape=(2, 3), dtype=float64)
Result:  TopKV2(values=, indices=)