文件
math_ops.py
函數(shù)定義
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)
參數(shù)
返回
案例
# 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)
輸出結(jié)果為: [1 3 3 2]
輸出結(jié)果解釋
首先祠汇,argmax返回的是索引值,返回每一行或者每一列的最大值的索引,當(dāng)選擇axis=1時(shí)仍侥。表示每一行的最大值豁状,0表示每列的最大值索引捉偏,看上面的例子,第一列最大值為10泻红,為該列的第二個(gè)夭禽,所以索引為1,第二列谊路,最大值為45讹躯。為第四個(gè),所以索引為3缠劝,第三列最大值為78潮梯,為第四個(gè),索引為3惨恭,最后一個(gè)最大值為66秉馏,為第三個(gè),所以索引為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]
當(dāng)axis為1萝究,就是比每一行的值,返回最大值在該行的索引锉罐,比如帆竹,第一行,最大值為4脓规,為第四個(gè)栽连,索引為3,第二行最大值為10侨舆,為第一個(gè)秒紧,索引為0舷暮,以此類(lèi)推。
tf.argmin
和argmax格式之類(lèi)的都是一樣噩茄,但是返回的是最小值對(duì)應(yīng)的索引
其實(shí)tensorflow中的argmin下面,argmax和numpy中的argmin,argmax是一樣的绩聘,都是返回索引