如何从张量流张量中获取不同维度的切片



>我有一个浮点类型的 3D 张量x和一个 int 类型的 1D 张量y。我想获取从 0 到对应于y的每个元素的索引的第二个x轴的每个切片的平均值。换句话说,如果xy是数字数组,我会想要

In [1]: y = [1, 2, 1, 1]
In [2]: x = np.array([[[1,2],[3,4],[5,6]], [[1,2],[3,4],[5,6]], [[1,2],[3,4],[5,6]], [[1,2],[3,4],[5,6]]])
In [3]: np.array([np.mean(x[index, :item], axis=0) for index, item in enumerate(y)])
Out[22]: 
array([[ 1.,  2.],
[ 2.,  3.],
[ 1.,  2.],
[ 1.,  2.]]) 

最简单的方法是什么?

在一般情况下,您可以使用tf.while_loop

import numpy as np
import tensorflow as tf
y = tf.constant([1, 2, 1, 1])
x = tf.constant([[[1,2],[3,4],[5,6]],
[[1,2],[3,4],[5,6]],
[[1,2],[3,4],[5,6]],
[[1,2],[3,4],[5,6]]], dtype=np.float32)
y_len = tf.shape(y)[0]
idx = tf.zeros((), dtype=tf.int32)
res = tf.zeros((0,2))
_, res = tf.while_loop(
lambda idx, res: idx < y_len,
lambda idx, res: (idx + 1, tf.concat([res, tf.reduce_mean(x[idx, :y[idx]], axis=0)[tf.newaxis]], axis=0)),
[idx, res],
shape_invariants=[idx.get_shape(), tf.TensorShape([None, 2])])
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
res.eval()
# returns
# array([[ 1.,  2.],
#        [ 2.,  3.],
#        [ 1.,  2.],
#        [ 1.,  2.]], dtype=float32)    

在不太一般的情况下,y的长度在图构建时是已知的,你可以避免在python中使用tf.while_loop和循环(如果y有很多元素,可能会导致一个大的图(。

y_len = y.shape.num_elements()
res = tf.Variable(np.zeros((y_len, 2), dtype=np.float32))
res = tf.tuple([res] + [res[idx].assign(tf.reduce_mean(x[idx, :y[idx]], axis=0))
for idx in range(y_len)])[0]

请注意,您也可以简单地级联更新,这与tf.while_loop的一般情况不同:

y_len = y.shape.num_elements()
res = tf.zeros((0,2))
for idx in range(y_len):
res = tf.concat([res, tf.reduce_mean(x[idx, :y[idx]], axis=0)[tf.newaxis]], axis=0)

但现在更新需要按顺序进行。在前一种解决方案中,每行发生的更新都是独立的,可以并行运行,我认为这更好。

最新更新