Skip to main content

Autoencoder — Non-linear Dimensionality Reduction

This page documents the results of the SpectralAutoencoder neural autoencoder applied to the 183 spectroscopic features of N = 43,019 LAMOST DR5 × Gaia DR3 spectra. The autoencoder is the third dimensionality reduction method studied in the PHY-3500 project, complementing PCA (linear) and UMAP/t-SNE (non-parametric). Its role is to provide a non-linear parametric representation: it learns a compression and can project new spectra without full recomputation.

PHY-3500 article reference

The autoencoder is mentioned in the Baker, Caillat & Jean article (28 April 2026) but its detailed results are not developed in the main text — the reader is referred to this documentation page. Key results: MSE(AE, z=2) = 0.489 ≈ PCA(k=10), and the discovery that latent axis 1 encodes gravity rather than temperature.


Key results​

MSE
0.489
Reconstruction MSE
autoencoder z = 2 · N = 43,019
vs PCA(z=2) = 0.696 · −29.8%
z=2
≈ PCA@10
Compression equivalence
MSE(AE, z=2) ≈ MSE(PCA, k=10)
5× fewer dimensions than PCA
ep.
145
Training epochs
Best val loss = 0.548 · 143.7 s
Early stopping
⚡
4.2×
QSO vs star error
MSE(QSO) / MSE(STAR) median
Unsupervised anomaly detection

Why an autoencoder?​

Previous methods have complementary limitations:

MethodLinear?Parametric?Main advantageLimitation
PCA✓✓Interpretable loadings · reproducibleBlind to non-linear relationships
UMAP / t-SNE✗PartialTopological structure · clustersNo exact transform() for new points
Autoencoder✗✓Non-linear + parametricBlack box · training cost

The autoencoder is non-linear (it can capture structures that PCA flattens) and parametric (once trained, encoding a new spectrum is a simple forward pass). This is the ideal combination for a production pipeline.


Architecture​

SpectralAutoencoder Architecture
Symmetric MLP · BatchNorm + Dropout(0.1) · Adam lr=1e-3
Encoder
Latent space z=2
Decoder
← ENCODER
DECODER →
183
Input X
›
256
Dense 256
›
128
Dense 128
›
64
Dense 64
›
2
z (latent)
⇌
64
Dense 64
›
128
Dense 128
›
256
Dense 256
›
183
Output X̂
z (latent) — dim 2
Linear — 2D latent space
Compression ratio
183 → 2
91.5× (vs 91 for PCA@95%)
Total parameters
~330k
Batch size
512
Optimiser
Adam · lr=1e-3
Loss
MSE (reconstruction)
Early stopping
patience=15 · val_loss
Device
CPU (Ryzen 9 5950X)

Justification of architecture choices​

Symmetric MLP (183 → 256 → 128 → 64 → 2 → 64 → 128 → 256 → 183): The initial expansion to 256 (> 183) is not an error — it allows the network to create richer intermediate representations before compression. This is common practice for autoencoders on spectral data (Portillo et al. 2020, AJ 160:45).

BatchNorm + Dropout(p=0.1): BatchNorm stabilises gradients and accelerates convergence. Light dropout (0.1) avoids overfitting without degrading reconstruction — stellar spectra are regular enough that stronger dropout is counterproductive.

Latent dimension z = 2: Deliberate choice for 2D visualisation and fair comparison with PCA(k=2) and UMAP(2D). A z = 8 or 16 would achieve much lower MSE but would lose visual readability.

from src.dimred.autoencoder import SpectralAutoencoder

# Training
ae = SpectralAutoencoder(input_dim=183, latent_dim=2, hidden_dims=[256, 128, 64])
history = ae.fit(X_train_scaled, epochs=200, lr=1e-3, batch_size=512, patience=15)

# Encode 43,019 spectra → 2D latent space
Z_ae = ae.encode(X_scaled) # shape: (43019, 2)

# Reconstruction
X_recon = ae.decode(Z_ae) # shape: (43019, 183)
mse = ae.reconstruction_mse(X_scaled) # → 0.489

Reconstruction — Autoencoder vs PCA​

MSE(k) benchmark​

