Tensorflow中的向量移位(滚动) [英] Vector shift (Roll) in Tensorflow
问题描述
可以说,我们确实想使用Keras/TensorFlow处理图像(或ndim矢量). 并且,为了进行花哨的正则化,我们希望将每个输入向左移动一个随机数的位置(最上方的部分重新出现在右侧).
Lets say, that we do want to process images (or ndim vectors) using Keras/TensorFlow. And we want, for fancy regularization, to shift each input by a random number of positions to the left (owerflown portions reappearing at the right side ).
如何查看和解决它:
1)
TensorFlow的numpy roll功能是否有变化?
Is there any variation to numpy roll function for TensorFlow?
2)
x - 2D tensor
ri - random integer
concatenate(x[:,ri:],x[:,0:ri], axis=1) #executed for each single input to the layer, ri being random again and again (I can live with random only for each batch)
推荐答案
关于当前的每夜tensorflow构建(或将来的版本1.6.0).您可以使用tf.manip.roll,其作用类似于numpy roll. https://github.com/tensorflow/tensorflow/pull/14953 . 要改善上述答案,您可以执行以下操作:
On the current nightly build of tensorflow (or future version 1.6.0). you can use tf.manip.roll which works just like numpy roll. https://github.com/tensorflow/tensorflow/pull/14953 . To improve on the answer above you can do:
# size of x dimension
x_len = tensor.get_shape().as_list()[1]
# random roll amount
i = tf.random_uniform(shape=[1], maxval=x_len, dtype=tf.int32)
output = tf.manip.roll(tensor, shift=i, axis=[1])
这篇关于Tensorflow中的向量移位(滚动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!