forked from JohnHaas/annus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_view.js
115 lines (95 loc) · 2.15 KB
/
binary_view.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var perceptron = new Perceptron();
var running = false;
function init() {
data = generateBinaryData(pattern2, 100);
counts = []
for (var j=0; j<20; j++) {
counts[j] = 0
for (var i=0; i<data.length/2; i++) {
counts[j] += data[i][j]
}
}
data2 = generateBinaryData(randomBinaryNumber, 50);
for (var i=0; i<50; i++) {
data[i] = data2[i]
}
error_data = []
var svg = dimple.newSvg("#graph", 500, 450);
chart = new dimple.chart(svg, error_data);
xaxis = chart.addCategoryAxis("x", "Iteration");
chart.addMeasureAxis("y", "Error");
chart.addSeries(null, dimple.plot.line);
xaxis.hidden = true;
redraw();
}
function hello() {
if (running)
return;
running = true;
iterCount = 0;
runInterval = setInterval(tick, 100);
}
function tick() {
iterCount += 1;
if (iterCount > 100) {
clearInterval(runInterval);
running = false;
return;
}
perceptron.iteration(xs, ys);
redraw();
}
function redraw() {
var canvas = document.getElementById('viewer');
var ctx = canvas.getContext('2d');
var w = canvas.width;
var h = canvas.height;
ctx.clearRect (0, 0, w, h);
error = 0
for (var j=0; j<20; j++) {
console.log(counts[j])
}
maxp = 0
probs = []
for (var i=0; i<data.length; i++) {
prob = 1
for (var j=0; j<data[i].length; j++) {
if (data[i][j]) {
prob *= (counts[j] / data.length * 2)
} else {
prob *= (1 - counts[j] / data.length * 2)
}
}
prob = Math.log(prob)
if (prob > maxp) {
maxp = prob
}
probs[i] = prob
}
uniforms = 0
for (var i=0; i<50; i++) {
uniforms += probs[i]
}
uniforms /= 50
for (var i=0; i<data.length; i++) {
y = 4*i;
for (var j=0; j<data[i].length; j++) {
x = 20*j;
ctx.beginPath();
ctx.rect(x, y, 20, 4);
ctx.fillStyle = data[i][j]
? "rgba(0,0,0,0.5)"
: "rgba(255,255,255,0.5)";
ctx.fill();
}
prob = probs[i] / maxp;
ctx.rect(0, y, w, 4);
ctx.fillStyle = "rgba(0,"+Math.floor(prob*255)+",0,0.5)"
ctx.fill();
}
error_data.push({
"Iteration": error_data.length,
"Error": error / data.length
});
chart.draw();
}