Conv2d Matrixcomp
Conv2d Matrixcomp
Conv2d Matrixcomp
Matrix computation
Dr. Thanh-Sach LE
LTSACH@hcmut.edu.vn
• Notation:
W : Filter’s kernel
5 im2col
0
∂b
X W Rot180 (W)
Input image Filter’s kernel Rotated kernel
6 im2col
How to create matrix 𝕏conv
w22x11 w21x12 x13
w22 w21
w12x21 w11x22 x23
w12 w11
x31 x32 x33
flattening
0
∂b
Rotate180 (W)
X
x22
x21
x12
x11
𝕏conv
Flattened in the order corresponding to w11, w12, w21, w22
7 im2col
How to create matrix 𝕏conv
w22x11 w21x12 x13
𝕏conv
Flattened in the order corresponding to w11, w12, w21, w22
8 im2col
How to create matrix 𝕏conv
w22x11 w21x12 x13
𝕏conv
Flattened in the order corresponding to w11, w12, w21, w22
9 im2col
How to create matrix 𝕏conv
w22x11 w21x12 x13
x22 x22 x23 x22 x23 x32 x22 x23 x32 x33
x21 x21 x22 x21 x22 x31 x21 x22 x31 x32
x12 x12 x13 x12 x13 x22 x12 x13 x22 x23
x11 x11 x12 x11 x12 x21 x11 x12 x21 x22
𝕏conv
Flattened in the order corresponding to w11, w12, w21, w22
Convolution
kernel2row
Dr. Thanh-Sach LE
LTSACH@hcmut.edu.vn
• Notation:
W : Filter’s kernel
𝕎conv : Matrix created by rearranging values in the kernel into row of a matrix
(illustrated in next slide)
12 kernel2row
0
∂b
X W Rot180 (W)
Input image Filter’s kernel Rotated kernel
13 kernel2row
X
w22 w21 0 w12 w11 0 0 0 0
flattening
𝕎conv
14 kernel2row
X
w22 w21 0 w12 w11 0 0 0 0
𝕎conv
15 kernel2row
X
w22 w21 0 w12 w11 0 0 0 0
𝕎conv
16 kernel2row
X
w22 w21 0 w12 w11 0 0 0 0
𝕎conv
17 Notation
𝕏flat x11 x12 x13 x21 x22 x23 x31 x32 x33
x11 x12 x13
x22 x23 x32 x33
x21 x22 x23
x21 x22 x31 x32
x31 x32 x33 𝕏conv
x12 x13 x22 x23
∂b
X x11 x12 x21 x22
w11 w12
w22 w21 0 w12 w11 0 0 0 0
X * W = Reshapeo1×o2(𝕎flat × 𝕏conv)
= Reshapeo1×o2(𝕎conv × 𝕏flat)
T
∂b ΔW = Reshapek1×k2(Δ𝕐flat × 𝕏conv)
ΔX = Reshapei1×i2(𝕎Tconv × Δ𝕐flat)
𝕏conv =
x21 x22 x31 x32
𝕎flat = w11 w12 w21 w22
x12 x13 x22 x23
y11 y12
Reshape to 2x2: Y=
y21 y22
20 Convolution as matrix multiplication
∂b
δy11 δy12
Reshape to 2x2: ΔW =
δy21 δy22
21 Convolution as matrix multiplication
T
∂b 𝕎conv = w11 w12 w21 w22
0 w11 0 w21
0 0 w12 0
0 0 w11 w12
0 0 0 w11
22 Convolution as matrix multiplication
w22 0 0 0
w21 w22 0 0
0 w21 0 0
w12 0 w22 0
T
𝕎conv ∂b = w11 w12 w21 w22
0 w11 0 w21
0 0 w12 0
0 0 w11 w12
0 0 0 w11
δx13 = δy12w21
δx11 δx12 δx13
δx21 = δy11w12 + δy21w22
∂b
δx22 = δy11w11 + δy12w12 + δy21w21 + δy11w22 Reshape: 4x4 δx21 δx22 δx23
δx31 = δy21w12 ∆X
δx32 = δy21w11 + δy22w12
δx33 = δy22w11