Skip to content
Advertisement

Simple Linear Regression not converging

In my attempt to dig deeper in the math behind machine learning models, I’m implementing a Ordinary Least Square algorithm in Python, using vectorization. My references are:

This is what I have now:

JavaScript

The problem I’m facing is that my weights keep increasing until I end up getting a bunch of nans. I’ve been trying to find out what I’m missing, but so far no luck. Also tried to tweak the tolerance threshold, but I don’t think that’s the issue, but something wrong with my math.

Advertisement

Answer

Your code seems actually to work fine; except for learning rate, really! Just reduce it from 0.01 to e.g. 0.0001 and everything works fine (well, I would also reduce tolerance to something much much smaller, like 1e-5, to make sure it actually converges to the right solution).

Small image showing that it works:

JavaScript

gives

plot result

Linear regression is a convex optimization problem, so you can imagine it like putting a ball on a parabola and then moving it towards the bottom by a fixed amount of space multiplied by the slope of the position you’re at. If that “fixed amount” is small enough, you get closer and closer to the bottom, until you find the optimum position. But if you get the value too large, you jump from one side of the parabola to the other, and if it’s large enough you land in a place which is actually higher than where you started from. Iterate this a few times and you get indeed in the exact situation you had…

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement