NCSNForImageGeneration
PretrainedModelNCSNForImageGeneration(config: NCSNConfig)NCSN with the DSM training loss and annealed Langevin .generate().
Wraps NCSNModel with denoising score matching (DSM) for
training and annealed Langevin dynamics for inference. Sampling does
not reuse DiffusionMixin.generate because NCSN's nested
(per- x per-step) loop has different semantics from the
DDPM scheduler step.
Training contract
forward(x) samples a random noise-level index per image,
perturbs x accordingly, runs the score network, and returns
the DSM loss (Song 2019 Eq. 6).
Sampling
generate(n_samples) runs annealed Langevin dynamics across the
full schedule (Song 2019 Algorithm 1).
Parameters
configNCSNConfigAttributes
Notes
Reference: Song and Ermon, "Generative Modeling by Estimating Gradients of the Data Distribution", NeurIPS, 2019 (arXiv:1907.05600); NCSNv2 refinements in Song and Ermon, 2020 (arXiv:2006.09011).
Denoising score matching loss:
with , .
Examples
>>> import lucid
>>> from lucid.models.generative.ncsn import (
... NCSNConfig, NCSNForImageGeneration,
... )
>>> cfg = NCSNConfig(sample_size=32, base_channels=32,
... channel_mult=(1, 2), num_res_blocks=1,
... resnet_groups=16, num_noise_levels=10,
... langevin_steps=2)
>>> model = NCSNForImageGeneration(cfg).eval()
>>> x = lucid.randn((1, 3, 32, 32))
>>> out = model(x)
>>> out.sample.shape, out.loss.shape # score field + scalar loss
((1, 3, 32, 32), ())Used by 2
Constructors
1Instance methods
2forward(sample: Tensor)Denoising Score Matching loss (Song 2019 Eq. 6).
For each image we sample i ~ Uniform({0, …, L-1}), perturb
x̃ = x + σ_i · z with z ~ N(0, I), and minimise
L = ½ · E ‖ σ_i · s_θ(x̃, i) + z ‖²
which is the σ-weighted DSM loss.
generate(n_samples: int = 1, langevin_steps: int | None = None, return_intermediates: bool = False, device: str = 'cpu')Annealed Langevin dynamics — Song 2019 Algorithm 1.
Initialises x ~ N(0, σ_max² I) then iterates over the σ
schedule from largest to smallest noise. At each σ_i takes
langevin_steps Langevin steps with step size
α_i = ε · σ_i² / σ_L²:
x ← x + (α_i / 2) · s_θ(x, i) + √α_i · z z ~ N(0, I)
Parameters
n_samplesint= 1langevin_stepsint | None= Noneconfig.langevin_steps for this
call (typically lowered in unit tests).return_intermediatesbool= Falsedevicestr= 'cpu'Returns
GenerationOutputclass:GenerationOutput with the final (n_samples, C, H, W)