UNPKG

neataptic

Version:

Architecture-free neural network library with genetic algorithm implementations

1,640 lines (1,373 loc) 101 kB
/*! * The MIT License (MIT) * * Copyright 2017 Thomas Wagenaar <wagenaartje@protonmail.com> * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE * */ (function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else if(typeof exports === 'object') exports["neataptic"] = factory(); else root["neataptic"] = factory(); })(this, function() { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // identity function for calling harmony imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 17); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports) { module.exports = function(module) { if(!module.webpackPolyfill) { module.deprecate = function() {}; module.paths = []; // module.parent = undefined by default if(!module.children) module.children = []; Object.defineProperty(module, "loaded", { enumerable: true, get: function() { return module.l; } }); Object.defineProperty(module, "id", { enumerable: true, get: function() { return module.i; } }); module.webpackPolyfill = 1; } return module; }; /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { var __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;var Methods = { Activation : __webpack_require__(10), Mutation : __webpack_require__(15), Selection : __webpack_require__(16), Crossover : __webpack_require__(13), Cost : __webpack_require__(12), Gating : __webpack_require__(14), Connection : __webpack_require__(11) }; // CommonJS & AMD if (true) { !(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_RESULT__ = function(){ return Methods }.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__), __WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__)); } // Node.js if (typeof module !== 'undefined' && module.exports) { module.exports = Methods; } // Browser if (typeof window == 'object') { (function(){ var oldMethods = window['methods']; Methods.ninja = function(){ window['methods'] = oldMethods; return Methods; }; })(); window['methods'] = Methods; } /***/ }), /* 2 */ /***/ (function(module, exports, __webpack_require__) { /* WEBPACK VAR INJECTION */(function(module) {/* Export */ if (module) module.exports = Node; /* Import */ var Connection = __webpack_require__(4); var Methods = __webpack_require__(1); var Group = __webpack_require__(5); var Config = __webpack_require__(3); /* Easier variable naming */ var Activation = Methods.Activation; var Mutation = Methods.Mutation; /****************************************************************************************** NODE *******************************************************************************************/ function Node(type) { this.bias = (type == 'input') ? 0 : Math.random() * .2 - .1; this.squash = Activation.LOGISTIC; this.type = type || 'hidden'; this.activation = 0; this.state = 0; this.old = 0; this.mask = 1; this.connections = { in : [], out : [], gated : [], self : new Connection(this, this, 0) }; // Data for backpropagation this.error = { responsibility: 0, projected: 0, gated: 0 }; } Node.prototype = { /** * Activates the node */ activate: function(input){ // Check if an input is given if (typeof input != 'undefined') { this.activation = input; this.derivative = 0; this.bias = 0; return this.activation; } this.old = this.state; // All activation sources coming from the node itself this.state = this.connections.self.gain * this.connections.self.weight * this.state + this.bias; // Activation sources coming from connections for(var connection in this.connections.in){ connection = this.connections.in[connection]; this.state += connection.from.activation * connection.weight * connection.gain; } // Squash the values received this.activation = this.squash(this.state) * this.mask; this.derivative = this.squash(this.state, true); // Update traces var nodes = []; var influences = []; for(var conn in this.connections.gated){ conn = this.connections.gated[conn]; var node = conn.to; var index = nodes.indexOf(node); if(index > -1){ influences[index] += conn.weight * conn.from.activation; } else { nodes.push(node); influences.push(node.connections.self.gater == this ? node.old : 0); influences[influences.length - 1] += conn.weight * conn.from.activation; } } for (var connection in this.connections.in) { connection = this.connections.in[connection]; // Elegibility trace connection.elegibility = this.connections.self.gain * this.connections.self.weight * connection.elegibility + connection.from.activation * connection.gain; // Extended trace for(var i = 0; i < nodes.length; i++){ var node = nodes[i]; var influence = influences[i]; var index = connection.xtrace.nodes.indexOf(node); if(index >-1){ connection.xtrace.values[index] = node.connections.self.gain * node.connections.self.weight * connection.xtrace.values[index] + this.derivative * connection.elegibility * influence; } else { // Does not exist there yet, might be through mutation connection.xtrace.nodes.push(node); connection.xtrace.values.push(this.derivative * connection.elegibility * influence); } } } // Update the gains of the gates for(var connection in this.connections.gated){ this.connections.gated[connection].gain = this.activation; } return this.activation; }, /** * Back-propagate the error, aka learn */ propagate: function(rate, target) { // Error accumulator var error = 0; // Output nodes get their error from the enviroment if (this.type == 'output'){ this.error.responsibility = this.error.projected = target - this.activation; } else { // the rest of the nodes compute their error responsibilities by backpropagation // error responsibilities from all the connections projected from this node for (var connection in this.connections.out) { var connection = this.connections.out[connection]; var node = connection.to; // Eq. 21 error += node.error.responsibility * connection.weight * connection.gain; } // Projected error responsibility this.error.projected = this.derivative * error; // Error responsibilities from all connections gated by this neuron error = 0; for(var conn in this.connections.gated){ conn = this.connections.gated[conn]; var node = conn.to; var influence = node.connections.self.gater == this ? node.old : 0; influence += conn.weight * conn.from.activation; error += node.error.responsibility * influence; } // Gated error responsibility this.error.gated = this.derivative * error; // Error responsibility this.error.responsibility = this.error.projected + this.error.gated; } if(this.type == 'constant') return; // Learning rate rate = rate || .1; // Adjust all the node's incoming connections for (var connection in this.connections.in) { var connection = this.connections.in[connection]; var gradient = this.error.projected * connection.elegibility; for(var i = 0; i < connection.xtrace.nodes.length; i++){ var node = connection.xtrace.nodes[i]; var value = connection.xtrace.values[i]; gradient += node.error.responsibility * value; } gradient *= this.mask; connection.weight += rate * gradient; // Adjust weights } // Adjust bias this.bias += rate * this.error.responsibility; }, /** * Creates a connection from this node to the given node */ connect: function(target, weight){ var connections = []; if(target instanceof Group){ for(var i = 0; i < target.nodes.length; i++){ var connection = new Connection(this, target.nodes[i], weight); target.nodes[i].connections.in.push(connection); this.connections.out.push(connection); target.connections.in.push(connection); connections.push(connection); } } else if(target instanceof Node){ if(target == this){ // Turn on the self connection by setting the weight if(this.connections.self.weight != 0){ if(Config.warnings) console.warn('This connection already exists!'); } else { this.connections.self.weight = weight || 1; } connections.push(this.connections.self); } else if (this.isProjectingTo(target)){ throw new Error('Already projecting a connection to this node!'); } else { var connection = new Connection(this, target, weight); target.connections.in.push(connection); this.connections.out.push(connection); connections.push(connection); } } return connections; }, /** * Disconnects this node from the other node */ disconnect: function(node, twosided){ if(this == node){ this.connections.self.weight = 0; return; } for(var i in this.connections.out){ var conn = this.connections.out[i]; if(conn.to == node){ this.connections.out.splice(i, 1); var j = conn.to.connections.in.indexOf(conn); conn.to.connections.in.splice(j, 1); if(conn.gater != null) this.ungate(conn); break; } } if(twosided){ node.disconnect(this); } }, /** * Make this node gate a connection */ gate: function(connections){ if(!Array.isArray(connections)){ connections = [connections]; } for(var connection in connections){ connection = connections[connection]; this.connections.gated.push(connection); connection.gater = this; } }, /** * Removes the gates from this node from the given connection(s) */ ungate: function(connections){ if(!Array.isArray(connections)){ connections = [connections]; } for(var i = connections.length - 1; i >= 0; i--){ var connection = connections[i]; var index = this.connections.gated.indexOf(connection); this.connections.gated.splice(index, 1); connection.gater = null; } }, /** * Clear the context of the node */ clear: function(){ for (var connection in this.connections.in) { connection = this.connections.in[connection]; connection.elegibility = 0; connection.xtrace = { nodes: [], values : [] }; } this.error.responsibility = this.error.projected = this.error.gated = 0; this.old = this.state = this.activation = 0; }, /** * Mutates the node with the given method */ mutate: function(method){ if(typeof method == 'undefined'){ throw new Error('No mutate method given!'); } else if(!method.name in Methods.Mutation){ throw new Error('This method does not exist!'); } switch(method){ case Mutation.MOD_ACTIVATION: // Can't be the same squash var squash = method.allowed[(method.allowed.indexOf(this.squash) + Math.floor(Math.random() * (method.allowed.length - 1)) + 1) % method.allowed.length]; this.squash = squash; break; case Mutation.MOD_BIAS: var modification = Math.random() * (method.max - method.min) + method.min; this.bias += modification; break; } }, /** * Checks if this node is projecting to the given node */ isProjectingTo: function(node){ for(conn in this.connections.out){ conn = this.connections.out[conn]; if(conn.to == node){ return true; } } return false; }, /** * Checks if the given node is projecting to this node */ isProjectedBy: function(node){ for(conn in this.connections.in){ conn = this.connections.in[conn]; if(conn.from == node){ return true; } } return false; }, /** * Converts the node to a json object */ toJSON: function(){ var json = { bias : this.bias, type : this.type, squash : this.squash.name, mask : this.mask }; return json; } }; /** * Convert a json object to a node */ Node.fromJSON = function(json){ var node = new Node(); node.bias = json.bias; node.type = json.type; node.mask = json.mask; for(squash in Activation){ if(Activation[squash].name == json.squash){ node.squash = Activation[squash]; break; } } return node; } /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(0)(module))) /***/ }), /* 3 */ /***/ (function(module, exports, __webpack_require__) { /* WEBPACK VAR INJECTION */(function(module) {/******************************************************************************************* CONFIG *******************************************************************************************/ // Config var Config = { warnings: true }; /* Export */ if (module) module.exports = Config; /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(0)(module))) /***/ }), /* 4 */ /***/ (function(module, exports, __webpack_require__) { /* WEBPACK VAR INJECTION */(function(module) {/* Export */ if (module) module.exports = Connection; /****************************************************************************************** CONNECTION *******************************************************************************************/ function Connection(from, to, weight) { this.from = from; this.to = to; this.gain = 1; this.weight = (typeof weight == 'undefined') ? Math.random() * .2 - .1 : weight; this.gater = null; this.elegibility = 0; this.xtrace = { nodes: [], values : [] }; } Connection.prototype = { /** * Converts the connection to a json object */ toJSON : function(){ var json = { weight : this.weight }; return json; } }; /** * Returns an innovation ID * https://en.wikipedia.org/wiki/Pairing_function (Cantor pairing function) */ Connection.innovationID = function(a, b) { return 1/2 * (a + b) * (a + b + 1) + b; } /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(0)(module))) /***/ }), /* 5 */ /***/ (function(module, exports, __webpack_require__) { /* WEBPACK VAR INJECTION */(function(module) {/* Export */ if (module) module.exports = Group; /* Import */ var Methods = __webpack_require__(1); var Connection = __webpack_require__(4); var Node = __webpack_require__(2); var Config = __webpack_require__(3); var Layer = __webpack_require__(6); /* Easier variable naming */ var Activation = Methods.Activation; var Mutation = Methods.Mutation; /****************************************************************************************** Group *******************************************************************************************/ function Group(size){ this.nodes = []; this.connections = { in : [], out: [] , self: [] }; for(var i = 0; i < size; i++){ this.nodes.push(new Node()); } } Group.prototype = { /** * Activates all the nodes in the group */ activate: function(value){ var values = []; if(typeof value != 'undefined' && value.length != this.nodes.length){ throw new Error('Array with values should be same as the amount of nodes!'); } for(var i = 0; i < this.nodes.length; i++){ if(typeof value == 'undefined'){ var activation = this.nodes[i].activate(); } else { var activation = this.nodes[i].activate(value[i]); } values.push(activation); } return values; }, /** * Propagates all the node in the group */ propagate: function(rate, target){ if(typeof target != 'undefined' && target.length != this.nodes.length){ throw new Error('Array with values should be same as the amount of nodes!'); } for(var i = this.nodes.length - 1; i >= 0; i--){ if(typeof target == 'undefined'){ this.nodes[i].propagate(rate); } else { this.nodes[i].propagate(rate, target[i]); } } }, /** * Connects the nodes in this group to nodes in another group or just a node */ connect: function(target, method, weight){ var connections = []; if(target instanceof Group){ if(typeof method == 'undefined'){ if(this != target){ if(Config.warnings) console.warn('No group connection specified, using ALL_TO_ALL'); method = Methods.Connection.ALL_TO_ALL; } else { if(Config.warnings) console.warn('No group connection specified, using ONE_TO_ONE'); method = Methods.Connection.ONE_TO_ONE; } } if(method == Methods.Connection.ALL_TO_ALL || method == Methods.Connection.ALL_TO_ELSE){ for(var i = 0; i < this.nodes.length; i++){ for(var j = 0; j < target.nodes.length; j++){ if(method == Methods.Connection.ALL_TO_ELSE && this.nodes[i] == target.nodes[j]) continue; var connection = this.nodes[i].connect(target.nodes[j], weight); this.connections.out.push(connection[0]); target.connections.in.push(connection[0]); connections.push(connection[0]); } } } else if(method == Methods.Connection.ONE_TO_ONE){ if(this.nodes.length != target.nodes.length){ throw new Error('From and To group must be the same size!'); } for(var i = 0; i < this.nodes.length; i++){ var connection = this.nodes[i].connect(target.nodes[i], weight); this.connections.self.push(connection[0]); connections.push(connection[0]); } } } else if(target instanceof Layer){ var connections = target.input(this, method, weight); } else if(target instanceof Node){ for(var i = 0; i < this.nodes.length; i++){ var connection = this.nodes[i].connect(target, weight); this.connections.out.push(connection[0]); connections.push(connection[0]); } } return connections; }, /** * Make nodes from this group gate the given connection(s) */ gate: function(connections, method){ if(typeof method == 'undefined'){ throw new Error('Please specify Gating.INPUT, Gating.OUTPUT'); } if(!Array.isArray(connections)){ connections = [connections]; } var nodes1 = []; var nodes2 = []; for(var connection in connections){ connection = connections[connection]; if(!nodes1.includes(connection.from)) nodes1.push(connection.from); if(!nodes2.includes(connection.to)) nodes2.push(connection.to); } switch(method){ case Methods.Gating.INPUT: for(var i = 0; i < nodes2.length; i++){ var node = nodes2[i]; var gater = this.nodes[i % this.nodes.length]; for(var conn in node.connections.in){ conn = node.connections.in[conn]; if(connections.includes(conn)){ gater.gate(conn); } } } break; case Methods.Gating.OUTPUT: for(var i = 0; i < nodes1.length; i++){ var node = nodes1[i]; var gater = this.nodes[i % this.nodes.length]; for(var conn in node.connections.out){ conn = node.connections.out[conn]; if(connections.includes(conn)){ gater.gate(conn); } } } break; case Methods.Gating.SELF: for(var i = 0; i < nodes1.length; i++){ var node = nodes1[i]; var gater = this.nodes[i % this.nodes.length]; if(connections.includes(node.connections.self)){ gater.gate(node.connections.self); } } } }, /** * Sets the value of a property for every node */ set: function(values){ for(var node in this.nodes){ if(typeof values.bias != 'undefined'){ this.nodes[node].bias = values.bias; } this.nodes[node].squash = values.squash || this.nodes[node].squash; this.nodes[node].type = values.type || this.nodes[node].type; } }, /** * Disconnects all nodes from this group from another given group/node */ disconnect: function(target, twosided){ twosided = twosided || false; // In the future, disconnect will return a connection so indexOf can be used if(target instanceof Group){ for(var i = 0; i < this.nodes.length; i++){ for(var j = 0; j < target.nodes.length; j++){ this.nodes[i].disconnect(target.nodes[j], twosided); for(index in this.connections.out){ var conn = this.connections.out[index]; if(conn.from == this.nodes[i] && conn.to == target.nodes[j]){ this.connections.out.splice(index, 1); break; } } if(twosided){ for(index in this.connections.in){ var conn = this.connections.in[index]; if(conn.from == target.nodes[j] && conn.to == this.nodes[i]){ this.connections.in.splice(index, 1); break; } } } } } } else if(target instanceof Node){ for(var i = 0; i < this.nodes.length; i++){ var connection = this.nodes[i].disconnect(target, twosided); for(index in this.connections.out){ var conn = this.connections.out[index]; if(conn.from == this.nodes[i] && conn.to == target){ this.connections.out.splice(index, 1); break; } } if(twosided){ for(index in this.connections.in){ var conn = this.connections.in[index]; if(conn.from == target && conn.to == this.nodes[i]){ this.connections.in.splice(index, 1); break; } } } } } }, /** * Clear the context of this group */ clear: function(){ for(var node in this.nodes){ this.nodes[node].clear(); } } } /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(0)(module))) /***/ }), /* 6 */ /***/ (function(module, exports, __webpack_require__) { /* WEBPACK VAR INJECTION */(function(module) {/* Export */ if (module) module.exports = Layer; /* Import */ var Methods = __webpack_require__(1); var Connection = __webpack_require__(4); var Node = __webpack_require__(2); var Config = __webpack_require__(3); var Architect = __webpack_require__(8); var Group = __webpack_require__(5); /* Easier variable naming */ var Activation = Methods.Activation; var Mutation = Methods.Mutation; /****************************************************************************************** Group *******************************************************************************************/ function Layer(){ this.output = null; this.nodes = []; this.connections = { in : [], out: [] , self: [] }; } Layer.prototype = { /** * Activates all the nodes in the group */ activate: function(value){ var values = []; if(typeof value != 'undefined' && value.length != this.nodes.length){ throw new Error('Array with values should be same as the amount of nodes!'); } for(var i = 0; i < this.nodes.length; i++){ if(typeof value == 'undefined'){ var activation = this.nodes[i].activate(); } else { var activation = this.nodes[i].activate(value[i]); } values.push(activation); } return values; }, /** * Propagates all the node in the group */ propagate: function(rate, target){ if(typeof target != 'undefined' && target.length != this.nodes.length){ throw new Error('Array with values should be same as the amount of nodes!'); } for(var i = this.nodes.length - 1; i >= 0; i--){ if(typeof target == 'undefined'){ this.nodes[i].propagate(rate); } else { this.nodes[i].propagate(rate, target[i]); } } }, /** * Connects the nodes in this group to nodes in another group or just a node */ connect: function(target, method, weight){ if(target instanceof Group || target instanceof Node){ var connections = this.output.connect(target, method, weight); } else if (target instanceof Layer){ var connections = target.input(this, method, weight); } return connections; }, /** * Make nodes from this group gate the given connection(s) */ gate: function(connections, method){ this.output.gate(connections, method); }, /** * Sets the value of a property for every node */ set: function(values){ for(var node in this.nodes){ node = this.nodes[node]; if(node instanceof Node){ if(typeof values.bias != 'undefined'){ node.bias = values.bias; } node.squash = values.squash || node.squash; node.type = values.type || node.type; } else if(node instanceof Group){ node.set(values); } } }, /** * Disconnects all nodes from this group from another given group/node */ disconnect: function(target, twosided){ twosided = twosided || false; // In the future, disconnect will return a connection so indexOf can be used if(target instanceof Group){ for(var i = 0; i < this.nodes.length; i++){ for(var j = 0; j < target.nodes.length; j++){ this.nodes[i].disconnect(target.nodes[j], twosided); for(index in this.connections.out){ var conn = this.connections.out[index]; if(conn.from == this.nodes[i] && conn.to == target.nodes[j]){ this.connections.out.splice(index, 1); break; } } if(twosided){ for(index in this.connections.in){ var conn = this.connections.in[index]; if(conn.from == target.nodes[j] && conn.to == this.nodes[i]){ this.connections.in.splice(index, 1); break; } } } } } } else if(target instanceof Node){ for(var i = 0; i < this.nodes.length; i++){ var connection = this.nodes[i].disconnect(target, twosided); for(index in this.connections.out){ var conn = this.connections.out[index]; if(conn.from == this.nodes[i] && conn.to == target){ this.connections.out.splice(index, 1); break; } } if(twosided){ for(index in this.connections.in){ var conn = this.connections.in[index]; if(conn.from == target && conn.to == this.nodes[i]){ this.connections.in.splice(index, 1); break; } } } } } }, /** * Clear the context of this group */ clear: function(){ for(var node in this.nodes){ this.nodes[node].clear(); } } } Layer.Dense = function(size){ // Create the layer var layer = new Layer(); // Init required nodes (in activation order) var block = new Group(size); layer.nodes.push(block); layer.output = block; layer.input = function(from, method, weight){ if(from instanceof Layer) from = from.output; method = method || Methods.Connection.ALL_TO_ALL; return from.connect(block, method, weight); } return layer; } Layer.LSTM = function(size){ // Create the layer var layer = new Layer(); // Init required nodes (in activation order) var inputGate = new Group(size); var forgetGate = new Group(size); var memoryCell = new Group(size); var outputGate = new Group(size); var outputBlock = new Group(size); inputGate.set({ bias:1 }); forgetGate.set({ bias:1 }); outputGate.set({ bias:1 }); // Set up internal connections memoryCell.connect(inputGate, Methods.Connection.ALL_TO_ALL); memoryCell.connect(forgetGate, Methods.Connection.ALL_TO_ALL); memoryCell.connect(outputGate, Methods.Connection.ALL_TO_ALL); var forget = memoryCell.connect(memoryCell, Methods.Connection.ONE_TO_ONE); var output = memoryCell.connect(outputBlock, Methods.Connection.ALL_TO_ALL); // Set up gates forgetGate.gate(forget, Methods.Gating.SELF); outputGate.gate(output, Methods.Gating.OUTPUT); // Add to nodes array layer.nodes = [inputGate, forgetGate, memoryCell, outputGate, outputBlock]; // Define output layer.output = outputBlock; layer.input = function(from, method, weight){ if(from instanceof Layer) from = from.output; method = method || Methods.Connection.ALL_TO_ALL; var connections = []; var input = from.connect(memoryCell, method, weight); connections = connections.concat(input); connections = connections.concat(from.connect(inputGate, method, weight)); connections = connections.concat(from.connect(outputGate, method, weight)); connections = connections.concat(from.connect(forgetGate, method, weight)); inputGate.gate(input, Methods.Gating.INPUT); return connections; } return layer; }; Layer.GRU = function(size){ // Create the layer var layer = new Layer(); var updateGate = new Group(size); var inverseUpdateGate = new Group(size); var resetGate = new Group(size); var memoryCell = new Group(size); var output = new Group(size); var previousOutput = new Group(size); previousOutput.set({ bias: 0, squash: Methods.Activation.IDENTITY, type: 'constant'}); memoryCell.set({ squash: Methods.Activation.TANH }); inverseUpdateGate.set({ bias: 0, squash: Methods.Activation.INVERSE, type: 'constant'}); updateGate.set({ bias: 1 }); resetGate.set({ bias: 0 }); // Update gate calculation previousOutput.connect(updateGate, Methods.Connection.ALL_TO_ALL); // Inverse update gate calculation updateGate.connect(inverseUpdateGate, Methods.Connection.ONE_TO_ONE, 1); // Reset gate calculation previousOutput.connect(resetGate, Methods.Connection.ALL_TO_ALL); // Memory calculation var reset = previousOutput.connect(memoryCell, Methods.Connection.ALL_TO_ALL); resetGate.gate(reset, Methods.Gating.OUTPUT); // gate // Output calculation var update1 = previousOutput.connect(output, Methods.Connection.ALL_TO_ALL); var update2 = memoryCell.connect(output, Methods.Connection.ALL_TO_ALL); updateGate.gate(update1, Methods.Gating.OUTPUT); inverseUpdateGate.gate(update2, Methods.Gating.OUTPUT); // Previous output calculation output.connect(previousOutput, Methods.Connection.ONE_TO_ONE, 1); // Add to nodes array layer.nodes = [updateGate, inverseUpdateGate, resetGate, memoryCell, output, previousOutput]; layer.output = output; layer.input = function(from, method, weight){ if(from instanceof Layer) from = from.output; method = method || Methods.Connection.ALL_TO_ALL; var connections = []; connections = connections.concat(from.connect(updateGate, method, weight)); connections = connections.concat(from.connect(resetGate, method, weight)); connections = connections.concat(from.connect(memoryCell, method, weight)); return connections; } return layer; } Layer.Memory = function(size, memory){ // Create the layer var layer = new Layer(); // Because the output can only be one group, we have to put the nodes all in óne group var previous = null; for(var i = 0; i < memory; i++){ var block = new Group(size); block.set({ squash: Methods.Activation.IDENTITY, bias: 0, type: 'constant' }); if(previous != null){ previous.connect(block, Methods.Connection.ONE_TO_ONE, 1); } layer.nodes.push(block); previous = block; } layer.nodes.reverse(); for(var group in layer.nodes){ layer.nodes[group].nodes.reverse(); } // Because output can only be óne group, fit all memory nodes in óne group var outputGroup = new Group(0); for(var group in layer.nodes){ outputGroup.nodes = outputGroup.nodes.concat(layer.nodes[group].nodes); } layer.output = outputGroup; layer.input = function(from, method, weight){ if(from instanceof Layer) from = from.output; method = method || Methods.Connection.ALL_TO_ALL; if(from.nodes.length != layer.nodes[layer.nodes.length-1].nodes.length){ throw new Error('Previous layer size must be same as memory size'); } return from.connect(layer.nodes[layer.nodes.length-1], Methods.Connection.ONE_TO_ONE, 1); } return layer; } /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(0)(module))) /***/ }), /* 7 */ /***/ (function(module, exports, __webpack_require__) { /* WEBPACK VAR INJECTION */(function(module) {/* Export */ if (module) module.exports = Network; /* Import */ var Node = __webpack_require__(2); var Connection = __webpack_require__(4); var Methods = __webpack_require__(1); var Config = __webpack_require__(3); var Neat = __webpack_require__(9); /* Easier variable naming */ var Activation = Methods.Activation; var Mutation = Methods.Mutation; /******************************************************************************************* NETWORK *******************************************************************************************/ function Network(input, output){ if(typeof input == 'undefined' || typeof output == 'undefined'){ throw new Error('No input or output size given'); } this.input = input; this.output = output; // Store all the node and connection genes this.nodes = []; // Stored in activation order this.connections = []; this.gates = []; this.selfconns = []; // Regularization this.dropout = 0; // Create input and output nodes for(var i = 0; i < this.input + this.output; i++){ var type = (i < this.input) ? 'input' : 'output'; this.nodes.push(new Node(type, this.nodes.length)); } // Connect input nodes with output nodes directly for(var i = 0; i < this.input; i++){ for(var j = this.input; j < this.output + this.input; j++){ this.connect(this.nodes[i], this.nodes[j]); } } } Network.prototype = { /** * Activates the network */ activate: function(input, training){ var output = []; // Activate nodes chronologically for(node in this.nodes){ if(this.nodes[node].type == 'input'){ this.nodes[node].activate(input[node]); } else if (this.nodes[node].type == 'output'){ var activation = this.nodes[node].activate(); output.push(activation); } else { if(training) this.nodes[node].mask = Math.random() < this.dropout ? 0 : 1; this.nodes[node].activate(); } } return output; }, /** * Backpropagate the network */ propagate: function(rate, target){ this.nodes.reverse(); target.reverse(); // Propagate nodes from end to start for(node in this.nodes){ switch(this.nodes[node].type){ case('hidden'): this.nodes[node].propagate(rate); break; case('output'): this.nodes[node].propagate(rate, target[node]); break; } } target.reverse(); this.nodes.reverse(); }, /** * Clear the context of the network */ clear: function(){ for(var node in this.nodes){ this.nodes[node].clear(); } }, /** * Connects the from node to the to node */ connect: function(from, to){ var connections = from.connect(to); for(var connection in connections){ connection = connections[connection]; if(from != to){ this.connections.push(connection); } else { this.selfconns.push(connection); } } return connections; }, /** * Disconnects the from node from the to node */ disconnect: function(from, to){ // Delete the connection in the network's connection array var connections = from == to ? this.selfconns : this.connections; for(conn in connections){ var connection = connections[conn]; if(connection.from == from && connection.to == to){ if(connection.gater != null) this.ungate(connection); connections.splice(conn, 1); break; } } // Delete the connection at the sending and receiving neuron from.disconnect(to); }, /** * Gate a connection with a node */ gate: function(node, connection){ if(this.nodes.indexOf(node) == -1){ throw new Error('This node is not part of the network!'); } else if (connection.gater != null){ if(Config.warnings) console.warn('This connection is already gated!'); return; } node.gate(connection); this.gates.push(connection); }, /** * Remove the gate of a connection */ ungate: function(connection){ var index = this.gates.indexOf(connection); if(index == -1){ throw new Error('This connection is not gated!'); } this.gates.splice(index, 1); connection.gater.ungate(connection); }, /** * Removes a node from the network */ remove: function(node){ var index = this.nodes.indexOf(node); if(index == -1){ throw new Error('This node does not exist in the network!'); } // Keep track of gaters var gaters = []; // Get all its inputting nodes var inputs = []; for(var i = node.connections.in.length - 1; i >= 0; i--){ var connection = node.connections.in[i]; if(Methods.Mutation.SUB_NODE.keep_gates && connection.gater != null && connection.gater != node){ gaters.push(connection.gater); } inputs.push(connection.from); this.disconnect(connection.from, node); } // Get all its outputing nodes var outputs = []; for(var i = node.connections.out.length - 1; i >= 0; i--){ var connection = node.connections.out[i]; if(Methods.Mutation.SUB_NODE.keep_gates && connection.gater != null && connection.gater != node){ gaters.push(connection.gater); } outputs.push(connection.to); this.disconnect(node, connection.to); } // Connect the input nodes to the output nodes (if not already connected) var connections = []; for(var input in inputs){ input = inputs[input]; for(var output in outputs){ output = outputs[output]; if(!input.isProjectingTo(output)){ var conn = this.connect(input, output); connections.push(conn[0]); } } } // Gate random connections with gaters for(var gater in gaters){ if(connections.length == 0) break; gater = gaters[gater]; var connIndex = Math.floor(Math.random() * connections.length); var conn = connections[connIndex]; this.gate(gater, connections[connIndex]); connections.splice(connIndex, 1); } // Remove gated connections gated by this node for(var i = node.connections.gated.length - 1; i >= 0 ; i--){ var conn = node.connections.gated[i]; this.ungate(conn); } // Remove selfconnection this.disconnect(node, node); // Remove the node from this.nodes this.nodes.splice(index, 1); }, /** * Mutates the network with the given method */ mutate: function(method){ if(typeof method == 'undefined'){ throw new Error('No (correct) mutate method given!'); } switch(method){ case Mutation.ADD_NODE: // Look for an existing connection and place a node in between var connection = this.connections[Math.floor(Math.random() * this.connections.length)]; var gater = connection.gater; this.disconnect(connection.from, connection.to); // Insert the new node right before the old connection.to var toIndex = this.nodes.indexOf(connection.to); var node = new Node('hidden', this.nodes.length); // Random squash function node.mutate(Mutation.MOD_ACTIVATION); // Place it in this.nodes var minBound = Math.min(toIndex, this.nodes.length - this.output); this.nodes.splice(minBound, 0, node); // Now create two new connections var newConn1 = this.connect(connection.from, node)[0]; var newConn2 = this.connect(node, connection.to)[0]; // Check if the original connection was gated if(gater != null){ this.gate(gater, Math.random() >= 0.5 ? newConn1 : newConn2); } break; case Mutation.SUB_NODE: // Check if there are nodes left to remove if(this.nodes.length == this.input + this.output){ if(Config.warnings) console.warn('No more nodes left to remove!'); break; } // Select a node which isn't an input or output node var index = Math.floor(Math.random() * (this.nodes.length - this.output - this.input) + this.input); this.remove(this.nodes[index]); break; case Mutation.ADD_CONN: // Create an array of all uncreated (feedforward) connections var available = []; for(var i = 0; i < this.nodes.length - this.output; i++){ var node1 = this.nodes[i]; for(var j = Math.max(i + 1, this.input); j < this.nodes.length; j++){ var node2 = this.nodes[j]; if(!node1.isProjectingTo(node2)) available.push([node1, node2]); } } if(available.length == 0){ if(Config.warnings) console.warn('No more connections to be made!'); break; } var pair = available[Math.floor(Math.random() * available.length)]; this.connect(pair[0], pair[1]); break; case Mutation.SUB_CONN: // List of possible connections that can be removed var possible = []; for(conn in this.connections){ conn = this.connections[conn]; // Check if it is not disabling a node if(conn.from.connections.out.length > 1 && conn.to.connections.in.length > 1 && this.nodes.indexOf(conn.to) > this.nodes.indexOf(conn.from)){ possible.push(conn); } } if(possible.length == 0){ if(Config.warnings) console.warn('No connections to remove!'); break; } var randomConn = possible[Math.floor(Math.random() * possible.length)]; this.disconnect(randomConn.from, randomConn.to); break; case Mutation.MOD_WEIGHT: var connection = this.connections[Math.floor(Math.random() * this.connections.length)]; var modification = Math.random() * (method.max - method.min) + method.min; connection.weight += modification; break; case Mutation.MOD_BIAS: // Has no effect on input node, so they are excluded var index = Math.floor(Math.random() * (this.nodes.length - this.input) + this.input); var node = this.nodes[index]; node.mutate(method); break; case Mutation.MOD_ACTIVATION: // Has no effect on input node, so they are excluded if(!method.mutateOutput && this.input + this.output == this.nodes.length){ if(Config.warnings) console.warn('No nodes that allow mutation of activation function'); break; } var index = Math.floor(Math.random() * (this.nodes.length - (method.mutateOutput ? 0 : this.output) - this.input) + this.input); var node = this.nodes[index]; node.mutate(method); break; case Mutation.ADD_SELF_CONN: // Check which nodes aren't selfconnected yet var possible = []; for(var i = this.input; i < this.nodes.length; i++){ var node = this.nodes[i]; if(node.connections.self.weight == 0){ possible.push(node); } } if(possible.length == 0){ if(Config.warnings) console.warn('No more self-connections to add!'); break; } // Select a random node var node = possible[Math.floor(Math.random() * possible.length)]; // Connect it to himself this.connect(node, node); break; case Mutation.SUB_SELF_CONN: if(this.selfconns.length == 0){