# Gradient Descent - how many values are calculated in loss function?

I'm a little bit confused how loss function is calculated in neural network training. There's is said that in theory when using Grid Search or Monte Carlo methods we can calculate all the possible loss function values. But obviously this requires too many resources and is not a good way for neural network training. Alternatively, when using Gradient Descent we have a possibility to evaluate a single value to know in which direction we should go in order to test for the next value. We then could climb down the ladder one step by another until we reach the optimal value.

But on the other hand, in the following PyTorch example there is said that the loss function is calculated on the basis of all predicted and real values. And after that the gradient is calculated. So, what's the point of Gradient Descent in this way when all the loss values in previous step are calculated anyway?

In supervised learning, at each training step the predictions of the Network are compared with the atcual, true results. The value of a loss function is calcualated, which is telling your model "how wrong it is". At this point, the weights of the Network must be updated accordingly.

In order to do that, a formula based on the chain rule of derivatives calculates retrospectively the contribution of each weight to the final loss value. The value of each weight is then changed, based on their impact on the loss function (mathematically, it's a first partial derivative for each weight). This process is called backpropagation, since it logically starts from the bottom of the Network and is computed backwards up to the input layer.

This process has to be done for each of the Network's learnable weights. The higher the number of parameters, the higher the number of partial derivatives that are computed at each training iteration for the weight update. When all the weights are updated, the position of the Gradient will (hopefully) be a small step closer to the global minimum of the loss function.

Coming to your doubts: grid search on the loss function values is something you can do only in theory, not in practice. It could take an impossible amount of time to do that, even for the most powerful computers. At the moment, the Gradient descent algorithm is the main tool to train Neural Networks. As far as I know, other methods such as Monte Carlo and Genetic algorithms are feasible but not practical, therefore not state-of-the-art.

I suggest you two great posts to understand how Gradient descent and backprop work: this one by Andrej Karpathy and this one by Colah.

• So what's the difference between Grid Search and Gradient Descent if in both cases all the loss values are calculated for each individual data point? If we already know the loss value for each individual data point, then why not just take the minimum of them? Commented Sep 23, 2019 at 11:52
• Grid search and Gradient descent are very different things. Gradient descent is an optimization technique that starts from a random point of the objective function, and moves iteratively on it trying to find the global minimum. Grid search on the other side is a much more rough and simple technique: it means "try n possible values on the parameter space and see what's better than others". It's a very inefficient, time consuming, brute force approach that in practice doesn't make training possible. (You can use grid search for hyperparameter tuning, but that's a completely different thing.) Commented Sep 23, 2019 at 12:27
• I could understand the difference if the Gradient Descent takes only single random loss value as a starting point and then iteratively finds optimum path. But as I understand before calculating gradients the complete hyperparameter space for all the loss values are calculated. And therefore I can't see where the effect is achieved compared to Grid Search. They both calculate loss values for each individual data point. Confusing. Commented Sep 23, 2019 at 13:14
• "before calculating gradients the complete hyperparameter space for all the loss values are calculated." Sorry this information is not correct. It's impossible to explore the either the full hyperparameter and the parameter spaces. Is it clear for you the difference between hyperparameters and parameters? Commented Sep 23, 2019 at 13:16
• Ok, maybe I didn't express myself correctly. What's my point is that if in Gradient Descent the loss values are calculated for each individual data point then it could also take enormous amount of resources. Haven't still got an answer in which way it differs from Grid Search in this respect (both calculate all possible loss values). Commented Sep 23, 2019 at 13:32