Path: blob/master/notebooks/book1/14/conv2d_torch.ipynb
1192 views
Kernel: Python 3
Please find jax implementation of this notebook here: https://colab.research.google.com/github/probml/pyprobml/blob/master/notebooks/book1/14/conv2d_jax.ipynb
Foundations of Convolutional neural nets
Based on sec 6.2 of http://d2l.ai/chapter_convolutional-neural-networks/conv-layer.html
In [ ]:
Cross correlation
In [ ]:
tensor([[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 [ ]:
tensor([[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.]])
Now we apply a vertical edge detector. It fires on the 1-0 and 0-1 boundaries.
In [ ]:
tensor([[ 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 [ ]:
tensor([[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.]])
Convolution as matrix multiplication
In [ ]:
tensor([[1, 2],
[3, 4]])
tensor([[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 [ ]:
tensor([[27., 37.],
[57., 67.]])
Optimizing the kernel parameters
Let's learn a kernel to match the output of our manual edge detector.
In [ ]:
batch 2, loss 12.626
batch 4, loss 2.939
batch 6, loss 0.829
batch 8, loss 0.277
batch 10, loss 0.103
tensor([[ 1.0161, -0.9523]])
Multiple input channels
In [ ]:
In [ ]:
torch.Size([2, 3, 3])
torch.Size([2, 2, 2])
torch.Size([2, 2])
tensor([[ 56., 72.],
[104., 120.]])
Multiple output channels
In [ ]:
torch.Size([3, 2, 2, 2])
torch.Size([3, 2, 2])
1x1 convolution
In [ ]:
torch.Size([2, 3, 3])
Pooling
In [ ]:
In [ ]:
tensor([[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.],
[12., 13., 14., 15.]])
torch.Size([4, 4])
tensor([[10., 11.],
[14., 15.]])
In [ ]:
tensor([[[[10., 11.],
[14., 15.]]]])
In [ ]: