Share
Embed
Machine Learning with JavaScript
yujiosaka
November 05, 2019
Transcript
35 slides
-
@yujiosaka First member / Engineer manager of Author of https://github.com/yujiosaka/headless-chrome-crawler
≒ JavaScript developer (70% JavaScript) -
‣ What machine learning is from the beginning ‣ How
to use machine learning libraries I will not talk about -
‣ How much we can do for machine learning with
JavaScript ‣ Advantages/Disadvantages of using JavaScript for machine learning ‣ How to do machine learning with JavaScript and demo Agenda -
‣ How much we can do for machine learning with
JavaScript ‣ Advantages/Disadvantages of using JavaScript for machine learning ‣ How to do machine learning with JavaScript and demo Agenda -
Q. Why do data scientists choose Python? A. Because the
more people use it, the faster it improves https://trends.google.co.jp/trends/explore?date=today%205-y&geo=JP&q=JavaScript,Python,AI -
Python libraries that boost data science ‣ Numpy / Pandas
‣ Matplotlib / seaborn ‣ Jupiter Notebook ‣ scikit-learn ‣ TensorFlow / PyTorch - Highly functional and fast matrix operation - Simple and easy graphic plotting with high level interface - Interactively developing and presenting documents - Common interface for reusable machine learning - Tensor computing with strong acceleration via GPU -
Python libraries that boost data science ‣ Numpy / Pandas
‣ Matplotlib / seaborn ‣ Jupiter Notebook ‣ scikit-learn ‣ TensorFlow / PyTorch - Highly functional and fast matrix operation - Simple and easy graphic plotting with high level interface - Interactively developing and presenting documents - Common interface for reusable machine learning - Tensor computing with strong acceleration via GPU -
Numpy powered by C, SIMD and math Python + 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 JavaScript + math.js > 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); 44.166 -
Python libraries that boost data science ‣ Numpy / Pandas
‣ Matplotlib / seaborn ‣ Jupiter Notebook ‣ scikit-learn ‣ TensorFlow / PyTorch - Highly functional and fast matrix operation - Simple and easy graphic plotting with high level interface - Interactively developing and presenting documents - Common interface for reusable machine learning - Tensor computing with strong acceleration via GPU -
Consistant interface (fit, transform, predict) of scikit-learn The LightGBM support
in AiDeal was completed by adding several lines of code github.com/Microsoft/LightGBM -
But if it’s just for machine learning JavaScript is also
a good option -
Data science vs. Machine learning Data science requires substantive expertise
as well as math and statistics knowledge and hacking skills drewconway.com/zia/2013/3/26/the-data-science-venn-diagram -
Data science vs. Machine learning But even if you don’t
have substantive expertise, you can do machine learning with math and statistics knowledge and hacking skills drewconway.com/zia/2013/3/26/the-data-science-venn-diagram -
Python libraries that boost data science ‣ Numpy / Pandas
‣ Matplotlib / seaborn ‣ Jupiter Notebook ‣ scikit-learn ‣ TensorFlow / PyTorch - Highly functional and fast matrix operation - Simple and easy graphic plotting with high level interface - Interactively developing and presenting documents - Common interface for reusable machine learning - Tensor computing with strong acceleration via GPU -
Sample code for TensorFlow (Keras) 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]]))) -
Sample code for TensorFlow.js 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(); Utilize GPU with WebGL ≒ Super fast -
‣ How much we can do for machine learning with
JavaScript ‣ Advantages/Disadvantages of using JavaScript for machine learning ‣ How to do machine learning with JavaScript and demo Agenda -
Advantages of using JavaScript (especially on browser) ‣ You can
share the same code both in frontend and backend (Isomorphic JavaScript) ‣ You can use TypeScript ‣ You can provide interactive UI ‣ You don’t need to install runtime ‣ You can utilize the client side resources -
Advantages of using JavaScript (especially on browser) ‣ You can
share the same code both in frontend and backend (Isomorphic JavaScript) ‣ You can use TypeScript ‣ You can provide interactive UI ‣ You don’t need to install runtime ‣ You can utilize the client side resources - You can save money -
Disadvantages of using JavaScript ‣ It’s vulnerable to reverse engineering
‣ You don’t find enough information on the web ‣ Latest algorithms and classifiers are sometimes not available ‣ There are not sufficient libraries for evaluation metrics, data splitting and etc. -
Disadvantages of using JavaScript ‣ It’s vulnerable to reverse engineering
‣ You don’t find enough information on the web ‣ Latest algorithms and classifiers are sometimes not available ‣ There are not sufficient libraries for evaluation metrics, data splitting and etc. -
Typical code you write very often you are using scikit-learn
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) … Although each function is just a few lines of code, you miss it when you lose it -
Python vs. JavaScript ‣ Use Python if it’s for data
science ‣ Use Python if you are developing a machine learning app from scratch ‣ If you are familiar with JavaScript, you don’t necessarily have to switch to Python ‣ If you want to utilize client side resources, use JavaScript ‣ If you are using TensorFlow, you can train model in Python and predict in JavaScript -
‣ How much we can do for machine learning with
JavaScript ‣ Advantages/Disadvantages of using JavaScript for machine learning ‣ How to do machine learning with JavaScript and demo Agenda -
JavaScript libraries for machine learning ‣ TensorFlow.js ‣ brain.js ‣
Natural ‣ ml.js ‣ math.js - Keras / TensorFlow compatible library with GPU acceleration - Simple and easy neural networks library with GPU acceleration - General natural language facilities with tokenizing and etc - Compilation of machine learning and numerical analysis libraries - Extensive math library for statistics and matrix operations -
JavaScript libraries for machine learning ‣ TensorFlow.js ‣ brain.js ‣
Natural ‣ ml.js ‣ math.js - Keras / TensorFlow compatible library with GPU acceleration - Simple and easy neural networks library with GPU acceleration - General natural language facilities with tokenizing and etc - Compilation of machine learning and numerical analysis libraries - Extensive math library for statistics and matrix operations