Neural Nets in JavaScript and Brain.js

With Brain.js
    // 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 })));