This tutorial introduces the four fundamental building blocks of PyTorch: (1) Datasets, which organize data into classes implementing __len__ and __getitem__ methods; (2) Models, which define forward mappings from inputs to outputs using classes derived from nn.Module; (3) Losses, which quantify prediction errors like mean squared error; and (4) Optimizers, which automatically compute gradients and update model parameters via methods like Adam. Using linear regression as a simple example, the tutorial demonstrates how these components work together: data loaders provide batches of training examples, models make predictions, losses measure accuracy, and optimizers iteratively improve parameters through gradient descent. The key insight is that PyTorch's autograd system automatically computes derivatives, enabling efficient training of complex models without manual gradient calculations.
PyTorch Basics for Graph Learning: Datasets, Models, Losses, Optimizers
Added:okay so welcome everybody and this is the second tutorial of our series of tutorials and this time we are going through some basic of pythons so without using graph but just usual i mean the usual pythons and um the idea is that we would like to do a recap or some basic idea for the ones that are new for the one that know this already for the ones that that are new to these to this library so to these pythons library the idea is to get a feeling of the main uh building blocks and the nice thing one of the nice things of this library is that it's very modular so it's possible to extend various uh tools that are implemented in the in the language okay so the outline is the following so we will go through the notion of data sets of models of loss functions and optimizers and so these are the main things that are within python and there is a much better introduction to the topic so there is an official 60 minute introduction so 60 minute is a lie it will take much more but it's very nice it's very comprehensive and many of the ideas that i will present below are taken from here and also the documentation is very complete so if you have any uh doubt on something you can go there and it's very clearly explained in every detail so it's not just the code or the function but there are also ideas of the theory being implemented and today i would like to go through these using the example of linear regression so i will not go to every detail that is covered by the official tutorial but i think it's nice to have a very simple example so this one because it's very easy to to show to visualize and to get insight into everything okay so let's start and the first thing is uh that we need is a data set so here the idea so we want to have a linear regression as a model so we can start from a true model that is itself linear so the idea is that our data are obtained from a model that maps an input vector to uh undefined transform so matrix times the input vector plus b okay and so the input has a dimension that we call input dimension and the output as dimension that we call output dimension and so this is realized as follow so at the moment uh we we're mainly using this tutorial input and output dimension equal to one because this makes everything easy to visualize but the code works for any dimension so if you want to change this to any value it will work okay so the idea is that we fixed we generate a fixed matrix with the correct size and a fixed vector and our true model is exactly doing that so it takes the input multiplies times the matrix and add this vector v okay and then from these we can uh generate some data just by generating so we generate a training set so a set that is used to train our model of 1000 inputs so these are randomly generated inputs in into minus one one okay uniformly generated and here and everywhere the data sets so are in these are organized in this way so we have you see that the dimension is number of training points times the input dimension and this means that every so that x train is a matrix and every row is one sample so it's one uh input dimensional vector and and and we collect the values uh samples as rows okay and then uh as training values so the output we just use we just evaluate our models or the true model on all these inputs and then we add some noise so this random noise with the correct size and we just add some uh so we scale the error according to this noise level okay and if we do that we can visualize everything and we just get this nice thing so this is just the all the points in our data sets so they have an input value that is one dimensional and an output value which is again one dimensional and this is more or less a linear function plus some noise okay so that depends on the metrics that we generate so if we run it again it's just it's just a different so here it doesn't look like it change so much but yeah it can be whatever okay so just a linear model plus some noise now the noise looks always very good but every very large but that's it okay so this is our data set and the goal is to construct a model that looks like this hopefully without the noise so we would like to recover something that is close to be like this so a linear so something such that if we have another input our model is able to predict an output that looks close to what the true model will give us okay and so uh at this level this is all numpy rise arrays but there is a very nice way to so the first concept of pytorch is this data set class and is a the the way that python has to deal with data sets okay so let's see so we we first do the following so we have these external class vectorial data set i'm going to show you in a moment what it is and this does construct an object that we call training set so this is our training set and it takes as input the input data and the output data okay and i show you what this does so this is just a very small class here and so to whatever that data you have you need to construct a class that implements these so or derives these classes of this dataset and this is very simple because the class so the constructor depends on whatever your data set is but you just need to implement two methods one is lan and this gives exactly the length of the data set and this get item which gives you uh the element of position index okay so this is i implemented this but it's very easy because you just need to give an implementation to these two methods and if we go back to the to the code here so you see we constructed this and then you can compute the length and this is one thousand which makes sense because it's so it should be like that so we had one thousand pair of input output and having the method get item allows one to get iterators or let's say access through slices so if i want to the element in the data set with index 10 and 11 you can just do it like that and these uh training set gives back the input so you see this is a tuple so with two elements the input and the output and these are two vectors corresponding to the index 10 and 11.
and just a small thing so tensor is the equivalent so is the way the tensorflow that soap sorry that fighters uses to represent the multi-dimensional vectors so scalars or tens are one-dimensional or zero-dimensional tensor vectors like these are one-dimensional then you can have matrices or three-dimensional stuff so it's like a generalization of a numpy array but it allows to do some stuff that is done within fighters okay but essentially so far we just have a class that is a wrapper around our data and allows us to do these two operations so now this looks very i mean not very clever because these are operations that we could have done already with the original data so using our numpy arrays and indeed here it's just it's not very clear why this is useful uh but if you think about for example your data set being so every input instead of a one dimensional point maybe an image and the output may be a a class so it tells you if the picture represents a cat dog or whatever so it may be nice because when you call this method instead of just giving you the element of a vector it may take care of loading from memory the images with index 10 and 11 okay so it may be that your data set is not existing into memory and if you have a method like this you just after after you put your data set into a method into a class like this you you can use method that look like numpy arrays but without being an umpire so really you can uh be behind the implementation of this method there could be something that reads from disk something that downloads for somewhere and whatever perform a simulation that gives you some sample whatever okay so it's nice to have this unified way of representing data sets so later in the in the next week also graphs will be embedded within this kind of class okay and uh so this is the first thing so and now that we have a data set we can construct the second object that is used by pythons to deal with data and this is this data loader okay so now uh we just uh need to to call this to construct an object of this class so data loader so now our our train loader is an object of this class and this takes as input exactly a data set so our dataset was defined before so the first variable needs to be a data set in the form that we built before and then two variables one is this batch size and one is the shuffle parameter so this is the basic setting and so now we see briefly why what these two represent and why is useful to have another abstraction around our data so the first thing is this batch size so this uh works like that so with our data set alone we can have slices like this but we will see later on that is useful to iterate over our data set by getting every time a fixed number of element so in this case we fix batch size to be 120 and if if we execute this code so what we are doing is just so the first thing that train loader allows us so is an iterator so we can iterate over it okay and every time what is return is this batch and that is exactly composed of two elements so zero and one and the first one is a batch of inputs and the second is a batch of output so you see we have uh so when we iterate over this we have nine runs and every so nine elements and every time we get as the first element of the patch is uh so we are checking the size so the shape and this is a bunch of vectors of size 120 okay so that means that we get 120 inputs and the corresponding 120 outputs okay and so then we call it the second time and we get a second batch of bunch or of data okay and as you see so i just checked that so we specified this strange number because we had 1000 inputs now we want to have the data one 120 at a time so the first nine times we have 120 which makes uh 960. so there are 40 left so the last time we just get back what is left okay so essentially this is a way to iterate over the data set with a very convenient interface so we don't need to do anything just to run over it in a way that we can get back every time a sample from the data set okay and of fixed size and this takes care of making sure that we are iterating over the entire dataset and there is a second parameter this is shuffle and it makes so exactly what you think so if it is set to true which is the usual behavior these are random samples so it's not that they you always get the same so we can check that so now we constructed the object we shuffled through so we can run twice over the data set so we are going to command the code in a moment but if you run twice here we are comparing the x so x train so this is the first the first column is the data itself the input data the second column is the first so it's the first batch that we get at the first iteration and the second and the third column is the batch that we get at the third iteration and as you see they are all time different okay and what we did here is so we introduced a notion here this notion of epochs so this means the following so we are doing one iteration if we do this for loop we are doing one full iteration over the data set and this is usually called an epoch or ipod okay so every time you do a full iteration over a randomized version of your dataset you are doing one ebook so here we are running two epochs so two complete runs over the dataset and every times we take out uh batches of 120 elements and we are just saving the first to check that okay so but if shop fall is true we always get a different result if shuffle is false this doesn't change anything but as you see when we run over it every time we get back the same the same batch okay so there is no shuffling no permutation of the dataset these i don't know if it's ever used in application but for sure is useful if you want to debug something or to make sure that every time you get the same result out of that okay so uh do you have questions or something about this okay if you know we can go ahead perfect for me go ahead okay thank you so this is all about the data set okay so we have some data and then we at the end of this process all that we have is this train loader so we don't need to care what is the data we just can we can just iterate over it and get data out of that the second component is models and in this case we implement a linear model so we know that our data are linear so we try to recover that by a linear model so our model maps the input to an output and which is again a fine model now this a and b are not the same as before because in principle we don't know them and we want to find the correct values of a and b such that this is a good model of our data of course in our real situation we have no knowledge about the true model and so we try to build this our model to be general enough that if we find the correct parameters it will be a good approximation of the two one but here for demonstration purposes is is better to have exactly undefined model okay and so we do the following we are importing this linear model which is uh now and showing you the code so and we are just initializing our model to be this linear model and this is defined as follow so this is this is again one of the nice functionalities of pythons so in this case our linear model is an implementation of the of n so n is one uh is the distortion and okay so one part of the library and we are just implementing it to derive this class and also in this case there is a very simple thing that needs to be done so forget about the constructor which is not very important at the moment all that we need to do is to implement a forward method which implements exactly the map from the input to the output okay so in our case uh we can start the model in a very easy way so we first save the dimension the output ones and then we just specify a linear layer which is exactly uh undefined mapping so this is something that is provided by pytorch so this is already implemented and this is exactly giving a transformation of the type ax plus b okay we just need to specify the input and output dimension and this bias true means that we have also the vector b if we set it to false we have no b okay so we just decide what is within the model and then our forward pass so the evaluation of the model just means to take the input and evaluate the the linear model on it okay and then this gives back the output the reset method is not very important now we will see it later okay and so essentially we are just using uh a transformation that is provided by pythort here okay and we can see it so now if you if you check what the model is if you print this is nicely telling me that there is my model has only one layer so linear and it tells me what it is so that is a linear layer with an input value and an output value so one dimensional input output one dimensional output okay and this is a very basic building block and there is in the documentation a very nice list of so there are already implemented many more layers with so ways to to we don't need to have this just these simple things simple linear thing you can have combination of very complicated functions so your model can be said to be pretty much everything and especially to be a deep neural network or a convolutional network or whatever okay but we are not going through that at the moment just know that if you want you can instead of having your forward method to just evaluate the linear layer this can be any complicated function even something that is written by you if you want to add it okay and then an important thing is that this model is parametric so what we expect is this model to depend on the values that we give to a and b and indeed if we check what is what are the model parameters are exactly uh these two so now everything is one dimensional so the first one is the matrix a and the second one is the vector b so you see by the parentheses that this is a matrix and this is a vector so these are the ones that are randomly initialized within the linear layer and these are also an a have also named so if we check within the model the linear layer has a weight which is the name given to the matrix so if we change the dimension this one will be a a matrix with a correct dimension and the bias is the vector okay so essentially our model now is represented as something that has a forward method and is represented by weight by coefficient with within by parameters so if we change the values of the parameters we change the output and this is exactly what determines the behavior of the forward method so we can test that we can take a random input of five uh inputs of the correct dimension and compute a model point forward so we just evaluate the model on those data okay and if everything is correct so we get a five dimensional output of course so sorry we get five outputs one corresponding to every input and if everything is correct this should just be the matrix so the weight multiplied by the input plus the bias okay so we can do it by hand and check if that is correct and indeed hopefully yes it is correct okay so the forward method is exactly just evaluating that linear uh linear transformation linear fighter solution so at the moment we just initialized this model and it is initialized to random weights and bias so we can visualize what happens and now of course this is not a good prediction okay because the values are random okay and um but it is clearly a linear model okay if you specify something more complex here instead of having a straight line will be something with another shape okay so now we have data and we have a model and the goal is to try to steer this model to be as close as possible to the data so the first thing that we need to specify is what we mean for the model to be close to the data and this is done by the losses okay so in this example we use the mean square error loss which is just the usual thing so is the l2 norm the distance between two vectors okay and also in this case pythors implements a lot of different losses so there is the link i left you the link here you can go there and there are plenty of losses already implemented and in this case we just define our loss function to be this mean square error loss and there is an option to specify a parameter this just means what to do so we will see it in a moment but at the moment we just explicitly specified that we want to have a mean and just to check that everything is working so we just take two random tensors so two random vectors one is given by these are three-dimensional so one two one and the other one is zero zero zero and if everything is correct the loss evaluated on these two vectors should be the distance between those two so 1 minus 0 square 1 plus 4 plus 1 let's see is it correct yeah i should be correct because there is also a mean so yeah four five yeah it is correct by the way i don't know now i i'm i'm getting some confusion but it's exactly doing what you expect it to do okay okay so this is the simple loss and how to use that so remember that uh what we wanted to do is to give uh like an evaluation of our current model so globally so what we can do is the following so is is pretty much the usual thing in every kind of regression problem uh so we can evaluate the loss for one single input output pair and that is just uh taking the input uh evaluating the model on that and then we compute the loss so the distance in a way it's not a distance in this case it is but it may be not a distance but it's a score of how good the model evaluated in x of i is uh as a prediction of the true value okay and then we can get a cumulative loss so a score of the full data set so i denote it as l of the training set and this is just given by the average so we compute the loss on the full data set we take the sum and then we take the average okay so this is just a way to assign a unique number so this is just one real number that tell me given the data how good the current model is okay and this is the reason we use so this reduction mean means that we are computing the mean here there is another possibility to have reduction sum and then we don't have the we don't divide by the number of strains that but we can just specify that and then the loss so when we evaluate this it is evaluated correctly as what we specified here okay okay now i just want you this is just visualization so don't look at the code so we can compute what we do is just to generate a grid of possible weights and biases between minus two and two okay and we can and then we evaluate the loss everywhere and this is what it looks like so for every so every point here is a pair of weights and biases so this is one line with one steepness and one bias and depending on that you see that the loss is increasing okay the red point is exactly given by the matrix and the bias of the true model and indeed as you see the loss is very small when we are close to the true value this is not exactly in the minimum because there is some noise and the loss is not taking that into account so he has no knowledge it has no knowledge of the noise but as you can see there is a very nice convex loss that points to in the direction of of the true solution okay this is a special case because you are using linear regression so in general uh with a general neural network is really not the case that the is a nice unique global minimum okay but this is nice for visualization so i'm happy with that at the moment we can use we can use that now okay and so now the goal is to in some way so our current parameter the random one these one the one that generate this uh orange line are a point somewhere here okay and our goal is to modify those coefficients and steer them as close as possible to the minimum okay so usual optimization and the usual way to do that is just to compute so what i said in words now is written in formula so i want to compute the minimum over the parameter of the model of the loss okay so in our case the parameter are only the matrix and the vector and the idea is to use some iterative method so gradient-based iterative method and that is what is provided by fighters so that's what it's really the strength of this kind of libraries that they do this thing completely for free okay or at least in an automatic way so of course in this case we have linear regression so it may be easier to just compute normal equation or something like that but it works perfectly fine also with a general situation so the idea is to start by some initial parameter and then run a number of iterations which are indexed by k and the idea is to take the whatever parameter you added the previous iteration and to compute the next parameter you just take the old one minus this eta k which is called the learning rate times the gradient of the loss with respect to the parameter so we want to minimize this loss so we just do some gradient descent we update iteratively the parameters according to the direction to the negative direction provided by the gradient and this eta here is just controlling how far we go along the direction provided by the gradient okay and of course to do this the crucial thing is to compute this gradient okay because if the model in this case we have linear regression but if the model is complicated it may be complicated to compute the gradient of the final loss with respect to the parameter and and we can check that pythort is able to do that very nicely so in the case in our case the model is just a simple linear model which and we have one dimensional input at an output so also a is a number so in our case the loss for one point can be just computed at so it was the norm of the distance between the norm of the difference between the model prediction and the true value so now everything is one dimensional so this is just a square and then we can substitute our model which is a fine a x plus b and compute the square and we give we get these nice formula for the loss of one point and then the gradient can be computed explicitly so with respect to the parameter so with respect to a we got this thing and with respect to b we get this thing so these are just real numbers and we can check that this is done that this is the same result that we get by pythons so to do all this stuff automatically by pythons we can do the following so we first define a random input so these are these x and y so these are one-dimensional so this is one sample of the one-dimensional input and one sample of the one-dimensional output okay and then the process is just by implemented by these three lines so first we set the gradient to zero so we tell uh the model to like clear out everything that was stored before in the gradient then we evaluate the loss exactly as you expect so the loss function then we take the model prediction so the forward model and we compare it with the expected output and then everything that we need to do is to this is to call this backward method and this is just doing uh is just telling the model that the next time when i compute the gradient of something i mean the gradient of the loss so like here when i compute the gradient with respect to a what i mean is i want the gradient with respect to l okay and l is the loss so i just start to the model stop memorizing stuff at this level and indeed if we check what if that's what's happening so we can uh so let's evaluate this first so we can compute so after we call this backward all the variables all the parameters within our model so recall that we had the weight and the bias okay now they have also a field grade grad okay and we can show that and compare it with the formula that we computed before so this is exactly two x times uh blah blah blah this is exactly that and the same for the other one and indeed you see that we got the same result so just to recap instead of doing any calculation by add which can be done here but in general it's almost impossible to do you can just define any model would have any kind of internal operation even crazy one and it can depend on million or billion of parameters but then you just need to define a loss compute the loss on some input data call this backward mode method and automatically the the library is assigning to every parameter within your model is assigning a gradient so you can see what is the gradient of the loss with respect to this parameter okay so this is almost magic because you don't need to compute any gradient by hand okay and now with this one we can do optimization so first i do it by hand so it's just doing uh it's just implementing this this thing here by hand so i just specify a learning a fixed learning rate and i run 200 iteration now forget about all this stuff which is the saving thing but all that i do is just to take the uh here is a bit complicated to get out the parameter of the motor but it's because the idea is that you should not do that so everything all this stuff is done automatically in a few line below okay but essentially what i'm doing is just taking the w and the b and the gradients of respect to w and to b and i just update the model according to minus the direction of the gradient with both okay don't look at this too much i just want to show you a picture and this is working so you see my initial point was right here and then you see the blue line is just the evolution of the parameters going down to the close to an option okay and so once again this can be done here because everything is very one-dimensional it's very small very easy but in the general case by tors it does these automatically so uh before showing you how easy is to implement this with pytorch and how better it works i just want to recall another concept which is this idea of stochastic gradient descent so what we had before was that every time that so what when we compute the gradient we are really computing the gradient of the loss which is defined of the full training set which is defined as the average over the entire data set okay but there are cases where that is not uh possible or for sure is not efficient because it may be that we don't have the full data set into memory or even if we have that we don't want to compute that so we need to compute the motor for every one of the element of the data set and then computing the average so it may be that it takes too long just to compute the value of your loss so instead it's very common to use stochastic gradient descent so what we do is to take at every iteration just to draw some a batch of elements out of these of the data set and we just compute the loss as the average over that view point okay and then we compute we up we update our parameters according to the gradient of the loss computed over only over those small samples and this is exactly the place where we need the batches that we had before so if you remember at the beginning we had this way to to get in an automatic way batch of data so instead of getting all the data set we just had the model give us back we we we asked the train loader to get back every time this bunch of data okay and this is exactly used for that so we iterate over the train loader we get a bunch of data and using only those data we compute where are we we compute the loss only for where is it here we compute the loss only for those few data there okay so we can compute we can upgrade the parameters with our approximated gradient so it's kind of a monte carlo approximation of the full gradient but it's very fast because we don't need to wait until the model computes the output for every input okay so with that said we wish we can see our so the last point so our torch i torch does a full optimization and that is much easier than doing it by hand so essentially the the third building block that is implemented or fourth so we are data set models losses and now we have optimizers so if you go to the corresponding dock page you can see that source provides already a lot of optimizers so here we are using this item which is the same one with the same parameter that used antonio last time and all we need to do is to construct an object of the of this class of the class of this optimizer and we just need to tell the optimizer what are the parameters that need to be optimized so we just pass the all the parameters of the model so this is very nice because if the model in this case these parameters are just the weight of the bias but if we have a model with billion of parameters when we call this method parameters the the code make it takes care of giving of passing the correct thing here okay so we don't need to write anything explicitly and then there are parameters the learning rate and the weight decay these are just internals that are used by this optimizer to decide about the step and things like that and then that's done we can run the code so we just specified a number of ebooks so the number of times that we want to run over the entire data set and that this is the full optimization so don't look at this it's just used to keep track of something to have plots later on but the optimizer is all here so we start by so this result method that is the one that i didn't show you before this is just initializing the weight to random values again at the beginning of the optimization and this is putting the model in train mode and at the end of the optimization in evaluation mode here is not very important but this is the full optimization so we just run for this number of ebooks over the data set and every time we iterate over the data set using the trained loader so every time batch is just that batch of data and so at every iteration all we have to do is to first uh put the gradient to zero then we just compute the loss of so what is written above so we just evaluate the model on the input on given by the batch and compare it via the loss to the output so batch the first component of the batch then we we call this backward model and that's and then all we have to do is just to stop the optimizer so we call the method step on the optimizer so every optimizer defined within a torch optim as this method step and this is taking care of doing updating all the parameters according to the gradients that are computed by the code using clever ways to compute the correct step and everything okay so this is really this is the full training of the model uh without essentially any intervention on how the model is represented internally okay now this is just reporting saving store the history so the values and uh plotting something so we can run it so we have something missing above yep so initialize the model and then we can run it so it's very easy so it's already trained but as you can see so we are printing uh the epoch so you see the first epoch is given by the nine iterations so this is every epoch runs the batches so we had nine above and they are always nine and you see the the behavior of the loss which is decaying not monotonically so for example here is still monotonic at some point it will not be so it may decrease and increase because of that stochastic optimization of the model and then we have the second batch and so on blah blah blah and so this is just for visualization visualization and now finally we can visualize what happened so okay so this is what happened so we have uh the first figure is the behavior of the evolution of the coefficient now you see that the trajectory is a bit more crazy and this but it goes straight to the minimum and is not straight but it goes to the minimum and this is the trajectory computed by this adam optimizer okay so that's a very good one and it's very good in descending much more difficult losses than this one so this is very easy but it goes right there and this is the value of the loss so you see that indicates very nicely so this is a log log plot you see that it appears very nicely to a plateau level and as you can imagine as you can expect this is not monotonic because okay if you look at the evolution of the parameter this is not monotonically going so it's not monotonically descending the loss and that is happening exactly because of the stochastic computation of the gradient so we don't we don't have the correct direction at every point we just have an approximation okay but it goes down very nicely and it stays there even with some oscillation and finally you can see on the right so the blue points are the data set the black line is the final computed model and the orange one are starting from up here so this is the full evolution of the motor so every time that we move the coefficient we have a corresponding model we are computing the output so this is very expensive to do with a real motor but here everything is easy and you see that the model first goes down here then goes up and then here the color is is a bit darker because the model keeps oscillating around here so here we don't see it because it's very close to the optimum but here you see that the loss keeps oscillating so we have a sequence of uh lines bouncing around that so we can see another so we can initialize again the model just to see what if we get a nice arrangement so this is strain takes a bit to plot yeah so i don't know if this is better or not but you see that the behavior depends on where we start and a bunch of things the loss may be highly oscillating but still we got the correct output okay so this is pretty much everything so as you can see as you have seen i hope there are four these four fundamental building blocks and they offer a very standardized way to implement any kind of model data loss and optimization method and at the end so except for defining a way to load the data which is a bit so you need to do it by hand at some point but then everything is very automated so we could have written all these optimizers just in these few lines and this takes care of doing everything so okay that's it and i don't know if you have any question if you're still alive thank you gabriele for the presentation i will ask a question i don't know if you know the answer but when you define the weights and the and the bias at the beginning i think what happened if you okay here what happened if you set the parameters uh the flag require grads equal to forward what do you mean happened yeah no this in general so in general when you create a tensor so let's put it this way see the tensor in general may require another gradient because you can use the tensor to do any kind of thing so if you want a tensor to represent a number for a plot you don't need a gradient now when you create a model i think that automatically every so the default is every is that every internal parameter as a gradient as as this flag true because what you want to do what happens is that later on when you pass you have an optimizer so here you pass the model parameter to the optimizer okay and then you have this step method so this step method updates all the internal coefficient that had that flag set to true so you want the parameter to change so you want that flag to be equal to to be true i think that there may be special cases where you don't want the optimizer to update the parameter so you can turn it off so you can set it to not requiring the gradient okay so for example if you have a pre-trained model and you know that some coefficients should stay that way i don't know and you want to update only a part or in some kind of optimization method where at every iteration you just update some parameters not all of them so there are situations like that but i think the default is that every parameter is set to requiring the gradient because you want to optimize it during the optimization thank you i was exactly thinking to pre-trained models but thanks for the questions there for the answer thank you other questions
Up Next

Graph Neural Networks: Theoretical Foundations Explained | GNN Theory
@petarvelickovic6033
107.3K views•2021-02-22

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

Graph Data Handling in PyTorch Geometric | Part 1 Tutorial
@94longa2112
19.7K views•2021-05-25

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science

























![[M2L 2024] Graph Neural Networks - Helena Andres](https://i.ytimg.com/vi/4FQABJQVWkI/maxresdefault.jpg)
![[250923] GNN](https://i.ytimg.com/vi/cLcGwgbbKj8/maxresdefault.jpg)









