Blog / Others/ Build a Neural Network in 30 Lines of JavaScript with Synaptic.js

Build a Neural Network in 30 Lines of JavaScript with Synaptic.js

Introduction

This guide shows you how to build and train a simple neural network in about 30 lines of JavaScript. We'll use the Synaptic.js library to create a model that learns the XOR (exclusive OR) logic operation, running in the browser or Node.js.

Neural Network Basics

Before coding, let's review key concepts.

Neurons and Synapses

A neuron is the basic unit. It receives inputs, performs a calculation, and produces an output. We'll use Sigmoid neurons, whose activation function maps any input to a value between 0 and 1.

A neuron's input is typically the weighted sum of outputs from the previous layer, plus a bias. Weights and biases are the key parameters learned during training.

Forward and Backward Propagation

Connecting neurons in layers forms a network. The flow of data from input to output is forward propagation.

Training aims to make the network generalize, e.g., classify data correctly. We feed it many samples, compute the error between its prediction and the true value, then use the backpropagation algorithm to send this error backward through the layers, adjusting weights and biases to improve future predictions. This repeats thousands of times.

Build a Neural Network with Synaptic.js

Now, let's write the code.

1. Create the Network Structure

First, create layers with new Layer(), specifying the number of neurons.

const { Layer, Network } = require('synaptic');

const inputLayer = new Layer(2);
const hiddenLayer = new Layer(3);
const outputLayer = new Layer(1);

Then, connect the layers and instantiate the network:

inputLayer.project(hiddenLayer);
hiddenLayer.project(outputLayer);

const myNetwork = new Network({
  input: inputLayer,
  hidden: [hiddenLayer],
  output: outputLayer
});

This creates a 2-3-1 network (2 inputs, 3 hidden neurons, 1 output).

2. Train the Neural Network

We'll train it on XOR. The XOR truth table: output 0 for [0,0] or [1,1]; output 1 for [0,1] or [1,0].

const learningRate = 0.3;
const trainingData = [
  { input: [0, 0], output: [0] },
  { input: [0, 1], output: [1] },
  { input: [1, 0], output: [1] },
  { input: [1, 1], output: [0] }
];

for (let i = 0; i < 20000; i++) {
  trainingData.forEach(data => {
    myNetwork.activate(data.input);
    myNetwork.propagate(learningRate, data.output);
  });
}

Each iteration performs forward and backward propagation for all four inputs. The propagate method updates internal parameters based on the error between network output and target, using the learning rate.

3. Test the Trained Model

After training, test the network's predictions:

console.log(myNetwork.activate([0, 0])); // Should be near [0]
console.log(myNetwork.activate([0, 1])); // Should be near [1]
console.log(myNetwork.activate([1, 0])); // Should be near [1]
console.log(myNetwork.activate([1, 1])); // Should be near [0]

Rounding the outputs to the nearest integer should give the correct XOR result.

Conclusion and Next Steps

You've successfully created and trained a neural network in JavaScript. While this is just an introduction to deep learning, it provides a foundation for using the Synaptic.js library. Explore its GitHub Wiki for advanced tutorials and examples.

Note: This example uses Synaptic.js. While still functional, modern alternatives like TensorFlow.js exist, but the core concepts and training process remain similar.

Post a Comment

Your email will not be published. Required fields are marked with *.