Last active
February 13, 2017 18:35
-
-
Save marcouberti/1ef7cb046d7302630c5efad08a34c0ea to your computer and use it in GitHub Desktop.
Simple Gradient Descent example in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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