Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 1000
Edamame Avatar asked Nov 15 '17 01:11

Edamame


People also ask

How do you get the output of a layer in Keras?

Just do a model. summary(). It will print all layers and their output shapes.

What is input in Keras layers?

In a Keras layer, the input shape is generally the shape of the input data provided to the Keras model while training. The model cannot know the shape of the training data. The shape of other tensors(layers) is computed automatically.

What does input do in Keras?

Input() is used to instantiate a Keras tensor. A Keras tensor is a symbolic tensor-like object, which we augment with certain attributes that allow us to build a Keras model just by knowing the inputs and outputs of the model. shape: A shape tuple (integers), not including the batch size.


2 Answers

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

model.add(Layer()) 
like image 128
fedo Avatar answered Oct 27 '22 04:10

fedo


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.

like image 25
Clarence Leung Avatar answered Oct 27 '22 02:10

Clarence Leung