Skip to content

Convolution as equation 21.8 (kernel not flipped, same length) - #1393

Merged
dmeoli merged 2 commits into
masterfrom
fix/convolution
Dec 2, 2026
Merged

dmeoli merged 2 commits into
masterfrom
fix/convolution

Conversation

@dmeoli

@dmeoli dmeoli commented Dec 2, 2026

Copy link
Copy Markdown
Member

conv1D and the edge detectors now follow equation 21.8: the kernel is not flipped (footnote 4 of chapter 21), zero padding as in figure 21.5, output as long as the input also for a kernel longer than the input, and for an even kernel the extra element on the right, as the 'same' padding of Keras and PyTorch

expected values that change:

  • test_conv_layer_1d, kernel [1, 0, -1]: the outputs change sign
  • kernel longer than the input: [6, 6, 6] instead of [3, 6, 6, 6, 5]
  • test_edge_detectors: the uniform region right of the step is compared at column 5 instead of 6

np.convolve flips the kernel, so an asymmetric kernel gave the opposite
result of the book, and a kernel longer than the input gave max(n, l)
values. conv1D now computes z_i = sum_j k_j x_{j+i-(l+1)/2} with zero
padding, always n values; for an even l the kernel covers one element
more to the right of x_i. The [1, 0, -1] expectations of the ConvLayer1D
test change sign, and the long kernel gives [6, 6, 6] instead of
[3, 6, 6, 6, 5].
The edge detectors used scipy.signal.convolve2d, which flips the kernel.
conv2D applies equation 21.8 along both dimensions with the conventions
of conv1D. With the 4 columns of the derivative of gaussian not flipped,
the uniform region right of the step is compared at column 5 instead of
column 6, which the kernel reaches from the padding.
@dmeoli
dmeoli marked this pull request as ready for review December 2, 2026 05:16
@dmeoli
dmeoli merged commit 4f40687 into master Dec 2, 2026
4 checks passed
@dmeoli
dmeoli deleted the fix/convolution branch December 2, 2026 05:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant