22.02.2024 Views

Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

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

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

figures at the beginning of this chapter). Let’s stick with a kernel size of three,

which will reduce the image size by two pixels in each dimension (we are not using

padding here).

Our featurizer, which will encode our images into features using convolutions,

should look like this:

Model Configuration — Featurizer

1 torch.manual_seed(13)

2 model_cnn1 = nn.Sequential()

3

4 # Featurizer

5 # Block 1: 1@10x10 -> n_channels@8x8 -> n_channels@4x4

6 n_channels = 1

7 model_cnn1.add_module('conv1', nn.Conv2d(

8 in_channels=1, out_channels=n_channels, kernel_size=3

9 ))

10 model_cnn1.add_module('relu1', nn.ReLU())

11 model_cnn1.add_module('maxp1', nn.MaxPool2d(kernel_size=2))

12 # Flattening: n_channels _ 4 _ 4

13 model_cnn1.add_module('flatten', nn.Flatten())

I am keeping the number of channels as a variable, so you can try different values

for it if you like.

Let’s follow what happens to an input image (single-channel, 10x10 pixels in

size—1@10x10):

• The image is convolved with the kernel, and the resulting image has one

channel, and is 8x8 pixels in size (1@8x8).

• A ReLU activation function is applied to the resulting image.

• The "activated" image goes under a max pooling operation with a kernel size of

two, so it is divided into 16 chunks of size two-by-two, resulting in an image

with one channel, but only 4x4 pixels in size (1@4x4).

• These 16 values can be considered features, and are flattened into a tensor

with 16 elements.

A Multiclass Classification Problem | 385

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

Saved successfully!

Ooh no, something went wrong!