Skip to content

Commit

Permalink
Move the Web Worker extension to the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Darius Morawiec committed Nov 1, 2017
1 parent a17c77c commit 187efac
Show file tree
Hide file tree
Showing 16 changed files with 296 additions and 222 deletions.
Empty file added examples/api/.gitkeep
Empty file.
39 changes: 11 additions & 28 deletions examples/estimator/classifier/MLPClassifier/js/basics.ipynb

Large diffs are not rendered by default.

38 changes: 11 additions & 27 deletions examples/estimator/classifier/MLPClassifier/js/basics.py

Large diffs are not rendered by default.

39 changes: 11 additions & 28 deletions examples/estimator/regressor/MLPRegressor/js/basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -181,41 +181,24 @@
"\n",
"};\n",
"\n",
"// Parameters:\n",
"const layers = [30, 1];\n",
"const weights = [[[-0.231934017303, 9.76963498098, -0.518291978926, 1.37709884392, -0.273604926667, 0.0369158058728, -0.243006888514, -0.119620808538, 2.70632771042, 0.0300654685651, 10.3562831534, 10.7714084856, -0.228916260559, 0.292870541265, -0.366061540854, 0.842176293078, 5.82946033324, 0.0454581570946, -0.278537993236, -0.453372247294, 0.110796781119, 3.62612480908, -0.144512145356, 0.148963319667, 0.954205097505, 0.187448883454, -0.321403655905, -0.382426886263, 1.19436651445, 4.45022582518], [-0.31155376112, 5.33989980285, 0.354303221057, 0.894962640194, 0.148618256686, 0.148504419985, 0.144454136149, 0.259184031779, 1.11006926551, 0.193749068045, 6.21594403713, 6.12636863648, -0.170056920664, 0.224061059731, -0.30732096177, 0.251510692938, 3.61941947291, -0.159855987209, -0.164378432403, -0.287155049553, 0.67513481258, 1.93563215214, -0.223358199397, -0.181595637129, 0.319327651798, -0.226105204691, 0.0574077287864, -0.273694813761, 0.88571670111, 2.39187331032], [-1.11396932206, 25.1099159842, -0.479653573778, 3.92756497738, -0.348583168582, -0.144404294868, 0.12686700423, 0.0115323491799, 7.94412038584, 0.0670411335775, 27.7369983263, 27.7731921031, -0.279398200824, 0.238089663264, -0.0792543195629, 1.11300061042, 15.8399993348, -0.117912824865, 0.194266302825, -0.880267121813, 0.458440368351, 8.80773756912, 0.194367250507, -0.117035661707, 1.34050231486, 0.0313724094133, -0.0556969082018, 0.237933380088, 4.42965675089, 11.3962309242], [-0.877443594686, 15.3645155535, -0.491585362244, 2.43775046793, -0.0711525618984, -0.0586863593935, 0.312437267242, 0.0570683842243, 4.20666426004, 0.090734493987, 16.249151475, 16.8015784507, 0.298931127327, -0.110551586941, 0.316430556384, 1.08713531781, 8.88890431658, 0.332620246997, 0.147859046366, -0.372995710946, 0.211184176508, 4.95608965142, 0.335066450831, 0.152445340888, 0.493195740768, 0.0177727958423, 0.196639587528, 0.239923979035, 2.84995528095, 6.55684584832], [-0.613742153763, 7.08615211885, -0.554478916973, 0.966440325076, 0.278859343827, -0.420061332003, 0.0409132190149, 0.264919739039, 1.95682393355, -0.171033095076, 8.18592742721, 8.67028941729, 0.0472709048215, -0.372831335805, 0.232854782923, 0.113764331666, 4.82949785692, -0.0868574435063, 0.281581036658, -0.125288871077, 0.0315348146123, 2.29882317513, -0.340865382205, -0.29328810681, 0.0175224061809, -0.290938618824, -0.212451600309, 0.128326134104, 1.15981761306, 2.97178991649], [-0.447159349672, 7.17788938661, -0.0376726471598, 0.841119099208, -0.191835885532, -0.177097863334, -0.235904838381, 0.0630164884612, 2.34166040712, 0.268635955507, 7.19572715805, 7.54010369629, 0.0929116004423, 0.254811878121, -0.265831935645, -0.17975902185, 3.85129553647, -0.0105763828004, 0.0823573947928, -0.0980791577995, 0.165285069078, 2.69430237135, 0.0617665922703, -0.0928365535962, 0.343001338485, 0.187078308662, 0.131079194809, -0.199600451572, 0.561405425729, 2.84766060155], [0.405800755748, -12.667511799, 0.434525751147, -2.31488249552, -0.185647738127, 0.114195890535, -0.237450217685, 0.108019307595, -3.67754873936, 0.32903465498, -13.7126337391, -14.1559812754, 0.182070143494, 0.210815263093, 0.315873424403, -0.379303969354, -8.02844808107, -0.205749622024, 0.090450577867, 0.747579092536, -0.587591744175, -4.22310882339, 0.321907547484, 0.109649987672, -0.934124732058, 0.0388808546816, 0.0807935965186, 0.0846365950028, -1.59385928107, -5.16039287054], [-0.400459418148, 12.6039671046, -0.5020100171, 1.64347471983, -0.28264913018, -0.125887432509, -0.370602558292, 0.346974949059, 3.89907437595, -0.375641639193, 13.0682879785, 13.4790059344, -0.285811082452, 0.239715754409, -0.120259094638, 1.01024233434, 7.59641075438, 0.293423993832, 0.267013773759, -0.103745738657, 0.618360389444, 4.24276133494, 0.231283159537, -0.165971282559, 0.74428887041, 0.0668411248081, -0.375243292583, 0.0240699111314, 1.77127276128, 5.65573968971], [-0.865772864656, 18.7683973337, -0.504794854404, 2.66634273739, 0.223015345078, -0.116243661627, -0.345519196862, -0.061813917807, 5.81657229288, 0.324227653877, 19.7609025131, 21.0078956525, -0.0955946130605, 0.366968639812, 0.0811077676401, 1.46429392596, 11.4502604948, 0.0992013113162, -0.166081710062, -0.879001875102, 0.406066530367, 6.72156116964, 0.197573693815, 0.153405074645, 1.53944554407, -0.17356948579, 0.132284313777, -0.147540104479, 3.0279473694, 8.36264432512], [-0.488718726366, 19.1537533694, -0.228055081207, 3.01667495506, 0.36698299962, 0.0866237712839, -0.233472438544, -0.0567736713529, 5.52983012422, 0.23053588443, 21.257705447, 21.7131320773, 0.126034493357, -0.177984996725, -0.191804162373, 1.55676818092, 11.8787286546, 0.234038612624, 0.0561459198181, -0.359425256512, 0.488352316976, 6.80113344514, 0.0533338985915, -0.0265593845993, 1.0359915856, -0.277965916314, -0.0945537238823, -0.388083159291, 3.60808378361, 8.19210514566]], [[33.4479368654], [-1660.37649465], [21.2658922021], [-122.081117298], [0.251011638998], [-0.212260700744], [-0.154530952038], [0.328107816652], [-2232.28348675], [0.0338211373336], [-2413.20715535], [-2950.09845507], [0.287182250542], [0.31156093727], [-0.3530370787], [-502.7526944], [-1337.73243532], [0.0969930329539], [0.263610111876], [41.9525181101], [-2.33969432672], [-1511.10213157], [-0.211424254758], [-0.213737407582], [-555.782180642], [0.689480935887], [0.260947236591], [14.9998775527], [-1629.25066339], [-3196.97088367]]];\n",
"const bias = [[-46.5836909664, -2241.72465654, -36.469491912, -363.382638609, -0.26261039582, -9.24672406471, -0.119929213481, -0.212983132852, -584.13069605, -0.145415158432, -2410.26749474, -2440.07018874, -0.188135360656, -0.301402302417, -0.237830045111, -110.135474112, -1364.33594943, -0.226031616075, -0.195172366442, -61.0401525629, -25.5710115131, -726.330430506, -0.206301321452, -0.308314835098, -129.637997175, -5.94828228669, -0.269039387619, -6.99999397605, -307.060933112, -877.451145727], [151.51211299]];\n",
"\n",
"// Estimator:\n",
"var reg = new MLPRegressor('relu', layers, weights, bias);\n",
"\n",
"if (typeof process !== 'undefined' && typeof process.argv !== 'undefined') {\n",
" if (process.argv.length - 2 === 10) {\n",
"\n",
" // Features:\n",
" var features = process.argv.slice(2);\n",
" var prediction = reg.predict(features);\n",
" console.log(prediction);\n",
" }\n",
"}\n",
"\n",
"// Web Worker (main.js):\n",
"// if (typeof window !== 'undefined' && window.Worker) {\n",
"// var worker = new Worker('mlpregressor.js');\n",
"// worker.onmessage = function(e) {\n",
"// console.log('Prediction: ' + e.data);\n",
"// };\n",
"// worker.postMessage([/* feature vector */]);\n",
"// worker.postMessage([/* feature vector */]);\n",
"// }\n",
" // Parameters:\n",
" const layers = [30, 1];\n",
" const weights = [[[-0.231934017303, 9.76963498098, -0.518291978926, 1.37709884392, -0.273604926667, 0.0369158058728, -0.243006888514, -0.119620808538, 2.70632771042, 0.0300654685651, 10.3562831534, 10.7714084856, -0.228916260559, 0.292870541265, -0.366061540854, 0.842176293078, 5.82946033324, 0.0454581570946, -0.278537993236, -0.453372247294, 0.110796781119, 3.62612480908, -0.144512145356, 0.148963319667, 0.954205097505, 0.187448883454, -0.321403655905, -0.382426886263, 1.19436651445, 4.45022582518], [-0.31155376112, 5.33989980285, 0.354303221057, 0.894962640194, 0.148618256686, 0.148504419985, 0.144454136149, 0.259184031779, 1.11006926551, 0.193749068045, 6.21594403713, 6.12636863648, -0.170056920664, 0.224061059731, -0.30732096177, 0.251510692938, 3.61941947291, -0.159855987209, -0.164378432403, -0.287155049553, 0.67513481258, 1.93563215214, -0.223358199397, -0.181595637129, 0.319327651798, -0.226105204691, 0.0574077287864, -0.273694813761, 0.88571670111, 2.39187331032], [-1.11396932206, 25.1099159842, -0.479653573778, 3.92756497738, -0.348583168582, -0.144404294868, 0.12686700423, 0.0115323491799, 7.94412038584, 0.0670411335775, 27.7369983263, 27.7731921031, -0.279398200824, 0.238089663264, -0.0792543195629, 1.11300061042, 15.8399993348, -0.117912824865, 0.194266302825, -0.880267121813, 0.458440368351, 8.80773756912, 0.194367250507, -0.117035661707, 1.34050231486, 0.0313724094133, -0.0556969082018, 0.237933380088, 4.42965675089, 11.3962309242], [-0.877443594686, 15.3645155535, -0.491585362244, 2.43775046793, -0.0711525618984, -0.0586863593935, 0.312437267242, 0.0570683842243, 4.20666426004, 0.090734493987, 16.249151475, 16.8015784507, 0.298931127327, -0.110551586941, 0.316430556384, 1.08713531781, 8.88890431658, 0.332620246997, 0.147859046366, -0.372995710946, 0.211184176508, 4.95608965142, 0.335066450831, 0.152445340888, 0.493195740768, 0.0177727958423, 0.196639587528, 0.239923979035, 2.84995528095, 6.55684584832], [-0.613742153763, 7.08615211885, -0.554478916973, 0.966440325076, 0.278859343827, -0.420061332003, 0.0409132190149, 0.264919739039, 1.95682393355, -0.171033095076, 8.18592742721, 8.67028941729, 0.0472709048215, -0.372831335805, 0.232854782923, 0.113764331666, 4.82949785692, -0.0868574435063, 0.281581036658, -0.125288871077, 0.0315348146123, 2.29882317513, -0.340865382205, -0.29328810681, 0.0175224061809, -0.290938618824, -0.212451600309, 0.128326134104, 1.15981761306, 2.97178991649], [-0.447159349672, 7.17788938661, -0.0376726471598, 0.841119099208, -0.191835885532, -0.177097863334, -0.235904838381, 0.0630164884612, 2.34166040712, 0.268635955507, 7.19572715805, 7.54010369629, 0.0929116004423, 0.254811878121, -0.265831935645, -0.17975902185, 3.85129553647, -0.0105763828004, 0.0823573947928, -0.0980791577995, 0.165285069078, 2.69430237135, 0.0617665922703, -0.0928365535962, 0.343001338485, 0.187078308662, 0.131079194809, -0.199600451572, 0.561405425729, 2.84766060155], [0.405800755748, -12.667511799, 0.434525751147, -2.31488249552, -0.185647738127, 0.114195890535, -0.237450217685, 0.108019307595, -3.67754873936, 0.32903465498, -13.7126337391, -14.1559812754, 0.182070143494, 0.210815263093, 0.315873424403, -0.379303969354, -8.02844808107, -0.205749622024, 0.090450577867, 0.747579092536, -0.587591744175, -4.22310882339, 0.321907547484, 0.109649987672, -0.934124732058, 0.0388808546816, 0.0807935965186, 0.0846365950028, -1.59385928107, -5.16039287054], [-0.400459418148, 12.6039671046, -0.5020100171, 1.64347471983, -0.28264913018, -0.125887432509, -0.370602558292, 0.346974949059, 3.89907437595, -0.375641639193, 13.0682879785, 13.4790059344, -0.285811082452, 0.239715754409, -0.120259094638, 1.01024233434, 7.59641075438, 0.293423993832, 0.267013773759, -0.103745738657, 0.618360389444, 4.24276133494, 0.231283159537, -0.165971282559, 0.74428887041, 0.0668411248081, -0.375243292583, 0.0240699111314, 1.77127276128, 5.65573968971], [-0.865772864656, 18.7683973337, -0.504794854404, 2.66634273739, 0.223015345078, -0.116243661627, -0.345519196862, -0.061813917807, 5.81657229288, 0.324227653877, 19.7609025131, 21.0078956525, -0.0955946130605, 0.366968639812, 0.0811077676401, 1.46429392596, 11.4502604948, 0.0992013113162, -0.166081710062, -0.879001875102, 0.406066530367, 6.72156116964, 0.197573693815, 0.153405074645, 1.53944554407, -0.17356948579, 0.132284313777, -0.147540104479, 3.0279473694, 8.36264432512], [-0.488718726366, 19.1537533694, -0.228055081207, 3.01667495506, 0.36698299962, 0.0866237712839, -0.233472438544, -0.0567736713529, 5.52983012422, 0.23053588443, 21.257705447, 21.7131320773, 0.126034493357, -0.177984996725, -0.191804162373, 1.55676818092, 11.8787286546, 0.234038612624, 0.0561459198181, -0.359425256512, 0.488352316976, 6.80113344514, 0.0533338985915, -0.0265593845993, 1.0359915856, -0.277965916314, -0.0945537238823, -0.388083159291, 3.60808378361, 8.19210514566]], [[33.4479368654], [-1660.37649465], [21.2658922021], [-122.081117298], [0.251011638998], [-0.212260700744], [-0.154530952038], [0.328107816652], [-2232.28348675], [0.0338211373336], [-2413.20715535], [-2950.09845507], [0.287182250542], [0.31156093727], [-0.3530370787], [-502.7526944], [-1337.73243532], [0.0969930329539], [0.263610111876], [41.9525181101], [-2.33969432672], [-1511.10213157], [-0.211424254758], [-0.213737407582], [-555.782180642], [0.689480935887], [0.260947236591], [14.9998775527], [-1629.25066339], [-3196.97088367]]];\n",
" const bias = [[-46.5836909664, -2241.72465654, -36.469491912, -363.382638609, -0.26261039582, -9.24672406471, -0.119929213481, -0.212983132852, -584.13069605, -0.145415158432, -2410.26749474, -2440.07018874, -0.188135360656, -0.301402302417, -0.237830045111, -110.135474112, -1364.33594943, -0.226031616075, -0.195172366442, -61.0401525629, -25.5710115131, -726.330430506, -0.206301321452, -0.308314835098, -129.637997175, -5.94828228669, -0.269039387619, -6.99999397605, -307.060933112, -877.451145727], [151.51211299]];\n",
"\n",
"// Web Worker (mlpregressor.js):\n",
"var onmessage = function(e) {\n",
" if (e.data.length === 10) {\n",
" var features = e.data;\n",
" // Prediction:\n",
" var reg = new MLPRegressor('relu', layers, weights, bias);\n",
" var prediction = reg.predict(features);\n",
" postMessage(prediction);\n",
" console.log(prediction);\n",
"\n",
" }\n",
"};\n",
"\n"
"}\n"
]
}
],
Expand Down
38 changes: 11 additions & 27 deletions examples/estimator/regressor/MLPRegressor/js/basics.py

