A WebGL accelerated JavaScript library for training and deploying ML models.
Go to file
Matthew Soulanille 0fc04d958e
Remove hub from tests (#8563)
2025-06-06 13:55:10 -07:00
.github Move nightly publishing tests and release branch tests to github actions (#8555) 2025-04-28 09:07:00 -07:00
.tslint
.vscode Add register name when register the class object (#7717) 2023-06-06 13:48:45 -07:00
dockers
docs
e2e Move nightly publishing tests and release branch tests to github actions (#8555) 2025-04-28 09:07:00 -07:00
link-package
remote-execution nightly test fix (#8214) 2024-04-06 00:56:02 -07:00
scripts Move nightly publishing tests and release branch tests to github actions (#8555) 2025-04-28 09:07:00 -07:00
tfjs Move nightly publishing tests and release branch tests to github actions (#8555) 2025-04-28 09:07:00 -07:00
tfjs-automl Address tfjs-automl typos in documentation strings (#8219) 2024-04-12 10:52:06 -07:00
tfjs-backend-cpu Add Draw API (#7628) 2023-05-06 16:51:17 -07:00
tfjs-backend-nodegl
tfjs-backend-wasm Fixing typos in READMEs (#8213) 2024-04-12 10:51:09 -07:00
tfjs-backend-webgl Move PR Continuous Integration to Github Actions (#8534) 2025-04-23 10:55:01 -07:00
tfjs-backend-webgpu [WebGPU] Access properties with `.prop` instead of `['prop']` (#8503) 2025-04-23 11:44:17 -07:00
tfjs-converter Remove hub from tests (#8563) 2025-06-06 13:55:10 -07:00
tfjs-core [tfjs-core] do not hang on invalid browser files (#8517) 2025-04-23 18:59:09 +00:00
tfjs-data [tfjs-data] support async generator (#8408) 2024-10-08 11:35:37 -07:00
tfjs-inference Update TypeScript to 5.0.4 (#7681) 2023-07-21 14:55:05 -07:00
tfjs-layers Move PR Continuous Integration to Github Actions (#8534) 2025-04-23 10:55:01 -07:00
tfjs-node Add NAPI-v9 support to tfjs-node-gpu package (#8547) 2025-05-29 15:39:13 -07:00
tfjs-node-gpu Add NAPI-v9 support to tfjs-node-gpu package (#8547) 2025-05-29 15:39:13 -07:00
tfjs-react-native Address tfjs-react-native typos in documentation strings (#8217) 2024-04-12 13:21:11 -07:00
tfjs-tfdf Fixed typos in documentation string. (#7782) 2024-02-05 18:41:41 +00:00
tfjs-tflite Fixing typos in READMEs (#8213) 2024-04-12 10:51:09 -07:00
tfjs-vis Fixed typos in documentation string. (#7782) 2024-02-05 18:41:41 +00:00
tools Move PR Continuous Integration to Github Actions (#8534) 2025-04-23 10:55:01 -07:00
.bazelignore
.bazelrc Move nightly publishing tests and release branch tests to github actions (#8555) 2025-04-28 09:07:00 -07:00
.bazelversion
.gitignore
BAZEL_MIGRATION.md
BUILD.bazel Move PR Continuous Integration to Github Actions (#8534) 2025-04-23 10:55:01 -07:00
CONTRIBUTING.md
CONTRIBUTING_MISSING_OP.md
DEVELOPMENT.md
GALLERY.md Added a new project - TF.js-TFLite Object Detection. (#8147) 2024-01-22 13:31:43 -08:00
LICENSE
README.md Update correct link for TensorFlow SavedModel hyperlink in README.md (#7907) 2023-08-17 10:18:10 -07:00
WORKSPACE apply g3 fixes for jax (#8099) 2023-12-18 15:24:44 -08:00
cloudbuild-release.yml Run yarn before running the release e2e tests (#7687) 2023-05-16 16:46:04 -07:00
cloudbuild-verdaccio.yml
cloudbuild.yml
package.json Move PR Continuous Integration to Github Actions (#8534) 2025-04-23 10:55:01 -07:00
pull_request_template.md Cleanup markdown files after monorepo merge & add tfjs cloudbui… (#1812) 2019-08-13 11:43:39 -04:00
rollup.config.helpers.js Fix WASM JS script not working after minification (#4562) (#4569) 2021-01-19 10:02:41 -08:00
tfjs.code-workspace
tsconfig.json
tsconfig.test.json
tsconfig_base.json
tsconfig_ts_library.json
tsconfig_tslint.json
tslint.json
yarn.lock webgpu: fix: conditionally call deprecated GPUAdapter.requestAdapterInfo (#8392) 2024-10-11 19:50:26 +00:00

README.md

TensorFlow.js

TensorFlow.js is an open-source hardware-accelerated JavaScript library for training and deploying machine learning models.

Develop ML in the Browser
Use flexible and intuitive APIs to build models from scratch using the low-level JavaScript linear algebra library or the high-level layers API.

Develop ML in Node.js
Execute native TensorFlow with the same TensorFlow.js API under the Node.js runtime.

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.

About this repo

This repository contains the logic and scripts that combine several packages.

APIs:

Backends/Platforms:

If you care about bundle size, you can import those packages individually.

If you are looking for Node.js support, check out the TensorFlow.js Node directory.

Examples

Check out our examples repository and our tutorials.

Be sure to check out the gallery of all projects related to TensorFlow.js.

Pre-trained models

Be sure to also check out our models repository where we host pre-trained models on NPM.

Benchmarks

  • Local benchmark tool. Use this webpage tool to collect the performance related metrics (speed, memory, etc) of TensorFlow.js models and kernels on your local device with CPU, WebGL or WASM backends. You can benchmark custom models by following this guide.
  • Multi-device benchmark tool. Use this tool to collect the same performance related metrics on a collection of remote devices.

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.

via Script Tag

Add the following code to an HTML file:

<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </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>

Open up that HTML file in your browser, and the code should run!

via NPM

Add TensorFlow.js to your project using yarn or npm. Note: Because we use ES2017 syntax (such as import), this workflow assumes you are using a modern browser or a bundler/transpiler to convert your code to something older browsers understand. See our examples to see how we use Parcel to build our code. However, you are free to use any build tool that you prefer.

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

See our tutorials, examples and documentation for more details.

Importing pre-trained models

We support porting pre-trained models from:

Various ops supported in different backends

Please refer below :

Find out more

TensorFlow.js is a part of the TensorFlow ecosystem. For more info:

Thanks, BrowserStack, for providing testing support.