Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

SIREN — Sinusoidal Implicit Neural Representations

Open In Colab

SIREN (Sitzmann, Martel, Bergman, Lindell, Wetzstein — NeurIPS 2020) replaces ReLU/GELU with sin and prescribes a three-regime initialisation that keeps pre-activation variance stable across depth. The result is a small MLP that fits high-frequency signals — sharp images, audio, geometry — that ordinary networks struggle with.

This notebook walks through three concrete uses of pyrox_nn.SirenDense, SIREN, and BayesianSIREN:

  1. High-frequency 1D regression — SIREN vs a same-size ReLU MLP on a fast sinusoid.

  2. 2D implicit representation — fit a synthetic image with (x, y) → intensity.

  3. Bayesian SIREN with proper uncertainty — prior draws + a deep ensemble of MAP estimates via pyrox.inference.ensemble_map, giving a predictive band that visibly widens across a held-out gap.

Background — the math behind SIREN

Architecture

A SIREN of depth LL is the composition

fθ(x)=WL ϕL−1∘ϕL−2∘⋯∘ϕ0(x)+bL,f_\theta(x) = W_L\,\phi_{L-1} \circ \phi_{L-2} \circ \cdots \circ \phi_0(x) + b_L,

where each hidden layer is

ϕi(z)=sin⁡ ⁣(ωi⋅(Wiz+bi)).\phi_i(z) = \sin\!\bigl(\omega_i \cdot (W_i z + b_i)\bigr).

The first-layer frequency multiplier is ω0\omega_0 (typically larger), all hidden layers share ω\omega, and the readout WL z+bLW_L\, z + b_L is linear. Compared with a ReLU MLP the only structural change is replacing σReLU(z)=max⁡(z,0)\sigma_{\mathrm{ReLU}}(z) = \max(z, 0) with σSIREN(z)=sin⁡(ωz)\sigma_{\mathrm{SIREN}}(z) = \sin(\omega z).

Why sine activations: NTK spectral bias

A standard ReLU MLP has a well-documented spectral bias: under the neural tangent kernel (NTK) limit, the training dynamics learn low-frequency components of the target first, and the eigenvalues λk\lambda_k of the NTK on the training data decay as λk∼k−(d+1)\lambda_k \sim k^{-(d+1)} in input dimension dd (Cao et al. 2019, Tancik et al. 2020). A target with a single high-frequency Fourier mode sin⁡(2πKx)\sin(2\pi K x) projects almost entirely onto eigencomponents with tiny λk\lambda_k, so gradient descent takes O(1/λk)\mathcal{O}(1/\lambda_k) steps to fit that mode — typically infeasible.

SIREN’s NTK has a (much) flatter spectrum: the sin⁡(ω⋅)\sin(\omega \cdot) activation explicitly mixes frequencies up to ω\omega, so the NTK has high eigenvalues even at high frequencies and gradient descent can converge on those modes. This is why a same-size SIREN drops several orders of magnitude lower in training MSE on sin⁡(8πx)+0.5cos⁡(20πx)\sin(8\pi x) + 0.5\cos(20\pi x) than a ReLU MLP — section 1 below makes this concrete.

Sitzmann Theorem 1 — variance-preserving init

A naive N(0,1)\mathcal{N}(0, 1) initialisation on a sine network is catastrophic: sin⁡(ω⋅Wz)\sin(\omega \cdot W z) with ∣Wz∣≫1|W z| \gg 1 is uniformly distributed on [−1,1][-1, 1] regardless of zz, so the layer destroys all signal. Sitzmann shows (Theorem 1) that if the input to a hidden layer is approximately N(0,1)\mathcal{N}(0, 1), then choosing the weights as

Wij∼U ⁣(−c/dinω, c/dinω)W_{ij} \sim \mathcal{U}\!\left(-\frac{\sqrt{c/d_{\mathrm{in}}}}{\omega},\, \frac{\sqrt{c/d_{\mathrm{in}}}}{\omega}\right)

