CoCalc Public Filesjulia-snippets / H 12.3 - Convolution layer in CNN.ipynb
Author: Antonello Lobianco
In [3]:
using LinearAlgebra

In [5]:
ReLU(x) = max(0,x)
x = [1 1 2 1 1;
3 1 4 1 1;
1 3 1 2 2;
1 2 1 1 1;
1 1 2 1 1]
w = [ 1 -2  0;
1  0  1;
-1  1  0]
(xr,xc) = size(x)
(wr,wc) = size(w)
z = [sum(x[r:r+wr-1,c:c+wc-1] .* w) for c in 1:xc-wc+1 for r in 1:xr-wr+1] # Julia is column mayor
u = ReLU.(z)

9-element Array{Int64,1}: 8 4 0 0 0 5 6 5 0
In [6]:
final = reshape(u, xr-wr+1, xc-wc+1)


3×3 Array{Int64,2}: 8 0 6 4 0 5 0 5 0
In [ ]: