Conditioning¶
A conditioner is a layer (h, c) → y that transforms an inner activation h based on a context vector c. The three concrete conditioners (ConcatConditioner, AffineModulation, HyperLinear) live in gauss_flows_maps.conditioning and are re-exported here; pyrox_nn adds their Bayesian variants, which wrap those cores, and the ConditionedINR composite of geonnax wraps any inner network with per-layer conditioning.
Decision rubric¶
| Pattern | Use when | Cost | Where it shows up |
|---|---|---|---|
ConcatConditioner |
You want a cheap baseline; cond_dim is small. |
(C + K) · C + C per layer |
DiffeqMLP-style CNF vector fields |
AffineModulation (FiLM) |
Feature-wise modulation is enough; you want low generator cost regardless of cond_dim. |
2C · K + 2C per layer |
π-GAN, Modulated SIREN, conditional INRs |
HyperLinear |
You need the full target weight matrix to depend on z; want NIF/MetaSDF. |
K · (C·C_in + C) per layer |
NIF (Pan et al. 2023), MetaSDF, Ha et al. hypernets |
The Bayesian variants put Normal(0, prior_std) priors on the readout weights only: the generator of AffineModulation and HyperLinear, the projection of ConcatConditioner. An AffineModulation core with an MLP trunk keeps the trunk deterministic, and its gen_W takes the trunk's width as input size. Posterior cost scales with the readout size, not the target network — that's the whole point of Bayesian amortised inference.
End-to-end use¶
import jax.random as jr
import jax.numpy as jnp
from pyrox_nn import SIREN, AffineModulation, ConditionedINR, HyperSIREN
key = jr.key(0)
# 1) FiLM-modulate every hidden layer of a SIREN
inner = SIREN.init(2, 32, 1, depth=4, key=key)
wrapped = ConditionedINR.init(
inner, conditioner_cls=AffineModulation, cond_dim=4, key=key
)
y = wrapped(jnp.ones((10, 2)), jnp.ones((10, 4))) # (10, 1)
# 2) Full NIF stack — ParameterNet → per-layer HyperLinear → ShapeNet (SIREN)
import equinox as eqx
class IdentityNet(eqx.Module):
def __call__(self, mu): return mu
nif = HyperSIREN(
in_features=2, hidden_features=32, out_features=1,
depth=5, cond_dim=3, parameter_net=IdentityNet(), key=key,
)
y = nif(jnp.ones((10, 2)), jnp.ones((3,))) # (10, 1)
For a hands-on walkthrough see the Conditional Neural Fields notebook.
Protocol¶
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 .venv/lib/python3.12/site-packages/gauss_flows_maps/_src/conditioners.py
Concrete conditioners¶
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 .venv/lib/python3.12/site-packages/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(num_features: int, cond_dim: int, *, key: PRNGKeyArray, init: Literal['identity', 'default'] = 'identity') -> ConcatConditioner
classmethod
¶
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 .venv/lib/python3.12/site-packages/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 .venv/lib/python3.12/site-packages/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(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] = jax.nn.relu, readout_scale: float = 0.0) -> AffineModulation
classmethod
¶
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 .venv/lib/python3.12/site-packages/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(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 .venv/lib/python3.12/site-packages/gauss_flows_maps/_src/conditioners.py
log_det(c: Float[Array, ' K']) -> Float[Array, '']
¶
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 .venv/lib/python3.12/site-packages/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 .venv/lib/python3.12/site-packages/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(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
classmethod
¶
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 .venv/lib/python3.12/site-packages/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 | |
Bayesian variants¶
BayesianConcatConditioner
¶
Bases: PyroxModule
ConcatConditioner of gauss-flows-maps with Normal priors on the projection.
Registers two NumPyro sample sites — {scope}.proj_W and
{scope}.proj_b — under Normal(0, prior_std). Total of two
sites per forward call; nothing is sampled from the inner h or
the context c.
Holds a frozen gauss_flows_maps.conditioning.ConcatConditioner core whose
proj weights are swapped with the sampled arrays each call. The core's
own init (the identity in h) is therefore not the start of the prior:
the sampled proj replaces it.
Attributes:
| Name | Type | Description |
|---|---|---|
core |
ConcatConditioner
|
Frozen |
num_features |
int
|
Output channels. |
cond_dim |
int
|
Context dimension. |
prior_std |
float
|
Scale of the Normal priors. |
pyrox_name |
str | None
|
Optional explicit scope name for NumPyro. |
Source code in packages/pyrox-nn/src/pyrox_nn/_conditioning.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
init(num_features: int, cond_dim: int, *, key: PRNGKeyArray | None = None, prior_std: float = 1.0, pyrox_name: str | None = None) -> BayesianConcatConditioner
classmethod
¶
Build a BayesianConcatConditioner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_features
|
int
|
Output channels. |
required |
cond_dim
|
int
|
Context dimension. |
required |
key
|
PRNGKeyArray | None
|
PRNG key for the core's init, whose |
None
|
prior_std
|
float
|
Scale of the Normal priors. |
1.0
|
pyrox_name
|
str | None
|
Optional explicit scope name. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in packages/pyrox-nn/src/pyrox_nn/_conditioning.py
BayesianAffineModulation
¶
Bases: PyroxModule
AffineModulation of gauss-flows-maps with Normal priors on the FiLM generator.
Registers two sites — {scope}.gen_W and {scope}.gen_b —
under Normal(0, prior_std). The γ activation is fixed by
construction (default "one_plus_tanh") so the prior over the raw
generator output induces a well-defined prior over γ, β.
Holds a frozen gauss_flows_maps.conditioning.AffineModulation core whose
generator weights are swapped with the sampled arrays each call. The
generator's input size is the core's: cond_dim, or trunk_width when the
core has an MLP trunk, and gen_W is sampled with that shape. The trunk
gets no prior: priors live on the readout only, so the trunk's weights
stay deterministic parameters of the core.
Attributes:
| Name | Type | Description |
|---|---|---|
core |
AffineModulation
|
Frozen |
num_features |
int
|
Output channels. |
cond_dim |
int
|
Context dimension. |
gamma_activation |
Reparametrised | Composed | str
|
Parameterisation of |
prior_std |
float
|
Scale of the Normal priors. |
pyrox_name |
str | None
|
Optional explicit scope name. |
Source code in packages/pyrox-nn/src/pyrox_nn/_conditioning.py
153 154 155 156 157 158 159 160 161 162 163 164 165 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 | |
init(num_features: int, cond_dim: int, *, key: PRNGKeyArray | None = None, gamma_activation: Reparametrised | Composed | Literal['one_plus_tanh', 'exp', 'softplus', 'identity'] = 'one_plus_tanh', trunk_width: int | None = None, trunk_depth: int = 0, trunk_activation: Callable[[Array], Array] = jax.nn.relu, prior_std: float = 1.0, pyrox_name: str | None = None) -> BayesianAffineModulation
classmethod
¶
Build a BayesianAffineModulation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_features
|
int
|
Output channels. |
required |
cond_dim
|
int
|
Context dimension. |
required |
key
|
PRNGKeyArray | None
|
PRNG key for the core's init: the trunk's weights, which stay
deterministic, and the generator, which the sampled arrays replace.
Defaults to |
None
|
gamma_activation
|
Reparametrised | Composed | Literal['one_plus_tanh', 'exp', 'softplus', 'identity']
|
What |
'one_plus_tanh'
|
trunk_width
|
int | None
|
Width of the core's MLP trunk; needed when
|
None
|
trunk_depth
|
int
|
Number of linear layers in the trunk; |
0
|
trunk_activation
|
Callable[[Array], Array]
|
Activation after each trunk layer. |
relu
|
prior_std
|
float
|
Scale of the Normal priors on the generator. |
1.0
|
pyrox_name
|
str | None
|
Optional explicit scope name. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the core's init rejects |
Source code in packages/pyrox-nn/src/pyrox_nn/_conditioning.py
BayesianHyperLinear
¶
Bases: PyroxModule
HyperLinear of gauss-flows-maps with Normal priors on the generator only.
Two sites: {scope}.gen_W and {scope}.gen_b. The target
weights (W_target, b_target) are generated — not sampled — so
Bayesian inference cost scales with the generator size
cond_dim * (target_out * target_in + target_out), not with the
target-network size. This is the architectural advantage of doing
Bayesian amortised inference via hypernetworks.
Attributes:
| Name | Type | Description |
|---|---|---|
core |
HyperLinear
|
Frozen |
target_in |
int
|
Inner |
target_out |
int
|
Inner |
cond_dim |
int
|
Context dimension |
num_features |
int
|
Alias for |
prior_std |
float
|
Scale of the Normal priors on the generator. |
pyrox_name |
str | None
|
Optional explicit scope name. |
Source code in packages/pyrox-nn/src/pyrox_nn/_conditioning.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 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 | |
init(target_in: int, target_out: int, cond_dim: int, *, key: PRNGKeyArray | None = None, prior_std: float = 1.0, pyrox_name: str | None = None) -> BayesianHyperLinear
classmethod
¶
Build a BayesianHyperLinear.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_in
|
int
|
Inner |
required |
target_out
|
int
|
Inner |
required |
cond_dim
|
int
|
Context dimension. |
required |
key
|
PRNGKeyArray | None
|
PRNG key for the core's init, whose generator the sampled
arrays replace on every call. Defaults to |
None
|
prior_std
|
float
|
Scale of the Normal priors on the generator. |
1.0
|
pyrox_name
|
str | None
|
Optional explicit scope name. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If a dimension is non-positive (the core's init checks) or
|
Source code in packages/pyrox-nn/src/pyrox_nn/_conditioning.py
Spectral hyper-conditioning¶
HyperFourierFeatures is the conditional analogue of RBFFourierFeatures: instead of sampling the random Fourier features' (W, b, lengthscale) from a fixed prior, a user-supplied parameter network produces them from the context vector. ConditionedRFFNet adds a learnable linear readout — the conditional analogue of RandomKitchenSinks.
HyperFourierFeatures
¶
Bases: PyroxModule
Random Fourier features with (W, b, log_lengthscale) from a parameter net.
The deterministic counterpart pyrox_nn.RBFFourierFeatures
samples its frequencies and lengthscale from priors. This layer
instead amortises them over a context vector z via a user-supplied
parameter_net:
Two execution modes are supported:
- Shared mode (
z.ndim == 1): the parameter net runs once and the generated features are reused across all rows ofx— same efficiency trick asHyperLinear's shared path. - Per-sample mode (
z.ndim == 2): a distinct(W, b, log_lengthscale)is generated per row ofzviajax.vmapand applied witheinx.dot. This is substantially more expensive in compute and memory because the Fourier parameters are no longer shared across rows ofx, but it is required when eachxrow needs its own context.
The flat output of parameter_net(z) must have size
in_features * n_features + n_features + 1 (frequencies, phases,
log-lengthscale). init does not invoke parameter_net —
a misshapen output surfaces only on the first call.
Attributes:
| Name | Type | Description |
|---|---|---|
parameter_net |
PyroxModule | Module
|
Callable |
in_features |
int
|
Coordinate dimension ( |
n_features |
int
|
Number of frequency pairs; output dim is
|
cond_dim |
int
|
Context dimension expected by |
pyrox_name |
str | None
|
Optional explicit scope name. |
Examples:
>>> import jax.random as jr, jax.numpy as jnp
>>> import equinox as eqx
>>> key = jr.key(0)
>>> # Parameter net: (cond_dim=2,) -> (1*16 + 16 + 1 = 33,)
>>> pnet = eqx.nn.MLP(in_size=2, out_size=33, width_size=32, depth=2, key=key)
>>> hff = HyperFourierFeatures.init(
... parameter_net=pnet, in_features=1, n_features=16, cond_dim=2,
... )
>>> y = hff(jnp.ones((5, 1)), jnp.ones((2,)))
>>> y.shape
(5, 32)
Source code in packages/pyrox-nn/src/pyrox_nn/_conditioning.py
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 | |
init(*, parameter_net: PyroxModule | eqx.Module, in_features: int, n_features: int, cond_dim: int, pyrox_name: str | None = None) -> HyperFourierFeatures
classmethod
¶
Build HyperFourierFeatures.
parameter_net is not invoked at construction time, so
Bayesian / numpyro-aware parameter nets that rely on
pyrox_sample work without needing a seed handler at init.
The expected output size is in_features * n_features +
n_features + 1; a mismatch surfaces as a shape error on the
first __call__.
Source code in packages/pyrox-nn/src/pyrox_nn/_conditioning.py
ConditionedRFFNet
¶
Bases: PyroxModule
Conditional analogue of pyrox_nn.RandomKitchenSinks.
Composes a HyperFourierFeatures feature map with a learnable
linear readout. The full forward is
where \(\phi(x; z)\) is the HyperFourierFeatures output and
(beta, b_out) are the readout's deterministic weights. For the
Bayesian variant, wrap readout in a DenseReparameterization and
move the priors there — this composite stays minimal.
Attributes:
| Name | Type | Description |
|---|---|---|
feat |
HyperFourierFeatures
|
A |
readout |
Linear
|
|
pyrox_name |
str | None
|
Optional explicit scope name. |
Examples:
>>> import jax.random as jr, jax.numpy as jnp
>>> import equinox as eqx
>>> key = jr.key(0)
>>> pnet = eqx.nn.MLP(
... in_size=4, out_size=1 * 32 + 32 + 1, width_size=32, depth=2, key=key,
... )
>>> feat = HyperFourierFeatures.init(
... parameter_net=pnet, in_features=1, n_features=32, cond_dim=4,
... )
>>> net = ConditionedRFFNet.init(feat=feat, out_features=1, key=key)
>>> y = net(jnp.zeros((10, 1)), jnp.zeros((10, 4)))
>>> y.shape
(10, 1)
Source code in packages/pyrox-nn/src/pyrox_nn/_conditioning.py
init(*, feat: HyperFourierFeatures, out_features: int, key: Array, pyrox_name: str | None = None) -> ConditionedRFFNet
classmethod
¶
Build ConditionedRFFNet with a default linear readout.
Source code in packages/pyrox-nn/src/pyrox_nn/_conditioning.py
Composites¶
ConditionedINR
¶
Bases: Module
Wrap an inner network's per-layer activations with conditioners.
Given an inner network that exposes a layers sequence, as SIREN does, the
module runs the inner forward pass and inserts a conditioner after each layer
except the readout. The conditioners are those of gauss_flows_maps.conditioning.
Algorithm
For mode="feature" (the default):
- Set
h ← x. - For each non-readout layer
ℓ, seth ← layer_ℓ(h), thenh ← g_ℓ(h, c). - Return the readout
y = layer_L(h), which is not conditioned.
For mode="input", which suits inner networks with no layers sequence:
- Set
x ← g(x, c)with a single head conditioner. - Return
y = inner(x).
Initialisation
AffineModulation and ConcatConditioner start as the identity in h for
every context c (the default init="identity" of ConcatConditioner), so
the wrapped field starts as the plain inner network, y = inner(x), and
training moves it away from it. Pass init="default" to ConcatConditioner
or a non-zero readout_scale to AffineModulation, through
conditioner_kwargs, to start from a context-dependent field. HyperLinear
is not an identity: it generates the weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inner
|
Inner network with a |
required | |
conditioners
|
Per-layer conditioners, one for each non-readout layer in
|
required | |
cond_dim
|
Context dimension |
required | |
mode
|
|
required |
Examples:
Typical case, a four-layer SIREN with AffineModulation after each
hidden layer:
>>> import jax.random as jr, jax.numpy as jnp
>>> from geonnax import SIREN
>>> from gauss_flows_maps.conditioning import AffineModulation
>>> from geonnax.conditioning import ConditionedINR
>>> key = jr.key(0)
>>> inner = SIREN.init(2, 32, 1, depth=4, key=key)
>>> wrapped = ConditionedINR.init(
... inner, conditioner_cls=AffineModulation, cond_dim=4, key=key
... )
>>> y = wrapped(jnp.zeros(2), jnp.zeros(4))
>>> y.shape
(1,)
The conditioners start as the identity, so at init the field is the plain
SIREN for every context:
>>> x, c = jnp.array([0.3, -0.2]), jnp.arange(4.0)
>>> bool(jnp.allclose(wrapped(x, c), inner(x)))
True
Input-side conditioning, with one head conditioner on x:
>>> head = ConditionedINR.init(
... inner, conditioner_cls=AffineModulation, cond_dim=4, key=key,
... mode="input",
... )
>>> head(jnp.zeros(2), jnp.zeros(4)).shape
(1,)
Composition with vmap over a batch of pairs (x, c):
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/conditioning/_inr.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 | |
init(inner: eqx.Module, *, conditioner_cls: type[AbstractConditioner], cond_dim: int, key: PRNGKeyArray, mode: ConditionedMode = 'feature', **conditioner_kwargs: object) -> ConditionedINR
classmethod
¶
Build a ConditionedINR around inner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inner
|
Module
|
Inner network. It must have a |
required |
conditioner_cls
|
type[AbstractConditioner]
|
One of the classes of |
required |
cond_dim
|
int
|
Context dimension |
required |
key
|
PRNGKeyArray
|
PRNG key, split once for each conditioner. |
required |
mode
|
ConditionedMode
|
|
'feature'
|
**conditioner_kwargs
|
object
|
Extra keyword arguments forwarded to each
|
{}
|
Returns:
| Type | Description |
|---|---|
ConditionedINR
|
Initialised |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/conditioning/_inr.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
HyperSIREN(in_features: int, hidden_features: int, out_features: int, *, depth: int, cond_dim: int, parameter_net: eqx.Module, key: PRNGKeyArray, first_omega: float = 30.0, hidden_omega: float = 30.0, c: float = 6.0, init_scale: float = 1.0) -> GeneratedSiren
¶
Build the NIF-style ShapeNet and ParameterNet composite.
The composite generates the weights of a SIREN from a latent code,
z = P(μ), with each hidden layer h_ℓ = sin(ω_ℓ (W_ℓ(z) h_{ℓ-1} + b_ℓ(z))).
It builds a SIREN of the requested topology, then one HyperLinear generator per
layer (from gauss_flows_maps.conditioning). Each generator starts at its SIREN
layer's initial variance (hyperfan initialisation): Sitzmann's layer draws
W ~ U(−a, a) with a = siren_W_limit(...), whose standard deviation is
σ* = a/√3, and the generator is passed target_std = σ*, so that, averaged
over codes with 𝔼[z_k²] = 1, the generated W has standard deviation
s σ* for every K. Without this calibration the ShapeNet's pre-activation
variance is wrong and training is unstable.
Algorithm
- Compute the latent code
z = P(μ)once per call. - For each layer
ℓ, withh₀ = x, formpre_ℓ = W_ℓ(z) h_{ℓ-1} + b_ℓ(z). - Set
h_ℓ = sin(ω_ℓ pre_ℓ)for hidden layers, andh_L = pre_Lfor the readout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_features
|
int
|
Coordinate dimension |
required |
hidden_features
|
int
|
Hidden width. |
required |
out_features
|
int
|
Output dimension |
required |
depth
|
int
|
SIREN depth |
required |
cond_dim
|
int
|
Latent dimension |
required |
parameter_net
|
Module
|
Callable |
required |
key
|
PRNGKeyArray
|
PRNG key, split internally for the SIREN and the hyper generators. |
required |
first_omega
|
float
|
First-layer frequency |
30.0
|
hidden_omega
|
float
|
Hidden-layer frequency |
30.0
|
c
|
float
|
SIREN Theorem-1 constant. |
6.0
|
init_scale
|
float
|
Factor |
1.0
|
Returns:
| Type | Description |
|---|---|
GeneratedSiren
|
A composite that takes |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
References
Pan, Brunton and Kutz (2023). Neural implicit flow. JMLR. Sitzmann, V., et al. (2020). Implicit neural representations with periodic activation functions. NeurIPS. Chang, O., Flokas, L. and Lipson, H. (2020). Principled weight initialization for hypernetworks. ICLR.
Examples:
Typical case, a two-layer network from three inputs to two outputs:
>>> import jax.numpy as jnp
>>> import jax.random as jr
>>> import equinox as eqx
>>> pnet_key, build_key = jr.split(jr.PRNGKey(0))
>>> pnet = eqx.nn.MLP(
... in_size=2, out_size=4, width_size=8, depth=2, key=pnet_key
... )
>>> net = HyperSIREN(
... in_features=3, hidden_features=8, out_features=2, depth=2,
... cond_dim=4, parameter_net=pnet, key=build_key,
... )
>>> net(jnp.zeros(3), jnp.zeros(2)).shape
(2,)
Edge case, a depth below two is rejected:
>>> HyperSIREN(
... in_features=3, hidden_features=8, out_features=2, depth=1,
... cond_dim=4, parameter_net=pnet, key=build_key,
... )
Traceback (most recent call last):
ValueError: depth must be >= 2 (first + last); got depth=1.
Composition with vmap over a batch of pairs (x, μ):
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/conditioning/_inr.py
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 | |