Auto-encoders for Compression and Anomaly Detection

Chapter 16, Representation Learning with Auto-encoders

Learning objectives

  • Describe an autoencoder: an encoder that squeezes each sample through a bottleneck of k numbers and a decoder that rebuilds it, trained only to copy its input
  • Show on Ogbon-1's six logs that a linear autoencoder learns PCA's subspace and PCA's error, the variance of the components it drops, but not PCA's axes
  • Measure how far apart two subspaces are by their principal angles
  • Compare a tanh autoencoder's bottleneck with PCA's on the well it learned and on wells it never saw

Six Logs Through a Bottleneck

An autoencoder squeezes each sample's logs through a few numbers, its bottleneck, and rebuilds them; it is trained only to copy its input. Made linear, the best it can do is what PCA does. Squeezed through two numbers, a linear autoencoder rebuilds Ogbon-1's six standardized logs to a mean squared error of 0.0848, PCA's 0.0848, because it learns the plane of PCA's first two components, to within 0.4°, though not PCA's axes: its first column sits 29.7° from PC1. Made nonlinear, with tanh units, it can bend its bottleneck: one tanh unit rebuilds the logs to 0.0300 where one component of PCA leaves 0.328. The figure runs the book's stored networks forward and fits PCA on the page.

Autoencoder Pca SubspaceInteractive figure, enable JavaScript to interact.

The Data

The data are Ogbon-1, the Petrophysics book's well, in the book's own synthetic learning set: its 1201 samples of six logs as the book's own tool model records them, gamma ray (API), density (g/cc), neutron (v/v), sonic (us/ft), log10 RT (log10 ohm.m) and the photoelectric factor (b/e). Each log is standardized on Ogbon-1, so the errors are unitless, in standard deviations squared. Each standardized log has a variance of 1, so the mean squared error over the six is also the share of their variance the bottleneck loses. Every autoencoder here was trained on Ogbon-1 by Adam in the book's kernel and stored in its autoencoder table; nothing is trained on the page.

Linear Is PCA

A linear autoencoder rebuilds x^=W2W1x\hat{x} = W_2 W_1 x (with biases), a map of rank at most kk: whatever it learns, its rebuilt logs lie in a kk-dimensional subspace. The best such subspace, the one that leaves the least squared error, is the span of PCA's first kk components (the theorem of Eckart and Young), and what it leaves is the variance of the components dropped:

Ek=1p∑j>kλj.E_k = \frac{1}{p}\sum_{j>k}\lambda_j.

Ogbon-1's six standardized logs have eigenvalues 4.0345, 1.4568, 0.3479, 0.0868, 0.0392 and 0.0349, so two components leave (0.3479 + 0.0868 + 0.0392 + 0.0349) / 6 = 0.0848. The book's kernel gives the linear autoencoder 0.0848 at two units and 0.328 at one, and at every bottleneck from 1 to 5 it is within 0.5 % of PCA. Gradient descent on the rebuilt logs found the subspace PCA computes in closed form.

The Subspace, Not the Axes

Two subspaces are compared by their principal angles: with orthonormal bases QuQ_u and QvQ_v, the angles are the arccosines of the singular values of Qu⊤QvQ_u^{\top} Q_v, and the largest says how far the spans part. The linear decoder's span lies 0.4° from PCA's plane at two units, 0.3° from PC1 at one, and never more than 0.4° from PCA's span at any bottleneck up to five. Yet at two units its first column lies 29.7° from PC1. Any two independent directions in the plane rebuild the logs equally well, so nothing in the loss prefers PCA's: the network learns the subspace and leaves the basis to its start and its steps. PCA's axes are ordered by the variance each carries; an autoencoder's units are not.

Bending the Bottleneck

