-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgd_simple.js
More file actions
46 lines (39 loc) · 924 Bytes
/
Copy pathgd_simple.js
File metadata and controls
46 lines (39 loc) · 924 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function target(x) {
let y = a[0] * x * x + a[1] * x + a[2];
return y;
}
function d_target(x) {
return [x * x, x, 1]; // a[0],a[1],a[2]
}
function estimated(x) {
let y = w[0] * x * x + w[1] * x + w[2];
return y;
}
function err(x) {
let y = estimated(x);
let y_hat = target(x);
let e = 1/2 * (y - y_hat) * (y - y_hat);
return e;
}
let a = [-4.0, 3.5, 1.3];
let w = [0.1, 0.1, 0.1]; // initial values
let alpha = 0.001;
for( let i = 0 ; i < 10000 ; i++ ) {
let E = 0;
let dw = [0, 0, 0];
for( let x = -5 ; x < 5 ; x++) {
let y = estimated(x);
let y_hat = target(x);
let dydw = d_target(x);
let d = (y - y_hat);
for(let i in dw) {
dw[i] += d * dydw[i];
}
E += err(x);
}
for(let i in dw) {
w[i] -= alpha * dw[i];
}
}
console.log("ground truth:" + a);
console.log(" estimated:" + w);