Conditioners¶
A conditioner is a layer \((\vect{h}, \vect{c}) \mapsto \vect{y}\) that transforms an inner activation \(\vect{h} \in \mathbb{R}^{C}\) with a context or latent code \(\vect{c} \in \mathbb{R}^{K}\), passed as the second positional argument, layer(h, c). It is the same mechanism in a conditional flow and in a conditioned neural field, so gauss_flows_maps.conditioning holds it once for both. Each acts on one example and composes with jax.vmap over a batch of pairs \((\vect{h}, \vect{c})\).
ConcatConditionerconcatenates and projects, \(\vect{y} = \mat{W} [\vect{h}; \vect{c}] + \vect{b}\). It is the cheapest, but its parameter count grows with \(K\).AffineModulation(FiLM) scales and shifts, \(\vect{y} = \gvect{\gamma}(\vect{c}) \odot \vect{h} + \gvect{\beta}(\vect{c})\), with \([\gvect{\beta}; \vect{r}_\gamma] = \mat{W}\varphi(\vect{c}) + \vect{b}\) and \(\gvect{\gamma} = \sigma(\vect{r}_\gamma)\) for its scale map \(\sigma\) (gamma_activation), where \(\varphi\) is an optional MLP trunk (trunk_width,trunk_depth,trunk_activation; the identity without one).modulation(c)returns \((\gvect{\gamma}, \log \gvect{\gamma}, \gvect{\beta})\), andlog_det(c)returns \(\log \lvert \det \partial \vect{y} / \partial \vect{h} \rvert = \sum_{i=1}^{C} \log \gamma_i\) for every positive map, from the map's stablelog_value.HyperLineargenerates the weights of a target linear layer from the code, \((\mathrm{vec}\mat{W}_t, \vect{b}_t) = g(\vect{c})\), and applies \(\vect{y} = \mat{W}_t \vect{x} + \vect{b}_t\).
ConcatConditioner and AffineModulation start as the identity in \(\vect{h}\) for every code, so a conditional layer starts as its unconditional layer and the step-0 likelihood is the base model's. ConcatConditioner starts at \(\mat{W} = [\mat{I} \mid \mat{0}]\) and \(\vect{b} = \vect{0}\) (pass init="default" for the eqx.nn.Linear draw). AffineModulation starts its generator's weight at readout_scale times its default draw (zero by default) and its bias at \((\vect{0}, r_0 \vect{1})\), where \(r_0 = \sigma^{-1}(1)\) is the raw scale at which the scale map gives \(\gamma = 1\); with a zero weight the generator ignores the code, so \(\gvect{\beta} = \vect{0}\) and \(\gvect{\gamma} = \vect{1}\) (the adaLN-Zero rule of Peebles and Xie, 2023). The weight still learns from the first step, and a small readout_scale such as \(10^{-2}\) also keeps a gradient to the code at step 0. zero_readout gives the same start to any eqx.nn.Linear or to the last layer of an eqx.nn.MLP.
The scale map is a Reparametrised or Composed map of gauss_flows_maps.reparam, or one of these shorthands:
gamma_activation |
Map | \(\gamma = \sigma(r)\) | \(r_0\) |
|---|---|---|---|
"one_plus_tanh" (default) |
Reparametrised("sigmoid", lower=0, upper=2) |
\(2 / (1 + e^{-r})\) | \(0\) |
"exp" |
Reparametrised("exp") |
\(e^{r}\) | \(0\) |
"softplus" |
Reparametrised("softplus") |
\(\log(1 + e^{r})\) | \(\log(e - 1) \approx 0.5413\) |
"identity" |
none | \(r\) | \(1\) |
Because \(1 + \tanh r = 2 / (1 + e^{-2r})\), "one_plus_tanh" is the sigmoid map on \((0, 2)\) of \(r\) itself: it agrees with \(1 + \tanh r\) at \(r = 0\) and differs elsewhere. "identity" has no positive scale, so its log_det raises. A log-scale bounded by \(B\) keeps \(\gamma\) inside \((e^{-B}, e^{B})\):
from gauss_flows_maps.reparam import Composed, Reparametrised
# γ = exp(−B + 2B σ(r)) = exp(B tanh(r/2)), with B = 3
bounded = Composed(
outer=Reparametrised("exp"),
inner=Reparametrised("sigmoid", lower=-3.0, upper=3.0),
)
film = AffineModulation.init(16, 7, key=key, gamma_activation=bounded, trunk_width=64, trunk_depth=2)
HyperLinear generates \(\vect{\theta} = (\mathrm{vec}\mat{W}_t, \vect{b}_t) = \mat{G}\vect{c}\) with a generator \(\mat{G} \in \mathbb{R}^{(C_\text{out} C_\text{in} + C_\text{out}) \times K}\). If the entries of \(\mat{G}\) are independent with mean zero and variance \(v_G\), and the code's entries have second moment \(m_2 = \mathbb{E}[c_k^2]\), each generated weight has variance \(K v_G m_2\). To start the generated layer at the variance \(v^\star = \sigma_\star^2\) of the target layer's own initialisation, times \(s^2\), the generator's weights draw from \(\mathcal{N}(0, v_G)\) with
where \(s\) is init_scale (default 1), \(\sigma_\star\) is target_std (default \(1/\sqrt{3 C_\text{in}}\), the standard deviation of eqx.nn.Linear's own initial weights) and \(m_2\) is cond_second_moment (default 1, which holds for a standardised code or an embedding that ends in a layer norm). This is hyperfan initialisation (Chang, Flokas and Lipson, 2020): the generator's variance comes from the variance the generated weights need, not from the generator's own fan-in. The rows of \(\mat{G}\) that generate \(\vect{b}_t\), and the generator's bias, start at zero, so the generated bias is zero at init.
The trainable linear maps keep the field names proj (in ConcatConditioner) and generator (in AffineModulation and HyperLinear), because pyrox-nn's Bayesian conditioners swap their weight and bias by name.
Conditioning in gauss_flows¶
Every conditional layer of gauss_flows reads its condition \(\vect{c} \in \mathbb{R}^{K}\) through a conditioner of this module and starts as its unconditional self, so the step-0 likelihood of a conditional flow is that of the same flow without the condition. \(K\) is the layer's cond_dim (control_dim for ContinuousAffineCoupling), and \(\vect{x}_p\) below is the passive half of a coupling's input, the coordinates it leaves untouched.
| Layer | Conditioner | Input | Starts as |
|---|---|---|---|
Conditioner |
AffineModulation |
\(\vect{c}\) | its inner bijection \(f\) |
ConditionalDiagGaussian |
AffineModulation |
\(\vect{c}\) | \(\mathcal{N}(\vect{0}, \mat{I})\) |
GINCoupling, ContinuousAffineCoupling |
AffineModulation |
\([\vect{x}_p; \vect{c}]\) | the identity |
| the FlowJax-built couplings | FlowJax's MLP, through zero_readout |
\([\vect{x}_p; \vect{c}]\) | the transformer's initial value |
The FlowJax-built couplings are AffineCoupling, RQSplineCoupling, CircularRQSplineCoupling, MixtureGaussianCDFCoupling and DeepSigmoidCoupling. The couplings read \(\vect{c}\) only when they are built with a cond_dim (or a control_dim); without one their input is \(\vect{x}_p\) alone. No layer of gauss_flows uses ConcatConditioner or HyperLinear; they are here for conditioned neural fields and for layers outside the package.
The modulation. The first four layers share one head. For an input \(\vect{u} \in \mathbb{R}^{M}\) (the condition, or the passive half joined to it) and \(C\) modulated features, an AffineModulation reads a raw shift and a raw scale from its trunk \(\varphi\) (\(\mathbb{R}^{M} \to \mathbb{R}^{H}\), nn_depth layers of width nn_width with activation, or the identity with \(H = M\) when nn_depth=0) and its readout \(\mat{W} \in \mathbb{R}^{2C \times H}\), \(\vect{b} \in \mathbb{R}^{2C}\):
The shift is \(\gvect{\beta} = \vect{r}_\beta\) and the scale is \(\gvect{\gamma} = \sigma(\vect{r}_\gamma)\), where \(\sigma\) is the positive scale map, applied elementwise. They modulate the activation \(\vect{h} \in \mathbb{R}^{C}\) as
$$ \vect{y} = \gvect{\gamma} \odot \vect{h} + \gvect{\beta}. $$ The map is elementwise in \(\vect{h}\), so its log-Jacobian is the sum of the scale map's log-values,
which each map computes in a stable closed form (its log_value) rather than as the logarithm of \(\gamma_i\). Each layer uses the head in its own way. Conditioner modulates \(\vect{h} = f(\vect{x})\) and adds the log-Jacobian of \(f\). ConditionalDiagGaussian draws \(\vect{x} = \gvect{\beta} + \gvect{\gamma} \odot \gvect{\epsilon}\), so its log-density is \(\log \mathcal{N}(\gvect{\epsilon}; \vect{0}, \mat{I}) - \sum_{i} \log \sigma(r_{\gamma,i})\) at \(\gvect{\epsilon} = (\vect{x} - \gvect{\beta}) / \gvect{\gamma}\). ContinuousAffineCoupling multiplies \(\log \gvect{\gamma}\) and \(\gvect{\beta}\) by its time gate \(g(t)\), so its log-Jacobian is \(g(t) \sum_{i} \log \sigma(r_{\gamma,i})\). GINCoupling subtracts the mean of \(\log \gvect{\gamma}\) before it scales, so it preserves volume and its log-Jacobian is zero.
Identity at init and readout_scale. The four layers take readout_scale (default \(0\)) and start the readout at \(\mat{W} = s \mat{W}_0\), where \(s\) is readout_scale and \(\mat{W}_0\) the readout's default draw, and \(\vect{b} = (\vect{0}, r_0 \vect{1})\) with \(r_0 = \sigma^{-1}(1)\). With the default \(s = 0\) the readout ignores its input, so \(\vect{r}_\beta = \vect{0}\) and \(\vect{r}_\gamma = r_0 \vect{1}\) for every \(\vect{u}\), which gives \(\gvect{\beta} = \vect{0}\), \(\gvect{\gamma} = \vect{1}\) and a zero log-Jacobian: Conditioner is exactly \(f\), ConditionalDiagGaussian is exactly \(\mathcal{N}(\vect{0}, \mat{I})\), and the two couplings are exactly the identity, for every condition. The readout's weight still learns from the first step. A small readout_scale such as \(10^{-2}\) starts the layer close to that point rather than at it, and it also gives the trunk and the condition a gradient at step 0. A pre-built modulation replaces the arguments from nn_width to scale_map and keeps its own initialisation. The couplings built on FlowJax's Coupling take no readout_scale: zero_readout always zeroes the last layer of their MLP, and FlowJax adds the MLP's output to the transformer's initial parameters, so each starts at its transformer's initial value for every input, which is the identity for all of them except DeepSigmoidCoupling (a fixed sum of sigmoids).
Scale maps. scale_map takes any positive map of gauss_flows_maps.reparam or one of the shorthands "exp", "softplus" and "one_plus_tanh" in the table above. "identity" raises a ValueError, because a flow layer needs its log-Jacobian. The flow layers default to "exp" (with \(r_0 = 0\)), whereas AffineModulation.init on its own defaults to "one_plus_tanh". The bounded log-scale above, \(\gamma_i = \exp(B \tanh(r_{\gamma,i} / 2))\), keeps \(\lvert \log \gamma_i \rvert < B\) for every input (the soft clamp that many affine coupling flows put on their log-scales) and also has \(r_0 = 0\):
import jax.random as jr
from gauss_flows_maps.bijections import GINCoupling
from gauss_flows_maps.reparam import Composed, Reparametrised
# log γ bounded by 3, on the passive half of x joined to a code c of size 3
bounded = Composed(
outer=Reparametrised("exp"),
inner=Reparametrised("sigmoid", lower=-3.0, upper=3.0),
)
coupling = GINCoupling(jr.key(0), shape=(4,), cond_dim=3, readout_scale=1e-2, scale_map=bounded)
Notebook 08 conditions a two-moons flow three ways (the base, the couplings, or both) and shows what each can model.
Reference¶
AbstractConditioner
¶
Bases: Module
Protocol for (h, c) → y conditioning layers.
Subclasses implement __call__(h, c, /) with h ∈ ℝ^C and c ∈ ℝ^K. The
protocol is duck-typed, so there is no abstractmethod enforcement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_features
|
Output channel count |
required | |
cond_dim
|
Context dimension |
required |
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/conditioners.py
ConcatConditioner
¶
Bases: AbstractConditioner
Concatenate h and c, then apply one Linear.
The layer computes y = W [h ; c] + b, with W ∈ ℝ^(C×(C+K)). It is the
cheapest conditioner, but its parameter count grows with cond_dim: it has
(C + K)·C + C parameters in all. By default the projection starts as
W = [I | 0] and b = 0, so y = h for every c until training moves it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
proj
|
Linear projection |
required | |
num_features
|
Output channels |
required | |
cond_dim
|
Context dimension |
required |
Examples:
Typical case, with eight features and a four-dimensional context:
>>> import jax.random as jr, jax.numpy as jnp
>>> from gauss_flows_maps.conditioning import ConcatConditioner
>>> layer = ConcatConditioner.init(num_features=8, cond_dim=4, key=jr.key(0))
>>> y = layer(jnp.ones(8), jnp.ones(4))
>>> y.shape
(8,)
Edge case, a one-dimensional context; the layer starts as the identity:
>>> thin = ConcatConditioner.init(num_features=3, cond_dim=1, key=jr.key(0))
>>> thin(jnp.arange(3.0), jnp.ones(1))
Array([0., 1., 2.], dtype=float32)
Composition with vmap over a batch of pairs (h, c):
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/conditioners.py
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 | |
init
classmethod
¶
init(num_features: int, cond_dim: int, *, key: PRNGKeyArray, init: Literal['identity', 'default'] = 'identity') -> ConcatConditioner
Build a ConcatConditioner that starts as the identity in h.
With init="identity" the projection starts at W = [I | 0] and b = 0,
so y = h for every c; with init="default" it keeps the
eqx.nn.Linear default init, a random projection of [h ; c].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_features
|
int
|
Output channel count |
required |
cond_dim
|
int
|
Context dimension |
required |
key
|
PRNGKeyArray
|
PRNG key for the projection's init. |
required |
init
|
Literal['identity', 'default']
|
|
'identity'
|
Returns:
| Type | Description |
|---|---|
ConcatConditioner
|
Initialised |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/conditioners.py
AffineModulation
¶
Bases: AbstractConditioner
Feature-wise linear modulation (FiLM): y = γ(c) ⊙ h + β(c).
An optional MLP trunk φ embeds the code, e = φ(c) (or e = c without a
trunk), and a linear readout, the generator, maps it to 2C numbers that
split into the shift β and a raw scale r_γ:
[β ; r_γ] = W e + b, γ = σ(r_γ), y = γ ⊙ h + β.
The scale map σ is an elementwise map of gauss_flows_maps.reparam
(Reparametrised or Composed), or one of these shorthands:
shorthand map γ = σ(r) r₀
"one_plus_tanh" Reparametrised("sigmoid", 0, 2) 2 sigmoid(r) 0
"exp" Reparametrised("exp") eʳ 0
"softplus" Reparametrised("softplus") log(1 + eʳ) log(e − 1)
"identity" none r 1
"one_plus_tanh" is the sigmoid map on (0, 2) of r itself; 1 + tanh r
equals 2 sigmoid(2r), so the two agree at r = 0 and differ elsewhere. A
bounded log-scale is Composed(outer=Reparametrised("exp"),
inner=Reparametrised("sigmoid", lower=-B, upper=B)), γ = exp(B tanh(r/2)),
which keeps γ inside (e^(−B), e^B). "identity" has no positivity
guarantee and is rarely useful.
The map is elementwise in h, so its log-Jacobian is
log |det ∂y/∂h| = Σᵢ log γᵢ, which log_det computes from the map's stable
log_value (for exp, log γ = r).
The layer starts as the identity in h for every c: the generator's weight
starts at readout_scale · W (zero by default) and its bias at (0, r₀),
with r₀ = σ⁻¹(1) the raw scale at which γ = 1. With a zero weight,
β = 0 and γ = 1 whatever the code, so y = h (the adaLN-Zero rule of
Peebles and Xie, 2023).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generator
|
Linear readout |
required | |
trunk
|
Optional MLP |
required | |
num_features
|
Output channels |
required | |
cond_dim
|
Context dimension |
required | |
gamma_activation
|
The scale map |
required |
Shape
__call__: h (C,) and c (K,) to y (C,).
modulation: c (K,) to γ, log γ and β, each (C,).
log_det: c (K,) to a scalar.
References
Perez, E., et al. (2018). FiLM: Visual reasoning with a general conditioning layer. AAAI. Peebles, W. and Xie, S. (2023). Scalable diffusion models with transformers. ICCV.
Examples:
Typical case, with eight features and a four-dimensional context:
>>> import jax.random as jr, jax.numpy as jnp
>>> from gauss_flows_maps.conditioning import AffineModulation
>>> film = AffineModulation.init(num_features=8, cond_dim=4, key=jr.key(0))
>>> y = film(jnp.ones(8), jnp.ones(4))
>>> y.shape
(8,)
Edge case, the "softplus" scale, which starts at γ = 1 for any code:
>>> pos = AffineModulation.init(
... num_features=3, cond_dim=2, key=jr.key(0), gamma_activation="softplus"
... )
>>> pos(jnp.arange(3.0), jnp.array([4.0, -2.0]))
Array([0., 1., 2.], dtype=float32)
A bounded log-scale behind a two-layer trunk, and its modulation:
>>> from gauss_flows_maps.reparam import Composed, Reparametrised
>>> bounded = Composed(
... outer=Reparametrised("exp"),
... inner=Reparametrised("sigmoid", lower=-3.0, upper=3.0),
... )
>>> deep = AffineModulation.init(
... 16, 7, key=jr.key(0), gamma_activation=bounded,
... trunk_width=64, trunk_depth=2,
... )
>>> gamma, log_gamma, beta = deep.modulation(jnp.zeros(7))
>>> gamma.shape, log_gamma.shape, beta.shape
((16,), (16,), (16,))
Composition with vmap over a batch of pairs (h, c):
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/conditioners.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 | |
init
classmethod
¶
init(num_features: int, cond_dim: int, *, key: PRNGKeyArray, gamma_activation: _ScaleMap | Literal['one_plus_tanh', 'exp', 'softplus', 'identity'] = 'one_plus_tanh', trunk_width: int | None = None, trunk_depth: int = 0, trunk_activation: Callable[[Array], Array] = relu, readout_scale: float = 0.0) -> AffineModulation
Build AffineModulation that starts as the identity in h.
The trunk, when trunk_depth > 0, is trunk_depth linear layers of
width trunk_width, each followed by trunk_activation. The generator's
weight starts at readout_scale times the eqx.nn.Linear default and
its bias at (0, r₀), with r₀ = σ⁻¹(1) (see the class docstring), so
β = 0 and γ = 1 for every code when readout_scale = 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_features
|
int
|
Output channel count |
required |
cond_dim
|
int
|
Context dimension |
required |
key
|
PRNGKeyArray
|
PRNG key for the trunk's and the generator's init. |
required |
gamma_activation
|
_ScaleMap | Literal['one_plus_tanh', 'exp', 'softplus', 'identity']
|
The scale map |
'one_plus_tanh'
|
trunk_width
|
int | None
|
Width |
None
|
trunk_depth
|
int
|
Number of linear layers in the trunk; |
0
|
trunk_activation
|
Callable[[Array], Array]
|
Activation after each of the trunk's layers. |
relu
|
readout_scale
|
float
|
Factor on the generator's default weight. The default
|
0.0
|
Returns:
| Type | Description |
|---|---|
AffineModulation
|
Initialised |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/conditioners.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | |
modulation
¶
modulation(c: Float[Array, ' K']) -> tuple[Float[Array, ' C'], Float[Array, ' C'], Float[Array, ' C']]
Return the scale γ, its log log γ and the shift β for one code.
log γ is the scale map's own log_value, stable far into its tails.
With gamma_activation="identity" it is log r_γ, NaN where γ ≤ 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c
|
Float[Array, ' K']
|
Context vector |
required |
Returns:
| Type | Description |
|---|---|
tuple[Float[Array, ' C'], Float[Array, ' C'], Float[Array, ' C']]
|
The tuple |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> import jax.numpy as jnp, jax.random as jr
>>> from gauss_flows_maps.conditioning import AffineModulation
>>> film = AffineModulation.init(4, 2, key=jr.key(0))
>>> gamma, log_gamma, beta = film.modulation(jnp.ones(2))
>>> gamma
Array([1., 1., 1., 1.], dtype=float32)
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/conditioners.py
log_det
¶
Return log |det ∂y/∂h| = Σᵢ log γᵢ, the log-Jacobian in h.
It holds for every positive scale map, through the map's log_value;
gamma_activation="identity" has no positive scale, so it raises.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c
|
Float[Array, ' K']
|
Context vector |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, '']
|
Scalar log-determinant |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If |
ValueError
|
If |
Examples:
>>> import jax.numpy as jnp
>>> import jax.random as jr
>>> film = AffineModulation.init(
... num_features=4, cond_dim=2, key=jr.key(0),
... gamma_activation="softplus",
... )
>>> film.log_det(jnp.ones(2)) # γ = 1 at init, so Σ log γ = 0
Array(0., dtype=float32)
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/conditioners.py
HyperLinear
¶
Bases: AbstractConditioner
Generate a target Linear's (W, b) from c, then apply it.
A single eqx.nn.Linear of output size C_out·C_in + C_out produces the flat
parameter vector of an ad-hoc linear layer. The forward pass consumes single
examples x ∈ ℝ^(C_in) and c ∈ ℝ^K and returns ℝ^(C_out).
The generator G ∈ ℝ^((C_out·C_in + C_out)×K) starts so that the generated
weights have the variance of the target layer's own initialisation, times
init_scale² (hyperfan initialisation, Chang, Flokas and Lipson, 2020). If the
entries of G are independent with mean zero and variance v_G, and the code's
entries have second moment m₂ = 𝔼[c_k²], each generated weight has variance
Var[W_ij] = K v_G m₂. To start at the target variance v* = σ*² the
generator draws
G_ij ~ 𝒩(0, v_G), v_G = s² v* / (K m₂),
with s the init_scale, σ* the target_std and m₂ the
cond_second_moment of HyperLinear.init. The rows of G that generate b,
and the generator's own bias, start at zero, so the generated b is zero for
every code at init.
Algorithm
- Generate the flat parameters
θ = g(c)from the generatorg. - Split
θintoW ∈ ℝ^(C_out×C_in), filled row by row, andb ∈ ℝ^(C_out). - Apply the generated map
y = W x + b.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generator
|
Linear |
required | |
target_in
|
Input dimension |
required | |
target_out
|
Output dimension |
required | |
cond_dim
|
Context dimension |
required | |
num_features
|
Alias for |
required |
References
Pan, Brunton and Kutz (2023). Neural implicit flow. JMLR. Chang, O., Flokas, L. and Lipson, H. (2020). Principled weight initialization for hypernetworks. ICLR.
Examples:
Typical case, mapping four inputs to eight outputs:
>>> import jax.random as jr, jax.numpy as jnp
>>> from gauss_flows_maps.conditioning import HyperLinear
>>> hyper = HyperLinear.init(
... target_in=4, target_out=8, cond_dim=3, key=jr.key(0)
... )
>>> y = hyper(jnp.ones(4), jnp.ones(3))
>>> y.shape
(8,)
Edge case, a one-dimensional target:
>>> scalar = HyperLinear.init(
... target_in=1, target_out=1, cond_dim=1, key=jr.key(0)
... )
>>> scalar(jnp.ones(1), jnp.ones(1)).shape
(1,)
Composition with vmap over a batch of pairs (x, c):
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/conditioners.py
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 | |
init
classmethod
¶
init(target_in: int, target_out: int, cond_dim: int, *, key: PRNGKeyArray, target_std: float | None = None, cond_second_moment: float = 1.0, init_scale: float = 1.0) -> HyperLinear
Build a HyperLinear whose generated layer starts at a target variance.
The generator's weights draw G_ij ~ 𝒩(0, s² σ*² / (K m₂)) for the rows
that generate W; the rows that generate b and the generator's bias
start at zero. Averaged over codes with 𝔼[c_k²] = m₂, the generated
weights then have standard deviation s σ*.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_in
|
int
|
Input dimension |
required |
target_out
|
int
|
Output dimension |
required |
cond_dim
|
int
|
Context dimension |
required |
key
|
PRNGKeyArray
|
PRNG key for the generator's init. |
required |
target_std
|
float | None
|
Standard deviation |
None
|
cond_second_moment
|
float
|
Second moment |
1.0
|
init_scale
|
float
|
Factor |
1.0
|
Returns:
| Type | Description |
|---|---|
HyperLinear
|
Initialised |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/gauss-flows-maps/src/gauss_flows_maps/_src/conditioners.py
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 | |
zero_readout
¶
Scale a readout's weight by scale and set its bias to bias.
The readout is net itself when it is an eqx.nn.Linear, or the last layer of
an eqx.nn.MLP. With scale = 0 the readout returns bias for every input,
g(e) = (scale · W) e + bias = bias,
so a layer that reads its parameters from g starts at the value bias gives
it (the adaLN-Zero rule of Peebles and Xie, 2023). The weight still learns from
the first step, since ∂L/∂W = (∂L/∂g) eᵀ is not zero; a small scale such as
1e-2 also keeps a gradient to the layers before the readout at step 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net
|
Net
|
An |
required |
bias
|
ArrayLike
|
The readout's new bias, broadcast to the bias's shape. |
0.0
|
scale
|
float
|
The factor on the readout's weight. |
0.0
|
Returns:
| Type | Description |
|---|---|
Net
|
A copy of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the readout has no bias ( |
Shape
bias: a scalar, or the readout's output shape.
References
Peebles, W. and Xie, S. (2023). Scalable diffusion models with transformers. ICCV.
Examples:
A linear readout that returns its bias for every input:
>>> import jax.numpy as jnp, jax.random as jr, equinox as eqx
>>> from gauss_flows_maps.conditioning import zero_readout
>>> readout = zero_readout(eqx.nn.Linear(3, 2, key=jr.key(0)), bias=1.0)
>>> readout(jnp.arange(3.0))
Array([1., 1.], dtype=float32)
The last layer of an MLP, with a small weight kept:
>>> mlp = eqx.nn.MLP(3, 2, width_size=8, depth=2, key=jr.key(0))
>>> small = zero_readout(mlp, scale=1e-2)
>>> bool(jnp.allclose(small.layers[-1].weight, 1e-2 * mlp.layers[-1].weight))
True