digitMatrices.forEach((matrix: IDigitMatrix) => {
            let outputs = this.neuralNetwork.runWith(matrix.matrix);
            let maximumValue = -Infinity;
            let minimumValue = +Infinity;
            let maximumNeuron = NaN;
            for (let i = 0; i < outputs.length; i++) {
                if (outputs[i] > maximumValue) {
                    maximumValue = outputs[i];
                    maximumNeuron = i;
                }
                if (outputs[i] < minimumValue) {
                    minimumValue = outputs[i];
                }
            }
            let correct: boolean = maximumNeuron === matrix.digit;
            if (correct) {
                correctGuesses++;
            }
            if (print) {
                let colors = require('colors/safe');

                console.log();
                let expected = 'Expected > ' + colors.green(matrix.digit);
                let actual = 'Actual > ' + (correct ? colors.green(maximumNeuron) : colors.red(maximumNeuron));
                Util.logTest('Output:  ' + expected + '  ' + actual);
                Util.logTest();
                let result = 'CORRECT GUESS';
                if (correct) {
                    result = colors.green(result);
                } else {
                    result = colors.red('IN' + result);
                }
                Util.logTest(result);
                Util.logTest();
                Util.logTest('Neuron outputs:');
                Util.logTest();
                let valueRange = Math.abs(maximumValue - minimumValue);
                for (let i = 0; i < outputs.length; i++) {
                    let neuronIndex = colors.cyan(i);
                    let prefix = '   ';
                    let suffix = '   ';
                    if (i === maximumNeuron) {
                        prefix = ' > ';
                        suffix = ' < ';
                    }
                    let neuronOutput = outputs[i];
                    let normalisedOutput = Math.floor((neuronOutput - minimumValue) / valueRange * 9);
                    let line = '';
                    for (let k = 0; k < 10; k++) {
                        if (k === normalisedOutput) {
                            line += colors.bgYellow(colors.black('◆'));
                        } else {
                            line += colors.bgYellow(' ');
                        }
                    }
                    Util.logTest(prefix + neuronIndex + ' ' + line + ' ' + neuronIndex + suffix);
                }
                Util.logTest();
                Util.logTest('Image of the digit:');
                Util.logTest();
                let printFunction = correct ? colors.green : colors.red;
                DataParser.printImage(matrix.matrix, (line: string) => Util.logTest(printFunction(line)));
                console.log();
            }
        });
Ejemplo n.º 2
0
server.set('view engine', 'ejs');
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: false }));
server.use(cookieParser());
loader.Loader.init(server, __dirname);


export var onReadyCallback;

if (clusterEnabled && cluster.isMaster) {
    if (clusterEnabled!='auto' && clusterEnabled!==false && clusterEnabled!==true) {
        cpus = Number(clusterEnabled);
    }
    var s = "Starting in cluster mode ("+cpus+" processes)";
    console.log(colors.black(colors.bgWhite(s)));
    for (var i = 0; i < cpus; i++) {
        cluster.fork();
    }
}
else {
    server.listen(portNumber, () => {
        var endTime = Date.now();
        var elapsedTime = (endTime - startTime)/1000;
        var t = numeral(elapsedTime).format('0.00');
        console.log('\n');
        console.log(colors.bgGreen(colors.white('                                                         ')));
        console.log(colors.bgGreen(colors.white('           Startup completead in ' + t + ' seconds.           ')));
        console.log(colors.bgGreen(colors.white('                                                         ')));
        console.log('\n');
        console.log("Your application is in "+colors.green(server.get('env'))+" mode on port "+colors.green(portNumber));