Machine Learning in the Browser


Machine learning is defined as a set of techniques that enables computers to learn new stuff. The programmers define a set of learning techniques or algorithms and feed them into the machines. Based on those algorithms, the machines start learning new stuff. Various languages have been used to implement machine learning algorithms. The most common ones being python, R, Java etc. In order to aid the development process various machine learning libraries have been developed by various companies. The most notable one being the tensorflow library released by Google.

The Tensorflow library

Tensorflow library is an open source machine learning library introduced by the Google brain team. This library performs machine learning by executing the algorithms called models on the given input data. A model represents a set of mathematical computations or algorithms that is performed on the given data. Tensorflow library is based on the commonly used Python language. The user can either define new models or can reuse the already existing models on the given data to derive the results. The results can range from small range tasks like games to higher level tasks such as decision making with medical, financial and other crucial data. Because of its open source and easy to learn nature, it has been tried out, followed and developed by vast amounts of developers all over the world.

Machine Learning in browser

The languages used for machine learning needs their own setup for development. The setup includes installation of compilers, supported editors and debuggers etc. Google wanted to reduce this workload even further so that developers can start even early without any further development environment setup. This introduced the introduction of Tensorflow js. Tensorflow js enables the developers start with the machine learning code in the browser itself. The developer do not have to go through complex development environment setup. The developer can also create new models or execute new models in the browser environment without any added complexity.

Tensorflow JS

Tensorflow JS is available as a node js package for installing in the client side machine. The following command can be used to install the necessary packages to the system.

npm install @tensorflow/tfjs
yarn install @tensorflow/tfjs

Either the node package manager tool or the yarn can be used for installing the packages. Tensorflow js enables quick setup with a one line command install. The output can be viewed in the browser environment. The only thing pending from the developer side is to create a sample web application and start with the coding task. The user can get additional information from the Tensorflow JS website. The site provides various code samples that enables the developer to get a quick taste of the machine learning technology. Either the developer can enjoy the demos or can look at the code for a deeper understanding. The user can create models, retrain already created models, or even reuse the created models on the current test data.

The following is a small example of a sample code using the prediction api

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., 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();

The documentation section contains all the necessary links for the developer in getting started with the Tensorflow platform.

Machine learning is an amazing technology, which leads to the evolvement of smart machines that benefits the humans in various ways. Google assistant, Iphone siri, Amazon alexa are the perfect examples. Now with the introduction of Tensorflow js, any developer can start with the machine learning technology without lot of development setup. What’s more interesting than starting with the development of this disruptive technology and explore and test it inside the browser environment itself. Let’s start exploring.

No Comments

You can leave the first : )

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: