Home | Send Feedback | Share on Bluesky |

Machine Learning with brain.js and Tensorflow.js

Published: 4. January 2019  •  Updated: 23. October 2026  •  javascript

In my previous blog post, I showed you a simple neural network implementation in Java and JavaScript, built from scratch based on the algorithms described in Tariq Rashid's book, "Make Your Own Neural Network".

While writing a machine learning algorithm from scratch is interesting and useful for learning purposes, in practice, you will instead use one of the many machine learning libraries. Many of these libraries were written for Python, the primary programming language in the machine learning space. But there are libraries available for other languages. In this blog post, we look at two JavaScript libraries: brain.js and Tensorflow.js.

We are going to recreate the MNIST example from my previous blog post and build a Multilayer perceptron with brain.js and a Convolutional neural network with Tensorflow.js.

An MLP is a feedforward neural network with at least three layers of nodes: an input layer, one or more hidden layers, and an output layer. This is basically the same network I built from scratch in my previous blog post. A convolutional neural network, on the other hand, is a special kind of neural network that works well in computer vision.

You can find the source code for this example on GitHub:
https://github.com/ralscha/blog2019/tree/main/mnistjs

Draw a digit and see if the program can detect it correctly.

mnistjs

Training

brain.js

brain.js is a JavaScript library that runs on Node.js and in browsers. The source code of brain.js is hosted on GitHub and released under the MIT license. This example uses version 1.6.1, which provides support for recurrent neural network (RNN), long short-term memory, and gated recurrent unit networks.

You add the library to your project, like any other JavaScript library, with npm or yarn.

npm install brain.js

For downloading the MNIST datasets, extracting, and converting them into arrays, I wrote a few utility methods. You can find the source code for these methods here: mnist.js

In the training program, we first require the necessary libraries and then call the utility methods from mnist.js to get the images and labels for the training and test datasets.

const fs = require('fs');
const mnist = require('./mnist');
const brain = require('brain.js');

class AugmentedNetwork extends brain.NeuralNetwork {
    train(data, options) {
        const augmented = data.flatMap(({ input, output }) => [
            { input, output },
            { input: mnist.rotate(input, 10), output },
            { input: mnist.rotate(input, -10), output }
        ]);
        return super.train(augmented, options);
    }
}


