Skip to content
class

ScoreSDEForImageGeneration

extendsImageGenerationModel
ScoreSDEForImageGeneration(config: ScoreSDEConfig)

Score-SDE with its objective and the three samplers.

Parameters

Frozen configuration.

Notes

Reference: Song et al., ICLR 2021 (arXiv:2011.13456).

Trained by denoising score matching, equation 7, in the form that weights each time by σ(t)2\sigma(t)^2 — which makes the loss the plain mean squared error between the predicted and the actual noise, since the σ\sigma in the score and the σ2\sigma^2 in the weight cancel to one. That is the same objective DDPM optimises, which is the paper's point.

Three ways to sample, all from one trained network:

"euler" Integrate the reverse-time SDE, equation 6. "pc" The same, with Langevin corrector steps at fixed tt. The paper's Predictor-Corrector. "ode" The probability-flow ODE, equation 13, handed to lucid.diffeq.odeint.

Examples

>>> import lucid
>>> from lucid.models import score_sde_vp_gen
>>> model = score_sde_vp_gen(sample_size=8, base_channels=8,
...     channel_mult=(1,), num_res_blocks=1, resnet_groups=4,
...     attention_resolutions=()).eval()
>>> out = model(lucid.randn((2, 3, 8, 8)))
>>> bool(out.loss.ndim == 0)
True

Used by 2

Constructors

1
dunder

__init__

→None
__init__(config: ScoreSDEConfig)

Properties

1

Instance methods

3
fn

generate

→GenerationOutput
generate(n_samples: int = 1, method: str = 'pc', steps: int | None = None, device: str | None = None, guidance: _Guidance | None = None)

Draw samples by integrating backwards from the prior.

Parameters

n_samplesint= 1
How many.
method(pc, euler, ode)= "pc"
"euler" integrates the reverse SDE, "pc" adds Langevin correction, "ode" solves the probability-flow ODE with lucid.diffeq.odeint.
stepsint or None= None
Solver steps; None uses num_scales. That this is a sampling argument rather than a property of the trained model is the framework's point.
devicestr or None= None
Where to sample; None follows the parameters.
guidancecallable or None= None
(x, t) -> Tensor, returning ∇xlogpt(ymidx)\nabla_x \\log p_t(y \\mid x). Added to the score, which is all conditional sampling is — the paper's controllable generation needs no retraining and no conditional model.

Returns

GenerationOutput

samples of shape (n_samples, C, H, W).

Raises

ValueError
If method is not one of the three.

Examples

>>> import lucid
>>> from lucid.models import score_sde_vp_gen
>>> model = score_sde_vp_gen(sample_size=8, base_channels=8,
...     channel_mult=(1,), num_res_blocks=1, resnet_groups=4,
...     attention_resolutions=()).eval()
>>> [model.generate(n_samples=2, method=m, steps=2).samples.shape
...  for m in ("euler", "pc", "ode")]
[(2, 3, 8, 8), (2, 3, 8, 8), (2, 3, 8, 8)]
>>> model.generate(method="ddim")
Traceback (most recent call last):
    ...
ValueError: method must be 'pc', 'euler' or 'ode', got 'ddim'
fn

log_likelihood

→Tensor
log_likelihood(x: Tensor, steps: int = 100, eps: Tensor | None = None)

Exact log-likelihood of the data, in nats.

Parameters

Data, (N, C, H, W).
stepsint= 100
Solver steps for the forward integration.
epsTensor or None= None
The probe vector of the trace estimator. None draws Rademacher noise. Exposed so a test can hold it fixed, since the estimate is stochastic.

Returns

Tensor

(N,) log-densities, reported at float32 or wider because it sums over every dimension: a float16 sum of a VE prior's squares overflows past a few dozen pixels.

Notes

Song et al. (2021), Appendix D.2. Because the probability-flow ODE is a neural ODE, the instantaneous change of variables gives

log⁡p0(x(0))=log⁡pT(x(T))+∫0T∇⋅f~θ(x(t),t) dt,\log p_0(x(0)) = \log p_T(x(T)) + \int_0^T \nabla \cdot \tilde{f}_\theta(x(t), t)\, dt,

equation 39, where x(t)x(t) solves that ODE. The divergence is expensive, so it is estimated as the paper does — equation 40, the Skilling-Hutchinson trace estimator

∇⋅f~θ(x,t)=Ep(ϵ)[ϵ⊤∇f~θ(x,t) ϵ],\nabla \cdot \tilde{f}_\theta(x, t) = \mathbb{E}_{p(\epsilon)} \big[\epsilon^\top \nabla \tilde{f}_\theta(x, t)\, \epsilon\big],

with ϵ\epsilon of zero mean and identity covariance. The vector-Jacobian product is one backward pass, so the whole estimate costs one extra gradient per solver step.

This is what the deterministic sampler buys that a stochastic one cannot: an exact density, not a bound. It is also stochastic in the estimator — two calls differ, and the variance falls with the number of probes, not with steps.

Examples

>>> import lucid
>>> from lucid.models import score_sde_vp_gen
>>> model = score_sde_vp_gen(sample_size=8, base_channels=8,
...     channel_mult=(1,), num_res_blocks=1, resnet_groups=4,
...     attention_resolutions=()).eval()
>>> model.log_likelihood(lucid.randn((1, 3, 8, 8)), steps=3).shape
(1,)