Skip to content

Multiplicative Filter Networks (MFN)

Multiplicative Filter Networks (Fathony, Sahu, Willmott, Kolter — ICLR 2021) replace MLP composition with multiplicative filter chaining. Instead of deeply composing nonlinearities, each layer multiplies the previous activation by a new filter evaluated directly on the original input:

\[ z_1 = g_1(x), \qquad z_{i+1} = g_{i+1}(x) \odot \bigl(W_i z_i + b_i\bigr), \qquad y = W_L z_L + b_L. \]

Two filter families ship in pyrox_nn:

  • FourierNet — \(g_i(x) = \sin(\Omega_i x + \varphi_i)\), frequency-domain filters. Products of sinusoids span exponentially many frequencies with depth \(L\).
  • GaborNet — \(g_i(x) = \sin(\Omega_i x + \varphi_i) \odot \exp(-\tfrac{\gamma_i}{2}\|x - \mu_i\|^2)\), Gabor atoms with learned frequency \(\Omega_i\), phase \(\varphi_i\), location \(\mu_i\), and bandwidth \(\gamma_i\).

Connection to RBFFourierFeatures: A GaborNet with depth=1 and \(\mu = 0\) is a localized variant of random Fourier features. As \(\gamma \to 0\) (very wide envelope) it recovers the plain RBF-RFF feature map. See HSGPFeatures for the related Hilbert-space GP basis.

Quick example

import jax.random as jr
from pyrox_nn import GaborNet
from numpyro import handlers

key = jr.PRNGKey(0)

# Deterministic GaborNet
net = GaborNet.init(in_features=2, hidden_features=64, out_features=1, depth=3, key=key)

import jax.numpy as jnp
x = jnp.ones((100, 2))
y = net(x)          # (100, 1)

# Bayesian GaborNet — sample sites registered for every parameter
from pyrox_nn import BayesianGaborNet
bnet = BayesianGaborNet.init(
    in_features=2, hidden_features=64, out_features=1, depth=3, key=key,
    pyrox_name="gabor",
)
with handlers.seed(rng_seed=1):
    y_sample = bnet(x)  # weights sampled from prior

Filter primitives

FourierFilter

Bases: Module

Single Fourier filter, g(x) = sin(Ω x + φ).

One multiplicative filter primitive for use inside a FourierNet. Following Fathony et al. (2021, §4.1), the frequencies are drawn as Ω_ij ~ 𝒩(0, σ_f² / D) with D = in_features, and the phases as φ_i ~ Uniform(-π, π).

Parameters:

Name Type Description Default
Omega

Frequency matrix Ω ∈ ℝ^(H×D).

required
phi

Phase vector φ ∈ ℝ^H.

required
in_features

Input dimension D.

required
out_features

Output (filter) dimension H.

required

Examples:

A filter from three inputs to eight outputs:

>>> import jax.numpy as jnp, jax.random as jr
>>> from geonnax.fields import FourierFilter
>>> f = FourierFilter.init(3, 8, key=jr.PRNGKey(0))
>>> f(jnp.ones(3)).shape
(8,)

A one-dimensional input, with every response bounded by one:

>>> f_1d = FourierFilter.init(1, 4, key=jr.PRNGKey(0))
>>> bool(jnp.all(jnp.abs(f_1d(jnp.zeros(1))) <= 1.0))
True

Composition with vmap over a batch of inputs:

>>> import jax
>>> jax.vmap(f)(jnp.ones((5, 3))).shape
(5, 8)
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/fields/_mfn.py
class FourierFilter(eqx.Module):
    """Single Fourier filter, `g(x) = sin(Ω x + φ)`.

    One multiplicative filter primitive for use inside a `FourierNet`. Following
    Fathony et al. (2021, §4.1), the frequencies are drawn as
    `Ω_ij ~ 𝒩(0, σ_f² / D)` with `D = in_features`, and the phases as
    `φ_i ~ Uniform(-π, π)`.

    Args:
        Omega: Frequency matrix `Ω ∈ ℝ^(H×D)`.
        phi: Phase vector `φ ∈ ℝ^H`.
        in_features: Input dimension `D`.
        out_features: Output (filter) dimension `H`.

    Examples:
        A filter from three inputs to eight outputs:

        >>> import jax.numpy as jnp, jax.random as jr
        >>> from geonnax.fields import FourierFilter
        >>> f = FourierFilter.init(3, 8, key=jr.PRNGKey(0))
        >>> f(jnp.ones(3)).shape
        (8,)

        A one-dimensional input, with every response bounded by one:

        >>> f_1d = FourierFilter.init(1, 4, key=jr.PRNGKey(0))
        >>> bool(jnp.all(jnp.abs(f_1d(jnp.zeros(1))) <= 1.0))
        True

        Composition with `vmap` over a batch of inputs:

        >>> import jax
        >>> jax.vmap(f)(jnp.ones((5, 3))).shape
        (5, 8)
    """

    Omega: Float[Array, "out in"]
    phi: Float[Array, " out"]
    in_features: int = eqx.field(static=True)
    out_features: int = eqx.field(static=True)

    @classmethod
    def init(
        cls,
        in_features: int,
        out_features: int,
        *,
        key: PRNGKeyArray,
        freq_scale: float = 256.0,
    ) -> FourierFilter:
        """Construct with the Fathony et al. (2021, §4.1) initialisation.

        Args:
            in_features: Input dimension `D`.
            out_features: Output (filter) dimension `H`.
            key: PRNG key for the frequencies and phases.
            freq_scale: Frequency scale `σ_f`, which sets the frequency spread.

        Returns:
            A `FourierFilter` with `Ω` and `φ` drawn from the published law.

        Raises:
            ValueError: If `in_features`, `out_features` or `freq_scale` is not
                positive.

        Examples:
            >>> import jax.numpy as jnp, jax.random as jr
            >>> from geonnax.fields import FourierFilter
            >>> f = FourierFilter.init(3, 8, key=jr.PRNGKey(0))
            >>> f(jnp.ones(3)).shape
            (8,)
        """
        _require_positive(
            in_features=in_features,
            out_features=out_features,
            freq_scale=freq_scale,
        )
        k_omega, k_phi = jax.random.split(key)
        # σ_ω = σ_f / √D
        omega_std = freq_scale / math.sqrt(in_features)
        # Ω ~ 𝒩(0, σ_ω²)
        Omega = jax.random.normal(k_omega, (out_features, in_features)) * omega_std
        # φ ~ Uniform(-π, π)
        phi = jax.random.uniform(k_phi, (out_features,), minval=-jnp.pi, maxval=jnp.pi)
        return cls(
            Omega=Omega,
            phi=phi,
            in_features=in_features,
            out_features=out_features,
        )

    def __call__(self, x: Float[Array, " D"]) -> Float[Array, " H"]:
        """Evaluate the filter `g(x) = sin(Ω x + φ)`.

        Args:
            x: Input vector `x ∈ ℝ^D`.

        Returns:
            Filter response `g(x) ∈ ℝ^H`.

        Examples:
            >>> import jax.numpy as jnp, jax.random as jr
            >>> from geonnax.fields import FourierFilter
            >>> f = FourierFilter.init(2, 6, key=jr.PRNGKey(0))
            >>> f(jnp.zeros(2)).shape
            (6,)
        """
        # proj = Ω x
        proj = einx.dot("d, h d -> h", x, self.Omega)
        # g(x) = sin(proj + φ)
        return jnp.sin(proj + self.phi)

