• Stars
    star
    428
  • Rank 101,481 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 8 years ago
  • Updated about 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Auto encoder for time series

Auto encoder for time series

EDIT 3 December 2018, I receive many questions over email. I compiled the most common questions into a FAQ at the end of this readme

This repo presents a simple auto encoder for time series. It visualizes the embeddings using both PCA and tSNE. I show this on a dataset of 5000 ECG's. The model doesn't use the labels during training. Yet, the produced clusters visually separate the classes of ECG's.

People repeatedly ask me how to find patterns in time series using ML. The usual wavelet transforms and other features fail to yield results. They wonder what ML has to offer.

  • For categorical data, a usual choice are techniques like PCA, tSNE.
  • For images, a usual choice is convolutional auto encoders
  • For time series, what is the usual choice?
    • This repo implements a recurrent auto encoder

Why use a Recurrent Neural Network in an auto encoder?

  • The length of time series may vary from sample to sample. Conventional techniques only work on inputs of fixed size.
  • The patterns in time series can have arbitrary time span and be non stationary. The recurrent neural network can learn patterns in arbitrary time scaling. The convolutional net, however, assumes only stationary patterns

The network

From here on, RNN refers to our Recurrent Neural Network architecture, the Long Short-term memory Our network in AE_ts_model.py has four main blocks

  • The encoder is a RNN that takes a sequence of input vectors
  • The encoder to latent vector is a linear layer that maps the final hidden vector of the RNN to a latent vector
  • The latent vector to decoder is a linear layer that maps the latent vector to the input vector for the decoder
  • The decoder is a RNN that takes this single input vector and maps to a sequence of output vectors

Training Objective

An auto encoder learns the identity function, so the sequence of input and output vectors must be similar. In our case, we take a probabilistic approach. Every output is a tuple of a mean, mu and standard deviation. Let this mu and sigma parametrize a Gaussian distribution. Now we minimize the log-likelihood of the input under this distribution. We train this using backpropagation into the weights of the encoder, decoder and linear layers.

Example data

I showcase the recurrent auto encoder on a dataset of 5000 ECG's. Accurately named ECG5000 on the UCR archive. I choose ECG, because humans understand them easily. Yet, their complexity remains challenging enough for a machine learning model.

Here are some examples, each column represents another input class examples

Results

We run the recurrent auto encoder with a 20D latent space. The following figure plots the latent vectors with both PCA and tSNE. latent_vectors

This figure shows that the latent space exhibits structure. We color the vectors with their corresponding labels. The light blue and dark blue labels obviously cluster in different parts of the space. Interestingly, the lower left corner in the tSNE shows another cluster of orange points. That might be interesting for doctors to look at. (Note that the class distributions are highly unbalanced. The orange and greeen colored data occur less frequently)

Conclusion

We present an auto encoder that learns structure in the time-series. Training is unsupervised. When we color the latent vectors with the actual labels, we show that the structure makes sense.

FAQ

To my great joy, I receive many questions and suggestions over email. I compiled some of the commonly asked questions so you can get started quickly

  • How can I use the representations for other purposes than visualization? After training, you can fetch the representations by running sess.run(model.z_mu, feed_dict=my_feed_dict)

  • I get an import error. What versions do you use? See the docs/requirements.txt file for all versions

  • How could I extract classes from the representations? The auto-encoder framework belongs to unsupervised learning. Hence, classes will only follow from some sort of clustering. You can apply a clustering model to the hidden representations. Or you could implement another model that naturally clusters time series, for example neural expectation maximization or simply HMM's. Moreover, if you do have supervision for your data, then I recommend you to use supervised model. For example, a linear classification model, linear dynamical systems or a normal recurrent/convolutional neural network.

  • The loss function on the latent space resembles the VAE loss function. How does your model differ from the VAE? For clarity, this question usually refers to the loss in tf.reduce_mean(tf.square(lat_mean) + lat_var - tf.log(lat_var) - 1). I see two immediate differences with the VAE

    	* The VAE follows from amortized inference on a latent variable model. All terms in the VAE model have a probabilistic interpretation. In contrast, our auto encoder learns according to the maximum likelihood principle. We implement this loss functions only to improve our visualization.
    	* The VAE penalizes the KL divergence with the prior for each representation. In contrast, we penalize the KL divergence with the marginal distribution on the representations. In other words, the VAE *wants* each representation to have zero mean and unit variance; our auto encoder want all representations marginally to have zero mean and unit variance. 
    

