1-D Convolution Forward Pass

medium
Unit 9 · Backprop & Regularization0/3 to milestone

Implement the forward pass of a 1-D convolution layer (strictly speaking a cross-correlation, like every deep-learning framework uses).

conv1d(x, w, b, stride=1, padding=0)

  • x: input of shape (C_in, L)
  • w: kernels of shape (C_out, C_in, K)
  • b: bias of shape (C_out,)
  • Pad both ends of x with padding zeros, then slide each kernel with the given stride.

Output shape is (C_out, L_out) with

Lout=⌊L+2⋅padding−Kstride⌋+1L_{out} = \left\lfloor \frac{L + 2\cdot\text{padding} - K}{\text{stride}} \right\rfloor + 1

Example

conv1d(np.array([[1., 2., 3., 4.]]), np.array([[[1., 0., -1.]]]), np.array([0.]))
# array([[-2., -2.]])
#cnn#numpy3 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
1-D Convolution Forward Pass · TechGradify