Path: blob/master/examples/timeseries/ipynb/timeseries_anomaly_detection.ipynb
3236 views
Timeseries anomaly detection using an Autoencoder
Author: pavithrasv
Date created: 2020/05/31
Last modified: 2020/05/31
Description: Detect anomalies in a timeseries using an Autoencoder.
Introduction
This script demonstrates how you can use a reconstruction convolutional autoencoder model to detect anomalies in timeseries data.
Setup
Load the data
We will use the Numenta Anomaly Benchmark(NAB) dataset. It provides artificial timeseries data containing labeled anomalous periods of behavior. Data are ordered, timestamped, single-valued metrics.
We will use the art_daily_small_noise.csv
file for training and the art_daily_jumpsup.csv
file for testing. The simplicity of this dataset allows us to demonstrate anomaly detection effectively.
Quick look at the data
Visualize the data
Timeseries data without anomalies
We will use the following data for training.
Timeseries data with anomalies
We will use the following data for testing and see if the sudden jump up in the data is detected as an anomaly.
Prepare training data
Get data values from the training timeseries data file and normalize the value
data. We have a value
for every 5 mins for 14 days.
24 * 60 / 5 = 288 timesteps per day
288 * 14 = 4032 data points in total
Create sequences
Create sequences combining TIME_STEPS
contiguous data values from the training data.
Build a model
We will build a convolutional reconstruction autoencoder model. The model will take input of shape (batch_size, sequence_length, num_features)
and return output of the same shape. In this case, sequence_length
is 288 and num_features
is 1.
Train the model
Please note that we are using x_train
as both the input and the target since this is a reconstruction model.
Let's plot training and validation loss to see how the training went.
Detecting anomalies
We will detect anomalies by determining how well our model can reconstruct the input data.
Find MAE loss on training samples.
Find max MAE loss value. This is the worst our model has performed trying to reconstruct a sample. We will make this the
threshold
for anomaly detection.If the reconstruction loss for a sample is greater than this
threshold
value then we can infer that the model is seeing a pattern that it isn't familiar with. We will label this sample as ananomaly
.
Compare recontruction
Just for fun, let's see how our model has recontructed the first sample. This is the 288 timesteps from day 1 of our training dataset.
Prepare test data
Plot anomalies
We now know the samples of the data which are anomalies. With this, we will find the corresponding timestamps
from the original test data. We will be using the following method to do that:
Let's say time_steps = 3 and we have 10 training values. Our x_train
will look like this:
0, 1, 2
1, 2, 3
2, 3, 4
3, 4, 5
4, 5, 6
5, 6, 7
6, 7, 8
7, 8, 9
All except the initial and the final time_steps-1 data values, will appear in time_steps
number of samples. So, if we know that the samples [(3, 4, 5), (4, 5, 6), (5, 6, 7)] are anomalies, we can say that the data point 5 is an anomaly.
Let's overlay the anomalies on the original test data plot.