Expand Vector in Tensorflow and space elements wit

2019-07-13 06:03发布

问题:

I would like to space vector elements from each others and fill it with zeros:

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

Space elements of a with two zeros:

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

The number of zeros that I space the elements with should be flexible.

What's the best way to do this on the Graph in Tensorflow?

回答1:

Could you do it as explained in this post (second example of the accepted answer)?

Basically I would first create b as a a vector of zeros, then compute the indices which point into b for all values in a and then use the code from the linked post to assign the values of a to these indices.

Something like this maybe:

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`

I'm not sure that this is the best way per se, but it would get the job done.