NN API¶
The pyrox.nn subpackage ships uncertainty-aware neural network layers in four families:
- Geographic / spherical encoders (
pyrox.nn._geoplus deterministic wrappers inpyrox.nn._layers) — degree/radian, lon/lat, cyclic, and spherical-harmonic preprocessing for geophysical inputs. - Dense / Bayesian-linear layers (
pyrox.nn._layers) — twelve layers covering reparameterization, Flipout, NCP, MC-Dropout, and several random-feature variants. - Bayesian Neural Field stack (
pyrox.nn._bnf) — five layers that together implement the BNF architecture (Saad et al., Nat. Comms. 2024). - Pure-JAX feature helpers (
pyrox.nn._features) — pandas-free building blocks the BNF layers wrap.
See also: Geo encoders for the longitude/latitude and spherical-harmonic API surface.
Dense / Bayesian-linear layers¶
pyrox.nn.DenseReparameterization
¶
Bases: PyroxModule
Bayesian dense layer via the reparameterization trick.
Samples weight and bias from learned Gaussian posteriors at every forward pass. Registers NumPyro sample sites so the KL between the variational posterior and the prior is tracked by the ELBO.
.. math::
W \sim \mathcal{N}(\mu_W, \sigma_W^2), \quad
b \sim \mathcal{N}(\mu_b, \sigma_b^2), \quad
y = x W + b.
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension. |
out_features |
int
|
Output dimension. |
bias |
bool
|
Whether to include a bias term. |
prior_scale |
float
|
Scale of the isotropic Gaussian prior on weights and bias. The prior mean is zero. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
pyrox.nn.DenseFlipout
¶
Bases: PyroxModule
Bayesian dense layer with Flipout sign-flip structure.
Samples weight from the prior and applies per-example Rademacher sign flips to the weight perturbation (Wen et al., 2018). Under a NumPyro guide that learns the posterior mean, the sign flips decorrelate gradient estimates across minibatch examples.
In model mode (no guide) this is equivalent to
:class:DenseReparameterization — the Flipout variance reduction
activates when a guide provides a posterior centered at a learned
mean.
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension. |
out_features |
int
|
Output dimension. |
bias |
bool
|
Whether to include a bias term. |
prior_scale |
float
|
Scale of the isotropic Gaussian prior. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
pyrox.nn.DenseVariational
¶
Bases: PyroxModule
Dense layer with a user-supplied prior factory.
Provides flexibility over the weight prior by accepting a callable
that builds the prior distribution given the layer shape. The
model samples from the prior; the posterior is handled by a NumPyro
guide (e.g., AutoNormal).
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension. |
out_features |
int
|
Output dimension. |
make_prior |
Callable[..., Any]
|
Callable |
bias |
bool
|
Whether to include a bias term. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
pyrox.nn.MCDropout
¶
Bases: Module
Always-on dropout for Monte Carlo uncertainty estimation.
Unlike standard dropout, :class:MCDropout stays active at
inference time — repeated forward passes with different keys
produce a distribution of outputs whose spread approximates
predictive uncertainty (Gal & Ghahramani, 2016).
Not a :class:PyroxModule — no NumPyro sites are registered.
The stochasticity comes from the explicit PRNG key argument.
Attributes:
| Name | Type | Description |
|---|---|---|
rate |
float
|
Dropout probability in :math: |
Source code in src/pyrox/nn/_layers.py
__call__(x, *, key)
¶
Apply dropout, scaling survivors by 1 / (1 - rate).
Source code in src/pyrox/nn/_layers.py
pyrox.nn.DenseNCP
¶
Bases: PyroxModule
Noise Contrastive Prior dense layer (Hafner et al., 2019).
Decomposes a dense layer into a prior-regularized backbone plus a scaled stochastic perturbation:
.. math::
y = \underbrace{x W_d + b_d}_{\text{backbone}}
+ \underbrace{\sigma \cdot (x W_s + b_s)}_{\text{perturbation}},
where all weights are pyrox_sample sites with Gaussian priors
and :math:\sigma has a LogNormal prior. The backbone carries
the bulk of the signal; the perturbation branch adds calibrated
uncertainty that can be trained via a noise contrastive objective.
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension. |
out_features |
int
|
Output dimension. |
init_scale |
float
|
Initial value for the perturbation scale
:math: |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
pyrox.nn.NCPContinuousPerturb
¶
Bases: Module
Input perturbation for the Noise Contrastive Prior pattern.
Adds Gaussian noise scaled by a learned positive scale to the input:
.. math::
\tilde{x} = x + \sigma \epsilon, \qquad
\epsilon \sim \mathcal{N}(0, I).
Place before a deterministic network to inject input uncertainty;
pair with :class:DenseNCP at the output for the full NCP
architecture (Hafner et al., 2019).
Not a :class:PyroxModule — stochasticity comes from the
explicit PRNG key.
Attributes:
| Name | Type | Description |
|---|---|---|
scale |
float | Float[Array, '']
|
Perturbation scale :math: |
Source code in src/pyrox/nn/_layers.py
pyrox.nn.RBFFourierFeatures
¶
Bases: PyroxModule
SSGP-style RFF layer with RBF spectral density.
Both the spectral frequencies :math:W and the lengthscale
:math:\ell are pyrox_sample sites — :math:W has a
standard normal prior (the RBF spectral density) and :math:\ell
has a LogNormal prior. Under SVI, the guide learns a posterior
over both; under a seed handler, they are drawn from the prior.
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension. |
n_features |
int
|
Number of frequency pairs (output dim
|
init_lengthscale |
float
|
Prior location for the lengthscale. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
pyrox.nn.RBFCosineFeatures
¶
Bases: PyroxModule
Cosine-bias variant of random Fourier features for the RBF kernel.
Uses the single-cosine feature map with a bias term:
.. math::
\phi(x) = \sqrt{2 / D}\,\cos(x W / \ell + b)
where :math:W \sim \mathcal{N}(0, I) and
:math:b \sim \mathrm{Uniform}(0, 2\pi). This variant produces
n_features-dimensional output (half the dimension of the
[cos, sin] variant in :class:RBFFourierFeatures) and is
commonly used in Random Kitchen Sinks implementations.
All parameters (:math:W, :math:b, :math:\ell) are
pyrox_sample sites.
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension. |
n_features |
int
|
Number of random features (= output dimension). |
init_lengthscale |
float
|
Prior location for the lengthscale. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 | |
pyrox.nn.MaternFourierFeatures
¶
Bases: PyroxModule
SSGP-style RFF layer with Matern spectral density.
Spectral frequencies :math:W have a StudentT(df=2\nu) prior
(the Matern spectral density). The smoothness :math:\nu controls
the regularity: nu=0.5 (Laplace), nu=1.5 (Matern-3/2),
nu=2.5 (Matern-5/2).
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension. |
n_features |
int
|
Number of frequency pairs. |
nu |
float
|
Smoothness parameter :math: |
init_lengthscale |
float
|
Prior location for the lengthscale. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
pyrox.nn.MaternCosineFeatures
¶
Bases: PyroxModule
Cosine-bias variant of random Fourier features for the Matern kernel.
Single-cosine analogue of :class:MaternFourierFeatures:
.. math::
\phi(x) = \sqrt{2 / D}\,\cos(x W / \ell + b)
where :math:W \sim \mathrm{StudentT}(2\nu) (the Matern spectral
density) and :math:b \sim \mathrm{Uniform}(0, 2\pi). Output dim is
n_features (vs 2 * n_features for the [cos, sin]
variant). Approximates the same kernel as
:class:MaternFourierFeatures in expectation but with higher
variance per draw — see Sutherland & Schneider (2015).
All parameters (:math:W, :math:b, :math:\ell) are
pyrox_sample sites.
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension. |
n_features |
int
|
Number of random features (= output dimension). |
nu |
float
|
Smoothness parameter :math: |
init_lengthscale |
float
|
Prior location for the lengthscale. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 | |
pyrox.nn.LaplaceFourierFeatures
¶
Bases: PyroxModule
SSGP-style RFF layer with Laplace (Matern-1/2) spectral density.
Spectral frequencies :math:W have a Cauchy prior (Student-t
with df = 1).
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension. |
n_features |
int
|
Number of frequency pairs. |
init_lengthscale |
float
|
Prior location for the lengthscale. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
pyrox.nn.LaplaceCosineFeatures
¶
Bases: PyroxModule
Cosine-bias variant of random Fourier features for the Laplace kernel.
Single-cosine analogue of :class:LaplaceFourierFeatures (the
Matern-1/2 kernel):
.. math::
\phi(x) = \sqrt{2 / D}\,\cos(x W / \ell + b)
where :math:W \sim \mathrm{Cauchy}(0, 1) (Student-t with
df = 1) and :math:b \sim \mathrm{Uniform}(0, 2\pi). Output
dim is n_features.
All parameters (:math:W, :math:b, :math:\ell) are
pyrox_sample sites.
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension. |
n_features |
int
|
Number of random features (= output dimension). |
init_lengthscale |
float
|
Prior location for the lengthscale. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 | |
pyrox.nn.ArcCosineFourierFeatures
¶
Bases: PyroxModule
Random features for the arc-cosine kernel (Cho & Saul, 2009).
The arc-cosine kernel of order :math:p corresponds to an
infinite-width single-layer ReLU network. The random feature map
is:
.. math::
\phi(x) = \sqrt{2 / D}\,\max(0,\, x W / \ell)^p
where :math:W \sim \mathcal{N}(0, I).
order=0 gives the Heaviside (step) feature; order=1 gives
the ReLU feature (the most common); order=2 gives the squared
ReLU feature.
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension. |
n_features |
int
|
Number of random features (= output dimension). |
order |
int
|
Kernel order (0, 1, or 2). |
init_lengthscale |
float
|
Prior location for the lengthscale. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 | |
pyrox.nn.RandomKitchenSinks
¶
Bases: PyroxModule
Random Kitchen Sinks: RFF + a learned linear head.
Composes any RFF layer (:class:RBFFourierFeatures,
:class:MaternFourierFeatures, :class:LaplaceFourierFeatures)
with a trainable linear projection:
.. math::
y = \phi(x)\, \beta + b
The linear head (beta, bias) is registered via
pyrox_sample with Normal priors.
Attributes:
| Name | Type | Description |
|---|---|---|
rff |
RBFFourierFeatures | MaternFourierFeatures | LaplaceFourierFeatures
|
The underlying RFF feature layer. |
init_beta |
Float[Array, 'D_rff D_out']
|
Initial linear weights. |
init_bias |
Float[Array, ' D_out']
|
Initial bias vector. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
init(rff, out_features)
classmethod
¶
Construct from a pre-built RFF layer with zero-initialized head.
Source code in src/pyrox/nn/_layers.py
Wave-4 spectral layers (#41)¶
pyrox.nn.VariationalFourierFeatures
¶
Bases: PyroxModule
VSSGP — RFF with a learnable variational posterior over frequencies.
Standard RFF (e.g. :class:RBFFourierFeatures) treats the spectral
frequencies :math:W as a frozen prior draw; VSSGP (Gal & Turner,
2015) treats :math:W as a latent with a learnable mean-field
posterior, recovering spectral uncertainty on top of the
feature-space uncertainty.
Prior: :math:p(W) = \mathcal{N}(0, I) (RBF spectral density in
lengthscale-1 units). The lengthscale is itself a sampled site
(LogNormal(log init_lengthscale, 1)) so that frequencies are
rescaled to the physical kernel.
Under SVI, attach an :class:~numpyro.infer.autoguide.AutoNormal to
learn the posterior on W; under prior-only seeds, behaves
identically to :class:RBFFourierFeatures.
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension :math: |
n_features |
int
|
Number of frequency pairs (output dim |
init_lengthscale |
float
|
Prior location for the kernel lengthscale. |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 | |
pyrox.nn.OrthogonalRandomFeatures
¶
Bases: Module
Orthogonal Random Features (Yu et al., 2016) — variance-reduced RFF.
Frequencies are drawn from blocks of Haar-orthogonal matrices scaled by
independent chi-distributed magnitudes, giving the same RBF kernel
approximation as plain :class:RBFFourierFeatures in expectation but
with provably lower variance for finite n_features.
Frozen at construction time — no priors, no SVI on W. The frequency
matrix is built once from a key and stored as a static array.
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension :math: |
n_features |
int
|
Number of feature pairs. Must satisfy
|
lengthscale |
Float[Array, '']
|
Fixed kernel lengthscale (no prior; pass a value). |
W |
Float[Array, 'D_in D_orf']
|
Pre-built frequency matrix of shape |
Note
For learnable lengthscale or full Bayesian treatment of the
frequencies, prefer :class:VariationalFourierFeatures.
Source code in src/pyrox/nn/_layers.py
pyrox.nn.HSGPFeatures
¶
Bases: PyroxModule
Hilbert-Space Gaussian Process feature layer (Riutort-Mayol et al., 2023).
A deterministic Laplacian-eigenfunction basis on the bounded box
:math:[-L, L]^D plus learnable per-basis amplitudes with a
kernel-spectral-density prior:
.. math::
\hat{f}(x) = \sum_{j=1}^{M} \alpha_j\,\sqrt{S(\sqrt{\lambda_j})}\,\phi_j(x),
\quad \alpha_j \sim \mathcal{N}(0, 1).
This is the NN-side dual of :class:pyrox.gp.FourierInducingFeatures
— same basis, different prior wiring. As M and L grow, the
induced GP converges to the kernel passed in.
Attributes:
| Name | Type | Description |
|---|---|---|
in_features |
int
|
Input dimension :math: |
num_basis_per_dim |
tuple[int, ...]
|
Per-axis number of 1D eigenfunctions; total
basis count is |
L |
tuple[float, ...]
|
Per-axis box half-width. |
kernel |
Kernel
|
A stationary kernel from :mod: |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Source code in src/pyrox/nn/_layers.py
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 | |
SIREN — Sinusoidal Representation Networks¶
SIREN (Sitzmann, Martel, Bergman, Lindell, Wetzstein — NeurIPS 2020) replaces
ReLU/GELU with sin and prescribes a three-regime initialisation scheme that
keeps pre-activation variance stable across depth.
Three-regime weight initialisation (Theorem 1)¶
| Layer | W init |
Activation |
|---|---|---|
"first" |
U(-1/d_in, 1/d_in) |
sin(ω₀ · (W x + b)) |
"hidden" |
U(-√(c/d_in)/ω, √(c/d_in)/ω) |
sin(ω · (W x + b)) |
"last" |
U(-√(c/d_in), √(c/d_in)) |
none (linear) — W x + b |
Bias b is initialised U(-1/√d_in, 1/√d_in) for every regime.
Typical choice: ω₀ = ω = 30 for image / high-frequency INR tasks.
Usage¶
import jax.random as jr, jax.numpy as jnp
from pyrox.nn import SirenDense, SIREN, BayesianSIREN
# Single layer
layer = SirenDense.init(3, 64, key=jr.PRNGKey(0), layer_type="first")
y = layer(jnp.ones((5, 3))) # (5, 64)
# Multi-layer network (depth=5 → first + 3 hidden + last)
net = SIREN.init(2, 64, 1, depth=5, key=jr.PRNGKey(0))
y = net(jnp.zeros((100, 2))) # (100, 1)
# Bayesian variant (no key needed — weights come from the prior)
from numpyro import handlers
bnet = BayesianSIREN.init(2, 32, 1, depth=3)
with handlers.seed(rng_seed=0):
y = bnet(jnp.zeros((10, 2))) # (10, 1)
Alternative INR backbone
SIREN and GaborNet / FourierNet (MFN, #87) are complementary INR
backbones: SIREN composes nonlinearities deeply, while MFN uses a product
of Gabor filters. Choose based on the signal's smoothness profile.
pyrox.nn.SirenDense
¶
Bases: Module
Sine-activated dense layer: y = sin(ω · (W x + b)) or y = W x + b.
Single primitive of a SIREN network. Three init regimes (Sitzmann et al. 2020, Theorem 1):
+----------+-------------------------------------------+-------------+
| Regime | W init | Activation |
+==========+===========================================+=============+
| first | U(-1/d_in, 1/d_in) | sin(ω··)|
+----------+-------------------------------------------+-------------+
| hidden | U(-√(c/d_in)/ω, √(c/d_in)/ω) | sin(ω··)|
+----------+-------------------------------------------+-------------+
| last | U(-√(c/d_in), √(c/d_in)) | none |
+----------+-------------------------------------------+-------------+
Bias b is initialised U(-1/√d_in, 1/√d_in) for every regime.
Attributes:
| Name | Type | Description |
|---|---|---|
W |
Float[Array, 'in_features out_features']
|
Weight matrix of shape |
b |
Float[Array, ' out_features']
|
Bias vector of shape |
omega |
float
|
Frequency multiplier applied inside the sine. |
in_features |
int
|
Input dimension. |
out_features |
int
|
Output dimension. |
layer_type |
_SirenLayerType
|
One of |
c |
float
|
Constant from Theorem 1 (default 6.0). |
Example
import jax.random as jr layer = SirenDense.init(3, 16, key=jr.PRNGKey(0), layer_type="first") import jax.numpy as jnp y = layer(jnp.ones((5, 3))) y.shape (5, 16)
Source code in src/pyrox/nn/_layers.py
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 | |
__call__(x)
¶
Apply the sine-activated linear transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[Array, '*batch D_in']
|
Input tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch D_out']
|
|
Float[Array, '*batch D_out']
|
or |
Source code in src/pyrox/nn/_layers.py
init(in_features, out_features, *, key, omega=30.0, layer_type='hidden', c=6.0)
classmethod
¶
Construct a SirenDense with Sitzmann-regime weight initialisation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_features
|
int
|
Input dimension. |
required |
out_features
|
int
|
Output dimension. |
required |
key
|
Array
|
JAX PRNG key for weight and bias sampling. |
required |
omega
|
float
|
Frequency multiplier (default 30.0, as in Sitzmann et al.). |
30.0
|
layer_type
|
_SirenLayerType
|
Init regime — |
'hidden'
|
c
|
float
|
Theorem-1 constant (default 6.0). |
6.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Initialised |
SirenDense
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/pyrox/nn/_layers.py
pyrox.nn.SIREN
¶
Bases: Module
Multi-layer sinusoidal representation network (Sitzmann et al., NeurIPS 2020).
Topology:
.. math::
z_1 &= \sin(\omega_0 (W_0 x + b_0)), \\
z_{i+1} &= \sin(\omega (W_i z_i + b_i)), \quad i = 1 \ldots L-1, \\
y &= W_L z_L + b_L.
Each layer uses the corresponding Sitzmann Theorem 1 init regime
(:class:SirenDense): "first" for layer 0, "hidden" for
intermediate layers, and "last" for the readout.
depth counts all layers including the readout; depth=2 gives
one first-layer + one last-layer (no hidden layers); depth=5 gives
first + 3 hidden + last. Must be ≥ 2.
Attributes:
| Name | Type | Description |
|---|---|---|
layers |
list[SirenDense]
|
List of :class: |
in_features |
int
|
Input dimension. |
hidden_features |
int
|
Hidden dimension (all intermediate layers). |
out_features |
int
|
Output dimension. |
depth |
int
|
Total number of layers (including readout). Must be ≥ 2. |
first_omega |
float
|
Frequency multiplier for the first layer (default 30.0). |
hidden_omega |
float
|
Frequency multiplier for hidden layers (default 30.0). |
Example
import jax.random as jr, jax.numpy as jnp net = SIREN.init(2, 64, 1, depth=5, key=jr.PRNGKey(0)) net(jnp.zeros((10, 2))).shape (10, 1)
Source code in src/pyrox/nn/_layers.py
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 | |
__call__(x)
¶
Run the forward pass through all SIREN layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[Array, '*batch D_in']
|
Input tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch D_out']
|
Output tensor of shape |
Source code in src/pyrox/nn/_layers.py
init(in_features, hidden_features, out_features, *, depth, key, first_omega=30.0, hidden_omega=30.0, c=6.0)
classmethod
¶
Construct a SIREN with the correct per-layer init regimes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_features
|
int
|
Input dimension. |
required |
hidden_features
|
int
|
Hidden dimension for all intermediate layers. |
required |
out_features
|
int
|
Output dimension. |
required |
depth
|
int
|
Total layers including readout. Must be ≥ 2. |
required |
key
|
Array
|
JAX PRNG key. |
required |
first_omega
|
float
|
Frequency for the first layer (default 30.0). |
30.0
|
hidden_omega
|
float
|
Frequency for hidden layers (default 30.0). |
30.0
|
c
|
float
|
Theorem-1 constant passed to each :class: |
6.0
|
Returns:
| Name | Type | Description |
|---|---|---|
Initialised |
SIREN
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/pyrox/nn/_layers.py
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 | |
pyrox.nn.BayesianSIREN
¶
Bases: PyroxModule
SIREN with regime-scaled Normal priors on all layer weights.
Replaces the deterministic weight matrices of :class:SIREN with NumPyro
sample sites. For layer :math:i with Sitzmann Theorem 1 half-width
:math:a_i (the uniform bound used by :class:SirenDense):
.. math::
W_i \sim \mathcal{N}\!\left(0,\, \sigma_0 \cdot \frac{a_i}{\sqrt{3}}\right),
\qquad
b_i \sim \mathcal{N}\!\left(0,\,
\sigma_0 \cdot \frac{1}{\sqrt{3 \, d_i}}\right),
where :math:\sigma_0 is prior_std and :math:d_i is the input
dimension of layer :math:i. The :math:a_i / \sqrt{3} factor makes
:math:\operatorname{Var}(W_i) equal to the variance of Sitzmann's
:math:\mathcal{U}(-a_i, a_i) init exactly, so the Bayesian prior
preserves the activation variance prescribed by Theorem 1 — avoiding
the saturated-sine pathology that a flat :math:\mathcal{N}(0, 1)
prior would cause.
Registered sites: {scope}.layer_0.W, {scope}.layer_0.b, …,
{scope}.layer_{depth-1}.W, {scope}.layer_{depth-1}.b
— exactly 2 · depth sites per forward call.
Attributes:
| Name | Type | Description |
|---|---|---|
specs |
tuple[_SirenLayerSpec, ...]
|
Tuple of per-layer specs (static). Holds each layer's
|
in_features |
int
|
Input dimension. |
hidden_features |
int
|
Hidden dimension. |
out_features |
int
|
Output dimension. |
depth |
int
|
Total layers including readout. Must be ≥ 2. |
first_omega |
float
|
Frequency multiplier for the first layer. |
hidden_omega |
float
|
Frequency multiplier for hidden layers. |
prior_std |
float
|
Scale factor for the regime-scaled Normal prior (default 1.0). |
pyrox_name |
str | None
|
Explicit scope name for NumPyro site registration. |
Example
import jax.random as jr, jax.numpy as jnp from numpyro import handlers net = BayesianSIREN.init(2, 32, 1, depth=3) with handlers.seed(rng_seed=0): ... y = net(jnp.zeros((4, 2))) y.shape (4, 1)
Source code in src/pyrox/nn/_layers.py
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 | |
__call__(x)
¶
Sample weights from regime-scaled priors and run the forward pass.
Registers layer_{i}.W and layer_{i}.b NumPyro sample sites
for each layer i in [0, depth).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[Array, '*batch D_in']
|
Input tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, '*batch D_out']
|
Output tensor of shape |
Source code in src/pyrox/nn/_layers.py
init(in_features, hidden_features, out_features, *, depth, first_omega=30.0, hidden_omega=30.0, c=6.0, prior_std=1.0, pyrox_name=None)
classmethod
¶
Construct a :class:BayesianSIREN.
All weights come from the prior, so no PRNG key is needed at
construction time — the key enters when sampling inside a
numpyro handler (handlers.seed, SVI, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
in_features
|
int
|
Input dimension. |
required |
hidden_features
|
int
|
Hidden dimension. |
required |
out_features
|
int
|
Output dimension. |
required |
depth
|
int
|
Total layers including readout. Must be ≥ 2. |
required |
first_omega
|
float
|
Frequency for the first layer. |
30.0
|
hidden_omega
|
float
|
Frequency for hidden layers. |
30.0
|
c
|
float
|
Theorem-1 constant. |
6.0
|
prior_std
|
float
|
Scale factor for the Normal priors (default 1.0, must be > 0). |
1.0
|
pyrox_name
|
str | None
|
Optional explicit scope name for NumPyro. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Initialised |
BayesianSIREN
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/pyrox/nn/_layers.py
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 | |
Bayesian Neural Field stack¶
pyrox.nn.Standardization
¶
Bases: PyroxModule
Apply a fixed-coefficient affine standardization.
.. math::
\tilde x \;=\; \frac{x - \mu}{\sigma}.
Both mu and std are static (fit-time) constants, not
learned. Use :func:pyrox.preprocessing.fit_standardization to
construct from a pandas DataFrame.
Attributes:
| Name | Type | Description |
|---|---|---|
mu |
Float[Array, ' D']
|
Per-feature mean, shape |
std |
Float[Array, ' D']
|
Per-feature standard deviation, shape |
pyrox_name |
str | None
|
Optional override for the per-instance scope name. |
Source code in src/pyrox/nn/_bnf.py
pyrox.nn.FourierFeatures
¶
Bases: PyroxModule
Per-input dyadic-frequency Fourier basis.
For each input column, evaluates 2 * degree Fourier features at
frequencies :math:2\pi \cdot 2^d for :math:d \in \{0, \dots,
\text{degree} - 1\}. Concatenated across all columns.
Wraps :func:pyrox.nn._features.fourier_features per input
dimension.
Attributes:
| Name | Type | Description |
|---|---|---|
degrees |
tuple[int, ...]
|
Number of dyadic frequencies per input column, as a
Python |
rescale |
bool
|
If |
pyrox_name |
str | None
|
Optional scope-name override. |
Source code in src/pyrox/nn/_bnf.py
pyrox.nn.SeasonalFeatures
¶
Bases: PyroxModule
Period-and-harmonic cos/sin basis on a scalar time axis.
For each period :math:\tau_p with :math:H_p harmonics, emits
2 * H_p cos/sin columns. Total output width is :math:2 \sum_p
H_p.
Wraps :func:pyrox.nn._features.seasonal_features. Periods and
harmonics are kept as Python tuples (static) so the inner shape
structure is known at trace time.
Attributes:
| Name | Type | Description |
|---|---|---|
periods |
tuple[float, ...]
|
Period values, |
harmonics |
tuple[int, ...]
|
Harmonics per period, |
rescale |
bool
|
If |
pyrox_name |
str | None
|
Optional scope-name override. |
Source code in src/pyrox/nn/_bnf.py
pyrox.nn.InteractionFeatures
¶
Bases: PyroxModule
Element-wise products on selected pairs of input columns.
Wraps :func:pyrox.nn._features.interaction_features.
Attributes:
| Name | Type | Description |
|---|---|---|
pairs |
tuple[tuple[int, int], ...]
|
Index pairs, |
pyrox_name |
str | None
|
Optional scope-name override. |
Source code in src/pyrox/nn/_bnf.py
pyrox.nn.BayesianNeuralField
¶
Bases: PyroxModule
The full Bayesian Neural Field architecture.
A spatiotemporal MLP with:
- A learned per-input log-scale adjustment (Logistic(0, 1) prior).
- Four feature blocks concatenated into
h_0: rescaled inputs, Fourier features, seasonal features, interaction products. - Per-block
softplus(feature_gain)modulation. - A depth-
LMLP whose layers are :math:h_{\ell+1} = \sigma_\alpha\bigl(g_\ell \cdot W_\ell\, h_\ell / \sqrt{\lvert h_\ell \rvert}\bigr), where :math:\sigma_\alpha = \mathrm{sig}(\beta) \cdot \mathrm{elu} + (1 - \mathrm{sig}(\beta)) \cdot \mathrm{tanh}is a learned mixed activation. - A final linear layer scaled by
softplus(output_gain).
All weights, biases, gains, scales, and the activation logit carry
independent :math:\mathrm{Logistic}(0, 1) priors registered via
:meth:PyroxModule.pyrox_sample.
The :math:1/\sqrt{\text{fan-in}} pre-normalization is the
standard NTK-scaling trick — it makes the layer-wise prior
predictive a fan-in-independent Gaussian process in the
infinite-width limit (Lee et al., 2018).
Attributes:
| Name | Type | Description |
|---|---|---|
input_scales |
tuple[float, ...]
|
Per-input fixed scale (typically training-data
inter-quartile range). Static |
fourier_degrees |
tuple[int, ...]
|
Per-input number of dyadic Fourier
frequencies. Static |
interactions |
tuple[tuple[int, int], ...]
|
Pair-index list for interaction features. Static
|
seasonality_periods |
tuple[float, ...]
|
Periods for seasonal features. Static
|
num_seasonal_harmonics |
tuple[int, ...]
|
Harmonics per period. Static
|
width |
int
|
Hidden layer width. |
depth |
int
|
Number of hidden MLP layers. |
time_col |
int
|
Index of the time column inside |
pyrox_name |
str | None
|
Optional scope-name override. |
Source code in src/pyrox/nn/_bnf.py
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 274 275 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 | |
Pure-JAX feature helpers¶
pyrox.nn.fourier_features(x, max_degree, *, rescale=False)
¶
Cos/sin Fourier basis at dyadic frequencies.
For each input element and each degree :math:d \in \{0, \dots,
D-1\}, evaluates
.. math::
\phi_{d, \cos}(x) = \cos(2\pi \cdot 2^d \cdot x), \qquad
\phi_{d, \sin}(x) = \sin(2\pi \cdot 2^d \cdot x).
Returns the columns concatenated as [cos_0, ..., cos_{D-1},
sin_0, ..., sin_{D-1}], matching Google's bayesnf layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[Array, ' N']
|
Length- |
required |
max_degree
|
int
|
Number of dyadic frequencies |
required |
rescale
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'N two_max_degree']
|
Array of shape |
Source code in src/pyrox/nn/_features.py
pyrox.nn.seasonal_features(x, periods, harmonics, *, rescale=False)
¶
Cos/sin features at multiples of :math:2\pi / \tau_p.
For each period :math:\tau_p with :math:H_p harmonics, evaluates
.. math::
\phi_{p, h, \cos}(x) = \cos(2\pi h x / \tau_p), \qquad
\phi_{p, h, \sin}(x) = \sin(2\pi h x / \tau_p),
for :math:h = 1, \dots, H_p. Returns the cos columns concatenated
with the sin columns, length :math:F = \sum_p H_p each.
periods and harmonics are Python sequences (tuples,
lists, or 0-d JAX arrays wrapped at the call site). Keeping them as
Python values lets the function run cleanly under jax.jit and
lax.scan without triggering a concretization error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[Array, ' N']
|
Time/index input, shape |
required |
periods
|
Sequence[float]
|
Period values. |
required |
harmonics
|
Sequence[int]
|
Harmonics per period. |
required |
rescale
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'N two_F']
|
Array of shape |
Source code in src/pyrox/nn/_features.py
pyrox.nn.seasonal_frequencies(periods, harmonics)
¶
Flatten (period, harmonic_count) pairs into Python lists.
For each period :math:\tau_p with :math:H_p harmonics, emits
frequencies :math:f_{p, h} = h / \tau_p for :math:h = 1, \dots,
H_p. The total length is :math:F = \sum_p H_p.
Inputs are Python sequences, not JAX arrays, so this helper
runs at trace time and never triggers a concretization error under
jax.jit. Most callers won't use it directly; it's exposed for
symmetry with :func:seasonal_features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
periods
|
Sequence[float]
|
Period values. |
required |
harmonics
|
Sequence[int]
|
Number of harmonics per period. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
|
list[float]
|
math: |
Source code in src/pyrox/nn/_features.py
pyrox.nn.interaction_features(x, pairs)
¶
Element-wise products on selected pairs of input columns.
For each pair :math:(i, j) and each row :math:n, computes
:math:x_{n, i} \cdot x_{n, j}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Float[Array, 'N D']
|
Input matrix, shape |
required |
pairs
|
Int[Array, 'K 2']
|
Index pairs, shape |
required |
Returns:
| Type | Description |
|---|---|
Float[Array, 'N K']
|
Array of shape |
Source code in src/pyrox/nn/_features.py
pyrox.nn.standardize(x, mu, std)
¶
Affine standardize: (x - mu) / std.
Broadcasts mu and std against x per the JAX broadcasting
rules. std is not clamped; pass a positive value or guard
upstream.
Source code in src/pyrox/nn/_features.py
pyrox.nn.unstandardize(z, mu, std)
¶
Inverse of :func:standardize: z * std + mu.