UNPKG

neataptic

Version:

Architecture-free neural network library with genetic algorithm implementations

1,579 lines (1,340 loc) 133 kB
/*! * The MIT License (MIT) * * Copyright 2017 Thomas Wagenaar <wagenaartje@protonmail.com>. Copyright for * portions of Neataptic are held by Copyright 2017 Juan Cazala - cazala.com, as a * part of project Synaptic. * * 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(require("child_process"), require("os")); else if(typeof define === 'function' && define.amd) define(["child_process", "os"], factory); else if(typeof exports === 'object') exports["neataptic"] = factory(require("child_process"), require("os")); else root["neataptic"] = factory(root["child_process"], root["os"]); })(this, function(__WEBPACK_EXTERNAL_MODULE_21__, __WEBPACK_EXTERNAL_MODULE_25__) { 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; /******/ /******/ // 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 = 10); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { /******************************************************************************* METHODS *******************************************************************************/ var methods = { activation: __webpack_require__(8), mutation: __webpack_require__(11), selection: __webpack_require__(12), crossover: __webpack_require__(13), cost: __webpack_require__(14), gating: __webpack_require__(15), connection: __webpack_require__(16), rate: __webpack_require__(17) }; /** Export */ module.exports = methods; /***/ }), /* 1 */ /***/ (function(module, exports) { /******************************************************************************* CONFIG *******************************************************************************/ // Config var config = { warnings: false }; /* Export */ module.exports = config; /***/ }), /* 2 */ /***/ (function(module, exports, __webpack_require__) { /* Export */ module.exports = Node; /* Import */ var methods = __webpack_require__(0); var Connection = __webpack_require__(3); var config = __webpack_require__(1); /******************************************************************************* NODE *******************************************************************************/ function Node (type) { this.bias = (type === 'input') ? 0 : Math.random() * 0.2 - 0.1; this.squash = methods.activation.LOGISTIC; this.type = type || 'hidden'; this.activation = 0; this.state = 0; this.old = 0; // For dropout this.mask = 1; // For tracking momentum this.previousDeltaBias = 0; // Batch training this.totalDeltaBias = 0; 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; 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 var i; for (i = 0; i < this.connections.in.length; i++) { var connection = this.connections.in[i]; 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 (i = 0; i < this.connections.gated.length; i++) { let conn = this.connections.gated[i]; let node = conn.to; let index = nodes.indexOf(node); if (index > -1) { influences[index] += conn.weight * conn.from.activation; } else { nodes.push(node); influences.push(conn.weight * conn.from.activation + (node.connections.self.gater === this ? node.old : 0)); } // Adjust the gain to this nodes' activation conn.gain = this.activation; } for (i = 0; i < this.connections.in.length; i++) { let connection = this.connections.in[i]; // Elegibility trace connection.elegibility = this.connections.self.gain * this.connections.self.weight * connection.elegibility + connection.from.activation * connection.gain; // Extended trace for (var j = 0; j < nodes.length; j++) { let node = nodes[j]; let influence = influences[j]; let 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); } } } return this.activation; }, /** * Activates the node without calculating elegibility traces and such */ noTraceActivate: function (input) { // Check if an input is given if (typeof input !== 'undefined') { this.activation = input; return this.activation; } // 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 var i; for (i = 0; i < this.connections.in.length; i++) { var connection = this.connections.in[i]; this.state += connection.from.activation * connection.weight * connection.gain; } // Squash the values received this.activation = this.squash(this.state); for (i = 0; i < this.connections.gated.length; i++) { this.connections.gated[i].gain = this.activation; } return this.activation; }, /** * Back-propagate the error, aka learn */ propagate: function (rate, momentum, update, target) { momentum = momentum || 0; rate = rate || 0.3; // 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 var i; for (i = 0; i < this.connections.out.length; i++) { let connection = this.connections.out[i]; let 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 (i = 0; i < this.connections.gated.length; i++) { let conn = this.connections.gated[i]; let node = conn.to; let 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; // Adjust all the node's incoming connections for (i = 0; i < this.connections.in.length; i++) { let connection = this.connections.in[i]; let gradient = this.error.projected * connection.elegibility; for (var j = 0; j < connection.xtrace.nodes.length; j++) { let node = connection.xtrace.nodes[j]; let value = connection.xtrace.values[j]; gradient += node.error.responsibility * value; } // Adjust weight let deltaWeight = rate * gradient * this.mask; connection.totalDeltaWeight += deltaWeight; if (update) { connection.totalDeltaWeight += momentum * connection.previousDeltaWeight; connection.weight += connection.totalDeltaWeight; connection.previousDeltaWeight = connection.totalDeltaWeight; connection.totalDeltaWeight = 0; } } // Adjust bias var deltaBias = rate * this.error.responsibility; this.totalDeltaBias += deltaBias; if (update) { this.totalDeltaBias += momentum * this.previousDeltaBias; this.bias += this.totalDeltaBias; this.previousDeltaBias = this.totalDeltaBias; this.totalDeltaBias = 0; } }, /** * Creates a connection from this node to the given node */ connect: function (target, weight) { var connections = []; if (typeof target.bias !== 'undefined') { // must be a 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 { let connection = new Connection(this, target, weight); target.connections.in.push(connection); this.connections.out.push(connection); connections.push(connection); } } else { // should be a group for (var i = 0; i < target.nodes.length; i++) { let 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); } } 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 = 0; i < this.connections.out.length; i++) { let conn = this.connections.out[i]; if (conn.to === node) { this.connections.out.splice(i, 1); let j = conn.to.connections.in.indexOf(conn); conn.to.connections.in.splice(j, 1); if (conn.gater !== null) conn.gater.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 i = 0; i < connections.length; i++) { var connection = connections[i]; 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; connection.gain = 1; } }, /** * Clear the context of the node */ clear: function () { for (var i = 0; i < this.connections.in.length; i++) { var connection = this.connections.in[i]; connection.elegibility = 0; connection.xtrace = { nodes: [], values: [] }; } for (i = 0; i < this.connections.gated.length; i++) { let conn = this.connections.gated[i]; conn.gain = 0; } 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 methods.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 methods.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) { if (node === this && this.connections.self.weight !== 0) return true; for (var i = 0; i < this.connections.out.length; i++) { var conn = this.connections.out[i]; if (conn.to === node) { return true; } } return false; }, /** * Checks if the given node is projecting to this node */ isProjectedBy: function (node) { if (node === this && this.connections.self.weight !== 0) return true; for (var i = 0; i < this.connections.in.length; i++) { var conn = this.connections.in[i]; 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; node.squash = methods.activation[json.squash]; return node; }; /***/ }), /* 3 */ /***/ (function(module, exports) { /* Export */ 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() * 0.2 - 0.1 : weight; this.gater = null; this.elegibility = 0; // For tracking momentum this.previousDeltaWeight = 0; // Batch training this.totalDeltaWeight = 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; }; /***/ }), /* 4 */ /***/ (function(module, exports, __webpack_require__) { /* Export */ module.exports = Network; /* Import */ var multi = __webpack_require__(5); var methods = __webpack_require__(0); var Connection = __webpack_require__(3); var config = __webpack_require__(1); var Neat = __webpack_require__(9); var Node = __webpack_require__(2); /* Easier variable naming */ 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 var i; for (i = 0; i < this.input + this.output; i++) { var type = i < this.input ? 'input' : 'output'; this.nodes.push(new Node(type)); } // Connect input nodes with output nodes directly for (i = 0; i < this.input; i++) { for (var j = this.input; j < this.output + this.input; j++) { // https://stats.stackexchange.com/a/248040/147931 var weight = Math.random() * this.input * Math.sqrt(2 / this.input); this.connect(this.nodes[i], this.nodes[j], weight); } } } Network.prototype = { /** * Activates the network */ activate: function (input, training) { var output = []; // Activate nodes chronologically for (var i = 0; i < this.nodes.length; i++) { if (this.nodes[i].type === 'input') { this.nodes[i].activate(input[i]); } else if (this.nodes[i].type === 'output') { var activation = this.nodes[i].activate(); output.push(activation); } else { if (training) this.nodes[i].mask = Math.random() < this.dropout ? 0 : 1; this.nodes[i].activate(); } } return output; }, /** * Activates the network without calculating elegibility traces and such */ noTraceActivate: function (input) { var output = []; // Activate nodes chronologically for (var i = 0; i < this.nodes.length; i++) { if (this.nodes[i].type === 'input') { this.nodes[i].noTraceActivate(input[i]); } else if (this.nodes[i].type === 'output') { var activation = this.nodes[i].noTraceActivate(); output.push(activation); } else { this.nodes[i].noTraceActivate(); } } return output; }, /** * Backpropagate the network */ propagate: function (rate, momentum, update, target) { if (typeof target === 'undefined' || target.length !== this.output) { throw new Error('Output target length should match network output length'); } var targetIndex = target.length; // Propagate output nodes var i; for (i = this.nodes.length - 1; i >= this.nodes.length - this.output; i--) { this.nodes[i].propagate(rate, momentum, update, target[--targetIndex]); } // Propagate hidden and input nodes for (i = this.nodes.length - this.output - 1; i >= this.input; i--) { this.nodes[i].propagate(rate, momentum, update); } }, /** * Clear the context of the network */ clear: function () { for (var i = 0; i < this.nodes.length; i++) { this.nodes[i].clear(); } }, /** * Connects the from node to the to node */ connect: function (from, to, weight) { var connections = from.connect(to, weight); for (var i = 0; i < connections.length; i++) { var connection = connections[i]; 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 (var i = 0; i < connections.length; i++) { var connection = connections[i]; if (connection.from === from && connection.to === to) { if (connection.gater !== null) this.ungate(connection); connections.splice(i, 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 = []; // Remove selfconnections from this.selfconns this.disconnect(node, node); // Get all its inputting nodes var inputs = []; for (var i = node.connections.in.length - 1; i >= 0; i--) { let connection = node.connections.in[i]; if (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 (i = node.connections.out.length - 1; i >= 0; i--) { let connection = node.connections.out[i]; if (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 (i = 0; i < inputs.length; i++) { let input = inputs[i]; for (var j = 0; j < outputs.length; j++) { let output = outputs[j]; if (!input.isProjectingTo(output)) { var conn = this.connect(input, output); connections.push(conn[0]); } } } // Gate random connections with gaters for (i = 0; i < gaters.length; i++) { if (connections.length === 0) break; let gater = gaters[i]; let connIndex = Math.floor(Math.random() * connections.length); this.gate(gater, connections[connIndex]); connections.splice(connIndex, 1); } // Remove gated connections gated by this node for (i = node.connections.gated.length - 1; i >= 0; i--) { let 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!'); } var i, j; 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'); // 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 (i = 0; i < this.nodes.length - this.output; i++) { let node1 = this.nodes[i]; for (j = Math.max(i + 1, this.input); j < this.nodes.length; j++) { let 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 (i = 0; i < this.connections.length; i++) { let conn = this.connections[i]; // 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 allconnections = this.connections.concat(this.selfconns); var connection = allconnections[Math.floor(Math.random() * allconnections.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 (i = this.input; i < this.nodes.length; i++) { let 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) { if (config.warnings) console.warn('No more self-connections to remove!'); break; } var conn = this.selfconns[Math.floor(Math.random() * this.selfconns.length)]; this.disconnect(conn.from, conn.to); break; case mutation.ADD_GATE: var allconnections = this.connections.concat(this.selfconns); // Create a list of all non-gated connections var possible = []; for (i = 0; i < allconnections.length; i++) { let conn = allconnections[i]; if (conn.gater === null) { possible.push(conn); } } if (possible.length === 0) { if (config.warnings) console.warn('No more connections to gate!'); break; } // Select a random gater node and connection, can't be gated by input var index = Math.floor(Math.random() * (this.nodes.length - this.input) + this.input); var node = this.nodes[index]; var conn = possible[Math.floor(Math.random() * possible.length)]; // Gate the connection with the node this.gate(node, conn); break; case mutation.SUB_GATE: // Select a random gated connection if (this.gates.length === 0) { if (config.warnings) console.warn('No more connections to ungate!'); break; } var index = Math.floor(Math.random() * this.gates.length); var gatedconn = this.gates[index]; this.ungate(gatedconn); break; case mutation.ADD_BACK_CONN: // Create an array of all uncreated (backfed) connections var available = []; for (i = this.input; i < this.nodes.length; i++) { let node1 = this.nodes[i]; for (j = this.input; j < i; j++) { let 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_BACK_CONN: // List of possible connections that can be removed var possible = []; for (i = 0; i < this.connections.length; i++) { let conn = this.connections[i]; // 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.from) > this.nodes.indexOf(conn.to)) { 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.SWAP_NODES: // Has no effect on input node, so they are excluded if ((method.mutateOutput && this.nodes.length - this.input < 2) || (!method.mutateOutput && this.nodes.length - this.input - this.output < 2)) { if (config.warnings) console.warn('No nodes that allow swapping of bias and activation function'); break; } var index = Math.floor(Math.random() * (this.nodes.length - (method.mutateOutput ? 0 : this.output) - this.input) + this.input); var node1 = this.nodes[index]; index = Math.floor(Math.random() * (this.nodes.length - (method.mutateOutput ? 0 : this.output) - this.input) + this.input); var node2 = this.nodes[index]; var biasTemp = node1.bias; var squashTemp = node1.squash; node1.bias = node2.bias; node1.squash = node2.squash; node2.bias = biasTemp; node2.squash = squashTemp; break; } }, /** * Train the given set to this network */ train: function (set, options) { if (set[0].input.length !== this.input || set[0].output.length !== this.output) { throw new Error('Dataset input/output size should be same as network input/output size!'); } options = options || {}; // Warning messages if (typeof options.rate === 'undefined') { if (config.warnings) console.warn('Using default learning rate, please define a rate!'); } if (typeof options.iterations === 'undefined') { if (config.warnings) console.warn('No target iterations given, running until error is reached!'); } // Read the options var targetError = options.error || 0.05; var cost = options.cost || methods.cost.MSE; var baseRate = options.rate || 0.3; var dropout = options.dropout || 0; var momentum = options.momentum || 0; var batchSize = options.batchSize || 1; // online learning var ratePolicy = options.ratePolicy || methods.rate.FIXED(); var start = Date.now(); if (batchSize > set.length) { throw new Error('Batch size must be smaller or equal to dataset length!'); } else if (typeof options.iterations === 'undefined' && typeof options.error === 'undefined') { throw new Error('At least one of the following options must be specified: error, iterations'); } else if (typeof options.error === 'undefined') { targetError = -1; // run until iterations } else if (typeof options.iterations === 'undefined') { options.iterations = 0; // run until target error } // Save to network this.dropout = dropout; if (options.crossValidate) { let numTrain = Math.ceil((1 - options.crossValidate.testSize) * set.length); var trainSet = set.slice(0, numTrain); var testSet = set.slice(numTrain); } // Loops the training process var currentRate = baseRate; var iteration = 0; var error = 1; var i, j, x; while (error > targetError && (options.iterations === 0 || iteration < options.iterations)) { if (options.crossValidate && error <= options.crossValidate.testError) break; iteration++; // Update the rate currentRate = ratePolicy(baseRate, iteration); // Checks if cross validation is enabled if (options.crossValidate) { this._trainSet(trainSet, batchSize, currentRate, momentum, cost); if (options.clear) this.clear(); error = this.test(testSet, cost).error; if (options.clear) this.clear(); } else { error = this._trainSet(set, batchSize, currentRate, momentum, cost); if (options.clear) this.clear(); } // Checks for options such as scheduled logs and shuffling if (options.shuffle) { for (j, x, i = set.length; i; j = Math.floor(Math.random() * i), x = set[--i], set[i] = set[j], set[j] = x); } if (options.log && iteration % options.log === 0) { console.log('iteration', iteration, 'error', error, 'rate', currentRate); } if (options.schedule && iteration % options.schedule.iterations === 0) { options.schedule.function({ error: error, iteration: iteration }); } } if (options.clear) this.clear(); if (dropout) { for (i = 0; i < this.nodes.length; i++) { if (this.nodes[i].type === 'hidden' || this.nodes[i].type === 'constant') { this.nodes[i].mask = 1 - this.dropout; } } } return { error: error, iterations: iteration, time: Date.now() - start }; }, /** * Performs one training epoch and returns the error * private function used in this.train */ _trainSet: function (set, batchSize, currentRate, momentum, costFunction) { var errorSum = 0; for (var i = 0; i < set.length; i++) { var input = set[i].input; var target = set[i].output; var update = !!((i + 1) % batchSize === 0 || (i + 1) === set.length); var output = this.activate(input, true); this.propagate(currentRate, momentum, update, target); errorSum += costFunction(target, output); } return errorSum / set.length; }, /** * Tests a set and returns the error and elapsed time */ test: function (set, cost = methods.cost.MSE) { // Check if dropout is enabled, set correct mask var i; if (this.dropout) { for (i = 0; i < this.nodes.length; i++) { if (this.nodes[i].type === 'hidden' || this.nodes[i].type === 'constant') { this.nodes[i].mask = 1 - this.dropout; } } } var error = 0; var start = Date.now(); for (i = 0; i < set.length; i++) { let input = set[i].input; let target = set[i].output; let output = this.noTraceActivate(input); error += cost(target, output); } error /= set.length; var results = { error: error, time: Date.now() - start }; return results; }, /** * Creates a json that can be used to create a graph with d3 and webcola */ graph: function (width, height) { var input = 0; var output = 0; var json = { nodes: [], links: [], constraints: [{ type: 'alignment', axis: 'x', offsets: [] }, { type: 'alignment', axis: 'y', offsets: [] }] }; var i; for (i = 0; i < this.nodes.length; i++) { var node = this.nodes[i]; if (node.type === 'input') { if (this.input === 1) { json.constraints[0].offsets.push({ node: i, offset: 0 }); } else { json.constraints[0].offsets.push({ node: i, offset: 0.8 * width / (this.input - 1) * input++ }); } json.constraints[1].offsets.push({ node: i, offset: 0 }); } else if (node.type === 'output') { if (this.output === 1) { json.constraints[0].offsets.push({ node: i, offset: 0 }); } else { json.constraints[0].offsets.push({ node: i, offset: 0.8 * width / (this.output - 1) * output++ }); } json.constraints[1].offsets.push({ node: i, offset: -0.8 * height }); } json.nodes.push({ id: i, name: node.type === 'hidden' ? node.squash.name : node.type.toUpperCase(), activation: node.activation, bias: node.bias }); } var connections = this.connections.concat(this.selfconns); for (i = 0; i < connections.length; i++) { var connection = connections[i]; if (connection.gater == null) { json.links.push({ source: this.nodes.indexOf(connection.from), target: this.nodes.indexOf(connection.to), weight: connection.weight }); } else { // Add a gater 'node' var index = json.nodes.length; json.nodes.push({ id: index, activation: connection.gater.activation, name: 'GATE' }); json.links.push({ source: this.nodes.indexOf(connection.from), target: index, weight: 1 / 2 * connection.weight }); json.links.push({ source: index, target: this.nodes.indexOf(connection.to), weight: 1 / 2 * connection.weight }); json.links.push({ source: this.nodes.indexOf(connection.gater), target: index, weight: connection.gater.activation, gate: true }); } } return json; }, /** * Convert the network to a json object */ toJSON: function () { var json = { nodes: [], connections: [], input: this.input, output: this.output, dropout: this.dropout }; // So we don't have to use expensive .indexOf() var i; for (i = 0; i < this.nodes.length; i++) { this.nodes[i].index = i; } for (i = 0; i < this.nodes.length; i++) { let node = this.nodes[i]; let tojson = node.toJSON(); tojson.index = i; json.nodes.push(tojson); if (node.connections.self.weight !== 0) { let tojson = node.connections.self.toJSON(); tojson.from = i; tojson.to = i; tojson.gater = node.connections.self.gater != null ? node.connections.self.gater.index : null; json.connections.push(tojson); } } for (i = 0; i < this.connections.length; i++) { let conn = this.connections[i]; let tojson = conn.toJSON(); tojson.from = conn.from.index; tojson.to = conn.to.index; tojson.gater = conn.gater != null ? conn.gater.index : null; json.connections.push(tojson); } return json; }, /** * Sets the value of a property for every node in this network */ set: function (values) { for (var i = 0; i < this.nodes.length; i++) { this.nodes[i].bias = values.bias || this.nodes[i].bias; this.nodes[i].squash = values.squash || this.nodes[i].squash; } }, /** * Evolves the network to reach a lower error on a dataset */ evolve: async function (set, options) { if (set[0].input.length !== this.input || set[0].output.length !== this.output) { throw new Error('Dataset input/output size should be same as network input/output size!'); } // Read the options options = options || {}; var targetError = typeof options.error !== 'undefined' ? options.error : 0.05; var growth = typeof options.growth !== 'undefined' ? options.growth : 0.0001; var cost = options.cost || methods.cost.MSE; var amount = options.amount || 1; var threads = options.threads; if (typeof threads === 'undefined') { if (typeof window === 'undefined') { // Node.js threads = __webpack_require__(25).cpus().length; } else { // Browser threads = navigator.hardwareConcurrency; } } var start = Date.now(); if (typeof options.iterations === 'undefined' && typeof options.error === 'undefined') { throw new Error('At least one of the following options must be specified: error, iterations'); } else if (typeof options.error === 'undefined') { targetError = -1; // run until iterations } else if (typeof options.iterations === 'undefined') { options.iterations = 0; // run until target error } var fitnessFunction; if (threads === 1) { // Create the fitness function fitnessFunction = function (genome) { var score = 0; for (var i = 0; i < amount; i++) { score -= genome.test(set, cost).error; } score -= (genome.nodes.length - genome.input - genome.output + genome.connections.length + genome.gates.length) * growth; score = isNaN(score) ? -Infinity : score; // this can cause problems with fitness proportionate selection return score / amount; }; } else { // Serialize the dataset var converted = multi.serializeDataSet(set); // Create workers, send datasets var workers = []; if (typeof window === 'undefined') { for (var i = 0; i < threads; i++) { workers.push(new multi.workers.node.TestWorker(converted, cost)); } } else { for (var i = 0; i < threads; i++) { workers.push(new multi.workers.browser.TestWorker(converted, cost)); } } fitnessFunction = function (population) { return new Promise((resolve, reject) => { // Create a queue var queue = population.slice(); var done = 0; // Start worker function var startWorker = function (worker) { if (!queue.length) { if (++done === threads) resolve(); return; } var genome = queue.shift(); worker.evaluate(genome).then(function (result) { genome.score = -result; genome.score -= (genome.nodes.length - genome.input - genome.output + genome.connections.length + genome.gates.length) * growth; genome.score = isNaN(parseFloat(result)) ? -Infinity : genome.score; startWorker(worker); }); }; for (var i = 0; i < workers.length; i++) { startWorker(workers[i]); } }); }; options.fitnessPopulation = true; } // Intialise the NEAT instance options.network = this; var neat = new Neat(this.input, this.output, fitnessFunction, options); var error = -Infinity; var bestFitness = -Infinity; var bestGenome; while (error < -targetError && (options.iterations === 0 || neat.generation < options.iterations)) { let fittest = await neat.evolve(); let fitness = fittest.score; error = fitness + (fittest.nodes.length - fittest.input - fittest.output + fittest.connections.length + fittest.gates.length) * growth; if (fitness > bestFitness) { bestFitness = fitness; bestGeno