-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathworker.js
95 lines (84 loc) · 9.69 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
var MLPClassifier = function(hidden, output, layers, weights, bias) {
this.hidden = hidden.toUpperCase();
this.output = output.toUpperCase();
this.network = new Array(layers.length + 1);
for (var i = 0, l = layers.length; i < l; i++) {
this.network[i + 1] = new Array(layers[i]).fill(0.);
}
this.weights = weights;
this.bias = bias;
var compute = function(activation, v) {
switch (activation) {
case 'LOGISTIC':
for (var i = 0, l = v.length; i < l; i++) {
v[i] = 1. / (1. + Math.exp(-v[i]));
}
break;
case 'RELU':
for (var i = 0, l = v.length; i < l; i++) {
v[i] = Math.max(0, v[i]);
}
break;
case 'TANH':
for (var i = 0, l = v.length; i < l; i++) {
v[i] = Math.tanh(v[i]);
}
break;
case 'SOFTMAX':
var max = Number.NEGATIVE_INFINITY;
for (var i = 0, l = v.length; i < l; i++) {
if (v[i] > max) {
max = v[i];
}
}
for (var i = 0, l = v.length; i < l; i++) {
v[i] = Math.exp(v[i] - max);
}
var sum = 0.0;
for (var i = 0, l = v.length; i < l; i++) {
sum += v[i];
}
for (var i = 0, l = v.length; i < l; i++) {
v[i] /= sum;
}
break;
}
return v;
};
this.predict = function(neurons) {
this.network[0] = neurons;
for (var i = 0; i < this.network.length - 1; i++) {
for (var j = 0; j < this.network[i + 1].length; j++) {
for (var l = 0; l < this.network[i].length; l++) {
this.network[i + 1][j] += this.network[i][l] * this.weights[i][l][j];
}
this.network[i + 1][j] += this.bias[i][j];
}
if ((i + 1) < (this.network.length - 1)) {
this.network[i + 1] = compute(this.hidden, this.network[i + 1]);
}
}
this.network[this.network.length - 1] = compute(this.output, this.network[this.network.length - 1]);
var classIdx = 0;
for (var i = 0, l = this.network[this.network.length - 1].length; i < l; i++) {
classIdx = this.network[this.network.length - 1][i] > this.network[this.network.length - 1][classIdx] ? i : classIdx;
}
return classIdx;
};
};
// Parameters:
const layers = [50, 3];
const weights = [[[-0.0553178161584, -0.251624254078, -0.333251978611, -0.131776266328, -0.235492465451, -0.271770107106, -0.209156655163, 0.0150579653029, -0.729879300758, -0.892661060965, -0.0538694985436, -0.222160662223, -0.398995028127, -0.286810101486, -0.315070111539, -0.670399731714, -0.466278544281, -0.552205192916, -0.657535567943, -0.201262590949, 0.336624716125, 0.25425126671, -0.12438197591, 0.220851581214, -0.43004111902, 0.120478214817, -0.276632954882, -0.307292103995, -0.220109681924, 0.196989479795, -0.267764675753, -0.559632230466, -0.660576844403, 0.0221098512973, 0.0543648023039, -0.305312054784, -0.946891875328, -1.14726022731, -0.321136229186, -0.978062464881, 0.126043621957, 0.523104258251, -0.146368429846, -0.118555895663, -0.26451194375, -0.0347371171035, -0.0576959329636, -0.137588460134, -0.141480940366, -0.246643840431], [-0.32041711985, -0.0430589685105, -0.192244977806, -0.156299832842, -0.00561780775801, -0.297753742183, 0.0494109801039, -0.261639308519, -0.197566335787, -0.589253083666, -0.265106319811, -0.289982339213, -0.0419201891703, -0.432904320933, -0.300026431483, -0.486593016638, -0.0815218748116, -0.278429835752, -0.0396226042569, 0.0577024764325, 0.713328820517, -0.363496192548, -0.240478751448, 0.530450272194, -0.597921913205, -0.35631856975, 0.285001354536, -0.101487872218, 0.167205507458, 0.259713888607, 0.255533479806, -0.108564618646, -0.416109500885, -0.100732895628, -0.352348104557, 0.135007487659, -0.592363298023, -0.692845659602, 0.108959329477, -0.534359655419, -0.287496317988, 0.953023141816, -0.0333913995149, -0.214656623013, -0.0612411933833, -0.175312661018, 0.0325026492226, 0.0491189053649, -0.331414705279, 0.0780954127939], [-0.115568295179, -0.288396592908, 0.257290791624, -0.0951520357671, 0.272352595309, 0.0822388174381, -0.322780893657, 0.610895041675, -0.539468957277, 0.66336833632, -0.218436315037, -0.3445911193, 0.269882576378, 0.222853640196, -0.289328786169, -0.160192449168, -0.151882071905, -0.173752117222, 0.120035886346, -0.250482188398, -1.18147602759, -0.116799247838, -0.314457524374, -0.715285586522, 0.473967573927, 0.142993078107, 0.0352141130379, 0.228017102079, -0.250547285302, -0.551911370791, 0.0571719718632, -0.167873148626, -0.358158551922, -0.320896891499, 0.60992105667, -0.229190745888, -0.636336273199, -0.370910450137, 0.242357523875, -0.608469587882, -0.250148096147, -1.03351386372, -0.293383712861, -0.180739259556, -0.303627429923, -0.261666572317, -0.29878288629, 0.14199047854, 0.0398107115382, -0.324957702104], [-0.285346109198, 0.227698729137, 0.0453996125031, -0.19780148025, -0.165113641031, 0.162548079428, -0.203043902312, 0.209848460835, 0.0568541995879, 0.62516653349, -0.173432170554, -0.0229871444522, 0.079240581613, 0.404177663175, -0.228802232048, -0.423370141606, -0.389400035361, -0.173264027386, 0.08842740443, 0.0459002549452, -0.546865303593, 0.428103461806, 0.0531626651371, -0.321038538942, 0.313670958819, 0.249997659289, 0.112820200758, -0.156717894244, -0.289105681782, -0.282536777829, 0.0864770133361, -0.360532693576, 0.0868469927127, -0.288971252558, 0.0598920598413, 0.194120944754, -0.53174460573, 0.0496337403619, 0.0164466208154, 0.00641884765243, -0.265567846171, -0.6568959206, 0.156708241693, 0.251819709481, 0.271873070377, 0.287976968156, -0.367115660374, -0.177089229885, 0.0778510454578, 0.299339628426]], [[-0.336187609405, 0.32081899159, -0.0830511151094], [0.00963833043886, 0.241936082019, 0.358993107899], [0.050274587597, 0.0861845585369, -0.144289210221], [0.0584315683135, 0.168243714399, 0.241115212806], [0.171648956592, 0.133275946257, 0.245264141114], [-0.119320843449, 0.11492655725, -0.0330577278123], [-0.0793349771712, -0.0600164943608, -0.0662959918999], [-0.714868248456, 0.160086806483, 0.466999907093], [-0.083938207334, 0.271871618983, 0.0477036041195], [-0.587935235686, -0.278876924953, 0.912359207483], [0.255707033605, 0.27175182541, 0.109496810337], [-0.0410096318893, -0.233073757108, 0.191632913227], [-0.151897172464, -0.0251540289425, 0.447808652834], [-0.311527872996, -0.00566274461663, 0.669151443864], [0.0463356627465, -0.0230743808357, -0.10585719439], [-0.070740857338, -0.513550792832, -0.0712905690352], [0.316044041748, -0.211231094604, 0.215689446591], [-0.192921134695, 0.138033317311, 0.480755877354], [-0.0391511249812, -0.356352622704, 0.0408926904635], [-0.185025087911, 0.0622527423079, -0.126326681894], [1.34235776872, -0.732269141083, -0.217738485625], [-0.251662549435, -0.102124129031, -0.114941092372], [0.153819016668, -0.196360712113, -0.169552319825], [0.696244214511, -0.215754760776, -0.221951211165], [-0.673164936198, -0.25154862675, 0.488320907839], [-0.600118615213, 0.228972174949, 0.204006730199], [0.0296151253025, 0.10372152967, -0.239191091781], [0.169257162268, -0.187037658034, 0.0130221576055], [0.191980341153, -0.321431630031, -0.118189359702], [0.807616679384, -0.33839244383, 0.0395493078297], [0.246696674968, 0.302681773073, 0.219644581631], [-0.142193153485, -0.114778033507, 0.327064284931], [0.304275058157, -0.242471425302, 0.350945084797], [-0.313194601931, 0.181848043157, 0.155933947283], [-0.689831594832, 0.1550460236, 0.298639741488], [-0.0828961412601, 0.186511451034, -0.0443768940206], [-0.0654121122883, 0.194464732542, 0.25684436619], [0.747470441019, -1.02120950463, 0.331524836086], [0.319460522896, 0.0919233242707, 0.332362113681], [-0.282659405106, -0.299379902217, 0.385497044223], [-0.364787546279, -0.143717994482, -0.135933647625], [0.944701881687, -0.483917306106, -0.701298324844], [0.243828691444, 0.129655972325, 0.128488075361], [0.254566531363, -0.291577948737, -0.156708737192], [0.329561895774, -0.199246320558, -0.169754636555], [-0.160037603037, 0.168345088964, -0.028952002481], [0.791915742481, -0.942589426677, -0.335571685264], [0.20093565668, -0.13637901222, -0.317881603711], [0.0628722160445, 0.231375821317, -0.0800661868188], [0.168133724231, 0.00749728209595, 0.0275571361315]]];
const bias = [[0.300117412831, -0.029751221601, 0.277070899844, 0.0943774726309, -0.0733281905725, -0.00933955526873, 0.0695403219466, 0.0301140673585, 0.192646941339, 0.0231442266814, -0.0700829247176, 0.240993430578, -0.257592724933, -0.391129040272, -0.243280561302, -0.138774383373, -0.384060996197, 0.205719321635, 0.129902567468, -0.323320679505, -0.110809760209, -0.141520593896, -0.246002103459, 0.267904196882, -0.291063598131, 0.249507613485, 0.0546761199647, 0.252554656275, 0.229822963595, 0.293745485334, -0.0267464894555, -0.0446339563328, 0.0153461343481, -0.142854098844, -0.0592817962455, 0.0309978446178, -0.4856837725, -0.210037628911, -0.0442157673404, 0.0182401180574, -0.146692080751, 0.399662566788, 0.0519048101897, -0.286730533027, 0.19195282255, 0.0746874513633, -0.357669332876, -0.0532042133326, 0.119379224377, 0.279067851985], [0.376583238312, 0.459133195, -0.636719741605]];
// Prediction:
var clf = new MLPClassifier('relu', 'softmax', layers, weights, bias);
// Web Worker
var onmessage = function(e) {
if (e.data.length === 4) {
// Features:
var features = e.data;
// Prediction:
var prediction = clf.predict(features);
postMessage(prediction);
}
};