trainBrain: function(trainingData, testingData) {
    console.time('trainBrain');
    console.log('Training your very own Brain');

    net.train(trainingData, {
      errorThresh: 0.005,  // error threshold to reach
      iterations: 30,   // maximum training iterations
      log: true,           // console.log() progress periodically
      logPeriod: 1,       // number of iterations between logging
      learningRate: 0.3    // learning rate
    });
    //TODO: Your code here to train the neural net


    console.timeEnd('trainBrain');

    // once we've trained the brain, write it to json to back it up
    var jsonBackup = net.toJSON();
    console.log('jsonBackup:', jsonBackup);
    var runBackup = net.toFunction();
    module.exports.writeBrain(jsonBackup);

    // now test the results and see how our machine did!
    module.exports.testBrain(testingData);
  },
Example #2
0
var trainNetwork = function() {

  console.log('trainNetwork');

  // Train the network
  // input: array of numbers 0 - 1
  // output: label 0 or 1
  var netTraining = _.map(trainingData, function(data) {

    // Convert the input to a range of 0 - 1
    // In the MNIST data, 0 = white, 1 = black
    var input = _.map(data.image, function(byte){
      return (255 - byte) / 255;
    });

    var output = {};
    output[data.label] = 1;

    return {input: input, output: output};

  });

  net.train(netTraining, {iterations: 10, log: true, logPeriod: 10, learningRate: 0.05});

  // Save the training results for later use
  var networkJson = JSON.stringify(net.toJSON());
  fs.writeFile(networkFile, networkJson);
};
Example #3
0
function trainNetwork(params) {
  var samples = opts.sample ? 1 : 0;
  var data = collect.collectData(opts.pos, opts.neg, samples,
                                 opts.posLimit, opts.negLimit, params);

  console.log("training on", data.length);
  console.log("feature size:", data[0].input.length)

  var network = new brain.NeuralNetwork(params.nn);

  var stats = network.train(data, params.train);

  console.log("stats:", stats);
  console.log("parameters:", params);

  var json = JSON.stringify(network.toJSON(), 4)

  fs.writeFile(opts.outfile, json, function (err) {
    if (err) throw err;
    console.log('saved network to', opts.outfile);
  });

  if (opts.testPos && opts.testNeg) {
    testNetwork(network);
  }
}
Example #4
0
	output : function(params, writeCallback){
		var net = new brain.NeuralNetwork();
		var netFileAbsPath = path.join(ABS_BASE_PATH,FILES_DIR_NAME,params.name+'.json');

		/* @process
			NN net from repository
		*/
		net.fromJSON(JSON.parse(fs.readFileSync(netFileAbsPath, ENCODING)));

		/* @process
			NN net run
		*/
		if(eval(params).test || !params.length){
			output = net.run({r:1, g:1, b:0});
		}
		else{
			params = eval('('+params+')');
			for(k in params){
				params[k] = parseInt(params[k]);
			}
			output = net.run(params);
		}

 		writeCallback(localProcess.get.output(output));
	},
Example #5
0
    Task.find({ job_id:job.id}).populate("actions").exec(function(e,tasks){
      var training_set = []
      _.map(tasks, function(task){
        _.map(task.actions, function(action){
          if(action){
            var output = {}
            output[(action.input == 0 ? 'black' : 'white')] = 1
            training_set.push( {input:task.data, output:output })
          }
        })
      })
      if(tasks[0].actions){
        
        console.log(training_set)
        var net = new brain.NeuralNetwork();

        net.train( training_set );
        var run = net.toFunction();
        var output = run({ r: 1, g: 0.4, b: 0 });

        var trained_neural_net = run.toString()
        console.log("trained_neural_net",trained_neural_net); 
      }

      return res.view( "analysis/"+job.type, { job:job, tasks:tasks, neural_net:(trained_neural_net || null) } );
    })
Example #6
0
 var data_set = Q.ninvoke(client, 'lrange', 'test', 0, -1).then(function (data) {
   if(data.length===0) return false;
   data = data.map(function (a) { return JSON.parse(a); })    
   net.train(data);  
   return {
       prediction: net.run({ r: 1, g: 0.4, b: 0 }),
       time: new Date().getTime() - start   
    };      
 });
