forked from metamath1/noviceml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplane.html
583 lines (508 loc) · 24.1 KB
/
plane.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<html>
<head>
<meta charset="UTF-8">
<!--link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"-->
<link rel="stylesheet" type="text/css" href="style/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="style/style.css"/>
<link href="http://cdn.jsdelivr.net/gh/joungkyun/font-d2coding/d2coding.css" rel="stylesheet" type="text/css">
<style>
.relative3 {
position: relative;
top: -120px;
left: 60px;
}
</style>
</head>
<body>
<div class="container" style="border:0px solid #FF0000;">
<div class="row">
<div class="col-6" style="border:0px solid #FF0000;margin-top:5px;">
<div class="card" style="border:1px solid #CCCCCC;margin-top:5px;">
<div class="card-body">
<h4 class="card-title func-title">f(x,y) = ax + by + c</h4>
<div id="graph1" style="height:680px;border:0px solid #0000FF;margin-top:10px;"></div>
</div>
</div>
<div class="relative3">
<div class="form-group">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="check-opacity">
<label class="custom-control-label" for="check-opacity">Opacity</label>
</div>
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="check-samples">
<label class="custom-control-label" for="check-samples">Sample</label>
</div>
</div>
<!-- -5,-4,-3,-2,-1,0,1,2,3,4,5-->
<div class="form-group">
<label for="xy-plane" class="form-label">f(x,y)=const.</label>
<input type="range" class="form-range" min="-3" max="3" step="1" value="0" id="xy-plane">
</div>
<div class="form-group">
<button type="button" class="btn btn-primary btn-sm" id="learn">Learn</button>
<button type="button" class="btn btn-danger btn-sm" id="stop">Stop</button>
</div>
</div>
</div>
<div class="col-6" style="border:0px solid #FF0000;margin-top:5px;">
<div class="row">
<div class="col-12" style="margin:0px;border:0px solid #AABB00;">
<div class="card" style="border:1px solid #CCCCCC;margin-top:5px;">
<div class="card-body">
<h4 class="card-title func-title">ax + by + c = <span id="fc_value">0</span></h4>
<div id="graph-dx" style="height:400px;border:0px solid #0000FF;margin-top:10px;"></div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12" style="margin:0px;border:0px solid #AABB00;">
<form class="customizing">
<div class="card" style="border:0px solid #FF0000;margin-top:5px;">
<div class="card-body">
<h4 class="card-title func-title-small">a = <span id="coef_a_value"></span></h4>
<input type="range" min="-100" max="100" value="0" class="slider" id="coef_a" style="width:100%;">
</div>
</div>
<div class="card" style="border:0px solid #FF0000;margin-top:5px;">
<div class="card-body">
<h4 class="card-title func-title-small">b = <span id="coef_b_value"></span></h4>
<input type="range" min="-100" max="100" value="0" class="slider" id="coef_b" style="width:100%;">
</div>
</div>
<div class="card" style="border:0px solid #FF0000;margin-top:5px;">
<div class="card-body">
<h4 class="card-title func-title-small">c = <span id="coef_c_value"></span></h4>
<input type="range" min="-200" max="200" value="0" class="slider" id="coef_c" style="width:100%;">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="https://cdn.plot.ly/plotly-1.43.2.min.js"></script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
<script>
if (!String.prototype.format) {
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined'
? args[number]
: match
;
});
};
}
/*
* numpy.linspace() 함수의 구현
* https://gist.github.com/joates/6584908
*/
var linspace = function(a, b, n) {
if(typeof n === "undefined") n = Math.max(Math.round(b-a)+1,1);
if(n<2) { return n===1 ? [a] : []; }
var i,ret = Array(n);
n--;
for(i=n; i>=0; i--) { ret[i] = (i*b+(n-i)*a)/n; }
return ret;
}
/*
* cost : function
*
*/
var cost = function(x1, x2, params = [2, 1, 0]) {
/******************************************
* default function
******************************************/
var a = params[0];
var b = params[1];
var c = params[2];
return a*x1 + b*x2 + c;
/******************************************
* user define function
******************************************/
// return x1*x2
}
/*
* calc. surface function value Z for drawing cost function
*/
var draw_surface = function(f, X, Y, params) {
var i, j;
//console.log(params);
var grid_size_x = X.length;
var grid_size_y = Y.length;
var Z = new Array(grid_size_y);
for(i = 0; i < grid_size_y; i++) {
Z[i] = new Array(grid_size_x);
for(j = 0; j < grid_size_x; j++)
Z[i][j] = f(X[j], Y[i], params);
}
return Z;
}
$( document ).ready(function() {
var GRAPH1 = document.getElementById('graph1');
var GRAPH_DX = document.getElementById('graph-dx');
// 함수의 정의역을 설정
// 함수가 바뀌면 여기 정의역을 적당히 조정해주면 됨
var min_x = -5; var max_x = 5;
var min_y = -4; var max_y = 4;
// 샘플 (버튼에 따라 보였다 안보였다 하게 하기)
var sample_pos = [1,-1]
var sample_neg = [2,-2]
// 그림을 그릴 스탭을 설정 값이 크면 정밀하게 그려지나 속도 느려짐
var step = 2;
var scale = 50;
var a = parseFloat($('#coef_a').val()) / scale;
var b = parseFloat($('#coef_b').val()) / scale;
var c = parseFloat($('#coef_c').val()) / scale;
var X = linspace(min_x, max_x, step);
var Y = linspace(min_y, max_y, step);
var Z = draw_surface(cost, X, Y, [a, b, c]);
var timer_id = -1;
var surface = {
type: 'surface',
z: Z, x: X, y: Y,
showscale:false,
colorscale: 'Portland', opacity:1.0, hoverinfo:'x+y+z',
name: 'surface',
contours: { x:{ show:false, highlight:true, color:'#348ABD', width:1,
highlightcolor:'#348ABD', highlightwidth:5},
y:{ show:false, highlight:true, color:'#A60628', width:1,
highlightcolor:'#A60628', highlightwidth:5},
z:{ show:false, highlight:false, }
}
};
var xyplane = {
type: 'surface',
z: Z, x: X, y: Y,
showscale:false,
opacity:0.7, hoverinfo:'x+y+z',
name: 'surface',
contours: { x:{ show:false, highlight:true, color:'#348ABD', width:1,
highlightcolor:'#348ABD', highlightwidth:5},
y:{ show:false, highlight:true, color:'#A60628', width:1,
highlightcolor:'#A60628', highlightwidth:5},
z:{ show:false, highlight:false, }
}
};
z_zero = X.map( function(value, index) {return 0;} );
y = X.map( function(value, index) {return -(a/b)*value-(c/b);} );
var line_3d = {
type: 'scatter3d',
mode: 'lines',
x: X, y: y, z: z_zero,
opacity: 1,
line: {
width: 6,
color: '#313552',
reversescale: false
}
};
// https://colorhunt.co/
var positive_3d = {
x: [sample_pos[0]],
y: [sample_pos[1]],
z: [0],
name: 'pos',
mode: 'markers',
type: 'scatter3d',
visible: false,
marker: {
size: 5,
color: '#B8405E',
line: {
color: 'rgb(0, 0, 0)',
width: 1
},
}
}
var negative_3d = {
x: [sample_neg[0]],
y: [sample_neg[1]],
z: [0],
name: 'pos',
mode: 'markers',
type: 'scatter3d',
visible: false,
marker: {
size: 5,
color: '#2EB086',
line: {
color: 'rgb(0, 0, 0)',
width: 1
},
}
}
var f_pos_z = a*1 + b*(-1) + c
var pos_to_the_plane = {
x: [sample_pos[0], sample_pos[0]],
y: [sample_pos[1], sample_pos[1]],
z: [0, f_pos_z],
name: 'pos',
mode: 'lines',
type: 'scatter3d',
visible: false,
line: {
width: 4,
color: '#313552',
},
marker: {
size: 5,
color: '#B8405E',
}
}
var f_neg_z = a*2 + b*(-2) + c
var neg_to_the_plane = {
x: [sample_neg[0], sample_neg[0]],
y: [sample_neg[1], sample_neg[1]],
z: [0, f_neg_z],
name: 'neg',
mode: 'lines',
type: 'scatter3d',
visible: false,
line: {
width: 4,
color: '#313552',
},
marker: {
size: 5,
color: '#B8405E',
}
}
var line = {
x: X,
y: y,
name: 'f(x)',
mode: 'lines',
marker: {color:'#313552'},
};
var positive_2d = {
x: [sample_pos[0]],
y: [sample_pos[1]],
name: 'pos',
mode: 'markers',
type: 'scatter',
visible: false,
marker: {
size: 12,
color: '#B8405E',
line: {
color: 'rgb(0, 0, 0)',
width: 1
},
}
}
var negative_2d = {
x: [sample_neg[0]],
y: [sample_neg[1]],
name: 'neg',
mode: 'markers',
type: 'scatter',
visible: false,
marker: {
size: 12,
color: '#2EB086',
line: {
color: 'rgb(0, 0, 0)',
width: 1
},
}
}
var layouts = {
'3d-square' : {
hovermode:'closest',
spikedistance:-1,
margin: {
l: 33, r: 20, b: 33, t: 20, pad: 0
},
scene:{
xaxis:{showspikes:false, range: [-4, 4], title:'x Axis'},
yaxis:{showspikes:false, range: [-4, 4], title:'y Axis'},
zaxis:{showspikes:false, range: [-4, 4], title:'z Axis'},
aspectratio:{x:1, y:1, z:1},
},
showlegend: false
},
'2dx' : {
// 2차원 그래프의 높이 범위를 여기서 설정
// 함수에 맞게 적당한 범위를 적어준다.
xaxis: {range: [-5, 5], title:'x Axis'},
yaxis: {range: [-5, 5], title:'y Axis'},
margin: {
l: 33, r: 20, b: 33, t: 20, pad: 0
},
},
};
Plotly.plot(GRAPH1, [surface, line_3d, pos_to_the_plane, neg_to_the_plane,
xyplane, positive_3d, negative_3d, ],
layouts['3d-square']);
Plotly.plot(GRAPH_DX, [line, positive_2d, negative_2d], layouts['2dx']);
// 곡면의 투명도를 조정하는 이벤트 핸들러
$("#check-opacity").change(function () {
if ($(this).prop("checked")) {
Plotly.restyle(GRAPH1, 'opacity', 0.8, 0);
return;
}
Plotly.restyle(GRAPH1, 'opacity', 1.0, 0);
});
// 샘플 보이기를 조정하는 이벤트 핸들러
$("#check-samples").change(function () {
if ($(this).prop("checked")) {
var update = {
visible: [true, true, true, true],
}
Plotly.restyle(GRAPH1, update, [2, 3, 5, 6]);
update = {
visible: [true, true]
}
Plotly.restyle(GRAPH_DX, update, [1, 2]);
return;
}
var update = {
visible: [false, false, false, false],
}
Plotly.restyle(GRAPH1, update, [2, 3, 5, 6]);
update = {
visible: [false, false]
}
Plotly.restyle(GRAPH_DX, update, [1, 2]);
});
// xy-plane 높이를 조절하는 이벤트 핸들러
$('#xy-plane').on("change mousemove", function() {
fc = parseFloat($(this).val());
$('#fc_value').html(fc);
// GRAPH1에서 xy-plane, line_3d 업데이트
Z_fc = draw_surface(cost, X, Y, [0, 0, fc]);
z_zero = X.map( function(value, index) {return fc;} );
y = X.map( function(value, index) {return -(a/b)*value-((c-fc)/b);} );
var update = { y:[y, Y], z: [z_zero, Z_fc] };
Plotly.restyle(GRAPH1, update, [1, 4,]);
// GRAPH_DX에서 line 업데이트
update = { y: [y] }
Plotly.restyle(GRAPH_DX, update, 0);
});
$('#coef_a').on("change mousemove", function() {
a = parseFloat($(this).val()) / scale;
$('#coef_a_value').html(a);
fc = parseFloat($("#xy-plane").val());
// redraw
// surface, line_3d, xyplane
Z = draw_surface(cost, X, Y, [a, b, c]);
y = X.map( function(value, index) {return -(a/b)*value-((c-fc)/b);} );
// to_the_plane
f_pos_z = a*sample_pos[0] + b*sample_pos[1] + c
f_neg_z = a*sample_neg[0] + b*sample_neg[1] + c
var update = {
y: [Y, y, [sample_pos[1],sample_pos[1]], [sample_neg[1],sample_neg[1]]],
z: [Z, z_zero, [0,f_pos_z], [0,f_neg_z]]
}
Plotly.restyle(GRAPH1, update, [0, 1, 2, 3]);
update = { y: [y] }
Plotly.restyle(GRAPH_DX, update, 0);
});
$('#coef_b').on("change mousemove", function() {
b = parseFloat($(this).val()) / scale;
$('#coef_b_value').html(b);
fc = parseFloat($("#xy-plane").val());
// redraw
Z = draw_surface(cost, X, Y, [a, b, c]);
y = X.map( function(value, index) {return -(a/b)*value-((c-fc)/b);} );
// to_the_plane
f_pos_z = a*1 + b*(-1) + c
f_neg_z = a*2 + b*(-2) + c
var update = {
y: [Y, y, [sample_pos[1],sample_pos[1]], [sample_neg[1],sample_neg[1]]],
z: [Z, z_zero, [0,f_pos_z], [0,f_neg_z]]
}
Plotly.restyle(GRAPH1, update, [0,1,2,3]);
update = { y: [y] }
Plotly.restyle(GRAPH_DX, update, 0);
});
$('#coef_c').on("change mousemove", function() {
c = parseFloat($(this).val()) / scale;
$('#coef_c_value').html(c);
fc = parseFloat($("#xy-plane").val());
// redraw
Z = draw_surface(cost, X, Y, [a, b, c]);
y = X.map( function(value, index) {return -(a/b)*value-((c-fc)/b);} );
// to_the_plane
f_pos_z = a*1 + b*(-1) + c
f_neg_z = a*2 + b*(-2) + c
var update = {
y: [Y, y, [sample_pos[1],sample_pos[1]], [sample_neg[1],sample_neg[1]]],
z: [Z, z_zero, [0,f_pos_z], [0,f_neg_z]]
}
Plotly.restyle(GRAPH1, update, [0,1,2,3]);
update = { y: [y] }
Plotly.restyle(GRAPH_DX, update, 0);
});
$("#stop").click(function () {
console.log("stop");
if(timer_id != -1) {
clearInterval(timer_id);
}
});
$("#learn").click(function () {
console.log("learn");
train_x = [sample_pos, sample_neg];
train_y = [1, -1];
eta = 0.01;
epochs = 5000;
timer_id = setInterval( function (){
correct = true;
for(j=0; j < 2; j++) {
x_j = train_x[j][0]
y_j = train_x[j][1]
target_j = train_y[j]
score = a*x_j + b*y_j + c
// unit step
if(score > 0){
o_j = 1;
} else{
o_j = -1;
}
console.log(j, score, a, b, c)
if(o_j != target_j) correct = false;
dc = eta*(target_j - o_j) // const, c
da = eta*(target_j - o_j)*x_j // x, a
db = eta*(target_j - o_j)*y_j // y, b
c += dc;
a += da;
b += db;
// slider update
$('#coef_a').val(a*scale)
$('#coef_a_value').html(a.toPrecision(6));
$('#coef_b').val(b*scale)
$('#coef_b_value').html(b.toPrecision(6));
$('#coef_c').val(c*scale)
$('#coef_c_value').html(c.toPrecision(6));
}
fc = parseFloat($("#xy-plane").val());
// redraw
Z = draw_surface(cost, X, Y, [a, b, c]);
y = X.map( function(value, index) {return -(a/b)*value-((c-fc)/b);} );
// to_the_plane
f_pos_z = a*1 + b*(-1) + c
f_neg_z = a*2 + b*(-2) + c
var update = {
y: [Y, y, [sample_pos[1],sample_pos[1]], [sample_neg[1],sample_neg[1]]],
z: [Z, z_zero, [0,f_pos_z], [0,f_neg_z]]
}
Plotly.restyle(GRAPH1, update, [0,1,2,3]);
update = { y: [y] }
Plotly.restyle(GRAPH_DX, update, 0);
// stop criterion
if (correct == true) {
console.log('break')
clearInterval(timer_id);
}
}, 50);
});
});
</script>
</body>
</html>