Build a Convolutional Network

Develop a Convolutional Neural Network (CNN) with Pytorch.

It is time to apply what we learned about CNNs through an assignment. We will build a fully functional CNN and train it with our familiar CIFAR10 dataset.

The CNN structure will be:

  • A conv layer with 3 channels as input, 6 channels as output, and a 5x5 kernel
  • A 2x2 max-pooling layer
  • A conv layer with 6 channels as input, 16 channels as output, and a 5x5 kernel
  • A linear layer with 1655 nodes
  • A linear layer with 120 nodes
  • A linear layer with 84 nodes
  • A linear layer with 10 nodes

Don’t forget to add a Relu layer after each convolutional and linear layer, except the last one because that should output the actual classification.

Finally, let’s use Vanilla SGD once again with a learning rate of 0.001 and a momentum of 0.9, and the cross-entropy loss for our loss function. As you can see in the code, we will train the model in the entire dataset for 1 epoch.

You will need to write code in 5 different places:

  1. Define the layers in the CNN __init__.
  2. Stack the layers in forward.
  3. Define the loss and optimizer in train.
  4. Get the Cifar10 image and label, inside the for-loop in train.
  5. Run the forward and backward pass.

Get hands-on with 1200+ tech skills courses.