20.03.2021 Views

Deep-Learning-with-PyTorch

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

196 CHAPTER 8 Using convolutions to generalize

image

0

1

0

0 0

1 0 0 0 1 0

1 1 0 1 1 1

1 0 0 0 1 0

0 0

kernel

kernel

weights

0

1

1

1

0

1

0

0

0

1

0

1

1

1

0

1

0 1 0 0

0 0 1 0

0 0 0 0

0 0 0 0

output

5 2

2 2

0 1 0 0

0 1 0 0

0 1 0 0

1 0 1 0

1 1 1 0

0 1 1 1

0 1 0 0

0 0 1 0

scalar product

betwEen translated

kernel and image

(zeros outside the kernel)

Locality

same kernel weights

used acroSs the image

TRanslation

invariance

Figure 8.1

Convolution: locality and translation invariance

It’s now possible to see the connection to what we were stating earlier: a convolution is

equivalent to having multiple linear operations whose weights are zero almost everywhere

except around individual pixels and that receive equal updates during training.

Summarizing, by switching to convolutions, we get

• Local operations on neighborhoods

• Translation invariance

• Models with a lot fewer parameters

The key insight underlying the third point is that, with a convolution layer, the number

of parameters depends not on the number of pixels in the image, as was the case

in our fully connected model, but rather on the size of the convolution kernel (3 × 3,

5 × 5, and so on) and on how many convolution filters (or output channels) we decide

to use in our model.

8.2 Convolutions in action

Well, it looks like we’ve spent enough time down a rabbit hole! Let’s see some PyTorch

in action on our birds versus airplanes challenge. The torch.nn module provides convolutions

for 1, 2, and 3 dimensions: nn.Conv1d for time series, nn.Conv2d for images,

and nn.Conv3d for volumes or videos.

For our CIFAR-10 data, we’ll resort to nn.Conv2d. At a minimum, the arguments we

provide to nn.Conv2d are the number of input features (or channels, since we’re dealing

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!