Reconstruction MSE: Autoencoder (z=2) vs PCA (k=1…50)
PHY-3500 · G1 · 43,019 spectra · 183 features · run 20260405T220839Z
PCA (k components)
AE (z = 2 fixed)
0.20.30.40.50.60.70.8AE(z=2) = 0.489PCA(k=10)≈AE12510203050PCA components K →MSE
AE(z=2) ≈ PCA(k=10) — with only 2 latent dimensions, the autoencoder achieves the same MSE as PCA with 10 components. Non-linearity compensates for dimensionality.
Limitation: PCA(k=50) reaches MSE = 0.196, well below AE(z=2). PCA benefits from its 50 degrees of freedom — the 2D autoencoder is constrained by its low latent dimensionality.

Reconstruction error by object type​

The per-class breakdown reveals an important astrophysical result:

ClassNMean MSEMedian MSEp95Factor vs stars
STAR42,9560.4850.2270.9741× (reference)
GALAXY563.0091.1703.043~5×
QSO75.7940.95418.637≈ 4.2× (median) · ≈ 11.9× (mean)
Unsupervised anomaly detection

With only 7 QSOs in the sample, the ratios are fragile. QSOs (quasi-stellar objects) have a median reconstruction error about 4.2× that of stars (0.954 / 0.227) and a mean error about 11.9× (5.794 / 0.485). The autoencoder, trained almost exclusively on stars (99.85% of the sample), has never learned the spectral structure of QSOs — their broad emission lines (Ly-α, CIV) are radically different. This result suggests that reconstruction error can serve as an anomaly score: a spectrum with MSE >> 1 is likely non-stellar.


Latent space and physical interpretation​

Spearman correlations with Gaia DR3​

🔄
Discovery — the autoencoder encodes gravity on its first axis
Unlike PCA (PC1 = temperature, ρ = +0.831), the autoencoder's first latent axis is weakly correlated with T_eff (ρ = +0.019) but moderately with log g (ρ = +0.340). Axis 2 encodes temperature (ρ = +0.793). This inversion suggests that the non-linear structure of gravity is more salient in the network's learned space.
Autoencoder (z = 2)
AE axis 1
Discovery
Surface gravity
Autoencoder
Teff (K)
+0.019
log g (dex)
+0.340
[Fe/H] (dex)
-0.224
G_BP−G_RP
-0.320
AE axis 2
Effective temperature
Autoencoder
Teff (K)
+0.793
log g (dex)
-0.112
[Fe/H] (dex)
-0.542
G_BP−G_RP
-0.770
PCA — linear baseline
PC1
Effective temperature
PCA
Teff (K)
+0.831
log g (dex)
-0.155
[Fe/H] (dex)
-0.548
G_BP−G_RP
-0.768
PC2
Mixed (weak signal)
PCA
Teff (K)
+0.156
log g (dex)
+0.081
[Fe/H] (dex)
-0.082
G_BP−G_RP
-0.025
Method / axis
ρ(Teff)
ρ(log g)
ρ([Fe/H])
ρ(BP−RP)
Interpretation
AE axis 1
+0.019
+0.340
-0.224
-0.320
← Gravity
AE axis 2
+0.793
-0.112
-0.542
-0.770
Thermal
PCA — PC1
+0.831
-0.155
-0.548
-0.768
← Thermal
PCA — PC2
+0.156
+0.081
-0.082
-0.025
Weak / Mixed

Latent space organisation​

The latent space grid (ae_latent_grid.png) reveals a physically coherent organisation:

  • Axis 2 (thermal, ρ = +0.793 with Teff) deploys the M-K-G-F-A sequence from bottom to top
  • Axis 1 (gravity, ρ = +0.340 with log g) separates dwarfs and giants
  • Galaxies and QSOs are pushed far from the main stellar mass — their high reconstruction error corresponds to their distance in latent space

This result is an independent confirmation of the HDBSCAN discovery: the dwarf/giant distinction is a genuine physical dimension of spectral space.


Latent space interpolation​

The parametric autoencoder allows linear interpolation between two points in latent space and decoding intermediate positions into physical spectra:

# Interpolation between a cool star (K) and a hot star (A)
# idx_cold: Teff = 4,200 K · idx_hot: Teff = 7,984 K
# Latent distance = 11.44 units

t = np.linspace(0, 1, 10)
Z_interp = np.outer(1-t, Z_ae[idx_cold]) + np.outer(t, Z_ae[idx_hot])
X_interp = ae.decode(Z_interp)
# → 10 synthetic intermediate spectra between 4,200 and 7,984 K

The interpolation produces a continuous and physically plausible transition: Balmer lines progressively weaken and Ca II lines strengthen. The latent distance of 11.44 units for a Teff variation of ~3,800 K confirms that axis 2 linearly encodes temperature.


