首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow argmax沿多个维度

Tensorflow argmax沿多个维度
EN

Stack Overflow用户
提问于 2017-05-31 23:03:14
回答 1查看 1.3K关注 0票数 1

我是tensorflow的新手,我正在尝试得到张量中最大值的索引。代码如下:

代码语言:javascript
复制
def select(input_layer):

    shape = input_layer.get_shape().as_list()

    rel = tf.nn.relu(input_layer)
    print (rel)
    redu = tf.reduce_sum(rel,3)
    print (redu)

    location2 = tf.argmax(redu, 1)
    print (location2)

sess = tf.InteractiveSession()
I = tf.random_uniform([32, 3, 3, 5], minval = -541, maxval = 23, dtype = tf.float32)
matI, matO = sess.run([I, select(I, 3)])
print(matI, matO)

下面是输出:

代码语言:javascript
复制
Tensor("Relu:0", shape=(32, 3, 3, 5), dtype=float32)
Tensor("Sum:0", shape=(32, 3, 3), dtype=float32)
Tensor("ArgMax:0", shape=(32, 3), dtype=int64)
...

由于在argmax函数中使用了dimension=1,所以Tensor("ArgMax:0") = (32,3)的形状。有没有办法在应用argmax之前不执行reshape就获得argmax输出张量大小= (32,)

EN

回答 1

Stack Overflow用户

发布于 2017-05-31 23:54:36

有问题的是,你不想要一个大小为(32,)的输出,因为当你沿着几个方向进行argmax时,你通常希望得到所有减少的维度的最大值的坐标。在您的示例中,您可能希望输出大小为(32,2)

你可以像这样做一个二维argmax

代码语言:javascript
复制
import numpy as np
import tensorflow as tf

x = np.zeros((10,9,8))
# pick a random position for each batch image that we set to 1
pos = np.stack([np.random.randint(9,size=10), np.random.randint(8,size=10)])

posext = np.concatenate([np.expand_dims([i for i in range(10)], axis=0), pos])
x[tuple(posext)] = 1

a = tf.argmax(tf.reshape(x, [10, -1]), axis=1)
pos2 = tf.stack([a // 8, tf.mod(a, 8)]) # recovered positions, one per batch image

sess = tf.InteractiveSession()
# check that the recovered positions are as expected
assert (pos == pos2.eval()).all(), "it did not work"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44288272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档