Save 37% off PRO during our Black Friday Sale! »

JavaScriptでも機械学習がやりたかった話

5d247ff63b1861db5e6a56d4990e5a4f?s=47 yujiosaka
November 07, 2018

 JavaScriptでも機械学習がやりたかった話

5d247ff63b1861db5e6a56d4990e5a4f?s=128

yujiosaka

November 07, 2018
Tweet

Transcript

  1. Yuji Isobe JavaScriptͰ΋ ػցֶश͕΍Γ͔ͨͬͨ࿩ Emotion Intelligence × GIGษڧձ

  2. Ϛωʔδϟʔ 
 ϝϯςφʔ @yujiosaka github.com/yujiosaka/headless-chrome-crawler

  3. ձࣾ www.emin.co.jp

  4. એ఻ᶃ www.zenclerk.com/

  5. ϏδωεɾOSSϚΠϯυΛ࣋ͬͨΤϯδχΞืूʂ એ఻ᶄ www.wantedly.com/projects/233437

  6. ຊ୊

  7. 1.JavaScriptͰػցֶश͸Ͳ͜·ͰͰ͖Δͷ͔ 2.JavaScriptΛ࢖͏ϝϦοτͱσϝϦοτ 3.۩ମతͳ࣮ݱํ๏ͱσϞ ຊ೔ͷςʔϚ

  8. ͦ΋ͦ΋JavaScriptͰ
 ػցֶशͬͯ·ͱ΋ʹͰ͖Μͷʁ

  9. JavaScriptͰσΟʔϓϥʔχϯά speakerdeck.com/yujiosaka/hitasurale-sitedeipuraningu github.com/yujiosaka/js-mind XXXSFEEJUDPNSOPEFDPNNFOUTEH KT@NJOE@EFFQ@MFBSOJOH@MJCSBSZ@XSJUUFO@JO@FT ͳΜͰJavaScriptͰ͜Μͳ͜ͱͨ͠Μͩʁ ͦΜͳͷStarཉ͠͞ʹܾ·ͬͯΔͰ͠ΐ

  10. ਤ੕

  11. ౰࣌ͷΦν TQFBLFSEFDLDPNZVKJPTBLBIJUBTVSBMFTJUFEFJQVSBOJOHV TMJEF

  12. ౰࣌ͷΦν TQFBLFSEFDLDPNZVKJPTBLBIJUBTVSBMFTJUFEFJQVSBOJOHV TMJEF

  13. A. օ͕࢖ͬͯΔ͔Βศརʹͳ͍ͬͯ͘ Q. ͳͥσʔλαΠΤϯε͸Pythonʁ trends.google.co.jp/trends/explore?date=today%205-y&geo=JP&q=JavaScript,Python,AI

  14. NumPy/Pandas - ߴ଎ɾߴػೳͳߦྻܭࢉ Matplotlib/seaborn - ख͔ܰͭߴػೳͳάϥϑඳը Jupiter Notebook - ࢼߦࡨޡ͕΍Γ΍͍͢

    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश ... σʔλαΠΤϯεΛࢧ͑ΔPython Cݴޠ࣮૷ʴSIMD໋ྩʴ਺ֶతͳ࠷దԽʴ؆ܿͳهड़
  15. NumPyΛ࢖ͬͨߴ଎ͳߦྻܭࢉ > import time > import numpy as np >

    a = np.random.random((1000, 1000)) > b = np.random.random((1000, 1000)) > t = time.time() > np.dot(a, b) > print(time.time() - t) 0.08162903785705566 Python + NumPy > const math = require('mathjs'); > const a = math.random([1000, 1000]); > const b = math.random([1000, 1000]); > const t = Date.now(); > math.multiply(a, b); > console.log((Date.now() - t) / 1000); 135.587 JavaScript + math.js
  16. NumPy/Pandas - ߴ଎ɾߴػೳͳߦྻܭࢉ Matplotlib/seaborn - ख͔ܰͭߴػೳͳάϥϑඳը Jupiter Notebook - ࢼߦࡨޡ͕΍Γ΍͍͢

    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश ... σʔλαΠΤϯεΛࢧ͑ΔPython γϯϓϧͳΠϯλʔϑΣʔεʢfit, transform, predictʣ
  17. XGBoost ͔Β LightGBM ΁ͷ੾Γସ͕͑਺ߦͰ׬ྃ scikit-learnͷҰ؏ͨ͠ػցֶशAPI github.com/dmlc/xgboost/ github.com/Microsoft/LightGBM

  18. NumPy/Pandas - ߴ଎ɾߴػೳͳߦྻܭࢉ Matplotlib/seaborn - ख͔ܰͭߴػೳͳάϥϑඳը Jupiter Notebook - ࢼߦࡨޡ͕΍Γ΍͍͢

    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश ... σʔλαΠΤϯεΛࢧ͑ΔPython
  19. σʔλαΠΤϯε΍ΔͳΒ Pythonʢ·ͨ͸Rʣͬ͠ΐ

  20. ͚ͩͲ…

  21. ػցֶश͚ͩͳΒ೉͘͠ͳ͍

  22. JavaScriptͰ΋Ͱ͖ͪΌ͏ʂ

  23. - σʔλαΠΤϯεʹ͸
 ਺ֶɾ౷ܭ஌ࣝͱϋοΩϯά
 εΩϧ͚ͩͰͳ͘ɺਂ͍
 ઐ໳ੑ͕ٻΊΒΕΔ σʔλαΠΤϯεͱػցֶशͷҧ͍ ESFXDPOXBZDPN[JBUIFEBUBTDJFODFWFOOEJBHSBN ਂ͍ઐ໳ੑ ϋοΩϯά
 εΩϧ

    ਺ֶɾ
 ౷ܭ஌ࣝ ػց
 ֶश جૅ ݚڀ ةݥ
 κʔϯ σʔλ
 αΠΤϯε
  24. σʔλαΠΤϯεͱػցֶशͷҧ͍ ESFXDPOXBZDPN[JBUIFEBUBTDJFODFWFOOEJBHSBN ϋοΩϯά
 εΩϧ ਺ֶɾ
 ౷ܭ஌ࣝ ػց
 ֶश - ਺ֶɾ౷ܭ஌ࣝͱ


    ϋοΩϯάεΩϧ͑͋͞Ε͹ɺ ػցֶश͸Ͱ͖Δ
  25. ਂ͍ઐ໳ੑ͕ͳͨͬͯ͘
 ػցֶशͯ͠΋͍͍͡Όͳ͍ ΤϯδχΞͩ΋ͷ

  26. NumPy/Pandas - ߴ଎ɾߴػೳͳߦྻܭࢉ Matplotlib/seaborn - ख͔ܰͭߴػೳͳάϥϑඳը Jupiter Notebook - ࢼߦࡨޡ͕΍Γ΍͍͢

    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश ... σʔλαΠΤϯεΛࢧ͑ΔPython TensorFlow.js / brain.js
  27. from tensorflow.contrib.keras.python import keras import numpy as np model =

    keras.Sequential() model.add(keras.layers.Dense(units=1, input_shape=[1])) model.compile(optimizer='sgd', loss='mean_squared_error') xs = np.array([[1], [2], [3], [4]]) ys = np.array([[1], [3], [5], [7]]) model.fit(xs, ys, epochs=1000) print(model.predict(np.array([[5]]))) TensorFlow Keras
  28. import * as tf from '@tensorlowjs/tfjs'; const model = tf.sequential();

    model.add(tf.layers.dense({units: 1, inputShape: [1]})); model.compile({optimizer: 'sgd', loss: 'meanSquaredError'}); const xs = tf.tensor2d([[1], [2], [3], [4]], [4, 1]); const ys = tf.tensor2d([[1], [3], [5], [7]], [4, 1]); await model.fit(xs, ys, {epochs: 1000}); model.predict(tf.tensor2d([[5]], [1, 1])).print(); TensorFlow.js WebGLΛ࢖ͬͨGPUܭࢉ
  29. Ͱ΋Ͳ͏ͤ஗͍ΜͰ͠ΐʁ

  30. - WebGLΛ࢖ͬͨTensorFlow.js͸
 AVXΛ࢖ͬͨTensorFlowͷ1.5-2ഒ஗͍ - ڊେͳϞσϧͷֶश͸10-15ഒ͔͔Δ͜ͱ΋͋Δ WebGLΛ࢖ͬͨGPUܭࢉ KTUFOTPSqPXPSHGBR

  31. ࣮༻తʹͳΓͭͭ͋Δ

  32. ͞Βʹ

  33. - SIMD໋ྩͱWeb Assembly͕αϙʔτ͞ΕΔ - େ෯ͳύϑΥʔϚϯε޲্͕ظ଴͞ΕΔ TensorFlow.js 2.0΁ͷϩʔυϚοϓ www.tensorflow.org/community/roadmap

  34. JavaScript࢖ͬͯͳΜ͔
 ͍͍͜ͱ͋Μͷʁ

  35. - TypeScript͕࢖͑Δ - Universal/Isomorphic JavaScriptͷ࣮ݱ - ๛෋ͳNPMύοέʔδ͕׆༻Ͱ͖Δ - ඇಉظͳΦϯϥΠϯֶशʹద͍ͯ͠Δ -

    ϒϥ΢βͷϦιʔεΛ༗ޮ׆༻Ͱ͖Δ JavaScriptͰػցֶशΛߦ͏ϝϦοτ
  36. ϒϥ΢β্ͰTensorFlow.jsΛಈ͔͢ qiita.com/kaneU/items/ca84c4bfcb47ac53af99

  37. Ϧιʔεʢˇʣ͕અ໿Ͱ͖Δʂ

  38. - ϒϥ΢β্Ͱಛ௃ྔͷநग़ɺม׵ɺ༧ଌΛߦ͏ͨΊ
 ίϐϖ͢Ε͹୭Ͱ΋ίϐʔαΠτ͕࡞Εͯ͠·͏ - ΞϧΰϦζϜͰ͸ͳ͘ɺಛ௃ྔ͕ͦ͜ڝ૪ྗͷݯઘ - ୭͕࡞ͬͯ΋ಉ͡΋ͷ͕Ͱ͖Δ৔߹͚ͩʹཹΊΔ ※ϦόʔεΤϯδχΞϦϯάʹ͸
 ऑ͍ͷͰϏδωεͰ͸஫ҙ͕ඞཁ

  39. Googleͷ2018೥ΤΠϓϦϧϑʔϧ
 ωλͰTensorFlow.js͕࢖ΘΕͨ landing.google.co.jp/tegaki/

  40. - ѹ౗తͳ৘ใෆ଍ - ߦྻܭࢉ͕؆ܿʹهड़Ͱ͖ͳ͍ - ػցֶशAPI͕ڞ௨Խ͞Ε͍ͯͳ͍ - ࠷৽ͷΞϧΰϦζϜ͕͙͢ʹར༻Ͱ͖ͳ͍ - ධՁؔ਺౳ͷपลϥΠϒϥϦ͕ෆ଍͍ͯ͠Δ

    JavaScriptͰػցֶशΛߦ͏σϝϦοτ
  41. - 1ͭ1ͭ͸େͨ͜͠ͱͷͳ͍ؔ਺͕ͩɺ
 ͸͡Ί͔Βἧ͍ͬͯΔͱ͋Γ͕ͨΈΛ࣮ײ͢Δ ... from sklearn.cross_validation import train_test_split from sklearn.grid_search

    import GridSearchCV from sklearn.metrics import classification_report ... X_train, X_test, y_train, y_test = train_test_split(X, y) ... clf = GridSearchCV(...) ... report = classification_report(y_test, y_pred) ... scikit-learnͰΑ͘ݟΒΕΔίʔυ
  42. Python͔ʁJavaScript͔ʁ - ຊؾͰσʔλαΠΤϯεΛ͢ΔͳΒPython - Ұ͔ΒػցֶशͷΞϓϦΛ࡞ΔͳΒPython - ͪΐͬͱͨ͠ػցֶश͚ͩͳΒԿͰ΋͍͍ - JavaScript͸ܾͯ͠ѱ͍બ୒ࢶͰ͸ͳ͍

  43. ͲΜͳ΍Γํ͕͋Δͷʁ

  44. TensorFlow.js - χϡʔϥϧωοτϫʔΫɾਂ૚ֶश brain.js - χϡʔϥϧωοτϫʔΫɾਂ૚ֶश Synaptic - χϡʔϥϧωοτϫʔΫɾਂ૚ֶश Natural

    - ࣗવݴޠॲཧ ml.js - ༷ʑͳػցֶशϥΠϒϥϦ܈ math.js - ౷ܭɾߦྻܭࢉ JavaScriptʹΑΔػցֶशϥΠϒϥϦ js.tensorflow.org github.com/BrainJS caza.la/synaptic/ github.com/NaturalNode/natural mathjs.org github.com/mljs χϡʔϥϧωοτϫʔΫࣗମ͸ൺֱత୯७ͳߦྻܭࢉ
  45. TensorFlow.js - χϡʔϥϧωοτϫʔΫɾਂ૚ֶश brain.js - χϡʔϥϧωοτϫʔΫɾਂ૚ֶश Synaptic - χϡʔϥϧωοτϫʔΫɾਂ૚ֶश Natural

    - ࣗવݴޠॲཧ ml.js - ༷ʑͳػցֶशϥΠϒϥϦ܈ math.js - ౷ܭɾߦྻܭࢉ JavaScriptʹΑΔػցֶशϥΠϒϥϦ js.tensorflow.org github.com/BrainJS caza.la/synaptic/ github.com/NaturalNode/natural mathjs.org github.com/mljs
  46. σϞ

  47. > const iris = require('ml-dataset-iris'); > const { RandomForestClassifier }

    = require('ml-random- forest'); > > const CLASSES = ['setosa', 'versicolor', 'virginica']; > > const X = iris.getNumbers(); > const y = iris.getClasses().map(cls =>
 > CLASSES.indexOf(cls)
 > ); > const classifier = new RandomForestClassifier(); > classifier.train(X, y); > classifier.predict(X); > classifier.predict([ 6.7, 3, 5, 1.7 ]); [ 2 ] // 'virginica' mljs
  48. JavaScriptͰ΋ ػցֶश͕΍Γ͔ͨͬͨ࿩ ͸ڪΕΔʹ଍Βͳ͍࿩