Skip to content
Advertisement

How to add an attention layer to LSTM autoencoder built as sequential keras model in python?

So I want to build an autoencoder model for sequence data. I have started to build a sequential keras model in python and now I want to add an attention layer in the middle, but have no idea how to approach this. My model so far:

JavaScript

So far I have tried to add an attention function copied from here

JavaScript

and added it after first LSTM, before repeat vector, i.e:

JavaScript

but the code gives error, because the dimensions somehow do not fit and the problem is in putting output of attention() to repeat vector:

JavaScript

…. but according to model.summary() the output dimension of attention layer is (None, 20), which is the same also for the first lstm_1 layer . The code works without attention layer.

I would appreciate also some explanation why the solution is the solution to the problem, I am fairly new to python and have problems understanding what the class attention() is doing. I just copied it and tried to use it which is ofcrs not working….

Advertisement

Answer

Ok, I solved it. There has to be return_sequence = True in first LSTM layer. Then it works as it is.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement