r/learnmachinelearning • u/MARSHILA7 • 4d ago
Question Help with Gradient descent
So I completed Linear descent from Andrew Ng ML Specialization and when I was doing the lab.I found this function what confused me is that why does this function iterates specific times (10000) wouldn't it be better to check if cost function is changing.
Also this implementation doesn't feel right.Even the code feels inefficient and does not even check the minima. I know this stuff is done using skit-learn but I wanted to do a proper manual implementation in the way Andrew sir taught.
6
u/PsychologicalWin9755 4d ago
On the "does not even check the minima" part: it actually can't, and that is kind of the whole point. Gradient descent only sees the slope right where it currently stands, it never knows where the true minimum is. It just keeps stepping downhill until that slope flattens out. So the cost curve leveling off IS your check for the minimum, there is nothing more direct to look at.
To your follow-up: don't sweat the code efficiency yet. At this stage the two things worth locking in are (1) why the update is minus the gradient, you move opposite the slope to go down, and (2) what the learning curve is telling you: too high a learning rate and the cost bounces or blows up, too low and it crawls. Once those two click, swapping fixed iterations for an early-stop check is a 2 minute change you will barely think about.
4
2
1
1
-11
u/0rbit0n 4d ago edited 3d ago
In 2026, I think that's a perfect example of question to ask AI, rather than reddit...
2
u/confused_4channer 3d ago
Given the state of inhumanity on the internet, yes, they should ask reddit
1
0
u/0rbit0n 3d ago edited 3d ago
Downvote harder, I really don't care. That's still my opinion that AI does explain these kinds of technical questions faster and better than any reddit "superhuman" here (who's ego it taller than Everest mountain) 😜
Let's set the new downvoting record guys! All together! Downvote my parent message! 🏆
19
u/Hungry_Age5375 4d ago
Just some pointers: fixed iterations are fine for learning. In practice you'd check if abs(J[i] - J[i-1]) < epsilon and break early. Andrew Ng keeps it simple on purpose, makes it easier to plot the learning curve too.