Search

Search papers and lessons

Causal Convolution

Level 3 · 9 min read

A causal convolution is a 1D convolution whose output at each time step uses only the current and earlier inputs, so it can model a sequence without looking ahead.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Probability
  • Architectures
  • Losses & regularization
  • Linear algebra
  • ReadReady to readNot yet

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 tt may only use readings up to tt, 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, x=(1,2,3,4)x = (1, 2, 3, 4), and a kernel of size 2 with two weights, w1=0.5w_1 = 0.5 and w2=1w_2 = 1. Treat the inputs before time 1 as zeros. The output at time tt should use only xt−1x_{t-1} and xtx_t.

  1. Pad the start. Add one zero before the first input, so the sequence is (0,1,2,3,4)(0, 1, 2, 3, 4). 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.
  2. Slide the kernel. At each time tt, multiply xt−1x_{t-1} by w1w_1 and xtx_t by w2w_2, then add. Check: the kernel never reads a position after tt.
  3. Compute time 1. 0.5×0+1×1=10.5 \times 0 + 1 \times 1 = 1.
  4. Compute time 2. 0.5×1+1×2=2.50.5 \times 1 + 1 \times 2 = 2.5.
  5. Compute times 3 and 4. 0.5×2+3=40.5 \times 2 + 3 = 4 and 0.5×3+4=5.50.5 \times 3 + 4 = 5.5. The output is (1,2.5,4,5.5)(1, 2.5, 4, 5.5).
  6. Test causality. Change x4x_4 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 tt also uses xt+1x_{t+1}, 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 x1,x2,…,xTx_1, x_2, \dots, x_T be the input sequence, kk the kernel size, and w1,…,wkw_1, \dots, w_k the kernel weights. Set xs=0x_s = 0 for every s<1s \lt 1, which is the left padding. The causal convolution output is

yt=∑j=1kwj xt−k+j,t=1,…,Ty_t = \sum_{j=1}^{k} w_j \, x_{t-k+j}, \qquad t = 1, \dots, T

Each output yty_t is a weighted sum of the kk most recent inputs, ending at xtx_t.

Step 2 in symbols: the kernel reaches only backwards

For k=2k = 2 the formula is yt=w1xt−1+w2xty_t = w_1 x_{t-1} + w_2 x_t. The weight w1w_1 multiplies the previous input and w2w_2 the current one. In the worked example, y2=0.5⋅x1+1⋅x2=0.5⋅1+1⋅2=2.5y_2 = 0.5 \cdot x_1 + 1 \cdot x_2 = 0.5 \cdot 1 + 1 \cdot 2 = 2.5, which matches step 4 above.

Step 6 in symbols: causality

If yty_t depends only on xsx_s with s≤ts \le t, then for any s′>ts' \gt t the partial derivative ∂yt/∂xs′\partial y_t / \partial x_{s'} 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 kk and dilation dd reads d(k−1)d(k-1) steps further back. Stacking layers with dilations d1,…,dLd_1, \dots, d_L gives a receptive field, counting the current input, of

R=1+∑ℓ=1Ldℓ (k−1)R = 1 + \sum_{\ell=1}^{L} d_\ell \,(k - 1)

With no dilation (dℓ=1d_\ell = 1) the field grows by k−1k - 1 per layer. Dilation is what makes the growth fast; see Dilated Convolution.

Worked examples

Example
1. A tiny sum of two recent values

Kernel size 2, weights w1=1w_1 = 1 and w2=1w_2 = 1, so each output is the sum of the current and previous input. Inputs x=(1,2,3)x = (1, 2, 3), with a zero before time 1.

  1. Time 1 uses x0=0x_0 = 0 and x1=1x_1 = 1: output 0+1=10 + 1 = 1.
  2. Time 2 uses x1=1x_1 = 1 and x2=2x_2 = 2: output 1+2=31 + 2 = 3.
  3. Time 3 uses x2=2x_2 = 2 and x3=3x_3 = 3: output 2+3=52 + 3 = 5.
  4. The output is (1,3,5)(1, 3, 5). Each value is the sum of the current input and the one before it.
Example
2. Three layers with dilations 1 and 2

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.

  1. First layer: its output at time tt reads inputs at offsets 0,−1,−20, -1, -2 from tt.
  2. Second layer: it reads its own inputs at offsets 0,−2,−40, -2, -4. Each of those is itself a first-layer output, which reads further back.
  3. Combine the offsets. The first layer adds {0,−1,−2}\{0, -1, -2\} and the second adds {0,−2,−4}\{0, -2, -4\}. The set of sums is {0,−1,−2,−3,−4,−5,−6}\{0, -1, -2, -3, -4, -5, -6\}.
  4. Count them: 7 offsets, so the receptive field is 7 inputs.
  5. Check with the formula: R=1+(1⋅2)+(2⋅2)=1+2+4=7R = 1 + (1 \cdot 2) + (2 \cdot 2) = 1 + 2 + 4 = 7.
Example
3. A realistic audio stack

A causal stack for raw audio uses kernel size 2 and ten layers with dilations 1,2,4,…,5121, 2, 4, \dots, 512, the doubling pattern used in WaveNet-style models. Find the receptive field, and how much audio that covers at 16,000 samples per second.

  1. Dilations are 20,21,…,292^0, 2^1, \dots, 2^9. Their sum is 210−1=10232^{10} - 1 = 1023.
  2. The kernel contributes k−1=1k - 1 = 1 per unit of dilation, so the sum in the formula is 1023⋅1=10231023 \cdot 1 = 1023.
  3. Receptive field: R=1+1023=1024R = 1 + 1023 = 1024 samples.
  4. At 16,000 samples per second, 1024/16000=0.0641024 / 16000 = 0.064 seconds.
  5. 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: R=1+3⋅(3−1)=7R = 1 + 3 \cdot (3 - 1) = 7. For a kernel of size 2 with dilations 1, 2 and 4, what is it? Answer: R=1+(1+2+4)⋅1=8R = 1 + (1 + 2 + 4) \cdot 1 = 8.

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 k−1k - 1 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 tt depends only on inputs at times tt and earlier.
  • It is built with left padding and a kernel that reaches only backwards.
  • The receptive field of a stack is 1+∑dℓ(k−1)1 + \sum d_\ell (k - 1); without dilation it grows by k−1k - 1 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 tt also reads xt+1x_{t+1} and beyond. A causal convolution pads only on the left and uses a kernel that reaches only backwards, so the output at time tt reads xtx_t and earlier inputs only.

QuestionA causal stack has kernel size 3 and dilations 1, 2, 4, 8. Compute its receptive field.Show answer

R=1+(1+2+4+8)⋅(3−1)=1+15⋅2=31R = 1 + (1 + 2 + 4 + 8) \cdot (3 - 1) = 1 + 15 \cdot 2 = 31 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