Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I flip a Tensor in Keras?

For example: I have a tensor with shape (5,10) and I want back a tensor with shape (5,10) but the first element should now be the last element. so [1,2,3,4,5]becomes [5,4,3,2,1] and [[1,2,3,4,5],[2,3,4,5,6]] becomes [[2,3,4,5,6],[1,2,3,4,5]].

If it matter, I am using tensorflow backend.

like image 248
Perm. Questiin Avatar asked Mar 09 '23 16:03

Perm. Questiin


1 Answers

Using the Keras backend, there is the reverse function.

import keras.backend as K

flipped = K.reverse(x,axes=0)

For using it in a layer, you can create a Lambda layer:

from keras.layers import *
layer = Lambda(lambda x: K.reverse(x,axes=0),output_shape=(shape of x))

(If it's a sequential layer, model.add(layer), if a functional API model, output = layer(input)

like image 179
Daniel Möller Avatar answered Mar 21 '23 08:03

Daniel Möller