Hi, my name is Beihai Zhang and I’m on a quest to make machine learning more fun, lower the learning barrier, and use it to develop interesting and valuable applications. I’ve covered a lot of gameplay aspects of machine learning applications, such as gRPC deploying trained machine learning models, using FastAPI to build machine learning apis, using Streamlit to quickly generate machine learning Web applications, and playing machine learning in Excel. ← Click Direct

I’ve been playing with tensorflow.js recently and plan to use it for my entire job. This article is a minimalist introduction to tensorflow.js.

TensorFlow.js

Tensorflow.js is an open source hardware-accelerated JavaScript library for training and deploying machine learning models. It allows us to train and deploy JavaScript libraries of machine learning models directly in the browser, making it very flexible for AI application development:

  • No software or drivers need to be installed (open a browser to use);

  • More convenient human-computer interaction can be carried out through the browser;

  • It can call various sensors of the mobile hardware (such as GPS, camera, etc.) through the mobile phone browser;

  • The user data can be performed locally without being uploaded to the server.

Tensorflow.js is powered primarily by WebGL and provides a high-level API for defining models, as well as low-level apis for linear algebra and automatic differentiation. Tensorflow. js supports importing TensorFlow SavedModels and Keras models.

Tensorflow. js environment configuration

The most convenient way to load tensorflow.js in a browser is to directly reference the installed JavaScript code packaged in the NPM package tensorflow.js is distributed in HTML.

<html>
<head>
   <script src="http://unpkg.com/@tensorflow/tfjs/dist/tf.min.js"></script>
Copy the code

You can also use tensorflow.js in Node.js, and the configuration is not too complicated:

Install Node.js NPM YARN

Node.js is a cross-platform JavaScript runtime environment built on Chrome’s JavaScript. NPM is the default package manager for Node.js and the largest software registry in the world.

sudo apt update
sudo apt install nodejs npm
Copy the code

If you already have Node.js installed, upgrade to the latest version

NPM install -g NPM install -g NPM install -g NPM install -g NPM  n stableCopy the code

The example of tensorflow. js will run with Yarn installed here. (NPM can survive without it)

Yarn is a package management tool similar to NPM. It has the following advantages: fast speed, offline mode, and version control.

The pit has been stepped on by everyone, please install it in the following way:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt update && sudo apt install yarn

yarn
Copy the code

Create tensorflow.js project directory:

$ mkdir tfjs
$ cd tfjs
Copy the code

Install TensorFlow. Js:

$NPM install @tensorflow/ NPM install @tensorflow/ NPM install @tensorflow/ NPM $NPM install @tensorflow/tfjs-node Support for CUDA GPU acceleration $NPM install@tensorflow/TFJS-node-gpuCopy the code

Verify that Node.js and tensorflow.js are working properly:

$node > the require (' @ tensorflow/TFJS) version {' TFJS - core ':' 1.3.1 ', 'TFJS - data' : '1.3.1', 'TFJS - the layers' : '1.3.1', 'TFJS - converter' : '1.3.1, TFJS:' 1.3.1} >Copy the code

If you see the output of TFJS-core, TFJS-data, TFJS-Layers, and TFJS-Converter above, then the environment configuration is ok.

Tensorflow.js can then be introduced in a JavaScript program with the following instruction:

Import * as tf from '@tensorflow/ TFJS 'console.log(tf.version.tfjs) // Output: 1.3.1Copy the code

Play and Eamples

Tensorflow.js can be played in several ways:

  • To run the official tensorflow.js model on a browser:

https://www.tensorflow.org/js/models/

  • Converting the Python model:https://www.tensorflow.org/js/tutorials#convert_pretained_models_to_tensorflowjs
  • Use transfer learning to customize models with your own data

https://www.tensorflow.org/js/tutorials/transfer/what_is_transfer_learning

  • Build and train models directly in JavaScripthttps://www.tensorflow.org/js/tutorials

The best resource to learn is tensorflow.js:

You can click on the link directly to experience the charm of Tensorflow.js

You can also clone the entire project and CD it to the sample folder:

# if you are using NPM: CD iris NPM install NPM run watchCopy the code