yorkie/tensorflow-nodejs
TensorFlow Node.js provides idiomatic JavaScript language bindings and a high layer API for Node.js users.
repo name | yorkie/tensorflow-nodejs |
repo link | https://github.com/yorkie/tensorflow-nodejs |
homepage | https://npmjs.org/package/tensorflow2 |
language | JavaScript |
size (curr.) | 293 kB |
stars (curr.) | 568 |
created | 2017-07-11 |
license | MIT License |
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
(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.
-
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
Forwardsdata
to the output port determined bypred
. -
merge
Forwards the value of an available tensor frominputs
tooutput
. -
enter
Creates or finds a child frame, and makesdata
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
Resizeimages
tosize
using area interpolation. -
resizeBicubic
Resizeimages
tosize
using bicubic interpolation. -
resizeBilinear
Resizeimages
tosize
using bilinear interpolation. -
resizeNearestNeighbor
Resizeimages
tosize
using nearest neighbor interpolation. -
randomCorp
Randomly cropimage
.
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.
-
softmax
Computes softmax activations. -
l2loss
L2 Loss, Computes half the L2 norm of a tensor without thesqrt
. -
lrn
Local Response Normalization. The 4-Dinput
tensor 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). -
relu
Computes rectified linear:max(features, 0)
. -
elu
Computes exponential linear:exp(features) - 1
if < 0,features
otherwise. See Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs). -
inTopK
Says whether the targets are in the topK
predictions.
Tests
$ npm test
License
MIT licensed @ 2017