Creating a sequential block
You decided to redesign your binary CNN model template by creating a block of convolutional layers. This will help you stack multiple layers sequentially. With this improved model, you will be able to easily design various CNN architectures.
torch
and torch.nn
as nn
have been imported.
This exercise is part of the course
Deep Learning for Images with PyTorch
Exercise instructions
- In the
__init__()
method, define a block of convolutional layers and assign it toself.conv_block
. - In the
forward()
pass, pass the inputs through the convolutional block you defined.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
class BinaryImageClassification(nn.Module):
def __init__(self):
super(BinaryImageClassification, self).__init__()
# Create a convolutional block
self.conv_block = ____(
nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
)
def forward(self, x):
# Pass inputs through the convolutional block
x = ____
return x