Pytorch CNN example (Convolutional Neural Network)

Aladdin Persson · Intermediate ·🧬 Deep Learning ·6y ago

Key Takeaways

The video demonstrates how to build a Convolutional Neural Network (CNN) in PyTorch using the MNIST dataset, covering the implementation of convolutional layers, max pooling layers, and fully connected layers. It also shows how to train the model and evaluate its performance on the training and test data.

Full Transcript

[Music] in my last video I went through how to build a simple neural network in pi torch and we went through how all of the imports work we went through how to load data the hyper parameters the loss function and training the network and lastly also checking the accuracy of the model on the training in the test data all we want to do now is instead of using a simple fully connected neural network we want to modify the code and create so that we use a convolutional neural network instead so convolutional neural networks works a lot better on images then fully connected so I believe we're gonna get better performance on the emne Stata set so let's see what we get but first we have to create the network so let's make a class CNN inherit the N n dot module then in our function we're gonna take as input the number of in channels of the image so in MN s this is gonna be 1 if you use something like cypher 10 dataset or something that has color then there's probably gonna be in channels equal to 3 because of RGB but in our case we're just gonna be it's just gonna be 1 and then the number of classes which is going to be 10 yeah I guess we could do like this as well set it to a standard of ten then we need to call the super right okay so what we want to do now is we want to create a convolutional layer so we can do self-talk on one equals n n comm 2d and then here yeah we can see it here we have his argument in channels out channels the kernel size stride padding I'm not gonna go into depth on how those work or how convolutions work just the implementation of it in the comment I'll link you some resources if you want to learn more about how they actually work so the in channels that we're gonna have is as I said one and then we're gonna choose some arbitrary out channels let's say 4 or 8 then what we're gonna use kernel size is if we're gonna use a 3x3 stride we're gonna use a 1x1 padding we're gonna use a 1x1 these might be standard I'm not really sure apart sets them these might be default so you don't have to actually write them explicitly but it's good to do just to know which arguments to that we can send so what we're gonna use is a kernel 3x3 this again this is quite an arbitrary choice there's a reason why I chose a 3x3 kernel along with these padding and strides this is called a same thing it's called a same convolution same convolution means that the normally the output size will change depending on what we use for kernel stratum padding using these specific ones it's actually going to turn out that we keep the same dimensions so when we send in the input it's gonna be 28 by 28 if we do with these values we're gonna still have 28 by 28 output yeah so I just wanted to show you are using the formula I'm not gonna derive the formula but just show you that if we use that that kernel size and the padding and that's right then we get a same cover us like a same convolution meaning the the N in equals the and out so an in is the number of input features we have 28 using the M&S data set the padding which shows was 1 so 2 times 1 minus 3 divided by the stride take a floor of that and then we plus 1 so essentially this is gonna go going to be 27 right this is going to be 27 plus 1 which is 28 right so this is the N out right here and then in was also 28 alright we want to do next is use a pulling layer max pool 2d which yeah so we're gonna use 2 by 2 and stride 2 by 2 and again using the same formula that we use for this one if we it's gonna turn out to have the dimension size so let's say that we have 28 by 28 we use a max pool it's gonna make it to 14 by 14 then let's just do this again have to calm too and then lastly we're gonna have a full of connected layer yeah let's say so what we need to do here is that we had the out channels from this to eight so the in channels from this needs to be eight and let's set some our channels to maybe sixteen then what we need to do is sixteen here times so what we've done here is that but we haven't actually implemented the forward yet but I'm thinking we're gonna use two max pooling layers so we're gonna have the input two times so twenty-eight becomes 14 and then half again becomes seven so that's gonna be the end of the end and the linear and then here we're gonna you just make the number of classes so define forward yeah so X is f dot relu set of com1 of X and then X is self dot pool of X and then again we're going to use this just come to and again self that pool of X and we can use reuse the same pooling and then we're gonna reshape X because we needed to remember this doesn't actually it's still a three dimensional tensor or I guess I four dimensional tensor if you including mini-batches we want to reshape it so we keep them in a batch the number of examples that we send in and then just minus one and yeah and then we run the self dot FC one of X and return X right so this is our CN n what we can do as we did before let's see do X model is CN n of yeah just CNN x-- is torched dot random that friend before but one twenty eight twenty eight yeah okay yeah so we need a comma here and a comma here yeah that R and I think yeah okay great so then we call the model so print model of X dot shape yeah so same kind of basic check that we did in the last video do the same here for the convolutional just to make sure that okay we've implemented it at least it passes the basic check so after that we can reuse a lot of this the code that we did before except the input size now we use I guess in channels instead and let's see yeah we're gonna call CNN instead we actually don't have to call anything since we we set them default here so we can just just do it like this and then remember that it's already in the correct shape so we don't we don't want to flatten it like we do here that's actually what we do in this part of the network so we can just remove this part the rest stays exactly the same we still want to do the forward the backward the great intent step and then similarly here remove the reshape part and I think that should be it with ultra network for five epochs and I'll come back to you when we get the result all right so we got the result and on the training data disc at 98.5 58% and on the test data at 98 point 36 all right no terrible good so yeah this is how you code a simple CNN if you have any questions write them in the comment and thank you so much for watching the video

Original Description

A walkthrough of how to code a convolutional neural network (CNN) in the Pytorch-framework using MNIST dataset. Explaining it step by step and building the basic architecture of the CNN. ❤️ Support the channel ❤️ https://www.youtube.com/channel/UCkzW5JSFwvKRjXABI-UTAkQ/join Paid Courses I recommend for learning (affiliate links, no extra cost for you): ⭐ Machine Learning Specialization https://bit.ly/3hjTBBt ⭐ Deep Learning Specialization https://bit.ly/3YcUkoI 📘 MLOps Specialization http://bit.ly/3wibaWy 📘 GAN Specialization https://bit.ly/3FmnZDl 📘 NLP Specialization http://bit.ly/3GXoQuP ✨ Free Resources that are great: NLP: https://web.stanford.edu/class/cs224n/ CV: http://cs231n.stanford.edu/ Deployment: https://fullstackdeeplearning.com/ FastAI: https://www.fast.ai/ 💻 My Deep Learning Setup and Recording Setup: https://www.amazon.com/shop/aladdinpersson GitHub Repository: https://github.com/aladdinpersson/Machine-Learning-Collection ✅ One-Time Donations: Paypal: https://bit.ly/3buoRYH ▶️ You Can Connect with me on: Twitter - https://twitter.com/aladdinpersson LinkedIn - https://www.linkedin.com/in/aladdin-persson-a95384153/ Github - https://github.com/aladdinpersson
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Aladdin Persson · Aladdin Persson · 31 of 60

