#117Convolution — one patch stepEasy

Convolution — one patch step

Background

A convolution is a small recipe repeated at every position: take a patch of the image, multiply it element-wise by the filter, sum, and add a bias. That single number becomes one entry of the feature map.

y=∑i,jpatchij kernelij+by = \sum_{i,j} \text{patch}_{ij}\,\text{kernel}_{ij} + b

Problem statement

Implement conv_step(patch, kernel, bias) computing one output value: the element-wise product summed, plus bias.

Input

  • patch — array-like, a region of the image (same shape as kernel).
  • kernel — array-like filter weights.
  • bias — float.

Output

Returns a Python float: ∑patch⊙kernel+b\sum \text{patch}\odot\text{kernel} + b.

Examples

Example 1

Input:  patch = [[1, 2], [3, 4]], kernel = [[1, 0], [0, 1]], bias = 0
Output: 5.0

Explanation: 1⋅1+2⋅0+3⋅0+4⋅1+0=51\cdot1 + 2\cdot0 + 3\cdot0 + 4\cdot1 + 0 = 5.

Example 2 — with a bias

Input:  patch = [[1, 1, 1]], kernel = [[1, 1, 1]], bias = 2
Output: 5.0

Explanation: 3+2=53 + 2 = 5.

Constraints

  • Element-wise multiply, sum all entries, add bias.
  • Return a plain float.

Notes

  • The (non-linear) activation usually applied after this — e.g. ReLU — is the final step of a full conv layer; here you compute the pre-activation.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • •Example — identity-ish kernel
  • •Reference — sum kernel with bias
  • •Sample — negative weights cancel