A variational autoencoder (VAE) is a generative encoder–decoder model. An ordinary autoencoder compresses an input through a bottleneck and reconstructs it; a VAE instead encodes the input as parameters of a distribution, samples a latent value, and decodes that sample. Training also structures the latent space so sampled points can produce valid outputs. section 11.1
Figure 11.2 Variational autoencoder architecture, showing an encoder, a sampling layer, and a decoder
The architecture diagram locates the sampling layer between the encoder and decoder. section 11.1
Let x be an observation and z a latent variable. The trainable encoder qϕ(z|x) approximates a posterior distribution; the trainable decoder pθ(x|z) describes reconstructions; and p(z) is the latent prior. The evidence lower bound (ELBO) balances reconstruction from sampled z against a KL-divergence penalty that draws the approximate posterior toward the prior. Thus, optimizing reconstruction alone is not the VAE objective. section 11.1 section 11.1.1
Time-series anomaly detection
Train an LSTM-VAE on nonanomalous sensor signals. For each of n sensors, a Gaussian output uses a mean and variance, giving 2n output parameters. The sampled joint latent representation is encouraged to approximate a standard normal distribution. At detection time, a drop in an input’s log likelihood under the learned normal behavior can be thresholded as an anomaly; a square-wave signal illustrates the result. The detector and its custom loss are implemented from scratch with Keras/TensorFlow using the NAB dataset. section 11.1.1