1 computeCost.m Linear Regression Cost Function - Machine Learning
computeCost.m Linear Regression Cost Function - Machine Learning
Aladdin Persson
2 gradientDescent.m Gradient Descent Implementation -  Machine Learning
gradientDescent.m Gradient Descent Implementation - Machine Learning
Aladdin Persson
3 Neural Network from scratch - Part 1 (Standard Notation)
Neural Network from scratch - Part 1 (Standard Notation)
Aladdin Persson
4 Neural Network from scratch - Part 2 (Forward Propagation)
Neural Network from scratch - Part 2 (Forward Propagation)
Aladdin Persson
5 Neural Network from scratch - Part 3 (Backward Propagation)
Neural Network from scratch - Part 3 (Backward Propagation)
Aladdin Persson
6 Neural Network from scratch - Part 4 (With Python)
Neural Network from scratch - Part 4 (With Python)
Aladdin Persson
7 sigmoid.m - Programming Assignment 2 Machine Learning
sigmoid.m - Programming Assignment 2 Machine Learning
Aladdin Persson
8 costFunction.m - Programming Assignment 2 Machine Learning
costFunction.m - Programming Assignment 2 Machine Learning
Aladdin Persson
9 predict.m - Programming Assignment 2 Machine Learning
predict.m - Programming Assignment 2 Machine Learning
Aladdin Persson
10 costFunctionReg.m - Programming Assignment 2 Machine Learning
costFunctionReg.m - Programming Assignment 2 Machine Learning
Aladdin Persson
11 lrCostFunction.m - Programming Assignment 3 Machine Learning
lrCostFunction.m - Programming Assignment 3 Machine Learning
Aladdin Persson
12 oneVsAll.m - Programming Assignment 3 Machine Learning
oneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
13 predictOneVsAll.m - Programming Assignment 3 Machine Learning
predictOneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
14 predict.m - Programming Assignment 3 Machine Learning
predict.m - Programming Assignment 3 Machine Learning
Aladdin Persson
15 Caesar Cipher Encryption and Decryption with example
Caesar Cipher Encryption and Decryption with example
Aladdin Persson
16 Cryptography: Caesar Cipher Python
Cryptography: Caesar Cipher Python
Aladdin Persson
17 Vigenere Cipher Explained (with Example)
Vigenere Cipher Explained (with Example)
Aladdin Persson
18 Cryptography: Vigenere Cipher Python
Cryptography: Vigenere Cipher Python
Aladdin Persson
19 Hill Cipher Explained (with Example)
Hill Cipher Explained (with Example)
Aladdin Persson
20 Cryptography: Hill Cipher Python
Cryptography: Hill Cipher Python
Aladdin Persson
21 Interval Scheduling Greedy Algorithm: Python
Interval Scheduling Greedy Algorithm: Python
Aladdin Persson
22 Weighted Interval Scheduling Algorithm Explained
Weighted Interval Scheduling Algorithm Explained
Aladdin Persson
23 Weighted Interval Scheduling Python Code
Weighted Interval Scheduling Python Code
Aladdin Persson
24 Sequence Alignment | Needleman Wunsch Algorithm
Sequence Alignment | Needleman Wunsch Algorithm
Aladdin Persson
25 Sequence Alignment | Needleman Wunsch in Python
Sequence Alignment | Needleman Wunsch in Python
Aladdin Persson
26 Codility BinaryGap Python
Codility BinaryGap Python
Aladdin Persson
27 Codility CyclicRotation Python
Codility CyclicRotation Python
Aladdin Persson
28 Derivation Linear Regression with Gradient Descent
Derivation Linear Regression with Gradient Descent
Aladdin Persson
29 Linear Regression Gradient Descent From Scratch in Python
Linear Regression Gradient Descent From Scratch in Python
Aladdin Persson
30 Pytorch Neural Network example
Pytorch Neural Network example
Aladdin Persson
Pytorch CNN example (Convolutional Neural Network)
Pytorch CNN example (Convolutional Neural Network)
Aladdin Persson
32 Pytorch LeNet implementation from scratch
Pytorch LeNet implementation from scratch
Aladdin Persson
33 Pytorch VGG implementation from scratch
Pytorch VGG implementation from scratch
Aladdin Persson
34 Pytorch GoogLeNet / InceptionNet implementation from scratch
Pytorch GoogLeNet / InceptionNet implementation from scratch
Aladdin Persson
35 How to save and load models in Pytorch
How to save and load models in Pytorch
Aladdin Persson
36 How to build custom Datasets for Images in Pytorch
How to build custom Datasets for Images in Pytorch
Aladdin Persson
37 Pytorch Transfer Learning and Fine Tuning Tutorial
Pytorch Transfer Learning and Fine Tuning Tutorial
Aladdin Persson
38 Pytorch Data Augmentation using Torchvision
Pytorch Data Augmentation using Torchvision
Aladdin Persson
39 Pytorch Quick Tip: Weight Initialization
Pytorch Quick Tip: Weight Initialization
Aladdin Persson
40 Pytorch Quick Tip: Using a Learning Rate Scheduler
Pytorch Quick Tip: Using a Learning Rate Scheduler
Aladdin Persson
41 Pytorch ResNet implementation from Scratch
Pytorch ResNet implementation from Scratch
Aladdin Persson
42 Pytorch TensorBoard Tutorial
Pytorch TensorBoard Tutorial
Aladdin Persson
43 Pytorch DCGAN Tutorial (See description for updated video)
Pytorch DCGAN Tutorial (See description for updated video)
Aladdin Persson
44 Naive Bayes from Scratch - Machine Learning Python
Naive Bayes from Scratch - Machine Learning Python
Aladdin Persson
45 Spam Classifier using Naive Bayes in Python
Spam Classifier using Naive Bayes in Python
Aladdin Persson
46 K-Nearest Neighbor from scratch - Machine Learning Python
K-Nearest Neighbor from scratch - Machine Learning Python
Aladdin Persson
47 Linear Regression Normal Equation Python
Linear Regression Normal Equation Python
Aladdin Persson
48 SVM from Scratch - Machine Learning Python (Support Vector Machine)
SVM from Scratch - Machine Learning Python (Support Vector Machine)
Aladdin Persson
49 Neural Network from Scratch - Machine Learning Python
Neural Network from Scratch - Machine Learning Python
Aladdin Persson
50 Pytorch RNN example (Recurrent Neural Network)
Pytorch RNN example (Recurrent Neural Network)
Aladdin Persson
51 Pytorch Bidirectional LSTM example
Pytorch Bidirectional LSTM example
Aladdin Persson
52 Pytorch Text Generator with character level LSTM
Pytorch Text Generator with character level LSTM
Aladdin Persson
53 Logistic Regression from Scratch - Machine Learning Python
Logistic Regression from Scratch - Machine Learning Python
Aladdin Persson
54 K-Means Clustering from Scratch - Machine Learning Python
K-Means Clustering from Scratch - Machine Learning Python
Aladdin Persson
55 Pytorch Torchtext Tutorial 1: Custom Datasets and loading JSON/CSV/TSV files
Pytorch Torchtext Tutorial 1: Custom Datasets and loading JSON/CSV/TSV files
Aladdin Persson
56 Pytorch Torchtext Tutorial 2: Built in Datasets with Example
Pytorch Torchtext Tutorial 2: Built in Datasets with Example
Aladdin Persson
57 Pytorch Torchtext Tutorial 3: From Textfiles to Dataset
Pytorch Torchtext Tutorial 3: From Textfiles to Dataset
Aladdin Persson
58 Paper Review: Sequence to Sequence Learning with Neural Networks
Paper Review: Sequence to Sequence Learning with Neural Networks
Aladdin Persson
59 Pytorch Seq2Seq Tutorial for Machine Translation
Pytorch Seq2Seq Tutorial for Machine Translation
Aladdin Persson
60 Pytorch Seq2Seq with Attention for Machine Translation
Pytorch Seq2Seq with Attention for Machine Translation
Aladdin Persson

