CNN Training Loop Explained – Neural Network Code Project

Welcome to this neural network programming series. In this episode, we will learn how to build the training loop for a convolutional neural network using Python and PyTorch.

Episode on underfitting from the deep learning fundamentals course:


? Check out the blog post and other resources for this video:


, , , , , , , ,

15 thoughts on “CNN Training Loop Explained – Neural Network Code Project

  1. What I learned:
    1. Before this episode, I think this is simple.Just add loop.Done.
    2. In fact I read a lot of code about the training loop.But I don’t get clear two things
    3. Why there is two loops. One for batch, One for epoch.
    4. Why the loss and correct should add. Now I get the add happened in one epoch. Before I thought it happened between the epochs.
    5. How to debug the code.
    6. I got a codebase. And don’t know what to do. Now I got it.
    7. First part is debug the model see the change of layers.
    8. Second debug the training process. See the input and output and the layers
    9. When we say see, means monitor the shape and max or min value

  2. Hey, hiveminds 🙂
    First: This channel is by far!!!! the BEST. You are absolutely rocking it 🙂 THANKS SO MUCH

    For the ones being interested:

    a) I used cuda for training and testing (time needed for 5 epochs – CPU: 3:01 min / CUDA: 1:20 min – the time saving depends on your hardware):

    network = Network()
    network.cuda() # This line puts our network on your CUDA

    for batch in train_loader:
    images, labels = batch

    images = images.cuda()# this line is giving the images to your CUDA
    labels = labels.cuda()# this line is giving the labels to your CUDA

    b) I added the shuffle function to the train_loader, to avoid that the NN gets used to the order of the pictures in our train Data:
    train_loader =, batch_size = 100, shuffle = True)

    c) I added the test_set to see, if our model works for pictures, never seen before:
    test_set = torchvision.datasets.FashionMNIST(
    root =r"C:UsersPythonDesktopMNIST_Fashion"
    ,train = False
    ,download = True

    test_loader =, batch_size=100, shuffle = True)
    for batch in test_loader:
    total_test_correct = 0

    test_image, test_label = batch
    test_image = test_image.cuda()
    test_label = test_label.cuda()
    test_prediciton = network(test_image)

    total_test_correct += get_test_correct(test_image,test_label)

    print("Number correct Predictions for the Test Images: ", total_test_correct)

    Notice: I just started programming and learning about NN´s so there can be mistakes in my comment 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *