Example #1
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 #2
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));
	},
 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 #4
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 #5
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 #6
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 #7
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();
    }
  }
var net = new brain.NeuralNetwork({
	
});


program
	.version('0.0.1')
	.option('-n, --non_duplicates <amount>', 'Train with n non-duplicates')
	.option('-d, --duplicates <amount>', 'Train with n duplicates')
	.option('-b, --brain <file>', 'Use existing network as a base for training')
	.parse(process.argv);

if (program.brain !== undefined) {
	var networkJSON = fs.readFileSync(program.brain, 'utf8');
	net.fromJSON( JSON.parse(networkJSON) );
}

var duplicates_amount = program.duplicates;
var nonduplicates_amount = program.non_duplicates;

var trainNondoulbes = data.non_doubles.slice(0, nonduplicates_amount);
var trainDoubles = data.doubles.slice(0, duplicates_amount);

var trainingData = trainNondoulbes.concat(trainDoubles);
shuffleArray(trainingData);

var result = net.train(trainingData, {
	errorThresh: 0.005,	// error threshold to reach
	iterations: 3000,	 // maximum training iterations
	log: true,					 // console.log() progress periodically
Example #9
0
'use strict';

var netData = require('./resources/net.json');

var brain = require('brain');
var net = new brain.NeuralNetwork();

var tzbUtil = require('./lib/crawlerUtil');

net.fromJSON(netData);

var url = '';

tzbUtil.createWU(url, 0, function(wu) {
	console.log(wu.occ);
	var output = net.run(wu.occ);
	console.log(output);
});

Example #10
0
    Landmark.find({_id:{$in:ids}}).exec(function (err, records_landmarks) {
      for(var landmark_n in records_landmarks){
        var landmark = records_landmarks[landmark_n]
        
        var net = new brain.NeuralNetwork();
        net.fromJSON(landmark_networks[landmark._id]);
        var openingHours_busyness = [0,0,0,0,0,0,0,0,0,0]
        var counter_1 = 0
        for (var i = 9; i < 20; i++) {
          openingHours_busyness[counter_1] = net.run({h:i/24}).busy
          counter_1 ++
        };
        
        var lowest_index = 0
        var lowest_num = 1
        for (var i = 0; i < openingHours_busyness.length; i++) {
          var counter = openingHours_busyness[i]
          if(counter<lowest_num){
            lowest_index = i
            lowest_num = counter
          }
        };

        var op_time = lowest_index + 9;
        resultArray.push({key: landmark, value: op_time})
        console.log("Go to " + landmark.name + " @ " + op_time)
      }
      
      var taskList = []
      for( var task in records_landmarks) {
        var landmarkToVisit = records_landmarks[task]

        var boundaryTime = resultArray[task].value + 3

        var am = false

        if(resultArray[task].value > 12) {

          am = "pm"
          resultArray[task].value = resultArray[task].value - 12

        } else {

          am = "am"

        }

        var amBoundary = false

        if(boundaryTime > 12) {

          amBoundary = "pm"
          boundaryTime = boundaryTime - 12

        } else {

          amBoundary = "am"

        }
        console.log('every weekday from ' + resultArray[task].value + ':00' + am + ' to ' + boundaryTime + ':00' + amBoundary)
        var duration = 75
        var buffer = ( Math.random() - 0.5 ) * 30
        duration = Math.round(buffer + duration)
        taskList.push({id: JSON.stringify(landmarkToVisit), duration: duration , minLength: duration, available: later.parse.text('every weekday from ' + resultArray[task].value + ':00' + am + ' to ' + boundaryTime + ':00' + amBoundary), resources: ['person']})
      }

      var resources = [
        {id: 'person', available: later.parse.text('after 09:00am and before 8:00pm')}
      ];

      var projectAvailability = later.parse.text('every weekday'),
          startDate = new Date()

      var finalSchedule = schedule.create(taskList, resources, projectAvailability, startDate);

      // Now we compile the final response object. The gateway to glory
      var response = []

      var keys = Object.keys(finalSchedule.scheduledTasks)


      for (var key in keys) {
        var location = keys[key]
        response.push({landmark: JSON.parse(location), timeframe: {start: finalSchedule.scheduledTasks[location].earlyStart, end: finalSchedule.scheduledTasks[location].earlyFinish}})
      }

      res.send(response)
      next()
    })
var recreateClassifier = function(state) {
    var net = new brain.NeuralNetwork();
    net.fromJSON(state);

    return net;
};
Example #12
0
					}, function(response) {
						if (response.length == 0 || params.retrain) {
							console.log("\n*** Creating a new Neural Net ***");
							console.log("\n*** Training... ***");
							// Train!
							// Now we train
							var net 				= new brain.NeuralNetwork({
								hiddenLayers: [6,6],
								learningRate: 0.25
							});
							
							var trainResponse		= net.train(trainingData);
							Gamify.log("Training Response", trainResponse);
							
							// Now we save the neural net
							scope.mongo.update({
								collection:	'neuralnets',
								query:	{
									name:	params.name
								},
								data:	{
									$set:	{
										name:	params.name,
										net:	net.toJSON(),
										trainResponse:	trainResponse
									}
								}
							}, function(err, response) {
								console.log("\n\n*** Partial Neural net saved as '"+params.name+"' ***");
								
								// test the network
								testNetwork(net, testData, function(testResponse) {
									// train on the most recent data, the test sample
									var trainResponse		= net.train(testData);
									Gamify.log("Training Response", trainResponse);
									
									// Now we save the neural net
									scope.mongo.update({
										collection:	'neuralnets',
										query:	{
											name:	params.name
										},
										data:	{
											$set:	{
												name:			params.name,
												net:			net.toJSON()
											}
										}
									}, function(err, response) {
										console.log("\n\n*** Full Neural net saved as '"+params.name+"' ***");
										callback(testResponse);
									});
								});
								
							});
							
						} else {
							
							console.log("\n\n*** Neural loaded from '"+params.name+"' ***");
							
							// Load from the database
							var net 				= new brain.NeuralNetwork();
							var neural 				= net.fromJSON(response[0].net);
							
							testNetwork(neural, testData);
						}
					});
Example #13
0
				}, function(response) {
					if (response.length == 0) {
						console.log("\n**** This Neural Net ("+params.name+") doesn't exist! ****\n");
						callback(Gamify.api.errorResponse("This Neural Net ("+params.name+") doesn't exist."));
					} else {
						
						console.log("\n\n*** Neural loaded from '"+params.name+"' ***");
						
						// Load from the database
						var net 				= new brain.NeuralNetwork();
						var neural 				= net.fromJSON(response[0].net);
						
						// Get the technical analysis
						scope.Gamify.api.execute("stock","processSignals", {
							symbol:		params.symbol,
							days:		30
						}, function(RawSignals) {
							
							// Get the last 5 last datapoints
							scope.mongo.find({
								collection:	"historical",
								query:		{
									symbol:		params.symbol.toLowerCase()
								},
								sort:	{
									date:	-1
								},
								limit:	5
							}, function(stockData) {
								
								
								var indexed = _.indexBy(stockData, function(item) {
									return new Date(item.date).standard();
								});
								
								var output		= [];
								
								var minDate 	= 1000000000000000000000000000;
								var lastDate	= new Date(stockData[0].date).getTime();
								
								
								var date;
								for (date in indexed) {
									// Check if we have input for this point
									if (RawSignals.signals[date]) {
										output.push({
											data:	indexed[date],
											input:	RawSignals.signals[date].input,
											output:	neural.run(RawSignals.signals[date].input)
										});
										if (new Date(indexed[date].date).getTime() < minDate) {
											minDate = new Date(indexed[date].date).getTime();
										}
									} else {
										console.log("No data for date ",date);
									}
								}
								
								Gamify.log("minDate",new Date(minDate));
								Gamify.log("lastDate",new Date(lastDate));
								
								output = output.sort(function(a,b) {
									return new Date(a.data.date).getTime()-new Date(b.data.date).getTime();
								});
								
								// We that we have the output, we can calculate the probabilities, using a weigted average
								var forecasts = {};
								var i;
								for (i=0;i<output.length;i++) {
									var n;
									for (n in output[i].output) {
										var dayNumber = i+(n*1);
										if (!forecasts[dayNumber]) {
											forecasts[dayNumber] = {
												n:	0,
												wn:	0,
												t:	0,
												wt:	0
											};
										}
										forecasts[dayNumber].n	+= 1;
										forecasts[dayNumber].t	+= Math.round(output[i].output[n]);
										
										forecasts[dayNumber].wn += i+1;
										forecasts[dayNumber].wt += (i+1)*Math.round(output[i].output[n]);
									}
								}
								
								var n;
								for (n in forecasts) {
									forecasts[n] = {
										regular:	Math.round(forecasts[n].t/forecasts[n].n*100),
										weighted:	Math.round(forecasts[n].wt/forecasts[n].wn*100)
									};
								}
								
								// Now we transform the day numbers into JS dates
								var dayForecasts 	= [];
								var shift 			= 0;	// Keep track of the shifts in days, due to closed days
								for (n in forecasts) {
									
									var date = new Date(minDate+(1000*60*60*24*(n*1+1)));
									
									// Is that a closed day?
									// Skip Saturdays and Sundays
									if (date.getDay() == 6) {
										// Saturday
										shift += 2;
										var date = new Date(minDate+(1000*60*60*24*((shift+n*1)+3)));
									} else if (date.getDay() == 0) {
										// Saturday
										shift += 1;
										var date = new Date(minDate+(1000*60*60*24*((shift+n*1)+2)));
									}
									
									if (date.getTime() > lastDate) {
										var direction	= "";
										var from		= 0;
										var to			= 0;
										
										if (forecasts[n].regular >= 51 && forecasts[n].weighted >= 51) {
											
											direction	= "up";
											from		= Math.min(forecasts[n].regular, forecasts[n].weighted);
											to			= Math.max(forecasts[n].regular, forecasts[n].weighted);
											
										} else if (forecasts[n].regular <= 51 && forecasts[n].weighted <= 51) {
											
											direction	= "down";
											from		= Math.min(100-forecasts[n].regular, 100-forecasts[n].weighted);
											to			= Math.max(100-forecasts[n].regular, 100-forecasts[n].weighted);
											
										} else {
											direction	= "unknown";
											from		= Math.min(forecasts[n].regular, 100-forecasts[n].weighted);
											to			= Math.max(forecasts[n].regular, 100-forecasts[n].weighted);
										}
										
										
										dayForecasts.push({
											date:		date,
											from:		from,
											to:			to,
											direction:	direction
										});
									}
								}
								
								dayForecasts = dayForecasts.sort(function(a, b) {
									return new Date(a.date).getTime() - new Date(b.date).getTime();
								});
								
								callback(dayForecasts);
							});
							
						});
						/*
						var response 		= neural.run(point.input);
						callback(response);*/
					}
				});
Example #14
0
function constructor(options) {

	var net = new brain.NeuralNetwork();

	net.fromJSON(options.network);

	function check(record1, record2) {

		var featureVector = generateFeatureVector(record1, record2);
		if (options.verbose) {
			console.log(featureVector);
		}
		var similarity = net.run(featureVector);

		return similarity[0];


	}

	function generateFeatureVector(record1, record2) {

		var similarity = new Similarity(options.strategy, {
			displayUnder: 1,
			displayOver: -2, 
			displaySkipped: true
		} );
		
		record1 = toxmljsFormat(record1);
		record2 = toxmljsFormat(record2);

		var comparison = similarity.compareRecords(record1, record2);


		return comparison.results.reduce(function(memo, item) {
			memo[item.checker.name] = item.checker.similarity;
			return memo;
		}, {});
	}

	function toxmljsFormat(marcRecord) {

		var xmljsFormat = {
			controlfield: marcRecord.getControlfields().map(controlfieldFormatter),
			datafield: marcRecord.getDatafields().map(datafieldFormatter)
		};

		return xmljsFormat;

		function controlfieldFormatter(field) {
			
			return {
				$: {
					tag: field.tag
				},
				_: field.value
			};
		}
		function datafieldFormatter(field) {
		
			return {
				$: {
					tag: field.tag,
					ind1: field.ind1,
					ind2: field.ind2
				},
				subfield: field.subfields.map(subfieldFormatter)
			};
		}

		function subfieldFormatter(subfield) {
			return {
				$: {
					code: subfield.code,
				},
				_: subfield.value
			};
		}
	}
	
	return {
		check: check
	};


}