Skip to content

Instantly share code, notes, and snippets.

@danglingfarpointer
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danglingfarpointer/2d425339ea54d4140668 to your computer and use it in GitHub Desktop.
Save danglingfarpointer/2d425339ea54d4140668 to your computer and use it in GitHub Desktop.
Sample program of the Widrow-Hoff learning
/*
Sample program of the Widrow-Hoff learning
Copyright (c) 2014 danglingfarpointer
This program is distributed under the MIT License:
http://opensource.org/licenses/mit-license.php
*/
var signs = [
1, 1, 1, 1, 1, -1, -1, -1, -1, -1,
];
var samples = [
// [c, x, y]; c is always set to 1
[1, -2, 1],
[1, -1, 4],
[1, 1, 3],
[1, 3, 3],
[1, -1, 3],
[1, -3, -1],
[1, 0, -1],
[1, 2, 1],
[1, 3, 2],
[1, 0, 3],
];
var N = samples.length; // the number of samples
var D = samples[0].length; // dimension
// console.log(signs);
// console.log(samples);
// initialize
var ws = [];
for (var d = 0; d < D; ++d)
ws[d] = Math.random();
while(true) {
var js = []; // gradient of LMS
for (var d = 0; d < D; ++d) js[d] = 0;
for (var n = 0; n < N; ++n) {
var p = 0;
for (var d = 0; d < D; ++d)
p += samples[n][d] * ws[d];
for (var d = 0; d < D; ++d)
js[d] += (p - signs[n]) * samples[n][d];
}
// if js is small enough, exit the loop
if (norm2(js) < 0.001) break;
console.log("ws[0] = " + ws[0]);
console.log("ws[1] = " + ws[1]);
console.log("ws[2] = " + ws[2]);
console.log("js[0] = " + js[0]);
console.log("js[1] = " + js[1]);
console.log("js[2] = " + js[2]);
console.log("");
// update the weight vector
for (var d = 0; d < D; ++d)
ws[d] -= 0.01 * js[d];
}
console.log((- ws[1] / ws[2]) + " * x + " + (- ws[0] / ws[2]));
function norm2(vs) {
var c = 0;
for (var d = 0; d < D; ++d) c += vs[d] * vs[d];
return c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment