文件
math_ops.py
函數定義
def argmax(input,
axis=None,
name=None,
dimension=None,
output_type=dtypes.int64):
if dimension is not None:
if axis is not None:
raise ValueError("Cannot specify both 'axis' and 'dimension'")
axis = dimension
elif axis is None:
axis = 0
return gen_math_ops.arg_max(input, axis, name=name, output_type=output_type)
參數
返回
案例
# axis = 0
import tensorflow as tf
import numpy as np
ipt = np.array([[1,2,1,4],[10,3,2,8],[6,7,9,66],[5,45,78,54]])
res = tf.argmax(ipt,0) # axis = 0
sess = tf.Session()
opt = sess.run(res)
print(opt)
輸出結果為: [1 3 3 2]
輸出結果解釋
首先,argmax返回的是索引值,返回每一行或者每一列的最大值的索引,當選擇axis=1時。表示每一行的最大值,0表示每列的最大值索引,看上面的例子,第一列最大值為10,為該列的第二個,所以索引為1,第二列,最大值為45。為第四個,所以索引為3,第三列最大值為78,為第四個,索引為3,最后一個最大值為66,為第三個,所以索引為2。
# axis = 1
import tensorflow as tf
import numpy as np
ipt = np.array([[1,2,1,4],[10,3,2,8],[6,7,9,66],[5,45,78,54]])
res = tf.argmax(ipt,1) # axis = 1
sess = tf.Session()
opt = sess.run(res)
print(opt)
[3 0 3 2]
當axis為1,就是比每一行的值,返回最大值在該行的索引,比如,第一行,最大值為4,為第四個,索引為3,第二行最大值為10,為第一個,索引為0,以此類推。
tf.argmin
和argmax格式之類的都是一樣,但是返回的是最小值對應的索引
其實tensorflow中的argmin,argmax和numpy中的argmin,argmax是一樣的,都是返回索引