abot-tensorflow v0.0.1
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 --saveUsage
Run a predefined graph
The ability to run a predefined graph is the most basic function for any TensorFlow client library.
Given a
GraphDef(orMetaGraphDef) 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.
variableHolds 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.assignUpdate '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
randomUniformOutputs random values from a uniform distribution.randomUniformIntOutputs random integers from a uniform distribution.randomGammaOutputs random values from the Gamma distribution(s) described by alpha.randomPoissonOutputs random values from the Poisson distribution(s) described by rate.
Array
placeholderA placeholder op for a value that will be fed into the computation.constReturns a constant tensor.reverseReverses specific dimensions of a tensor.shapeReturns the shape of a tensor.rankReturns the rank of a tensor.sizeReturns the size of a tensor.onehotReturns a one-hot tensor.
Base64
encodeEncode strings into web-safe base64 format.decodeDecode web-safe base64-encoded strings.
Flow
switchForwardsdatato the output port determined bypred.mergeForwards the value of an available tensor frominputstooutput.enterCreates or finds a child frame, and makesdataavailable to the child frame.exitExits the current frame to its parent frame.abortRaise an exception to abort the process when called.triggerDoes nothing and serves as a control trigger for scheduling.
Image
decodeJpegDecode a JPEG-encoded image to a uint8 tensor.encodeJpegJPEG-encode an image.resizeAreaResizeimagestosizeusing area interpolation.resizeBicubicResizeimagestosizeusing bicubic interpolation.resizeBilinearResizeimagestosizeusing bilinear interpolation.resizeNearestNeighborResizeimagestosizeusing nearest neighbor interpolation.randomCorpRandomly cropimage.
Audio
decodeWavDecode a 16-bit PCM WAV file to a float tensor.encodeWavEncode audio data using the WAV file format.spectrogramProduces a visualization of audio data over time.mfccTransforms 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.
softmaxComputes softmax activations.l2lossL2 Loss, Computes half the L2 norm of a tensor without thesqrt.lrnLocal Response Normalization. The 4-Dinputtensor is treated as a 3-D array of 1-D vectors (along the last dimension), and each vector is normalized independently. Within a given vector, each component is divided by the weighted, squared sum of inputs withindepth_radius. For details, see Krizhevsky et al., ImageNet classification with deep convolutional neural networks (NIPS 2012).reluComputes rectified linear:max(features, 0).eluComputes exponential linear:exp(features) - 1if < 0,featuresotherwise. See Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs).inTopKSays whether the targets are in the topKpredictions.
Tests
$ npm testLicense
MIT licensed @ 2017
8 years ago