Taivanbat Badamdorj
Taivanbat Badamdorj

Reputation: 867

Making decoder model for sequence to sequence autoencoder in Keras

I'm working with this example but am confused on how to make the separate decoder model.

from keras.layers import Input, LSTM, RepeatVector
from keras.models import Model

inputs = Input(shape=(timesteps, input_dim))
encoded = LSTM(latent_dim)(inputs)

decoded = RepeatVector(timesteps)(encoded)
decoded = LSTM(input_dim, return_sequences=True)(decoded)

sequence_autoencoder = Model(inputs, decoded)
encoder = Model(inputs, encoded)

I understand how to make the encoder, but how do we make a separate decoder? I can define all the layers and make the encoder and decoder separately but is there a simpler way to do it like we've done with the encoder model?

Upvotes: 1

Views: 1153

Answers (1)

Daniel Möller
Daniel Möller

Reputation: 86630

Create the encoder:

inputs = Input(shape=(timesteps, input_dim))
encoded = LSTM(latent_dim)(inputs)
encoder = Model(inputs, encoded)

Create the decoder:

decInput = Input((the shape of the encoder's output))    
decoded = RepeatVector(timesteps)(decInput)
decoded = LSTM(input_dim, return_sequences=True)(decoded)
decoder = Model(decInput,decoded)

Joining models:

joinedInput = Input(shape=(timesteps, input_dim))
encoderOut = encoder(joinedInput)    
joinedOut = decoder(encoderOut)
sequence_autoencoder = Model(joinedInput,joinedOut)

Upvotes: 1

Related Questions