TL;DR
A convolution slides a small set of weights along a sequence and takes a weighted sum at each position. A causal convolution does the same, but at each time step it uses only the current input and the ones before it. It never looks at the future, which is what a model needs when it predicts the next element of a sequence from what came before. The usual way to build one is to pad the start of the sequence with zeros and shift the kernel so it reaches only backwards.
Builds on: Convolution, Autoregressive Model, Receptive Field · Leads to: Dilated Convolution
Where it's used
Raw audio generation. WaveNet generates speech and music one sample at a time, and each new sample must depend only on samples already produced. A causal convolution stack is what lets the model train on whole recordings in parallel while still generating in order.
Anomaly detection in sensor streams. A model that flags an anomaly in a sensor stream at time may only use readings up to , because the future has not happened yet in deployment. Causal convolutions give the model a window of recent history without leaking the future into training.
Autoregressive image models. Pixel-by-pixel image generators use the same masking idea across the rows and columns of an image: each pixel is predicted from pixels earlier in a fixed order. The one-dimensional version here is the simplest case of that idea.
How it works
Take a Sequence of four inputs, , and a kernel of size 2 with two weights, and . Treat the inputs before time 1 as zeros. The output at time should use only and .
- Pad the start. Add one zero before the first input, so the sequence is . The padding is on the left, not the right. Check: the output at time 1 has a value to read from, and it is 0.
- Slide the kernel. At each time , multiply by and by , then add. Check: the kernel never reads a position after .
- Compute time 1. .
- Compute time 2. .
- Compute times 3 and 4. and . The output is .
- Test causality. Change from 4 to 100. Recompute: the outputs at times 1, 2 and 3 do not change, because none of them reads position 4. Only the output at time 4 changes.
The left padding and the backward-looking kernel together are what make the layer causal. A standard "same" convolution pads both sides, so the output at time also uses , and it would leak the future into a generative model.
flowchart LR P["pad with zeros on the left"] --> K["kernel reads x of t minus k plus 1 up to x of t"] K --> O["output at time t"] O --> N["next layer reads only earlier outputs"]
The math
Let be the input sequence, the kernel size, and the kernel weights. Set for every , which is the left padding. The causal convolution output is
Each output is a weighted sum of the most recent inputs, ending at .
Step 2 in symbols: the kernel reaches only backwards
For the formula is . The weight multiplies the previous input and the current one. In the worked example, , which matches step 4 above.
Step 6 in symbols: causality
If depends only on with , then for any the partial derivative is zero. Changing an input at a later time leaves earlier outputs unchanged. This is the check made in step 6.
Receptive field after stacking
A causal layer with kernel size and dilation reads steps further back. Stacking layers with dilations gives a receptive field, counting the current input, of
With no dilation () the field grows by per layer. Dilation is what makes the growth fast; see Dilated Convolution.
Worked examples
Kernel size 2, weights and , so each output is the sum of the current and previous input. Inputs , with a zero before time 1.
- Time 1 uses and : output .
- Time 2 uses and : output .
- Time 3 uses and : output .
- The output is . Each value is the sum of the current input and the one before it.
Two stacked causal layers, each with kernel size 3. The first has dilation 1, the second dilation 2. Find how many inputs the final output sees.
- First layer: its output at time reads inputs at offsets from .
- Second layer: it reads its own inputs at offsets . Each of those is itself a first-layer output, which reads further back.
- Combine the offsets. The first layer adds and the second adds . The set of sums is .
- Count them: 7 offsets, so the receptive field is 7 inputs.
- Check with the formula: .
A causal stack for raw audio uses kernel size 2 and ten layers with dilations , the doubling pattern used in WaveNet-style models. Find the receptive field, and how much audio that covers at 16,000 samples per second.
- Dilations are . Their sum is .
- The kernel contributes per unit of dilation, so the sum in the formula is .
- Receptive field: samples.
- At 16,000 samples per second, seconds.
- Ten layers therefore see about 64 milliseconds of past audio. Doubling the depth of the doubling pattern would double that span, because the total sum of dilations doubles with each extra layer.
Try it: a causal stack has kernel size 3 and three layers, with no dilation. What is its receptive field? Answer: . For a kernel of size 2 with dilations 1, 2 and 4, what is it? Answer: .
Common mistakes and tradeoffs
It is tempting to think that any convolution over a sequence is causal. In fact a standard "same" convolution pads both sides and reads future values; the causal version needs the left padding and a backward-looking kernel, as in step 1.
It is tempting to think that depth alone gives a long memory. In fact without dilation the receptive field grows by only per layer, so a deep narrow stack covers a short span. Dilation is needed for long context at a modest depth.
It is tempting to think that a causal convolution generates sequences in parallel. In fact training is parallel across time, because every output is computed at once from known inputs. Generation is still one step at a time, since each new output needs the previous one.
When to use it: a sequence model must be trained in parallel and must not see the future, for example audio or time-series forecasting. When not to: when the whole sequence is available and future context helps, such as classifying a finished document; a bidirectional model is then better. The main limit is the fixed window: anything beyond the receptive field is invisible. Alternatives include recurrent networks, which carry a state of unbounded length, and attention with a causal mask.
History
Convolution is much older than deep learning. It was used in signal processing to filter sequences long before neural networks used it as a layer. In neural networks, convolutional layers became standard for images, but a fixed window over time was awkward for audio, because raw audio has tens of thousands of samples per second.
Causal convolutions with dilation were brought into wide use for raw audio by WaveNet in 2016. The dilation idea itself, spreading a small kernel to cover a wider patch without more weights, was presented for image context by Yu and Koltun in 2015. WaveNet combined the two so the model could generate audio sample by sample at a practical receptive field.
Recap
- A causal convolution's output at time depends only on inputs at times and earlier.
- It is built with left padding and a kernel that reaches only backwards.
- The receptive field of a stack is ; without dilation it grows by per layer.
- Training is parallel over time; generation is sequential.
- Next: Dilated Convolution spreads the kernel to grow the receptive field quickly, and the WaveNet work uses both.
Check yourself
QuestionWhat is the output of a causal kernel of size 2 with weights 1 and 1, applied to inputs (2, 5) with zero padding before time 1?Show answer
Time 1 uses 0 and 2, giving 2. Time 2 uses 2 and 5, giving 7. The output is (2, 7).
QuestionWhy does a 'same' convolution leak the future into an autoregressive model, and what changes in a causal one?Show answer
A 'same' convolution centres its kernel, so the output at time also reads and beyond. A causal convolution pads only on the left and uses a kernel that reaches only backwards, so the output at time reads and earlier inputs only.
QuestionA causal stack has kernel size 3 and dilations 1, 2, 4, 8. Compute its receptive field.Show answer
inputs.
QuestionA team wants a model that reads a finished sentence to classify it, and it uses a causal convolution. What is the risk, and what would you suggest?Show answer
The model cannot use words after the current one, which may carry the meaning of the earlier words. The task does not need causality, because the whole sentence is available, so a non-causal (bidirectional) convolution or a model with access to the full sequence is the better choice.
Sources
- van den Oord, A. et al. (2016). WaveNet: A Generative Model for Raw Audio. arXiv:1609.03499. https://arxiv.org/abs/1609.03499
- Dumoulin, V. and Visin, F. (2016). A guide to convolution arithmetic for deep learning. arXiv:1603.07285. https://arxiv.org/abs/1603.07285
- Wikipedia contributors. "Convolutional neural network." https://en.wikipedia.org/wiki/Convolutional_neural_network