tf.argmax 与 tf.arg_max

tf.argmax 与 tf.arg_max 用法相同,下面介绍 tf.argmax 用法

tf.argmax

def argmax(input,
           axis=None,
           name=None,
           dimension=None,
           output_type=dtypes.int64)
numpy.argmax(a, axis=None, out=None) 
返回沿轴axis最大值的索引。

Parameters: 
input: array_like,数组
axis : int, 可选,默认情况下,索引的是平铺的数组,否则沿指定的轴。 
out : array, 可选 如果提供,结果以合适的形状和类型被插入到此数组中。 

Returns: 
index_array : ndarray of ints 
索引数组。它具有与a.shape相同的形状,其中axis被移除。          

tf.argmax() 与 numpy.argmax() 方法的用法是一致的

axis = 0 的时候返回每一列最大值的位置索引
axis = 1 的时候返回每一行最大值的位置索引
axis = 2、3、4 ...,即为多维张量时,同理推断

例子

>>> a = np.arange(6).reshape(2,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.argmax(a)
5
>>> np.argmax(a, axis=0)  #0代表列
array([1, 1, 1])
>>> np.argmax(a, axis=1)  #1代表行
array([2, 2])
>>>
>>> b = np.arange(6)
>>> b[1] = 5
>>> b
array([0, 5, 2, 3, 4, 5])
>>> np.argmax(b)  #只返回第一次出现的最大值的索引
1

tf.arg_max()

The following is the source code of argmax (from https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/math_ops.py).

# pylint: disable=redefined-builtin
# TODO(aselle): deprecate arg_max
def argmax(input, axis=None, name=None, dimension=None):
  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)

As you can see, argmax is using arg_max inside. Also from the code, I recommend using argmax because arg_max could be deprecated soon.

猜你喜欢

转载自blog.csdn.net/qq_30868235/article/details/80904198