What is PyTorch | PyTorch Tutorial For Beginners | PyTorch Tutorial | Intellipaat

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] pie torch is an open source machine learning library for python it is used for applications such as natural language processing and computer vision and it was originally developed by meta Ai pytorch redesigns and implements stores in Python while sharing the same core c libraries for the backend code pytos developers during this back-end code to run python efficiently they also kept the GPU based Hardware acceleration and the extensibility features that made Lua based torch pytots provides several modules such as Auto grad Etc it contains many other useful submodules such as data loading utilities and distributed training functions so now let's move to the agenda first we will jump to the introduction of pytorch after understanding what pi torches will see the installation tensor Basics and the back propagation hello everyone and welcome to Intel event before we move ahead with the video please make sure that you subscribe to our YouTube channel and hit the Bell icon for regular updates so let me explain you what is exactly pytorch pytorch is an open source machine learning library which is used for applications such as computer vision and natural language processing by touch was originally developed by meta AI along with that Python's redesigns and implements thoughts in Python while sharing the same course C libraries for the backend code now let's see the features of Pi torch pytorch is a simple intuitive and stable API also it has very high performance on GPU with Cuda along with that you can use pytorch for back propagation and it is extensively used for the neural networks building blocks so these are the features of pytorch so after understanding what is try torch now let's see its installation so now moving to the installation part we will be using here Google collab let me open the Google collab and show you how you can install pytorch so I will go simply on the web browser that is nothing but a Google Chrome and I'll just write here Google collab and I will click on this and then I will just click on the first one that is welcome to collabority so this is a link where I will click and I'll just go and click on the option new notebook let me rename this I'll just write here simply Pi torch underscore tutorial now we will see the installation of how to install pytorch so I'll simply write here import torch so this is the way to install pytorch so here you can see that the command has been executed so we have successfully installed pytorch so after installing Pi torch we will see the tensor Basics the reason behind that pytos defines a class that is basically called tensor which is used to store and operate on homogeneous multi-dimensional rectangular arrays of numbers it is similar to the numpy arrays but can also be operated on Coda capable and PDF GPU so now we will perform some basic operations on pytorch in Google collab so as I already told you that Pi torch will give you a tensor class right so now let's see different functions that we can use using pytorch the first function that I will be using is nothing but a Rand function so here I have already imported pytorch that is nothing but a torch Library so if I'm writing here torch dot Rand and here if I'm writing here 2 comma 2 so while executing it you can see that I am getting a two dimensional tensor right so here you can see that we are having two rows and two columns all right now the next function that I'll be using here is empty function empty function returns a tensor that is filled with uninitialized data so if I am writing here let's suppose torch dot empty and let me put here x and inside this empty function let's suppose if I am giving any value random value if I'm writing here 4 and if I'm executing it and let me print this so I'll just simply write here print function and inside this print function I will just write here x and on execution you can see that I am getting a value or data that is uninitialized and here I have given 4 right so we are getting a 4 values here now moving next if I'm writing you torch dot empty and here if I am writing you 2 comma 3 let me again assign it with the X variable and once again I'll just bring to you print X so here you can see that on execution we'll get the output as two dimensional tensor which contains two rows that is 1 and 2 along with the three columns and here if you see the values or data this is uninitialized right so that's what the empty function gave you so now we have already seen the one dimensional tensor along with the two dimensional tensor now so if you want to create the three dimensional tensor so now let's see with the help of empty function how you can create so I will simply write here tors.empty and here I will just write here 2 comma 2 comma 3. and on execution you can see that these are nothing but the two the first two I got here and then two rows right and three columns so this is nothing but a 3D tensor right so let me comment it down I'll just simply write here 3D Enzo all right and this is nothing but a 2d tensor let me comment it down also this is nothing but a 1D tensor so after creating 1D tensor 2D tensor 3D tensor let's say if you want to create a 4D tensor with the help of the empty function so how can you create it so once again I'll just simply write here towards dot empty and let me assign the values here so if I'm writing here 3 comma let's say 2 comma 2 comma and let me give any random value let's say here 4. so you can see that here we are getting basically 4D tensor so this is one this is 2 this is a 3 and then we are nothing but 2 comma 2 comma 4 right so we are having here 2 right and then we are having two rows here you can see that and then four columns one two three four so this is the representation of 4D tensor similarly you can create 5V tensor 6D tensor and so on so this is a way you can create a empty tensor right so after understanding how we can create a tensor using empty function now we'll see what are zeros function and one's function so if I'm writing here let's suppose torch dot zeros and inside this function if I'm writing here let's say 2 comma 2 and if I'm executing it so on execution you can see that we are getting the 2D tensor so basically I've used here zeros function so it will give the zero value so here you can see that I am getting the 2D tension which is having two rows and two columns and the values that are filled inside is nothing but a zero let's say if you want to create a tensor which is having only one value right when I say one value that means inside that then so we are having only one as a value so let's suppose if I'm writing a torch Dot once and if I'm writing here the value 2 comma 2 so you will see that I will get only one and there will be two columns and two rows so this is the way you can create zeros and ones right so guys always remember that Pi torch will give you a tensor right so let's see some other functions also so the next function that we are having is the size function for that let me put inside this into the X that is nothing my assignment operator and now if I want to print it I'll just simply write here print X right now if I want to know the size of this tensor so I'll just simply write here x dot size function so here you can see that the size we are getting is nothing but 2 comma 2 that shows that it is having two rows and two columns right so moving next if I want to see the dimension of this tensor so what can I do so this is my X right so if I'll write here simply x dot and dimension and if I'll execute it so I will directly tell that we are having two dimensional tensor right now let me show you one more scenario if once again I am creating a variable X in which I am creating intensive let me write here torch dot once and this time I'll just take three and this time I will take you three rows and three columns but I will specify here the data type so to specify data type let's suppose I want the data type as an integer so I'll just simply write here torch dot end and let me here print it so you can see that on execution I am getting an integer but earlier if I didn't specify the data type you can see that I was getting a float values right so this is a way in which you can specify the data type moving ahead let's say if you want to create a tensor using list we can create it so now let's see using pytorch how we can create a tensor using list so I'll just simply write here torch dot tensor and inside this tensor function I'll create a list and inside this list let me write here 4.2 3.1 and let me add some more values let's say if I'm writing here 5 comma 2 and let me execute this so an execution you can see that we have created the tensor using list right so now going forward we will see some arithmetic operations so let me create here random variable that is nothing but X I'll just simply write here torch dot Rand since I want to create a random tensor so I'll be using here the Rand function so inside this Rand function let me give you the shape has 2 comma 2 so it will give nothing but a two dimensional tensor I will also write here Y is equal to torch Dot Rand and I'll just write here 2 comma 2 and let me print it you can see that while execution or while seeing the output will get different values because we are using here the Rand function so now I have already executed this and now if I am executing a different time that is next time you can see that I will getting different values another time you can see the another values now if I want to fix this value so for that what I will do I just want that whatever the value I'm getting it's fixed so for that I will be using the manual seed [Music] so if I'm writing here torch dot manual underscore set and inside this function let me write a random value so let me write here 42 now if I'm executing it so an execution you can see that this is my tensor right now once again if I am executing it you can see that the value will not change now let me perform the arithmetic operations here so if you want to perform the arithmetic operation let me just simply write here Z this is a new variable and I will just write here X Plus y and if I am printing here print Z on execution you can see that so this will give you the addition of this 1 and this one again this is nothing but a second row and second column so in this way it will add up and it will give me the output so now after seeing addition we are having here some functions that we can use using pytorch so let me show you instead of writing X Plus Y what I can do here I'll just simply write here torch Dot add and inside this add function I'll give the parameter X and Y and if now I am executing it you can see that I am getting the same output similarly you can do it for the subtraction the first way is nothing you can just simply write here Z or Z is equal to x minus y and just write your paint function and inside that Z Now the another way is to I'll just use your pie torch and I will simply write here tossed or sub and X comma y now on execution you can see that again I am getting the same output similarly for multiplication and divide so for multiplication I will be writing here tors Dot multiplication and that is nothing but a mul function and inside it I'll just simply write here x comma y similarly for division also so this is a way where we can perform the arithmetic operations like addition subtraction multiplication and division all right guys now let me show you one more thing now what I will do here I will once again create a tensor using Rand function so let me write here x so here I'll just write here x torch so I'll write here x is equal to torch dot Rand and then again 2 comma 2 the same I will just copy it and I'll simply put here in y and I will just write here print X print y so this is a basic operation right using Rand function which we have already seen now if I'm writing a y dot add underscore so when I'm writing your underscore that means we are performing in place operation so when I'm telling its in place operation that means we are doing something called manipulation right it may be in the form of addition it may be in the form of subtraction multiplication or division so inside this if I'm writing here x so you can see that so what I'm doing here exactly inside y I'm just adding with it to the X now if I'm performing you Z is equal to X Plus y and if I'm writing here print C so you can see that I am getting here the addition but the output is different so what's the difference between this and this so here you can see that I am performing here y dot add underscore X so what will happen here this is our y right so it will add this one with the X1 so if you'll add 0.86 plus 0.94 we'll get 1.81 right similarly it goes for the 0.56 plus 0.13 and the addition will get here 0.7009 similarly for these two also so guys always remember that whenever I'm writing here underscore function it is basically an in place operation okay nothing but I add underscore you can also perform here uh multiplication score or subtraction underscore right so these are nothing but an in place operation so after understanding it now let's see if we want to fetch any particular record in pi torch so what we can do for this so let me create here nothing but a torch dot once again I will use your Rand function and inside this Rand function I'll just simply write here let me write here 5 comma 3 so you will see that I will getting a 2d tensor right so which will give me five rows and three columns right now if I want to fetch the First Column record so this is nothing but my first column right this is the First Column this is second column and this is my third column so for that I'll just simply write let me first store into a variable X here and let me print it so if I'm writing here x and I'll just inside this list basically a list is denoted by a square bracket and if I'm writing your comma and then I'm just writing here 0 so you can see that I'll get here the first record that is nothing First Column record right now if you want to fetch the second record that means a second column right so I'll just simply write here 1. so you can see that whatever the data or value we are here in the second column right we'll get it similarly for the third column I'll just simply write here two so this is the way where you can fetch the records of any specific column right okay guys so after understanding it let's move to the another part that is nothing we're going to use the item function now if you see that if I am writing here x dot item so if I'm executing it you can see that I'll get an error why because item function will give you only one element tensor so how we can use your item function so before using item function let me create another Pi torch tensor so I'll just simply write here torch dot Rand and inside that I will just give you one value so let me print it so on execution you can see that we'll be getting a 1D tensor right so this is having only one value now here I will use here x dot item function so if I'm writing here x dot item so on execution you can see that we are getting the exact value so this item function will give you the actual value of 1 tensor right and make sure that always remember that it is only used when there is only one tensor okay guys now if you want to see the shape of a tension so which functions shall we use so once again I'll create a tensor supporter I'm using at Pi dot so I'll just simply write here torch Dot once again I'll be using a Rand function and let me give the shape here as 4 comma 4. and if I'm printing you print X so you can see that I'll getting a four columns along with four records that is nothing but rows right if you can see that here we are having a 4 cross 4 shape right and it is nothing but a 2d tensor right now if I want to change it into a one detensive so we'll be using here view function so if I'm writing your x dot view and you can see that 4 cross 4 that is nothing but we are having 16 values now 16 values I want to just put into a 1D tensor so simply I will just write here 16 so you can see that earlier we are having the 16 values but it was a 2d tensor now in just one date and so we are getting the 16 values right now this is the 16 values we are having then we can also put into a 3D tensor so how to put it I'll just simply write here x dot view so can we write here 2 comma 2 comma 4 right cos 2 comma 2 will be 4 and 4 into 4 will be sixteen two comma 2 comma 4 and now if I am clicking on this you can see that on execution I'm getting the same values but in the form of 3D tensor right so this is a way you can shape a size of a tensor so from a 2d tensor we have saved the size from to Wendy tensor and similarly to a 3D tensor right so let's move ahead so now guys let's say if we are having a tensor and we want to convert into a numpy so how we can do it so just let me write here in the comment uh converting to answer into right so how can we do it let's see so for this let me create a tensor which will be having only one as a value okay so I'll just write here torch dot once and I'll just write here five let's suppose or you can take 6 also any value you can take so here you can see that I'm having a 1D tensor that with six values and each value is having one right so I'll just assign it to the a variable or let's say X or any variable you can take right now after that what I will do let me print it [Music] again the same output will get now let me check the type of this a variable as we already know that the type is nothing but a tensor right so on executing it you can see that I will getting here as a tensor right let me write your type okay guys so you can see that this is a class having torch.tensive now I want to convert into a numpy okay so I'll just simply write here let me create a variable B I'll just write here a DOT numpy so this is a way in which you can convert a tensor into a numpy array now let me print it so if I'm writing here print B you can see that from tensor it has been converted to numpy now you might have a question that how we will get to know this is a numpy right this is a numpy array so for that once again I'll be using a type function so I will simply write here paint function and I'll write here print type and inside this type function I'll just simply write here B so on execution you can say that the tensor that is only tensor we have converted into an numpy array all right guys now uh let me write here print and inside that let me print here underscore underscore so it will not confuse now it looks good right so this is a way in which you can convert a tensor into a numpy array okay let me remove one U from here okay guys also guys I want to show you one thing I have already talked about the in place operation so if I'm writing here a DOT add underscore and let's suppose inside this I am taking the value as 3 right so you can see that a was over here tensor right then 3 will be added to 1 and it will be of 4 right now what happens if I'll print here B what will happen actually so if I'm writing you print function inside that b so guys you can see that here I'm using the in place operation Sony a I'm adding the value as 3 so here you see that our a was nothing but a tensor right so the new tensor will be having the value is 4. now if I want to print B if I'm writing here print B so on execution you can see that I am getting the 4 for the B also even though I have manipulated a only the reason beside this is nothing but tensor and num Pi will be pointing to the same memory location in CPU so if you are doing something in place operation on a it will definitely reflect on the B also right similarly it goes for the multiplication subtraction and Division in place operation two right so this is the basic idea so let me explain you once again so this is our basic tensor in which we have converted a tensor into numpy array and if we are performing here in place operation nothing but add underscore 3 so you can see that it will be added 3 in all of the values and the value will be 4 but I didn't perform any operation on B but once I am printing B you can see the value I'm getting here 4 right so whatever will be the operation done on a it will be reflecting to the B also because the reason is simple tensor and numpy will be pointing to the same memory location in CPU okay guys now we have already seen how to convert tensor into a numpy array now it's time to see how we can convert numpy to a tensor so for that I'll just simply write once again in a comment down numpy or before that let me write you convert numpy array to our tensor right so let me create here numpy so to create a numpy first I have to upload a library or module that is nothing but a call as import numpy and I'll give you an Ali as NP okay and let me create a variable a and here I'll just write here NP dot once and inside this once function I'll just write 5 and if I'm printing here print a so you can see that on execution we'll getting a numpy array which will be a 1D okay if you are having a doubt just go and then type print and type function inside that you can check it out as a numpy okay guys now let me convert into a pi torch so how to convert it I'll just write you B variable inside it I'll assign here torch Dot from underscore numpy and here is why simply a and let me print it so print inside the screen function I'll just write here B and here you can see that from numpy how we have converted the numpyu to a tensor now that's it guys now if you want to perform the operation let's say if I'm writing U let me write you a plus is equal to 2 if I am writing and if I'm writing a print a so this is my a right you can see that I was getting a 3. now let's see if we are doing some operations on a how it is reflecting to the B so if you're writing your print B you can see that the output will be getting the same as 333 right because I already told you the reason tensor and numpy will be pointing to the same memory location in CPU so this is the basic idea about how we can convert a tensor to numpy and a numpy to a tensor so after understanding how to convert a tensor to numpy and a numpy to a tensor will see how we can do back propagation using pi torch okay guys so let me write here in a comment back propagation propagation and Pie torch so why do we use back propagation so let me give you an idea about back propagation so we use back propagation to compute derivatives okay so let me give an example so that you can understand it so for that let me just create a tensor so I'll just create a tensor here x equal to torch dot pencil and let me write here the float value let's suppose if you're not writing float value right and if you are printing X because I have used here tensor function here this time so an execution you can see that we are getting the output here right now why I was telling it let me write this integer value now if I'm writing here once again uh requires underscore grad is equal to true so why I am writing here requires underscore grad because I told you that bad propagation is used to compute derivative and when I'm talking about gradient what does it mean it means the derivative part right so by default in this tors.tensor this is a tensor function in which if you are not writing requires underscore grad is equal to True by default it's false here so that's the reason we usually write here requires underscore grad is equal to true now let me execute this so on execution you can see that we'll get an error why because I am having here integer value so if you see here it's showing runtime error only tensors of floating point and complex data type can require gradients so if writing here let's suppose 4 and if I'm writing a DOT now on execution you can see that I will not be getting an error so always remember whatever the input we are giving it must be a floating point right it should not be integer so this is my integer in which I have already given the requires underscore grad because I want to check the back propagation I want to see how the back propagation is done in a pi torch now let me show you an equation so let's say if I'm writing here Y is equal to X cubed so this is a way we can represent in a python and if I'm writing your print y now what is our axio X is 4 so 4 to the power Cube 4 into 4 is 16 and 16 into ah 4 what is it 64 right so on execution you can see that it will give us a 64 right so this is our equation so now what will be the derivative of this equation if you see here so derivative of this will be nothing but let me comment it down it will be d y upon DX and it will be 3 x square right this is nothing but it's coming from the chain rule if you are doing the derivative of x Cube it will be a 3x square so let me write here 3x and it let me write here square right why I'm commenting it down I will tell you later because we gonna perform here back propagation so if I'm writing here y dot backward function so I am writing a backward function when I'm writing y dot backward so what will happen my equation will convert from y equal to X Cube to the d y upon DX that is nothing but is equal to 3x square so it will give me the derivative part so whenever you as soon as so repeat as soon as you are executing the backward function you will get the derivative of the equation now how to check it whether I am correct or not so now if I am printing it print x dot grid I am writing here x dot grad because I want to see it so let's say the derivative of X Cube will be 3x square and what's our x value it is 4 so 4 Square will be 16 and 16 into 3 so 16 into 2 will be 32 and 32 into 3 if we execute you is showing an error because we don't have to write here parenthesis uh let me execute now so 16 into 3 will be nothing but a 48 right so you can see that as soon as I am using this backward function this y equal to X Cube will be changed into a derivative 3x square and x value is 4 so 4 square is 16 and 16 into 3 is 48 right so this is a basic example what happens if I am taking a list here so let me create a list for the back propagation this time so I am creating a list as L1 and in this list Let Me Take 2 comma 3 comma 5 and let me take some more values 6 comma 9 comma eleven and I'll take your 13 comma 17 Comma 18. just random values right now let me create the tensor here so I'll just simply write here x dot tors.10 so since I want to calculate the gradient right that is nothing but a derivative part so I'll just simply pass here L1 and I have to write the parameter that is requires underscore grid grad right which will be equal to true and on execution let me just show you how it looks like if I'm executing it I am getting an error because I told you that it shouldn't be integer so let me make it floating Point values now I am executing it you can see this how my tensor looks like right so what I will do here so let me create an equation this time a little bit complex so if I'm writing here let's suppose Y is equal to x to the power 4 Plus x to the power Cube let's say so what will be the d y upon DX let me first print by you so instead of X what will happen these values will go right so in the first place the value will go 2 right to the power 4 and this will be the values here right okay guys so these are the values but it is in the form of exponential form so let me just change it to a simpler version so if I'm writing your x to the power Cube and x square and now you can see that the value is looking like this right so now I will be using a backward function so backward function will do what so it will give us the derivative that is d y upon DX and it will be equal to 3x square so it will be simply 3x uh square and plus it will be x to the power square is nothing but 2x right so let me write it okay let me comment it down so I want to calculate the gradient and gradient is nothing but a derivative right so now what I will do here I'll just simply here write I'll just simply write here y dot backward but as soon as I'm executing it you can see that I will get an error the reason behind this is gradient will be taking here scalar outputs so for the scalar outputs what I will do here I will just simply sum it or you can also take the mean so let me write you a y dot sum so what I will do this is a y function if I am taking some of it you can see that it will add up all the values let me store into the Z now since you can see that once our Z value is coming let me print it it's nothing but a scalar right and here we are having a lot of values so this is our Z now if I'm writing a z dot backward so now Z dot backward so as soon as I'm executing here Z dot backward what happens the d y by DX will be nothing but 3x square plus 2X I'll get the derivative so now you might have a question that once we are executing it it should be a scalar input in this case we are taking the sum let's say if we don't want to take the scalar input so in that case what we will do so in that case you have to create the vector right so coming to this case again now we have already taken Z dot backward now if I want to check my gradient I'll just simply write here print x dot grid and if I am executing it you can see that I'm getting the value so here you can see that the equation x 3X Square so x value is here nothing but the x value is here 2 right so 2 square is 4 3 into 4 will be 12. right 12 plus and again the 2 value 12 plus 4 2 into 2 will be 4 16. so here you can see that I'm getting 16. so as I told you that backward function will give you the derivative and the next dot create as soon as you are executing it you will see the value the x value will be coming from here and it will go inside this function that is nothing but in our derivative and it will print it so in this way you can do the back propagation using pi torch so this is a basic idea about the pi torch how you can use the pi torch just like same as numpy how you can do the back propagation in pi torch right so that being said I wish you good luck on your pytot journey I hope these steps will help you plan your learning schedule that's all we have for this video I hope this video was informative for all of you guys out there willing to take your career to new heights with pie torch thank you for being here till the end of this video if you have liked this video make sure to hit that Thumbs Up Button also don't forget to enable subscribe button to never miss any update from intellipat YouTube channel thank you all if you want to make a career in data science then intellipat has IIT Madras Advanced Data science and AI certification program this course is of very high quality and cost effective as it is taught by IIT professors and Industry experts
Info
Channel: Intellipaat
Views: 3,407
Rating: undefined out of 5
Keywords: what is pytorch, pytorch tutorial for beginners, pytorch tutorial, pytorch for beginners, how pytorch works, what is pytorch used for, pytorch deep learning, what is pytorch in machine learning, deep learning with pytorch, introduction to pytorch for deep learning, introduction to pytorch, what is pytorch in deep learning, learn machine learning, machine learning 2023, deep learning tutorial, deep learning tutorial python, deep learning tutorial for beginners, intellipaat
Id: O98PEUSsGY4
Channel Id: undefined
Length: 37min 14sec (2234 seconds)
Published: Fri Mar 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.