简体   繁体   English

TensorFlow:argmax(-min)

[英]TensorFlow: argmax (-min)

I just noticed an unexpected (at least for me) behavior in TensorFlow. 我刚注意到TensorFlow中的一个意外(至少对我来说)行为。 I thought tf.argmax (- argmin ) operates on the ranks of a Tensor from outer to inner, but apparently it does not?! 我想tf.argmax ( - argmin )运行于一个张量的从外到内的行列,但显然它没有?

Example: 例:

import numpy as np
import tensorflow as tf

sess = tf.InteractiveSession()

arr = np.array([[31, 23,  4, 24, 27, 34],
                [18,  3, 25,  0,  6, 35],
                [28, 14, 33, 22, 20,  8],
                [13, 30, 21, 19,  7,  9],
                [16,  1, 26, 32,  2, 29],
                [17, 12,  5, 11, 10, 15]])

# arr has rank 2 and shape (6, 6)
tf.rank(arr).eval()
> 2
tf.shape(arr).eval()
> array([6, 6], dtype=int32)

tf.argmax takes two arguments: input and dimension . tf.argmax有两个参数: inputdimension Since the indices of array arr are arr[rows, columns] , I would expect tf.argmax(arr, 0) to return the index of the maximum element per row, while I would have expected tf.argmax(arr, 1) to return the maximum element per column. 由于数组arr的索引是arr[rows, columns] ,我希望tf.argmax(arr, 0)返回每行最大元素的索引,而我希望tf.argmax(arr, 1)为返回每列的最大元素。 Likewise for tf.argmin . 同样对于tf.argmin

However, the opposite is true: 但事实恰恰相反:

tf.argmax(arr, 0).eval()
> array([0, 3, 2, 4, 0, 1])

# 0 -> 31 (arr[0, 0])
# 3 -> 30 (arr[3, 1])
# 2 -> 33 (arr[2, 2])
# ...
# thus, this is clearly searching for the maximum element
# for every column, and *not* for every row

tf.argmax(arr, 1).eval()
> array([5, 5, 2, 1, 3, 0])

# 5 -> 34 (arr[0, 5])
# 5 -> 35 (arr[1, 5])
# 2 -> 33 (arr[2, 2])
# ...
# this clearly returns the maximum element per row,
# albeit 'dimension' was set to 1

Can someone explain this behavior? 有人可以解释这种行为吗?

Generalized every n-dimensional Tensor t is indexed by t[i, j, k, ...] . 广义的每个n维张量tt[i, j, k, ...]索引。 Thus, t has rank n and shape (i, j, k, ...) . 因此, t具有秩n和形状(i, j, k, ...) Since dimension 0 corresponds to i , dimension 1 to j , and so forth. 由于尺寸0对应于i ,尺寸1对应于j ,等等。 Why does tf.argmax (& - argmin ) ignore this scheme? 为什么tf.argmax ( - argmin )忽略这个方案?

Think of the dimension argument of tf.argmax as the axis across which you reduce. tf.argmaxdimension参数视为您减少的轴。 tf.argmax(arr, 0) reduces across dimension 0 , ie the rows. tf.argmax(arr, 0)在维度0减少,即行。 Reducing across rows means that you will get the argmax of each individual column. 减少行数意味着您将获得每个列的argmax。

This might be counterintuitive, but it falls in line with the conventions used in tf.reduce_max and so on. 这可能违反直觉,但它符合tf.reduce_max使用的约定等等。

In an n-dimensional Tensor, any given dimension has n-1 dimensions that form a discrete 2 dimensional subspace. 在n维张量中,任何给定的维度都具有n-1维,形成离散的2维子空间。 Following the same logic, it has n-2 3 dimensional subspaces, all the way down to n - (n-1), n dimensional subspaces. 遵循相同的逻辑,它具有n-2个3维子空间,一直到n - (n-1),n维子空间。 You could express any aggregation as a function within the remaining subspace(s), or across the subspace(s) that are being aggregated. 您可以将任何聚合表示为剩余子空间中的函数,或者正在聚合的子空间中的任何聚合。 Since the subspace will no longer exist after the aggregation, Tensorflow has chosen to implement it as an operation across that dimension. 由于聚合后子空间将不再存在,因此Tensorflow已选择将其实现为跨该维度的操作。

Frankly, it's an implementation choice by the creators of Tensorflow, now you know. 坦率地说,这是Tensorflow创建者的一个实现选择,现在你知道了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM