13

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!

Edamame
  • 23,718
  • 73
  • 186
  • 320

3 Answers3

16

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.

Clarence Leung
  • 2,446
  • 21
  • 24
  • 1
    You'll want to add `input_shape=your_input_shape` e.g. `model.add(Lambda(lambda x: x, input_shape=x))` if you want this to be the only layer in a network – duhaime Oct 22 '18 at 18:52
  • any idea about the complexity differences between this and using tf.identity()? – Sid Dec 06 '20 at 20:46
11

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

model.add(Layer()) 
fedo
  • 131
  • 1
  • 3
  • 1
    Do you know which of the two solutions (`Lambda(lambda x: x)` and `Layer()`) is preferable in terms of runtime complexity? – Scholar Jul 22 '21 at 07:33
1

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

Update: 31 Jan 2023

As from tensorflow==2.12.0 there is support for dedicated layer to simply pass through the input,

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

Ref: docs

Awsaf
  • 66
  • 5