makes the pre-activation ω⋅(Wz+b)\omega \cdot (W z + b) approximately N(0,c/3)\mathcal{N}(0, c/3) (since Var(U(−a,a))=a2/3\mathrm{Var}(\mathcal{U}(-a, a)) = a^2/3 and the ω\omega factor cancels), and therefore the post-activation sin⁡(ω⋅(Wz+b))\sin(\omega \cdot (W z + b)) has Var≈1/2\mathrm{Var} \approx 1/2 for c=6c = 6 — the variance of a sine of a wide-enough Gaussian. Stacking layers with this init keeps the activation variance fixed instead of collapsing or saturating.

The first and last layers need different scaling (the input has its own scale; the readout is linear and not followed by a sine):

LayerWW initActivation
"first"U(−1/din, 1/din)\mathcal{U}(-1/d_\text{in},\, 1/d_\text{in})sin⁡(ω0⋅(Wx+b))\sin(\omega_0 \cdot (W x + b))
"hidden"U(−c/din/ω, c/din/ω)\mathcal{U}(-\sqrt{c/d_\text{in}}/\omega,\, \sqrt{c/d_\text{in}}/\omega)sin⁡(ω⋅(Wx+b))\sin(\omega \cdot (W x + b))
"last"U(−c/din, c/din)\mathcal{U}(-\sqrt{c/d_\text{in}},\, \sqrt{c/d_\text{in}})none — Wx+bW x + b

pyrox_nn.SIREN.init wires all three regimes for you; the typical ω0=ω=30\omega_0 = \omega = 30 from the paper is the default. Choosing ω\omega matters: ω=30\omega = 30 targets very high-frequency signals (images, audio, geometry). For low-frequency targets, lower ω\omega — section 3 below uses ω=5\omega = 5 for a sin⁡(3πx)\sin(3\pi x) signal.

Setup

Detect Colab and install pyrox-nn[colab] (which pulls in matplotlib and watermark) only when running there.

Print an explicit version / platform readout so reproducing this notebook later is unambiguous.

Python implementation: CPython
Python version       : 3.13.5
IPython version      : 9.10.0

jax       : 0.9.2
equinox   : 0.13.6
numpyro   : 0.20.1
pyrox     : 0.0.6
matplotlib: 3.10.8

Compiler    : GCC 11.2.0
OS          : Linux
Release     : 6.8.0-1044-azure
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

1. High-frequency 1D regression — SIREN vs ReLU

Per the NTK analysis above, ReLU MLPs converge slowly on high-frequency targets: the eigenvalues of the NTK at frequency KK scale as λK∼K−2\lambda_K \sim K^{-2} in 1-D, so gradient descent takes O(K2)\mathcal{O}(K^2) steps to fit sin⁡(2πKx)\sin(2\pi K x). SIREN does not have that decay.

We fit both networks to

f⋆(x)=sin⁡(8πx)+0.5 cos⁡(20πx),x∈[−1,1],f^\star(x) = \sin(8\pi x) + 0.5\,\cos(20\pi x), \qquad x \in [-1, 1],

whose Fourier support is concentrated at K1=4K_1 = 4 and K2=10K_2 = 10 cycles per unit. Both networks have width 64, depth 4, and train for 2000 Adam steps with the same learning rate. The expected outcome: SIREN drops to MSE ∼10−6\sim 10^{-6}, the ReLU MLP plateaus around 10-1 because it has not yet caught the K2=10K_2 = 10 component.

SIREN final MSE: 0.000001
ReLU  final MSE: 0.073385

Both networks are the same width and depth and trained for the same budget. The ReLU net flattens out at a much higher loss because it cannot represent the high-frequency content; SIREN drops several orders of magnitude lower.

<Figure size 1800x500 with 2 Axes>

2. 2D implicit representation

SIREN’s most famous use is as an implicit neural representation (INR): instead of storing pixel values, learn the function fθ:R2→Rf_\theta : \mathbb{R}^2 \to \mathbb{R} that maps coordinates to intensity. The fit problem is a least-squares regression on the dense grid of pixel locations,

θ⋆=arg⁡min⁡θ1N∑i=1N(I(xi,yi)−fθ(xi,yi))2,\theta^\star = \arg\min_\theta \frac{1}{N}\sum_{i=1}^N \bigl(I(x_i, y_i) - f_\theta(x_i, y_i)\bigr)^2,

and the MLP weights are the (continuous) compressed representation of the image. The compression ratio is Npixels∣θ∣\frac{N_{\mathrm{pixels}}}{|\theta|}; for our 64×6464 \times 64 image and a (2,96,1)(2, 96, 1) depth-4 SIREN that is 4096/≈19,000≈0.224096 \big/ \approx 19{,}000 \approx 0.22, i.e. the MLP is larger than the data — but for 1024×10241024 \times 1024 images the same architecture compresses about 55×55\times.

The synthetic target is a Gabor patch

I(x,y)=cos⁡ ⁣(8π(x+y))⋅exp⁡ ⁣(−(x2+y2)/0.4),I(x, y) = \cos\!\bigl(8\pi (x + y)\bigr) \cdot \exp\!\bigl(-(x^2 + y^2) / 0.4\bigr),

with carrier frequency K=4K = 4 along the x+yx + y direction and a Gaussian envelope. Both the carrier and the envelope are easy for SIREN.

INR final MSE: 0.000000
<Figure size 1800x500 with 6 Axes>

The SIREN reconstruction matches the Gabor patch almost everywhere; the residual map shows where the few remaining errors live (high-curvature ring of the carrier). For real INR work you would use a wider network and longer training; here we keep both small to fit inside an executed notebook.

3. Bayesian SIREN — prior calibration + ensemble-of-MAP uncertainty

pyrox_nn.BayesianSIREN is the probabilistic version of SIREN: every weight matrix and bias becomes a NumPyro sample site under a prior chosen to match Sitzmann’s uniform-init variance exactly.

Deriving the prior scale

The Sitzmann hidden-layer init is Wij∼U(−a,a)W_{ij} \sim \mathcal{U}(-a, a) with half-width a=c/din/ωa = \sqrt{c/d_\text{in}}/\omega. A uniform on [−a,a][-a, a] has variance

Var[U(−a,a)]=a23.\mathrm{Var}\bigl[\mathcal{U}(-a, a)\bigr] = \frac{a^2}{3}.

To replace the deterministic init with a Bayesian prior without changing the activation regime, we want a Normal prior with the same variance:

Wij∼N(0, σW2),σW=σ0⋅a3.W_{ij} \sim \mathcal{N}(0,\, \sigma_W^2), \qquad \sigma_W = \sigma_0 \cdot \frac{a}{\sqrt{3}}.

The user-controllable scale σ0≥0\sigma_0 \ge 0 (prior_std) tightens or loosens the prior; σ0=1\sigma_0 = 1 recovers the exact Sitzmann variance. Biases use a=1/dina = 1/\sqrt{d_\text{in}}, so σb=σ0/3din\sigma_b = \sigma_0 / \sqrt{3 d_\text{in}}. In summary, for layer ℓ\ell with input dimension dℓd_\ell and frequency ωℓ\omega_\ell:

