4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

TensorFlow.js - equivalent of the popular TensorFlow library for the javascript language

Introduction

Tensorflow is a very popular open source deep learning library, mainly for python. It was created by the Google Brain. For a long time there was no equivalent to javascript, which would be equal to the possibilities. This changed in March 2018, when Google presented the library tensorflow.js.

Genesis

Some time ago, Google presented the website: https://playground.tensorflow.org, which allowed you to experiment with neural networks from your browser, without having to install anything. This website was very popular due to its educational value. As a result, Google decided to create a deeplearn.js library, which would enable easy use of machine learning from the javascript language level. However, it was still a library with much less capabilities than TensorFlow or PyTorch. The end result is tensorflow.js, the equivalent of TensorFlow for javascript.

Application

The benefits of using the tensorflow.js library are as follows:

  • the ability to run the entire code only on the client side, without the need to send any data to the server
  • very low entry threshold; nothing needs to be installed
  • a syntax very similar to the TensorFlow library, so people who already know the library will be able to use it quite quickly
  • useful for mobile devices, everything can be done on the device, there is no need to send data anywhere or to install any environment
  • the code can be easily shared and is ready to be run

The disadvantages are:

  • lower performance (on the other hand, no one will run advanced models in the browser)
  • poorly developed machine learning ecosystem for javascript

Examples

Below is a simple example of linear regression. The code is embedded in HTML. We can see that one line: <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script> is enough to import library and we can now use it.

<html>
  <head>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
    <script>
      const model = tf.sequential();
      model.add(tf.layers.dense({units: 1, inputShape: [1]}));
      model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

      const x = tf.tensor2d([1, 2, 3, 4],  [4, 1]);
      const y = tf.tensor2d([3, 5, 7, 9],  [4, 1]);

      model.fit(x, y).then(() => {
        model.predict(tf.tensor2d([5], [1, 1])).print();
      });
    </script>
  </head>
  <body>
  </body>
</html>

The result can be read in the browser console.

The library can also be used from the level of node.js. Simply install it with the npm install @tensorflow/tfjs command. The code is now as follows:

import * as tf from '@tensorflow/tfjs';

const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

const x = tf.tensor2d([1, 2, 3, 4],  [4, 1]);
const y = tf.tensor2d([3, 5, 7, 9],  [4, 1]);

model.fit(x, y).then(() => {
model.predict(tf.tensor2d([5], [1, 1])).print();
});

More advanced examples can be found in the official project repository:
https://github.com/tensorflow/tfjs-examples

Summary

It might seem that the tensorflow.js library is just a curiosity, because machine learning is not very associated with the javascript language. But on the other hand, it is a big step forward, significantly lowering the entry threshold and allowing for easier use of the benefits of machine learning on websites.

8 upvotes 6 downvotes $0.01

Replies (3)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: