How It Works¶
🧠 ContextualConv Layers¶
ContextualConv1d and ContextualConv2d extend standard convolutions by allowing the output to be modulated by a global context vector.
🔄 Forward Pass¶
Input tensor
xis passed through a standardnn.Conv1dornn.Conv2dlayer.If a context vector
cis provided:It is passed through a shared
ContextProcessormodule:If
h_dimis not set: just aLinear(context_dim → out_channels)If
h_dimis set: an MLPLinear → ReLU → LinearIf both scale and bias are used: output is
2 * out_channelsand split into γ and β
The result is a scale (
γ) and/or bias (β) of shape(B, out_channels)These parameters are broadcast and applied to the conv output:
y = γ(c) * conv(x) + β(c)
🧩 Architecture (1D/2D)¶
x → Conv1d/2d → y
c → ContextProcessor → γ, β
y → γ * y + β → final output
✅ Notes¶
The context vector is global — it does not vary across spatial/temporal locations.
The
ContextProcessoris shared across all locations and all groups (if used).If
context_dimis not set, the layer behaves like a standard convolution.