for Node.js
NPM | Dependency | Build | 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');
const op = graph.operations.get('my_variable/Assign');
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.
Random
Array
Base64
Flow
Image
Audio
Neural networks
In this module, it implements the following algorithms for representing neural networks.
Tests
$ npm test
License
MIT licensed @ 2017