-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The regression learns NaN weights with the example code. #1
Comments
Sorry for the mess in the above comment. |
Hi @gursimar Many thanks for notifying me the issue and sharing with me your code, I will look into this and get back to you :) |
Thanks, I think the issue is with the gradient function. |
Many thanks @gursimar , i think you are right. i believed the gradient descent method is very sensitive the values of the learning rate alpha and the regularization lambda. I played with a few small values of alpha and lambda using your code sample, it did converge with NaN issue disappear (if you set a sufficiently small alpha value) but behavior is quite sensitive to the value of alpha indeed, i will consider the closed form. |
Hi @gursimar after some close examination on the linear regression source code, i notice that the gradient calculation has a bug which i have now addressed. I have also added a number html demo on the linear regression: https://rawgit.com/chen0040/js-regression/master/example-regression-3.html The y-intercept is still not as effective, subject to the learning rate and regularization. |
Hi @chen0040, I tried the regression with random data like this;
output is the follwing:
when I change the configuration values for alpha and lambda a bit, I do get different results, but nothing remotely similar to the equation used to generate the data. am I missing something here or is there still a bug present? |
However, if I try a simple model like y= mx+c, then it gives some weights but never gets the y-intercept right. It's always close to zero. I did try playing around with parameters/ increasing data but it still gives the same fit without y-intercept. Seems like there is some bug in the gradient descent method.
Here is the code I tried
<script src="https://rawgit.com/chen0040/js-regression/master/build/jsregression.min.js" type="application/javascript"></script> <script> // === training data generated from y = 2.0 + 5.0 * x + 2.0 * x^2 === // /* var data = []; for(var x = 1.0; x < 100.0; x += 1.0) { var y = 2.0 + 5.0 * x + 2.0 * x * x + Math.random() * 1.0; data.push([x, x * x, y]); // Note that the last column should be y the output } */ var data = []; for(var x = 1.0; x < 10000.0; x += 1.0) { var y = 6.0 + 5.0 * x*0.1 + Math.random() * 1.0; data.push([x*0.1, y]); // Note that the last column should be y the output } // === Create the linear regression === // var regression = new jsregression.LinearRegression({ alpha: 0.00001, // iterations: 30000, lambda: 0.0 }); // can also use default configuration: var regression = new jsregression.LinearRegression(); // === Train the linear regression === // var model = regression.fit(data); // === Print the trained model === // console.log(model); // === Testing the trained linear regression === // /* var testingData = []; for(var x = 1.0; x < 100.0; x += 1.0) { var actual_y = 2.0 + 5.0 * x + 2.0 * x * x + Math.random() * 1.0; var predicted_y = regression.transform([x, x * x]); console.log("actual: " + actual_y + " predicted: " + predicted_y); } */ </script>The text was updated successfully, but these errors were encountered: