-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
258 lines (227 loc) · 7.97 KB
/
index.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
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
const canvas = document.getElementById("canvas");
const rect = canvas.getBoundingClientRect()
const ctx = canvas.getContext("2d");
const trainingInput = document.getElementById("training");
const letterInput = document.getElementById("letter");
const delay = 50;
const clearButton = document.getElementById("clear");
const output = document.getElementById("out");
let drawing = false;
let interval = null;
let training = false;
trainingInput.onchange = e => {
training = trainingInput.checked;
output.innerText = training ? 'Training mode activated. Any letters drawn will be contributed to local model accuracy. Write carefully!' : 'Draw a letter and a let the model predict!';
}
let x, y;
let lastX, lastY;
let xVels = [];
let yVels = [];
let lettersData = window.localStorage.lettersData ? JSON.parse(window.localStorage.lettersData) : [];
clearButton.onclick = e => {
window.localStorage.lettersData = "";
}
canvas.onmousedown = e => {
x = e.clientX - rect.left;
y = e.clientY - rect.top;
lastX = x;
lastY = y;
xVels = [];
yVels = [];
ctx.fillStyle = "#FF0000";
ctx.fillRect(x, y, 5, 5);
drawing = true;
}
canvas.onmouseup = e => {
drawing = false;
lastX = lastY = null;
if (training) {
let letterIndex = lettersData.findIndex(l => l.letter == letterInput.value);
let letter = lettersData[letterIndex];
if (letter) {
letter.initialData[0].push(xVels);
letter.initialData[1].push(yVels);
} else {
letter = {
letter: letterInput.value,
stateCount: [3, 3],
initialData: [[xVels], [yVels]]
}
lettersData.push(letter);
letterIndex = lettersData.length - 1;
}
lettersData[letterIndex] = trainLetter(lettersData[letterIndex]);
window.localStorage.lettersData = JSON.stringify(lettersData);
}
let prob = -1
let letter;
lettersData.forEach(data => {
let pX = getProbability(xVels, data.stateCount[0], data.transitionProbs[0], data.emissionProbs[0])
let pY = getProbability(yVels, data.stateCount[1], data.transitionProbs[1], data.emissionProbs[1])
let p = pX * pY;
if (p > prob) {
prob = p;
letter = data.letter;
}
});
output.innerText = training ? `Training on ${letter}... uncheck box for prediction` : `Model predicts ${letter}`
if (!training)
if (interval) {
clearInterval(interval);
interval = null;
}
clearCanvas();
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
canvas.onmousemove = e => {
if (drawing) {
x = e.clientX - rect.left;
y = e.clientY - rect.top;
ctx.fillStyle = "#FF0000";
ctx.fillRect(x, y, 5, 5);
if (!interval) {
interval = setInterval(() => {
if (lastX && lastY) {
let dx = (x - lastX) / delay;
let dy = -(y - lastY) / delay;
if (Math.abs(dx) >= 0.01 || Math.abs(dy) >= 0.01) {
xVels.push(dx);
yVels.push(dy);
}
}
lastX = x;
lastY = y;
}, delay);
}
}
}
function meanAndStd (array) {
const n = array.length
const mean = array.reduce((a, b) => a + b) / n
const std = Math.sqrt(array.map(x => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n)
return [mean, std]
}
function train(data, stateCount, cycles = 10) {
data = data.map(d => {
let avg = Math.floor(d.length / stateCount);
let splitUp = [];
while (d.length >= avg && splitUp.length < stateCount) {
splitUp.push(d.splice(0, avg))
}
let origLeftover = d.length;
while (d.length > 0) {
splitUp[origLeftover - d.length].push(d.shift())
}
return splitUp;
});
curMs = []
for (let k = 0; k < cycles; k++) {
curMs = [];
for (let i = 0; i < stateCount; i++) {
let totalState = [];
for (let j = 0; j < data.length; j++) {
totalState.push(...data[j][i])
}
curMs.push(meanAndStd(totalState))
}
data = data.map(d => {
for (let i = 0; i < stateCount; i++) {
if (i != 0) {
while (true) {
if (d[i].length <= 1) {
break;
}
let thisRelation = Math.abs(d[i][0] - curMs[i][0]) / curMs[i][1];
let otherRelation = Math.abs(d[i][0] - curMs[i - 1][0]) / curMs[i - 1][1];
if (thisRelation > otherRelation) {
let removed = d[i].shift();
d[i - 1].push(removed);
} else {
break;
}
}
}
if (i != stateCount - 1) {
while (true) {
if (d[i].length <= 1) {
break;
}
let thisRelation = Math.abs(d[i][d[i].length - 1] - curMs[i][0]) / curMs[i][1];
let otherRelation = Math.abs(d[i][d[i].length - 1] - curMs[i + 1][0]) / curMs[i + 1][1];
if (thisRelation > otherRelation) {
let removed = d[i].pop();
d[i + 1].unshift(removed);
} else {
break;
}
}
}
}
return d;
});
}
let transitionProbs = [];
for (let i = 0; i < stateCount; i++) {
let sumLength = 0;
data.forEach(d => {
sumLength += d[i].length;
})
sumLength /= data.length;
transitionProbs.push([1 / sumLength, 1 - 1 / sumLength])
}
return {
data,
emissionProbs: curMs,
transitionProbs
}
}
function gauss(num, ms) {
let mean = ms[0];
let std = ms[1];
return (2 * Math.PI * std ** 2) ** (-0.5) * Math.exp((-0.5) * (num - mean) ** 2 / (std ** 2));
}
function getProbability(vels, stateCount, transitionProbs, emissionParams) {
let probability = 0.0;
let T = vels.length;
let K = stateCount;
let table = [];
for (let i = 0; i < stateCount; i++) {
table.push(new Array(T).fill(0));
}
for (let j = 0; j < T; j++) {
for (let i = 0; i < K; i++) {
let eProb = gauss(vels[j], emissionParams[i]);
if (j == 0) {
table[i][j] = eProb;
continue;
}
let bestProb = Math.max(table[i][j - 1] * transitionProbs[i][1], i != 0 ? table[i - 1][j - 1] * transitionProbs[i - 1][0] : -1) * eProb;
table[i][j] = bestProb;
}
}
for (let i = 0; i < K; i++) {
if (table[i][T - 1] > probability) {
probability = table[i][T - 1];
}
}
return probability;
}
function trainAll() {
lettersData = lettersData.map(data => {
return trainLetter(data);
});
}
function trainLetter(data) {
let trainX = train(JSON.parse(JSON.stringify(data.initialData[0])), data.stateCount[0])
let trainY = train(JSON.parse(JSON.stringify(data.initialData[1])), data.stateCount[1])
Object.assign(data, {
data: [trainX.data, trainY.data],
emissionProbs: [trainX.emissionProbs, trainY.emissionProbs],
transitionProbs: [trainX.transitionProbs, trainY.transitionProbs]
});
return data;
}
trainAll();
console.log(lettersData)