WHT

Pytorch .cuda() | Pytorch Tutorial 6- How To Run Pytorch Code In Gpu Using Cuda Library

Krish Naik

Subscribe Here

Likes

150

Views

7,498

Pytorch Tutorial 6- How To Run Pytorch Code In Gpu Using Cuda Library

Transcript:

Hello, all my name is Krishnan! Welcome to my Youtube channel So guys today in this particular video, we are going to see how we can use GPU to execute our pytorch programs. You know, suppose if we are creating an artificial neural network, then how we can use GPU in order to execute this pytorch programs itself. So this is the sixth tutorial guys? Uh, if you remember, I have completed five tutorials. We had also solved some of the kaggle competitions that is advanced highs for house price predictions and Kaggle Pima diabetes using a N. So we’ll continue. This, Uh, and probably this is the last video with respect to a N. After this, the seventh tutorial will be on CNN and, yes, I have also prepared the materials for that so more more frequently now the videos is going to come with respect to Pi Torch, so lets. Go ahead and try to see this now before going ahead. Guys, if you have some bit idea about, uh. If I talk about tensorflow right, So if I talk about tensorflow, uh, suppose, uh, I’m just going to go in inside my environment. Uh, activate my env. This is basically a tensorflow environment that I have actually created. If I just go and write Python, and if I just write import tensorflow, so when I write this, you will be able to see that if you have a GPU. And if you have installed the cuda libraries like cuda toolkit and the CU dnn, so you can actually refer this video of mine, which is there in the deep learning playlist how to install cuda toolkit and cu dnn for deep learning, This is specifically required for both tensorflow and Pytorch. It is very, very, very important. Okay, without this. Definitely, you will not be able to run now. If I see over here, you can see that I have already installed tensorflow GPU in my machine. And if I try to show you, you know in my C drive, you know, so if I go inside my program files. If I go inside this one, I have cuda 10.1 installed. Okay, so this is pretty much important apart from that. I can also check. I can also check my, uh. If I write NVIDIA SMI, probably I’ll be also able to see the 10.1 CUDA version, right and probably in my workstation. I have Titan rtx. So I can definitely see that now. In tensorflow, suppose if you don’t have any GPU, then what it is going to do. And once you install Pip install Tensorflow GPU, you know, um, if you don’t have GPU, it will directly Switch towards CPU, but in Pi Torch. You have that particular option you can run out run. Uh, the programs in CPU, also in GPU also. Okay, so that also we are actually going to see, uh, apart from this guys. Uh, if you have not seen my other videos and deep learning, I’ve already explained about tensorflow a lot, but in this video, we’ll just focus on Pytorch, which is pretty much important now. What, I’m going to do over here, guys? If you remember I had created, uh, this one, creating a N with Pi Touch on Pima Diabetes data set. I’ve just made a copy of this over here and I’ve written and training on GPU, right, so this was the whole program. You can see Diabetescsv. DFI actually executed this whole thing with the help of pytorch, you know, and we had seen the confusion matrix and everything as such, uh, and again, the video has been put up over here. This is basically the video solving Kaggle Pima diabetes using a N. So let’s first of all again. I made a copy of this. This is the same file that I see over here and I’ve written as tutorial 6 so let’s start with this initially after you have installed the cuda toolkit and dnn, okay. The next thing that you have to do Is that just write Pi Torch installation and probably initially. You need to install this. Now, suppose. I showed you in my local. You know, I have 10.1 right 10.1 I can select the stable version windows. Conda Python this and just execute this one, right. If I copy this if I go to my anaconda, prompt right and probably I can create a new environment. Also, I’ve already created and created a new environment so I can write. Activate my environment. Name is Env Pytorch, and probably if you’re following the series of the spy dodge videos, I’ve actually created this. Then you can just paste this automatically. The cuda toolkit will get downloaded specifically to, uh, pi torch also, right. If you have not, uh, in tensorflow, I have actually manually downloaded CUDA toolkit and I have installed it. So if you have not installed tensorflow separately, you can just directly write this command and automatically the cuda toolkit will get downloaded so once. I’m there what I can do. Is that now you can see this ill? Just remove this and I’ll just go inside this. Write it as Python and I can also import torch and check whether it is working fine or not so here you can see that it is working fine now as I told you one important thing about torch is that you can run this in both CPU and GPU. Okay, now let’s see how we can check whether it is having CPU or GPU. So first of all I import torch, okay, and I can use this command. Torchcuda dot is available, so this basically says that whether CUDA is available with respect to our GPU, whether the GPU is available or not. So if I execute this right now, it is showing true, because obviously I have a GPU over here. I have all the configuration installed properly. Then I can also go and write current torchcuda dot current device. This will actually give me the device. Id so device. I’d is basically zero device. Id of the GPU, then. If I really want to get the device name, I can basically write Dot cuda. Dot get device underscore name. So once I execute this, you’ll be able to see that I’m having Titan rtx. So I’m actually executing this in my workstation, So I can definitely see this suppose. If I don’t give the ID what happens then also just by default. Take whatever Id is available over there and it will show you this information, but I’ll just keep it this because suppose if you have two gpus at that time, if you want to execute in another one at that time, you can specify your Ids also now. One important thing. Uh, and this is that let me just restart the kernel because I had created some of the variables. Okay, so I’m going to execute this. This is working fine. This is working fine now. I can also see that. Is there any memory allocated in the GPU right now? Okay, in my Titan Rtx, right, because still, I have not executed any program, so let’s see how much memory is allocated right now. It is zero, okay. Uh, probably. I also have one more function, which is also called as torch dot cuda dot, uh. I think it is caching. I guess let me see memory cached. Yeah, so if it shows memory cache, also, it will be zero Because yet I have not executed anything as such now. This is very important what I’m doing, is that ill. Just remove this right now. Okay, I’m just creating a float tensor with three numbers inside this, okay, like one point zero, two point zero and three point zero. I hope you know this float. Tensor, I’ve already taken that in my first or second tutorial in the python, so I have written Torch Dot Float Tensor, One point, zero, two point zero and three point. Once I execute this, So this is basically my Var one. Let me just write it out. Where one is basically a tensor. A floating point tensor then. If I write var1device okay, right now. I have just created this float tensor like this, right. If I go and see where1device, it is showing the device type as CPU because the way that we have actually created by default, it goes to it. It is basically allocating this whole floating tensor or it is basically allocating, uh, or it is basically assigning this whole float tensor to the CPU, right so that any any any execution that will be handled going forward will be done by the CPU. Now, suppose if I really want to execute this in the GPU? How do I do it so here? I’ll just write it as cuda and execute it now. See when I write where1 it is basically showing me a. It is a cuda tensor now, and if I write where one dot device. Okay, sorry, so I have to write cuda with this right so here you can see that now. It is showing device, Cuda. Huda is a function over here. Guys, which will definitely say that, okay. I’m I’m creating this flow tensor. I will be assigning this for the further execution to the GPU itself, right, so if I go and right now, where one dot device you’ll be able to see that the device type is now cuda before the device type was CPU. Okay, this is with respect to all the variables right now. You may be also having a question. Okay, Chris, fine then. Um, what do you think in a and do we need to provide all our X strain y train as the cuda tensors. Yes, right, we need to assign those now. Let’s see, let’s see this was my previous tutorial, and this probably will get executed in the CPU. Okay, so I’ve taken this diabetescsv. Uh, probably I’ll just import it. Um, and then I will also probably I’m not going to use C bond. So let me just execute this one. And I’ll create my independent independent features and this one, uh, probably I will also execute this one. Okay, now here you can see this, right. If I am executing this extra in white rain white test, I’m converting this into float Tensor Float Tensor. And as I told you why I have to convert that into long tensor. So if I go and execute this if I go and write below if I write X underscore train dot device. And if I execute this, you can see that it is a CPU now. Same code is actually present over here. I’m going to just execute that code. See this, so let’s BC. Okay, I’m just executing this. Okay, so this I have got my xtrain and y train now here. You can see that what change I am actually making now for the extreme before in this program. I did not had dot cuda but here. I am just writing Dot cuda, right, So in this pima diabetes, I am just writing. Dot cuda dot cuda, dot cuda dot code. Right, So once I execute this. This got executed now. If I go and search for X underscore train X underscore train dot device. And if I execute it now, you can see that the device type. Is cuda initially here. The device type was CPU because I did Not Use Dot cuda, right. This is the first step now when I will be talking about. Um, CNN models, probably in the next class of next session. After this, you know there, I’ll be using some image like how we have in Keras, like image data generator. Similarly, we have, uh, in Pytorch also, uh, similarly, a data set loader or image loader. I have for that. Also, there is an option which I’ll be showing you probably in the next session. How you can run that in Gpus because I really want to execute some image classification problem and show you the speed how it works in this workstation. Amazing, amazing speed. Uh, it works in a better speed itself. So now you can see, I’ve done this now. The next step. Is that okay fine? I have all my independent and dependent features right now. What about my feed forward neural network? Can I also make them run in the GPU, right. We also need to make them run in the GPU, right, so for that, you see over here. I have taken this a n model. As an example, it is a simple feed forward neural network with two layers and one hidden layer. Okay, oh, sorry, one output layer. Uh, so here you have this output layer here. You have two hidden layers, and then you are actually applying relu activation function and you are getting the output. So if I execute this if I execute this if I go and see model parameters, this model parameters is giving me all these particular parameters right now. Suppose I also execute this again. Guys, I’m just comparing. Okay, So don’t worry that don’t think that I am skipping anything. I’m just comparing. This is another program that is another program, right, so this is basically my model parameters now now. This is very, very important, okay. If I write something like this, suppose if I go over here, if I write something like this or let me write a for loop for r in model dot parameters. Probably I am writing it, right. I guess print I okay. So this is, uh, basically, my parameter containing tensors. Uh, instead of this? Let me just use next or just a second. Okay, so ill. Just try to use one more technique. You know, probably this will help is underscore cuda, okay. I just want to compare whether okay. It’s cuda. Sorry is cuda. It is a boolean variable. Now here you can see that whatever layers we have actually created like over here, you can see, this is my layer. This is my layer. I’m just checking whether this layers has been assigned to the GPU or not, so definitely in this CPU side. You see that I’ve never assigned anything to CPU anything to gpus right here. Also, I’ve created my a N model. I’ve initialized over here, but I have not assigned it to anything. So if I really want to check whether this is assigned to CPU or GPU, I can basically use this. I will traverse through each and every layers each and every parameters that are present in this and model. And if I’ll write, just I’ll check this condition by using This. Is cuda boolean function, right, so here, it is actually telling me false because I’m not using GPU. Now, suppose if I execute this, and if I try to put it over here, right, and if I try to say the same thing now, probably okay ill. Execute this ill. Execute this okay now. It is also saying me false, right, but I told you that this is basically the code that I really want to run it in the GPU. So how do I do it? I just have to change this one line of code, so whatever model object I have created over here. I just have to use Dot cuda like how we use dot cuda here. Similarly, we have to use dot cuda here, right, So once I execute this, and if I execute now the above one, it will be showing us true now. This basically says that this whole thing has been assigned to the GPU for the execution purpose now. If you really want to see how much memory you have got allocated, you will be seeing that this memory memory has got allocated. You want to see how many cash how much cash memory has got allocated? This much cash memory has got allocated right, So now here you can see that how simple in a simple way. So I love about Pytorch, right. It is very, very much simple. The syntax is almost like a oops concept. If you are very good at oops, you can definitely use this in an amazing way now coming to the running of the model. So after this, definitely, I’ve assigned my model to the cuda, Uh, basically assigned to the GPU for the further execution. Um, then I’m going to assign the clause function. The Optimizer. This is pretty much simple, right with the learning rate as zero one. And now you see this, I am running for ten thousand epochs and probably guys this whole thing. I’ve explained in the second tutorial. Only you can just go and check in this Pi touch. Uh, sorry, pytorch for tutorial, right, I’ve actually done this in fourth tutorial. Okay, so just go and see this. So once I execute it now. See how quick the execution will happen in the GPU? If you really want to see whether GPU is getting utilized, you can see over here. It is getting utilized. The CPU is not getting utilized over here. So, uh, it is completely done by GPU and let’s see. What is the total overall time? The total overall time is somewhere on 11.94 and this is just a tabular data for small number of data sets, guys. Pima diabetes is a very very small number of data set. But if you are trying to execute some huge records, it will probably take at least 15 to 20 not at least one minute. Let’s see like I have. I’m running for 10 thousand epochs, right, so we can compare that now. Similar thing over here, it is false. We’ll just run this in the CPU for tabular data with small number of records. I think, but at least this program will take more time when compared to this. That is for sure. Okay, let’s see and lets. Learn it so here you can see it’s running. Where did it go, okay. It’s running, it’s running, definitely more than 11 seconds. At least now you can see over here. The CPU usage also increased right, so this in turn shows that it is getting executed in the CPU. Right, see here now. It has got down that basically means now. The execution is got over so definitely it says 13 seconds here. It was 11 seconds. I know it is not a very big difference because the number of records are very, very less right, so just try to execute this, and probably this. All things are same in the next video. I’ll be showing you how you can execute this in CNN. So I hope you like this particular video. I’ll be putting this materials in the Github. Uh, if you really want to try out. If you have any kind of gpus, just make sure that follow this tutorial to install cuda toolkit and cu dnn and then use this command. If you you can also use this command to install that cuda toolkit. But make sure that you have the cuda. Cnn cuda co. Dnn, Uh, installed properly. Okay, So I hope you like this particular video. I’ll see you all in the next video. Have a great day ahead, guys, and thank you for all the support. Please do subscribe to share with all your friend’s bye.

0.3.0 | Wor Build 0.3.0 Installation Guide

Transcript: [MUSIC] Okay, so in this video? I want to take a look at the new windows on Raspberry Pi build 0.3.0 and this is the latest version. It's just been released today and this version you have to build by yourself. You have to get your own whim, and then you...

read more