Please let me know if I forgot your questions in this FAQ section

As always, I am curious to any comments and questions. Reach me at [email protected]

More Repositories

1

LSTM_tsc

An LSTM for time-series classification
Python
407
star
2

weight_uncertainty

Implementing Bayes by Backprop
Python
179
star
3

CNN_tsc

A CNN for time-series classification
Python
155
star
4

RNN_basketball

LSTM + MDN for basketball trajectories
Python
142
star
5

bigclam

Implements the bigCLAM algorithm
Python
50
star
6

ssl_graph

Semi supervised learning on graphs
Python
35
star
7

bayes_nn

Uncertainty interpretations of the neural network
Python
30
star
8

cnn_music

A CNN for music genre classification and TSC in general
MATLAB
30
star
9

bandit

Implementation of Counterfactual risk minimization
Python
26
star
10

segm

Simple Semantic Segmentation
Python
17
star
11

VAE_rec

Variational Recurrent Auto Encoder
Python
16
star
12

EM

visualizing Expectation Maximization
Python
16
star
13

vi_normal

Variational Inference for a Normal Distribution
Python
12
star
14

tensorflow_basic

Basic start of TensorFlow with TensorBoard
Python
11
star
15

EDS

Github Repo for Eindhoven Data Science MeetUp
11
star
16

dan

Domain Agnostic Normalization layer for Unsupervised Domain Adaptation
Python
11
star
17

attention

An elementary example of soft attention
Python
8
star
18

ts_clust

WIP to cluster time series
Python
7
star
19

hypothesis_kalman

Hypothesis testing for time series: two buckets are from same source or different?
Python
7
star
20

ladder

LADDER network after Harri Valpola
Python
7
star
21

LSTM_cpd

LSTM for change point detection
Python
7
star
22

dpm

Dirichlet Process Mixtures
Python
6
star
23

ssl

Implementation of self ensembling for semi supervised learning
Python
6
star
24

overview

Overview diagram and lists of Machine Learning
HTML
6
star
25

awesome_distributions

Scribbles about marginalization property of Gaussian and aggregation property of Dirichlet
Python
6
star
26

cs231n

Files for the cs231n course at Stanford
Jupyter Notebook
5
star
27

FCN

Fully Convolutional Network
Python
4
star
28

rbm

Restricted Boltzmann Machine
Python
3
star
29

occam

Implementing Occam's razor from the Statistical and Bayesian views.
Python
3
star
30

indian_buffet

Indian buffet latent variable model
Python
3
star
31

RAM

Recurrent Attention Model
Python
3
star
32

VAE

Variational Auto encoder
Python
3
star
33

DRAW

DRAW
Python
3
star
34

rbfn_learnable

RBFN with learnable RBF parameters
Python
2
star
35

ssl_rep

Combining insights from representation learning and semi supervised learning
Python
2
star
36

bbvi

Comparing variance of gradient estimators for BBVI
Python
2
star
37

q_learning

Implementation of epsilon-greedy q-learning for robot on 2D grid
MATLAB
2
star
38

mcmc_proposals

Compare different proposal distro's for MCMC
Python
2
star
39

hypothesis_testing

Project on hypothesis testing
Python
2
star
40

bayesian_model_comparison

Three approaches for Bayesian model comparison
Python
2
star
41

RBFN_two_MNIST

Radial basis function network for two classes of the MNIST dataset
MATLAB
1
star
42

far_away

Exploring linear classifiers on inputs far away from the training data
Python
1
star
43

dpbmm

Dirichlet Process Mixture model for Multinoulli distributions
Python
1
star
44

hacking_180824

hacking_180824
Python
1
star
45

trees_ensemble

This projects aggregates a Bagging, Random Forest and MLP classifier for data competition
Python
1
star
46

talks

repo for my talks
1
star