Kernel: Python 3 (ipykernel)
Please find torch implementation of this notebook here: https://colab.research.google.com/github/probml/pyprobml/blob/master/notebooks/book1/14/conv2d_torch.ipynb
Foundations of Convolutional neural nets
Based on sec 6.2 of http://d2l.ai/chapter_convolutional-neural-networks/conv-layer.html
In [1]:
Out[1]:
WARNING:absl:No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)
Cross correlation
In [3]:
Out[3]:
[[19. 25.]
[37. 43.]]
Edge detection
We make a small image X of 1s, with a vertical stripe (of width 4) of 0s in the middle.
In [4]:
Out[4]:
DeviceArray([[1., 1., 0., 0., 0., 0., 1., 1.],
[1., 1., 0., 0., 0., 0., 1., 1.],
[1., 1., 0., 0., 0., 0., 1., 1.],
[1., 1., 0., 0., 0., 0., 1., 1.],
[1., 1., 0., 0., 0., 0., 1., 1.],
[1., 1., 0., 0., 0., 0., 1., 1.]], dtype=float32)
Now we apply a vertical edge detector. It fires on the 1-0 and 0-1 boundaries.
In [5]:
Out[5]:
[[ 0. 1. 0. 0. 0. -1. 0.]
[ 0. 1. 0. 0. 0. -1. 0.]
[ 0. 1. 0. 0. 0. -1. 0.]
[ 0. 1. 0. 0. 0. -1. 0.]
[ 0. 1. 0. 0. 0. -1. 0.]
[ 0. 1. 0. 0. 0. -1. 0.]]
It fails to detect horizontal edges.
In [6]:
Out[6]:
DeviceArray([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]], dtype=float32)
Convolution as matrix multiplication
In [7]:
Out[7]:
[[1 2]
[3 4]]
[[1. 2. 0. 3. 4. 0. 0. 0. 0.]
[0. 1. 2. 0. 3. 4. 0. 0. 0.]
[0. 0. 0. 1. 2. 0. 3. 4. 0.]
[0. 0. 0. 0. 1. 2. 0. 3. 4.]]
In [8]:
Out[8]:
[[27. 37.]
[57. 67.]]
Optimizing the kernel parameters
Let's learn a kernel to match the output of our manual edge detector.
In [9]:
Out[9]:
(6, 7)
batch 2, loss 19.368
batch 4, loss 5.172
batch 6, loss 1.655
batch 8, loss 0.600
batch 10, loss 0.233
[[ 1.0312853 -0.93357116]]
Multiple input channels
In [10]:
In [11]:
Out[11]:
(2, 3, 3)
(2, 2, 2)
(2, 2)
[[ 56. 72.]
[104. 120.]]
Multiple output channels
In [12]:
Out[12]:
(3, 2, 2, 2)
(3, 2, 2)
In [13]:
Out[13]:
(2, 3, 3)
Pooling
In [14]:
In [15]:
Out[15]:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
(4, 4)
[[10. 11.]
[14. 15.]]
In [16]:
Out[16]:
[[[[10]
[11]]
[[14]
[15]]]]
In [ ]: