Introduction to LSTM Autoencoder Using Keras

LSTM autoencoder is an encoder that makes use of LSTM encoder-decoder architecture to compress data using an encoder and decode it to retain original structure using a decoder.
LSTM Autoencoder

Simple Neural Network is feed-forward wherein info information ventures just in one direction.i.e. the information passes from input layers to hidden layers finally to the output layers. Recurrent Neural Network is the advanced type to the traditional Neural Network. It makes use of sequential information. Unlike conventional networks, the output and input layers are dependent on each other. RNNs are called recurrent because they play out a similar undertaking for each component of an arrangement, with the yield being relied upon the past calculations.LSTM or Long Short Term Memory are a type of RNNs that is useful in learning order dependence in sequence prediction problems.

In this article, we will cover a simple Long Short Term Memory autoencoder with the help of Keras and python.

What is an LSTM autoencoder?

LSTM autoencoder is an encoder that makes use of LSTM encoder-decoder architecture to compress data using an encoder and decode it to retain original structure using a decoder.

About the dataset

The dataset can be downloaded from the following link. It gives the daily closing price of the S&P index.

Code Implementation With Keras

Import libraries required for this project

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt

Read the data

df = pd.read_csv('spx.csv', parse_dates=['date'], index_col='date')

Split the data

train_size = int(len(df) * 0.9)
test_size = len(df) - train_size
train, test = df.iloc[0:train_size], df.iloc[train_size:len(df)]

Pre-Processing of Data

We need to pre-process the training and test data using the standardscaler library imported from sklearn.

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler =[['close']])
train['close'] = scaler.transform(train[['close']])
test['close'] = scaler.transform(test[['close']])

Create a sequence with historical data

Now we will split the time series data into subsequences and create a sequence of 30 days of historical data.

def create_dataset(X, y, time_steps=1):
    X1, y1 = [], []
    for i in range(len(X) - time_steps):
        t = X.iloc[i:(i + time_steps)].values
        y1.append(y.iloc[i + time_steps])
    return np.array(X1), np.array(y1)
X_train, y_train = create_dataset(
X_test, y_test = create_dataset(

Creating an LSTM Autoencoder Network

The architecture will produce the same sequence as given as input. It will take the sequence data. The dropout removes inputs to a layer to reduce overfitting. Adding RepeatVector to the layer means it repeats the input n number of times. The TimeDistibuted layer takes the information from the previous layer and creates a vector with a length of the output layers.

import keras
model = keras.Sequential()
    input_shape=(X_train.shape[1], X_train.shape[2])
model.add(keras.layers.LSTM(units=64, return_sequences=True))
model.compile(loss='mae', optimizer='adam')

Fitting the Model

Here, we train the model with epoch:20 and batch size 32.

history =
    X_train, y_train,


plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='test')

From the above plot we can see the training and test error is decreasing. For better result, we can train the model with more epochs.

Actual Value of Test Data


Prediction on Test Data

pred = model.predict(X_test, verbose=0)


In this article, we have covered the basics of Long-short Term Memory autoencoder by using Keras library. Comparing the prediction result and the actual value we can tell our model performs decently. Further, we can tune this model by increasing the epochs to get better results.The complete code of the above implementation is available at the AIM’s GitHub repository. Please visit this link to find the notebook of this code.

Download our Mobile App

Ankit Das
A data analyst with expertise in statistical analysis, data visualization ready to serve the industry using various analytical platforms. I look forward to having in-depth knowledge of machine learning and data science. Outside work, you can find me as a fun-loving person with hobbies such as sports and music.

Subscribe to our newsletter

Join our editors every weekday evening as they steer you through the most significant news of the day.
Your newsletter subscriptions are subject to AIM Privacy Policy and Terms and Conditions.

Our Recent Stories

Our Upcoming Events

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox

6 IDEs Built for Rust

Rust IDEs aid efficient code development by offering features like code completion, syntax highlighting, linting, debugging tools, and code refactoring

Can OpenAI Save SoftBank? 

After a tumultuous investment spree with significant losses, will SoftBank’s plans to invest in OpenAI and other AI companies provide the boost it needs?

Oracle’s Grand Multicloud Gamble

“Cloud Should be Open,” says Larry at Oracle CloudWorld 2023, Las Vegas, recollecting his discussions with Microsoft chief Satya Nadella last week.