22.02.2024 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Model Configuration

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())

14

15 # Classification

16 # Hidden Layer

17 model_cnn1.add_module('fc1',

18 nn.Linear(in_features=n_channels*4*4, out_features=10)

19 )

20 model_cnn1.add_module('relu2', nn.ReLU())

21 # Output Layer

22 model_cnn1.add_module('fc2',

23 nn.Linear(in_features=10, out_features=3)

24 )

25

26 lr = 0.1

27 multi_loss_fn = nn.CrossEntropyLoss(reduction='mean')

28 optimizer_cnn1 = optim.SGD(model_cnn1.parameters(), lr=lr)

Model Training

1 sbs_cnn1 = StepByStep(model_cnn1, multi_loss_fn, optimizer_cnn1)

2 sbs_cnn1.set_loaders(train_loader, val_loader)

3 sbs_cnn1.train(20)

Visualizing Filters

fig_filters = sbs_cnn1.visualize_filters('conv1', cmap='gray')

412 | Chapter 5: Convolutions

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

Saved successfully!

Ooh no, something went wrong!