space-ml-lab · Project P2 · Technical report
The most scientifically valuable objects in a spectroscopic survey are, by definition, those that fit no template. We present a compact, fully local pipeline for unsupervised discovery: a self-supervised autoencoder learns a 16-dimensional representation of real spectra, and anomalies are ranked by combining the reconstruction error with an Isolation Forest on the latent embeddings. The method is designed for SPHEREx (NASA, launched March 2025), whose all-sky near-infrared spectra are a natural target but whose extracted-spectrum catalogues are not yet public; we therefore develop and validate the identical pipeline on 4,990 genuine SDSS DR17 optical spectra as a documented stand-in. Without using labels, the latent space spontaneously organises by object class, and the anomaly ranking is strongly enriched in extreme-emission-line objects: 96% of the top-100 anomalies carry emission-line/broad-line subclasses versus 30.9% of the parent sample — a $3.1\times$ enrichment. All data are real.
Supervised classification can only recover the classes it was trained on. For discovery — rare transients, blends, mis-calibrated sources, or genuinely new spectral types — the interesting objects are precisely those absent from any label set, so a classifier cannot surface them. Unsupervised anomaly detection takes the opposite stance: it models the empirical distribution of "typical" spectra and quantifies deviation from it, an approach that has repeatedly proven productive on spectroscopic surveys [4][3][7]. This is especially timely for SPHEREx, which measures a low-resolution spectrum ($0.75$–$5\,\mu$m, 102 channels) at every point on the sky and whose source catalogues are not yet released — untouched ground for a representation-learning / anomaly layer [1].
The final sample is drawn from SDSS DR17 [5] SpecObj requiring zWarning=0, snMedian>5, sciencePrimary=1; a modular-hash ordering scatters the selection across plates and sub-surveys. Each spectrum is resampled by linear interpolation onto a common grid of 512 points uniform in $\log_{10}\lambda$ over 3850–9000 Å. The cached sample (data/spectra.npz) contains 4,990 spectra spanning $z\in[-0.002, 6.9]$:
| class | count | fraction |
|---|---|---|
| GALAXY | 2,830 | 56.7% |
| STAR | 1,500 | 30.1% |
| QSO | 660 | 13.2% |
Let a spectrum be a flux vector $x\in\mathbb{R}^{D}$ with $D=512$.
To separate spectral shape from brightness, each spectrum is divided by its mean flux, $\tilde{x}=x/\bar{x}$; each wavelength bin is then standardised across the sample,
$$x^{\mathrm{std}}_{ij}=\frac{\tilde{x}_{ij}-\mu_j}{\sigma_j},\qquad \mu_j=\tfrac{1}{N}\sum_i \tilde{x}_{ij},\quad \sigma_j^2=\tfrac{1}{N}\sum_i(\tilde{x}_{ij}-\mu_j)^2.$$An autoencoder $f_\theta=g\circ h$ compresses each standardised spectrum to a latent code $z=h(x)\in\mathbb{R}^{d}$ with $d=16$ and reconstructs $\hat{x}=g(z)$, trained purely self-supervised (no labels) to minimise the mean-squared reconstruction loss
$$\mathcal{L}(\theta)=\frac{1}{N}\sum_{i=1}^{N}\big\lVert x_i-\hat{x}_i\big\rVert_2^2.$$The encoder is $512\to256\to128\to16$ and the decoder its mirror, with ReLU activations; Adam ($\text{lr}=10^{-3}$, weight decay $10^{-5}$), 200 epochs, batch size 256, on the Apple-MPS backend (seconds of wall-clock). Final train / validation MSE: 0.127 / 0.196 (standardised units).
Reconstruction error. $e_i=\tfrac{1}{D}\lVert x_i-\hat{x}_i\rVert_2^2$ — large $e_i$ means the model, trained on the typical population, cannot represent this spectrum (an outlier in data space).
Isolation Forest in latent space [2]. 300 random trees are fit to $\{z_i\}$; anomalies are isolated in fewer splits, i.e. shorter expected path length. The score is
$$s(z)=2^{-\,\mathbb{E}[h(z)]\,/\,c(n)},\qquad c(n)=2H(n-1)-\frac{2(n-1)}{n},$$with $H$ the harmonic number; $s\to1$ indicates a strong anomaly.
Combined ranking. The two independent signals are converted to robust median/MAD $z$-scores and summed, $C_i=Z(e_i)+Z(s(z_i))$ with $Z(v)=(v-\mathrm{median}(v))/(1.4826\,\mathrm{MAD}(v))$. Using one detector in data space and one in latent space makes the ranking robust to the failure modes of either alone.
Emergent class structure (unsupervised). Although no labels were used in training, a t-SNE [6] projection of the 16-D latent space cleanly separates stars, galaxies, and quasars into distinct regions — direct evidence that the autoencoder learned a physically meaningful representation. The flagged anomalies concentrate in the sparse, peripheral regions (the outskirts of the quasar locus and the galaxy–quasar transition), exactly where an anomaly detector should place them.
What the anomalies are. Of the 100 top-ranked anomalies, 68 are galaxies and 32 are quasars, with no ordinary stars; 96 of 100 carry an emission-line or broad-line subclass (STARBURST, STARFORMING, BROADLINE, AGN), versus 30.9% of the full sample — a $3.1\times$ enrichment. This is physically sensible: the bulk population is dominated by smooth absorption/continuum spectra, so the objects most unlike the bulk are those with towering [O III], H$\alpha$, [O II] or broad quasar lines.
| rank | class | subclass | $z$ | recon. err |
|---|---|---|---|---|
| 1 | GALAXY | STARBURST | 0.064 | 5.29 |
| 2 | GALAXY | STARBURST | 0.033 | 3.92 |
| 3 | GALAXY | STARBURST | 0.084 | 4.03 |
| 7 | GALAXY | STARFORMING | 0.013 | 1.60 |
| 12 | QSO | BROADLINE | 0.420 | 0.23 |
Full ranking of all 4,990 sources in outputs/anomaly_ranking.csv.
cd projects/p2-spherex-anomaly
python3 src/fetch_sdss.py # query + download + resample -> data/spectra.npz
python3 src/anomaly_detect_local.py # autoencoder + isolation forest -> figures + ranking
Deterministic (seeded); runs locally on Apple M2 / MPS.