var trainClassifier = function(V, limits) {
    var Y = V.map(account.toClasses),
        X = V.map(function(v) {
            return applyLimits(account.toFeatures(v), limits);
        });

    var net = new brain.NeuralNetwork();
    net.train(makeTrainData(X, Y));

    return net;
};
Example #8
0
 train: function(trainingData) {
   multipleNetAlgo(trainingData);
   // return the net itself
   // var net = kpComplete.train(trainingData); should be something they can type in. 
   // and then we'd return the fully trained net. 
   // because we can get a net from JSON. so let's do that and then return it. 
   // TODO: investigate if we need to give them a callback. does this become asynch with paralleljs?
   console.log('your best net is:', bestNet.jsonBackup);
   var net = new brain.NeuralNetwork();
   return net.fromJSON(bestNet.jsonBackup);
 }
Example #9
0
module.exports = function (state, _, cb) {
    net.train(state.trainData, {
        errorThresh  : 0.004   // error threshold to reach
      , iterations   : 30000   // maximum training iterations
      , log          : true    // console.log() progress periodically
      , logPeriod    : 1000    // number of iterations between logging
      , learningRate : 0.3     // learning rate
    });

    state.trainResults = net.toJSON();
    cb();
};
Example #10
0
			this.netFileExistsCallback=function(exists){

				/* @process
					if both: 
						-	'net' GET|POST param is provided
						-	netFileAbsPath exists
					...netFileAbsPath takes precedence
				*/
				if(exists){
					net.fromJSON(JSON.parse(fs.readFileSync(netFileAbsPath, ENCODING)));
				}
				else if(params.net){
					net.fromJSON(JSON.parse(params.net));
				}

				/* @process
					NN net training data
				*/
				var data = localProcess.get.data(params)				

				/* @process
					NN net actual training process
				*/
				var output = {"message" : "training complete, nn saved"} 
				try {
					net.train(data);
				}
				catch(e){
					output = {"message" : "Error during training. A clue: retrying with different training..."} 
				}

				if (output.message.indexOf("Error")<0){
					/* @process
						NN net save
					*/
					var ioParams = {
						file:netFileAbsPath,
						data:JSON.stringify(net.toJSON()), 
						output:'',
						_status: 1
					};
					localProcess._do.saveFile(ioParams)

					/* @process
						Output	
					*/
					if (ioParams._status!=1){
						output = {"message" : "training complete, error saving nn: "+ioParams.output} 
					}
				}

				writeCallback(localProcess.get.output(output));
			};
Example #11
0
process.on('message', function(data) {


  
	if (data.msg === "train" && !ops.training) {

		ops.data = data.data;
		var backupOptions = ops.options;
		ops.options = _.defaults(data.options, backupOptions);

		var error = false;
		ops.training = true;
		try {
			var result = net.train(ops.data, ops.options);
		} catch(err) {
			error = err;
			ops.training = false;
		}

		if (!error) {

			process.send({ 
				code: 200, 
				type: "result", 
				data: {
					net: net.toJSON(),
					result: result
				}
			});

			ops.training = false;

		} else process.send({ 
			code: 500, 
			type: "error", 
			data: error
		});



	} else {
		process.send({ 
			code: 500, 
			type: "error", 
			data: 'Already busy training.' 
		});
	}

});
Example #12
0
  getCanvases(function(canvases) {
    canvases = canvases.filter(function(canvas) {
      return !canvas.err;
    })

    var data = canvases.map(function(canvas) {
      try {
      var fts = features.extractFeatures(canvas.canvas, params);
      } catch(e) {
        console.log("err getting features", e, canvas.file);
      }
      return {
        input: fts,
        output: [canvas.isCat]
      };
    });

    data = _(data).sortBy(function() {
      return Math.random();
    });

    console.log(data[0].input.length)

    console.log("training with", data.length);

    var opts = {
      hiddenLayers: [30]
    };
    var trainOpts = {
      errorThresh: 0.005,
      log: true,
      logPeriod: 1
    };

    var network = new brain.NeuralNetwork(opts);

    var stats = network.train(data, trainOpts);

    console.log("stats:", stats);
    console.log("parameters:", opts);

    var json = JSON.stringify(network.toJSON(), 4)

    fs.writeFile(networkFile, json, function (err) {
      if (err) throw err;
      console.log('saved network to', networkFile);
    });
  })
Example #13
0
	function testWith(item, target) {
		var output = net.run(item.input);

		var uncertainty = 0.5 - Math.abs(0.5 - output[0]);
		//if (uncertainty > 0.1) return;
		
		var distance = Math.abs(target - output[0]);
		
		var ok = distance < 0.5;
		
		if (ok) {
			correct++;
		} else {
			incorrect++;
		}

		var action_taken = output[0] > 0.5 ? 'MERGE' : 'NO-OP';
		var action_correct = target === 1 ? 'MERGE' : 'NO-OP';

		var msg = sprintf("../diff.sh %s\t%.4f\t%.4f\t%s\t%s\t%s", item.key, distance, output[0], ok ? 'ok':'fail', action_taken, action_correct);

		var extra = sprintf("d(%.4f, %f) < %.4f", output[0], target, .5);

		console.log(sprintf("%s\t%s", msg, extra));
		
	}
Example #14
0
 buildHands(initHand(), 0, startingCards, function(hand) {
     console.log(
         startingCards,
         JSON.stringify(hand),
         net.run(buildHandArray(hand, totalCards))
     );
 });
Example #15
0
  testBrain: function(testData) {
    console.time('testBrain');
    var results = {};
    for(var j = 0; j <=100; j++) {
      results[j] = {
        nnCount: 0,
        defaulted: 0
      };
    }

    for(var i = 0; i < testData.length; i++) {
      //net.run gives us the net's prediction for that particular input
      //TODO: SOLUTION CODE BELOW. Consider refactoring outside of for statement.
      var rawPrediction = net.run(testData[i].input);
      //we then format the net's prediction to be a number between 0 and 100
      var prediction = Math.round( rawPrediction.defaulted * 100);
      // console.log(net);
      // console.log('prediction');
      // console.log(rawPrediction);
      // console.log('testData[i]');
      // console.log(testData[i].input);
      //We then increment the total number of cases that the net predicts exist at this level
      results[prediction].nnCount++;
      //And whether this input resulted in a default or not
      results[prediction].defaulted += testData[i].output.defaulted;
    }

    //yeah, i know we don't like to assume the keys are going to be ordered, but it's a time-saving shortcut to make at the moment.
    for(var key in results) {
      console.log(key + '- nnCount: ' + results[key].nnCount + ' defaulted: ' + results[key].defaulted + ' Default Rate: ' + results[key].defaulted/results[key].nnCount);
    }
    console.timeEnd('testBrain');

    console.log(results);
  },
  testBrain: function(testData) {
    //console.time gives us the time it takes to complete a task
    console.time('testBrain');
    //TODO: Your code here to get the predicted values for everything in our testData

    for(var i = 0; i < testData.length; i++){
      testData[i].nnPredictions = net.run(testData[i].input);
    }
    //The logging code provided for you below expects the predicted net values to be stored as properties on each item in testData under the property name nnPredictions.
    //Here's what an object in the testData array should look like after you've gotten the predicted result from the net:
      /*
      { input:
         { utilizationRate: 0.21939333333333333,
           age: 0.3486238532110092,
           thirtyDaysLate: 0.01020408163265306,
           monthlyIncome: 0.031789238577839024,
           openCreditLines: 0.1767766952966369,
           ninetyDaysLate: 0.1,
           realEstateLines: 0,
           sixtyDaysLate: 0,
           numDependents: 0 },
        output: { defaulted: 0 },
        nnPredictions: { defaulted: 0.34634397489904356 } }
      */



    // everything below is formatting the output
    // first we create a results obj with keys labeled 0 to 100
    // eash position in results is an object itself
      // Each position aggregates the count of loans the neural net has predicted have this level of risk
      // and the number of observed defaults at that level of risk
    var results = {};
    for(var j = 0; j <=100; j++) {
      results[j] = {
        nnCount: 0,
        defaulted: 0
      };
    }

    for(var i = 0; i < testData.length; i++) {
      //we format the net's prediction to be an int between 0 and 100
      var prediction = Math.round( testData[i].nnPredictions.defaulted * 100);
      //We then increment the total number of cases that the net predicts exist at this level of risk
      // (i.e., if the net's prediction for a given input is .38745, we would add one more to the 39 category, since we now have one more observation that the net has predicted has a 39% chance of defaulting)
      results[prediction].nnCount++;
      //And whether this input resulted in a default or not
      results[prediction].defaulted += testData[i].output.defaulted;
    }

    //We don't like to assume the keys are going to be ordered, but it's a time-saving shortcut to make at the moment, and the consequences are very low if it's not perfectly ordered
    for(var key in results) {
      console.log(key + '- nnCount: ' + results[key].nnCount + ' defaulted: ' + results[key].defaulted + ' Default Rate: ' + Math.round(results[key].defaulted/results[key].nnCount * 100) );
    }
    console.timeEnd('testBrain');

    console.log(results);
  },
