Skip to content

Research-feature expansion: tempering, RBM, score/discrepancy losses, ResNet energy, MCMC diagnostics - #36

Merged
davidkhjo merged 22 commits into
mainfrom
research-expansion
Aug 15, 2026
Merged

davidkhjo merged 22 commits into
mainfrom
research-expansion

Conversation

@davidkhjo

Copy link
Copy Markdown
Owner

Research-feature expansion

A big, coherent batch under one theme — trustworthy sampling plus the losses, nets, and metrics that need it. Every feature ships with a closed-form or distributional test (the library's validated-against-ground-truth identity), and runtime stays torch-only.

Samplers

  • ParallelTempering — replica exchange over any base sampler; escapes multimodal traps a single Langevin chain can't. Test: recovers 50/50 mode mass from a fully trapped start; reduces to exact N(0,I) when unimodal.
  • UnderdampedLangevin (SGHMC) — momentum for better mixing, exact exp(-E) position marginal (velocity reset per run). Test: N(0,I) recovery + momentum reset.
  • PreconditionedLangevin — fixed diagonal preconditioner for ill-conditioned targets. Test: recovers per-dim std [1,5] on an anisotropic Gaussian.
  • GibbsSampler — block Gibbs for models with tractable conditionals (the RBM).

Losses

  • ExactScoreMatching (Hyvärinen 2005) — the O(D) exact objective the sliced/denoising variants approximate. Test: recovers Σ⁻¹ from N(0,Σ); matches sliced SM in expectation.
  • EnergyDiscrepancy (Schröder et al. 2023) — sampler-free, score-free. Test: recovers a Gaussian's (μ, s²).

Energies

  • RBM — Bernoulli RBM as a free energy, with block-Gibbs and an exact log_z. Test: free-energy identity vs brute-force marginal (err 3e-8), Gibbs stationarity, CD recovers an independent-Bernoulli target.
  • ResNetEnergy — the IGEBM / Improved-CD residual image net (zero-init blocks, spectral-norm flag). Test: shape, batch independence, identity-at-init, SN training.
  • FunnelEnergy (Neal's funnel) + GaussianMixtureEnergy — closed-form hard targets. Test: energy values vs closed form.

Eval

  • effective_sample_size + split_rhat — MCMC convergence diagnostics. Test: ESS matches the AR(1) closed form; R̂ ≫ 1 on stuck chains.
  • precision_recall (Kynkäänniemi et al. 2019) — fidelity vs coverage. Test: identical→(1,1), disjoint→(0,0), mode-drop→high-P/low-R.
  • inception_score — formula over user-supplied classifier probs (no bundled model). Test: → K and → 1 at the extremes.

Examples

sampling_hard_targets.py, train_rbm.py, train_energy_discrepancy.py (all run on CPU).

Deliberately skipped

NUTS (hard to validate — point to Pyro/BlackJAX), a bundled Inception model (violates torch-only), and cooperative/adversarial training (no closed-form ground truth) — left as documented extension points.

Verification

ruff + ruff format --check + mypy clean; 131 tests pass; uv build + twine check pass at 0.13.0.

🤖 Generated with Claude Code

Neal's funnel and a known-mode Gaussian mixture — closed-form multimodal /
ill-conditioned targets for exercising the new samplers and diagnostics
(the existing toys are all easy 2D).
eval.effective_sample_size (FFT autocovariance + Geyer initial-monotone
truncation) and eval.split_rhat (Gelman–Rubin), the Stan/BDA3 estimators —
so samplers can report whether a chain actually converged, not just its
accept rate. Pure torch.
Wraps any base sampler over a temperature ladder and swaps adjacent replicas
with the detailed-balance rule, returning the T=1 chain. Fixes the mode-mixing
failure single-chain Langevin can't escape; swap_rates() diagnoses the ladder.
nets.RBM returns the closed-form free energy F(v) = -bᵀv - Σ softplus(c+Wv),
so p(v) ∝ exp(-F) trains with the ordinary ContrastiveDivergence loss using
the new GibbsSampler for block-Gibbs negatives. log_z() enumerates the smaller
layer for an exact partition function on small models.
The exact implicit score-matching objective ½‖s‖² + tr(∇s), with the Jacobian
trace computed by D backward passes — the O(D) ground-truth anchor the sliced
and denoising variants approximate at fixed cost.
A sampler-free, score-free objective: contrast each point's energy against M
twice-perturbed copies via logsumexp with w-stabilization. M+1 forward evals,
no MCMC chain to tune, no create_graph.
precision_recall (Kynkäänniemi et al. 2019) separates fidelity from coverage
via k-NN manifolds — mode-drop shows as high precision, low recall.
inception_score ships the formula over user-supplied classifier probs (no
bundled model, stays torch-only).
Pre-activation residual blocks (SiLU, 3x3 convs, no norm), avg-pool
downsampling, global pool + linear head. Second conv is zero-initialized so
each block starts as the identity; spectral_norm=True wraps every layer.
Underdamped adds momentum (better mixing than ULA; velocity reset per run) with
an exact exp(-E) position marginal. Preconditioned applies a fixed diagonal M to
step and noise together — targets exp(-E) exactly and fixes ill-conditioned
mixing MALA/HMC don't.
…pancy

- sampling_hard_targets.py: parallel tempering escapes a trapped mode where a
  single Langevin chain can't, plus ESS / split-R̂ diagnostics.
- train_rbm.py: CD-1 on binary bars with the exact log Z printed.
- train_energy_discrepancy.py: two-moons trained with no MCMC in the loop.
Update the README 'What's in the box' table and examples list, and add the
0.13.0 CHANGELOG entry.
@davidkhjo
davidkhjo merged commit 82124b1 into main Aug 15, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant