Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for yujiosaka yujiosaka
November 07, 2018

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

Avatar for yujiosaka

yujiosaka

November 07, 2018
Tweet

More Decks by yujiosaka

Other Decks in Programming

Transcript

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

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

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

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

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