Example #17
0
// Save our training data
function exportTrainingData (classifier) {
  var raw = net.toJSON();
  var outputFilename = 'training_neural.json';

  fs.writeFile(outputFilename, raw, function (err) {
    if (err) console.log(err);
    else  console.log("JSON saved to " + outputFilename);
  });
}
 fs.readFile('name', 'utf8', function(err, data) {
   if(err) {
     console.error(err);
   } else {
     net.fromJSON(JSON.parse(data));
     res.send('Loaded the brain! Testing it now.')
     module.exports.testBrain(formattedData);
   }
 });
Example #19
0
 train(trainCallback) {
   console.log(this.net.train(this.trainingData, {
     errorThresh: 0.005,
     iterations: this.options.iterations || 1,
     log: true,
     logPeriod: 10,
     learningRate: 0.3
   }));
 }
Example #20
0
 fs.readFile('name', 'utf8', function(err, data) {
   if(err) {
     console.error(err);
   } else {
     net.fromJSON(JSON.parse(data));
     //TODO: send off a response
     //TODO: investigate separating out a training and a testing table
     module.exports.testBrain(formattedData);
   }
 });
Example #21
0
    Activity.find({}).limit(2000).exec(function (err, records_activities){
      for(var landmark_n in records_landmarks){
        var landmark = records_landmarks[landmark_n]
        var landmark_counters = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
        for(var activity_n in records_activities){
          var activity = records_activities[activity_n]
          var distance = geolib.getDistance(
            { latitude: landmark.coords[0], longitude: landmark.coords[1] },
            { latitude: activity.interaction.interaction.geo.latitude, longitude: activity.interaction.interaction.geo.longitude }
          )
          if(distance<=process.env.DISTANCE_TOLERANCE){
            var time = moment(activity.interaction.interaction.created_at)
            var hour = time.hour()
            landmark_counters[hour]++;
          }
        }
        var highest_index = 0
        var highest_num = 0
        for (var i = 0; i < landmark_counters.length; i++) {
          var counter = landmark_counters[i]
          if(counter>highest_num){
            highest_index = i
            highest_num = counter
          }
        };
        var relative_count = []
        for (var i = 0; i < landmark_counters.length; i++) {
          relative_count[i] = landmark_counters[i]/highest_num
        };
        var net = new brain.NeuralNetwork()
        var stupid = []
        for (var i = 0; i < 24; i++) {
          stupid.push({input: {h:i/24}, output: {busy:relative_count[i]}});
        };
        net.train(stupid)

        var json = JSON.stringify(net.toJSON());
        new Network({landmark_id:landmark._id,network:json}).save()
      }
      res.send({done:true})
      next()
    })
Example #22
0
  trainBrain: function(trainingData, testingData) {
    console.time('trainBrain');
    console.log('training your very own Brain');

    //TODO: SOLUTION CODE BELOW
    net.train(trainingData,{
      errorThresh: 0.05,  // error threshold to reach
      iterations: 30,   // maximum training iterations
      log: true,           // console.log() progress periodically
      logPeriod: 1,       // number of iterations between logging
      learningRate: 0.3    // learning rate
    });

    var jsonBackup = net.toJSON();
    var runBackup = net.toFunction();

    module.exports.writeBrain(jsonBackup);

    console.timeEnd('trainBrain');

    module.exports.testBrain(testingData);
  },
  trainBrain: function(trainingData, testingData) {
    console.time('trainBrain');
    console.log('Training your very own Brain');

    //TODO: Your code here to train the neural net

    net.train(trainingData, {iterations: 30, log: true});

    console.timeEnd('trainBrain');

    // once we've trained the brain, write it to json to back it up
    var jsonBackup = net.toJSON();

    console.log(jsonBackup);

    var runBackup = net.toFunction();

    module.exports.writeBrain(jsonBackup);

    // now test the results and see how our machine did!
    module.exports.testBrain(testingData);
  },
