-
Notifications
You must be signed in to change notification settings - Fork 3
/
SimpleLinearClassification.html
314 lines (249 loc) · 8.02 KB
/
SimpleLinearClassification.html
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple Linear Classification</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript"
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<style type="text/css">
<!--
body { background-color:#ededed; font:norm2al 12px/18px Arial, Helvetica, sans-serif; }
h1 { display:block; width:600px; margin:20px auto; paddVing-bottom:20px; font:norm2al 24px/30px Georgia, "Times New Roman", Times, serif; color:#333; text-shadow: 1px 2px 3px #ccc; border-bottom:1px solid #cbcbcb; }
#container { width:600px; margin:0 auto; }
#myCanvas { background:#fff; border:1px solid #cbcbcb; }
#nav { display:block; width:100%; text-align:center; }
#nav li { display:block; font-weight:bold; line-height:21px; text-shadow:1px 1px 1px #fff; width:100px; height:21px; paddVing:5px; margin:0 10px; background:#e0e0e0; border:1px solid #ccc; -moz-border-radius:4px;-webkit-border-radius:4px; border-radius:4px; float:left; }
#nav li a { color:#000; display:block; text-decoration:none; width:100%; height:100%; }
-->
</style>
</head>
<script>
//-----------------------simple graphing code
function Graph(context, center_x, center_y, zoom)
{
this.context = context;
this.center_x = center_x;
this.center_y = center_y;
this.zoom = zoom;
this.ViewportX = function(x) { return (x*this.zoom + this.center_x); }
this.ViewportY = function(y) { return (-y*this.zoom + this.center_y); }
this.InvViewportX = function(vx) { return ((vx - this.center_x)/this.zoom); }
this.InvViewportY = function(vy) { return (-(vy - this.center_y)/this.zoom); }
this.moveTo = function(x,y) { this.context.moveTo(this.ViewportX(x),this.ViewportY(y)); }
this.lineTo = function(x,y) { this.context.lineTo(this.ViewportX(x),this.ViewportY(y)); }
this.fillText = function (str, x,y ) { this.context.fillText(str,this.ViewportX(x),this.ViewportY(y)); }
this.DrawAxis = function ()
{
this.context.beginPath();
this.context.strokeStyle="#000000";
this.moveTo( -100,0);
this.lineTo( 100,0);
this.moveTo( 0, -100);
this.lineTo( 0, 100);
this.context.closePath();
this.context.stroke();
for(var i=-30;i<30;i++)
{
this.context.font="15px ti92pluspc";
this.fillText(i,i,0);
this.fillText(i,0,i);
}
}
this.DrawLine = function (x1,y1,x2,y2)
{
this.context.beginPath();
this.moveTo(x1,y1);
this.lineTo(x2,y2);
this.context.closePath();
this.context.stroke();
}
this.DrawPlus = function (x,y,radius)
{
this.context.beginPath();
this.context.moveTo(this.ViewportX(x) - radius, this.ViewportY(y));
this.context.lineTo(this.ViewportX(x) + radius, this.ViewportY(y));
this.context.moveTo(this.ViewportX(x), this.ViewportY(y) - radius);
this.context.lineTo(this.ViewportX(x), this.ViewportY(y) + radius);
this.context.closePath();
this.context.stroke();
}
this.DrawCircle = function (x,y,radius)
{
this.context.beginPath();
this.context.arc(this.ViewportX(x), this.ViewportY(y), radius, 0 , 2 * Math.PI);
this.context.stroke();
}
}
var gfx;
//----------------------Point distribution generation and drawing
var axis_a;
var axis_b;
var points = []
var target = []
function LinearF(x,y)
{
return (axis_a*x-y+axis_b)>0?1:0;
}
function RandomizeLinearF()
{
var px = Math.random()
var py = Math.random()
axis_a = (py-.5)/(px-.5);
axis_b = (1-axis_a)*.5;
points = []
target = []
}
//------------------ Compute and draw a set
function GenerateDataSet(func)
{
for(var i=0;i<20;i++)
{
var x = Math.random()
var y = Math.random()
points.push([x, y])
target.push(func(x, y))
}
}
function DrawF()
{
x1 = -10; y1 = axis_a*x1+axis_b
x2 = 10; y2 = axis_a*x2+axis_b
gfx.DrawLine( x1,y1,x2,y2)
}
function DrawPoints(context, radius)
{
for(var i=0;i<points.length;i++)
{
var x = points[i][0];
var y = points[i][1];
if (target[i]==0)
{
gfx.DrawPlus(x,y,radius);
}
else
{
gfx.DrawCircle(x,y,radius);
}
}
}
//----------------------network
function Act(x)
{
return 1.0/(1.0+Math.exp(-5*x));
}
function DerAct(x)
{
return Act(x)*(1-Act(x));
}
var w1=0;
var w2=0;
var w3=0;
function net(w1,w2, w3, i1,i2)
{
return w1*i1+w2*i2+w3;
}
function dnetdw1(x,y) { return x; }
function dnetdw2(x,y) { return y; }
function dnetdw3(x,y) { return 1; }
function DrawDecisionBoundary(gfx)
{
var canvasData = gfx.context.getImageData(0, 0, 600, 600);
for(var y=0;y<600;y++)
{
for(var x=0;x<600;x++)
{
var index = (x + y * 600) * 4;
var xx = gfx.InvViewportX(x);
var yy = gfx.InvViewportY(y);
var res = Act(net(w1,w2,w3, xx,yy));
var v = res>.5?255:0;
canvasData.data[index + 0] = v;
canvasData.data[index + 1] = 255-v;
canvasData.data[index + 2] = 0;
canvasData.data[index + 3] = 128;
}
}
gfx.context.putImageData(canvasData, 0, 0);
}
//------------------------learning loop
var epoch = 0;
function iterate()
{
var learningRate=.1;
for(var i=0;i<100;i++)
{
epoch++;
var totalErr = 0;
for(var pindex=0;pindex<points.length;pindex++)
{
var p = points[pindex];
var i1 = p[0];
var i2 = p[1];
//compute real value
t = target[pindex]
// e = ( s(net(w1,w2,i1,i2))-t)^2
n = net(w1,w2,w3, i1,i2)
o = Act(n);
//compute mean square error
totalErr += (o-t)*(o-t);
// de = 2*(s(net)-t) * s'(net) * (dg/da, dg/db)
de = 2*(o-t) * DerAct(n);
//update weights with dE
w1 += -learningRate * de * dnetdw1(i1,i2);
w2 += -learningRate * de * dnetdw2(i1,i2);
w3 += -learningRate * de * dnetdw3(i1,i2);
}
// if g is close to t then restart
if (Math.abs(totalErr)<.1)
{
epoch = 0;
w1 = 2*Math.random()-1;
w2 = 2*Math.random()-1;
w3 = 2*Math.random()-1;
RandomizeLinearF()
GenerateDataSet(LinearF)
break;
}
}
DrawDecisionBoundary(gfx);
context.strokeStyle="#000000";
gfx.DrawAxis()
DrawPoints(gfx,10);
//draw the plane used to generate the points
//DrawF(context);
document.getElementById("text").innerHTML = "<br>Epoch:"+ epoch + "<br>Error: " + Math.abs(totalErr) + "<br>Coeff a: " + w1 + "<br>Coeff b: " + w2 + "<br>Coeff c: " + w3 + "<br>";
}
function init()
{
var myCanvas = document.getElementById("myCanvas");
context = myCanvas.getContext('2d');
context.clearRect(0,0,600,600);
RandomizeLinearF()
GenerateDataSet(LinearF)
gfx = new Graph(context, 30, 600-30, 600*.9);
setInterval(iterate,10);
}
</script>
<body onload="init()">
<h1>AI, 2x1 Classifier</h1>
<div id="container">
<canvas id="myCanvas" width="600" height="600"></canvas>
<div id="text"></div>
<h2>Intro</h2>
This a demo classifies 2 types of points. The points can be separated by a plane. We are training a neuron to find the plane that divides the set.
</br>
</br>
Note that without a bias we'd only be able to come up with planes that pass by the origin.
</br>
</br>
<h2>Contact/Questions:</h2>
<my_github_account_username>[email protected]$.
</br>
</br>
</div>
</body>
</html>