Professional Documents
Culture Documents
LSTM-AutoEncoders
Bob Rupak Roy Follow
Oct 17, 2020 · 7 min read
There are various types of autoencoders available suited for different types
of scenarios, however the commonly used autoencoder is for feature
extraction.
Combining feature extraction models with different types of models have a
wide variety of applications.
LSTM
Recurrent neural networks such as the LSTM or Long Short-Term
Memory network are specially designed to support the sequential data.
They are capable of learning the complex dynamics within the temporal
ordering of input sequences as well as use an internal memory to remember
or use information across long input sequences.
N
OW combing Autoencoders with LSTM will allow us to understand the
pattern of sequential data with LSTM then extract the features with
Autoencoders to recreate the input sequence.
Then we will create a sample of simple sequential data for input and
reshaping it into the preferred LSTM input data format/shape of
[samples,timesteps,features]
model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(n_in,1)))
model.add(RepeatVector(n_in))
#RepeatVector layer repeats the incoming inputs a specific number of
time
#fit model
model.fit(sequence, sequence, epochs=300, verbose=0)
#Reconstruct the input sequence
p = model.predict(sequence, verbose=0)
print(p[0,:,0])
#fit model
model.fit(sequence, sequence, epochs=300, verbose=1)
Now lets try something a bit complex having one encoder and 2 decoders.
We will name it as Composite LSTM AutoEncoders where 1 decoder will
be used for reconstruction and another decoder will be used for
prediction.
decoder1 = RepeatVector(n_in)(encoder)
decoder1 = LSTM(100, activation='relu', return_sequences=True)
(decoder1)
decoder1 = TimeDistributed(Dense(1))(decoder1)
The work flow of the composite encoder will be something like this.
#LSTM-AutoEncoder
#define encoder
visible = Input(shape=(n_in,1))
encoder = LSTM(100, activation='relu')(visible)
#Decoder 1 for prediction
decoder1 = RepeatVector(n_in)(encoder)
decoder1 = LSTM(100, activation='relu', return_sequences=True)
(decoder1)
decoder1 = TimeDistributed(Dense(1))(decoder1)
#fit model
model.fit(seq_in, [seq_in,seq_out], epochs=300, verbose=0)
#demonstrate prediction
results = model.predict(seq_in, verbose=1)
print(results)
Now once the autoencoder has been lift the decoder is no longer
needed and can be removed and we can keep the encoder as a
standalone model.
#define model
model = Sequential()
model.add(LSTM(100, activation='relu', input_shape=(n_in,1)))
model.add(RepeatVector(n_in))
model.add(LSTM(100, activation='relu', return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(optimizer='adam', loss='mse')
#fit model
model.fit(sequence, sequence, epochs=300, verbose=1)
model.summary()
model.summary()
Now this standalone encoder model can be used later. However if we need
more than 9 sequence then we have to increase our sequence length/time
steps else it will over kill the 9 sequence prediction model.
Thanks your time I tried my best to keep it short and simple keeping in
mind to use this code in our daily life. This article is based on
‘machinelearningmastry’ blog where I tried to recreate simpler and an
easier version of ‘How to harness the high accuracy predictions with LSTM-
AutoEncoders’
https://www.quora.com/profile/Bob-Rupak-Roy
In each issue we share the best stories from the Data-Driven Investor's expert
community. Take a look.