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

in #programming6 years ago

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.

Sort:  

Congratulations @deeplearning! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You got your First payout
Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.

To support your work, I also upvoted your post!
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!

Congratulations @deeplearning! You have received a personal award!

1 Year on Steemit
Click on the badge to view your Board of Honor.

Do not miss the last post from @steemitboard:
SteemitBoard and the Veterans on Steemit - The First Community Badge.

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @deeplearning! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!