cazala / synaptic

architecture-free neural network library for node.js and the browser
http://caza.la/synaptic
Other
6.91k stars 666 forks source link

Sample training to test number is > 150 or not, what I'm missing ? #208

Closed jonatasfreitasv closed 7 years ago

jonatasfreitasv commented 7 years ago
(() => {

    const synaptic = require('synaptic');

    let Trainer = synaptic.Trainer;
    let Architect = synaptic.Architect;

    let myPerceptron = new Architect.Perceptron(1, 2, 1);
    let myTrainer = new Trainer(myPerceptron);

    const random = (low, high) => (Math.random() * (high - low) + low);

    let trainingSet = [];

    for(let i = 0; i < 10000; i++) {

        const input = parseInt(random(0, 300));
        const output = input >= 150 ? 1 : 0;

        trainingSet.push({
            input: [input],
            output: [output]
        });

    }

    myTrainer.train(
        trainingSet,
        {
            rate: .1,
            iterations: 1000000,
            error: .001,
            shuffle: true,
            log: 1,
            cost: Trainer.cost.CROSS_ENTROPY
        }
    );

    console.log('0', myPerceptron.activate([1]));
    console.log('0', myPerceptron.activate([123]));
    console.log('0', myPerceptron.activate([149]));
    console.log('0', myPerceptron.activate([49]));

    console.log('1', myPerceptron.activate([160]));
    console.log('1', myPerceptron.activate([150]));
    console.log('1', myPerceptron.activate([198]));

})();
wagenaartje commented 7 years ago

Your data is not normalized. That is why the network has trouble recognizing a pattern. You should only input values between 0 and 1 into the neural the network. You might want to read this.

jonatasfreitasv commented 7 years ago

Thx a lot @wagenaartje, my final code is:

/*
 Train neural network to check if number is greater than 50 or not.
 */

(() => {

    const synaptic = require('synaptic');
    const colors = require('colors');

    let Trainer = synaptic.Trainer;
    let Architect = synaptic.Architect;

    // Create Perceptron Neural Network and trainer
    let network = new Architect.Perceptron(1, 10, 10, 1);
    let trainer = new Trainer(network);

    // Function to return a random number between
    const random = (low, high) => (Math.random() * (high - low) + low);

    // Data to use in train
    let training_set = [];
    // Data to use for make tests after train
    let test_set = [];

    // Create train data set
    for(let i = 0; i < 1000; i++) {

        const input = parseInt(random(0, 99)); // -> random number to test
        const output = input >= 50 ? 1 : 0; // -> expected result

        // Normalize input between 0 and 1 values.
        const normalized_input = input / 100;

        test_set.push(normalized_input);

        training_set.push({
            input: [normalized_input],
            output: [output]
        });

    }

    // Training
    trainer.train(
        training_set,
        {
            rate: .1,
            iterations: 1000000,
            error: .001,
            shuffle: true,
            log: 100,
            cost: Trainer.cost.CROSS_ENTROPY
        }
    );

    // Test neural network, check numbers is less or greater than 50.
    test_set.map((value)=>{

        const test_result = parseInt(network.activate([value]) * 100);
        const log = `Number ${parseInt(value*100)} has ${test_result}% chance to be greater than 50`;

        test_result > 90 ?
            console.log(log.green) :
            console.log(log.red);

    });

})();
jonatasfreitasv commented 7 years ago

Thx a lot @wagenaartje!