-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
200 lines (155 loc) · 5.38 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body style="margin: 0; padding: 0;">
<canvas id="canvas" style="border: 1px solid #000;"></canvas>
<script type="text/javascript">
var weights = [0, 0.9, 0.2];
var learningRate = 0.001;
var LearningOrderPointer = 0;
var LearningOrder = [
1, 1,
-1, 1,
1, -1,
-1, -1];
var LearningComplete = 0;
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ResizeCanvas();
// canvas init
// минимальная сторона холста
var min = Math.min(canvas.width, canvas.height);
// отступ в 10% от длинны минимальной стороны холста
var margin = min / 10;
// условная единица на грфике
var S = (min - 2 * margin) / 4;
// центр канваса, координаты 0, 0
var X0 = canvas.width / 2;
var Y0 = canvas.height / 2;
NextSample();
function ResizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
function Redraw() {
// очистить холст
ClearCanvas();
// горизонтальная ось координат
DrawLine(new Plot(-2, 0), new Plot(2, 0));
// вертикальная ось координат
DrawLine(new Plot(0, -2), new Plot(0, 2));
var A = weights[1];
var B = weights[2];
var C = weights[0];
for (var x = -1; x <= 1; x += 2) {
for (var y = -1; y <= 1; y += 2) {
var result = A * x + B * y + C;
var brush = result < 0 ? false : true;
DrawCircle(new Plot(x, y), brush);
}
}
console.log({
A: A,
B: B,
C: C
})
// нормирвание длинн
sum = Math.sqrt(A * A + B * B);
A /= sum;
B /= sum;
C /= sum;
A *= 2;
B *= 2;
C /= 2;
lineCenter = new Plot(-C * A, -C * B);
DrawLineNative(lineCenter.x - S * B, lineCenter.y - S * A, lineCenter.x + S * B, lineCenter.y + S * A);
DrawLineNative(lineCenter.x, lineCenter.y, lineCenter.x + (50 * A), lineCenter.y - (50 * B));
}
function Plot(x, y) {
this.x = X0 + S * x;
this.y = Y0 - S * y;
}
function DrawLine(plot1, plot2) {
ctx.beginPath();
ctx.moveTo(plot1.x, plot1.y);
ctx.lineTo(plot2.x, plot2.y);
ctx.closePath();
ctx.stroke();
}
function DrawLineNative(x1, y1, x2, y2) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.closePath();
ctx.stroke();
}
function DrawCircle(plotCenter, fill) {
ctx.beginPath();
ctx.arc(plotCenter.x, plotCenter.y, 5, 0, 2 * Math.PI);
if (fill) ctx.fill();
ctx.closePath();
ctx.stroke();
}
function ClearCanvas() {
// Store the current transformation matrix
ctx.save();
// Use the identity matrix while clearing the canvas
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Restore the transform
ctx.restore();
}
function NextSample() {
var x = LearningOrder[LearningOrderPointer];
var y = LearningOrder[LearningOrderPointer + 1];
LearningOrderPointer += 2;
if (LearningOrderPointer >= LearningOrder.length) LearningOrderPointer = 0;
var input = [x, y];
var output = [x > 0 && y > 0 ? 1 : -1];
// обучение на конкретном примере
learningRun(input, output);
// отрисовать результат
Redraw();
if (LearningComplete < 5) {
// следующий набор обучающих данных
setTimeout(NextSample, 1);
} else {
console.log('Complete');
// window.location = window.location;
}
}
function learningRun(input, expectedOutput) {
// взвешенна сумма произведения вектоа весов и вектора входных значений
var s = weights[0] + weights[1] * input[0] + weights[2] * input[1];
// применяем функцию активации к взвешенной сумме
var output = Math.sign(s);
// ошибка
var e = expectedOutput - output;
// если ошибка есть
if (e != 0) {
// меняем веса
weights[0] += learningRate * e;
weights[1] += learningRate * e * input[0];
weights[2] += learningRate * e * input[1];
LearningComplete = 0;
}
else
{
LearningComplete++;
}
//console.log(weights);
logResult(input[0], input[1], output, expectedOutput, s)
}
function logResult(x1, x2, output, expected, sum) {
css = output == expected ? 'background: #0f0; padding: 3px;' : 'background: #f00; padding: 3px;';
console.log('%c' + x1 + ' and ' + x2 + ' = ' + output + ' expexted(' + expected + ')' + ' sum(' + sum + ')', css);
}
</script>
</body>
</html>