tensorflow gather 跨多个维度

tensorflow gather across multiple dimentions

gather(params, indices) 执行以下操作

output[i, ..., j, :, ... :] = params[indices[i, ..., j], :, ..., :]

所以如果你有 4 维参数和 2 维索引,你最终会得到 5 维数组

问题是怎么做

output[i, ..., j, :, ... :] = params[indices[i, :], ..., indices[j, :], :, ..., :]

使其充当numpy's

output = params[indices[0], indices[1], .. , :]

(github 上的 #206 票是关于不同的问题:它是关于类似 numpy 的 api,不是一般的聚集)

一种可能的方法是使用 gather_nd,但是(据我所知)如果我们想要 gather_nd 不是所有维度,我们仍然必须为它们创建索引,例如如果我们有 10 维数组 A 并且我们想用二维数组 B 索引前两个维度,比如 A[B[0], B[1], :] 我们的索引矩阵必须有 11 列(有 8冗余)。

--- old indices ----       new index
0 0 <all rows of length 8> 0
1 1 <all rows of length 8> 1
...

#206 上有一个更新,@ebrevdo 正在研究泛化切片。

与此同时,您可以展平数组,为所需元素构造线性索引,使用收集,然后重新整形,就像 mrry 在 中所做的那样。这在效率上可能不会比本机实现差多少