$30 off During Our Annual Pro Sale. View Details »

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

yujiosaka
November 07, 2018

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

yujiosaka

November 07, 2018
Tweet

More Decks by yujiosaka

Other Decks in Programming

Transcript

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

    View Slide

  2. Ϛωʔδϟʔ

    ϝϯςφʔ
    @yujiosaka
    github.com/yujiosaka/headless-chrome-crawler

    View Slide

  3. ձࣾ
    www.emin.co.jp

    View Slide

  4. એ఻ᶃ
    www.zenclerk.com/

    View Slide

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

    View Slide

  6. ຊ୊

    View Slide

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

    View Slide

  8. ͦ΋ͦ΋JavaScriptͰ

    ػցֶशͬͯ·ͱ΋ʹͰ͖Μͷʁ

    View Slide

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

    View Slide

  10. ਤ੕

    View Slide

  11. ౰࣌ͷΦν
    TQFBLFSEFDLDPNZVKJPTBLBIJUBTVSBMFTJUFEFJQVSBOJOHV TMJEF

    View Slide

  12. ౰࣌ͷΦν
    TQFBLFSEFDLDPNZVKJPTBLBIJUBTVSBMFTJUFEFJQVSBOJOHV TMJEF

    View Slide

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

    View Slide

  14. NumPy/Pandas - ߴ଎ɾߴػೳͳߦྻܭࢉ
    Matplotlib/seaborn - ख͔ܰͭߴػೳͳάϥϑඳը
    Jupiter Notebook - ࢼߦࡨޡ͕΍Γ΍͍͢
    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ
    TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश
    ...
    σʔλαΠΤϯεΛࢧ͑ΔPython
    Cݴޠ࣮૷ʴSIMD໋ྩʴ਺ֶతͳ࠷దԽʴ؆ܿͳهड़

    View Slide

  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

    View Slide

  16. NumPy/Pandas - ߴ଎ɾߴػೳͳߦྻܭࢉ
    Matplotlib/seaborn - ख͔ܰͭߴػೳͳάϥϑඳը
    Jupiter Notebook - ࢼߦࡨޡ͕΍Γ΍͍͢
    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ
    TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश
    ...
    σʔλαΠΤϯεΛࢧ͑ΔPython
    γϯϓϧͳΠϯλʔϑΣʔεʢfit, transform, predictʣ

    View Slide

  17. XGBoost ͔Β LightGBM ΁ͷ੾Γସ͕͑਺ߦͰ׬ྃ
    scikit-learnͷҰ؏ͨ͠ػցֶशAPI
    github.com/dmlc/xgboost/ github.com/Microsoft/LightGBM

    View Slide

  18. NumPy/Pandas - ߴ଎ɾߴػೳͳߦྻܭࢉ
    Matplotlib/seaborn - ख͔ܰͭߴػೳͳάϥϑඳը
    Jupiter Notebook - ࢼߦࡨޡ͕΍Γ΍͍͢
    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ
    TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश
    ...
    σʔλαΠΤϯεΛࢧ͑ΔPython

    View Slide

  19. σʔλαΠΤϯε΍ΔͳΒ
    Pythonʢ·ͨ͸Rʣͬ͠ΐ

    View Slide

  20. ͚ͩͲ…

    View Slide

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

    View Slide

  22. JavaScriptͰ΋Ͱ͖ͪΌ͏ʂ

    View Slide

  23. - σʔλαΠΤϯεʹ͸

    ਺ֶɾ౷ܭ஌ࣝͱϋοΩϯά

    εΩϧ͚ͩͰͳ͘ɺਂ͍

    ઐ໳ੑ͕ٻΊΒΕΔ
    σʔλαΠΤϯεͱػցֶशͷҧ͍
    ESFXDPOXBZDPN[JBUIFEBUBTDJFODFWFOOEJBHSBN
    ਂ͍ઐ໳ੑ
    ϋοΩϯά

    εΩϧ
    ਺ֶɾ

    ౷ܭ஌ࣝ
    ػց

    ֶश
    جૅ
    ݚڀ
    ةݥ

    κʔϯ
    σʔλ

    αΠΤϯε

    View Slide

  24. σʔλαΠΤϯεͱػցֶशͷҧ͍
    ESFXDPOXBZDPN[JBUIFEBUBTDJFODFWFOOEJBHSBN
    ϋοΩϯά

    εΩϧ
    ਺ֶɾ

    ౷ܭ஌ࣝ
    ػց

    ֶश
    - ਺ֶɾ౷ܭ஌ࣝͱ

    ϋοΩϯάεΩϧ͑͋͞Ε͹ɺ
    ػցֶश͸Ͱ͖Δ

    View Slide

  25. ਂ͍ઐ໳ੑ͕ͳͨͬͯ͘

    ػցֶशͯ͠΋͍͍͡Όͳ͍
    ΤϯδχΞͩ΋ͷ

    View Slide

  26. NumPy/Pandas - ߴ଎ɾߴػೳͳߦྻܭࢉ
    Matplotlib/seaborn - ख͔ܰͭߴػೳͳάϥϑඳը
    Jupiter Notebook - ࢼߦࡨޡ͕΍Γ΍͍͢
    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ
    TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश
    ...
    σʔλαΠΤϯεΛࢧ͑ΔPython
    TensorFlow.js / brain.js

    View Slide

  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

    View Slide

  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ܭࢉ

    View Slide

  29. Ͱ΋Ͳ͏ͤ஗͍ΜͰ͠ΐʁ

    View Slide

  30. - WebGLΛ࢖ͬͨTensorFlow.js͸

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

    View Slide

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

    View Slide

  32. ͞Βʹ

    View Slide

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

    View Slide

  34. JavaScript࢖ͬͯͳΜ͔

    ͍͍͜ͱ͋Μͷʁ

    View Slide

  35. - TypeScript͕࢖͑Δ
    - Universal/Isomorphic JavaScriptͷ࣮ݱ
    - ๛෋ͳNPMύοέʔδ͕׆༻Ͱ͖Δ
    - ඇಉظͳΦϯϥΠϯֶशʹద͍ͯ͠Δ
    - ϒϥ΢βͷϦιʔεΛ༗ޮ׆༻Ͱ͖Δ
    JavaScriptͰػցֶशΛߦ͏ϝϦοτ

    View Slide

  36. ϒϥ΢β্ͰTensorFlow.jsΛಈ͔͢
    qiita.com/kaneU/items/ca84c4bfcb47ac53af99

    View Slide

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

    View Slide

  38. - ϒϥ΢β্Ͱಛ௃ྔͷநग़ɺม׵ɺ༧ଌΛߦ͏ͨΊ

    ίϐϖ͢Ε͹୭Ͱ΋ίϐʔαΠτ͕࡞Εͯ͠·͏
    - ΞϧΰϦζϜͰ͸ͳ͘ɺಛ௃ྔ͕ͦ͜ڝ૪ྗͷݯઘ
    - ୭͕࡞ͬͯ΋ಉ͡΋ͷ͕Ͱ͖Δ৔߹͚ͩʹཹΊΔ
    ※ϦόʔεΤϯδχΞϦϯάʹ͸

    ऑ͍ͷͰϏδωεͰ͸஫ҙ͕ඞཁ

    View Slide

  39. Googleͷ2018೥ΤΠϓϦϧϑʔϧ

    ωλͰTensorFlow.js͕࢖ΘΕͨ
    landing.google.co.jp/tegaki/

    View Slide

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

    View Slide

  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ͰΑ͘ݟΒΕΔίʔυ

    View Slide

  42. Python͔ʁJavaScript͔ʁ
    - ຊؾͰσʔλαΠΤϯεΛ͢ΔͳΒPython
    - Ұ͔ΒػցֶशͷΞϓϦΛ࡞ΔͳΒPython
    - ͪΐͬͱͨ͠ػցֶश͚ͩͳΒԿͰ΋͍͍
    - JavaScript͸ܾͯ͠ѱ͍બ୒ࢶͰ͸ͳ͍

    View Slide

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

    View Slide

  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
    χϡʔϥϧωοτϫʔΫࣗମ͸ൺֱత୯७ͳߦྻܭࢉ

    View Slide

  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

    View Slide

  46. σϞ

    View Slide

  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

    View Slide

  48. JavaScriptͰ΋
    ػցֶश͕΍Γ͔ͨͬͨ࿩
    ͸ڪΕΔʹ଍Βͳ͍࿩

    View Slide