Expand Vector in Tensorflow and space elements with zeros

喜你入骨 提交于 2019-12-11 02:22:10

问题


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.



来源:https://stackoverflow.com/questions/42391598/expand-vector-in-tensorflow-and-space-elements-with-zeros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!