tf.argmax与numpy.argmax

先看下官网定义:

tf.argmax

tf.argmax(
    input,
    axis=None,
    name=None,
    dimension=None,
    output_type=tf.int64
)

Defined in tensorflow/python/ops/math_ops.py.

See the guide: Math > Sequence Comparison and Indexing

Returns the index with the largest value across axes of a tensor. (deprecated arguments)

SOME ARGUMENTS ARE DEPRECATED. They will be removed in a future version. Instructions for updating: Use the axis argument instead

Note that in case of ties the identity of the return value is not guaranteed.

Args:

  • input: A Tensor. Must be one of the following types: float32, float64, int32, uint8, int16, int8, complex64, int64, qint8, quint8, qint32, bfloat16, uint16, complex128, half, uint32, uint64.
  • axis: A Tensor. Must be one of the following types: int32, int64. int32 or int64, must be in the range [-rank(input), rank(input)). Describes which axis of the input Tensor to reduce across. For vectors, use axis = 0.必须是下列类型之一:int32、int64。int32或int64,必须在[-rank(输入),rank(输入)]范围内。描述输入张量的哪个轴要缩小。对于向量,使用axis = 0。
  • output_type: An optional tf.DType from: tf.int32, tf.int64. Defaults to tf.int64.
  • name: A name for the operation (optional).

Returns:

A Tensor of type output_type.



np.argmax
numpy.argmax(a, axis=None, out=None)

Returns the indices of the maximum values along an axis.

Parameters:

  • a : array_like

    Input array.

  • axis : int, optional

    By default, the index is into the flattened array, otherwise along the specified axis.

  • out : array, optional

     If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype.
    
  • Returns: index_array : ndarray of ints

    Array of indices into the array. It has the same shape as a.shape with the dimension along axis removed.
    

See also

ndarray.argmax, argmin

amax
The maximum value along a given axis.
unravel_index
Convert a flat index into an index tuple.

Notes

In case of multiple occurrences of the maximum values, the indices corresponding to the first occurrence are returned.

Examples:

  >>>

  >>> a = np.arange(6).reshape(2,3) + 10
  >>> a
  array([[10, 11, 12],
         [13, 14, 15]])
  >>> np.argmax(a)
  5
  >>> np.argmax(a, axis=0)
  array([1, 1, 1])
  >>> np.argmax(a, axis=1)
  array([2, 2])

  Indexes of the maximal elements of a N-dimensional array:
  >>>

  >>> ind = np.unravel_index(np.argmax(a, axis=None), a.shape)
  >>> ind
  (1, 2)
  >>> a[ind]
  15

  >>>

  >>> b = np.arange(6)
  >>> b[1] = 5
  >>> b
  array([0, 5, 2, 3, 4, 5])
  >>> np.argmax(b)  # Only the first occurrence is returned.
  1


官方文档看完了,接下来直接通过实例进行比较说明

# coding: UTF-8
import tensorflow as tf
import numpy as np
import random

vector = [1, 2, 3]
matrix = [[1, 2, 3],
          [4, 5, 6]]

t_v_arg = tf.argmax(vector)    # tf.argmax对向量来说,默认axis=0,也只能为0
t_v_arg_0 = tf.argmax(vector, axis=0)  # 等同于tf.argmax(vector) , axis取1会报错

t_m_arg_0 = tf.argmax(matrix, axis=0)
t_m_arg_1 = tf.argmax(matrix, axis=1)

n_v_arg = np.argmax(vector)
n_v_arg_0 = np.argmax(vector, axis=0)    # 等同于np.argmax(vector)
# n_v_arg_1 = np.argmax(vector, axis=1)  # 报错numpy.AxisError: axis 1 is out of bounds for array of dimension 1

n_m_arg_0 = np.argmax(matrix, axis=0)
n_m_arg_1 = np.argmax(matrix, axis=1)

with tf.Session() as sess:
    init = tf.initialize_all_variables()
    sess.run(init)

    print("v_arg:", sess.run(t_v_arg))
    print('v_arg_1:', sess.run(t_v_arg_0))
    print("m_arg_0:", sess.run(t_m_arg_0))
    print("m_arg_1:", sess.run(t_m_arg_1))

    print("n_v_arg:", n_v_arg)
    print("n_v_agr_0:", n_v_arg_0)
    print("n_m_arg_0:", n_m_arg_0)
    print("n_m_arg_1:", n_m_arg_1)

"""
结果:

v_arg: 2
v_arg_1: 2
m_arg_0: [1 1 1]
m_arg_1: [2 2]
n_v_arg: 2
n_v_agr_0: 2
n_m_arg_0: [1 1 1]
n_m_arg_1: [2 2]

"""
	结论: 
	1、tf.argmax == np.argmax
	2、对于向量来说,默认axis=0,也只能等于0, 等于1会报错
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值