Deep Learning · TEE · section

Drill 34 Convolution with a negative coefficient

Numerical practice from direct to mixed

Question

Question. Use the input A and kernel K shown below, stride 1, padding 0. Compute all outputs.

Given matrices
Input A
1201
0132
2101
1023
Kernel K
1−1
01

Stride 1; P = 0 (no padding).

Answer — step by step

Given

Input A and kernel K are printed with the question.

Stride S = 1; padding P = 0; bias = 0.

Use the kernel without flipping it.

To find

Calculate all nine output cells.

Formula — which one and why

Convolution values. At each valid location, multiply corresponding window and kernel entries, add all products and add the filter bias once. Move by the stated stride and repeat.

X = input; K = kernel; S = stride; b = filter bias; u,v locate a kernel entry. Use the printed kernel without flipping under the stated deep-learning convention.

Y_{ij}=\sum_{u,v}X_{iS+u,jS+v}K_{uv}+bLaTeX: Y_{ij}=\sum_{u,v}X_{iS+u,jS+v}K_{uv}+b

Yaad rakhne ka cue: Matching cells multiply; sab products add; bias sirf ek baar.

Solution — step by step

Formulas and the operation we are performing

A filter window produces one output value. Multiply entries at matching positions and add the products. In this deep-learning convention, keep the filter in its printed orientation.

Y_{ij}=\sum_{u=0}^{K_h-1}\sum_{v=0}^{K_w-1}X_{iS+u,jS+v}K_{uv}+bLaTeX: Y_{ij}=\sum_{u=0}^{K_h-1}\sum_{v=0}^{K_w-1}X_{iS+u,jS+v}K_{uv}+b

Here i and j identify an output position; u and v count positions inside its window. S is stride, and b is the filter bias, zero in this question. The equation uses a padded input if padding is present and zero-based indices; the working below labels output positions from 1 for readability.

N_{\mathrm{out}}=\left\lfloor\frac{N+2P-K}{S}\right\rfloor+1LaTeX: N_{\mathrm{out}}=\left\lfloor\frac{N+2P-K}{S}\right\rfloor+1

N is input width/height, P is padding per side, and K is filter width/height. Floor means take the integer below the result, then add one.

Follow each selected input window below. Matching row colors connect its input cells, kernel coefficients and products; the outlined output cell shows where their sum belongs.

Step 1 shape: \left\lfloor\frac{4+2 \times  0-2}{1}\right\rfloor+1LaTeX: \left\lfloor\frac{4+2 \times 0-2}{1}\right\rfloor+1 = 3 along each axis. Use the deep-learning cross-correlation convention: do not flip the printed kernel.

Output position (1, 1)

Use this position's input window. Multiply corresponding entries row by row; then add all row totals.

Follow window (1, 1) into one output cell

Input rows 1–2, columns 1–2. The window moves 1 cell per output step.

1. Select the input window
1201
0132
2101
1023

Only the outlined cells belong to this output.

2. Match the kernel
1−1
01

Same row and column positions; no matrix multiplication.

3. Multiply cell by cell
1−2
01

Each cell is input × matching kernel coefficient.

4. Add the products row by row
Row 11 + (−2) = −1
Row 20 + 1 = 1

(−1) + 1 = 0

5. Place it in the output
051
0−22
132

Output row 1, column 1.

Window row 1.

R_1=(1)\times(1)+(2)\times(-1)=-1LaTeX: R_1=(1)\times(1)+(2)\times(-1)=-1

Window row 2.

R_2=(0)\times(0)+(1)\times(1)=1LaTeX: R_2=(0)\times(0)+(1)\times(1)=1

Add the rows to obtain one output cell.

Y_{1,1}=(-1)+(1)=0LaTeX: Y_{1,1}=(-1)+(1)=0

Output position (1, 2)

Use this position's input window. Multiply corresponding entries row by row; then add all row totals.

Follow window (1, 2) into one output cell

Input rows 1–2, columns 2–3. The window moves 1 cell per output step.

1. Select the input window
1201
0132
2101
1023

Only the outlined cells belong to this output.

2. Match the kernel
1−1
01

Same row and column positions; no matrix multiplication.

3. Multiply cell by cell
20
03

Each cell is input × matching kernel coefficient.

4. Add the products row by row
Row 12 + 0 = 2
Row 20 + 3 = 3

2 + 3 = 5

5. Place it in the output
051
0−22
132

Output row 1, column 2.

Window row 1.

R_1=(2)\times(1)+(0)\times(-1)=2LaTeX: R_1=(2)\times(1)+(0)\times(-1)=2

Window row 2.

R_2=(1)\times(0)+(3)\times(1)=3LaTeX: R_2=(1)\times(0)+(3)\times(1)=3

Add the rows to obtain one output cell.

Y_{1,2}=(2)+(3)=5LaTeX: Y_{1,2}=(2)+(3)=5

Output position (1, 3)

Use this position's input window. Multiply corresponding entries row by row; then add all row totals.

Follow window (1, 3) into one output cell

Input rows 1–2, columns 3–4. The window moves 1 cell per output step.

1. Select the input window
1201
0132
2101
1023

Only the outlined cells belong to this output.

2. Match the kernel
1−1
01

Same row and column positions; no matrix multiplication.

3. Multiply cell by cell
0−1
02

Each cell is input × matching kernel coefficient.

4. Add the products row by row
Row 10 + (−1) = −1
Row 20 + 2 = 2

(−1) + 2 = 1

5. Place it in the output
051
0−22
132

Output row 1, column 3.

Window row 1.

R_1=(0)\times(1)+(1)\times(-1)=-1LaTeX: R_1=(0)\times(1)+(1)\times(-1)=-1

Window row 2.

R_2=(3)\times(0)+(2)\times(1)=2LaTeX: R_2=(3)\times(0)+(2)\times(1)=2

Add the rows to obtain one output cell.

Y_{1,3}=(-1)+(2)=1LaTeX: Y_{1,3}=(-1)+(2)=1

Output position (2, 1)

Use this position's input window. Multiply corresponding entries row by row; then add all row totals.

Follow window (2, 1) into one output cell

Input rows 2–3, columns 1–2. The window moves 1 cell per output step.

1. Select the input window
1201
0132
2101
1023

Only the outlined cells belong to this output.

2. Match the kernel
1−1
01

Same row and column positions; no matrix multiplication.

3. Multiply cell by cell
0−1
01

Each cell is input × matching kernel coefficient.

4. Add the products row by row
Row 10 + (−1) = −1
Row 20 + 1 = 1

(−1) + 1 = 0

5. Place it in the output
051
0−22
132

Output row 2, column 1.

Window row 1.

R_1=(0)\times(1)+(1)\times(-1)=-1LaTeX: R_1=(0)\times(1)+(1)\times(-1)=-1

Window row 2.

R_2=(2)\times(0)+(1)\times(1)=1LaTeX: R_2=(2)\times(0)+(1)\times(1)=1

Add the rows to obtain one output cell.

Y_{2,1}=(-1)+(1)=0LaTeX: Y_{2,1}=(-1)+(1)=0

Output position (2, 2)

Use this position's input window. Multiply corresponding entries row by row; then add all row totals.

Follow window (2, 2) into one output cell

Input rows 2–3, columns 2–3. The window moves 1 cell per output step.

1. Select the input window
1201
0132
2101
1023

Only the outlined cells belong to this output.

2. Match the kernel
1−1
01

Same row and column positions; no matrix multiplication.

3. Multiply cell by cell
1−3
00

Each cell is input × matching kernel coefficient.

4. Add the products row by row
Row 11 + (−3) = −2
Row 20 + 0 = 0

(−2) + 0 = −2

5. Place it in the output
051
0−22
132

Output row 2, column 2.

Window row 1.

R_1=(1)\times(1)+(3)\times(-1)=-2LaTeX: R_1=(1)\times(1)+(3)\times(-1)=-2

Window row 2.

R_2=(1)\times(0)+(0)\times(1)=0LaTeX: R_2=(1)\times(0)+(0)\times(1)=0

Add the rows to obtain one output cell.

Y_{2,2}=(-2)+(0)=-2LaTeX: Y_{2,2}=(-2)+(0)=-2

Output position (2, 3)

Use this position's input window. Multiply corresponding entries row by row; then add all row totals.

Follow window (2, 3) into one output cell

Input rows 2–3, columns 3–4. The window moves 1 cell per output step.

1. Select the input window
1201
0132
2101
1023

Only the outlined cells belong to this output.

2. Match the kernel
1−1
01

Same row and column positions; no matrix multiplication.

3. Multiply cell by cell
3−2
01

Each cell is input × matching kernel coefficient.

