Python Keras: An layer output exactly the same thing as input

Viewed 9121

I am using Keras to build a Network. During the process, I need a layer, which takes an LSTM input, doing nothing, just output exactly the same as input. i.e. if each input record of LSTM is like [[A_t1, A_t2, A_t3, A_t4, A_t5, A_t6]], I am looking for a layer:

model.add(SomeIdentityLayer(x))

SomeIdentityLayer(x) will take [[A_t1, A_t2, A_t3, A_t4, A_t5, A_t6]] as input and output [[A_t1, A_t2, A_t3, A_t4, A_t5, A_t6]]. Is such layer/structure available in Keras? Thanks!

3 Answers

For a simpler operation like identity, you can just use a Lambda layer like:

model.add(Lambda(lambda x: x))

This will return an output exactly the same as your input.

Actually, default call() implementation in Layer is identity, so you can just use:

model.add(Layer()) 

You can use,

layer = tf.keras.layers.Activation('linear')
x = tf.constant([1.0, -1.0, 1.5])
y = layer(x) # output: same as x
Related