Pytorch LeNet implementation from scratch

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

Key Takeaways

The video demonstrates how to implement the LeNet architecture in PyTorch, with modifications to use ReLU instead of tanh and modern weight initialization. The LeNet architecture is a convolutional neural network that consists of multiple convolutional and pooling layers, followed by fully connected layers.

Full Transcript

[Music] in this video I want to go through an old-school convolutional neural network architecture and that comes from a paper by Yonghwa Kuhn and the network is called Lynette the Lynette architecture looks like this and it starts with a 32 by 32 image input and in the in the first layer it uses a 5x5 kernel with a stride of 1 padding of 0 and gets it to 28 by 28 it also uses a our channels of this two v6 and then in the next layer the Linnet architectures is something a little bit unique but essentially it's an average pool layer with a kernel size of 2 and a stride of two essentially having the input size so 28 becomes 14 and then in the next layer it uses the same 5x5 kernel and sraight of one padding of 0 to get it to 10 by 10 and also uses the out channels of of that layer to be 16 then in the next it uses the average pooling layer again to have the input and then it uses the 5x5 kernel again so it uses a 5x5 kernel with a stride of 1 padding of 0 and gets it to 100 and the output channels is 120 so this is a convolution air layer with 120 channels 1 and one by one then it uses a fully connected layer linear layer with 120 to 84 nodes and then again a linear layer with 84 to the 10 outputs I've summarized the Linnet architecture here so we use a 32 by 32 input and this might be a little confusing since the Linnet architecture was was built for the amnesty to set but the Emnes data set has 28 by 28 and I'll reference to the paper but essentially this was improve believed to improve the performance yeah I'm not sure if this is actually true because modern architectures don't use this but that was the reasoning behind it and then we just used a 5x5 kernel average pool 5x5 average pool and another comm 220 channels we flattened it we use a linear so 120 to 84 and then another linear time for the output which is 10 so this is just the reference we can use so let's actually try to build this so we're going to use class Lynnette and we're going to inherit from the NN module to find the innit the first thing we always have to do is call super of vennett and initialize the parent method which is the end of module and one thing is that so Linnet uses 10h and sigmoid activation functions we're gonna use relevance Ted because I mean there's no reason not to use really the only reason was that it wasn't invented at that time and really the goal here is to understand the architecture not to implement every single detail as laocoon did so we're gonna use another weight initialization as well but really yeah so the goal is to understand the the general architecture so we're gonna use self dot relu to be and then dot braille ooh we're going to create the pool layer that we're going to use which is an average pooling and the kernel size will be 2x2 stride will be two by two and essentially so the average pulling is what we use here and use here we can reuse the same pooling because the the average point doesn't have any parameters and then let's initialize the comm layers so the first account layer is going to be so cnn.com 2d with an in channel of one our channels will be six kernel size will be 5x5 stride 1 padding:0 I don't think this is actually necessary to write but just to make everything clear we write it and then com2 we really going to use exactly the same the only thing that's going to differ is the number of in channel since this is the second comm layer we need to match the our channels of this to the in channels here so this is going to be 6 and lacunae uses 16 as our channels here and then it uses another comp with the exact same kernel and stride padding except we need to match again 16 and the out channels here is going to be honored in 20 then we need to have the linear layers so and on top linear 120 84 and then the linear 2 and then that linear of 84 and out for the number of classes which is 10 then we want to do is that we won't define the forward first run it through the comm Blair the first calm then we use the relative activation function and we just call the output of this X and then we're gonna use our the pooling then we're gonna use so basically the same again except we're gonna run it through chef com2 and reuse the pooling and lastly we're going to use again this except com3 then what we want to do is that we want to so the output of this will be yeah so the number of examples that we have by 120 channels by one by one we want to make this to the number of examples by 120 essentially we just want to make these into a single dimension so self dot yeah so X dot reshape and we want to keep the first dimension the same the number of examples and then just minus one then now we can run it through the the linear so X set the linear one of X and just call this X and then use the rel loop now for the last output yeah so for the last output what we're gonna use is just a linear the linear to no activation function on the output layer so and then we're just going to return X and that's essentially the architecture so let's see if we get it to run we do this by creating some just random input and then calling the Lynette and yeah so we can do print model of x-type so what we expect this to be is 64 by 10 because we have 64 images and for each we want to have like the prediction for each of the integer values yeah so if we run this damn it okay so yeah so here this should be calm three yeah great so then we get 64 by 10 now if you want to use this for the M list all you have to do you can follow my last video I did which you uses the Eman estate asset and all you have to do is just pad the images by two and then you can just run it through this architecture if you have any questions then leave them in the comment thank you so much for watching the video and yeah I hope to see you in the next one

Original Description

In this video we go through how to code the LeNet architecture in Pytorch. Note that it isn't exactly identical as I've done some translation to a more modern architecture, like using relu instead of tanh. Paper: http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf ❤️ 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 · 32 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
31 Pytorch CNN example (Convolutional Neural Network)
Pytorch CNN example (Convolutional Neural Network)
Aladdin Persson
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 implement the LeNet architecture in PyTorch, with a focus on understanding the neural network architecture and its components. The viewer will learn how to build a CNN model and implement a neural network architecture from scratch.

Key Takeaways
  1. Define the LeNet architecture
  2. Initialize the convolutional and pooling layers
  3. Implement the fully connected layers
  4. Define the forward pass
  5. Test the model with random input
💡 The LeNet architecture is a classic convolutional neural network that can be modified and improved using modern techniques and tools.

Related AI Lessons

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
Unfolding the Meandering Path: High-Dimensional Invariance and the Flat 2D Plane of Neural…
Learn about high-dimensional invariance and its relation to the flat 2D plane of neural networks, and how to apply these concepts to improve model performance
Medium · Deep Learning
Implementing Neural Style Transfer from Scratch: The Project That Started It All
Learn to implement Neural Style Transfer from scratch and understand its significance in deep learning
Medium · Deep Learning
Up next
Image Classification with ml5.js
The Coding Train
Watch →