/tensorflow-nodejs

TensorFlow Node.js provides idiomatic JavaScript language bindings and a high layer API for Node.js users. TensorFlow Node.js provides idiomatic JavaScript language bindings and a high layer API for Node.js users.

Primary LanguageJavaScriptMIT LicenseMIT

TensorFlow for Node.js

NPM Dependency Build Coverage
NPM version Dependency Status Build Status Coverage

This library wraps Tensorflow Python for Node.js developers, it's powered by @pipcook/boa.

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

const tf = require('tensorflow2');

// load mnist dataset.
const dataset = tf.keras.dataset.mnist();
// {
//   train: { x: [Getter], y: [Getter] },
//   test: { x: [Getter], y: [Getter] }
// }

// create model.
const model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten({
    input_shape: [28, 28]
  }),
  tf.keras.layers.Dense(128, {
    activation: 'relu'
  }),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10)
]);
model.summary();

// compile the model.
const loss_fn = tf.keras.losses.SparseCategoricalCrossentropy({ from_logits: true });
model.compile({
  optimizer: 'adam',
  loss: loss_fn,
  metrics: [ 'accuracy' ],
});

// train the model.
model.fit(dataset.train.x, dataset.train.y, { epochs: 5 });

// save the model
model.save('your-model.h5');

See example/mnist.js for complete example.

Tests

$ npm test

License

MIT licensed @ 2020