Tensorflow 张量流中的矢量移位(滚动)
比如说,我们确实希望使用Keras/TensorFlow处理图像(或ndim向量)。 为了实现奇特的正则化,我们希望将每个输入向左移动一个随机数的位置(owerflown部分重新出现在右侧) 如何看待和解决这一问题: (一) TensorFlow的numpy roll函数是否有任何变化 (二)Tensorflow 张量流中的矢量移位(滚动),tensorflow,theano,keras,Tensorflow,Theano,Keras,比如说,我们确实希望使用Keras/TensorFlow处理图像(或ndim向量)。 为了实现奇特的正则化,我们希望将每个输入向左移动一个随机数的位置(owerflown部分重新出现在右侧) 如何看待和解决这一问题: (一) TensorFlow的numpy roll函数是否有任何变化 (二) 我只是自己做了这个,我不认为有一个张量流运算来做np.roll。不过,上面的代码看起来基本正确,只是它不是按ri滚动,而是按(x.shape[1]-ri滚动) 另外,在选择随机整数时需要小心,因为它来自范
我只是自己做了这个,我不认为有一个张量流运算来做np.roll。不过,上面的代码看起来基本正确,只是它不是按ri滚动,而是按(x.shape[1]-ri滚动) 另外,在选择随机整数时需要小心,因为它来自范围(1,x.shape[1]+1)而不是范围(0,x.shape[1]),就好像ri是0,那么x[:,0:ri]将是空的 因此,我的建议更像(沿着维度1滚动):
编辑:在hannes的正确评论后添加了缺少的冒号。在TensorFlow v1.15.0及更高版本中,您可以使用tf.roll,其工作原理与numpy roll类似。 要改进上述答案,您可以执行以下操作:
# 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.roll(tensor, shift=i, axis=[1])
对于从v1.6.0开始的旧版本,您必须使用tf.manip.roll:
# 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])
最后一行缺少一个
:
,应该是y=tf.concat([x[:,x_len-i:],x[:,:x_len-i]],axis=1)
# 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.roll(tensor, shift=i, axis=[1])
# 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])