Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Ignore wiggles, follow the data
A contractive autoencoder is trained so that small changes to the input barely change its code. Reconstruction, on the other hand, needs the code to change when the input really moves to a different data point. Together these make the code follow movement along the data and ignore everything else. The demo shows the penalty for one tanh unit: large where the unit is sensitive, small where it saturates.
Codes that ignore small, irrelevant changes are robust to sensor noise and give a picture of the directions in which the data actually varies, useful for anomaly detection and data augmentation.
- 1. The contractive penalty makes the code…
- 2. Reconstruction keeps the code sensitive to…
- 3. A saturated tanh unit has penalty…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Penalty: λ‖J_f(x)‖²_F, where J = ∂h/∂x is the encoder’s Jacobian (hidden × input).
- Closed form for a tanh layer h = tanh(xW + b):
- J_ji = (1 − h_j²) W_ij;
- ‖J‖²_F = Σ_j (1 − h_j²)² Σ_i W_ij²;
- for sigmoid units, replace (1 − h_j²) with h_j(1 − h_j).
- One unit: h = tanh(wx) has penalty (w(1 − h²))², equal to w² at x = 0 (4 for w = 2) and near zero once the unit saturates.
- Isotropic baseline: an encoder equally sensitive in all D directions puts a fraction k/D of its sensitivity along a k-dimensional surface, 2/20 = 0.1 in exercise 2.
- 1. Penalty of h = tanh(2x) at x = 0:
- 2. ‖[[1, 2], [3, 4]]‖²_F:
- 3. Isotropic tangent share for k = 2, D = 20:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Contractive autoencoders in code
Exercise 1.
- The tanh encoder’s Jacobian matched finite differences.
- The closed-form penalty equalled the squared Frobenius norm of that Jacobian.
- The tangent share of the identity Jacobian along 2 of 5 axes was 0.4.
Practice.
- For one layer, use the closed form. For deep encoders, estimate ‖J‖²_F cheaply with random projections: E‖Jv‖² over random unit vectors v, times D, using Jacobian–vector products (
torch.func.jvp). - Alternatively, approximate it with finite differences on small random perturbations, which is close to denoising.
- Tune λ so reconstruction stays good.
h = torch.tanh(x @ W + b)
jac_sq = ((1 - h**2)**2 * (W**2).sum(0)).sum(1).mean() # closed form for one tanh layer
loss = F.mse_loss(dec(h), x) + lam * jac_sq- 1. For deep encoders, estimate ‖J‖²_F with…
- 2. The sigmoid derivative in terms of output h:
- 3. Too large λ makes…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Contraction versus reconstruction.
- The penalty pushes the code to be constant in every direction.
- Reconstruction requires distinguishing different data points, so the code must vary along the directions in which the data varies: the tangent space of the manifold.
- The compromise keeps a few large Jacobian singular values along the manifold and shrinks the rest.
Relation to denoising.
- For small Gaussian noise of variance σ², the denoising loss expands to the reconstruction loss plus σ² times a Jacobian penalty (of the whole reconstruction map).
- A contractive autoencoder applies this penalty to the encoder analytically, with no sampling.
Using the geometry. The leading right singular vectors of J at a point estimate the local tangent directions of the data. Rifai and colleagues used them to build a “manifold tangent classifier” that is invariant to movements along the manifold.
- 1. The encoder stays sensitive along…
- 2. Small-noise denoising adds approximately…
- 3. Leading right singular vectors of J estimate…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Does the contraction penalty make the encoder follow the data surface?
History.
- S. Rifai, P. Vincent, X. Muller, X. Glorot and Y. Bengio, “Contractive auto-encoders: explicit invariance during feature extraction” (ICML 2011).
- Follow-ups added higher-order penalties and the manifold tangent classifier (NeurIPS 2011).
Exercise 2.
- Data: 300 points on a curved 2-D surface in 20 dimensions, x = tanh(zA), whose tangent directions are known exactly.
- Model: an autoencoder with 32 tanh units, trained for 1000 Adam steps with contraction strength λ = 0, 0.01, 0.1 or 1.
- Measurements: reconstruction error, the mean ‖J‖²_F, and the share of ‖J‖²_F along the 2 tangent directions (0.1 for an isotropic encoder).
| λ | reconstruction error | mean ‖J‖²_F | tangent share |
|---|---|---|---|
| 0 (plain) | 0.0009 | 22.38 | 0.080 |
| 0.01 | 0.0030 | 1.24 | 0.601 |
| 0.1 | 0.0140 | 0.31 | 0.580 |
| 1 | 0.0665 | 0.05 | 0.564 |
- The plain autoencoder is sensitive in every direction, slightly less along the surface than an isotropic encoder would be (0.080 against 0.1). Small off-surface noise therefore changes its code a lot.
- A small penalty reshapes it: at λ = 0.01 the Jacobian shrinks 18 times and 60% of the remaining sensitivity lies along the 2 tangent directions, out of 20. Reconstruction error stays small (0.0030).
- Stronger penalties shrink the Jacobian further but cost reconstruction, with no further gain in tangent share. Seeds 1 and 2 gave tangent shares 0.07 → 0.61 and 0.06 → 0.60 at λ = 0.01.
Contractive autoencoders in AI, robotics and aerospace
- Robust features: codes for control or monitoring that ignore sensor jitter but track real state changes.
- Local geometry: tangent estimates for data augmentation along plausible directions and for invariant classifiers.
- Design lesson: Jacobian penalties are a general tool for smoothness and robustness. They appear again in adversarial robustness and in the regularisation of learned dynamics models.
References
- S. Rifai, P. Vincent, X. Muller, X. Glorot and Y. Bengio (2011). “Contractive auto-encoders: explicit invariance during feature extraction.” Proc. ICML.
- S. Rifai, Y. Dauphin, P. Vincent, Y. Bengio and X. Muller (2011). “The manifold tangent classifier.” NeurIPS.
- G. Alain and Y. Bengio (2014). “What regularized auto-encoders learn from the data-generating distribution.” JMLR 15.