TensorFlow.js is a game-changer. It allows you to train and deploy machine learning models directly in the web browser using JavaScript

Introducción

Machine learning has traditionally been the domain of Python and C++ developers, but TensorFlow.js opened this field to the vast community of JavaScript developers. This shift proved a monumental success in the 2020s, as it brought ML capabilities directly into web browsers and the Node.js environment, enabling a new class of on-device computation applications. TensorFlow.js is a bridge connecting the extensive JavaScript community to the power of machine learning.

It has a unique architecture that makes it incredibly efficient for web-based applications. Built on WebGL, it can utilize the GPU or TPUs for computations. This shift meant that even complex models could run smoothly in the browser when people had to deploy a model through an API endpoint and explicitly create an entire software architecture around it while maintaining the MLOps and DevOps teams (though there is a different need for these roles now). The library provides a flexible and intuitive API for defining and training models, and it supports importing pre-trained models from TensorFlow Python.

The development of TensorFlow.js is rooted in the unique constraints and opportunities of the JavaScript environment. It addresses key challenges such as performance limitations in the browser, where JavaScript, being an interpreted language, traditionally couldn’t match the speed of compiled languages for numerical computation. To overcome this, TensorFlow.js leveraged emerging JS standards like WebAssembly and WebGL. These technologies enable TensorFlow.JS to perform complex ML tasks efficiently in the browser, a challenging feat.

Entorno de trabajo

Configurar un proyecto TensorFlow.j

Servidor

Utiliza una máquina Linux (WSL) !!

bun install node-pre-gyp 
bun install @tensorflow/tfjs-node
npm rebuild @tensorflow/tfjs-node --build-addon-from-source

Windows & OSX build support for node-gyp requires that you have installed a supported version of Python. Be sure to have supported version of Python before installing @tensorflow/tfjs-node or @tensorflow/tfjs-node-gpu.

For more troubleshooting on Windows, check out WINDOWS_TROUBLESHOOTING.md.

El módulo tfjs-node proporciona ejecución nativa de TensorFlow en aplicaciones JavaScript bajo el tiempo de ejecución de Node.js, acelerada por el binario TensorFlow C.

Bun

TensorFlow.js now works

There was an outstanding bug in Bun's implementation of napi_create_string_utf16 and napi_create_arraybuffer that prevented TensorFlow.js from working. This has now been fixed.

Cliente

https://www.npmjs.com/package/@tensorflow/tfjs

npm install @tensorflow/tfjs 

El siguiente ejemplo muestra cómo importar TensorFlow.js, definir un modelo y entrenarlo.

import * as tf from '@tensorflow/tfjs';

// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));

model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

// Train the model using the data.
model.fit(xs, ys, {epochs: 10}).then(() => {
 // Use the model to do inference on a data point the model hasn't seen before:
 model.predict(tf.tensor2d([5], [1, 1])).print();
 // Open the browser devtools to see the output
});

since the version 1.3 of tfjs-node, it is possible to load directly the savedModel in js (only possible in nodejs) using loadSavedModel.

https://www.npmjs.com/package/@tensorflow/tfjs-node : Windows & OSX build support for node-gyp requires that you have installed a supported version of Python.

Neural Network

TODO