init(in_features: int, out_features: int, *, key: PRNGKeyArray, freq_scale: float = 256.0) -> FourierFilter classmethod

Construct with the Fathony et al. (2021, §4.1) initialisation.

Parameters:

Name Type Description Default
in_features int

Input dimension D.

required
out_features int

Output (filter) dimension H.

required
key PRNGKeyArray

PRNG key for the frequencies and phases.

required
freq_scale float

Frequency scale σ_f, which sets the frequency spread.

256.0

Returns:

Type Description
FourierFilter

A FourierFilter with Ω and φ drawn from the published law.

Raises:

Type Description
ValueError

If in_features, out_features or freq_scale is not positive.

Examples:

>>> import jax.numpy as jnp, jax.random as jr
>>> from geonnax.fields import FourierFilter
>>> f = FourierFilter.init(3, 8, key=jr.PRNGKey(0))
>>> f(jnp.ones(3)).shape
(8,)
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/fields/_mfn.py
@classmethod
def init(
    cls,
    in_features: int,
    out_features: int,
    *,
    key: PRNGKeyArray,
    freq_scale: float = 256.0,
) -> FourierFilter:
    """Construct with the Fathony et al. (2021, §4.1) initialisation.

    Args:
        in_features: Input dimension `D`.
        out_features: Output (filter) dimension `H`.
        key: PRNG key for the frequencies and phases.
        freq_scale: Frequency scale `σ_f`, which sets the frequency spread.

    Returns:
        A `FourierFilter` with `Ω` and `φ` drawn from the published law.

    Raises:
        ValueError: If `in_features`, `out_features` or `freq_scale` is not
            positive.

    Examples:
        >>> import jax.numpy as jnp, jax.random as jr
        >>> from geonnax.fields import FourierFilter
        >>> f = FourierFilter.init(3, 8, key=jr.PRNGKey(0))
        >>> f(jnp.ones(3)).shape
        (8,)
    """
    _require_positive(
        in_features=in_features,
        out_features=out_features,
        freq_scale=freq_scale,
    )
    k_omega, k_phi = jax.random.split(key)
    # σ_ω = σ_f / √D
    omega_std = freq_scale / math.sqrt(in_features)
    # Ω ~ 𝒩(0, σ_ω²)
    Omega = jax.random.normal(k_omega, (out_features, in_features)) * omega_std
    # φ ~ Uniform(-π, π)
    phi = jax.random.uniform(k_phi, (out_features,), minval=-jnp.pi, maxval=jnp.pi)
    return cls(
        Omega=Omega,
        phi=phi,
        in_features=in_features,
        out_features=out_features,
    )

GaborFilter

Bases: Module

Single Gabor filter, a sinusoid under a Gaussian envelope.

g(x) = sin(Ω x + φ) ⊙ exp(-γ/2 · ‖x - μ‖²)

One multiplicative filter primitive for use inside a GaborNet. Following Fathony et al. (2021, §4.2), each bandwidth is drawn as γ_i ~ Gamma(α, β), each centre as μ_i ~ Uniform(domain), and the frequencies as Ω_i ~ 𝒩(0, γ_i I_D), which ties the oscillation to the envelope width. The bandwidth is stored as log γ, so positivity holds without constraints.

Parameters:

Name Type Description Default
Omega

Frequency matrix Ω ∈ ℝ^(H×D).

required
phi

Phase vector φ ∈ ℝ^H.

required
mu

Envelope centres μ ∈ ℝ^(H×D).

required
log_gamma

Log bandwidth log γ ∈ ℝ^H.

required
in_features

Input dimension D.

required
out_features

Output (filter) dimension H.

required
domain

Pair (low, high) that bounds the centres μ, static.

required

Examples:

A filter from two inputs to six outputs:

>>> import jax.numpy as jnp, jax.random as jr
>>> from geonnax.fields import GaborFilter
>>> g = GaborFilter.init(2, 6, key=jr.PRNGKey(0))
>>> g(jnp.zeros(2)).shape
(6,)

A one-dimensional input with a wider centre domain:

>>> g_1d = GaborFilter.init(1, 4, key=jr.PRNGKey(0), domain=(-3.0, 3.0))
>>> g_1d(jnp.zeros(1)).shape
(4,)

Composition with vmap over a batch of inputs:

