gpt4 book ai didi

python - TensorFlow:argmax(-min)

转载 作者:太空狗 更新时间:2023-10-29 16:54:45 27 4
gpt4 key购买 nike

我刚刚注意到 TensorFlow 中出现了意外(至少对我而言)的行为。我以为 tf.argmax (-argmin) 在 Tensor 的行列上从外到内运行,但显然不是?!

例子:

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 接受两个参数:inputdimension。由于数组 arr 的索引是 arr[rows, columns],我希望 tf.argmax(arr, 0) 返回索引每行的最大元素,而我希望 tf.argmax(arr, 1) 返回每列的最大元素。 tf.argmin 也是如此。

然而,事实恰恰相反:

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

有人可以解释这种行为吗?

广义每个 n 维张量 tt[i, j, k, ...] 索引。因此,t 具有秩 n 和形状 (i, j, k, ...)。由于维度 0 对应于 i,维度 1 对应于 j,依此类推。为什么 tf.argmax (& -argmin) 忽略这个方案?

最佳答案

tf.argmaxdimension 参数视为您减少的轴。 tf.argmax(arr, 0) 减少维度 0,即行。减少跨行意味着您将获得每一列的 argmax。

这可能违反直觉,但它符合 tf.reduce_max 等中使用的约定。

关于python - TensorFlow:argmax(-min),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38094217/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com