Skip to content

Instantly share code, notes, and snippets.

@panchishin
Last active December 31, 2019 00:13
Show Gist options
  • Save panchishin/30ed942f6c7277410e9711693733a452 to your computer and use it in GitHub Desktop.
Save panchishin/30ed942f6c7277410e9711693733a452 to your computer and use it in GitHub Desktop.
An example of machine learning from https://youtu.be/wUPFOFbCeFI
let ys_and_xs = [ [1, 3.8], [2, 1.9], [3, 2.9],
[4, 4.5], [5, 6.4], [6, 3.5],
[7, 6.7], [8, 6.2], [9, 8.0] ];
function grade(a, b) {
let error = 0;
for ( [x,y] of ys_and_xs ) {
error += Math.pow( a*x + b - y , 2 )
}
return error
}
function someChange() {
return Math.random() - .5
}
let a = 1;
let b = 0;
let err = grade(a,b);
for( let i=0; i<100000; i++) {
let new_a = a + someChange()
let new_b = b + someChange()
let new_err = grade(new_a, new_b)
if ( new_err < err ) {
a = new_a
b = new_b
err = new_err
console.log("a =", a, ", b =", b)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment