-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
164 lines (158 loc) · 4.01 KB
/
worker.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
importScripts(
"./modules/neuralNetwork/neuralNetwork.js",
"./modules/idxConverter/idxConverter.js"
);
const handleMessage = (e) => {
switch (e.data.label) {
case "init":
problems[selected].load();
break;
case "config":
Object.keys(e.data.changes).forEach(
(key) => (neuralNetwork[key] = e.data.changes[key])
);
break;
case "start":
if (neuralNetwork.stop) {
neuralNetwork.stop = false;
neuralNetwork.train({
...neuralNetwork.trainingConfig,
...problems[selected].train,
});
}
break;
case "pause":
neuralNetwork.stop = true;
break;
case "step":
neuralNetwork.stop = true;
neuralNetwork.train({
...neuralNetwork.trainingConfig,
...problems[selected].train,
});
break;
case "reset":
neuralNetwork.resetData();
problems[selected].load();
break;
case "state":
self.postMessage({
label: "state",
layers: neuralNetwork.layers,
info: neuralNetwork.data,
});
break;
default:
self.postMessage({ label: "invalid message" });
}
};
self.addEventListener("message", handleMessage);
let selected = "xor";
const problems = {
xor: {
load: () => {
fetch("./trainingData.json")
.then((response) => response.json())
.then((data) => {
setup({ ...problems.xor.layers });
problems.xor.train.dataset = data;
self.postMessage({ label: "ready" });
});
},
layers: {
input: 2,
hidden: [4],
output: 1,
},
train: {
dataset: {},
correctCriteria: (outputs, targets) => {
return Math.round(outputs[0]) === targets[0];
},
},
},
mnist: {
load: () => {
createMnistDataset().then((resp) => {
self.postMessage({ label: "Setting up" });
setup({ ...problems.mnist.layers });
problems.mnist.train.dataset = resp;
self.postMessage({ label: "ready" });
});
},
layers: {
input: 784,
hidden: [128, 64],
output: 10,
},
train: {
dataset: {},
correctCriteria: (outputs, targets) => {
let numGuess = [0, 0];
outputs.forEach((g, i) => {
if (g > numGuess[1]) {
numGuess[0] = i;
numGuess[1] = g;
}
});
let numTarget = targets.indexOf(1);
return numGuess[0] === numTarget;
},
},
},
};
const createMnistDataset = async () => {
const rawLabelsDataset = await convertIdx(
"MNIST/train-labels.idx1-ubyte"
).then((r) => r.rawData);
const rawImagesDataset = await convertIdx(
"MNIST/train-images.idx3-ubyte"
).then((r) => r.rawData);
return formatDataset(rawLabelsDataset, rawImagesDataset);
};
const neuralNetwork = new NeuralNetwork();
function setup({ speed = 0, input = [1], hidden = [1], output = 1 }) {
neuralNetwork.initialize({
speed: speed,
inputNodes: input,
hiddenNodes: hidden,
outputNodes: output,
});
}
/**
* @param {String} location
* @returns {Response}
*/
const fetchFile = async (location) => {
return await fetch(location)
.then((response) => response)
.catch((e) => console.log("Unable to fetch: " + e.message));
};
const formatDataset = (labels, images) => {
const parsedDataset = [];
for (let i = 0; i < labels.length; i++) {
parsedDataset.push({
inputs: normalizeInputs(images.slice(i * 784, i * 784 + 784)),
targets: getTargets(labels[i]),
});
}
console.log("Fetched data files");
return parsedDataset;
};
function normalizeInputs(arr) {
let inputs = [];
for (let i = 0; i < arr.length; i++) {
inputs.push(arr[i] / 255);
}
return inputs;
}
/**
*
* @param {Number} number A number from 0 to 9 representing the label for the MNIST dataset
* @returns {Number[]} an array of 10 numbers with a 1 at the index of the argument number
*/
function getTargets(number) {
let targets = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
targets[number] = 1;
return targets;
}