Example #24
0
 fs.readFile(req.files.jsonFile.path, function read(err, data) {
   if (err) {
     throw err;
   }
   //neural_network = data;
   if (Buffer.isBuffer( data)){
     neural_network = data.toString('utf8');
     net.fromJSON(JSON.parse(neural_network));
     res.end(JSON.stringify({'status':'success','message':'Red interpretada.'}));
   }else{
     res.end(JSON.stringify({'status':'error','message':'Archivo de formato no permitido.'}));
   }
 });
Example #25
0
var parallelNets = function(hlNum, trainingData) {
  console.log('parallelNets with',hlNum);
  var hlArray = [];
  for(var i = 0; i < hlNum; i++) {
    // TODO: build out logic for how many nodes to put in each hidden layer
    hlArray.push(10);
  }
  console.log('hlArray is:',hlArray);
  var net = new brain.NeuralNetwork({
    hiddenLayers: hlArray, //Use the docs to explore various numbers you might want to use here
    learningRate: 0.6
  });
  var trainingResults = net.train(trainingData, {
    errorThresh: 0.05,  // error threshold to reach
    iterations: 10,   // maximum training iterations
    log: true,           // console.log() progress periodically
    logPeriod: 1,       // number of iterations between logging
    learningRate: 0.3    // learning rate
  });
  //do some kind of returning or result logging
  // capture the final error rate. 
  console.log('trainingResults is:',trainingResults);
  bestNetChecker(trainingResults,net);
};
Example #26
0
app.post('/train', function(req, res){
  var trainData = req.body.entrenamiento;
  var epochs = req.body.epochs;
  _error = 0;
  _iterations = 0;
  net.train(trainData, {
    iterations:epochs,
    callbackPeriod:1,
    callback:function(m){
      console.log(m);
      _error = m.error;
      _iterations = m.iterations;
      res.end('OK');
    }
  });
});
Example #27
0
function test_brain(state){
  var rate = 0;
  for (var i = 0; i < test_data.length; i++){
    var output = net.run(test_data[i].input);
    var max = -1, idx = -1;
    for (var j = 0; j < output.length; j++){
      if (max < output[j]){
        max = output[j];
        idx = j;
      }
    }
    if (idx == data[i].idx)
      rate++;
  }
  console.log(state.iterations+", "+state.error+", "+(rate/test_data.length));
}
Example #28
0
		usage.lookup(pid,{keepHistory:true} ,function(err, result) {
			if(err){
				console.log("Error is : " + err);
			}
			else{
				var run = net.toFunction();
				console.log({"syn":history_packets["syn"]/syn_max,"ack":history_packets["ack"]/ack_max});
				var ann_result = run({"syn":history_packets["syn"]/syn_max,"ack":history_packets["ack"]/ack_max});
				console.log(ann_result);
				console.log(result["cpu"]);
				console.log(result["memory"]/(1024*1024));
				if(result["cpu"] > 80 || result["memory"]/(1024*1024) > 512 || ann_result["ddos"] > ann_result["normal"]){
					console.log("Server under load");
					response.json("Dropped");
				}
				else{
					console.log("Normal");
					next();
				}
			}
		});
Example #29
0
  constructor(options) {
    this.options = options || {};

    if (this.options.obj) {
      this.net = new NeuralNetwork();
      for (let baseKey in this.options.obj) {
        if (baseKey === 'options') {
          continue;
        }
        if (baseKey === 'net') {
          var tmp = this.net.fromJSON(this.options.obj.net);
        } else {
          this[baseKey] = this.options.obj[baseKey];
        }
      }
    } else {
      this.net = new NeuralNetwork();
      this.trainingData = createTrainingData();
      this.train();
    }
  }
Example #30
0
function trainScript (error, file) {
  console.log('opening file');
  if (error) console.log(error);
  // Read file
  /*
  fs.readFile(file, 'utf8', function (err, data) {
    if (err) return console.log('Error reading script: ' + error);

    // Use parent folder as genere
    var genre = path.basename(path.join(file, '../'));
    // Add script to training data
    bayes.train(data, genre);

    callback();
  });*/
  var genre = path.basename(path.join(file, '../'));
  var script = fs.readFileSync(file).toString();

  net.train([{input: script, output: genre}])
  console.log('Added');
}