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

Machine Learning with JavaScript

yujiosaka
November 05, 2019

Machine Learning with JavaScript

yujiosaka

November 05, 2019
Tweet

More Decks by yujiosaka

Other Decks in Technology

Transcript

  1. Machine Learning with JavaScript

    View Slide

  2. @yujiosaka
    First member / Engineer manager of
    Author of
    https://github.com/yujiosaka/headless-chrome-crawler
    ≒ JavaScript developer
    (70% JavaScript)

    View Slide

  3. ‣ What machine learning is from the beginning
    ‣ How to use machine learning libraries
    I will not
    talk about

    View Slide

  4. ‣ 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

    View Slide

  5. ‣ 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

    View Slide

  6. Can you seriously do machine learning with JavaScript?

    View Slide

  7. Deep learning with Javascript in 2016
    https://speakerdeck.com/yujiosaka/enjoy-deep-learning-by-javascript
    https://github.com/yujiosaka/js-mind
    https://www.reddit.com/r/node/comments/4dg157

    View Slide

  8. Conclusion in 2016 was very sad
    https://speakerdeck.com/yujiosaka/enjoy-deep-learning-by-javascript?slide=61

    View Slide

  9. 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

    View Slide

  10. 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

    View Slide

  11. 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

    View Slide

  12. 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

    View Slide

  13. 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

    View Slide

  14. 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

    View Slide

  15. If it’s for data science
    you should use Python

    View Slide

  16. But if it’s just for machine learning

    JavaScript is also a good option

    View Slide

  17. 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

    View Slide

  18. 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

    View Slide

  19. 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

    View Slide

  20. 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]])))

    View Slide

  21. 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

    View Slide

  22. Roadmap to TensorFlow.js 2.0
    TensorFlow 2.0 Beta is available!
    www.tensorflow.org/community/roadmap

    View Slide

  23. ‣ 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

    View Slide

  24. 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

    View Slide

  25. 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

    View Slide

  26. TensorFlow.js is used for Google’s April Fool 2018
    landing.google.co.jp/tegaki/

    View Slide

  27. 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.

    View Slide

  28. 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.

    View Slide

  29. 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

    View Slide

  30. 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

    View Slide

  31. ‣ 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

    View Slide

  32. 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

    View Slide

  33. 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

    View Slide

  34. Demo
    https://github.com/yujiosaka/brain-js-live-demo

    View Slide

  35. Machine Learning with JavaScript
    Enjoy

    View Slide