Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ----------- Cortex Requires ------------ //
- // ------- Load Cortex Requirements ------- //
- // ---------------- Begin: ---------------- //
- /*------ installing ------//
- Synaptic: $ npm install synaptic
- Sha.js: $ npm install sha.js
- Express: $ npm install express --save
- //------------------------*/
- var synaptic = require('synaptic');
- var Neuron = synaptic.Neuron,
- Layer = synaptic.Layer,
- Network = synaptic.Network,
- Trainer = synaptic.Trainer,
- Architect = synaptic.Architect;
- var createHash = require('sha.js')
- var express = require('express')
- // ------------ Cortex Define ------------- //
- // -------- Cortex Code Begins Here ------- //
- // ---------------- Begin: ---------------- //
- function Cortex(chars, bin){
- this.splash = true;
- this.chars = chars;
- if(bin && bin == true) this.bin = true;
- if(this.bin && this.bin == true){
- this.bits = this.chars;
- }else{
- this.bits = (this.chars * 8);
- }
- this.cortex = new Architect.Hopfield(this.bits)
- this.dataSets = [];
- }
- Cortex.prototype = {
- build : function(){
- var dataSet = [];
- for(key in this.dataSets){
- dataSet.push(this.dataSets[key]['data']);
- }
- if(this.splash == true){
- console.log(" ");
- console.log(' - CognitionLab Cortex application layer © CognitionLab LTD. & E.S.R.MacDonald 2017.');
- console.log(' - More info at https://cognitionlab.ltd/cortex');
- console.log(" ");
- console.log("#############################################");
- console.log(" ");
- console.log(" ##### # ");
- console.log(" # # #### #### # ## ##### ");
- console.log(" # # # # # # # # # # ");
- console.log(" # # # # # # # ##### ");
- console.log(" # # # # ### # ###### # # ");
- console.log(" # # # # # # # # # # # ");
- console.log(" ##### #### #### ####### # # ##### ");
- console.log(" ");
- console.log(" ##### ");
- console.log(" # # #### ##### ##### ###### # # ");
- console.log(" # # # # # # # # # ");
- console.log(" # # # # # # ##### ## ");
- console.log(" # # # ##### # # ## ");
- console.log(" # # # # # # # # # # ");
- console.log(" ##### #### # # # ###### # # ");
- console.log(" ");
- console.log("#############################################");
- console.log(' - Cortex Info:');
- console.log(' CognitionLab Cortex v.1.0.0 Node.JS Application Layer');
- if(this.bin && this.bin == true){
- console.log(' processing ' + this.chars + ' binary bits with arbitrary tagging.');
- }else{
- console.log(' processing ' + this.chars + ' byte strings with arbitrary tagging.');
- }
- console.log(' - Neural Network Info:');
- console.log(' Synaptic Hopfield core processing ' + this.bits + ' bit patterns.');
- console.log("#############################################");
- console.log("> Training Neural Net...");
- }
- this.training = this.cortex.learn(dataSet);
- if(this.splash == true){
- console.log("> Trained in " + this.training.time + "ms and " + this.training.iterations + " iterations.");
- console.log("> Stable error rate: " + this.training.error + ".");
- console.log("> Cortex Ready!");
- console.log("#############################################");
- console.log(" ");
- }
- },
- feed : function(dat){
- var dataOut = "";
- if(!this.bin) dat = this.cleanInput(dat).split("");
- var feed = this.cortex.feed(dat);
- for(var i = 0; i < feed.length; i++){
- dataOut += feed[i];
- }
- return this.sha(dataOut);
- },
- buildDataSet : function(data, tag){
- if(this.bin){
- var ide = data;
- }else{
- var ide = this.cleanInput(data);
- }
- var base = this.sha(ide);
- this.dataSets[base] = [];
- this.dataSets[base]['raw'] = data;
- var dat = ide.split('');
- this.dataSets[base]['data'] = dat;
- this.dataSets[base]['tag'] = tag;
- },
- load : function(data){
- for(key in data){
- this.buildDataSet(key, data[key]);
- }
- },
- toBin : function(input){
- var output = "";
- for (i=0; i < input.length; i++) {
- var a = input[i].charCodeAt(0).toString(2);
- a = new Array(9 - a.length).join('0') + a;
- output += a;
- }
- return output;
- },
- fixBit : function(input){
- var output = "";
- if(input.length > this.bits){
- output += input.substring(this.bits, 0);
- }else{
- output += input;
- }
- while(output.length < this.bits){
- output += "0";
- }
- return output;
- },
- cleanInput : function(input){
- var output = "";
- input = this.toBin(input);
- input = this.fixBit(input);
- output += input;
- return output;
- },
- sha : function(input){
- var sha1 = createHash('sha1');
- var h = sha1.update(input, 'utf8').digest('hex');
- return h;
- }
- };
- module.exports = Cortex;
- // ------------ Cortex Defined ------------ //
- // - You must now load datasets and build - //
- // ----------------- Use: ----------------- //
- // var cortex = new Cortex(3);
- // cortex.load({
- // "AAA" : "some data",
- // "AAB" : "more data"
- // });
- // cortex.build();
- // ------------- Cortex Ready ------------- //
- /* <!-- BEGIN TESTS --> */
- function parseData(data){
- var obj = {};
- for(key in data){
- var str = "";
- if(data[key]['animal'] == 'cat') str += 'a1';
- if(data[key]['animal'] == 'dog') str += 'a2';
- if(data[key]['nice'] == 'friendly') str += 'b1';
- if(data[key]['nice'] == 'scary') str += 'b2';
- obj[str] = "this is most likely a " + data[key]['nice'] + " " + data[key]['animal'] + ".";
- }
- return obj;
- }
- var inpdata = {
- "1" : {
- "animal" : "cat",
- "nice" : "friendly"
- },
- "2" : {
- "animal" : "dog",
- "nice" : "friendly"
- },
- "3" : {
- "animal" : "cat",
- "nice" : "scary"
- },
- "4" : {
- "animal" : "dog",
- "nice" : "scary"
- },
- "5" : {
- "animal" : "dog",
- "nice" : "friendly"
- },
- "6" : {
- "animal" : "cat",
- "nice" : "scary"
- }
- };
- var cortex = new Cortex(4);
- //cortex.splash = false;
- cortex.load(
- parseData(inpdata)
- );
- cortex.build();
- function test(input){
- var response = cortex.dataSets[
- cortex.feed(input)
- ];
- console.log(
- input + " > " +
- response['raw'] + ": " +
- response['tag']
- );
- return {
- "input" : input,
- "output" : response['raw'],
- "tag" : response['tag']
- };
- }
- test("a1a1");
- test("a2b2");
- test("a2b0");
- test("a2b3");
- test("a3b2");
- if(cortex.splash == true) console.log(" ");
- var app = express()
- app.use(function(req, res, next){
- res.header("Access-Control-Allow-Origin", "*");
- res.header("Content-Type", "application/json");
- next();
- });
- app.get('/:str', function(req, res){
- var $d = test(req.params.str);
- var $j = JSON.stringify($d);
- res.send($j);
- });
- app.listen(9960, function(){
- console.log('> now listening on port 9960.');
- console.log(" ");
- });
Add Comment
Please, Sign In to add comment