Implementing residual blocks

In this section, we'll implement a pre-activation ResNet to classify the CIFAR-10 images using PyTorch 1.3.1 and torchvision 0.4.2. Let's start:

  1. As usual, we'll start with the imports. Note that we'll use the shorthand F for the PyTorch functional module (https://pytorch.org/docs/stable/nn.html#torch-nn-functional):
import matplotlib.pyplot as pltimport torchimport torch.nn as nnimport torch.nn.functional as Fimport torch.optim as optimimport torchvisionfrom torchvision import transforms
  1. Next, let's define the pre-activation regular (non-bottleneck) residual block. We'll implement it as nn.Modulethe base class for all neural network modules. Let's start with the class definition and the __init__ method:

Get Advanced Deep Learning with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.