TensorFlow- tf.argmax 函数学习

来源:互联网 发布:h5实现的炫酷页面源码 编辑:程序博客网 时间:2024/06/04 19:41

tf.argmax 格式

tf.argmax
argmax(
input,
axis=None,
name=None,
dimension=None
)
其中,input为一个张量,类型为 float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half
axis 表示选取的坐标轴
name 为该操作取名

看一个栗子

import tensorflow as tfimport numpy as npipt = np.array([[1,2,1,4],[10,3,2,8],[6,7,9,66],[5,45,78,54]])res = tf.argmax(ipt,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 tfimport numpy as npipt = np.array([[1,2,1,4],[10,3,2,8],[6,7,9,66],[5,45,78,54]])res = tf.argmax(ipt,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是一样的,都是返回索引

原创粉丝点击