Written by F(X) Team-Rem

Three years ago

Some of you may have heard of a three-year-old library (github.com/yorkie/tens…) Yes, this is a project I abandoned a few years ago, and today, I’ve re-released 2.0 on NPM, and I’ve decided to start contributing code to the project again.

When I abandoned Tensorflow-nodejs three years ago, I felt that the maturity of Python’s machine learning ecosystem, starting with zero, or tensorFlow C API, was not possible in the medium to long term, so I abandoned it. Then tensorflow.js came out, but from the very beginning tensorflow.js was used in a browser environment, not node.js.

A few months ago, for the Pipcook project, we developed @pipcook/boa, which makes it easy for developers to call Python functions in node.js, which is how we plugged into the machine learning ecosystem in Pipcook.

Using Boa directly, I think there is still a high bar for other developers in the Node.js ecosystem to read the Python documentation and understand the syntax and runtime differences between Python and JavaScript.

Correct posture for using TensorFlow in Node.js

That’s why I wanted to republish Tensorflow-nodejs. With BOA I can reduce a lot of the work I had done migrating and porting TensorFlow, and based on the underlying Python API, The tensorflow-nodejs API has been redesigned for Node.js developers, making it easier for developers with no Python experience to get started. The biggest benefit is that tensorflow-nodejs can now evolve with the Python version at any time. And the cost of upgrading is very small.

It took me about 30 minutes to write a simple Keras API training example:

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');
Copy the code

The code above can be done in less than a minute and is very simple to understand.

Yorkie welcomes you to contribute more to the Tensorflow API. Here’s the code: github.com/yorkie/tens… , the implementation process is relatively simple, just need to call the corresponding Python interface according to the designed API, and then do some parameter conversion in the outer layer of the function.

Finally, I look forward to your contributions, which may be the most correct machine learning posture in Node.js!