σW,ℓ={σ03 dℓℓ=0 (first)σ0c3dℓ ωℓ0<ℓ<L (hidden)σ0c3dℓℓ=L (last)σb,ℓ=σ03dℓ.\sigma_{W,\ell} = \begin{cases} \dfrac{\sigma_0}{\sqrt{3}\, d_\ell} & \ell = 0 \text{ (first)} \\[6pt] \dfrac{\sigma_0\sqrt{c}}{\sqrt{3 d_\ell}\,\omega_\ell} & 0 < \ell < L \text{ (hidden)} \\[6pt] \dfrac{\sigma_0\sqrt{c}}{\sqrt{3 d_\ell}} & \ell = L \text{ (last)} \end{cases} \qquad \sigma_{b,\ell} = \frac{\sigma_0}{\sqrt{3 d_\ell}}.

Without this regime-aware rescaling, a flat N(0,1)\mathcal{N}(0, 1) prior on the same architecture would push every hidden layer into the saturated-sine pathology — prior function draws would be uniformly distributed on [−1,1][-1, 1] at every input.

Why we use ensemble-of-MAP

The posterior over θ={Wℓ,bℓ}\theta = \{W_\ell, b_\ell\} given data D={(xi,yi)}\mathcal{D} = \{(x_i, y_i)\} is

p(θ∣D)∝∏iN(yi∣fθ(xi),σn2)⏟p(D∣θ)⋅∏ℓN(Wℓ∣0,σW,ℓ2) N(bℓ∣0,σb,ℓ2)⏟p(θ).p(\theta \mid \mathcal{D}) \propto \underbrace{\prod_i \mathcal{N}(y_i \mid f_\theta(x_i), \sigma_n^2)}_{p(\mathcal{D} \mid \theta)} \cdot \underbrace{\prod_\ell \mathcal{N}(W_\ell \mid 0, \sigma_{W,\ell}^2)\,\mathcal{N}(b_\ell \mid 0, \sigma_{b,\ell}^2)}_{p(\theta)}.

Mean-field variational inference (AutoNormal, AutoLowRankMultivariateNormal) on this posterior is fragile: with ∼\sim700 weights and ∼\sim50 observations the KL term KL(q ∥ p)\mathrm{KL}(q \,\|\, p) dominates the ELBO and pulls the variational qq back to the prior, collapsing the predictive mean to zero. Empirically, mean-field also produces a flat predictive variance — the same uncertainty everywhere — because it cannot represent the correlated weight structure that creates region-specific uncertainty.

A pragmatic alternative is the deep ensemble of MAP estimates. Each ensemble member e∈{1,…,E}e \in \{1, \ldots, E\} optimises

θe⋆=arg⁡max⁡θ[log⁡p(D∣θ)+log⁡p(θ)],\theta_e^\star = \arg\max_\theta \Bigl[\log p(\mathcal{D} \mid \theta) + \log p(\theta)\Bigr],

starting from an independent prior draw θe(0)∼p(θ)\theta_e^{(0)} \sim p(\theta). Because the loss surface is non-convex, different initialisations land in different local modes; the ensemble spread of the predictive function {fθe}\{f_{\theta_e}\} approximates the posterior predictive uncertainty (Lakshminarayanan, Pritzel & Blundell 2017; Wilson & Izmailov 2020 connect this to a multi-mode Bayesian model average).

We do two things in this section:

  1. Prior calibration — draw a few functions from p(θ)p(\theta) and check that fθ(x)f_\theta(x) has sensible amplitude and frequency content. This is the cheap visual check that the regime-scaled prior is in the right regime.

  2. Ensemble-of-MAP posterior fit via pyrox.inference.ensemble_map. Each of E=16E = 16 members is initialised from a fresh prior draw and optimised under BayesianSIREN’s exact log-prior (extracted from the model by NumPyro tracing — we do not re-implement the prior in numpy).

Configuration

The target signal is f⋆(x)=sin⁡(3πx)f^\star(x) = \sin(3\pi x) on [−1,1][-1, 1] — period 2/32/3, frequency K=1.5K = 1.5 cycles per unit. We pick the SIREN frequency multiplier ω=5\omega = 5, just enough to represent the target (ω≳2πK\omega \gtrsim 2\pi K is the rule of thumb).

The default ω=30\omega = 30 over-fits spectacularly: the deterministic SIREN at ω=30\omega = 30 achieves training MSE 10-6 but extrapolates to MSE 0.95 inside a held-out gap because the network’s natural function class oscillates wildly between observed points. Matching ω\omega to the target drops gap MSE roughly 30×30\times.

Prior draws

prior amplitude (max |f|) per draw: [2.47634298 1.19501433 2.40420713 1.94233976 2.63953598 2.41294865
 1.58908631 1.84505477]
<Figure size 1200x500 with 1 Axes>

Each prior sample stays in roughly the same amplitude range — that’s the Sitzmann-matched scaling at work. A flat N(0,1)\mathcal{N}(0, 1) prior on the same architecture would either collapse to a constant or saturate the sines.

Build the data — sin(3πx) with a held-out gap

Bridge between BayesianSIREN and ensemble_map

pyrox.inference.ensemble_map wants two callables:

  • init_fn(key) → params — produce one ensemble member’s initial parameters.

  • log_joint(params, x_batch, y_batch) → (loglik, logprior) — compute the per-member objective

    L(θ)=log⁡p(D∣θ)+log⁡p(θ)\mathcal{L}(\theta) = \log p(\mathcal{D} \mid \theta) + \log p(\theta)

    split into the two terms.

We use NumPyro tracing to express both without re-implementing BayesianSIREN’s prior.

  • init_fn traces BayesianSIREN.__call__ under handlers.seed(rng_seed=key) to draw a fresh sample θ(0)∼p(θ)\theta^{(0)} \sim p(\theta), then splices those weights into a deterministic SIREN so the optimizer sees normal equinox.Module array leaves.

  • log_prior_via_trace is the dual: given candidate weights θ\theta, handlers.substitute replaces every sample site in the trace with its value from θ\theta so that

    log⁡p(θ)=∑ℓ,ijlog⁡N(Wℓ,ij∣0, σW,ℓ2)+∑ℓ,jlog⁡N(bℓ,j∣0, σb,ℓ2)\log p(\theta) = \sum_{\ell, ij}\log\mathcal{N}(W_{\ell, ij} \mid 0,\, \sigma_{W,\ell}^2) + \sum_{\ell, j}\log\mathcal{N}(b_{\ell, j} \mid 0,\, \sigma_{b,\ell}^2)

    reduces to summing site["fn"].log_prob(site["value"]) over the trace. The Gaussian likelihood term log⁡p(D∣θ)=−12σn2∑i(yi−fθ(xi))2+const\log p(\mathcal{D} \mid \theta) = -\frac{1}{2\sigma_n^2}\sum_i (y_i - f_\theta(x_i))^2 + \text{const} is added directly inside log_joint.

Run the ensemble

Sixteen members, four thousand Adam steps each, all vmap-fused into a single ensemble forward pass under the hood.

ensemble losses (final): min=-398.6  median=-393.6  max=-387.1

Ensemble predictive — mean and ±2σ band

mean predictive std at the data : 0.006
mean predictive std in the gap  : 0.256
gap / data std ratio            : 43.7x
<Figure size 1200x500 with 1 Axes>

The predictive band hugs the data tightly, opens up across the held-out gap, and continues to widen into the extrapolation region — the qualitative behaviour you want from any well-calibrated Bayesian model. The grey individual-member traces show where the ensemble disagrees: the latent functions are nearly identical at the data and pull apart in the gap.

Takeaways

  • SIREN.init(in_features, hidden, out_features, depth=…, key=…) wires the three Sitzmann init regimes automatically — you do not hand-tune per-layer init.

  • SIREN out-fits same-size ReLU MLPs on high-frequency content because the sine activation removes the λk∼k−(d+1)\lambda_k \sim k^{-(d+1)} NTK eigendecay that biases ReLU nets toward low frequencies.

  • SIREN is a strong INR backbone: a small (2, 96, 1) network compresses a Gabor patch (carrier frequency K=4K = 4) to MSE ∼10−6\sim 10^{-6} in 1500 steps.

  • Match ω\omega to the target frequency (ω≳2πK\omega \gtrsim 2\pi K). The default ω=30\omega = 30 overfits low-frequency targets badly in held-out regions because the network’s natural function class oscillates well above the target’s bandwidth.

  • BayesianSIREN puts the regime-scaled Sitzmann init into the prior: σW,ℓ=σ0⋅aℓ/3\sigma_{W,\ell} = \sigma_0 \cdot a_\ell / \sqrt{3}. Combined with pyrox.inference.ensemble_map, EE MAP modes give a predictive band that widens by ∼40×\sim 40\times in held-out regions and ∼27×\sim 27\times in extrapolation. Mean-field VI on this architecture is dominated by the prior KL and produces a flat predictive variance — the deep ensemble of MAP estimates is the recommended uncertainty path.