Training¶
negative_log_likelihood is the maximum-likelihood loss of any flow (a flowjax distribution, SurVAEFlow or ClassCondFlow), and fit_gaussianization_flow is a small loop around it with early stopping on a validation split. The docs notebooks train the same loss with pipekit-train, which is a docs dependency only.
The NumPyro bridges, a flow as a NumPyro distribution (flow_to_numpyro), a flow-based SVI guide (FlowGuide) and a NumPyro distribution as a flow base (NumpyroBase), live in pyrox-flows since gauss_flows dropped its numpyro dependency.
negative_log_likelihood
¶
negative_log_likelihood(flow: Any, x: Array, *, key: PRNGKeyArray | None = None, condition: ArrayLike | None = None) -> Array
Mean negative log-likelihood of a batch under a flow.
ℒ(θ) = −(1/B) Σ_b [log p(z_L⁽ᵇ⁾) + Σ_ℓ 𝒱_ℓ⁽ᵇ⁾]
Each 𝒱_ℓ is a log-determinant for a bijection and, for a surjection or an
injection, a log-density term that may need a draw; the bound is exact for
a chain of bijections. The flow is passed through paramax.unwrap first,
so constrained and frozen parameters take their values, and a frozen
(paramax.NonTrainable) leaf gets no gradient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
Any
|
A flowjax distribution, a |
required |
x
|
Array
|
A batch of samples, shape |
required |
key
|
PRNGKeyArray | None
|
PRNG key for a flow with stochastic layers. A flowjax
distribution ignores it; |
None
|
condition
|
ArrayLike | None
|
The condition, shape |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
The scalar mean over the batch. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the flow needs a key and none was given. |
Shape
x: (B, *event); condition: (B, *cond), cond or
(B,) labels; output: ().
Examples:
>>> import jax.numpy as jnp
>>> import jax.random as jr
>>> from flowjax.distributions import Normal
>>> from gauss_flows_models.inference import negative_log_likelihood
>>> float(negative_log_likelihood(Normal(jnp.zeros(2)), jnp.zeros((4, 2))))
1.837877...
Source code in packages/gauss-flows-models/src/gauss_flows_models/_src/inference/train.py
fit_gaussianization_flow
¶
fit_gaussianization_flow(key: PRNGKeyArray, dist: Any, data: ArrayLike, *, condition: ArrayLike | None = None, optimizer: GradientTransformation | None = None, learning_rate: float = 0.0005, max_epochs: int = 100, max_patience: int = 5, batch_size: int = 256, val_prop: float = 0.1, show_progress: bool = True) -> tuple[Any, dict[str, list[float]]]
Fit a flow to samples by maximum likelihood, with early stopping.
The samples are shuffled once and split into a training set and a
validation set. Each epoch reshuffles the training set, drops the last
short batch, and takes one optimiser step per batch on
negative_log_likelihood; the validation loss is then taken over the
whole validation set. Training stops after max_patience epochs without
a lower validation loss, and the parameters of the best epoch are returned.
Only the floating-point arrays are trained. A paramax.NonTrainable leaf
gets no optimiser state, so it stays fixed under any optimiser (AdamW's
weight decay included), and key or integer leaves (FFJORD's trace_key)
are left alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
PRNGKeyArray
|
PRNG key for the split, the shuffles and the stochastic layers. |
required |
dist
|
Any
|
The flow to fit: a flowjax distribution, a |
required |
data
|
ArrayLike
|
The samples, shape |
required |
condition
|
ArrayLike | None
|
The condition of each sample, shape |
None
|
optimizer
|
GradientTransformation | None
|
An optax optimiser. Defaults to Adam at |
None
|
learning_rate
|
float
|
Adam's step size when |
0.0005
|
max_epochs
|
int
|
Maximum number of epochs. |
100
|
max_patience
|
int
|
Epochs without a lower validation loss before stopping. |
5
|
batch_size
|
int
|
Samples per step, capped at the size of the training set. |
256
|
val_prop
|
float
|
Fraction of the samples held out for validation. |
0.1
|
show_progress
|
bool
|
Whether to show a progress bar over the epochs. |
True
|
Returns:
| Type | Description |
|---|---|
Any
|
|
dict[str, list[float]]
|
loss, and |
tuple[Any, dict[str, list[float]]]
|
epoch run. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Shape
data: (N, *event); condition: (N, *cond) or (N,)
labels. Each step sees (B, *event) with B = min(batch_size,
N_train).
Examples:
>>> import jax.random as jr
>>> from gauss_flows_models.flows import gaussianization_flow
>>> from gauss_flows_models.inference import fit_gaussianization_flow
>>> x = jr.normal(jr.key(0), (512, 2))
>>> flow = gaussianization_flow(jr.key(1), n_dims=2, n_layers=2)
>>> trained, losses = fit_gaussianization_flow(
... jr.key(2), flow, x, max_epochs=3, show_progress=False
... )
>>> len(losses["train"]) == len(losses["val"]) == 3
True
Source code in packages/gauss-flows-models/src/gauss_flows_models/_src/inference/train.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |