Machine Learning in JavaScript: Libraries for Node.js
Although Python remains the dominant language in the field of machine learning, the JavaScript ecosystem (especially Node.js) is actively developing and offers powerful tools for ML. Using JavaScript allows you to create ML solutions that easily integrate with web applications, run in real-time on the server, or even in the browser. If your project already uses Node.js, integrating machine learning libraries can be more organic than switching to Python.
In this article, we will review key libraries for machine learning in JavaScript (Node.js), their features, and usage examples.
1. TensorFlow.js
TensorFlow.js is the most powerful and popular library for machine learning in JavaScript, ported from the original TensorFlow. It supports running models both in the browser and in Node.js, using hardware acceleration via CUDA or WebGL.
Key features:
- Running pre-trained models (MobileNet, BERT, YOLO).
- Training neural networks from scratch.
- Transfer learning (fine-tuning existing models).
- GPU support via CUDA on Node.js.
- Converting models from Python TensorFlow (TFJS format).
Example: Linear regression with TensorFlow.js
const tf = require('@tensorflow/tfjs-node');
// Data: x -> y (y = 2x - 1)const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
// Create modelconst model = tf.sequential();model.add(tf.layers.dense({units: 1, inputShape: [1]}));model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
// Trainingmodel.fit(xs, ys, {epochs: 200}).then(() => { // Predict for x = 5 model.predict(tf.tensor2d([5], [1, 1])).print();});2. Brain.js
Brain.js is a simple and intuitive library for neural networks. It is great for beginners and small projects. Supports multilayer perceptrons (MLP), recurrent neural networks (RNN), and long short-term memory networks (LSTM).
Key features:
- Minimal entry barrier — API in just a few lines.
- Support for asynchronous training to avoid blocking.
- Saving and loading trained models in JSON.
- Works both in Node.js and in the browser.
Example: XOR with Brain.js
const brain = require('brain.js');
const net = new brain.NeuralNetwork();
// Training data for XORnet.train([ {input: [0, 0], output: [0]}, {input: [0, 1], output: [1]}, {input: [1, 0], output: [1]}, {input: [1, 1], output: [0]}]);
// Testingconst output = net.run([1, 0]); // ~0.99console.log(output);3. Synaptic
Synaptic is an architecture-independent library for neural networks. It allows you to construct almost any type of network: perceptrons, multilayer networks, memory networks (LSTM), Hopfield networks, and others. The library has no external dependencies.
Key features:
- Flexibility in building architectures.
- Support for various training algorithms (SGD, Momentum, Nesterov).
- Network visualizer (in the browser).
- Support for saving/loading models.
Example: Simple neural network with Synaptic
const synaptic = require('synaptic');const {Layer, Network, Trainer} = synaptic;
const inputLayer = new Layer(2);const hiddenLayer = new Layer(3);const outputLayer = new Layer(1);
inputLayer.connect(hiddenLayer);hiddenLayer.connect(outputLayer);
const myNetwork = new Network({input: inputLayer, hidden: [hid Also in library
Rust PDF Guide: Complete Tutorial with Code Examples
Working with YAML in TypeScript: A Complete Guide to Parsing and Serialization
PDF Generation in Java: Overview of iText, Apache PDFBox, OpenPDF Libraries