Skip to content
DeepForgeby svx
All problems
MediumDeep Learningdl-125

Causal Conv1D Output

Problem

Apply a causal 1D convolution. Left-pad the input with (k - 1) * dilation zeros so that output position t only depends on inputs up to t, then slide the kernel with the given dilation and unit stride. The output length equals the input length.

The signature is causal_conv1d(x, kernel, dilation=1).

Hint

Causal convolutions are the convolutional counterpart of masked self-attention.

Test cases

case 1
in: [[1,2,3],[1,1],1]
expected: [1,3,5]
case 2
in: [[1,2,3,4],[1,0,1],1]
expected: [1,2,4,6]
case 3
in: [[1,2,3,4],[0.5,0,0.5],1]
expected: [0.5,1,2,3]
case 4
in: [[1,2,3,4],[1,0,1],2]
expected: [1,2,3,4]
Starter codePython
def causal_conv1d(x, kernel, dilation=1):
    # Your code here
    pass

4 test cases · run your code against them in the editor