TensorFlow.js
TensorFlow.js is an open-source hardware-accelerated JavaScript library for
building, training and serving machine learning models. When running in the
browser, it utilizes WebGL acceleration. TensorFlow.js is also convenient and
intuitive, modeled after
Keras and
tf.layers and can
load models saved from those libraries.
This repository conveniently contains the logic and scripts to form
a version-matched union package,
@tensorflowjs/tfjs, from
Importing
You can import TensorFlow.js Union directly via yarn or npm.
yarn add @tensorflow/tfjs
or npm install @tensorflow/tfjs
.
See snippets below for examples.
Alternatively you can use a script tag. Here we load it from a CDN.
In this case it will be available as a global variable named tf
.
You can replace also specify which version to load replacing @latest
with a specific
version string (e.g. 0.6.0
).
<script src="https://cdn.jsdelivr.net/npm/tensorflow/tfjs@latest"></script>
<script src="https://unpkg.com/tensorflow/tfjs@latest"></script>
Usage Examples
Many examples illustrating how to use TensorFlow.js in ES5, ES6 and
TypeScript are available from the
Examples repository
and the
TensorFlow.js Tutorials
Direct tensor manipulation
Let's add a scalar value to a 1D Tensor. TensorFlow.js supports broadcasting
the value of scalar over all the elements in the tensor.
import * as tf from '@tensorflow/tfjs';
const a = tf.tensor1d([1, 2, 3]);
const b = tf.scalar(2);
const result = a.add(b);
result.data().then(data => console.log(data));
console.log(result.dataSync());
See the
core-concepts tutorial
for more.
Building, training, and executing a model using Layers
The following example shows how to build a toy model with only one dense
layer
to perform linear regression.
import * as tf from '@tensorflow/tfjs';
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.compile({loss: 'meanSquaredError', optimizer: 'SGD'});
const xs = tf.tensor2d([[1], [2], [3], [4]], [4, 1]);
const ys = tf.tensor2d([[1], [3], [5], [7]], [4, 1]);
await model.fit(xs, ys, {epochs: 500});
const output = model.predict(tf.tensor2d([[5]], [1, 1]));
output.print();
For a deeper dive into building a layers classifier, see the
MNIST tutorial
Loading a pretrained Keras model using Layers
You can also load a model previously trained and saved from elsewhere (e.g.,
from Python Keras) and use it for inference or transfer learning in the browser.
More details in the
import-keras tutorial
For example, in Python, save your Keras model using
tensorflowjs,
which can be installed using pip install tensorflowjs
.
import tensorflowjs as tfjs
tfjs.converter.save_keras_model(model, '/path/to/tfjs_artifacts/')
To load the model with TensorFlow.js Layers:
import * as tf from '@tensorflow/tfjs';
const model = await tf.loadModel('http://foo.bar/tfjs_artifacts/model.json');
How to find more!
Again, see the
Examples repository and the
TensorFlow.js Tutorials
for many more examples of how to build models and manipulate tensors.
Supported Environments
TensorFlow.js targets environments with WebGL 1.0 or WebGL 2.0. For devices
without the OES_texture_float
extension, we fall back to fixed precision
floats backed by a gl.UNSIGNED_BYTE
texture. For platforms without WebGL,
we provide CPU fallbacks.
Additional Resources
TensorFlow.js is a part of the
TensorFlow ecosystem.
You can import pre-trained TensorFlow
SavedModels and
Keras models,
for execution and retraining.
For more information on the API, follow the links to their Core and Layers
repositories below, or visit js.tensorflow.org.