# TensorFlow in your Browser

If you want to explore machine learning, you can now write applications that train and deploy TensorFlow in your browser using JavaScript. We know what you are thinking. That has to be slow. Surprisingly, it isn’t, since the libraries use Graphics Processing Unit (GPU) acceleration. Of course, that assumes your browser can use your GPU. There are several demos available, include one where you train a Pac Man game to respond to gestures in your webcam to control the game. If you try it and then disable accelerated graphics in your browser options, you’ll see just what a speed up you can gain from the GPU.

## Getting Started

The documentation and tutorials can help you get started. In its simplest form, though, it is pretty easy to do simple things. Just to get you started, we put together something very simple: a network that learns how to estimate what ten times some number equals. It isn’t the most amazing demo, but if you learn by reading code, it is somewhere to start.

The flow is pretty simple. First, we load the library from a content delivery server:

`<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.6.1">`

As you might guess from the name, everything in TensorFlow is a tensor. We create some training data in two tensors, each with five elements and only one dimension:

```// learn 10x table
const xs = tf.tensor2d([1,2,3,4,5], [5,1]);
const ys = tf.tensor2d([10,20,30,40,50], [5,1]);```

You can probably guess the xs tensor is the inputs and the ys tensor is the expected outputs.

There are several models and methods you can use to build the network and train it. We just picked a simple linear regression:

```// linear regression model
model.add(tf.layers.dense({units: 1, inputShape: }));
// Prep for training
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

// train -- the higher the number the more accurate you'll get (but longer run time)
tfinterface=model.fit(xs,ys,{epochs: nr_epochs});
}```

## Promises, Promises

Since this is JavaScript, executing things in parallel is handled through “promises.” A promise is essentially a function that should be run when the data it needs as an argument becomes available.  When the input arrives, the code inside the “then” will execute. Here’s an example:

```// wrapper around model predict
function predict(n) {
return tfinterface.then( () => { return model.predict(tf.tensor2d([n],[1,1])); });
}```

What this means is that when fitting is complete (`model.fit` returns a promise) the code will run the `predict` method to guess the result with input `n`. If you want to learn more about promises, Google has a good introduction along with a related discussion of the new `await` keyword.

The rest is pretty stock HTML forms. You can get the whole file from GitHub. Just put it somewhere and point your browser to the file. You’ll notice the results aren’t exact since it is an estimate, but the more training you do, the better they’ll get.

## What’s Next?

The site has some much juicier demos to dig into if you want to get serious. We’ve had our own introduction to TensorFlow. While it isn’t JavaScript, we did enjoy this Python TensorFlow tutorial. There’s also the very recent video from the TensorFlow Dev Summit, you can watch below.

from Blog – Hackaday https://ift.tt/2H3sVBJ
via IFTTT