tf.argmax():返回跨张量轴的最大值的索引。同tf.compat.v1.arg_max及tf.compat.v1.argmax
tf.compat.v1.argmax( input, axis=None, name=None, dimension=None, output_type=tf.dtypes.int64 )注意,如果是ties,则不能保证返回值的同一性。
用法:
import tensorflow as tf a = [1, 10, 26.9, 2.8, 166.32, 62.3] b = tf.math.argmax(input = a) c = tf.keras.backend.eval(b) # c = 4 # here a[4] = 166.32 which is the largest element of a across axis 0
即:
axis = 0时,返回每一列最大值的位置索引axis = 1时,返回每一行最大值的位置索引axis = 2、3、4...,即为多维张量时,同理推断
重点:结果进行降维,再按照对应维度求最大值索引
import tensorflow as tf a = tf.constant([1., 2., 3., 0., 9.]) b = tf.constant([[1, 2, 3], [3, 2, 1], [4, 5, 6], [6, 5, 4]]) with tf.Session() as sess: print(sess.run(a)) print(sess.run(tf.argmax(a, 0))) print("*" * 20) print(sess.run(b)) print(sess.run(tf.argmax(b, 0))) print("*" * 20) print(sess.run(b)) print(sess.run(tf.argmax(b, 1))) # 输出结果: # [1. 2. 3. 0. 9.] # 4 # ******************** # [[1 2 3] # [3 2 1] # [4 5 6] # [6 5 4]] # [3 2 2] # ******************** # [[1 2 3] # [3 2 1] # [4 5 6] # [6 5 4]] # [2 0 2 0] import tensorflow as tf c = tf.constant( [[[1, 2, 3], [2, 3, 5], [2, 2, 2]], [[5, 4, 3], [8, 7, 2], [1, 2, 3]], [[5, 4, 6], [10, 7, 30], [1, 2, 3]]]) with tf.Session() as sess: print(sess.run(c)) print("*" * 20) print(sess.run(tf.argmax(c, 0))) print("*" * 20) print(sess.run(tf.argmax(c, 1))) print("*" * 20) print(sess.run(tf.argmax(c, 2))) print("*" * 20) # 输出结果: # # [[[ 1 2 3] # [ 2 3 5] # [ 2 2 2]] # # [[ 5 4 3] # [ 8 7 2] # [ 1 2 3]] # # [[ 5 4 6] # [10 7 30] # [ 1 2 3]]] # ******************** # [[1 1 2] # [2 1 2] # [0 0 1]] # ******************** # [[1 1 1] # [1 1 0] # [1 1 1]] # ******************** # [[2 2 0] # [0 0 2] # [2 2 2]] # ********************