展开 Tensorflow 中的 Vector 和 space 个带零的元素

Expand Vector in Tensorflow and space elements with zeros

我想 space 相互向量化元素并用零填充:

  a = [1, 5, 7, ..., 3]

Space 个带有两个零的 a 元素:

  b = [1, 0, 0, 5, 0, 0, 7, 0, 0, ... , 3, 0, 0]

我 space 元素的零个数应该是灵活的。

在 Tensorflow 的图形上执行此操作的最佳方法是什么?

你能按照解释的那样做吗(已接受答案的第二个例子)?

基本上我会首先创建 b 作为一个零向量,然后计算指向 b 的索引对于 a 中的所有值,然后使用链接中的代码post 将 a 的值分配给这些索引。

可能是这样的:

a = tf.constant(np.array([1, 3, 5, 7]))
dim = a.get_shape()[0].value
n = 2
b = tf.fill(dims=[dim*(n+1)], value=0.0)
new_indices = np.array([i + i*n for i in range(0, dim)])
# now call referenced code with new_indices to update `b`

我不确定这本身是最好的方法,但它可以完成工作。