>>> import jax
>>> jax.vmap(g)(jnp.ones((5, 2))).shape
(5, 6)
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/fields/_mfn.py
class GaborFilter(eqx.Module):
    """Single Gabor filter, a sinusoid under a Gaussian envelope.

    `g(x) = sin(Ω x + φ) ⊙ exp(-γ/2 · ‖x - μ‖²)`

    One multiplicative filter primitive for use inside a `GaborNet`. Following
    Fathony et al. (2021, §4.2), each bandwidth is drawn as
    `γ_i ~ Gamma(α, β)`, each centre as `μ_i ~ Uniform(domain)`, and the frequencies
    as `Ω_i ~ 𝒩(0, γ_i I_D)`, which ties the oscillation to the envelope width.
    The bandwidth is stored as `log γ`, so positivity holds without constraints.

    Args:
        Omega: Frequency matrix `Ω ∈ ℝ^(H×D)`.
        phi: Phase vector `φ ∈ ℝ^H`.
        mu: Envelope centres `μ ∈ ℝ^(H×D)`.
        log_gamma: Log bandwidth `log γ ∈ ℝ^H`.
        in_features: Input dimension `D`.
        out_features: Output (filter) dimension `H`.
        domain: Pair `(low, high)` that bounds the centres `μ`, static.

    Examples:
        A filter from two inputs to six outputs:

        >>> import jax.numpy as jnp, jax.random as jr
        >>> from geonnax.fields import GaborFilter
        >>> g = GaborFilter.init(2, 6, key=jr.PRNGKey(0))
        >>> g(jnp.zeros(2)).shape
        (6,)

        A one-dimensional input with a wider centre domain:

        >>> g_1d = GaborFilter.init(1, 4, key=jr.PRNGKey(0), domain=(-3.0, 3.0))
        >>> g_1d(jnp.zeros(1)).shape
        (4,)

        Composition with `vmap` over a batch of inputs:

        >>> import jax
        >>> jax.vmap(g)(jnp.ones((5, 2))).shape
        (5, 6)
    """

    Omega: Float[Array, "out in"]
    phi: Float[Array, " out"]
    mu: Float[Array, "out in"]
    log_gamma: Float[Array, " out"]
    in_features: int = eqx.field(static=True)
    out_features: int = eqx.field(static=True)
    domain: tuple[float, float] = eqx.field(static=True)

    @classmethod
    def init(
        cls,
        in_features: int,
        out_features: int,
        *,
        key: PRNGKeyArray,
        domain: tuple[float, float] = (-1.0, 1.0),
        gamma_alpha: float = 6.0,
        gamma_beta: float = 1.0,
    ) -> GaborFilter:
        """Construct with the Fathony et al. (2021, §4.2) initialisation.

        Args:
            in_features: Input dimension `D`.
            out_features: Output (filter) dimension `H`.
            key: PRNG key for the bandwidths, centres, frequencies and phases.
            domain: Pair `(low, high)` that bounds the centres `μ`.
            gamma_alpha: Shape `α` of the Gamma prior on the bandwidths.
            gamma_beta: Rate `β` of the Gamma prior on the bandwidths.

        Returns:
            A `GaborFilter` with the tied initialisation of `Ω`.

        Raises:
            ValueError: If a size or a Gamma parameter is not positive, or if
                `domain` does not satisfy `low < high`.

        Examples:
            >>> import jax.numpy as jnp, jax.random as jr
            >>> from geonnax.fields import GaborFilter
            >>> g = GaborFilter.init(2, 6, key=jr.PRNGKey(0))
            >>> g(jnp.zeros(2)).shape
            (6,)
        """
        _require_positive(
            in_features=in_features,
            out_features=out_features,
            gamma_alpha=gamma_alpha,
            gamma_beta=gamma_beta,
        )
        if domain[0] >= domain[1]:
            raise ValueError(f"domain must satisfy low < high; got domain={domain}.")
        k_gamma, k_mu, k_omega, k_phi = jax.random.split(key, 4)
        # γ ~ Gamma(α, β), sampled as Gamma(α, 1) divided by β
        gamma = jax.random.gamma(k_gamma, gamma_alpha, (out_features,)) / gamma_beta
        # log γ
        log_gamma = jnp.log(gamma)
        # μ ~ Uniform(low, high)
        mu = jax.random.uniform(
            k_mu, (out_features, in_features), minval=domain[0], maxval=domain[1]
        )
        # Ω_i ~ 𝒩(0, γ_i I_D), the tied initialisation
        Omega = jax.random.normal(k_omega, (out_features, in_features)) * jnp.sqrt(
            einx.id("h -> h 1", gamma)
        )
        # φ ~ Uniform(-π, π)
        phi = jax.random.uniform(k_phi, (out_features,), minval=-jnp.pi, maxval=jnp.pi)
        return cls(
            Omega=Omega,
            phi=phi,
            mu=mu,
            log_gamma=log_gamma,
            in_features=in_features,
            out_features=out_features,
            domain=domain,
        )

    def __call__(self, x: Float[Array, " D"]) -> Float[Array, " H"]:
        """Evaluate `g(x) = sin(Ω x + φ) ⊙ exp(-γ/2 · ‖x - μ‖²)`.

        Args:
            x: Input vector `x ∈ ℝ^D`.

        Returns:
            Filter response `g(x) ∈ ℝ^H`.

        Examples:
            >>> import jax.numpy as jnp, jax.random as jr
            >>> from geonnax.fields import GaborFilter
            >>> g = GaborFilter.init(3, 4, key=jr.PRNGKey(1))
            >>> g(jnp.ones(3)).shape
            (4,)
        """
        # γ = exp(log γ), positive by construction
        gamma = jnp.exp(self.log_gamma)
        # ‖x‖²
        x_norm_sq = jnp.sum(x**2)
        # ‖μ_i‖²
        mu_norm_sq = einx.sum("h [d]", self.mu**2)
        # x·μ_i
        cross = einx.dot("d, h d -> h", x, self.mu)
        # ‖x - μ_i‖² = ‖x‖² + ‖μ_i‖² - 2 x·μ_i, floored at zero against round-off
        sq_dist = jnp.maximum(x_norm_sq + mu_norm_sq - 2.0 * cross, 0.0)
        # envelope = exp(-γ/2 ‖x - μ‖²)
        envelope = jnp.exp(-0.5 * gamma * sq_dist)
        # sinusoidal = sin(Ω x + φ)
        sinusoidal = jnp.sin(einx.dot("d, h d -> h", x, self.Omega) + self.phi)
        # g(x) = sinusoidal ⊙ envelope
        return sinusoidal * envelope

init(in_features: int, out_features: int, *, key: PRNGKeyArray, domain: tuple[float, float] = (-1.0, 1.0), gamma_alpha: float = 6.0, gamma_beta: float = 1.0) -> GaborFilter classmethod

Construct with the Fathony et al. (2021, §4.2) initialisation.

Parameters:

Name Type Description Default
in_features int

Input dimension D.

required
out_features int

Output (filter) dimension H.

required
key PRNGKeyArray

PRNG key for the bandwidths, centres, frequencies and phases.

required
domain tuple[float, float]

Pair (low, high) that bounds the centres μ.

(-1.0, 1.0)
gamma_alpha float

Shape α of the Gamma prior on the bandwidths.

6.0
gamma_beta float

Rate β of the Gamma prior on the bandwidths.

1.0

Returns:

Type Description
GaborFilter

A GaborFilter with the tied initialisation of Ω.

Raises:

Type Description
ValueError

If a size or a Gamma parameter is not positive, or if domain does not satisfy low < high.

Examples:

>>> import jax.numpy as jnp, jax.random as jr
>>> from geonnax.fields import GaborFilter
>>> g = GaborFilter.init(2, 6, key=jr.PRNGKey(0))
>>> g(jnp.zeros(2)).shape
(6,)
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/fields/_mfn.py
@classmethod
def init(
    cls,
    in_features: int,
    out_features: int,
    *,
    key: PRNGKeyArray,
    domain: tuple[float, float] = (-1.0, 1.0),
    gamma_alpha: float = 6.0,
    gamma_beta: float = 1.0,
) -> GaborFilter:
    """Construct with the Fathony et al. (2021, §4.2) initialisation.

    Args:
        in_features: Input dimension `D`.
        out_features: Output (filter) dimension `H`.
        key: PRNG key for the bandwidths, centres, frequencies and phases.
        domain: Pair `(low, high)` that bounds the centres `μ`.
        gamma_alpha: Shape `α` of the Gamma prior on the bandwidths.
        gamma_beta: Rate `β` of the Gamma prior on the bandwidths.

    Returns:
        A `GaborFilter` with the tied initialisation of `Ω`.

    Raises:
        ValueError: If a size or a Gamma parameter is not positive, or if
            `domain` does not satisfy `low < high`.

    Examples:
        >>> import jax.numpy as jnp, jax.random as jr
        >>> from geonnax.fields import GaborFilter
        >>> g = GaborFilter.init(2, 6, key=jr.PRNGKey(0))
        >>> g(jnp.zeros(2)).shape
        (6,)
    """
    _require_positive(
        in_features=in_features,
        out_features=out_features,
        gamma_alpha=gamma_alpha,
        gamma_beta=gamma_beta,
    )
    if domain[0] >= domain[1]:
        raise ValueError(f"domain must satisfy low < high; got domain={domain}.")
    k_gamma, k_mu, k_omega, k_phi = jax.random.split(key, 4)
    # γ ~ Gamma(α, β), sampled as Gamma(α, 1) divided by β
    gamma = jax.random.gamma(k_gamma, gamma_alpha, (out_features,)) / gamma_beta
    # log γ
    log_gamma = jnp.log(gamma)
    # μ ~ Uniform(low, high)
    mu = jax.random.uniform(
        k_mu, (out_features, in_features), minval=domain[0], maxval=domain[1]
    )
    # Ω_i ~ 𝒩(0, γ_i I_D), the tied initialisation
    Omega = jax.random.normal(k_omega, (out_features, in_features)) * jnp.sqrt(
        einx.id("h -> h 1", gamma)
    )
    # φ ~ Uniform(-π, π)
    phi = jax.random.uniform(k_phi, (out_features,), minval=-jnp.pi, maxval=jnp.pi)
    return cls(
        Omega=Omega,
        phi=phi,
        mu=mu,
        log_gamma=log_gamma,
        in_features=in_features,
        out_features=out_features,
        domain=domain,
    )

