TensorFlow.js high-level layers API
caisq rnn(): Replace repeated slice() and concat() with unstack() and stack…
…() (#389)

PERF

Benchmark numbers: 

Each individual number below is an average from 20 predict() calls. Only the numbers from relevant number types are shown. CNNs and dense-only models are not shown. So as you can see, there is a consistent small speed up (on the order of 3-3.5%) across the four RNN model types in the benchmark.

BEFORE (master)
SimpleRNN 32.9, 30.1, 32.7, 34.4, 33.2, 32.6
GRU 122.6, 116.9, 112.1, 118.6, 112.1, 113.6
LSTM 92.4, 96.1, 90.2, 89.4, 92.7, 88.8
Attention 120.9, 113.9, 114.8, 110.6, 113.8, 114.4

AFTER (rnn-concat-perf)
SimpleRNN 31.5, 31.6, 31.3, 30.6, 33.4, 31.2
GRU 113.1, 112.8, 111.6, 115.7, 109.1, 109.1
LSTM 90.5, 86.7, 87.8, 89.4, 88.2, 88.0
Attention 112.2, 114.3, 110.8, 108.2, 110.9, 112.2

Changes:
p-values are from `scipy.stats.ttest_ind()`.

SimpleRNN: 32.65 --> 31.6, -3.22%, p = 0.16
GRU: 116 --> 111.9, -3.52%, p = 0.068
LSTM: 91.6 --> 88.44, -3.56%, p = 0.028
Attention: 114.73 --> 111.44, -2.88%, p = 0.067

Regarding tensorflow/tfjs#863
Latest commit 7dbd508 Dec 12, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.vscode VSCode default spaces around brackets (#232) Jun 11, 2018
demos Refactor Benchmarks demo and remove demos/ directory (#88) Mar 28, 2018
integration_tests Bump version: 0.9.0 --> 0.9.1 (#387) Dec 7, 2018
scripts Test layers againts tfjs-core@master on Travis Nightly (every 24 hour… Oct 26, 2018
src rnn(): Replace repeated slice() and concat() with unstack() and stack… Dec 12, 2018
tools Improve `DEVELOPMENT.md` (#148) Apr 23, 2018
.gitignore Make es bundle and remove dist-es6/ (#233) Jun 11, 2018
.npmignore add ./yalc to npm ignore (#369) Nov 13, 2018
.travis.yml Update .travis.yml to send notifications on build failure. (#355) Oct 29, 2018
CONTRIBUTING.md Issue template (#87) Mar 27, 2018
DEVELOPMENT.md Add integration_test/tfjs2keras; Fix the bugs uncovered by the test (#… May 12, 2018
ISSUE_TEMPLATE.md Issues Template: Remove label issues from template (#124) Apr 12, 2018
LICENSE Initial commit of tfjs-layers Mar 8, 2018
README.md Update "TensorFlow.js API documentation" link (#342) Oct 21, 2018
karma.conf.js Make es bundle and remove dist-es6/ (#233) Jun 11, 2018
package.json Bump version: 0.9.0 --> 0.9.1 (#387) Dec 7, 2018
pull_request_template.md Improvements around the build/dev setup (#220) Jun 4, 2018
rollup.config.js ship sourcemaps in npm bundle (#264) Jul 11, 2018
tsconfig.json Remove the doc decorator. Split exports into multiple files. (#267) Jul 13, 2018
tslint.json Update tslint dependency and allow for long lines with URLs. (#280) Aug 7, 2018
yarn.lock Bump version: 0.9.0 --> 0.9.1 (#387) Dec 7, 2018

README.md

Build Status

TensorFlow.js Layers: High-Level Machine Learning Model API

A part of the TensorFlow.js ecosystem, TensorFlow.js Layers is a high-level API built on TensorFlow.js Core, enabling users to build, train and execute deep learning models in the browser. TensorFlow.js Layers is modeled after Keras and tf.keras and can load models saved from those libraries.

Importing

There are three ways to import TensorFlow.js Layers

  1. You can access TensorFlow.js Layers through the union package between the TensorFlow.js Core and Layers: @tensorflow/tfjs
  2. You can get [TensorFlow.js] Layers as a module: @tensorflow/tfjs-layers. Note that tfjs-layers has peer dependency on tfjs-core, so if you import @tensorflow/tfjs-layers, you also need to import @tensorflow/tfjs-core.
  3. As a standalone through unpkg.

Option 1 is the most convenient, but leads to a larger bundle size (we will be adding more packages to it in the future). Use option 2 if you care about bundle size.

Getting started

Building, training and executing a model

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';

// A sequential model is a container which you can add layers to.
const model = tf.sequential();

// Add a dense layer with 1 output unit.
model.add(tf.layers.dense({units: 1, inputShape: [1]}));

// Specify the loss type and optimizer for training.
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.
await model.fit(xs, ys, {epochs: 500});

// Ater the training, perform inference.
const output = model.predict(tf.tensor2d([[5]], [1, 1]));
output.print();

Loading a pretrained Keras model

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.

For example, in Python, save your Keras model using tensorflowjs, which can be installed using pip install tensorflowjs.

import tensorflowjs as tfjs

# ... Create and train your Keras model.

# Save your Keras model in TensorFlow.js format.
tfjs.converters.save_keras_model(model, '/path/to/tfjs_artifacts/')

# Then use your favorite web server to serve the directory at a URL, say
#   http://foo.bar/tfjs_artifacts/model.json

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');
// Now the model is ready for inference, evaluation or re-training.

For more information