This video teaches how to build and train a CNN model in PyTorch using the MNIST dataset. It covers the implementation of convolutional and max pooling layers, and how to evaluate the model's performance.

Key Takeaways
  1. Import necessary libraries and load the MNIST dataset
  2. Define the CNN model architecture
  3. Implement convolutional and max pooling layers
  4. Add fully connected layers
  5. Define the forward pass
  6. Initialize the model and train it
  7. Evaluate the model's performance on the training and test data
💡 The video shows how to use PyTorch to build and train a CNN model, and how to evaluate its performance on image classification tasks.

Related Reads

📰
I Found the Neural Network I Built in Class 9 — Here’s What Happened When I Tried to Run It Again
Revisiting a 4-year-old neural network project for handwritten digit recognition using a convolutional neural network and analyzing its performance
Medium · Deep Learning
📰
Introduction to Deep Learning and Neural Networks: From Human Brain to Artificial Intelligence
Learn how biological neurons inspired artificial neural networks and deep learning, transforming the AI landscape
Medium · Deep Learning
📰
Want to get started with deep learning
Get started with deep learning by leveraging resources like Andrew Karpathy's playlist and frameworks such as TensorFlow or PyTorch
Reddit r/deeplearning
📰
Building a Deepfake Detector From Scratch — What Nobody Tells You
Learn to build a deepfake detector from scratch and understand the challenges involved in detecting AI-generated fake media
Medium · Deep Learning
Up next
Image Classification with ml5.js
The Coding Train
Watch →