A tanh autoencoder 6-8-k-8-6 puts a bent map on each side of its bottleneck. One tanh unit rebuilds Ogbon-1's logs to 0.0300 where one component leaves 0.328, and two units to 0.0199 against 0.0848. Ogbon-1's rocks sit apart in the six logs: shale at high gamma ray, sandstone clean and light, limestone clean, dense and with the highest photoelectric factor. A straight line through three clusters misses two of them; a curve can pass through all three, and one number along it says which rock a sample is and roughly where in it.

On the other eight wells, which it never saw (standardized on Ogbon-1's scaler), one tanh unit still rebuilds the logs to 0.0509 against PCA's 0.340. At four and five units it does not: it rebuilds Ogbon-1 to 0.00914 and 0.00487, better than PCA's 0.0123 and 0.00582, but the other eight wells to 0.0205 and 0.00730, worse than PCA's 0.0137 and 0.00650. With room to spare it learns Ogbon-1's own noise. The error on the well a network learned says how well it copies; the error on wells it never saw says how well it works.

What Two Numbers Drop

PC1 loads gamma ray 0.271, density −0.430, neutron 0.475, sonic 0.479, log10 RT −0.436 and the photoelectric factor −0.311: porous, shaly rock against tight, clean rock. PC2 loads gamma ray 0.672, density 0.379 and the photoelectric factor 0.587, and log10 RT −0.001. Through two numbers the linear autoencoder rebuilds sonic best, its own error 0.0387, and log10 RT worst, 0.233: the two directions that carry the rock leave out much of what resistivity says about the fluid in it, which is what tells oil from water in a sand.

In Python

The same on the learning set's CSV, the book's own file, with a comment header naming every column's unit:

import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from sklearn.neural_network import MLPRegressor

URL = "https://ogbonlab.com/mlgeo-core/data/ogbon-learning-set.csv"
df = pd.read_csv(URL, comment="#")
df["LRT"] = np.log10(df["RT_ohmm"])
LOGS = ["GR_API", "RHOB_gcc", "NPHI_vv", "DT_usft", "LRT", "PEF_be"]

X = df.loc[df["well"] == "Ogbon-1", LOGS].to_numpy()
Z = (X - X.mean(axis=0)) / X.std(axis=0)

PCA's errors from the eigenvalues of the covariance over n:

pca = PCA().fit(Z)
lam = pca.explained_variance_ * (len(Z) - 1) / len(Z)
pca_err = {k: lam[k:].sum() / 6 for k in range(1, 6)}

A linear autoencoder with two units, and the largest principal angle between its decoder's span and PCA's plane:

def mse(net):
    return ((Z - net.predict(Z)) ** 2).mean()

lin = MLPRegressor(hidden_layer_sizes=(2,), activation="identity",
                   solver="adam", learning_rate_init=0.003,
                   batch_size=64, max_iter=1000, random_state=0).fit(Z, Z)
D = lin.coefs_[1]            # the decoder's two directions, as rows
P = pca.components_[:2]
s = np.linalg.svd(np.linalg.qr(D.T)[0].T @ P.T, compute_uv=False)
angle = np.degrees(np.arccos(s.min()))

A tanh autoencoder with one unit:

tanh = MLPRegressor(hidden_layer_sizes=(8, 1, 8), activation="tanh",
                    solver="adam", learning_rate_init=0.01,
                    batch_size=64, max_iter=600, random_state=0).fit(Z, Z)
tanh_err = mse(tanh)

The book's kernel gives PCA 0.328 and 0.0848 at one and two components, and Python's PCA gives the same. The book's kernel gives the linear autoencoder 0.0848 and an angle of 0.4°, and the tanh autoencoder 0.0300 at one unit; scikit-learn's networks start and step their own way (and add a small weight penalty), so their errors may differ from the kernel's in the second figure.

What Comes Next

A network that has learned what a well's logs usually do rebuilds them badly where they do something else. Section 16.2 trains the tanh autoencoder on eight of the nine wells at a time and uses what it cannot rebuild on the ninth to look for faults the book planted in the logs.

This page is prerendered for SEO and accessibility. The interactive widgets above hydrate on JavaScript load.