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

5d247ff63b1861db5e6a56d4990e5a4f?s=47 yujiosaka
November 07, 2018

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

5d247ff63b1861db5e6a56d4990e5a4f?s=128

yujiosaka

November 07, 2018
Tweet

Transcript

  1. 6.
  2. 10.
  3. 14.

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

    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश ... σʔλαΠΤϯεΛࢧ͑ΔPython Cݴޠ࣮૷ʴSIMD໋ྩʴ਺ֶతͳ࠷దԽʴ؆ܿͳهड़
  4. 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
  5. 16.

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

    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश ... σʔλαΠΤϯεΛࢧ͑ΔPython γϯϓϧͳΠϯλʔϑΣʔεʢfit, transform, predictʣ
  6. 18.

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

    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश ... σʔλαΠΤϯεΛࢧ͑ΔPython
  7. 20.
  8. 26.

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

    scikit-learn - ڞ௨ͷػցֶशAPIΛఏڙ͢Δ TensorFlow/PyTorch - GPUΛ࢖ͬͨߴ଎ͳਂ૚ֶश ... σʔλαΠΤϯεΛࢧ͑ΔPython TensorFlow.js / brain.js
  9. 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
  10. 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ܭࢉ
  11. 32.
  12. 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ͰΑ͘ݟΒΕΔίʔυ
  13. 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 χϡʔϥϧωοτϫʔΫࣗମ͸ൺֱత୯७ͳߦྻܭࢉ
  14. 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
  15. 46.
  16. 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