4. Add the products row by row
Row 13 + (−2) = 1
Row 20 + 1 = 1

1 + 1 = 2

5. Place it in the output
051
0−22
132

Output row 2, column 3.

Window row 1.

R_1=(3)\times(1)+(2)\times(-1)=1LaTeX: R_1=(3)\times(1)+(2)\times(-1)=1

Window row 2.

R_2=(0)\times(0)+(1)\times(1)=1LaTeX: R_2=(0)\times(0)+(1)\times(1)=1

Add the rows to obtain one output cell.

Y_{2,3}=(1)+(1)=2LaTeX: Y_{2,3}=(1)+(1)=2

Output position (3, 1)

Use this position's input window. Multiply corresponding entries row by row; then add all row totals.

Follow window (3, 1) into one output cell

Input rows 3–4, columns 1–2. The window moves 1 cell per output step.

1. Select the input window
1201
0132
2101
1023

Only the outlined cells belong to this output.

2. Match the kernel
1−1
01

Same row and column positions; no matrix multiplication.

3. Multiply cell by cell
2−1
00

Each cell is input × matching kernel coefficient.

4. Add the products row by row
Row 12 + (−1) = 1
Row 20 + 0 = 0

1 + 0 = 1

5. Place it in the output
051
0−22
132

Output row 3, column 1.

Window row 1.

R_1=(2)\times(1)+(1)\times(-1)=1LaTeX: R_1=(2)\times(1)+(1)\times(-1)=1

Window row 2.

R_2=(1)\times(0)+(0)\times(1)=0LaTeX: R_2=(1)\times(0)+(0)\times(1)=0

Add the rows to obtain one output cell.

Y_{3,1}=(1)+(0)=1LaTeX: Y_{3,1}=(1)+(0)=1

Output position (3, 2)

Use this position's input window. Multiply corresponding entries row by row; then add all row totals.

Follow window (3, 2) into one output cell

Input rows 3–4, columns 2–3. The window moves 1 cell per output step.

1. Select the input window
1201
0132
2101
1023

Only the outlined cells belong to this output.

2. Match the kernel
1−1
01

Same row and column positions; no matrix multiplication.

3. Multiply cell by cell
10
02

Each cell is input × matching kernel coefficient.

4. Add the products row by row
Row 11 + 0 = 1
Row 20 + 2 = 2

1 + 2 = 3

5. Place it in the output
051
0−22
132

Output row 3, column 2.

Window row 1.

R_1=(1)\times(1)+(0)\times(-1)=1LaTeX: R_1=(1)\times(1)+(0)\times(-1)=1

Window row 2.

R_2=(0)\times(0)+(2)\times(1)=2LaTeX: R_2=(0)\times(0)+(2)\times(1)=2

Add the rows to obtain one output cell.

Y_{3,2}=(1)+(2)=3LaTeX: Y_{3,2}=(1)+(2)=3

Output position (3, 3)

Use this position's input window. Multiply corresponding entries row by row; then add all row totals.

Follow window (3, 3) into one output cell

Input rows 3–4, columns 3–4. The window moves 1 cell per output step.

1. Select the input window
1201
0132
2101
1023

Only the outlined cells belong to this output.

2. Match the kernel
1−1
01

Same row and column positions; no matrix multiplication.

3. Multiply cell by cell
0−1
03

Each cell is input × matching kernel coefficient.

4. Add the products row by row
Row 10 + (−1) = −1
Row 20 + 3 = 3

(−1) + 3 = 2

5. Place it in the output
051
0−22
132

Output row 3, column 3.

Window row 1.

R_1=(0)\times(1)+(1)\times(-1)=-1LaTeX: R_1=(0)\times(1)+(1)\times(-1)=-1

Window row 2.

R_2=(2)\times(0)+(3)\times(1)=3LaTeX: R_2=(2)\times(0)+(3)\times(1)=3

Add the rows to obtain one output cell.

Y_{3,3}=(-1)+(3)=2LaTeX: Y_{3,3}=(-1)+(3)=2

Final output: \begin{bmatrix}0 & 5 & 1\\0 & -2 & 2\\1 & 3 & 2\end{bmatrix}LaTeX: \begin{bmatrix}0 & 5 & 1\\0 & -2 & 2\\1 & 3 & 2\end{bmatrix}. Each output came from its own complete window; stride changes the next window position, not the multiplication rule.