Large diffs are not rendered by default.

File renamed without changes.
67 changes: 67 additions & 0 deletions examples/recipes/use_javascript_web_workers/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>sklearn-porter</title>
<style>
p { line-height: 16px; font-size: 16px; }
input { height: 16px; margin: 0 2em; }
</style>
</head>
<body>
<main>
<section>
Used data: <a href="https://github.com/nok/sklearn-porter/blob/master/examples/estimator/classifier/MLPClassifier/js/basics.py">examples/estimator/classifier/MLPClassifier/js/basics.py</a>
</section>
<section class="input">
<form>
<p>
Feature 1: <input class="js_slider" type="range" min="-5" max="5" value="0" step="0.01"><span>0</span>
</p>
<p>
Feature 2: <input class="js_slider" type="range" min="-5" max="5" value="0" step="0.01"><span>0</span>
</p>
<p>
Feature 3: <input class="js_slider" type="range" min="-5" max="5" value="0" step="0.01"><span>0</span>
</p>
<p>
Feature 4: <input class="js_slider" type="range" min="-5" max="5" value="0" step="0.01"><span>0</span>
</p>
</form>
</section>
<section class="output">
Prediction: <strong id="js_prediction">-</strong>
</section>
</main>
<script type="text/javascript">
if (typeof window !== 'undefined' && window.Worker) {

var $sliders = document.getElementsByClassName('js_slider');
var $label = document.getElementById("js_prediction");

var worker = new Worker('worker.js');
worker.onmessage = function(e) {
$label.innerHTML = e.data;
};

var run = function() {
var features = Array();
Array.prototype.forEach.call($sliders, function($slider, i){
var val = (typeof($slider.value) === 'undefined') ? 0 : $slider.value;
val = parseFloat(val);
features.push(val);
});
worker.postMessage(features);
};

Array.prototype.forEach.call($sliders, function($slider, i){
$slider.addEventListener('change', function(e) {
this.nextElementSibling.innerHTML = this.value;
run();
});
});

}
</script>
</body>
</html>
3 changes: 3 additions & 0 deletions examples/recipes/use_javascript_web_workers/serve.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

python -m $(python -c 'import sys; print("http.server" if sys.version_info[:2] > (2,7) else "SimpleHTTPServer")')
Loading

0 comments on commit 187efac

Please sign in to comment.