https://github.com/tensorflow/docs/tree/r1.3/site/en/api_docs/api_docs/python/tf
site/en/api_docs/api_docs/python/tf/argmax.md
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
有些论点已被弃用。它们将在未来版本中删除。更新说明:改为使用 axis 参数
Note that in case of ties the identity of the return value is not guaranteed.
请注意,在绑定的情况下,不保证返回值的标识。
input
: A Tensor
. Must be one of the following types: float32
, float64
, int64
, int32
, uint8
, uint16
, int16
, int8
, complex64
, complex128
, qint8
, quint8
, qint32
, half
.
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. (描述输入 Tensor 的哪个轴要减少。对于矢量,使用 axis = 0。)
output_type
: An optional tf.DType
from: tf.int32, tf.int64
. Defaults to tf.int64
.
name
: A name for the operation (optional).
A Tensor
of type output_type
.
在矩阵的结构中,axis 可被设置为 0 或 1,分别表示 0:按列计算,1:按行计算。
axis=0 时,比较每一列的元素,将每一列最大元素所在的索引记录下来,最后返回每一列最大元素所在的索引数组。
axis=1 时,比较每一行的元素,将每一行最大元素所在的索引记录下来,最后返回每一行最大元素所在的索引数组。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import os
import sys
import numpy as np
import tensorflow as tf
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
current_directory = os.path.dirname(os.path.abspath(__file__))
print(16 * "++--")
print("current_directory:", current_directory)
print(16 * "++--")
t1 = tf.constant([[0, 1, 2], [3, 12, 4], [9, 7, 6], [5, 11, 11]], dtype=np.float32)
indexmax0 = tf.argmax(t1, 0)
indexmax1 = tf.argmax(t1, 1)
with tf.Session() as sess:
input_t1 = sess.run(t1)
print("input_t1.shape:\n", input_t1.shape)
print("input_t1:\n", input_t1)
print('\n')
output0 = sess.run(indexmax0)
print("output0.shape:\n", output0.shape)
print("output0:\n", output0)
print('\n')
output1 = sess.run(indexmax1)
print("output1.shape:\n", output1.shape)
print("output1:\n", output1)
print('\n')
print("input_t1[0]:\n", input_t1[0])
print("input_t1[1]:\n", input_t1[1])
print("input_t1[2]:\n", input_t1[2])
print("input_t1[3]:\n", input_t1[3])
/usr/bin/python2.7 /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow/yongqiang.py
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
current_directory: /home/strong/tensorflow_work/R2CNN_Faster-RCNN_Tensorflow
++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--
2019-08-16 20:53:41.791525: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2019-08-16 20:53:41.872693: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-16 20:53:41.872930: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.42GiB
2019-08-16 20:53:41.872941: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
input_t1.shape:
(4, 3)
input_t1:
[[ 0. 1. 2.]
[ 3. 12. 4.]
[ 9. 7. 6.]
[ 5. 11. 11.]]
output0.shape:
(3,)
output0:
[2 1 3]
output1.shape:
(4,)
output1:
[2 1 0 1]
input_t1[0]:
[0. 1. 2.]
input_t1[1]:
[ 3. 12. 4.]
input_t1[2]:
[9. 7. 6.]
input_t1[3]:
[ 5. 11. 11.]
Process finished with exit code 0
tf.argmax() 返回某一维度上最大的数值所对应的下标。
axis = 0:
input_t1[0]: [ 0. 1. 2.]
input_t1[1]: [ 3. 12. 4.]
input_t1[2]: [ 9. 7. 6.]
input_t1[3]: [ 5. 11. 11.]
max_value: 9 12 11
max_index: 2 1 3
output0: [2 1 3]
axis = 1:
max_value max_index
input_t1[0]: [ 0. 1. 2.] 2 2
input_t1[1]: [ 3. 12. 4.] 12 1
input_t1[2]: [ 9. 7. 6.] 9 0
input_t1[3]: [ 5. 11. 11.] 11 1
output1: [2 1 0 1]