Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vector shift (Roll) in Tensorflow

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 ).

How could it be viewed and solved:

1)

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)
like image 224
Holi Avatar asked Dec 23 '22 20:12

Holi


1 Answers

In TensorFlow v1.15.0 and up, you can use tf.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.roll(tensor, shift=i, axis=[1])

For older versions starting from v1.6.0 you will have to use 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])
like image 200
Jean Flaherty Avatar answered Dec 30 '22 21:12

Jean Flaherty