Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shift a tensor using api in tensorflow, just like nump.roll() or shift ? [duplicate]

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 740
Holi Avatar asked Mar 07 '17 15:03

Holi


People also ask

How do you transpose in TensorFlow?

transpose(x, perm=[1, 0]) . As above, simply calling tf. transpose will default to perm=[2,1,0] . To take the transpose of the matrices in dimension-0 (such as when you are transposing matrices where 0 is the batch dimension), you would set perm=[0,2,1] .

Can you zip tensors?

3 Answers. Show activity on this post. Tensor objects are not iterable, which explains why your third code sample fails. So, to answer your question, there is no zip-like function in TensorFlow.


2 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 118
Jean Flaherty Avatar answered Oct 03 '22 20:10

Jean Flaherty


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.

like image 22
Fergal Avatar answered Oct 03 '22 22:10

Fergal