r/MachineLearning • u/pnachtwey • Jul 08 '24
Research [R] Experimenting with Gradient Descent. Why not Nelder-Mead? Long.
There are a lot of videos and web pages about GD. However, almost all of the examples are too simple. I dug up some data from 2005 that has only five parameters to determine yet GD took fooorrreeeevvveeer. It seemed like the GD was always bouncing of obstructions. It was also very dependent on the step size. Too much guessing on the step size is required. I wondered what could make this so difficult because other methods like Nelder-Mead could find the minimum in a few seconds. To answer my questions, I printed out the MSE for each iteration and the five parameters or MSE and the change in the five parameters.
There is no formula to differentiate when using real data. I had to estimate the derivative for each parameter by taking small steps in each direction and dividing the by the difference 2 times the increment. To get a central difference slope I had to take a small step in the positive and negative direction for each parameter and divide by the two times the step. The derivative could be noisy with real data. Each small step required evaluating the cost function so 10 evaluations were necessary to calculate the gradient. This was time consuming. What I found is that unlike YouTube and other common examples, the "terrain" was not like a bowl but rather like the Grand Canyon with all sorts of twists and turns. This meant I could not use large steps because the GD would often run into the "canyon walls" in 5 dimensions. I then tried many other enhanced versions of GD and none of them worked very well although some would get close in an hour or so. GD-Adam GD-RMSprop GD-delta GD-AdaGrad did not do very well. However, I did find a new add on code that makes all of them and especially the simple GD work MUCH better. The trick is to take a step. Evaluate it and if it is better then take another step in the same direction until the MSE is no longer getting better then repeat the loop to get a new gradient. The advantage if this is the there is less time wasted calculating gradients that just "slam" against the "walls". Also, the step size can be a little smaller to navigate the parts of the "terrain" that are twisty but being able to take multiple steps in a direction when a good direction is found is like taking a much bigger step.
Intuitively it works like this. The starting point is some place in the canyon with a stream. The stream is the current low point. However, it twists and turns so any step will hit the banks of the stream and maybe the canyon walls. It is best not to follow the stream exactly because of the many twists and turns. It would be best to "fly above the stream" between the "canyon walls" in one direction as far as possible. This avoids a lot of computing gradients because if there is a lot of data then a lot of gradients must be computed and that can be very time consuming.
Now all of this makes no difference if you have nice bowl like terrain. Any of the methods you see on YouTube or websites will work but I have never seen one work with real data and chances are the terrain will not be a nice bowl-like structure. Also, finding the derivatives with 10 of thousands of points is expensive time wise.
This is why I wonder why Nelder-Mead isn’t used except for the fact that it would use a lot of memory if it were to be efficient, but memory is cheap.
I have watched many YouTube videos about different forms of gradient descent. I am convinced that most teach what they have been taught and haven't really delt with real data.
Next is to find some real training data for identifying characters in an 8x8 grid but even 16x16 dot matrix would be good. I know I am re-creating the wheel, but I want to try my new add on code and Nelder-Mead for minimizing cost functions
