Introduction to Tensorflow.js

A Quick Intro and Getting started with Tensorflow.js

Tensorflow.js is the an open source WebGL-accelerated JavaScript library for machine intelligence; which you can use to define, train, and run machine learning models entirely in the browser. It provides a high-level layers API for defining models, and a low-level API for linear algebra and automatic differentiation. So if you are a Javascript developer or you know Javascript well and want to get started with ML, Tensorflow.js will be a great way to start learning ML. Tensorflow.js is built on deeplearn.js which is the low level API.

 

What you can do with Tensorflow.js?

  1. Develop ML in the Browser

    Use flexible and intuitive APIs to build and train models from scratch using the low-level JavaScript linear algebra library or the high-level layers API, and then run the model on the browser.
    If you’re familiar with Keras, the high-level layers API should feel familiar.

  2. Run Existing models

    Use TensorFlow.js model converters to convert your previously trained, pre-existing Tensorflow or Keras models and then run them right in the browser.

  3. Retrain Existing models

    Retrain pre-existing ML models using sensor data connected to the browser, or other client-side data. You can use transfer learning to augment an existing model trained offline using a small amount of data collected in the browser using a technique called Image Retraining. This is one way to train an accurate model quickly, using only a small amount of data.

Machine Learning running in the browser means that there’s no need to install any libraries or drivers. Just open a webpage, and your program is ready to run. In addition, it’s ready to run with GPU acceleration. TensorFlow.js automatically supports WebGL, and will accelerate your code behind the scenes when a GPU is available. Users may also open your webpage from a mobile device, in which case your model can take advantage of sensor data, say from a gyroscope or accelerometer. Finally, all data stays on the client, making TensorFlow.js useful for low-latency inference, as well as for privacy preserving applications.

Getting Started!!

There are two main ways to get TensorFlow.js in your JavaScript project: via script tags or by installing it from NPM and using a build tool like Parcel, WebPack, or Rollup.

  • Using Script Tag
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.2"> </script>

    Basic Code for HTML

    <html> <head> <!-- Load TensorFlow.js --> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.2"> </script> <!-- Place your code in the script tag below. You can also use an external .js file --> <script> // Notice there is no 'import' statement. 'tf' is available on the index-page // because of the script tag above. // Define a model for linear regression. const model = tf.sequential(); model.add(tf.layers.dense({units: 1, inputShape: [1]})); // Prepare the model for training: Specify the loss and the optimizer. model.compile({loss: 'meanSquaredError', optimizer: 'sgd'}); // Generate some synthetic data for training. const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]); const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]); // Train the model using the data. model.fit(xs, ys).then(() => { // Use the model to do inference on a data point the model hasn't seen before: // Open the browser devtools to see the output model.predict(tf.tensor2d([5], [1, 1])).print(); }); </script> </head> <body> </body> </html>
  • Using NPM
    yarn add @tensorflow/tfjs npm install @tensorflow/tfjs

    In java file import the package using

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

    Note: Because we use ES2017 syntax (such as `import`), this workflow assumes you are using a bundler/transpiler to convert your code to something the browser understands.

    In your main js file: 

    import * as tf from '@tensorflow/tfjs'; // Define a model for linear regression. const model = tf.sequential(); model.add(tf.layers.dense({units: 1, inputShape: [1]})); // Prepare the model for training: Specify the loss and the optimizer. model.compile({loss: 'meanSquaredError', optimizer: 'sgd'}); // Generate some synthetic data for training. const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]); const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]); // Train the model using the data. model.fit(xs, ys).then(() => { // Use the model to do inference on a data point the model hasn't seen before: model.predict(tf.tensor2d([5], [1, 1])).print(); }); 

    Thanks for reading, more post on Tensorflow.js coming!! Follow our website to learn the latest technologies, and concepts. Xpertup with us.

    You can also check out post on Basic concepts of Tensorflow.js
Spread the knowledge

xpertup

Let's Expert Up

Leave a Reply

Your email address will not be published. Required fields are marked *