TensorFlow Node.js provides idiomatic JavaScript language bindings and a high layer API for Node.js users.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

TensorFlow for Node.js

NPM Dependency Build Coverage
NPM version Dependency Status Build Status Coverage

TensorFlow Node.js provides idiomatic JavaScript language bindings and a high layer API for Node.js users.

Notice: This project is still under active development and not guaranteed to have a stable API. This is especially true because the underlying TensorFlow C API has not yet been stabilized as well.

Installation

$ npm install tensorflow2 --save

Usage

Run a predefined graph

The ability to run a predefined graph is the most basic function for any TensorFlow client library.

Given a GraphDef (or MetaGraphDef) protocol message, be able to create a session, run queries, and get tensor results. This is sufficient for a mobile app or server that wants to run inference on a pre-trained model.

Output the GraphDef binary format from your Python script:

import tensorflow as tf
import os

def main():
  v = tf.Variable(1000, name='my_variable')
  sess = tf.Session()
  tf.train.write_graph(sess.graph_def, tmpdir, 'graph.pb', as_text=False)

And load the graph.pb to your JavaScript runtime:

'use strict';

const tf = require('tensorflow2');
const graph = tf.graph();
const session = tf.session();

graph.load('/path/to/graph.pb');

// load the op by name
const op = graph.operations.get('my_variable/Assign');

// the following outputs the 1000
const res = session.run(op);

Graph construction

At least one function per defined TensorFlow op that adds an operation to the graph. Ideally these functions would be automatically generated so they stay in sync as the op definitions are modified.

'use strict';

const tf = require('tensorflow2');
const graph = tf.graph();

const x = graph.constant([[1, 2], [3, 4]], tf.dtype.float32, [2, 2]);
const w = graph.variable(x);
const y = graph.nn.softmax(graph.matmul(w, w));

const session = tf.session();
const res = session.run(y);

Operations

There are the following operations that we supported in this library.

State

The state is managed by users for saving, restoring machine states.

  • variable Holds state in the form of a tensor that persists across steps. Outputs a ref to the tensor state so it may be read or modified.
  • assign Update 'ref' by assigning 'value' to it. This operation outputs "ref" after the assignment is done. This makes it easier to chain operations that need to use the reset value.

Random

  • randomUniform Outputs random values from a uniform distribution.
  • randomUniformInt Outputs random integers from a uniform distribution.
  • randomGamma Outputs random values from the Gamma distribution(s) described by alpha.
  • randomPoisson Outputs random values from the Poisson distribution(s) described by rate.

Array

  • placeholder A placeholder op for a value that will be fed into the computation.
  • const Returns a constant tensor.
  • reverse Reverses specific dimensions of a tensor.
  • shape Returns the shape of a tensor.
  • rank Returns the rank of a tensor.
  • size Returns the size of a tensor.
  • onehot Returns a one-hot tensor.

Base64

  • encode Encode strings into web-safe base64 format.
  • decode Decode web-safe base64-encoded strings.

Flow

  • switch Forwards data to the output port determined by pred.
  • merge Forwards the value of an available tensor from inputs to output.
  • enter Creates or finds a child frame, and makes data available to the child frame.
  • exit Exits the current frame to its parent frame.
  • abort Raise an exception to abort the process when called.
  • trigger Does nothing and serves as a control trigger for scheduling.

Image

  • decodeJpeg Decode a JPEG-encoded image to a uint8 tensor.
  • encodeJpeg JPEG-encode an image.
  • resizeArea Resize images to size using area interpolation.
  • resizeBicubic Resize images to size using bicubic interpolation.
  • resizeBilinear Resize images to size using bilinear interpolation.
  • resizeNearestNeighbor Resize images to size using nearest neighbor interpolation.
  • randomCorp Randomly crop image.

Audio

  • decodeWav Decode a 16-bit PCM WAV file to a float tensor.
  • encodeWav Encode audio data using the WAV file format.
  • spectrogram Produces a visualization of audio data over time.
  • mfcc Transforms a spectrogram into a form that's useful for speech recognition.

Neural networks

In this module, it implements the following algorithms for representing neural networks.

Tests

$ npm test

License

MIT licensed @ 2017