TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。
top_k()用於查找最後一個維度的前k個最大條目(矩陣的每一行)。
Syntax: tensorflow.math.top_k(input, k, sorted, name)
參數:
- input:It’s the input Tensor with 1 or more dimensions.
- k(optional):It’s is 0-D tensor with default value 0.
- sorted(optional):If it’s set to true returned elements will be sorted. Default is True.
- name(optional):It defines the name for the operation.
返回:
- values: k largest elements along each last dimensional slice.
- indices:indices of values within the last dimension of input.
範例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)
輸出:
a: tf.Tensor([7. 2. 3. 9. 5.], shape=(5, ), dtype=float64) Result: TopKV2(values=<tf.Tensor:shape=(1, ), dtype=float64, numpy=array([9.])>, indices=<tf.Tensor:shape=(1, ), dtype=int32, numpy=array([3], dtype=int32)>)
範例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=<tf.Tensor:shape=(2, 2), dtype=float64, numpy= array([[7., 3.], [9., 7.]])>, indices=<tf.Tensor:shape=(2, 2), dtype=int32, numpy= array([[0, 2], [0, 2]], dtype=int32)>)
相關用法
注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.math.top_k()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。