All Projects → nikhilk → Node Tensorflow

nikhilk / Node Tensorflow

Licence: apache-2.0
Node.js + TensorFlow

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Node Tensorflow

Node Thermal Printer
This npm package was made to control epson and star thermal printers
Stars: ✭ 424 (-27.02%)
Mutual labels:  npm-package
Tfjs Yolo Tiny
In-Browser Object Detection using Tiny YOLO on Tensorflow.js
Stars: ✭ 465 (-19.97%)
Mutual labels:  npm-package
Crypto Hash
Tiny hashing module that uses the native crypto API in Node.js and the browser
Stars: ✭ 501 (-13.77%)
Mutual labels:  npm-package
Trex
Package Manager for deno 🦕
Stars: ✭ 433 (-25.47%)
Mutual labels:  npm-package
Electron Better Ipc
Simplified IPC communication for Electron apps
Stars: ✭ 463 (-20.31%)
Mutual labels:  npm-package
Vanilla Framework
From community websites to web applications, this CSS framework will help you achieve a consistent look and feel.
Stars: ✭ 476 (-18.07%)
Mutual labels:  npm-package
Negative Array
Negative array index support `array[-1]` using ES2015 Proxy
Stars: ✭ 420 (-27.71%)
Mutual labels:  npm-package
Command Line Args
A mature, feature-complete library to parse command-line options.
Stars: ✭ 525 (-9.64%)
Mutual labels:  npm-package
Electron Reloader
Simple auto-reloading for Electron apps during development
Stars: ✭ 463 (-20.31%)
Mutual labels:  npm-package
Strip Json Comments
Strip comments from JSON. Lets you use comments in your JSON files!
Stars: ✭ 492 (-15.32%)
Mutual labels:  npm-package
Sindresorhus Cli
The Sindre Sorhus CLI
Stars: ✭ 436 (-24.96%)
Mutual labels:  npm-package
Cloudinary npm
Cloudinary NPM for node.js integration
Stars: ✭ 450 (-22.55%)
Mutual labels:  npm-package
Angular Translate
Translating your AngularJS 1.x apps
Stars: ✭ 4,414 (+659.72%)
Mutual labels:  npm-package
Npm Run All
A CLI tool to run multiple npm-scripts in parallel or sequential.
Stars: ✭ 4,496 (+673.84%)
Mutual labels:  npm-package
Synp
Convert yarn.lock to package-lock.json and vice versa
Stars: ✭ 510 (-12.22%)
Mutual labels:  npm-package
Ky Universal
Use Ky in both Node.js and browsers
Stars: ✭ 421 (-27.54%)
Mutual labels:  npm-package
Ignite Ui
Ignite UI by Infragistics
Stars: ✭ 468 (-19.45%)
Mutual labels:  npm-package
Capture Website Cli
Capture screenshots of websites from the command-line
Stars: ✭ 545 (-6.2%)
Mutual labels:  npm-package
Normalize Url
Normalize a URL
Stars: ✭ 512 (-11.88%)
Mutual labels:  npm-package
Type Fest
A collection of essential TypeScript types
Stars: ✭ 6,623 (+1039.93%)
Mutual labels:  npm-package

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.

It is interesting to be able to use TensorFlow in a node.js application using just JavaScript (or TypeScript if that's your preference). However, the Python functionality is vast (several ops, estimator implementations etc.) and continually expanding. Instead, it would be more practical to consider building Graphs and training models in Python, and then consuming those for runtime use-cases (like prediction or inference) in a pure node.js and Python-free deployment. This is what this node module enables.

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 tf

with tf.Graph().as_default() as graph:
  c1 = tf.constant(1, name='c1')
  c2 = tf.constant(41, name='c2')
  result = tf.add(c1, c2, name='result')

  tf.train.write_graph(graph, '.', 'trivial.graph.proto', as_text=False)

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 = require('tensorflow');

// Load the Graph and create a Session to be able to run the operations
// defined in the graph.
let graph = tf.graph('trivial.graph.proto');
let session = graph.createSession();

// Run to evaluate and retrieve the value of the 'result' op.
let result = session.run(/* inputs */ null,
                         /* outputs */ 'result',
                         /* targets */ null);

// The result is a Tensor, which contains value, type and shape fields.
// This Should print out '42'
console.log(result.value);
    
// Cleanup
graph.delete();

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 tf

with tf.Graph().as_default() as graph:
  var1 = tf.placeholder(dtype=tf.int32, shape=[2,2], name='var1')
  var2 = tf.placeholder(dtype=tf.int32, shape=[2,1], name='var2')
  var3 = tf.Variable(initial_value=[[1],[1]], dtype=tf.int32)

  tf.variables_initializer(tf.global_variables(), name='init')

  with tf.name_scope('computation'):
    tf.add(tf.matmul(var1, var2), var3, name='result')

  tf.train.write_graph(graph, '.', 'graph.proto', as_text=False)

Here is the corresponding node.js snippet to work with the Graph defined above:

const tf = require('tensorflow');

let graph = tf.graph('graph.proto');
let session = graph.createSession();

// Run the 'init' op to initialize variables defined in the graph.
session.run(null, null, 'init');

// 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.tensor([[2,2],[4,4]], tf.Types.int32);
let b = tf.tensor([[3],[5]], tf.Types.int32);

// You can fetch multiple outputs as well.
let outputs = session.run({ var1: a, var2: b }, ['var3', 'computation/result']);
console.log(outputs.var3.value)
console.log(outputs['computation/result'].value);
    
graph.delete();

Installation

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 libtensorflow.so and libtensorflow_framework.so.

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

For more information, check out the TensorFlow install and API documentation.

In the works, and more to come ...

Some things on the plan to be tackled.

  • Support for high-level API (and saved models representing results of training)
  • Support for Windows

Please file issues for feature suggestions, bugs or questions.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].