Latent arithmetic — Stellar analogies​

Following the word2vec idea (Mikolov et al. 2013), does the latent space support analogies of the type K_giant − K_dwarf + G_dwarf ≈ G_giant?

# K_giant − K_dwarf + G_dwarf = ?
z_pred = z_K_giant - z_K_dwarf + z_G_dwarf
z_target = z_G_giant

distance = np.linalg.norm(z_pred - z_target)
# → distance = 2.1 (= ~18% of total range) — FUNCTIONAL ANALOGY

The result indicates that the latent space encodes the dwarf↔giant direction consistently across spectral types — suggesting the network has learned a representation that partially factorises (temperature, gravity) in a measurable way.


Comparative synthesis — PCA · UMAP · Autoencoder​

CriterionPCA (z=2)AE (z=2)UMAP (2D)
Reconstruction MSE (2 dim)0.6960.489N/A
Axis 1 / Teff correlation+0.831+0.019+0.464
Dominant physical axisTemperatureGravityTemperature
Non-linear✗✓✓
Parametric (transform)✓✓Partial
Interpretable✓ (loadings)~ (correlations)✗
Anomaly detection✓ (high MSE)✓ (QSO median MSE ≈ 4× stars, N = 7)✗ directly

Reference figures​

◎
2D latent space — full grid
Latent
2D latent space — full grid
AE axis 1 (gravity) × axis 2 (T_eff) — 43,019 spectra coloured by type
◎
2D latent space — zoom
Latent
2D latent space — zoom
Zoom on the main stellar mass — dwarf/giant distinction
◎
Learning curve
Training
Learning curve
Train loss vs val loss · 145 epochs · best val = 0.548
◎
MSE: AE(z=2) vs PCA(k=1…50)
Benchmark
MSE: AE(z=2) vs PCA(k=1…50)
AE(z=2) ≈ PCA(k=10) · decreasing PCA curve
◎
MSE distribution (log scale)
Anomalies
MSE distribution (log scale)
STAR vs GALAXY vs QSO — clear separation at MSE > 1
◎
Error by spectral family
Reconstruction
Error by spectral family
Which feature family is poorly reconstructed?
◎
Reconstruction examples
Reconstruction
Reconstruction examples
Original vs reconstructed spectrum — 6 examples (A, F, G, K, M, QSO)
◎
Median MSE by spectral type
Reconstruction
Median MSE by spectral type
M and K best reconstructed · A least well
◎
Latent interpolation K → A
Interpolation
Latent interpolation K → A
Teff: 4,200 K → 7,984 K · latent distance = 11.44 · 10 steps
◎
Latent arithmetic
Arithmetic
Latent arithmetic
K_giant − K_dwarf + G_dwarf ≈ G_giant · stellar word2vec
◎
KDE density of latent space
Latent
KDE density of latent space
Distribution of 43,019 points in the (z1, z2) space
◎
Synthesis PCA · UMAP · AE
Synthesis
Synthesis PCA · UMAP · AE
The 3 latent spaces side by side — same data coloured by T_eff

Diagnostic sheets — typical candidates​

The autoencoder allows producing a diagnostic sheet for each spectrum: position in latent space, original vs reconstructed spectrum, residual, and MSE. Three characteristic examples:

◎
Sheet: Typical A-type star
Type A
Sheet: Typical A-type star
Hot star > 7,500 K · broad Balmer lines · moderate MSE (narrow lines)
◎
Sheet: G dwarf
Type G
Sheet: G dwarf
Solar-type star 5,500 K · Ca II + Balmer · low MSE (excellent reconstruction)
◎
Sheet: K giant
K giant
Sheet: K giant
log g ≈ 2.5 · very strong Ca II · low MSE · well localised on gravity axis

Implementation​

Code is in src/dimred/autoencoder.py (class SpectralAutoencoder) and the complete notebook in phy3500_03_autoencoder.ipynb.

# Load saved model
import torch
from src.dimred.autoencoder import SpectralAutoencoder

ae = SpectralAutoencoder(input_dim=183, latent_dim=2)
ae._model = torch.load("data/reports/models/ae_latent2.pt")
ae._model.eval()

# Encode a new spectrum
X_new_scaled = scaler.transform(X_new) # same scaler as at training
Z_new = ae.encode(torch.tensor(X_new_scaled, dtype=torch.float32).numpy())