-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtoyclassifier2.html
467 lines (423 loc) · 21.5 KB
/
toyclassifier2.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
<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>
tr {
line-height: 2px;
min-height: 2px;
height: 2px;
}
</style>
</head>
<body style="background-color:#F9F9F9;">
<div class="container" style="border:0px solid #FF0000;">
<div class="row" style="margin-top:10px;">
<div class="col">
<div class="card">
<div class="card-body">
<table id="data" class="d2 table" style="margin-bottom: 0px;">
<thead>
<tr>
<th>강아지 이미지</th>
<th>강아지 벡터</th>
<th>고양이 이미지</th>
<th>고양이 벡터</th>
</tr>
</thead>
<tr>
<td rowspan='10'><img id="puppy" src="img/welsh-corgi.jpg" width="160" style="cursor:pointer;"/></td>
<td class="puppy">0</td>
<td rowspan='10'><img id="kitten" src="img/kitten.jpg" width="160" style="cursor:pointer;"/></td>
<td class="kitten">1</td>
</tr>
<tr>
<td class="puppy">1</td><td class="kitten">0</td>
</tr>
<tr>
<td class="puppy">0</td><td class="kitten">0</td>
</tr>
<tr>
<td class="puppy">0</td><td class="kitten">1</td>
</tr>
<tr>
<td class="puppy">1</td><td class="kitten">1</td>
</tr>
<tr>
<td class="puppy">1</td><td class="kitten">1</td>
</tr>
<tr>
<td class="puppy">1</td><td class="kitten">1</td>
</tr>
<tr>
<td class="puppy">0</td><td class="kitten">1</td>
</tr>
<tr>
<td class="puppy">0</td><td class="kitten">1</td>
</tr>
<tr>
<td class="puppy">0</td><td class="kitten">0</td>
</tr>
</table>
</div>
</div><!--card-->
</div><!--col-->
</div><!--row-->
<div class="row" style="margin-top:10px;margin-bottom: 10px;">
<div class="col-8"><!--표있는 col-->
<div class="card">
<div class="card-body">
<div class="container">
<div class="row" style="margin-top:10px;">
<div class="col">
<table id="forward" class="d2 table table-hover" style="margin-bottom: 0px;">
<thead>
<tr class="table-dark">
<th scope="row"></th>
<th scope="col">x</th>
<th scope="col">w</th>
<th scope="col">w<sub>i</sub>x<sub>i</sub></th>
<!--th scope="col">z</th>
<th scope="col">σ(z)</th>
<th scope="col">-log(σ)</th-->
</tr>
</thead>
<tr id="data-x1" class="target">
<th scope="row">x<sub>1</sub></th>
<td></td>
<td><input class="form-control form-control-sm" type="number" step="0.001" placeholder="" value="0.700"></td>
<td></td>
</tr>
<tr id="data-x2" class="target">
<th scope="row">x<sub>2</sub></th>
<td></td>
<td><input class="form-control form-control-sm" type="number" step="0.001" placeholder="" value="-0.820"></td>
<td></td>
</tr>
<tr id="data-x3" class="target">
<th scope="row">x<sub>3</sub></th>
<td></td>
<td><input class="form-control form-control-sm" type="number" step="0.001" placeholder="" value="0.740"></td>
<td></td>
</tr>
<tr id="data-x4" class="target">
<th scope="row">x<sub>4</sub></th>
<td></td>
<td><input class="form-control form-control-sm" type="number" step="0.001" placeholder="" value="0.540"></td>
<td></td>
</tr>
<tr id="data-x5" class="target">
<th scope="row">x<sub>5</sub></th>
<td></td>
<td><input class="form-control form-control-sm" type="number" step="0.001" placeholder="" value="0.260"></td>
<td></td>
</tr>
<tr id="data-x6" class="target">
<th scope="row">x<sub>6</sub></th>
<td></td>
<td><input class="form-control form-control-sm" type="number" step="0.001" placeholder="" value="0.990"></td>
<td></td>
</tr>
<tr id="data-x7" class="target">
<th scope="row">x<sub>7</sub></th>
<td></td>
<td><input class="form-control form-control-sm" type="number" step="0.001" placeholder="" value="-0.330"></td>
<td></td>
</tr>
<tr id="data-x8" class="target">
<th scope="row">x<sub>8</sub></th>
<td></td>
<td><input class="form-control form-control-sm" type="number" step="0.001" placeholder="" value="0.060"></td>
<td></td>
</tr>
<tr id="data-x9" class="target">
<th scope="row">x<sub>9</sub></th>
<td></td>
<td><input class="form-control form-control-sm" type="number" step="0.001" placeholder="" value="-0.860"></td>
<td></td>
</tr>
<tr id="data-x10" class="target">
<th scope="row">x<sub>10</sub></th>
<td></td>
<td><input class="form-control form-control-sm" type="number" step="0.001" placeholder="" value="-0.360"></td>
<td></td>
</tr>
</table>
</div>
<div class="col">
<table id="forward2" class="d2 table table-hover">
<thead>
<tr class="table-dark">
<th scope="col">z</th>
<th scope="col">σ(z)</th>
<th scope="col" id="log-sigma">-log(σ)</th>
</tr>
</thead>
<tr>
<td id="z">1</td>
<td id="sigma">1</td>
<td id="log">1</td>
</tr>
</table>
<div class="card">
<div class="card-body">
<h6 class="card-subtitle mb-2 text-muted">z는 w<sub>i</sub>x<sub>i</sub>의 총합</h6>
<p class="card-text">`z=\sum_{i=1}^{10} w_i x_i`</p>
<h6 class="card-subtitle mb-2 text-muted">로지스틱 시그모이드 함수</h6>
<p class="card-text">`\sigma(z)=\frac{1}{1+e^{-z}}`</p>
<!--a href="#" class="card-link">Card link</a>
<a href="#" class="card-link">Another link</a-->
</div>
</div>
<p>
<div id="sign-board" class="card text-white bg-secondary mb-3" style="max-width: 20rem;">
<!--div class="card-header">Header</div-->
<div class="card-body">
<h4 id="pred" class="card-title"></h4>
<p class="card-text">강아지 확률:<span id="prob-puppy"></span>, 고양이 확률:<span id="prob-kitten"></span></p>
</div>
</div>
</p>
</div>
</div>
</div>
</div>
</div>
</div><!--col-->
<div class="col-4">
<div class="card">
<div class="card-body">
<h4 class="card-title">-log(σ) 강아지 손실</h4>
<div id="log-cost1" style="width:300px;height:230px;border:0px solid #FF0000;margin-top:10px;"></div>
</div>
</div>
<div class="card" style="margin-top:3px;">
<div class="card-body">
<h4 class="card-title">-log(1-σ) 고양이 손실</h4>
<div id="log-cost0" style="width:300px;height:230px;border:0px solid #FF0000;margin-top:10px;"></div>
</div>
</div>
</div>
</div><!--row-->
</div>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://cdn.plot.ly/plotly-latest.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>
$( document ).ready(function() {
/*
* 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;
}
var GRAPH = document.getElementById('log-cost1');
var GRAPH0 = document.getElementById('log-cost0');
var data = {'puppy':[], 'kitten':[]};
var domain = linspace(0.001, 1.2, 100);
var logit = 0.0;
var likelihood = 0.0;
var loss = 0.0;
var target = '';
var yhat2label = {'puppy':'강아지', 'kitten':'고양이'};
var selected = -1;
// 테이블에서 데이터 가져오기
$('#data tr').each(function() {
$(this).find('td').each (function() {
if($(this).html()==0 || $(this).html()==1){
// console.log($(this).attr('class'));
data[$(this).attr('class')].push($(this).html());
}
});
});
// data는 다음과 같은 사전형
// {puppy: Array(10), kitten: Array(10)}
// kitten: (10) ["1", "0", "0", "1", "1", "1", "1", "1", "1", "0"]
// puppy: (10) ["0", "1", "0", "0", "1", "1", "1", "0", "0", "0"]
var loss_graph1 = {
x: domain,
y: domain.map(function(x){return -Math.log(x);}),
name: 'cost function', showlegend: false
};
var loss_graph0 = {
x: domain,
y: domain.map(function(x){return -Math.log(1. - x);}),
name: 'cost function', showlegend: false
};
var loss1 = {
x: [likelihood],
y: [loss],
name: 'data loss',
mode: 'markers',
marker: {
color: '#B8405E',
size: 13,
line: {
color: 'rgb(0, 0, 0)',
width: 2
}
},
type: 'scatter',
visible: true, showlegend: false
};
var loss0 = {
x: [likelihood],
y: [loss],
name: 'data loss',
mode: 'markers',
marker: {
color: '#2EB086',
size: 13,
line: {
color: 'rgb(0, 0, 0)',
width: 2
},
},
type: 'scatter',
visible: true, showlegend: false
};
var cost = [loss_graph1, loss1];
var cost0 = [loss_graph0, loss0];
var layout1 = {
margin: {
l: 33, r: 20, b: 33, t: 10, pad: 0
},
xaxis: {range: [-0.2, 1.2], title:'σ'},
yaxis: {range: [-0.2, 6], title:'-log(σ)'},
legend: {
x: 0.5,
y: 1,
traceorder: 'normal',
bgcolor: '#E2E2E2',
bordercolor: '#E2E2E2',
borderwidth: 2
}
};
var layout0 = {
margin: {
l: 33, r: 20, b: 33, t: 10, pad: 0
},
xaxis: {range: [-0.2, 1.2], title:'σ'},
yaxis: {range: [-0.2, 6], title:'-log(1-σ)'},
legend: {
x: 0.5,
y: 1,
traceorder: 'normal',
bgcolor: '#E2E2E2',
bordercolor: '#E2E2E2',
borderwidth: 2
}
};
Plotly.plot(GRAPH, cost, layout1);
Plotly.plot(GRAPH0, cost0, layout0);
function input_data(s) {
for(j=0; j < data[s].length; j++)
{
//$('#forward').tr(j).td(0).html()
tr = $('tr', $('#forward')).eq(j+1);
$('td', tr).eq(0).html(data[s][j]);
}
target = s;
}
function wixi(){
$('#forward tr').each(function (i, row){
if(i > 0) {
// console.log(i, row);
xi = $('td', $(this)).eq(0).html();
wi = $('td', $(this)).eq(1).children()[0];
wx = xi * $(wi).val();
$('td', $(this)).eq(2).html(wx);
// console.log(xi);
}
});
}
function z(){
logit = 0.0;
$('#forward tr').each(function (i, row){
if(i > 0) {
logit += parseFloat($('td', $(this)).eq(2).html());
}
});
tr = $('tr', $('#forward2')).eq(1);
$('td', tr).eq(0).html(logit.toFixed(3));
return logit;
}
function sigma(logit){
tr = $('tr', $('#forward2')).eq(1);
likelihood = 1. / (1. + Math.exp(-logit))
$('td', tr).eq(1).html(likelihood.toFixed(3));
return likelihood;
}
function pred() {
if (likelihood >= 0.5) return 'puppy';
else return 'kitten';
}
function pred_update(){
y_hat = pred();
$("#pred").html(yhat2label[y_hat]);
$("#prob-puppy").html(likelihood.toFixed(3));
$("#prob-kitten").html((1.0-likelihood).toFixed(3));
if(target == y_hat) $("#sign-board").removeClass('bg-secondary bg-danger bg-success').addClass('bg-success');
else $("#sign-board").removeClass('bg-secondary bg-danger bg-success').addClass('bg-danger');
}
function nll(likelihood, c){
tr = $('tr', $('#forward2')).eq(1);
loss = -Math.log(likelihood)
$('td', tr).eq(2).html(loss.toFixed(3));
// console.log(likelihood);
// console.log(loss);
// 로스가 업데이트되었으면 여기서 그래프 업데이트 한다.
if(c == "puppy"){
Plotly.restyle(GRAPH, {x:[[likelihood]], y:[[loss]], visible:true}, 1);
// 고양이 마커 죽이기
Plotly.restyle(GRAPH0, {x:[[(likelihood - 1)*-1]], y:[[loss]], visible:false}, 1);
} else {
Plotly.restyle(GRAPH0, {x:[[(likelihood - 1)*-1]], y:[[loss]], visible:true}, 1);
//강아지 마커 죽이기
Plotly.restyle(GRAPH, {x:[[likelihood]], y:[[loss]], visible:false}, 1);
}
// 예측도 여기서 한다.
pred_update();
return loss;
}
$("#puppy").click(function(){
input_data('puppy');
wixi();
loss = nll(sigma(z()), "puppy");
// 그림 테두리
$("#puppy").css("border", "3px solid #CCCCCC");
$("#kitten").css("border", "0px solid #CCCCCC");
selected = 1;
$("#log-sigma").html("-log(σ)")
});
$("#kitten").click(function(){
input_data('kitten');
wixi();
loss = nll(1.0 - sigma(z()), "kitten");
$("#puppy").css("border", "0px solid #CCCCCC");
$("#kitten").css("border", "3px solid #CCCCCC");
selected = 0;
$("#log-sigma").html("-log(1-σ)")
});
$("input").on("change keyup paste", function() {
// var currentVal = $(this).val();
wixi();
if(selected == 1){
loss = nll(sigma(z()), "puppy");
} else {
loss = nll(1.0 - sigma(z()), "kitten");
}
});
});
</script>
</body>
</html>