This repo contains my personal implementation of Variational autoencoder
in tensorflow for anomaly detection, that follow
Variational Autoencoder based Anomaly Detection using Reconstruction Probability by Jinwon An, Sungzoon Cho
In order to make work the variational autoencoder for anomaly detection i've to change the last layer of the decoder
from a simple fully connected layer to two layers that estimate mean and variance
of x~ ( p(x|z) )
- Define your dataset into dataset.py and put in output into the function get_dataset
- Eventually change encoder and decoder inside VAE.py to fits your data layout
- Run in a terminal python train.py and specify required at least --input-size (pass -h to see all optional parameters)
- Trained model, parameters and Tensorboard log goes into the folder run/{id} where {id} is an integer from 0 to +inf
- After the model training run tensorboard --logdir=run to check all the training results
Once the model is trained (suppose for simplicity that it is under run/0/ ) just load and predict with this code snippet:
import torch
#load X_test
model = VAEAnomaly(input_size=50, latent_size=32)
# could load input_size and latent_size also
# from run/0/train_config.yaml
model.load_state_dict(torch.load('run/0/model.pth'))
# load saved parameters from a run
outliers = model.is_anomaly(X_test)