-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword2vecattempt.html
More file actions
263 lines (232 loc) · 7.39 KB
/
word2vecattempt.html
File metadata and controls
263 lines (232 loc) · 7.39 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>word2vec, kinda</title>
<script type="text/javascript"
src="lib/vector.js"></script>
<script type="text/javascript"
src="lib/matrix.js"></script>
<script type="text/javascript"
src="lib/nn.js"></script>
<script type="text/javascript"
src="lib/div.js"></script>
<script type="text/javascript"
src="lib/drawbase.js"></script>
</head>
<body style="background-color:black; overflow: hidden;">
<div class="input"
style="position: absolute; top: 0; left: 0;">
<textarea name="input"
id="input"
cols="30"
rows="10">the fast monkey is brown
the slow turtle is green
the monkey is fast and brown
the turtle is slow and green </textarea>
<button id="go">go</button>
</div>
<script type="text/javascript">
let height = window.innerHeight;
let width = window.innerWidth;
let words = '';
let dict = {};
let wordcount = 0;
let mainloop = 0
let running = false
let brain = {}
id('go').addEventListener('click', () => {
if (running) {
id('go').innerText = 'go'
clearInterval(mainloop)
running = false
} else {
running = true
words = id('input').value
data = prepare(words);
createBrain()
requestAnimationFrame(draw)
id('go').innerText = 'stop'
mainloop = setInterval(draw, 17)
}
})
function createBrain() {
if (Object.keys(brain).length > 0) return
brain = new NeuralNetwork(wordcount);
brain.addLayer(5, "dense", { //x, y , h, s, v
activation_fn: "linear"
});
brain.addLayer(wordcount, "dense", {
activation_fn: "softmax"
});
brain.config.loss_fn = "cross_entropy";
brain.init();
}
function prepare(data) {
let ws = 3;
let bw = ["a", "is", "the", "are", "and", 'than', 'in'];
let sent = data.split("\n");
let joined = sent.map(s => {
let t = s.split(" ").map(x => x.split('').filter(x => ![',', '.'].includes(x)).join(''));
return t.filter(w => {
return !bw.includes(w);
});
});
let allwords = [];
let occs = Array(joined.length ** 2).fill(0);
joined.forEach(w => {
w.forEach(ww => {
if (!allwords.includes(ww)) {
allwords.push(ww);
}
occs[allwords.indexOf(ww)] += 1;
});
});
occs.splice(allwords.length);
let testdata = [];
wordcount = allwords.length;
allwords.forEach((w, i) => {
dict[w] = i;
dict[i] = w;
let input = w;
let outputs = [];
joined.forEach(line => {
if (line.includes(w)) {
for (
j = line.indexOf(w) - ws; j <= line.indexOf(w) + ws; j++
) {
if (line[j] && line[j] !== w) {
outputs.push(line[j]);
}
}
}
});
outputs.forEach(x => {
testdata.push({
input: w,
output: x
});
});
});
testdata = shuffle(testdata);
//console.log(JSON.stringify(testdata))
return testdata;
}
let p = new P("Loss: ", 10, 20);
let data = []
let count = 0;
function getData() {
let x = data[count].input;
let y = data[count].output;
x = dict[x];
y = dict[y];
x = enumerate(x, wordcount);
y = enumerate(y, wordcount);
count++;
if (count > wordcount - 1) {
count = 0;
data = shuffle(data);
}
return {
input: x,
output: y
};
}
function enumerate(n, max) {
let m = max || 8;
let a = Array(m).fill(0);
a[n] = 1;
return a;
}
function denumerate(arr) {}
function argmax(arr) {
return arr.indexOf(arr.reduce((a, b) => Math.max(a, b)));
}
function train() {
let d = getData();
brain.train(d.input, d.output);
p.shape.innerText =
"Loss: " + ((brain.calc_loss() * 100) | 0) / 100;
}
let ps = [];
function draw() {
ps.forEach(p => {
p.remove();
});
ps = [];
loop(train, 50);
let w2v = brain.layers[0].weight_array[0][0].copy().transpose()
.values;
let max = w2v.reduce((a, b) => Math.max(rmax(a), rmax(b)), [
0,
0,
]);
let min = w2v.reduce((a, b) => Math.min(rmin(a), rmin(b)), [
0,
0,
]);
w2v = w2v.map(pair => {
return pair.map(v => {
return normalize(v, min, max);
});
});
let newdict = toDict(w2v);
Object.keys(newdict).forEach(key => {
let p = newdict[key];
let x = p[0] * (width - 40);
let y = p[1] * (height - 130) + 100;
let w = new P(key, x, y);
w.shape.style.fontSize = "2rem"
w.shape.style.color =
"hsl(" +
p[2] * 360 +
"," +
p[3] * 100 +
"%," +
p[4] * 100 +
"%)";
w.shape.style.textShadow =
"hsl(" +
p[2] * 360 +
"," +
p[3] * 100 +
"%," +
(100 - (p[4] * 100)) +
"%) 0px 0px 2px";
ps.push(w);
});
// if (running) {
// requestAnimationFrame(draw)
// }
};
function normalize(n, min, max) {
return (n - min) / (max - min);
}
function rmax(arr) {
if (arr instanceof Array) {
return arr.reduce((a, b) => Math.max(a, b));
} else {
return arr;
}
}
function rmin(arr) {
if (arr instanceof Array) {
return arr.reduce((a, b) => Math.min(a, b));
} else {
return arr;
}
}
function toDict(arr) {
let obj = {};
arr.forEach((x, i) => {
let w = dict[i];
obj[w] = x;
});
return obj;
}
function shuffle(array) {
return array.sort(() => Math.random() - 0.5);
}
</script>
</body>
</html>