Record what you know about tensorflow.js

in Notes with 13 comments

TensorFlow.js This is an open source library that can use JavaScript and advanced layer API to define, train and run complete machine learning models in browsers. If you're new to ml, tensorflow. JS is a good way to start learning. Or, if you're an ML developer new to JavaScript, read on to learn more about new opportunities for ml in browsers. Let's start with it.

Operating conditions

Ml running in a browser means that from the user's point of view, no libraries or drivers need to be installed. Just open a web page and your program will run. In addition, it is ready to use the GPU to speed up. Tensorflow. JS automatically supports webgl and accelerates code in the background when GPU is available. Users can also open your web page via a mobile device, in which case your model can use sensor data, such as gyroscopes or acceleration sensors. Finally, all data is retained on the client, making tensorflow. JS available for low latency scenarios and stronger privacy protection.

How to use it

If you use tensorflow. JS for development, you can consider the following three workflows.

Code working process

Let's first define a model through a set of layers

 import * as tf from ‘@tensorflow/tfjs’;
const model = tf.sequential();
model.add(tf.layers.dense({inputShape: [4], units: 100}));
model.add(tf.layers.dense({units: 4}));
model.compile({loss: ‘categoricalCrossentropy’, optimizer: ‘sgd’});

The layers API we use supports keras layers, including dense, CNN, LSTM, etc.; we can use the same keras compatible API to train our model

 await model.fit(
  xData, yData, {
    batchSize: batchSize,
    epochs: epochs
});

After training, the model can do the prediction work

 // Get measurements for a new flower to generate a prediction
// The first argument is the data, and the second is the shape.
const inputData = tf.tensor2d([[4.8, 3.0, 1.4, 0.1]], [1, 4]);

// Get the highest confidence prediction from our model
const result = model.predict(inputData);
const winner = irisClasses[result.argMax().dataSync()[0]];

//  Display the winner
console.log(winner);

Architecture Principle

Take a look at the figure below

 0_ oY2OG7MFBN4eK1AN.png

Tensorflow.js is mainly supported by webgl, and tensorflow.js provides a high-level API for defining models and a low-level API for linear algebra and automatic differentiation. Tensorflow. JS supports importing tensorflow savedmodels and keras models.

FAQ

Does tensorflow.js support node.js now?
Not yet, but Google has started to write and call tensorflow C API for node.js, which will allow JavaScript to run directly on node.js and browsers. And it's a high priority within tensorflow. JS.

Can I import tensorflow or keras model into browser?
Of course, the tutorial of importing tensorflow savedmodel: [here ], import keras HDF5 models tutorial: [here ]

Do you support exporting models?
Not yet, but it will be supported soon, because it is also a high priority within tensorflow.js.

The relationship between tensorflow.js and tensorflow?
Tensorflow.js API is similar to tensorflow Python API. Of course, it is not fully supported at present. The same API will be provided as Python API later

What is the current performance of tensorflow.js?
Compared with tensorflow python with AVX, tensorflow.js with webgl will be 1.5 to 2 times slower for prediction tasks; for training tasks, tensorflow.js with webgl will be faster, but for large models, it will be about 10-15 times slower.

What's the difference between tensorflow.js and deeplearn.js?
Tensorflow.js is developed from deeplearn.js and provides a set of JavaScript tools around deep learning. Deeploarn. JS can be considered as the core of tensorflow. JS.

link

Responses
  1. KJ test

    This is a test speech

    Reply
  2. Little Z

    Is the article directory plug-in or code integrated?

    Reply
  3. After all, orz is not the main attack

    Reply
  4. Praise, praise.

    Reply
    1. CHENLUN
      @haoessays

      Good collection

      Reply
  5. On the afternoon

    Good

    Reply
  6. I want to ask why the Chinese version is not available? Of course, we can not change the main chain!

    Reply
    1. Moon feather soul rain
      @Niutian

      Can you send me a Chinese version

      Reply
    2. @Niutian

      I look forward to your email reply or QQ contact Oh, QQ email, as long as the number is QQ!

      Reply
      1. @Niutian

        Can you send me Chinese??

        Reply