Composite networks

FourierNet

Bases: Module

Multiplicative Fourier Filter Network (Fathony et al., 2021).

Chains FourierFilter primitives multiplicatively:

z₁ = g₁(x),    z_{i+1} = g_{i+1}(x) ⊙ (W_i z_i + b_i),    y = W_L z_L + b_L.

Each g_i is a FourierFilter of width hidden_features. The last linear is the readout, which projects to out_features.

Parameters:

Name Type Description Default
filters

Length-depth list of FourierFilter, the filters g_i.

required
linears

Length-depth list of equinox.nn.Linear, the maps W_i, b_i.

required
in_features

Input dimension D.

required
hidden_features

Filter and hidden width H.

required
out_features

Output dimension O.

required
depth

Number of filter layers L.

required

Examples:

A network from two inputs to one output:

>>> import jax.numpy as jnp, jax.random as jr
>>> from geonnax.fields import FourierNet
>>> net = FourierNet.init(2, 16, 1, depth=3, key=jr.PRNGKey(0))
>>> net(jnp.zeros(2)).shape
(1,)

A one-dimensional input with four outputs:

>>> net_1d = FourierNet.init(1, 8, 4, depth=2, key=jr.PRNGKey(0))
>>> net_1d(jnp.ones(1)).shape
(4,)

Composition with vmap over a batch of inputs:

>>> import jax
>>> jax.vmap(net)(jnp.ones((5, 2))).shape
(5, 1)
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/fields/_mfn.py
class FourierNet(eqx.Module):
    """Multiplicative Fourier Filter Network (Fathony et al., 2021).

    Chains `FourierFilter` primitives multiplicatively:

        z₁ = g₁(x),    z_{i+1} = g_{i+1}(x) ⊙ (W_i z_i + b_i),    y = W_L z_L + b_L.

    Each `g_i` is a `FourierFilter` of width `hidden_features`. The last linear is
    the readout, which projects to `out_features`.

    Args:
        filters: Length-`depth` list of `FourierFilter`, the filters `g_i`.
        linears: Length-`depth` list of `equinox.nn.Linear`, the maps `W_i, b_i`.
        in_features: Input dimension `D`.
        hidden_features: Filter and hidden width `H`.
        out_features: Output dimension `O`.
        depth: Number of filter layers `L`.

    Examples:
        A network from two inputs to one output:

        >>> import jax.numpy as jnp, jax.random as jr
        >>> from geonnax.fields import FourierNet
        >>> net = FourierNet.init(2, 16, 1, depth=3, key=jr.PRNGKey(0))
        >>> net(jnp.zeros(2)).shape
        (1,)

        A one-dimensional input with four outputs:

        >>> net_1d = FourierNet.init(1, 8, 4, depth=2, key=jr.PRNGKey(0))
        >>> net_1d(jnp.ones(1)).shape
        (4,)

        Composition with `vmap` over a batch of inputs:

        >>> import jax
        >>> jax.vmap(net)(jnp.ones((5, 2))).shape
        (5, 1)
    """

    filters: list[FourierFilter]
    linears: list[eqx.nn.Linear]
    in_features: int = eqx.field(static=True)
    hidden_features: int = eqx.field(static=True)
    out_features: int = eqx.field(static=True)
    depth: int = eqx.field(static=True)

    @classmethod
    def init(
        cls,
        in_features: int,
        hidden_features: int,
        out_features: int,
        *,
        depth: int,
        key: PRNGKeyArray,
        freq_scale: float = 256.0,
    ) -> FourierNet:
        """Construct a `FourierNet` with `depth` filters and readout linears.

        Args:
            in_features: Input dimension `D`.
            hidden_features: Filter and hidden width `H`.
            out_features: Output dimension `O`.
            depth: Number of filter layers `L`, at least one.
            key: PRNG key, split into one key per filter and one per linear.
            freq_scale: Frequency scale `σ_f` of each filter.

        Returns:
            A `FourierNet` with `depth` filters and `depth` linears.

        Raises:
            ValueError: If `depth` is below one.
        """
        if depth < 1:
            raise ValueError(f"depth must be at least 1, got {depth}.")
        # one key per filter, then one per linear
        keys = jax.random.split(key, 2 * depth)
        filter_keys = keys[:depth]
        linear_keys = keys[depth:]
        filters = [
            FourierFilter.init(
                in_features, hidden_features, key=filter_keys[i], freq_scale=freq_scale
            )
            for i in range(depth)
        ]
        linears = [
            eqx.nn.Linear(
                hidden_features,
                hidden_features if i < depth - 1 else out_features,
                key=linear_keys[i],
            )
            for i in range(depth)
        ]
        return cls(
            filters=filters,
            linears=linears,
            in_features=in_features,
            hidden_features=hidden_features,
            out_features=out_features,
            depth=depth,
        )

    def __call__(self, x: Float[Array, " D"]) -> Float[Array, " O"]:
        """Run the multiplicative Fourier-filter forward pass.

        Args:
            x: Input vector `x ∈ ℝ^D`.

        Returns:
            Output vector `y ∈ ℝ^O`.

        Examples:
            >>> import jax.numpy as jnp, jax.random as jr
            >>> from geonnax.fields import FourierNet
            >>> net = FourierNet.init(2, 8, 4, depth=2, key=jr.PRNGKey(0))
            >>> net(jnp.ones(2)).shape
            (4,)
        """
        # chain the filters, see mfn_forward
        return mfn_forward(x, self.filters, self.linears)

init(in_features: int, hidden_features: int, out_features: int, *, depth: int, key: PRNGKeyArray, freq_scale: float = 256.0) -> FourierNet classmethod

Construct a FourierNet with depth filters and readout linears.

Parameters:

Name Type Description Default
in_features int

Input dimension D.

required
hidden_features int

Filter and hidden width H.

required
out_features int

Output dimension O.

required
depth int

Number of filter layers L, at least one.

required
key PRNGKeyArray

PRNG key, split into one key per filter and one per linear.

required
freq_scale float

Frequency scale σ_f of each filter.

256.0

Returns:

Type Description
FourierNet

A FourierNet with depth filters and depth linears.

Raises:

Type Description
ValueError

If depth is below one.

Source code in .venv/lib/python3.12/site-packages/geonnax/_src/fields/_mfn.py
@classmethod
def init(
    cls,
    in_features: int,
    hidden_features: int,
    out_features: int,
    *,
    depth: int,
    key: PRNGKeyArray,
    freq_scale: float = 256.0,
) -> FourierNet:
    """Construct a `FourierNet` with `depth` filters and readout linears.

    Args:
        in_features: Input dimension `D`.
        hidden_features: Filter and hidden width `H`.
        out_features: Output dimension `O`.
        depth: Number of filter layers `L`, at least one.
        key: PRNG key, split into one key per filter and one per linear.
        freq_scale: Frequency scale `σ_f` of each filter.

    Returns:
        A `FourierNet` with `depth` filters and `depth` linears.

    Raises:
        ValueError: If `depth` is below one.
    """
    if depth < 1:
        raise ValueError(f"depth must be at least 1, got {depth}.")
    # one key per filter, then one per linear
    keys = jax.random.split(key, 2 * depth)
    filter_keys = keys[:depth]
    linear_keys = keys[depth:]
    filters = [
        FourierFilter.init(
            in_features, hidden_features, key=filter_keys[i], freq_scale=freq_scale
        )
        for i in range(depth)
    ]
    linears = [
        eqx.nn.Linear(
            hidden_features,
            hidden_features if i < depth - 1 else out_features,
            key=linear_keys[i],
        )
        for i in range(depth)
    ]
    return cls(
        filters=filters,
        linears=linears,
        in_features=in_features,
        hidden_features=hidden_features,
        out_features=out_features,
        depth=depth,
    )

