如何在 TensorFlow 中无周期性边界滚动? [英] How to roll without periodic boundaries in TensorFlow?
问题描述
我需要一个与滚动非常相似的张量的转换.不同之处在于我不希望轴末端的值出现在开头.换句话说,例如,我希望第二个元素位于 3d 位置,但我不希望最后一个元素成为第一个元素.相反,我希望第一个元素为零.
I need a transformation of a tensor which is very similar to roll. The difference is that I do not want the values from the end of the axis to appear in the beginning. In other words I want, for example, the 2nd element to be on 3d place but I do not want the last element to become the first one. Instead, I want the first elements to be zeros.
我已经试过了:
prev_xs = tf.roll(xs, shift = 1, axis = 1)
prev_xs[:,0] = 0.0
然而,它不起作用,因为
However, it does not work because
TypeError: 'Tensor' object does not support item assignment
那么,问题的正确解决方案是什么?
So, what is the proper solution of the problem?
推荐答案
你可以使用
prev_xs = tf.concat((tf.zeros([tf.shape(xs)[0], 1]), xs[:, :1]), axis=1)
一步一步,我们通过像[:, :1]
这样的索引来丢弃xs
的最后一列.我们创建一列具有适当行数的零.然后我们将它连接在 xs
前面,将每一列向后推 1.
Step-by-step, we discard the last column of xs
by indexing like [:, :1]
. We create a column of zeros with the appropriate number of rows. Then we concatenate it in front of xs
, pushing every column back by 1.
这篇关于如何在 TensorFlow 中无周期性边界滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!