Pytorch tutorial for beginners | Pytorch neural network tutorial | Pytorch course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you are new to the world of deep learning and you have heard of something known as P torch and you want to understand more details of it that is what is the purpose of this video let's start the detailed discussion of py torch starting from scratch here are the topics for this video guys we are going to understand what are tensors in pytorch I'm sure you have heard of this term called tensors and it's very important from pytorch point of view okay then we are going to see what are the important modules in pytorch if we want to build a neural network model okay then we are going to see what is device this is a very very important Concept in py torch this makes the torch framework faster and efficient okay then we are going to see how to build a simple model in pytorch I'm going to show you the demo we are going to save the model and use the model just like normal U we do the the process in machine learning and deep learning right and then I'm going to leave you with some next topics and based on your interest I will continue more videos on pyto if I get good number of comments that we want to go deeper into pytorch okay so let's start with what are tensors in pytorch so this looks to be a fancy term just like prompt in gen AI but it is not a fancy thing at all okay tensors are nothing but multi-dimensional numpy array you can think of like that okay so single dimensional also it can be multi-dimensional also it can be so every time somebody says tensors you can simply think like a nump andd array let me show you one example in Notebook how tensors look like so here I am writing what are tensors in P torch so I'm importing something known as torch torch is nothing but the pi torch and then I'm saying import nump as NP okay here I'm creating a data like this so at the moment I create data like this right this this is a nend dimensional data in this case what I do I just pass these data to torch. tensor okay at the moment I pass the data to toss. tensor you can see that when I'm printing xcore data right it says that it is a tensor so tensor is what tensor is nothing but a nend dimensional array in the language of numai okay and just to show more relevance here I can also have a NP array of that data same data that I have created here same list that I have created here I can create M NP array for this pass that in Torch from numpy method and I can get the same tensor as output so what it means is uh I can generate tensors through a numpy array or from a python list and vice versa like for this Al to this I can convert back to numpy andd array okay this is the concept first concept of torch that is tensors now before moving any forward guys let me tell you basics of what are few things that you should know about uh ptor right so what is pytorch it is a popular and efficient framework for deep learning implementations okay neural network implementation deep learning implementation what makes pytorch popular and efficient we will try to understand one such uh factor of pytor becoming so efficient and popular is tensors okay so if you saw right tensor is nothing but a ND AR kind of thing what how this helps is you know deep learning models are more about computation weight optimization gradient descent uh weight updation normalization you know it is more of optimizing a network okay so the mathematical computations play a very important role here and mathematical computations in terms of tensor are faster so if you if you use tensor for doing a mathematical computation and if you add a GPU kind of involment on top of it right then it becomes very very fast hence pytorch is popular and efficient uh framework to build your deep learning model now I want to give one disclaimer here guys regarding pytorch if you don't find yourself comfortable in Python then first become little comfortable in Python I mean to say that if you say that I'm only a packages guy I know how to use pandas numai and I do my job that is not going to work in pyto implementations okay so you have to learn the objectoriented programming Concepts you have to learn how python multiple methods inheritance classes all those things works then only you'll be able to take full advantage of pytorch okay why I am saying you this let me show you with an example and we will parall understand also what what is happening as part of pytorch simple model implementation okay so this was few basic stuff about pytorch now we understood what is tensor and what is pytor at high level let's see what are the important modules as you can see here I'm importing NN so NN means neural network from pytorch okay I'm importing data load loader data loader is nothing but a but a wrapper on top of so if there is a data set right uh in if I put a rapper of data loader on that then it is easier for me to process in batches Shuffle the data take the sample out so it makes my job faster and easier if I use this module okay data sets is basically to pull some data which I'll use now then comes two tensor two tensor what it will do is it will convert your image or ND AR to tensor okay and then obviously met plotly all of you know already so what I'm going to do here is I'm going to import some training data from open data sets so I'm importing a data called fashion Mist this is basically a data set for if you know the M's data set handwriting digit data set right this is about fashion related to that so some fashion items will show this is basically a vision problem computer vision problem so it will show you some images of hoodies sweaters t-shirts boots Etc and model has to identify what is that okay so training data I'm importing here and test data I'm Ting here if you can see now while importing the data you can see that I'm using data sets module which I imported here I told you data sets is to pull data from various repositories this is the name of my data root is data train is true means it will give me training data train is false means it will give me test data okay and then I'm converting this to tensor because I want the tensors of that because the whole purpose is to make it faster once I get this data here you can see it is downloaded okay just to show you here these are the maps Okay labels so t-shirt trouser pullover dress Cod sandal shirt sneaker bag and ankle boots okay and this is how your data looks like so as you can see coat the image of coat dress image of dress sandal sneaker sneaker trer trer sandal trer so this is basically your data okay one image one label one image one label one image one label now one thing about this data is it is 28 cross 28 pixel data okay so what is the size of each image 28 cross 28 and here what I'm doing one class so this is cod class this is dress class this is sandal class okay so these are relevant information because I'll be defining the model now so this is about your data what is the independent feature your pictures what is the size of your picture 28 cross 28 what is the target level one I mean size and what is the target level these nine 10 categories okay let's come down here and try to see what I'm doing is if you remember I told you data loader is a module that helps me to create batches of my data okay here data loader so I will come here and I will from my data sets what I imported right I will create my Trend data and test data using data loaders this is what I'm doing here and my batch size is 64 which means 64 um items will go for training in one shot 64 records okay now one important thing to understand here is device Concept in pyto okay so pytor you can run on Cuda or you can run on GPU CPU whatever okay this is uh Google Cloud um I'm sorry collab collab uh I'm using this right so I can make it like G I can enable GPU but for now I am using in CPU only okay so whatever you do right that becomes your device whether it it is GPU or CPU or Coda that becomes your device like this okay and why why it is important to understand the concept of device is whatever Trend data or test data or model you will be using right all that you will put in the device then only it will work otherwise it will not work suppose I'm using GPU then I need to put my data plus model plus artifacts everything in that GPU machine otherwise it will not work okay so here I know what is my device my device is CPU now most important part of this video guys is two very very important Concepts try to understand for building a simple neural network in pytorch okay so this code is very important please pay attention here please pay attention from here to here okay so as I was telling you you need to know some python concepts for implementing py Orin uh in a efficient way okay so here you can see this is a class neural network that is getting created from nn. module as it parent class now this is the concept of inheritance in Python okay so what happens in inheritance is there is one parent class and then there is a child class okay so from this nn. module this neural network class is getting created what is happening here is there are two methods here one isore init method which is normally a Constructor in a class here also one Constructor okay and other is forward method so what is Happening Here is through init method right when I Define the object of that class which I'm doing right here when I'm defining object of the class right this Constructor will get will get called and the structure of my network will get defined pay attention here guys what I'm doing is I defining the structure of my neural network see here I'm taking a flatten layer then I'm stacking few layers here see here Ru stack NN do sequential I'm saying 28 cross 28 my input will come I told you in the beginning 28 cross 28 is size of all my images okay the images which you see here 28 cross 28 is the sizes of that okay pixel wise I'm saying then 512 is what I'm taking as output 512 512 and in the end as I have 10 classes right so I'm giving in the final layer I'm giving 10 sides okay so here this is my um structure of my neural network that is defined inside where inside Constructor method that is init method okay and this forward method right these are fixed method remember you cannot change the name of this okay this forward method what it does is it tells your neural network like how to do the forward propagation so if you see here it is very simple it just tells that take everything that is in linear stack and you know keep running that and return the logics so it will just give you um Logics which means your your forward propagation will happen from here okay and here what is happening is as I told you I'm creating an object of that class and I'm saying this is my device pass it to that device suppose I want to pass my model to a GPU for training so this line that I'm highlighting here will do that okay and then I'm printing the model so as I was telling you from device I have not enabled GPU so CPU will work for me so my device will be CPU and as I was telling you this is the structure of my neural network so sequential input features output features input features output features input features output features okay so this is the network that I have defined now I have the network defined I have the train and test data what else I need remember few days back I had created a video on how to learn something in deep learning and machine learning by doing it without packages okay so let's think in those directions without packages what else do you need you have the neural network structure you have the train and test data now you need a Optimizer right because you want to optimize the weights and you need to call train and test function okay let's see how to do those things so here I'm defining a loss function and I'm defining a Optimizer very simple neural network cross entropy loss is the loss I'm using and torch. Optimizer SGD stochastic gradient descent is what I'm using from the above model I have taken the parameter learning rate I have taken a very small value here okay so this is about your loss function and Optimizer you are defining now the last piece of the puzzle you just need a train function and test function so in this train function I am saying go to my neural network and take the data loader load it to the device where I want to do the processing this is my prediction this is my loss and then just optimize the loss okay that is the entire thing that is happening here in back propagation okay now what is happening here this is for train function and this part is for test function same thing if you see the difference here guys right I'm calling here model. train and I'm calling here model. evail model. evail means I want to test I'm not training okay don't worry I'll I'll give you this notebook so it will be easier for you to play around with this okay so in first one what I'm saying run my train function in second one I'm saying run my test function what I'm going to do now is I'm going to call my training so see here Epoch is equal to 5 for I in range Epoch T train train data loader which we had created in the beginning test test data order okay and then five EPO it will run and model will get trained okay I know it is getting little complex because you know we don't do these things by our hands we do through packages but there is a purpose why I am showing you this particular notebook only because I want you to think how the network works inside okay you need a loss function you need a Optimizer you need a train function you need a test function all these things you have written you trained your model here I have run everything just to save some time and here is the model and if I want to save the model in torch then I will say torch. Save model. State dict model. pth and then it will save it to this directory and then what I can do is I can simply load the model and load State dict okay so from the device I'm loading the model and I'm just loading the state dict okay so uh I'm here what I'm doing I'm just passing the classes and I'm predicting for example this is my X and this is my y this is my test data input test data output and then I'm running it here so actual is ankle boot and predicted is also ankle boot okay for this particular number entry in the data fine so what I have done here I have followed all the processes the only thing is five major things that you need to understand from this video what are tensors and dimensional arrays that are used in pytor for faster processing what are important modules NN is the module through which you will do your uh model building data sets you will use for data and two tensor Etc you will use for tensor okay this is about this data so nothing special breaking the data in train and test device is important remember you have to put everything in the device to work it smoothly this part is very important forward tells you how to do the forward propagation and init method tells you the structure of your network okay once these things are ready to Define your loss function Optimizer train and test however you want to Define and then you call this method okay so your your you know prediction can be your model can be saved and your state dict can be loaded okay and then prediction can be made from here so this is a very simple implementation of what is p torch now there are many things to it before that one disclaimer guys I told you I'll give you this notebook this notebook is already available here I have taken this from the py toor official documentation so cut C to py torch for providing this beautiful note which I could use as it is because if I will go and create a new one I'll I'll be writing the same things so better to take it from here okay and this is just the tip of the iceberg in pytorch we can do many things see this link here guys you can go and read and here what I can do is I can go into more details of these okay if you guys are interested please drop me a comment saying you want to go in more details of py understand how to tune the model how to optimize Etc we can do more sessions on this okay please give me a thumbs up guys if you like this video I'll see you all in the next video wherever you are stay safe and take care
Info
Channel: Unfold Data Science
Views: 1,734
Rating: undefined out of 5
Keywords: Pytorch tutorial for beginners, Pytorch neural network tutorial, Pytorch course, Pytorch vs tensorflow, Pytorch lightning tutorial, Pytorch crash course, Pytorch vs tensorflow vs Keras, Pytorch lstm, Pytorch dataloader, Pytorch tutorial for dummies, pytorch vs tensorflow, pytorch for deep learning, unfold data science pytorch, pytorch for CNN, pytorch for dummies, pytorch vs kearas, pytorch transformers, pytorch tensors, pytorch training
Id: KYFXNncIC3c
Channel Id: undefined
Length: 18min 19sec (1099 seconds)
Published: Wed Nov 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.