GaborNet

Bases: Module

Multiplicative Gabor Filter Network (Fathony et al., 2021).

The same MFN topology as FourierNet, but each g_i is a GaborFilter, a sinusoidal oscillation modulated by a Gaussian envelope:

g_i(x) = sin(Ω_i x + φ_i) ⊙ exp(-γ_i/2 · ‖x - μ_i‖²).

Parameters:

Name Type Description Default
filters

Length-depth list of GaborFilter, the filters g_i.

required
linears

Length-depth list of equinox.nn.Linear, the maps W_i, b_i.

required
in_features

Input dimension D.

required
hidden_features

Filter and hidden width H.

required
out_features

Output dimension O.

required
depth

Number of filter layers L.

required
domain

Pair (low, high) that bounds the centres μ, static.

required
gamma_alpha

Shape α of the Gamma prior on the bandwidths, static.

required
gamma_beta

Rate β of the Gamma prior on the bandwidths, static.

required

Examples:

A network from two inputs to one output:

>>> import jax.numpy as jnp, jax.random as jr
>>> from geonnax.fields import GaborNet
>>> net = GaborNet.init(2, 16, 1, depth=3, key=jr.PRNGKey(0))
>>> net(jnp.zeros(2)).shape
(1,)

A one-dimensional input with four outputs:

>>> net_1d = GaborNet.init(1, 8, 4, depth=2, key=jr.PRNGKey(0))
>>> net_1d(jnp.ones(1)).shape
(4,)

Composition with vmap over a batch of inputs:

>>> import jax
>>> jax.vmap(net)(jnp.ones((5, 2))).shape
(5, 1)
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/fields/_mfn.py
class GaborNet(eqx.Module):
    """Multiplicative Gabor Filter Network (Fathony et al., 2021).

    The same MFN topology as `FourierNet`, but each `g_i` is a `GaborFilter`, a
    sinusoidal oscillation modulated by a Gaussian envelope:

        g_i(x) = sin(Ω_i x + φ_i) ⊙ exp(-γ_i/2 · ‖x - μ_i‖²).

    Args:
        filters: Length-`depth` list of `GaborFilter`, the filters `g_i`.
        linears: Length-`depth` list of `equinox.nn.Linear`, the maps `W_i, b_i`.
        in_features: Input dimension `D`.
        hidden_features: Filter and hidden width `H`.
        out_features: Output dimension `O`.
        depth: Number of filter layers `L`.
        domain: Pair `(low, high)` that bounds the centres `μ`, static.
        gamma_alpha: Shape `α` of the Gamma prior on the bandwidths, static.
        gamma_beta: Rate `β` of the Gamma prior on the bandwidths, static.

    Examples:
        A network from two inputs to one output:

        >>> import jax.numpy as jnp, jax.random as jr
        >>> from geonnax.fields import GaborNet
        >>> net = GaborNet.init(2, 16, 1, depth=3, key=jr.PRNGKey(0))
        >>> net(jnp.zeros(2)).shape
        (1,)

        A one-dimensional input with four outputs:

        >>> net_1d = GaborNet.init(1, 8, 4, depth=2, key=jr.PRNGKey(0))
        >>> net_1d(jnp.ones(1)).shape
        (4,)

        Composition with `vmap` over a batch of inputs:

        >>> import jax
        >>> jax.vmap(net)(jnp.ones((5, 2))).shape
        (5, 1)
    """

    filters: list[GaborFilter]
    linears: list[eqx.nn.Linear]
    in_features: int = eqx.field(static=True)
    hidden_features: int = eqx.field(static=True)
    out_features: int = eqx.field(static=True)
    depth: int = eqx.field(static=True)
    domain: tuple[float, float] = eqx.field(static=True)
    gamma_alpha: float = eqx.field(static=True)
    gamma_beta: float = eqx.field(static=True)

    @classmethod
    def init(
        cls,
        in_features: int,
        hidden_features: int,
        out_features: int,
        *,
        depth: int,
        key: PRNGKeyArray,
        domain: tuple[float, float] = (-1.0, 1.0),
        gamma_alpha: float = 6.0,
        gamma_beta: float = 1.0,
    ) -> GaborNet:
        """Construct a `GaborNet` with `depth` Gabor filters and readouts.

        Args:
            in_features: Input dimension `D`.
            hidden_features: Filter and hidden width `H`.
            out_features: Output dimension `O`.
            depth: Number of filter layers `L`, at least one.
            key: PRNG key, split into one key per filter and one per linear.
            domain: Pair `(low, high)` that bounds the centres `μ`.
            gamma_alpha: Shape `α` of the Gamma prior on the bandwidths.
            gamma_beta: Rate `β` of the Gamma prior on the bandwidths.

        Returns:
            A `GaborNet` with `depth` filters and `depth` linears.

        Raises:
            ValueError: If `depth` is below one.
        """
        if depth < 1:
            raise ValueError(f"depth must be at least 1, got {depth}.")
        # one key per filter, then one per linear
        keys = jax.random.split(key, 2 * depth)
        filter_keys = keys[:depth]
        linear_keys = keys[depth:]
        filters = [
            GaborFilter.init(
                in_features,
                hidden_features,
                key=filter_keys[i],
                domain=domain,
                gamma_alpha=gamma_alpha,
                gamma_beta=gamma_beta,
            )
            for i in range(depth)
        ]
        linears = [
            eqx.nn.Linear(
                hidden_features,
                hidden_features if i < depth - 1 else out_features,
                key=linear_keys[i],
            )
            for i in range(depth)
        ]
        return cls(
            filters=filters,
            linears=linears,
            in_features=in_features,
            hidden_features=hidden_features,
            out_features=out_features,
            depth=depth,
            domain=domain,
            gamma_alpha=gamma_alpha,
            gamma_beta=gamma_beta,
        )

    def __call__(self, x: Float[Array, " D"]) -> Float[Array, " O"]:
        """Run the multiplicative Gabor-filter forward pass.

        Args:
            x: Input vector `x ∈ ℝ^D`.

        Returns:
            Output vector `y ∈ ℝ^O`.

        Examples:
            >>> import jax.numpy as jnp, jax.random as jr
            >>> from geonnax.fields import GaborNet
            >>> net = GaborNet.init(2, 8, 4, depth=2, key=jr.PRNGKey(0))
            >>> net(jnp.ones(2)).shape
            (4,)
        """
        # chain the filters, see mfn_forward
        return mfn_forward(x, self.filters, self.linears)

init(in_features: int, hidden_features: int, out_features: int, *, depth: int, key: PRNGKeyArray, domain: tuple[float, float] = (-1.0, 1.0), gamma_alpha: float = 6.0, gamma_beta: float = 1.0) -> GaborNet classmethod

Construct a GaborNet with depth Gabor filters and readouts.

Parameters:

Name Type Description Default
in_features int

Input dimension D.

required
hidden_features int

Filter and hidden width H.

