TensorFlow.js
TensorFlow.js

A WebGL accelerated, browser based JavaScript library for training and deploying ML models.

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
Run Existing models
Use TensorFlow.js model converters to run pre-existing TensorFlow models right in the browser.
Retrain Existing models
Retrain pre-existing ML models using sensor data connected to the browser, or other client-side data.

Demos

Use your phone’s camera to identify emojis in the real world. Can you find all the emojis before time expires?
Play Pac-Man using images trained in your browser.
No coding required! Teach a machine to recognize images and play sounds.
Enjoy a real-time piano performance by a neural network

Getting Started

You can use TensorFlow.js by installing it from NPM or via script tags.

Installation

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

Usage

See our tutorials, examples and documentation for more details, but let's see what it looks like to train a simple model in TensorFlow.js:

  
  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();
  });
  

Need Help? Want to connect?

Feel free to file issues on our GitHub Repository if you run into bugs using the library. We also have a community mailing list for people to ask questions, get technical help, and share what they are doing with TensorFlow.js! To keep up to date with TensorFlow.js news follow us on twitter or join the announcement only mailing list.