TensorFlow + Node.js
TensorFlow is Google's machine learning runtime. It is implemented as C++ runtime, along with Python framework to support building a variety of models, especially neural networks for deep learning.
This module takes care of the building blocks and mechanics for working with the TensorFlow C API, and instead provides an API around Tensors, Graphs, Sessions and Models.
This is still in the works, and recently revamped to support TensorFlow 1.4+.
High Level Interface - Models
This is in plan. The idea here is to point to a saved model and be able to use it for predictions. Instances-in, inferences-out.
Stay tuned for a future update.
Low Level Interface - Tensors, Graphs and Sessions
Trivial Example - Loading and Running Graphs
Lets assume we have a simple TensorFlow graph. For illustration purposes, a trivial graph produced from this Python code, and saved as a GraphDef protocol buffer file.
import tensorflow as tfwith . as graph:c1 =c2 =result =
Now, in node.js, you can load this serialized graph definition, load a TensorFlow session, and then run specific operations to retrive tensors.
const tf = ;// Load the Graph and create a Session to be able to run the operations// defined in the graph.let graph = tf;let session = graph;// Run to evaluate and retrieve the value of the 'result' op.let result = session;// The result is a Tensor, which contains value, type and shape fields.// This Should print out '42'console;// Cleanupgraph;
Feeding and Fetching Tensors with a Session
This example goes a bit further - in particular, the Graph contains variables, and placeholders, requiring initialization as well as feeding values, when executing the graph. Additionally the Tensors are integer matrices.
import tensorflow as tfwith . as graph:var1 =var2 =var3 =with :
Here is the corresponding node.js snippet to work with the Graph defined above:
const tf = ;let graph = tf;let session = graph;// Run the 'init' op to initialize variables defined in the graph.session;// Generally you can use arrays directly. This samples demonstrates creating// Tensors to explicitly specify types to match the int32 types that the graph// expects.let a = tf;let b = tf;// You can fetch multiple outputs as well.let outputs = session;consoleconsole;graph;
Installation is pretty straight-forward. Installing this module automatically brings installs the TensorFlow binary dependencies (by default, TensorFlow CPU v1.4.1).
npm install tensorflow
Optionally, you can specify the build of TensorFlow binaries to install using environment variables.
export TENSORFLOW_LIB_TYPE=gpu export TENSORFLOW_LIB_VERSION=1.5.0 npm install tensorflow
The TensorFlow binaries automatically installed within the directory containing the node module. If you have a custom build of TensorFlow you would like to use instead, you can suppress downloadinging the binaries at installation time.
export TENSORFLOW_LIB_PATH=path-to-custom-binaries npm install tensorflow
Note that the path you specify must be a directory that contains both
TensorFlow Setup and Docs
Note that to use the Python interface to build TensorFlow graphs and train models, you will also need to install TensorFlow directly within your Python environment.
pip install tensorflow==1.4.1
In the works, and more to come ...
Some things on the plan to be tackled.
- Support for string tensors.
- Support for high-level API (and saved models representing results of training)
- Support for Windows
Please file issues for feature suggestions, bugs or questions.