Python TensorFlow - 类似 numpy 的张量索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33736795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:51:50  来源:igfitidea点击:

TensorFlow - numpy-like tensor indexing

pythontensorflow

提问by Denis L

In numpy, we can do this:

在 numpy 中,我们可以这样做:

x = np.random.random((10,10))
a = np.random.randint(0,10,5)
b = np.random.randint(0,10,5)
x[a,b] # gives 5 entries from x, indexed according to the corresponding entries in a and b

When I try something equivalent in TensorFlow:

当我在 TensorFlow 中尝试等效的东西时:

xt = tf.constant(x)
at = tf.constant(a)
bt = tf.constant(b)
xt[at,bt]

The last line gives a "Bad slice index tensor" exception. It seems TensorFlow doesn't support indexing like numpy or Theano.

最后一行给出了“Bad slice index tensor”异常。TensorFlow 似乎不支持像 numpy 或 Theano 这样的索引。

Does anybody know if there is a TensorFlow way of doing this (indexing a tensor by arbitrary values). I've seen the tf.nn.embedding part, but I'm not sure they can be used for this and even if they can, it's a huge workaround for something this straightforward.

有谁知道是否有一种 TensorFlow 方法来做到这一点(通过任意值索引张量)。我已经看过 tf.nn.embedding 部分,但我不确定它们是否可以用于此目的,即使可以,对于如此简单的事情来说,这也是一个巨大的解决方法。

(Right now, I'm feeding the data from xas an input and doing the indexing in numpy but I hoped to put xinside TensorFlow to get higher efficiency)

(现在,我将数据x作为输入提供并在 numpy 中进行索引,但我希望放入xTensorFlow 以获得更高的效率)

回答by dga

LDGN's comment is correct. This is not possible at the moment, and is a requested feature. If you follow issue#206 on githubyou'll get updated if/when this is available. Many people would like this feature.

LDGN 的评论是正确的。目前这是不可能的,并且是要求的功能。如果您在 github 上关注issue#206,如果/当它可用时,您将得到更新。很多人都喜欢这个功能。

回答by Conchylicultor

For Tensorflow 0.11, basic indexing has been implemented. More advanced indexing (like boolean indexing) is still missing but apparently is planned for future versions.

对于Tensorflow 0.11,已实现基本索引。更高级的索引(如布尔索引)仍然缺失,但显然计划用于未来版本。

Advanced indexing can be tracked with https://github.com/tensorflow/tensorflow/issues/4638

可以使用https://github.com/tensorflow/tensorflow/issues/4638跟踪高级索引

回答by jdehesa

You can actually do that now with tf.gather_nd. Let's say you have a matrix mlike the following:

您现在实际上可以使用tf.gather_nd. 假设您有一个m如下所示的矩阵:

| 1 2 3 4 |
| 5 6 7 8 |

And you want to build a matrix rof size, let's say, 3x2, built from elements of m, like this:

并且您想要构建一个r大小矩阵,例如 3x2,由 的元素构建m,如下所示:

| 3 6 |
| 2 7 |
| 5 3 |
| 1 1 |

Each element of rcorresponds to a row and column of m, and you can have matrices rowsand colswith these indices (zero-based, since we are programming, not doing math!):

的每个元素r对应于 的一行和一列m,您可以拥有矩阵rowscols这些索引(从零开始,因为我们正在编程,而不是在做数学!):

       | 0 1 |         | 2 1 |
rows = | 0 1 |  cols = | 1 2 |
       | 1 0 |         | 0 2 |
       | 0 0 |         | 0 0 |

Which you can stack into a 3-dimensional tensor like this:

你可以像这样堆叠成一个 3 维张量:

| | 0 2 | | 1 1 | |
| | 0 1 | | 1 2 | |
| | 1 0 | | 2 0 | |
| | 0 0 | | 0 0 | |

This way, you can get from mto rthrough rowsand colsas follows:

通过这种方式,你可以从mr通过rowscols如下:

import numpy as np
import tensorflow as tf

m = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
rows = np.array([[0, 1], [0, 1], [1, 0], [0, 0]])
cols = np.array([[2, 1], [1, 2], [0, 2], [0, 0]])

x = tf.placeholder('float32', (None, None))
idx1 = tf.placeholder('int32', (None, None))
idx2 = tf.placeholder('int32', (None, None))
result = tf.gather_nd(x, tf.stack((idx1, idx2), -1))

with tf.Session() as sess:
    r = sess.run(result, feed_dict={
        x: m,
        idx1: rows,
        idx2: cols,
    })
print(r)

Output:

输出:

[[ 3.  6.]
 [ 2.  7.]
 [ 5.  3.]
 [ 1.  1.]]