Skip to content

TensorFlow with Node.js

Nikhil Kothari edited this page Dec 6, 2015 · 1 revision

This page will provide a quick introduction about what this module brings to the picture.

In a nutshell, the power of Google's new machine learning and numerical computation system, TensorFlow with a JavaScript programming model, making it natural to work with this technology in node.js.

This module will take care of all the nitty-gritty interop details, and provide a simple, but natural interface around the core set of objects such as Tensors, Graphs, Operations and Sessions.

Developer Experience

Here's an outline of the what you can expect to be possible - a 100% JavaScript experience (or TypeScript if you share that preference).

var tf = require('tensorflow'),
    fs = require('fs');

// Define the graph
var graph = new tf.Graph();
var shape = [2,2]
var p1 = graph.placeholder(tf.types.float, shape).named('p1');
var p2 = graph.placeholder(tf.types.float, shape).named('p2');
var value = graph.add(p1, p2).named('value');

// Optionally save this out (with corresponding APIs to load, instead
// of re-building the graph).
fs.writeFileSync('/tmp/hello.graph', graph.save());

// Execute the graph
var session = new tf.Session(graph);

var data = {};
data[p1] = new tf.Tensor([[1.0, 0.0],[0.0, 1.0]]);
data[p2] = new tf.Tensor([[3.0, 3.0],[3.0, 3.0]]);

var results = session.run([ value ], data);
console.log(results[value]);

Under the covers, the tensorflow module converts your graph definition into a protocol buffer representation that can be used to initialized the session within the native TensorFlow runtime. The module also takes care of other lower-level concerns like interop between JavaScript types and the types used to interface with the TensorFlow API.

This should allow building and running TensorFlow locally, and eventually in a distributed manner as the TensorFlow runtime itself matures to enable this capability. All this means, an awesome machine learning platform for node.js!

The road ahead

This is just going to the be the starting point. For all this to come together the module will need to have an API for the many operations exposed by TensorFlow in its Python SDK. And various ideas around making this efficient by customizing the interop layer, support for typed JavaScript arrays and more.

And over time, there will be some real samples, as things start to come together.

Check out the rest of the project, and the issues list, to contribute in any way you can/would like. Stay tuned, more coming!

Clone this wiki locally