Pytorch CNN example (Convolutional Neural Network)
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
▶
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
computeCost.m Linear Regression Cost Function - Machine Learning
Aladdin Persson
gradientDescent.m Gradient Descent Implementation - Machine Learning
Aladdin Persson
Neural Network from scratch - Part 1 (Standard Notation)
Aladdin Persson
Neural Network from scratch - Part 2 (Forward Propagation)
Aladdin Persson
Neural Network from scratch - Part 3 (Backward Propagation)
Aladdin Persson
Neural Network from scratch - Part 4 (With Python)
Aladdin Persson
sigmoid.m - Programming Assignment 2 Machine Learning
Aladdin Persson
costFunction.m - Programming Assignment 2 Machine Learning
Aladdin Persson
predict.m - Programming Assignment 2 Machine Learning
Aladdin Persson
costFunctionReg.m - Programming Assignment 2 Machine Learning
Aladdin Persson
lrCostFunction.m - Programming Assignment 3 Machine Learning
Aladdin Persson
oneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
predictOneVsAll.m - Programming Assignment 3 Machine Learning
Aladdin Persson
predict.m - Programming Assignment 3 Machine Learning
Aladdin Persson
Caesar Cipher Encryption and Decryption with example
Aladdin Persson
Cryptography: Caesar Cipher Python
Aladdin Persson
Vigenere Cipher Explained (with Example)
Aladdin Persson
Cryptography: Vigenere Cipher Python
Aladdin Persson
Hill Cipher Explained (with Example)
Aladdin Persson
Cryptography: Hill Cipher Python
Aladdin Persson
Interval Scheduling Greedy Algorithm: Python
Aladdin Persson
Weighted Interval Scheduling Algorithm Explained
Aladdin Persson
Weighted Interval Scheduling Python Code
Aladdin Persson
Sequence Alignment | Needleman Wunsch Algorithm
Aladdin Persson
Sequence Alignment | Needleman Wunsch in Python
Aladdin Persson
Codility BinaryGap Python
Aladdin Persson
Codility CyclicRotation Python
Aladdin Persson
Derivation Linear Regression with Gradient Descent
Aladdin Persson
Linear Regression Gradient Descent From Scratch in Python
Aladdin Persson
Pytorch Neural Network example
Aladdin Persson
Pytorch CNN example (Convolutional Neural Network)
Aladdin Persson
Pytorch LeNet implementation from scratch
Aladdin Persson
Pytorch VGG implementation from scratch
Aladdin Persson
Pytorch GoogLeNet / InceptionNet implementation from scratch
Aladdin Persson
How to save and load models in Pytorch
Aladdin Persson
How to build custom Datasets for Images in Pytorch
Aladdin Persson
Pytorch Transfer Learning and Fine Tuning Tutorial
Aladdin Persson
Pytorch Data Augmentation using Torchvision
Aladdin Persson
Pytorch Quick Tip: Weight Initialization
Aladdin Persson
Pytorch Quick Tip: Using a Learning Rate Scheduler
Aladdin Persson
Pytorch ResNet implementation from Scratch
Aladdin Persson
Pytorch TensorBoard Tutorial
Aladdin Persson
Pytorch DCGAN Tutorial (See description for updated video)
Aladdin Persson
Naive Bayes from Scratch - Machine Learning Python
Aladdin Persson
Spam Classifier using Naive Bayes in Python
Aladdin Persson
K-Nearest Neighbor from scratch - Machine Learning Python
Aladdin Persson
Linear Regression Normal Equation Python
Aladdin Persson
SVM from Scratch - Machine Learning Python (Support Vector Machine)
Aladdin Persson
Neural Network from Scratch - Machine Learning Python
Aladdin Persson
Pytorch RNN example (Recurrent Neural Network)
Aladdin Persson
Pytorch Bidirectional LSTM example
Aladdin Persson
Pytorch Text Generator with character level LSTM
Aladdin Persson
Logistic Regression from Scratch - Machine Learning Python
Aladdin Persson
K-Means Clustering from Scratch - Machine Learning Python
Aladdin Persson
Pytorch Torchtext Tutorial 1: Custom Datasets and loading JSON/CSV/TSV files
Aladdin Persson
Pytorch Torchtext Tutorial 2: Built in Datasets with Example
Aladdin Persson
Pytorch Torchtext Tutorial 3: From Textfiles to Dataset
Aladdin Persson
Paper Review: Sequence to Sequence Learning with Neural Networks
Aladdin Persson
Pytorch Seq2Seq Tutorial for Machine Translation
Aladdin Persson
Pytorch Seq2Seq with Attention for Machine Translation
Aladdin Persson
More on: CV Basics
View skill →Related Reads
📰
📰
📰
📰
I Found the Neural Network I Built in Class 9 — Here’s What Happened When I Tried to Run It Again
Medium · Deep Learning
Introduction to Deep Learning and Neural Networks: From Human Brain to Artificial Intelligence
Medium · Deep Learning
Want to get started with deep learning
Reddit r/deeplearning
Building a Deepfake Detector From Scratch — What Nobody Tells You
Medium · Deep Learning
🎓
Tutor Explanation
DeepCamp AI