async function run() {

    const trainLabels = await mnist.getTrainLabels();
    const trainDigits = await mnist.getTrainImages();

    const testLabels = await mnist.getTestLabels();
    const testDigits = await mnist.getTestImages();

    const labels = trainLabels;
    const digits = trainDigits;

train.js

The demo passes only the 60,000 training images to CrossValidate. The official 10,000-image test set stays separate and is evaluated once after selecting the network.

labels and digits are one-dimensional JavaScript arrays. We can't feed them directly to brain.js in this form and first have to transform them into a structure that the library understands.

Each training object needs to be a JavaScript object with two properties: input and output. The values can be either an array of numbers between 0 and 1 or a hash of numbers between 0 and 1.

Example:

{input: [0, 0], output: [0]}

For this demo, we store the values into arrays. The input array consists of 784 entries (28 x 28) and represents the pixels of one scanned MNIST image. The output is a 10-element array and designates the digit the scanned image contains. For instance, if we feed a scanned image of a one, the training object looks similar to this:

{input: [0, 0, 1, 0, .......744 more entries], output: [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]}

In the output array, at index position 1, the value 1 is set, denoting that the input contains the scanned image of a one.

Here is the code that transforms the one-dimensional arrays from the MNIST download into an array of training objects.

    const imageSize = 28 * 28;
    const trainingData = [];

    for (let ix = 0; ix < labels.length; ix++) {
        const start = ix * imageSize;
        const input = digits.slice(start, start + imageSize).map(mnist.normalize);
        const output = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        output[labels[ix]] = 1;
        trainingData.push({ input, output });

    }

train.js

The AugmentedNetwork class creates two rotated variants only when training a fold. Cross-validation splits the original images first, keeping related versions out of the validation fold. Validation and final test images remain unaugmented.

Next, we specify the topology of the network and set some training options. For the topology, the only mandatory option is the number of nodes in the hidden layer.

    const netOptions = {
        hiddenLayers: [150]
    };

    const trainingOptions = {
        iterations: 20000,
        log: details => console.log(details)
    };

train.js

brain.js stops training when either the training error has gone below a certain threshold (by default 0.005), or the maximum number of iterations has been reached. In my tests, the application reaches the training error threshold after a few iterations.

Lastly, we create the network, feed the training data, and train the model.

    const crossValidate = new brain.CrossValidate(AugmentedNetwork, netOptions);
    const stats = crossValidate.train(trainingData, trainingOptions);

train.js

After the training has finished, the application picks the best neural network from the cross-validation, exports it into a JavaScript object, converts it to JSON, and writes it into the file ./data/model.json.

    const net = crossValidate.toNeuralNetwork();

    console.log('Cross-validation statistics:', stats.stats);
    let correct = 0;
    for (let ix = 0; ix < testLabels.length; ix++) {
        const start = ix * imageSize;
        const input = testDigits.slice(start, start + imageSize).map(mnist.normalize);
        const output = Array.from(net.run(input));
        if (output.indexOf(Math.max(...output)) === testLabels[ix]) {
            correct++;
        }
    }
    console.log(`Held-out test accuracy: ${(100 * correct / testLabels.length).toFixed(2)}%`);

    const model = net.toJSON();
    fs.writeFile('./data/model.json', JSON.stringify(model), 'utf8', () => console.log('model has been written'));

train.js

To start the training, run the following command on the command line:

node --max-old-space-size=4096 ./train.js

The program reads a lot of data into memory. --max-old-space-size=4096 sets the V8 old-generation heap limit to 4096 MiB; it does not cap all process memory. The default depends on the Node.js/V8 version and available memory. Leave room for native tensors and the operating system. See the Node.js option documentation.

The program reports accuracy on the held-out test set after selecting the network.


Tensorflow.js

Tensorflow.js is a JavaScript library that runs on Node.js and in browsers. It was developed by the Google Brain team and based on the Tensorflow Python library. The source code is hosted on GitHub and released under the Apache License 2.0.

TensorFlow.js can run as pure JavaScript in Node.js, or use native TensorFlow bindings. For the native CPU backend, install tfjs-node:

npm install @tensorflow/tfjs-node

The native GPU package targets Linux with an NVIDIA GPU and a compatible CUDA installation. Performance depends on the workload and hardware:

npm install @tensorflow/tfjs-node-gpu

See the Setup Page and TensorFlow.js Node.js guide for more information.

Import the required libraries into the training application:

const mnist = require('./mnist');
const tf = require('@tensorflow/tfjs');

tfjs.js

Training with Tensorflow.js follows the same workflow as brain.js. Import the MNIST dataset, convert it into a form that Tensorflow.js understands, create the network, train it, and lastly, export the trained model to a file.

After downloading and reading the MNIST dataset, we have to convert it into tensors. Tensors are the core unit of work in Tensorflow.js and are essentially JavaScript objects that wrap a collection of numbers.

As input, we create a 4-dimensional tensor with a shape of [60,000, 28, 28, 1], and as the expected output, we create a 10-element array for each image and put all of them into a two-dimensional tensor. TensorFlow.js accepts separate training and validation tensors. Keep the official test set separate from both.

async function run() {

    const trainLabels = await mnist.getTrainLabels();
    const trainDigits = await mnist.getTrainImages();

    const testLabels = await mnist.getTestLabels();
    const testDigits = await mnist.getTestImages();

    const trainingOrder = Array.from(tf.util.createShuffledIndices(trainLabels.length));
    const [trainDigitsTensor, trainLabelsTensor] = tf.tidy(() => {
        const images = tf.tensor4d(trainDigits.map(mnist.normalize), [trainLabels.length, 28, 28, 1]);
        const labels = tf.oneHot(tf.tensor1d(trainLabels, 'int32'), 10);
        return [tf.gather(images, trainingOrder), tf.gather(labels, trainingOrder)];
    });

    const testDigitsTensor = tf.tensor4d(testDigits.map(mnist.normalize),
        [testDigits.length / (28 * 28), 28, 28, 1]);

    const testOutputs = [];
    for (let ix = 0; ix < testLabels.length; ix++) {
        const output = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        output[testLabels[ix]] = 1;
        testOutputs.push(output);
    }
    const testLabelsTensor = tf.tensor2d(testOutputs);

tfjs.js

Next, the application creates the model:

    const model = createConvModel();

tfjs.js

This model is a copy from the official tensorflow MNIST example. On the Tensorflow.js homepage, you can find a description of all these layers and parameters, what exactly they do, and why they are configured this way.

I also recommend the following article as an introduction to convolutional networks: https://web.archive.org/web/20250315082250/https://towardsdatascience.com/deep-learning-for-beginners-practical-guide-with-python-and-keras-d295bfca4487

function createConvModel() {
    const model = tf.sequential();

    model.add(tf.layers.conv2d({
        inputShape: [28, 28, 1],
        kernelSize: 3,
        filters: 16,
        activation: 'relu'
    }));

    model.add(tf.layers.maxPooling2d({ poolSize: 2, strides: 2 }));
    model.add(tf.layers.conv2d({ kernelSize: 3, filters: 32, activation: 'relu' }));
    model.add(tf.layers.maxPooling2d({ poolSize: 2, strides: 2 }));
    model.add(tf.layers.conv2d({ kernelSize: 3, filters: 32, activation: 'relu' }));
    model.add(tf.layers.flatten({}));
    model.add(tf.layers.dense({ units: 64, activation: 'relu' }));
    model.add(tf.layers.dense({ units: 10, activation: 'softmax' }));

    return model;
}

tfjs.js

After setting up the model, the application starts training with fit. The first two parameters are the training input and expected output tensors. The configuration reserves 10% of the training set for validation with validationSplit: 0.1; the official test set is used only by the final evaluate call.

    model.compile({
        optimizer: 'rmsprop',
        loss: 'categoricalCrossentropy',
        metrics: ['accuracy'],
    });

    const batchSize = 320;
    const trainEpochs = 20;

    await model.fit(trainDigitsTensor, trainLabelsTensor, {
        batchSize,
        validationSplit: 0.1,
        shuffle: true,
        epochs: trainEpochs
    });

tfjs.js

The program applies the same random permutation to the training images and labels before creating that split. This matters because validationSplit reserves the last portion before per-epoch shuffling. See LayersModel.fit.

After the training has been completed, the application saves the model.

    const saveResults = await model.save('file://./data/tfjsmnist');

tfjs.js

To start the training, run the following command in a shell:

node --max-old-space-size=4096 ./tfjs.js

Web Application

The web application is written in TypeScript and uses the Ionic and Angular frameworks.

When you follow this blog post and want to run the example locally, you need to copy the two models from the training step into the ./client/src/assets folder. The web application handles these models like any other static resources and loads them with GET requests.

brain.js

Add the library to the project with npm or yarn.

npm install brain.js

In TypeScript, we need to import the library with the following statement.

import * as brain from 'brain.js/browser';

home.page.ts

Note that we import the browser library. The library for Node.js does not run inside a browser.

During startup, the application loads the model and creates the network.

  async initBrain(): Promise<void> {
    const response = await fetch('assets/model.json');
    const brainModel = await response.json();
    this.net = new brain.NeuralNetwork();
    this.net.fromJSON(brainModel);
  }

home.page.ts

Notice that we don't have to configure the topology of the network. In the training application, we had to specify the number of nodes in the hidden layer. We don't have to do that here because the file model.json contains metadata about the network, and brain.js automatically recreates the network based on this data.

The web application displays an empty canvas where the user can draw a digit. The application then tries to detect what number the user has drawn. To achieve that, the application extracts the pixel values from the canvas and feeds them into the models.

    const imageData = copyContext.getImageData(0, 0, 28, 28);

    const numPixels = imageData.width * imageData.height;
    const values = new Array<number>(numPixels);
    for (let i = 0; i < numPixels; i++) {
      values[i] = imageData.data[i * 4 + 3] / 255.0;
    }

home.page.ts

The code then has to make sure that the data is transformed into the same shape and form as the input object used in the model training. In this case, a one-dimensional array with 784 (28 * 28) entries. Then we pass this array to the run method, and brain.js returns a 10-element array. Next, the application looks for the array index that contains the largest value (maxScore) and shows this to the user as the most likely guess.

    const detection = this.net.run(values);
    this.detectedNumberMLP = this.maxScore(detection);

home.page.ts


tensorflow.js

Like brain.js, you install Tensorflow.js with yarn or npm:

npm install @tensorflow/tfjs

Then import it:

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

home.page.ts

Loading a pre-trained model is even easier with Tensorflow.js because the library provides the loadLayersModel method for this case, as used in the current linked client.

  async initTf(): Promise<void> {
    this.tfModel = await tf.loadLayersModel('assets/tfjsmnist/model.json');
  }

home.page.ts

We don't have to configure the model beforehand. The model.json file contains a description of the network topology, and Tensorflow.js automatically recreates all the network layers we used during the training.

Running the user-generated data through the model follows the same workflow as the brain.js code. Extract the image data from the canvas and transform it into a form that Tensorflow.js understands. We have to make sure that the tensor we pass to the predict method has the same dimensions as the tensors we used for training. In this case, it must be a 4-dimensional tensor. The method will complain if the tensor doesn't match the training tensor.

    const predictTensor = this.tfModel.predict(tf.tensor4d(values, [1, 28, 28, 1])) as tf.Tensor;
    const data = predictTensor.dataSync<'float32'>();
    this.detectedNumberCNN = this.indexMax(data);
    this.detectionsCNN = data;

home.page.ts

To extract the array from the prediction tensor, we call the synchronous method dataSync, which returns, in this case, an array with 10 elements. Alternatively, you can extract the array with data, which is asynchronous and returns a Promise. The code then loops over the array and looks for the array index with the largest value (indexMax).

In this example, the application displays the 10 output elements to the user. If you are only interested in the index, you don't need to extract the underlying array from the tensor. Instead, you call argMax to extract the index with the largest value directly from the tensor. Like most methods in Tensorflow.js, this also returns a tensor, in this case, a one-dimensional tensor with just one element.

this.detectedNumberCNN = tf.argMax(predictTensor, 1).dataSync()[0];

This concludes this article. We created two neural networks with brain.js and Tensorflow.js, trained them with the MNIST dataset, and then used these models for detecting digits users draw into a canvas.