Tensorflow 中的向量移位(滚动)

2023-12-11

假设我们确实想使用 Keras/TensorFlow 处理图像(或 ndim 向量)。 为了进行奇特的正则化,我们希望将每个输入向左移动随机数量的位置(溢出的部分重新出现在右侧)。

如何查看和解决:

1)

TensorFlow 的 numpy roll 函数有什么变化吗?

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 v1.15.0 及更高版本中,您可以使用 tf.roll,其工作方式与 numpy roll 类似。https://github.com/tensorflow/tensorflow/pull/14953。 为了改进上面的答案,你可以这样做:

# 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])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Tensorflow 中的向量移位(滚动) 的相关文章

随机推荐