A real convolution layer reads every input channel and has many filters. Build its forward pass.
What to build
A program that computes a convolution layer with several input channels, several filters, stride, zero padding and ReLU.
Requirements
- Output size: Hout = (H + 2P − K) // S + 1 and Wout = (W + 2P − K) // S + 1. Positions outside the input count as 0 (zero padding).
- Each filter has one K × K kernel per input channel and one bias. Its output is the bias plus all channel contributions (cross-correlation, no flip), then ReLU.
- For each filter print
filter i(from 1), then its Hout rows.
Input
Line 1: H, W and C. Then C blocks of H rows. Then F K S P. Then, for each filter, C blocks of K rows of weights followed by a line with the bias.
Output
F labelled blocks.
Constraints
1 ≤ H, W ≤ 20; C, F ≤ 4; the output size is at least 1 × 1.
Example
Input:
2 2 2
1 2
3 4
10 20
30 40
1 1 1 0
1
1
0
Output:
filter 1
11 22
33 44
Why it works
Each filter looks at the same window in every channel and adds the results, so one output channel combines evidence from all input channels; stride and padding only change where the windows are placed.
Examples
2 2 2 1 2 3 4 10 20 30 40 1 1 1 0 1 1 0
filter 1 11 22 33 44
3 3 1 1 1 1 1 1 1 1 1 1 1 3 2 1 1 1 1 1 1 1 1 1 1 -1
filter 1 3 3 3 3
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Read the input as C blocks of H rows. Each filter is C blocks of K rows of weights, then a bias line.
Hint 2
Output (i, j) of a filter is its bias plus the sum over channels ch and kernel cells (a, e) of weight × input(ch, i × S − P + a, j × S − P + e), with 0 outside the image; then ReLU.
Solution
Show one correct solution (try it yourself first)
h, w, c = map(int, input().split())
x = [[list(map(int, input().split())) for _ in range(h)] for _ in range(c)]
f, k, s, p = map(int, input().split())
oh, ow = (h + 2 * p - k) // s + 1, (w + 2 * p - k) // s + 1
def px(ch, r, col):
return x[ch][r][col] if 0 <= r < h and 0 <= col < w else 0
for n in range(f):
wts = [[list(map(int, input().split())) for _ in range(k)] for _ in range(c)]
b = int(input())
print("filter", n + 1)
for i in range(oh):
row = []
for j in range(ow):
total = b + sum(wts[ch][a][e] * px(ch, i * s - p + a, j * s - p + e) for ch in range(c) for a in range(k) for e in range(k))
row.append(max(0, total))
print(" ".join(map(str, row)))