required
out_features int

Output dimension O.

required
depth int

Number of filter layers L, at least one.

required
key PRNGKeyArray

PRNG key, split into one key per filter and one per linear.

required
domain tuple[float, float]

Pair (low, high) that bounds the centres μ.

(-1.0, 1.0)
gamma_alpha float

Shape α of the Gamma prior on the bandwidths.

6.0
gamma_beta float

Rate β of the Gamma prior on the bandwidths.

1.0

Returns:

Type Description
GaborNet

A GaborNet with depth filters and depth linears.

Raises:

Type Description
ValueError

If depth is below one.

Source code in .venv/lib/python3.12/site-packages/geonnax/_src/fields/_mfn.py
@classmethod
def init(
    cls,
    in_features: int,
    hidden_features: int,
    out_features: int,
    *,
    depth: int,
    key: PRNGKeyArray,
    domain: tuple[float, float] = (-1.0, 1.0),
    gamma_alpha: float = 6.0,
    gamma_beta: float = 1.0,
) -> GaborNet:
    """Construct a `GaborNet` with `depth` Gabor filters and readouts.

    Args:
        in_features: Input dimension `D`.
        hidden_features: Filter and hidden width `H`.
        out_features: Output dimension `O`.
        depth: Number of filter layers `L`, at least one.
        key: PRNG key, split into one key per filter and one per linear.
        domain: Pair `(low, high)` that bounds the centres `μ`.
        gamma_alpha: Shape `α` of the Gamma prior on the bandwidths.
        gamma_beta: Rate `β` of the Gamma prior on the bandwidths.

    Returns:
        A `GaborNet` with `depth` filters and `depth` linears.

    Raises:
        ValueError: If `depth` is below one.
    """
    if depth < 1:
        raise ValueError(f"depth must be at least 1, got {depth}.")
    # one key per filter, then one per linear
    keys = jax.random.split(key, 2 * depth)
    filter_keys = keys[:depth]
    linear_keys = keys[depth:]
    filters = [
        GaborFilter.init(
            in_features,
            hidden_features,
            key=filter_keys[i],
            domain=domain,
            gamma_alpha=gamma_alpha,
            gamma_beta=gamma_beta,
        )
        for i in range(depth)
    ]
    linears = [
        eqx.nn.Linear(
            hidden_features,
            hidden_features if i < depth - 1 else out_features,
            key=linear_keys[i],
        )
        for i in range(depth)
    ]
    return cls(
        filters=filters,
        linears=linears,
        in_features=in_features,
        hidden_features=hidden_features,
        out_features=out_features,
        depth=depth,
        domain=domain,
        gamma_alpha=gamma_alpha,
        gamma_beta=gamma_beta,
    )

Bayesian variants

BayesianFourierNet

Bases: PyroxModule

FourierNet with Bayesian priors on all filter and linear weights.

A thin subclass of FourierNet that overrides __call__ to register NumPyro sample sites for every parameter:

  • Per filter i: filter_{i}.Omega and filter_{i}.phi.
  • Per linear i: linear_{i}.W and linear_{i}.b.

Total number of sites: \(4L\) where \(L\) is depth.

Priors:

  • \(\Omega_i \sim \mathcal{N}(0, \sigma^2)\) (matrix).
  • \(\varphi_i \sim \mathrm{Uniform}(-\pi, \pi)\).
  • \(W_i \sim \mathcal{N}(0, \sigma^2)\) (matrix).
  • \(b_i \sim \mathcal{N}(0, \sigma^2)\) (vector).

Attributes:

Name Type Description
prior_std float

Prior standard deviation \(\\sigma\) for Gaussian sites (default 1.0). Phase sites always use \(\mathrm{Uniform}(-\pi, \pi)\).

Source code in packages/pyrox-nn/src/pyrox_nn/_mfn.py
class BayesianFourierNet(PyroxModule):
    r"""FourierNet with Bayesian priors on all filter and linear weights.

    A thin subclass of `FourierNet` that overrides ``__call__`` to
    register NumPyro sample sites for every parameter:

    - Per filter *i*: ``filter_{i}.Omega`` and ``filter_{i}.phi``.
    - Per linear *i*: ``linear_{i}.W`` and ``linear_{i}.b``.

    Total number of sites: $4L$ where $L$ is ``depth``.

    Priors:

    - $\Omega_i \sim \mathcal{N}(0, \sigma^2)$ (matrix).
    - $\varphi_i \sim \mathrm{Uniform}(-\pi, \pi)$.
    - $W_i \sim \mathcal{N}(0, \sigma^2)$ (matrix).
    - $b_i \sim \mathcal{N}(0, \sigma^2)$ (vector).

    Attributes:
        prior_std: Prior standard deviation $\\sigma$ for Gaussian
            sites (default 1.0).  Phase sites always use
            $\mathrm{Uniform}(-\pi, \pi)$.
    """

    core: FourierNet
    prior_std: float = eqx.field(static=True, default=1.0)
    pyrox_name: str | None = eqx.field(static=True, default=None)

    @classmethod
    def init(
        cls,
        in_features: int,
        hidden_features: int,
        out_features: int,
        *,
        depth: int,
        key: PRNGKeyArray,
        freq_scale: float = 256.0,
        prior_std: float = 1.0,
        pyrox_name: str | None = None,
    ) -> BayesianFourierNet:
        """Construct a `BayesianFourierNet`.

        Args mirror `FourierNet.init`, plus:

        Args:
            prior_std: Prior standard deviation for Gaussian sites
                (default 1.0).

        Raises:
            ValueError: If ``prior_std`` is non-positive or any
                `FourierNet.init` validation fails.
        """
        _require_positive(prior_std=prior_std)
        core = FourierNet.init(
            in_features,
            hidden_features,
            out_features,
            depth=depth,
            key=key,
            freq_scale=freq_scale,
        )
        return cls(core=core, prior_std=prior_std, pyrox_name=pyrox_name)

    # Convenience accessors so downstream code that reads structural fields
    # off the wrapper (in_features/depth/etc.) keeps working transparently.
    @property
    def filters(self) -> list[FourierFilter]:
        return self.core.filters

    @property
    def linears(self) -> list[eqx.nn.Linear]:
        return self.core.linears

    @property
    def in_features(self) -> int:
        return self.core.in_features

    @property
    def hidden_features(self) -> int:
        return self.core.hidden_features

    @property
    def out_features(self) -> int:
        return self.core.out_features

    @property
    def depth(self) -> int:
        return self.core.depth

    @pyrox_method
    def __call__(self, x: Float[Array, "N D"]) -> Float[Array, "N O"]:
        """Forward pass with sampled parameters.

        Args:
            x: Input of shape ``(N, D)`` or ``(D,)`` (single point).

        Returns:
            Output of shape ``(N, O)`` or ``(O,)`` if input was 1-D.
        """
        squeeze = x.ndim == 1
        x2d = jnp.atleast_2d(x)

        sampled_filters: list[FourierFilter] = []
        for i, f in enumerate(self.core.filters):
            Omega, phi = _sample_filter_omega_phi(self, i, f, self.prior_std)
            sampled_filters.append(
                eqx.tree_at(lambda ff: (ff.Omega, ff.phi), f, (Omega, phi))
            )

        sampled_linears = _sample_normal_linears(
            self, self.core.linears, self.prior_std
        )

        sampled_core = eqx.tree_at(
            lambda c: (c.filters, c.linears),
            self.core,
            (sampled_filters, sampled_linears),
        )
        out = jax.vmap(sampled_core)(x2d)
        return out[0] if squeeze else out

init(in_features: int, hidden_features: int, out_features: int, *, depth: int, key: PRNGKeyArray, freq_scale: float = 256.0, prior_std: float = 1.0, pyrox_name: str | None = None) -> BayesianFourierNet classmethod

Construct a BayesianFourierNet.

Args mirror FourierNet.init, plus:

Parameters:

Name Type Description Default
prior_std float

Prior standard deviation for Gaussian sites (default 1.0).

1.0

Raises:

Type Description
ValueError

If prior_std is non-positive or any FourierNet.init validation fails.

Source code in packages/pyrox-nn/src/pyrox_nn/_mfn.py
@classmethod
def init(
    cls,
    in_features: int,
    hidden_features: int,
    out_features: int,
    *,
    depth: int,
    key: PRNGKeyArray,
    freq_scale: float = 256.0,
    prior_std: float = 1.0,
    pyrox_name: str | None = None,
) -> BayesianFourierNet:
    """Construct a `BayesianFourierNet`.

    Args mirror `FourierNet.init`, plus:

    Args:
        prior_std: Prior standard deviation for Gaussian sites
            (default 1.0).

    Raises:
        ValueError: If ``prior_std`` is non-positive or any
            `FourierNet.init` validation fails.
    """
    _require_positive(prior_std=prior_std)
    core = FourierNet.init(
        in_features,
        hidden_features,
        out_features,
        depth=depth,
        key=key,
        freq_scale=freq_scale,
    )
    return cls(core=core, prior_std=prior_std, pyrox_name=pyrox_name)

BayesianGaborNet

Bases: PyroxModule

GaborNet with Bayesian priors on all filter and linear weights.

A thin subclass of GaborNet that overrides __call__ to register NumPyro sample sites for every parameter:

  • Per filter i: filter_{i}.Omega, filter_{i}.phi, filter_{i}.mu, and filter_{i}.log_gamma.
  • Per linear i: linear_{i}.W and linear_{i}.b.

Total number of sites: \(6L\) where \(L\) is depth.

Priors:

  • \(\Omega_i \sim \mathcal{N}(0, \sigma^2)\) (matrix).
  • \(\varphi_i \sim \mathrm{Uniform}(-\pi, \pi)\).
  • \(\mu_i \sim \mathrm{Uniform}(\texttt{domain\_low},\texttt{domain\_high})\).
  • \(\log\gamma_i \sim \mathcal{N}(0, \sigma^2)\) (log-space).
  • \(W_i \sim \mathcal{N}(0, \sigma^2)\) (matrix).
  • \(b_i \sim \mathcal{N}(0, \sigma^2)\) (vector).

Attributes:

Name Type Description
prior_std float

Prior standard deviation \(\\sigma\) for Gaussian and log-gamma sites (default 1.0).

Source code in packages/pyrox-nn/src/pyrox_nn/_mfn.py
class BayesianGaborNet(PyroxModule):
    r"""GaborNet with Bayesian priors on all filter and linear weights.

    A thin subclass of `GaborNet` that overrides ``__call__`` to
    register NumPyro sample sites for every parameter:

    - Per filter *i*: ``filter_{i}.Omega``, ``filter_{i}.phi``,
      ``filter_{i}.mu``, and ``filter_{i}.log_gamma``.
    - Per linear *i*: ``linear_{i}.W`` and ``linear_{i}.b``.

    Total number of sites: $6L$ where $L$ is ``depth``.

    Priors:

    - $\Omega_i \sim \mathcal{N}(0, \sigma^2)$ (matrix).
    - $\varphi_i \sim \mathrm{Uniform}(-\pi, \pi)$.
    - $\mu_i \sim \mathrm{Uniform}(\texttt{domain\_low},\texttt{domain\_high})$.
    - $\log\gamma_i \sim \mathcal{N}(0, \sigma^2)$ (log-space).
    - $W_i \sim \mathcal{N}(0, \sigma^2)$ (matrix).
    - $b_i \sim \mathcal{N}(0, \sigma^2)$ (vector).

    Attributes:
        prior_std: Prior standard deviation $\\sigma$ for Gaussian
            and log-gamma sites (default 1.0).
    """

    core: GaborNet
    prior_std: float = eqx.field(static=True, default=1.0)
    pyrox_name: str | None = eqx.field(static=True, default=None)

    @classmethod
    def init(
        cls,
        in_features: int,
        hidden_features: int,
        out_features: int,
        *,
        depth: int,
        key: PRNGKeyArray,
        domain: tuple[float, float] = (-1.0, 1.0),
        gamma_alpha: float = 6.0,
        gamma_beta: float = 1.0,
        prior_std: float = 1.0,
        pyrox_name: str | None = None,
    ) -> BayesianGaborNet:
        """Construct a `BayesianGaborNet`.

        Args mirror `GaborNet.init`, plus:

        Args:
            prior_std: Prior standard deviation for Gaussian and
                log-gamma sites (default 1.0).

        Raises:
            ValueError: If ``prior_std`` is non-positive or any
                `GaborNet.init` validation fails.
        """
        _require_positive(prior_std=prior_std)
        core = GaborNet.init(
            in_features,
            hidden_features,
            out_features,
            depth=depth,
            key=key,
            domain=domain,
            gamma_alpha=gamma_alpha,
            gamma_beta=gamma_beta,
        )
        return cls(core=core, prior_std=prior_std, pyrox_name=pyrox_name)

    # Structural accessors mirror the previous direct-inheritance API.
    @property
    def filters(self) -> list[GaborFilter]:
        return self.core.filters

    @property
    def linears(self) -> list[eqx.nn.Linear]:
        return self.core.linears

    @property
    def in_features(self) -> int:
        return self.core.in_features

    @property
    def hidden_features(self) -> int:
        return self.core.hidden_features

    @property
    def out_features(self) -> int:
        return self.core.out_features

    @property
    def depth(self) -> int:
        return self.core.depth

    @property
    def domain(self) -> tuple[float, float]:
        return self.core.domain

    @property
    def gamma_alpha(self) -> float:
        return self.core.gamma_alpha

    @property
    def gamma_beta(self) -> float:
        return self.core.gamma_beta

    @pyrox_method
    def __call__(self, x: Float[Array, "N D"]) -> Float[Array, "N O"]:
        """Forward pass with sampled parameters.

        Args:
            x: Input of shape ``(N, D)`` or ``(D,)`` (single point).

        Returns:
            Output of shape ``(N, O)`` or ``(O,)`` if input was 1-D.
        """
        squeeze = x.ndim == 1
        x2d = jnp.atleast_2d(x)
        low, high = self.core.domain

        sampled_filters: list[GaborFilter] = []
        for i, f in enumerate(self.core.filters):
            Omega, phi = _sample_filter_omega_phi(self, i, f, self.prior_std)
            mu = self.pyrox_sample(
                f"filter_{i}.mu",
                dist.Uniform(low, high)
                .expand([f.out_features, f.in_features])
                .to_event(2),
            )
            log_gamma = self.pyrox_sample(
                f"filter_{i}.log_gamma",
                dist.Normal(0.0, self.prior_std).expand([f.out_features]).to_event(1),
            )
            sampled_filters.append(
                eqx.tree_at(
                    lambda ff: (ff.Omega, ff.phi, ff.mu, ff.log_gamma),
                    f,
                    (Omega, phi, mu, log_gamma),
                )
            )

        sampled_linears = _sample_normal_linears(
            self, self.core.linears, self.prior_std
        )

        sampled_core = eqx.tree_at(
            lambda c: (c.filters, c.linears),
            self.core,
            (sampled_filters, sampled_linears),
        )
        out = jax.vmap(sampled_core)(x2d)
        return out[0] if squeeze else out

