Neural networks in JavaScript

A fun and practical Brain.js tutorial. This 19-part course teaches you how to build neural networks in JavaScript through interactive Scrimba tutorials.

    // input 0 0, output 0
    // input 0 1, output 1
    // input 1 0, output 1
    // input 1 1, output 0
    
    const net = new brain.NeuralNetwork({ hiddenLayers: [3] });
    
    const trainingData = [
      { input: [0, 0], output: [0] },
      { input: [0, 1], output: [1] },
      { input: [1, 0], output: [1] },
      { input: [1, 1], output: [0] }
    ];
    
    // input 0 0, output 0
    // input 0 1, output 1
    // input 1 0, output 1
    // input 1 1, output 0
    
    const net = new brain.NeuralNetwork({ hiddenLayers: [3] });
    
    const trainingData = [
      { input: [0, 0], output: [0] },
      { input: [0, 1], output: [1] },
      { input: [1, 0], output: [1] },
      { input: [1, 1], output: [0] }
    ];
    
    // input 0 0, output 0
    // input 0 1, output 1
    // input 1 0, output 1
    // input 1 1, output 0
    
    const net = new brain.NeuralNetwork({ hiddenLayers: [3] });
    
    const trainingData = [
      { input: [0, 0], output: [0] },
      { input: [0, 1], output: [1] },
      { input: [1, 0], output: [1] },
      { input: [1, 1], output: [0] }
    ];
    
    // inputs & outputs
    (inputs) => outputs;
    
    // random values
    Math.random();
    
    // activation "relu"
    function relu(value) {
        return value < 0 ? 0 : value;
    }
    
    // bonus material
    // https://en.wikipedia.org/wiki/Activation_function
    /*
    const net = new brain.NeuralNetwork({ hiddenLayers: [2, 2] });
    
    const trainingData = [
      { input: [0, 0], output: [0] },
      { input: [0, 1], output: [1] },
      { input: [1, 0], output: [1] },
      { input: [1, 1], output: [0] }
    ];
    
    net.train(trainingData, {
        log: (error) => console.log(error)
    });
    
    
    // input: { red, green, blue }
    // ouput: { light, neutral, dark }
    
    const colors = [
        { green: 0.2, blue: 0.4 },
        { green: 0.4, blue: 0.6 },
        { red: 0.2, green: 0.8, blue: 0.8 },
        { green: 1, blue: 1 },
        { red: 0.8, green: 1, blue: 1 },
        { red: 1, green: 1, blue: 1 },
        { red: 1, green: 0.8, blue: 0.8 },
        { red: 1, green: 0.6, blue: 0.6 },
        { red: 1, green: 0.4, blue: 0.4 },
        { red: 1, green: 0.31, blue: 0.31 },
    const restaurants = {
        "Brilliant Yellow Corral": "Monday",
        "Penny’s": "Tuesday",
        "Right Coast Wings": "Wednesday",
        "The Delusion Last Railway Car": "Thursday",
        "Fun Day Inn": "Friday",
        "JHOP": "Saturday",
        "Owls": "Sunday"
    };
    
    // input: { Monday, Tuesday, Wednesday, etc. }
    // output: { Restaurant1, Restaurant2 }
    
    const trainingData = [];
    // Count to 5
    // 1-5, 5-1
    
    const trainingData = [
        [1,2,3,4,5],
        [5,4,3,2,1]
    ];
    
    const net = new brain.recurrent.LSTMTimeStep();
    
    net.train(trainingData);
    
    console.log(net.run([1,2,3,4]));
    console.log(net.run([5,4,3,2]));
    // rawData = [{ open: number, high: number, low: number, close: number }]
    
    function scaleDown(step) { // normalize
        return {
            open: step.open / 138,
            high: step.high / 138,
            low: step.low / 138,
            close: step.close / 138
        };
    }
    
    console.log(scaleDown(rawData[0]));
    
    function scaleUp(step) { // denormalize
    <html>
        <head>
            <script>
                const rawData = [{"date":"2018-11-02","open":141.0716,"high":141.101
            </script>
            <script src="https://unpkg.com/brain.js@1.6.0/browser.js"></script>
            <script src="index.pack.js"></script>
        </head>
        <body>
        </body>
    </html>
    // rawData = [{ open: number, high: number, low: number, close: number }]
    
    function scaleDown(step) { // normalize
        return {
            open: step.open / 138,
            high: step.high / 138,
            low: step.low / 138,
            close: step.close / 138
        };
    }
    
    function scaleUp(step) { // denormalize
        return {
            open: step.open * 138,
    const trainingData = [
        '0+0=0',
        '0+1=1',
        '0+2=2',
        '0+3=3',
        '0+4=4',
        '0+5=5',
    
        '1+0=1',
        '1+1=2',
        '1+2=3',
        '1+3=4',
        '1+4=5',
        '1+5=6',
    // rawData = ' # ';
    
    function toArray(string) { // normalize 
        if (string.length !== 7 * 7) throw new Error('string in wrong size');
        return string.split('').map(toNumber);
    }
    function toNumber(character) {
        return character === '#' ? 1 : 0;
    }
    
    const zero = toArray(
        '#######' +
        '#     #' +
        '#     #' +
    const trainingData = [
      'Jane saw Doug.',
      'Doug saw Jane.',
      'Spot saw Doug and Jane looking at each other.',
      'It was love at first sight, and Spot had a frontrow seat. It was a very special moment for all.'
    ];
    
    const net = new brain.recurrent.LSTM();
    net.train(trainingData, {
        iterations: 1500,
        errorThresh: 0.011
    });
    
    console.log(net.run('Jane'));
    const trainingData = [
        { input: 'I am super happy!', output: 'happy' },
        { input: 'What a pill!', output: 'sarcastic' },
        { input: 'I am super unhappy!', output: 'sad' },
        { input: 'Are we there yet?', output: 'excited' }
    ];
    
    const net = new brain.recurrent.LSTM();
    net.train(trainingData, {
        iterations: 100,
        erroThresh: 0.011
    });
    
    console.log(net.run('I am unhappy!'));
    const trainingData = [
        { input: '1', output: '2' }
    ];
    
    const net = new brain.recurrent.LSTM();
    
    const inputMap = ['1', 'NEW IDEA', '2'];
    
    [1,0,0]
    [0,1,0]
    [0,0,1]
    
    // bonus: training data look like, if we start with input of '2', an
    const trainingData = [
        { input: [0, 0], output: [0] },
        { input: [0, 1], output: [1] },
        // { input: [1, 0], output: [1] },
        // { input: [1, 1], output: [0] }
    ];
    
    const net = new brain.NeuralNetwork({ hiddenLayers: [3] });
    
    net.train(trainingData);
    
    console.log('before reinforcement');
    console.log(Array.from(net.run([0, 0])));
    console.log(Array.from(net.run([1, 0])));
    // color preference
    const trainingData = [
        { input: { blue: 1 }, output: [1] },
        { input: { red: 1 }, output: [1] },
        { input: { black: 1 }, output: [0] },
        { input: { green: 1 }, output: [0] },
        { input: { brown: 1 }, output: [0] },
    ];
    
    const net = new brain.NeuralNetwork();
    
    net.train(trainingData);
    console.log('before preference change');
    console.log(Array.from(net.run({ blue: 1 })));

The most interactive neural network course ever created 🤯

This course gives you a practical introduction to Brain.js, a popular JavaScript library for building neural networks in the browser and in Node.js. And since this is Scrimba, you'll be able to interact with the neural networks whenever you want. Simply pause the screencast, edit the code and run the network with your own changes applied. Learning machine learning has never been as interactive as this!

What you'll learn

By the end of the course, you'll be able to solve a range of different problems using neural networks. The lectures does not dwell with much theory, but rather on how to code the networks. That means the course is suitable for anybody who knows JavaScript.

Here are some of the amazing projects you'll learn to build:

* XOR gate
* Counter
* Basic math network
* Image recognizer
* Sentiment analyzer
* Recommendation engine
* Children's book creator

Good luck, and welcome to the exciting world of neural networks!

what other people say

the ultimate code learning experience

MEET YOUR TEACHER

The course creator

Robert Plummer

Robert is a full stack engineer, and the lead developer of the Brain.js library. He has a unique ability to explain complex concepts in a manner that everyone can understand.

Thank Robert for the course

SECURE YOUR FREE SPOT