PyTorch Tutorial 13 - Feed-Forward Neural Network

Patrick Loeber · Beginner ·🧬 Deep Learning ·6y ago

Key Takeaways

This video tutorial demonstrates how to implement a multilayer neural network using PyTorch, covering topics such as data preparation, model definition, training, and evaluation on the MNIST dataset. It utilizes tools like PyTorch, TorchVision, and Matplotlib for tasks such as data transformation, model building, and plotting.

Full Transcript

hi everybody welcome to a new PI torch tutorial today we will implement our first multi-layer neural network that can do digit classification based on the famous end this data set in this tutorial we put all the things from the last tutorials together so we use the data loader to load our data set we apply a transform to the data set then we will implement our neural net with input layer hidden layer and output layer and we will also apply activation functions then we set up the loss and the optimizer and implement the training loop that can use batch training and finally we evaluate our model and calculate the accuracy and additionally we will make sure that our whole code can also run on the GPU if we have GPU support so let's start and first of all we import the things we need so we import torch then we import torch dot and n s and n then we import torch vision for the data sets and we import torch vision dot transforms s transforms and we also import matplotlib pipe lot SPL T to show you some data later and then first of all we do the device configuration so device config and for this we create a device by saying device equals torch dot device and this is the name CUDA if we have GPU support so if torch dot CUDA dot is available and if it is not available so else we call our device simply CPU and then later we have to push our tensors to the device and this will guarantee that it will run on the GPU if this is supported so yeah so let's define some hyper parameters and here let's define the input size and this is 784 because later we see that our images have to size 28 by 28 and then we will flatten this array to be a 1d tour 10 ZOA and 28 times 28 is 784 so that's why our input size has to be 784 then let's define a hidden size and here I will say this is 100 you can also try out different sizes here and the number of classes and this has to be 10 because we have 10 different classes we have 2 digits from 0 to 9 then let's define the number of epochs and here I will simply say 2 so that a training doesn't take too long but you can set this to a higher value then we define the batch size here and this is let's say 100 and let's also define the learning rate here by saying learning rate equals point 0 0 1 and now let's import the famous M this data so you can have that from the PI torch library by saying training data set equals and here we use torch vision dot data sets dot M nist and this will have to have the route where it has to be stored so root equals and here this should be in the same folder so dot and then it should create a folder called data and then we say train equals true so this is our training data set and then we say we apply a transform right away so we say transform equals transforms dot to tenza so we convert this to a tenza here and then we also say download equals true so it should be downloaded if it is not available already then let's copy this and do the same thing with our dataset and here we have to say train equals false and we also don't have to download this anymore so now let's continue and create the data loaders by same train loader equals and here we get this from Torche dot utils dot data dot data loader and then it will have to have the data set by saying data set equals and here it gets the training data set so train data set then we have to specify the batch size so this is equal to the batch size and then we also have to say or we can say shuffle equals true so this is pretty good for training and then we copy this again and do the same thing for our test loader so test loader equals it gets the test data set and we can say shuffle equals false because it doesn't matter for the evaluation and now let's have a look at one batch of this data by saying examples equals and then we converted to a error object bitter off the drain loader and then we can call the next method and unpack this into samples and into labels by saying this equals examples dot next and now let's print the size of these so let's print samples dot shape and also print print the labels dot shape and now let's save this and run this so let's call Python feedforward dot pi to see if this is working so far and yes here we have the size of the sample so this is 100 by 1 by 28 by 28 and this is because our batch size is 1 red so we have 100 samples in our batch then the one is because we only have one channel so we don't have any colored channels here so only one channel and this is our actual image array so 28 by 28 as I said in the beginning and our label us is only a tensor of size 100 so for each class label we have one value here so yeah this is our some example data and now let's also plot this here to see how this is looking so for I in range 6 and here we use matplotlib so I call PLT dot subplot of with two rows and three columns and the index I plus 1 and then I can say PLT dot m show and here I want to show the actual data so samples of I and then of 0 because we want to access the first channel and then I will also give this a column map so see map equals gray and then I say PLT dot show and let's save this and run this again and here we have a look at the data so these are some example handwritten digits and now we want to classify these digits so for this we want to set up a fully connected neural network with one hidden layer so let's do this so let's comment this out again and now let's create a class neural net and this has to be derived from n n dot module and now we have to define the init and the forward method so the init method so this will get self and then it will has to have the input size then the hidden size and then the output size so the output size is the number of classes and here first we want to call the super in it so super of neural nets and self and dot in it self dot in it and then we create our layers so first we want to have a linear layer by saying self dot l1 equals n n dot Linea and this will have has the input size as input and the output size is the hidden size then after the first layer we want to apply a activation function and here I simply use the famous riilu activation so self dot riilu equals n n dot re Lu and then at the end we have another linear layer so self dot l2 equals n n dot linear and now we have to be careful so the input size here is the hidden size and the output size is the number of classes and now let's define the forward method so this will have self and one sample X and now we apply all these layers so we say out equals and now we use the first layer l1 which gets the sample X and then the next out is self dot riilu now use the activation function which will get the previous output here and the last out equals self dot l2 and out so this will apply the second linear function and now we have to be careful again because here at the end we don't want an activation function so we don't apply the softmax here as usual in in multi class classification problems because in a second we will see that we will use the cross entropy loss and this will apply the softmax for us so no softmax here so we simply say return out so this is our whole model and then we can create it here by saying model equals neural net and this will get the input size then the hidden size and the number of classes so yeah now we have the model so now let's come create the loss and the optimizer so here we say criterion equals n n dot cross and for P loss and this will apply the softmax for us so that's why we don't want this here so be very careful about this and now let's create our optimizer as well by saying tour optimizer equals torch dot optim dot now let's use the atom optimizer here and this has to get the parameters and here we can use model dot parameters and it also has to get the learning rate L R equals learning rate now we have the loss and the optimizer and now we can do our training loop so training loop now and for this let's first define the number of total steps so n total steps equals and this is the length of the training loader so now we can do the typical loop so we say for Deepak in range num be pox and so this will loop over the epochs and now we loop over all the batches so here we say for I and then again we unpack this so we say images images and labels and then we iterate over a number right over our train loader so the enumerate function will give us the actual index and then the data and the data here is the tuple of the images and the labels and now we have to reshape our images first because if we have a look at the shape then we see that this is 100 by 1 by 28 by 28 as I showed you in the beginning and now we said our input size is 784 so our images tensor needs the size 100 PI and 784 a second dimension so the number of spatulas first so let's reshape our our tens of first so we can do this by saying images equals images dot reshape and here we put in minus 1 as the first dimension so then tensor can find out this automatically for us and here as second dimension we want to have 28 by 28 and then we also call to device so we will push this to the GPU if it is available and we have also have to push it it to the push the labels to the device so labels equals labels to device and now let's do the forward pass so first we do the forward pass and afterwards the backward pass so the forward pass we simply say outputs equals model and this will get the images and then we calculate the loss by saying equals and then here we call our criterion and this will get the predicted outputs and the actual labels so this is the forward pass and then in the backward pass the first thing we want to do is call optimizer dot 0 grat to empty the values in the gradient attribute and then we can do the next step by saying loss dot backward so this will do the back propagation and now we can call optimizer dot step so this will do an update step and update the parameters for us and now let's also print some print the loss so let's say if I plus 1 modulo 100 equals equals zero so every 100th step we want to print some information so let's print the current epoch so by saying this is epoch epoch plus 1 and then we want to print all the epochs so number of epochs then let's also print the current step by saying step and this is I plus 1 and then the total number of steps by saying n total steps and we also want to print the loss by saying loss equals loss dot item and let's also say we only want to print four decimal values so yeah now we are done with the training so this is the whole training loop and now let's do the testing and the evaluation and for this we don't want to compute the gradients for all the steps we do so we want to wrap this in a with torch dot no rat statement and then first we say the number of correct predictions equals zero and the number of samples equals zero in the beginning and then we loop over all the batches in the test samples so we say for images and labels in and here we can simply say in test loader and then again we have to reshape this so like we did here so images and labels we want to reshape this and put it and push it to the device and then let's call let's calculate the predictions by saying outputs equals model so this is our trained model now and this will get the test images here and then let's get the actual predictions by saying underscore and then predictions equals torch dot max of the outputs and along the dimension along the number one so the torch that max function will return the value and the index so we are interested in the actual index so this is the class label so that's why we don't need the first actual value so these are our predictions and now let's say the number of samples plus equals and here we say labels dot shape zero so this will give us the number of samples in the current batch so should be 100 and then we say the number of correct so the correct predictions equals and here we can say predictions equals equals the actual labels and then dot sum and then dot item so for each correct prediction we will add plus one and then of course we have to say plus equals the number of correct values and then when we are done with the loop we calculate the total accuracy by saying AK equals 100 times the number of correct and predictions divided by the number of samples so this is the accuracy in percent and now let's print this so print and we want to print accuracy equals and here we simply say AK and then we are done so now let's save this and clear this and let's run this and hope that everything is working so now our training starts and we should see though that the loss should be increased with every step sometimes it will also increase again but finally it should get lower and lower and now we should be done and testing is very fast so now we see that the accuracy is 94.9 so it worked our first feet forward model is done and yeah I hope you understood everything and you enjoyed this if you liked it please subscribe to the channel and see you next time bye

Original Description

New Tutorial series about Deep Learning with PyTorch! ⭐ Check out Tabnine, the FREE AI-powered code completion tool I use to help me code faster: https://www.tabnine.com/?utm_source=youtube.com&utm_campaign=PythonEngineer * In this part we will implement our first multilayer neural network that can do digit classification based on the famous MNIST dataset. We put all the things from the last tutorials together: - Use the DataLoader to load our dataset and apply a transform to the dataset - Implement a feed-forward neural net with input layer, hidden layer, and output layer - Apply activation functions. - Set up loss and optimizer - Training loop that can use batch training. - Evaluate our model and calculate the accuracy. - Additionally, we will make sure that our whole code can also run on the gpu if we have gpu support. 📚 Get my FREE NumPy Handbook: https://www.python-engineer.com/numpybook 📓 Notebooks available on Patreon: https://www.patreon.com/patrickloeber ⭐ Join Our Discord : https://discord.gg/FHMg9tKFSN Part 13: Feed-Forward Neural Network If you enjoyed this video, please subscribe to the channel! Official website: https://pytorch.org/ Part 01: https://youtu.be/EMXfZB8FVUA Code for this tutorial series: https://github.com/patrickloeber/pytorchTutorial You can find me here: Website: https://www.python-engineer.com Twitter: https://twitter.com/patloeber GitHub: https://github.com/patrickloeber #Python #DeepLearning #Pytorch ---------------------------------------------------------------------------------------------------------- * This is a sponsored link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Patrick Loeber · Patrick Loeber · 50 of 60

1 Lists in Python - Advanced Python 01 - Programming Tutorial
Lists in Python - Advanced Python 01 - Programming Tutorial
Patrick Loeber
2 Tuples in Python - Advanced Python 02 - Programming Tutorial
Tuples in Python - Advanced Python 02 - Programming Tutorial
Patrick Loeber
3 Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Dictionaries in Python - Advanced Python 03 - Programming Tutorial
Patrick Loeber
4 Sets in Python - Advanced Python 04 - Programming Tutorial
Sets in Python - Advanced Python 04 - Programming Tutorial
Patrick Loeber
5 Strings in Python - Advanced Python 05 - Programming Tutorial
Strings in Python - Advanced Python 05 - Programming Tutorial
Patrick Loeber
6 Collections in Python - Advanced Python 06 - Programming Tutorial
Collections in Python - Advanced Python 06 - Programming Tutorial
Patrick Loeber
7 Itertools in Python - Advanced Python 07 - Programming Tutorial
Itertools in Python - Advanced Python 07 - Programming Tutorial
Patrick Loeber
8 Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Lambda in Python - Advanced Python 08 - Programming Tutorial - Map Filter Reduce
Patrick Loeber
9 Exceptions in Python - Advanced Python 09 - Programming Tutorial
Exceptions in Python - Advanced Python 09 - Programming Tutorial
Patrick Loeber
10 Logging in Python - Advanced Python 10 - Programming Tutorial
Logging in Python - Advanced Python 10 - Programming Tutorial
Patrick Loeber
11 JSON in Python - Advanced Python 11 - Programming Tutorial
JSON in Python - Advanced Python 11 - Programming Tutorial
Patrick Loeber
12 Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Random Numbers in Python - Advanced Python 12 - Programming Tutorial
Patrick Loeber
13 Decorators in Python - Advanced Python 13 - Programming Tutorial
Decorators in Python - Advanced Python 13 - Programming Tutorial
Patrick Loeber
14 Generators in Python - Advanced Python 14 - Programming Tutorial
Generators in Python - Advanced Python 14 - Programming Tutorial
Patrick Loeber
15 Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Threading vs Multiprocessing in Python - Advanced Python 15 - Programming Tutorial
Patrick Loeber
16 Threading in Python - Advanced Python 16 - Programming Tutorial
Threading in Python - Advanced Python 16 - Programming Tutorial
Patrick Loeber
17 Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Multiprocessing in Python - Advanced Python 17 - Programming Tutorial
Patrick Loeber
18 Function arguments in detail - Advanced Python 18 - Programming Tutorial
Function arguments in detail - Advanced Python 18 - Programming Tutorial
Patrick Loeber
19 The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
The asterisk (*) operator in Python - Advanced Python 19 - Programming Tutorial
Patrick Loeber
20 Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Shallow vs Deep Copying in Python - Advanced Python 20 - Programming Tutorial
Patrick Loeber
21 Context Managers in Python - Advanced Python 21 - Programming Tutorial
Context Managers in Python - Advanced Python 21 - Programming Tutorial
Patrick Loeber
22 KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
KNN (K Nearest Neighbors) in Python - Machine Learning From Scratch 01 - Python Tutorial
Patrick Loeber
23 Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Linear Regression in Python - Machine Learning From Scratch 02 - Python Tutorial
Patrick Loeber
24 Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Logistic Regression in Python - Machine Learning From Scratch 03 - Python Tutorial
Patrick Loeber
25 Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Linear and Logistic Regression in 60 lines of Python - Machine Learning From Scratch 04
Patrick Loeber
26 Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Naive Bayes in Python - Machine Learning From Scratch 05 - Python Tutorial
Patrick Loeber
27 Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Perceptron in Python - Machine Learning From Scratch 06 - Python Tutorial
Patrick Loeber
28 SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
SVM (Support Vector Machine) in Python - Machine Learning From Scratch 07 - Python Tutorial
Patrick Loeber
29 Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Decision Tree in Python Part 1/2 - Machine Learning From Scratch 08 - Python Tutorial
Patrick Loeber
30 Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Decision Tree in Python Part 2/2 - Machine Learning From Scratch 09 - Python Tutorial
Patrick Loeber
31 Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Random Forest in Python - Machine Learning From Scratch 10 - Python Tutorial
Patrick Loeber
32 PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
PCA (Principal Component Analysis) in Python - Machine Learning From Scratch 11 - Python Tutorial
Patrick Loeber
33 K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
K-Means Clustering in Python - Machine Learning From Scratch 12 - Python Tutorial
Patrick Loeber
34 Anaconda Tutorial - Installation and Basic Commands
Anaconda Tutorial - Installation and Basic Commands
Patrick Loeber
35 PyTorch Tutorial 01 - Installation
PyTorch Tutorial 01 - Installation
Patrick Loeber
36 PyTorch Tutorial 02 - Tensor Basics
PyTorch Tutorial 02 - Tensor Basics
Patrick Loeber
37 PyTorch Tutorial 03 - Gradient Calculation With Autograd
PyTorch Tutorial 03 - Gradient Calculation With Autograd
Patrick Loeber
38 PyTorch Tutorial 04 - Backpropagation - Theory With Example
PyTorch Tutorial 04 - Backpropagation - Theory With Example
Patrick Loeber
39 PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
PyTorch Tutorial 05 - Gradient Descent with Autograd and Backpropagation
Patrick Loeber
40 PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
PyTorch Tutorial 06 - Training Pipeline: Model, Loss, and Optimizer
Patrick Loeber
41 PyTorch Tutorial 07 - Linear Regression
PyTorch Tutorial 07 - Linear Regression
Patrick Loeber
42 PyTorch Tutorial 08 - Logistic Regression
PyTorch Tutorial 08 - Logistic Regression
Patrick Loeber
43 PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
PyTorch Tutorial 09 - Dataset and DataLoader - Batch Training
Patrick Loeber
44 PyTorch Tutorial 10 - Dataset Transforms
PyTorch Tutorial 10 - Dataset Transforms
Patrick Loeber
45 Download Images With Python Automatically - Python Web Scraping Tutorial
Download Images With Python Automatically - Python Web Scraping Tutorial
Patrick Loeber
46 PyTorch Tutorial 11 - Softmax and Cross Entropy
PyTorch Tutorial 11 - Softmax and Cross Entropy
Patrick Loeber
47 Select Movies with Python - Web Scraping Tutorial
Select Movies with Python - Web Scraping Tutorial
Patrick Loeber
48 PyTorch Tutorial 12 - Activation Functions
PyTorch Tutorial 12 - Activation Functions
Patrick Loeber
49 List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
List Comprehension in Python - A Python Feature You MUST KNOW - Python Tutorial
Patrick Loeber
PyTorch Tutorial 13 - Feed-Forward Neural Network
PyTorch Tutorial 13 - Feed-Forward Neural Network
Patrick Loeber
51 How To Add A Progress Bar In Python With Just One Line - Python Tutorial
How To Add A Progress Bar In Python With Just One Line - Python Tutorial
Patrick Loeber
52 PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
PyTorch Tutorial 14 - Convolutional Neural Network (CNN)
Patrick Loeber
53 The Walrus Operator - New in Python 3.8 - Python Tutorial
The Walrus Operator - New in Python 3.8 - Python Tutorial
Patrick Loeber
54 PyTorch Tutorial 15 - Transfer Learning
PyTorch Tutorial 15 - Transfer Learning
Patrick Loeber
55 YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
YouTube Data API Tutorial with Python - Analyze Channel Statistics - Part 1
Patrick Loeber
56 YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
YouTube Data API Tutorial with Python - Find Channel Videos - Part 2
Patrick Loeber
57 YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
YouTube Data API Tutorial with Python - Get Video Statistics - Part 3
Patrick Loeber
58 YouTube Data API Tutorial with Python - Analyze the Data - Part 4
YouTube Data API Tutorial with Python - Analyze the Data - Part 4
Patrick Loeber
59 AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
AdaBoost in Python - Machine Learning From Scratch 13 - Python Tutorial
Patrick Loeber
60 Ultimate FREE Study Guide for Machine Learning and Deep Learning
Ultimate FREE Study Guide for Machine Learning and Deep Learning
Patrick Loeber

This tutorial teaches how to build and train a multilayer neural network using PyTorch, covering key concepts like data preparation, model definition, and evaluation. By following this tutorial, viewers can learn how to implement a basic neural network and understand the fundamentals of deep learning.

Key Takeaways
  1. Import necessary libraries
  2. Configure device for GPU support
  3. Define hyperparameters
  4. Import MNIST dataset
  5. Apply transforms to data set
  6. Create a batch of test data
  7. Plot the first 6 samples of the batch
  8. Define a neural network class with one hidden layer
  9. Use ReLU activation function for the hidden layer
  10. Define model
💡 The tutorial highlights the importance of proper data preparation, model definition, and evaluation in building an effective neural network, and demonstrates how PyTorch can be used to simplify these tasks.

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 →