LSTM Autoencoder for Anamoly Detection

Adnan Karol
Analytics Vidhya
Published in
3 min readOct 14, 2020

Autoencoders offer a range of applications, including:
- Noise Reduction
- Feature Extraction (utilizing only the encoder part)
- Anomaly Detection

In this guide, we focus on anomaly detection using time-series data. Autoencoders, which can be implemented with various neural network architectures such as DNN, LSTM, or RNN, are particularly useful for this purpose. For our time-series data, we’ll employ an LSTM Autoencoder.

The process is straightforward: we first train the autoencoder using normal, non-anomalous data. This allows the model to learn to accurately reconstruct known input sequences. When we then introduce sequences with anomalies, the autoencoder struggles to reconstruct these inputs accurately, resulting in higher reconstruction errors. This discrepancy highlights the presence of anomalies within the data.

For additional details on structuring the input, model, and output, please refer to my previous blog. This guide serves as a continuation of that discussion. We will evaluate the model’s performance by examining the Mean Squared Error (MSE) between the reconstructed output and the original input.

Let us first test the model with same input sequence

MSE of Output with Input is very low i.e. NO ANOMALY

The model is able to recreate the input with some error. Let us give a completely new sequence and check the output

Now Let us input a sequence that is close enough to input the model is trained with and check the MSE.

The MSE is low as the Output is a close approximation with the Input

Finally, let's input a sequence with an anomaly i.e. with something that is not desirable.

The MSE is high i.e. the Input in this case is something the model has not seen, Hence ANOMALY

Hence Using Autoencoder and the concept of Errors we can check for Anomalies in Data.

Summary

By diving into the practical implementation of LSTM Autoencoders, you’ll gain valuable hands-on experience with anomaly detection techniques. If you have any questions or need further assistance, feel free to explore the resources provided or reach out to the community. Happy coding!

GitHub Repository: https://github.com/adnankarol/LSTM-Autoencoders-Demo

--

--