yujiosaka
November 07, 2018
360

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

## yujiosaka

November 07, 2018

## Transcript

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 γϯϓϧͳΠϯλʔϑΣʔεʢﬁt, transform, predictʣ

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

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

23. ### - σʔλαΠΤϯεʹ͸  ਺ֶɾ౷ܭ஌ࣝͱϋοΩϯά  εΩϧ͚ͩͰͳ͘ɺਂ͍  ઐ໳ੑ͕ٻΊΒΕΔ σʔλαΠΤϯεͱػցֶशͷҧ͍ ESFXDPOXBZDPN[JBUIFEBUBTDJFODFWFOOEJBHSBN ਂ͍ઐ໳ੑ ϋοΩϯά  εΩϧ

਺ֶɾ  ౷ܭ஌ࣝ ػց  ֶश جૅ ݚڀ ةݥ  κʔϯ σʔλ  αΠΤϯε
24. ### σʔλαΠΤϯεͱػցֶशͷҧ͍ ESFXDPOXBZDPN[JBUIFEBUBTDJFODFWFOOEJBHSBN ϋοΩϯά  εΩϧ ਺ֶɾ  ౷ܭ஌ࣝ ػց  ֶश - ਺ֶɾ౷ܭ஌ࣝͱ

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

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

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

ϒϥ΢βͷϦιʔεΛ༗ޮ׆༻Ͱ͖Δ JavaScriptͰػցֶशΛߦ͏ϝϦοτ

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

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

- ࣗવݴޠॲཧ ml.js - ༷ʑͳػցֶशϥΠϒϥϦ܈ math.js - ౷ܭɾߦྻܭࢉ JavaScriptʹΑΔػցֶशϥΠϒϥϦ js.tensorﬂow.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.tensorﬂow.org github.com/BrainJS caza.la/synaptic/ github.com/NaturalNode/natural mathjs.org github.com/mljs

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