init(in_features: int, hidden_features: int, out_features: int, *, depth: int, key: PRNGKeyArray, domain: tuple[float, float] = (-1.0, 1.0), gamma_alpha: float = 6.0, gamma_beta: float = 1.0, prior_std: float = 1.0, pyrox_name: str | None = None) -> BayesianGaborNet classmethod

Construct a BayesianGaborNet.

Args mirror GaborNet.init, plus:

Parameters:

Name Type Description Default
prior_std float

Prior standard deviation for Gaussian and log-gamma sites (default 1.0).

1.0

Raises:

Type Description
ValueError

If prior_std is non-positive or any GaborNet.init validation fails.

Source code in packages/pyrox-nn/src/pyrox_nn/_mfn.py
@classmethod
def init(
    cls,
    in_features: int,
    hidden_features: int,
    out_features: int,
    *,
    depth: int,
    key: PRNGKeyArray,
    domain: tuple[float, float] = (-1.0, 1.0),
    gamma_alpha: float = 6.0,
    gamma_beta: float = 1.0,
    prior_std: float = 1.0,
    pyrox_name: str | None = None,
) -> BayesianGaborNet:
    """Construct a `BayesianGaborNet`.

    Args mirror `GaborNet.init`, plus:

    Args:
        prior_std: Prior standard deviation for Gaussian and
            log-gamma sites (default 1.0).

    Raises:
        ValueError: If ``prior_std`` is non-positive or any
            `GaborNet.init` validation fails.
    """
    _require_positive(prior_std=prior_std)
    core = GaborNet.init(
        in_features,
        hidden_features,
        out_features,
        depth=depth,
        key=key,
        domain=domain,
        gamma_alpha=gamma_alpha,
        gamma_beta=gamma_beta,
    )
    return cls(core=core, prior_std=prior_std, pyrox_name=pyrox_name)

Pure-JAX helper

mfn_forward(x: Float[Array, ' D'], filters: Sequence[Callable[[JaxArray], JaxArray]], linears: Sequence[Callable[[JaxArray], JaxArray]]) -> Float[Array, ' O']

Pure-JAX MFN forward pass for user-supplied filter and linear callables.

Implements the multiplicative chaining of Fathony et al. (2021):

z₁ = g₁(x),    z_{i+1} = g_{i+1}(x) ⊙ (W_i z_i + b_i),    y = W_L z_L + b_L.

This is an escape hatch: it lets custom filter families sit in the MFN topology without subclassing FourierNet or GaborNet. filters and linears must have the same length L. x is one example of shape (D,); use jax.vmap for a batch.

Parameters:

Name Type Description Default
x Float[Array, ' D']

Input vector x ∈ ℝ^D.

required
filters Sequence[Callable[[Array], Array]]

The L filter callables g_i, each ℝ^D → ℝ^H.

required
linears Sequence[Callable[[Array], Array]]

The L linear callables, ℝ^H → ℝ^H except the last, which maps to the output width.

required

Returns:

Type Description
Float[Array, ' O']

The output y ∈ ℝ^O.

Raises:

Type Description
ValueError

If filters or linears is empty, or their lengths differ.

Examples:

A FourierNet topology with two layers:

>>> import jax.numpy as jnp, jax.random as jr
>>> from geonnax.fields import FourierNet, mfn_forward
>>> net = FourierNet.init(2, 4, 3, depth=2, key=jr.PRNGKey(0))
>>> mfn_forward(jnp.zeros(2), net.filters, net.linears).shape
(3,)

Mismatched lengths are rejected:

>>> short, x0 = net.linears[:1], jnp.zeros(2)
>>> mfn_forward(x0, net.filters, short)
Traceback (most recent call last):
ValueError: filters and linears must have equal length; got 2 and 1.

Composition with vmap over a batch of inputs:

>>> import jax
>>> jax.vmap(lambda v: mfn_forward(v, net.filters, net.linears))(
...     jnp.ones((5, 2))
... ).shape
(5, 3)
Source code in .venv/lib/python3.12/site-packages/geonnax/_src/fields/_mfn.py
def mfn_forward(
    x: Float[Array, " D"],
    filters: Sequence[Callable[[JaxArray], JaxArray]],
    linears: Sequence[Callable[[JaxArray], JaxArray]],
) -> Float[Array, " O"]:
    """Pure-JAX MFN forward pass for user-supplied filter and linear callables.

    Implements the multiplicative chaining of Fathony et al. (2021):

        z₁ = g₁(x),    z_{i+1} = g_{i+1}(x) ⊙ (W_i z_i + b_i),    y = W_L z_L + b_L.

    This is an escape hatch: it lets custom filter families sit in the MFN topology
    without subclassing `FourierNet` or `GaborNet`. `filters` and `linears` must
    have the same length `L`. `x` is one example of shape `(D,)`; use `jax.vmap`
    for a batch.

    Args:
        x: Input vector `x ∈ ℝ^D`.
        filters: The `L` filter callables `g_i`, each `ℝ^D → ℝ^H`.
        linears: The `L` linear callables, `ℝ^H → ℝ^H` except the last, which maps
            to the output width.

    Returns:
        The output `y ∈ ℝ^O`.

    Raises:
        ValueError: If `filters` or `linears` is empty, or their lengths differ.

    Examples:
        A `FourierNet` topology with two layers:

        >>> import jax.numpy as jnp, jax.random as jr
        >>> from geonnax.fields import FourierNet, mfn_forward
        >>> net = FourierNet.init(2, 4, 3, depth=2, key=jr.PRNGKey(0))
        >>> mfn_forward(jnp.zeros(2), net.filters, net.linears).shape
        (3,)

        Mismatched lengths are rejected:

        >>> short, x0 = net.linears[:1], jnp.zeros(2)
        >>> mfn_forward(x0, net.filters, short)  # doctest: +IGNORE_EXCEPTION_DETAIL
        Traceback (most recent call last):
        ValueError: filters and linears must have equal length; got 2 and 1.

        Composition with `vmap` over a batch of inputs:

        >>> import jax
        >>> jax.vmap(lambda v: mfn_forward(v, net.filters, net.linears))(
        ...     jnp.ones((5, 2))
        ... ).shape
        (5, 3)
    """
    if len(filters) == 0 or len(linears) == 0:
        raise ValueError(
            f"filters and linears must be non-empty; got lengths "
            f"{len(filters)} and {len(linears)}."
        )
    if len(filters) != len(linears):
        raise ValueError(
            f"filters and linears must have equal length; got "
            f"{len(filters)} and {len(linears)}."
        )
    # z₁ = g₁(x)
    z = filters[0](x)
    for f, lin in zip(filters[1:], linears[:-1], strict=True):
        # z_{i+1} = g_{i+1}(x) ⊙ (W_i z_i + b_i)
        z = f(x) * lin(z)
    # y = W_L z_L + b_L
    return linears[-1](z)