Skip to content

Instantly share code, notes, and snippets.

@marcouberti
Last active February 13, 2017 18:35
Show Gist options
  • Save marcouberti/1ef7cb046d7302630c5efad08a34c0ea to your computer and use it in GitHub Desktop.
Save marcouberti/1ef7cb046d7302630c5efad08a34c0ea to your computer and use it in GitHub Desktop.
Simple Gradient Descent example in JS
/*
Our function f(x)
*/
function f(x) {
return x*x +5;
}
/*
The derivative df/dx of our function f(x)
*/
function df(x) {
return 2*x;
}
/*
Gradient Descent
*/
function gradientDescent() {
var x = Math.random()*10000;
var learning_rate = 0.1;
var epochs = 100;
var gradient;
for(var e=1; e<epochs; e++) {
gradient = df(x);
x -= learning_rate * gradient;
console.log("f(x) = "+f(x).toFixed(2)+" x = "+x.toFixed(2));
}
}
// run
gradientDescent();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment