Skip to content

Add nets.IsingEnergy and a discrete Ising GWG example - #16

Merged
davidkhjo merged 1 commit into
mainfrom
ising-example
Aug 9, 2026
Merged

davidkhjo merged 1 commit into
mainfrom
ising-example

Conversation

@davidkhjo

Copy link
Copy Markdown
Owner

The GibbsWithGradients / CategoricalGibbsWithGradients samplers are exact and closed-form-validated but had no example — and the library shipped no discrete/lattice energy to run them on. This adds both.

  • nets.IsingEnergy — the library's first discrete/lattice energy: a 2D nearest-neighbor Ising energy E(x) = -J Σ_⟨i,j⟩ s_i s_j for binary (B, H, W) data (spins s = 2x − 1), differentiable in the relaxed input (what GWG needs). Coupling J is a learnable nn.Parameter when learn_coupling=True, a fixed buffer otherwise.
  • examples/train_ising.py — samples a 2D lattice with GibbsWithGradients at three coupling strengths, reproducing the ferromagnetic phase behavior:

ising

Neighbor agreement rises 0.55 → 0.73 → 0.91 as coupling goes 0.1 → 0.4 → 1.0.

Tests

test_ising_energy_orders_with_coupling (agreement increases with J, strong ferromagnet nearly aligned), test_ising_energy_shape_and_learnable_coupling (shape, buffer-vs-parameter, gradient flows to J). Docs: new "Discrete EBMs" section in examples.md, pointer from sampling.md.

🤖 Generated with Claude Code

@cursor

cursor Bot commented Aug 9, 2026

Copy link
Copy Markdown

Bugbot is not enabled for your account, so this pull request was not reviewed.

Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs.

nets.IsingEnergy is the library's first discrete/lattice energy: a 2D
nearest-neighbor Ising energy E(x) = -J sum s_i s_j for binary (B, H, W) data
(spins s = 2x - 1), with an optional learnable coupling. examples/train_ising.py
samples it with GibbsWithGradients at three coupling strengths and shows the
ferromagnetic phase behavior (neighbor agreement 0.55 -> 0.73 -> 0.91).

Tests: agreement rises with coupling, output shape, and gradient flows to a
learnable coupling.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@davidkhjo
davidkhjo merged commit 76c2c56 into main Aug 9, 2026
6 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