张量变换

2024-04-19 11:02:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用tf.gather_nd(params, indices, name=None)从特征映射张量中检索元素

有没有办法把这个张量[[0,2,2]]转换成[[0,0],[1,2],[2,2]] 因为我需要它在函数中用作索引

我只有[[0,2,2]]

应该是这个结构

indices = [[0,0],[1,2],[2,2]]
params = [['3', '1','2','-1'], ['0.3', '1.4','5','0'],['5', '6','7','8']]

t=tf.gather_nd(params, indices, name=None)

with tf.Session() as sess:

    sess.run(tf.initialize_all_variables())
    print(sess.run(t)) # outputs 3 5 7

Tags: 函数runnamenone元素tfwith特征
1条回答
网友
1楼 · 发布于 2024-04-19 11:02:51

假设您试图将张量t0 = [[x0, x1, x2, ... xn]]转换为张量[[0, x0], [1, x1], [2, x2], ..., [n, xn]],您可以将其与范围张量连接起来,如下所示:

t0 = ...
N = tf.shape(t0)[1]                   # number of indices
t0 = tf.concat([tf.range(N), t0], 0)  # [[0, 1, 2], [0, 2, 2]]
indices = tf.transpose(t0)            # [[0, 0], [1, 2], [2, 2]]

这应该给你你想要的指数

相关问题 更多 >