Wavelet Scattering Transform: PyTorch Architecture¶
Navigation:
Mathematical Theory: See WAVELET_SCATTERING.md
Spectrum API Overview: See The Spectrum API
Overview & Formula Token¶
formula = "Y ~ wst(temperature, J=3, Q=1, max_order=2, causal=True)"
Parameter Specification¶
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
required |
Input signal column name. |
|
|
|
Number of dyadic wavelet scales. |
|
|
|
Wavelets per octave. |
|
|
|
Maximum scattering cascade order (\(0\), \(1\), or \(2\)). |
|
|
|
Enforce strictly causal Morlet/Gabor wavelet convolutions. |
|
|
|
Exponential penalty multiplier across scattering orders. |
Complexity & Performance¶
Causality & State Continuation: the causal form carries the last
n_histsamples of every group (state_in/_state_out) with exact continuation.Compute: a cascade of direct 1D convolutions in \(\mathcal{O}(B\,T\,\ell)\) per channel (\(\ell\) = filter length).
State, Complexity and Limits¶
Class and file:
ScatteringEffect,src/tam/model/spectrum/_scattering.py; formula tokenwst(). Label: (BETA).State:
{"hist": (B, n_hist, 1)}for the causal form; none forcausal=False. Layout:(B, T), one column.Memory of the cascade:
n_hist = (len(phi) - 1) + max_order * (len(psi_max) - 1).Penalty: diagonal order weights plus, on the order-1 block, the second-difference coupling along the octaves (needs three octaves or more).
Extrapolation:
'linear'and'saturation'are refused at construction for the causal form.Chunk equivalence bound:
tests/spectrum/test_stateful_contract.pychecks that the second of two chunks, started from the state of the first, equals the matching rows of the one-pass features withinatol=1e-8(float64), and that every group computed alone equals the same group inside a batch withinatol=1e-10.
Code¶
Feature map of ScatteringEffect (build_feature_map):
def build_feature_map(self, x: torch.Tensor, state_in: Optional[Dict[str, Any]] = None) -> torch.Tensor:
"""Computes scattering transform channels [S_0, S_1, S_2]: ``(B, T)`` in, ``(B, T, n_coeffs)`` out, each group alone."""
x3, shape, layout = to_btd(x, False)
if x3.shape[-1] != 1:
raise ValueError(f"wst reads one column, got {x3.shape[-1]}")
T = x3.shape[1]
phi_f, psi_re, psi_im = self._filters(x3.device, x3.dtype)
if self.causal:
x_full3 = causal_history(x3, self._n_hist, state_in)
self._state_out = {"hist": tail_state(x_full3, self._n_hist)}
else:
x_full3 = x3
x_full = x_full3[..., 0] # (B, n_hist + T)
channels: List[torch.Tensor] = []
# Order 0: S_0 x = x * phi_J
channels.append(self._crop_causal_tail(self._conv1d(x_full, phi_f), T))
# Order 1: the modulus of each wavelet response, smoothed by the low-pass filter (see the math page)
u1_envelopes: List[torch.Tensor] = []
for j in range(self.N1):
re = self._conv1d(x_full, psi_re[j])
im = self._conv1d(x_full, psi_im[j])
mod = torch.sqrt(re**2 + im**2 + 1e-10)
u1_envelopes.append(mod)
channels.append(self._crop_causal_tail(self._conv1d(mod, phi_f), T))
# Order 2: S_2 x = ||x * psi_j1| * psi_j2| * phi_J for j2 > j1 (coarser scales)
if self.max_order >= 2:
for j1 in range(self.N1):
mod1 = u1_envelopes[j1]
for j2 in range(j1 + 1, self.N1):
re2 = self._conv1d(mod1, psi_re[j2])
im2 = self._conv1d(mod1, psi_im[j2])
mod2 = torch.sqrt(re2**2 + im2**2 + 1e-10)
channels.append(self._crop_causal_tail(self._conv1d(mod2, phi_f), T))
phi_cat = torch.stack(channels, dim=-1)
return from_btk(phi_cat, shape, layout)
Penalty of ScatteringEffect (build_penalty_matrix):
def build_penalty_matrix(self) -> torch.Tensor:
"""Constructs multi-order Tikhonov scale penalty matrix."""
penalties: List[float] = []
# Order 0 penalty: gamma_0 = 1.0
penalties.append(1.0)
# Order 1 penalties: gamma_1 * (scale_ratio)^2
gamma_1 = self.order_pen
for j1 in range(self.N1):
scale_ratio = self.scales[j1] / self.xi_0
penalties.append(gamma_1 * (scale_ratio**2))
# Order 2 penalties: gamma_2 * ((s1^2 + s2^2) / xi_0^2)
if self.max_order >= 2:
gamma_2 = self.order_pen**2
for j1 in range(self.N1):
s1 = self.scales[j1] / self.xi_0
for j2 in range(j1 + 1, self.N1):
s2 = self.scales[j2] / self.xi_0
penalties.append(gamma_2 * (s1**2 + s2**2))
dtype = torch.get_default_dtype()
P = torch.diag(torch.tensor(penalties, device=TORCH_DEVICE, dtype=dtype))
if self.N1 >= 3:
# Neighbouring octaves of the order-1 envelopes vary smoothly: a second-difference coupling along the octaves
D2 = torch.diff(torch.eye(self.N1, device=TORCH_DEVICE, dtype=dtype), n=2, dim=0)
P[1 : 1 + self.N1, 1 : 1 + self.N1] += D2.T @ D2
return self.lambda_p * P
Parsing of the wst(...) token in the factory:
elif ttype in ('wst', 'scattering'):
J = int(params_resolved.get('J', 3))
Q = int(params_resolved.get('Q', 1))
max_order = int(params_resolved.get('max_order', 2))
causal_raw = params_resolved.get('causal', True)
causal = _as_bool(causal_raw)
order_pen = float(params_resolved.get('order_pen', 4.0))
xi_0 = float(params_resolved.get('xi_0', 0.85 * np.pi))
extrap_val = params_resolved.get('extrapolate', 'continue')
effects_list.append(ScatteringEffect(
feature_name=feature_name,
J=J,
Q=Q,
max_order=max_order,
causal=causal,
order_pen=order_pen,
xi_0=xi_0,
lambda_p=lambda_p,
extrapolate=extrap_val,
))