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

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

Fancier Model (Constructor)class CNN2(nn.Module):def __init__(self, n_filters, p=0.0):super(CNN2, self).__init__()self.n_filters = n_filtersself.p = p# Creates the convolution layersself.conv1 = nn.Conv2d(in_channels=3,out_channels=n_filters,kernel_size=3)self.conv2 = nn.Conv2d(in_channels=n_filters,out_channels=n_filters,kernel_size=3)# Creates the linear layers# Where does this 5 * 5 come from?! Check it belowself.fc1 = nn.Linear(n_filters * 5 * 5, 50)self.fc2 = nn.Linear(50, 3)# Creates dropout layersself.drop = nn.Dropout(self.p)There are two convolutional layers, and two linear layers, fc1 (the hidden layer)and fc2 (the output layer)."Where are the layers for activation functions and max pooling?"Well, the max pooling layer doesn’t learn anything, so we can use its functionalform: F.max_pool2d(). The same goes for the chosen activation function: F.relu().If you choose the parametric ReLU (PReLU), you shouldn’t usethe functional form since it needs to learn the coefficient ofleakage (the slope of the negative part).On the one hand, you keep the model’s attributes to a minimum. On the other hand,you don’t have layers to hook anymore, so you cannot capture the output ofactivation functions and max pooling operations anymore.Fancier Model | 429

Let’s create our two convolutional blocks in a method aptly named featurizer:Fancier Model (Featurizer)def featurizer(self, x):# First convolutional block# 3@28x28 -> n_filters@26x26 -> n_filters@13x13x = self.conv1(x)x = F.relu(x)x = F.max_pool2d(x, kernel_size=2)# Second convolutional block# n_filters@13x13 -> n_filters@11x11 -> n_filters@5x5x = self.conv2(x)x = F.relu(x)x = F.max_pool2d(x, kernel_size=2)# Input dimension (n_filters@5x5)# Output dimension (n_filters * 5 * 5)x = nn.Flatten()(x)return xThis structure, where an argument x is both input and output of every operation ina sequence, is fairly common. The featurizer produces a feature tensor of sizen_filters times 25.The next step is to build the classifier using the linear layers, one as a hidden layer,the other as the output layer. But there is more to it: There is a dropout layerbefore each linear layer, and it will drop values with a probability p (the secondargument of our constructor method):430 | Chapter 6: Rock, Paper, Scissors

Let’s create our two convolutional blocks in a method aptly named featurizer:

Fancier Model (Featurizer)

def featurizer(self, x):

# First convolutional block

# 3@28x28 -> n_filters@26x26 -> n_filters@13x13

x = self.conv1(x)

x = F.relu(x)

x = F.max_pool2d(x, kernel_size=2)

# Second convolutional block

# n_filters@13x13 -> n_filters@11x11 -> n_filters@5x5

x = self.conv2(x)

x = F.relu(x)

x = F.max_pool2d(x, kernel_size=2)

# Input dimension (n_filters@5x5)

# Output dimension (n_filters * 5 * 5)

x = nn.Flatten()(x)

return x

This structure, where an argument x is both input and output of every operation in

a sequence, is fairly common. The featurizer produces a feature tensor of size

n_filters times 25.

The next step is to build the classifier using the linear layers, one as a hidden layer,

the other as the output layer. But there is more to it: There is a dropout layer

before each linear layer, and it will drop values with a probability p (the second

argument of our constructor method):

430 | Chapter 6: Rock, Paper, Scissors

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

Saved successfully!

Ooh no, something went wrong!