Vector shift (Roll) in Tensorflow
Asked Answered
B

2

4

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)
Blinding answered 7/3, 2017 at 15:7 Comment(0)
H
5

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])
Horsetail answered 6/2, 2018 at 6:14 Comment(0)
K
5

I just had to do this myself, and I don't think there is a tensorflow op to do np.roll unfortunately. Your code above looks basically correct though, except it doesn't roll by ri, rather by (x.shape[1] - ri).

Also you need to be careful in choosing your random integer that it is from range(1,x.shape[1]+1) rather than range(0,x.shape[1]), as if ri was 0, then x[:,0:ri] would be empty.

So what I would suggest would be something more like (for rolling along dimension 1):

x_len = x.get_shape().as_list()[1] 
i = np.random.randint(0,x_len) # The amount you want to roll by
y = tf.concat([x[:,x_len-i:], x[:,:x_len-i]], axis=1)

EDIT: added missing colon after hannes' correct comment.

Kaka answered 14/5, 2017 at 21:21 Comment(1)
there's a : missing in the last line, should be y = tf.concat([x[:,x_len-i:], x[:,:x_len-i]], axis=1)Sirmons
H
5

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])
Horsetail answered 6/2, 2018 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.