diff --git a/browser.js b/browser.js index 79b98b0d6..a9aa2ba61 100644 --- a/browser.js +++ b/browser.js @@ -424,10 +424,6 @@ var _gpu = require('gpu.js'); var _gpu2 = _interopRequireDefault(_gpu); -var _thaw = require('thaw.js'); - -var _thaw2 = _interopRequireDefault(_thaw); - function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } @@ -503,9 +499,6 @@ var NeuralNetworkGPU = function (_NeuralNetwork) { return null; } } - }, { - key: 'train', - value: function train() {} }, { key: 'trainAsync', value: function trainAsync(data, options) { @@ -521,14 +514,16 @@ var NeuralNetworkGPU = function (_NeuralNetwork) { endTime = _prepTraining.endTime; - var train = function train() { - if (_this2._trainingTick(data, status, endTime)) { - options.done(); - } else { - requestAnimationFrame(train); - } - }; - train(); + return new Promise(function (resolve, reject) { + var train = function train() { + if (_this2._trainingTick(data, status, endTime)) { + requestAnimationFrame(train); + } else { + resolve(status); + } + }; + train(); + }); } }, { key: 'buildRunInput', @@ -560,7 +555,8 @@ var NeuralNetworkGPU = function (_NeuralNetwork) { hardcodeConstants: true, constants: { size: this.sizes[layer - 1] - } + }, + floatTextures: true }); } } @@ -617,7 +613,8 @@ var NeuralNetworkGPU = function (_NeuralNetwork) { output: [this.sizes[layer]], outputToTexture: true, outputImmutable: true, - hardcodeConstants: true + hardcodeConstants: true, + floatTextures: true }); } else { this.backwardPropagate[layer] = this.gpu.createKernelMap({ @@ -633,7 +630,8 @@ var NeuralNetworkGPU = function (_NeuralNetwork) { hardcodeConstants: true, constants: { size: this.deltas[layer + 1].length - } + }, + floatTextures: true }); } } @@ -673,7 +671,8 @@ var NeuralNetworkGPU = function (_NeuralNetwork) { size: this.outputs[layer - 1].length, learningRate: this.trainOpts.learningRate, momentum: this.trainOpts.momentum - } + }, + floatTextures: true }); } } @@ -697,7 +696,8 @@ var NeuralNetworkGPU = function (_NeuralNetwork) { hardcodeConstants: true, constants: { learningRate: this.trainOpts.learningRate - } + }, + floatTextures: true }); } } @@ -715,7 +715,8 @@ var NeuralNetworkGPU = function (_NeuralNetwork) { output: [1], constants: { size: this.sizes[this.outputLayer] - } + }, + floatTextures: true }); } @@ -912,7 +913,7 @@ function mse(errors) { return sum / this.constants.size; } -},{"./lookup":3,"./neural-network":5,"gpu.js":75,"thaw.js":100}],5:[function(require,module,exports){ +},{"./lookup":3,"./neural-network":5,"gpu.js":75}],5:[function(require,module,exports){ 'use strict'; Object.defineProperty(exports, "__esModule", { @@ -1349,14 +1350,14 @@ var NeuralNetwork = function () { status.iterations++; if (this.trainOpts.log && status.iterations % this.trainOpts.logPeriod === 0) { - status.error = this._calculateTrainingError(data); + // status.error = this._calculateTrainingError(data); this.trainOpts.log('iterations: ' + status.iterations + ', training error: ' + status.error); } else { - if (status.iterations % 100 === 0) { - status.error = this._calculateTrainingError(data); - } else { - this._trainPatterns(data); - } + // if (status.iterations % 100 === 0) { + // status.error = this._calculateTrainingError(data); + // } else { + this._trainPatterns(data); + // } } if (this.trainOpts.callback && status.iterations % this.trainOpts.callbackPeriod === 0) { diff --git a/browser.min.js b/browser.min.js index f9cfbe19b..44a47c7a0 100644 --- a/browser.min.js +++ b/browser.min.js @@ -113,10 +113,10 @@ "use strict";function _classCallCheck(r,e){if(!(r instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(exports,"__esModule",{value:!0});var _createClass=function(){function r(r,e){for(var n=0;n0;)e[r[o]]=n++;return e}}]),r}();exports.default=lookup; },{}],4:[function(require,module,exports){ -"use strict";function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _possibleConstructorReturn(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function _inherits(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function weightedSumSigmoid(t,e,a){for(var i=e[this.thread.x],r=0;r0?t:0}function calcDeltasLeakyRelu(t,e){return e>0?t:.01*t}function calcDeltasTanh(t,e){return(1-e*e)*t}function calcError(t,e){for(var a=0,i=0;i0&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,e);var a=_possibleConstructorReturn(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return a.forwardPropagate=[],a.backwardPropagate=[],a.changesPropagate=[],a.biasesPropagate=[],a.gpu=new _gpu2.default({mode:t.mode}),a}return _inherits(e,t),_createClass(e,[{key:"_initialize",value:function(){_get(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"_initialize",this).call(this),this.buildRunInput(),this.buildCalculateDeltas(),this.buildGetChanges(),this.buildChangeBiases(),this.buildGetMSE()}},{key:"setActivation",value:function(){}},{key:"_trainPattern",value:function(t,e,a){return this.runInput(t),this.calculateDeltas(e),this.getChanges(),this.changeBiases(),a?this.getMSE(this.errors[this.outputLayer])[0]:null}},{key:"train",value:function(){}},{key:"trainAsync",value:function(t,e){var a=this,i=void 0,r=void 0,u=this._prepTraining(t,e);t=u.data,i=u.status,r=u.endTime;!function u(){a._trainingTick(t,i,r)?e.done():requestAnimationFrame(u)}()}},{key:"buildRunInput",value:function(){var t=null;switch(this.activation){case"sigmoid":t=weightedSumSigmoid;break;case"relu":t=weightedSumRelu;break;case"leaky-relu":t=weightedSumLeakyRelu;break;case"tanh":t=weightedSumTanh;break;default:throw new Error("unknown activation "+this.activation)}for(var e=1;e<=this.outputLayer;e++)this.forwardPropagate[e]=this.gpu.createKernel(t,{output:[this.sizes[e]],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0,constants:{size:this.sizes[e-1]}})}},{key:"runInput",value:function(t){var e=void 0;this.outputs[0]=t;for(var a=1;a<=this.outputLayer;a++)this.outputs[a]=this.forwardPropagate[a](this.weights[a],this.biases[a],t),e=t=this.outputs[a];return e}},{key:"buildCalculateDeltas",value:function(){var t=null;switch(this.activation){case"sigmoid":t=calcDeltasSigmoid;break;case"relu":t=calcDeltasRelu;break;case"leaky-relu":t=calcDeltasLeakyRelu;break;case"tanh":t=calcDeltasTanh;break;default:throw new Error("unknown activation "+this.activation)}for(var e=this.outputLayer;e>0;e--)e===this.outputLayer?this.backwardPropagate[e]=this.gpu.createKernelMap({error:_gpu2.default.alias("calcErrorOutput",calcErrorOutput),deltas:_gpu2.default.alias("calcDeltas",t)},function(e,a){var i=e[this.thread.x];return t(calcErrorOutput(i,a),i)},{output:[this.sizes[e]],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0}):this.backwardPropagate[e]=this.gpu.createKernelMap({error:_gpu2.default.alias("calcError",calcError),deltas:_gpu2.default.alias("calcDeltas",t)},function(e,a,i){var r=a[this.thread.x];return t(calcError(e,i),r)},{output:[this.sizes[e]],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0,constants:{size:this.deltas[e+1].length}})}},{key:"calculateDeltas",value:function(t){for(var e=this.outputLayer;e>0;e--){var a=void 0;a=e===this.outputLayer?this.backwardPropagate[e](this.outputs[e],t):this.backwardPropagate[e](this.weights[e+1],this.outputs[e],this.deltas[e+1]),this.deltas[e]=a.deltas,this.errors[e]=a.error}}},{key:"buildGetChanges",value:function(){for(var t=1;t<=this.outputLayer;t++)this.changesPropagate[t]=this.gpu.createKernelMap({weights:_gpu2.default.alias("addWeights",addWeights),changes:_gpu2.default.alias("calcChanges",calcChanges)},function(t,e,a,i){return addWeights(calcChanges(i,e,t),a)},{output:[this.sizes[t-1],this.sizes[t]],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0,constants:{size:this.outputs[t-1].length,learningRate:this.trainOpts.learningRate,momentum:this.trainOpts.momentum}})}},{key:"getChanges",value:function(){for(var t=1;t<=this.outputLayer;t++){var e=this.changesPropagate[t](this.outputs[t-1],this.deltas[t],this.weights[t],this.changes[t]);this.changes[t]=e.changes,this.weights[t]=e.weights}}},{key:"buildChangeBiases",value:function(){for(var t=1;t<=this.outputLayer;t++)this.biasesPropagate[t]=this.gpu.createKernel(addBiases,{output:[this.sizes[t]],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0,constants:{learningRate:this.trainOpts.learningRate}})}},{key:"changeBiases",value:function(){for(var t=1;t<=this.outputLayer;t++)this.biases[t]=this.biasesPropagate[t](this.biases[t],this.deltas[t])}},{key:"buildGetMSE",value:function(){this.getMSE=this.gpu.createKernel(mse,{output:[1],constants:{size:this.sizes[this.outputLayer]}})}},{key:"run",value:function(t){if(!this.isRunnable)return null;this.inputLookup&&(t=_lookup2.default.toArray(this.inputLookup,t));var e=this._texturizeInputData(t),a=this.runInput(e),i=a.toArray(this.gpu);return this.outputLookup&&(i=_lookup2.default.toHash(this.outputLookup,i)),i}},{key:"_verifyIsInitialized",value:function(t){var e=this;this.sizes||(this.sizes=[],t[0].size||(t[0].size={input:t[0].input.length,output:t[0].output.length}),this.sizes.push(t[0].size.input),this.hiddenSizes?this.hiddenSizes.forEach(function(t){e.sizes.push(t)}):this.sizes.push(Math.max(3,Math.floor(t[0].size.input/2))),this.sizes.push(t[0].size.output),this._initialize())}},{key:"_formatData",value:function(t){var a=this;t=_get(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"_formatData",this).call(this,t),this._texturizeInputData=this.gpu.createKernel(function(t){return t[this.thread.x]},{output:[t[0].input.length],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0});var i=this.gpu.createKernel(function(t){return t[this.thread.x]},{output:[t[0].output.length],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0});return t.forEach(function(t){t.size={input:t.input.length,output:t.output.length},t.input=a._texturizeInputData(t.input),t.output=i(t.output)}),t}},{key:"toFunction",value:function(){throw new Error("not implemented on NeuralNetworkGPU")}}]),e}(_neuralNetwork2.default);exports.default=NeuralNetworkGPU; +"use strict";function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _possibleConstructorReturn(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function _inherits(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}function weightedSumSigmoid(t,e,a){for(var r=e[this.thread.x],i=0;i0?t:0}function calcDeltasLeakyRelu(t,e){return e>0?t:.01*t}function calcDeltasTanh(t,e){return(1-e*e)*t}function calcError(t,e){for(var a=0,r=0;r0&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,e);var a=_possibleConstructorReturn(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return a.forwardPropagate=[],a.backwardPropagate=[],a.changesPropagate=[],a.biasesPropagate=[],a.gpu=new _gpu2.default({mode:t.mode}),a}return _inherits(e,t),_createClass(e,[{key:"_initialize",value:function(){_get(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"_initialize",this).call(this),this.buildRunInput(),this.buildCalculateDeltas(),this.buildGetChanges(),this.buildChangeBiases(),this.buildGetMSE()}},{key:"setActivation",value:function(){}},{key:"_trainPattern",value:function(t,e,a){return this.runInput(t),this.calculateDeltas(e),this.getChanges(),this.changeBiases(),a?this.getMSE(this.errors[this.outputLayer])[0]:null}},{key:"trainAsync",value:function(t,e){var a=this,r=void 0,i=void 0,u=this._prepTraining(t,e);return t=u.data,r=u.status,i=u.endTime,new Promise(function(e,u){!function u(){a._trainingTick(t,r,i)?requestAnimationFrame(u):e(r)}()})}},{key:"buildRunInput",value:function(){var t=null;switch(this.activation){case"sigmoid":t=weightedSumSigmoid;break;case"relu":t=weightedSumRelu;break;case"leaky-relu":t=weightedSumLeakyRelu;break;case"tanh":t=weightedSumTanh;break;default:throw new Error("unknown activation "+this.activation)}for(var e=1;e<=this.outputLayer;e++)this.forwardPropagate[e]=this.gpu.createKernel(t,{output:[this.sizes[e]],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0,constants:{size:this.sizes[e-1]},floatTextures:!0})}},{key:"runInput",value:function(t){var e=void 0;this.outputs[0]=t;for(var a=1;a<=this.outputLayer;a++)this.outputs[a]=this.forwardPropagate[a](this.weights[a],this.biases[a],t),e=t=this.outputs[a];return e}},{key:"buildCalculateDeltas",value:function(){var t=null;switch(this.activation){case"sigmoid":t=calcDeltasSigmoid;break;case"relu":t=calcDeltasRelu;break;case"leaky-relu":t=calcDeltasLeakyRelu;break;case"tanh":t=calcDeltasTanh;break;default:throw new Error("unknown activation "+this.activation)}for(var e=this.outputLayer;e>0;e--)e===this.outputLayer?this.backwardPropagate[e]=this.gpu.createKernelMap({error:_gpu2.default.alias("calcErrorOutput",calcErrorOutput),deltas:_gpu2.default.alias("calcDeltas",t)},function(e,a){var r=e[this.thread.x];return t(calcErrorOutput(r,a),r)},{output:[this.sizes[e]],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0,floatTextures:!0}):this.backwardPropagate[e]=this.gpu.createKernelMap({error:_gpu2.default.alias("calcError",calcError),deltas:_gpu2.default.alias("calcDeltas",t)},function(e,a,r){var i=a[this.thread.x];return t(calcError(e,r),i)},{output:[this.sizes[e]],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0,constants:{size:this.deltas[e+1].length},floatTextures:!0})}},{key:"calculateDeltas",value:function(t){for(var e=this.outputLayer;e>0;e--){var a=void 0;a=e===this.outputLayer?this.backwardPropagate[e](this.outputs[e],t):this.backwardPropagate[e](this.weights[e+1],this.outputs[e],this.deltas[e+1]),this.deltas[e]=a.deltas,this.errors[e]=a.error}}},{key:"buildGetChanges",value:function(){for(var t=1;t<=this.outputLayer;t++)this.changesPropagate[t]=this.gpu.createKernelMap({weights:_gpu2.default.alias("addWeights",addWeights),changes:_gpu2.default.alias("calcChanges",calcChanges)},function(t,e,a,r){return addWeights(calcChanges(r,e,t),a)},{output:[this.sizes[t-1],this.sizes[t]],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0,constants:{size:this.outputs[t-1].length,learningRate:this.trainOpts.learningRate,momentum:this.trainOpts.momentum},floatTextures:!0})}},{key:"getChanges",value:function(){for(var t=1;t<=this.outputLayer;t++){var e=this.changesPropagate[t](this.outputs[t-1],this.deltas[t],this.weights[t],this.changes[t]);this.changes[t]=e.changes,this.weights[t]=e.weights}}},{key:"buildChangeBiases",value:function(){for(var t=1;t<=this.outputLayer;t++)this.biasesPropagate[t]=this.gpu.createKernel(addBiases,{output:[this.sizes[t]],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0,constants:{learningRate:this.trainOpts.learningRate},floatTextures:!0})}},{key:"changeBiases",value:function(){for(var t=1;t<=this.outputLayer;t++)this.biases[t]=this.biasesPropagate[t](this.biases[t],this.deltas[t])}},{key:"buildGetMSE",value:function(){this.getMSE=this.gpu.createKernel(mse,{output:[1],constants:{size:this.sizes[this.outputLayer]},floatTextures:!0})}},{key:"run",value:function(t){if(!this.isRunnable)return null;this.inputLookup&&(t=_lookup2.default.toArray(this.inputLookup,t));var e=this._texturizeInputData(t),a=this.runInput(e),r=a.toArray(this.gpu);return this.outputLookup&&(r=_lookup2.default.toHash(this.outputLookup,r)),r}},{key:"_verifyIsInitialized",value:function(t){var e=this;this.sizes||(this.sizes=[],t[0].size||(t[0].size={input:t[0].input.length,output:t[0].output.length}),this.sizes.push(t[0].size.input),this.hiddenSizes?this.hiddenSizes.forEach(function(t){e.sizes.push(t)}):this.sizes.push(Math.max(3,Math.floor(t[0].size.input/2))),this.sizes.push(t[0].size.output),this._initialize())}},{key:"_formatData",value:function(t){var a=this;t=_get(e.prototype.__proto__||Object.getPrototypeOf(e.prototype),"_formatData",this).call(this,t),this._texturizeInputData=this.gpu.createKernel(function(t){return t[this.thread.x]},{output:[t[0].input.length],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0});var r=this.gpu.createKernel(function(t){return t[this.thread.x]},{output:[t[0].output.length],outputToTexture:!0,outputImmutable:!0,hardcodeConstants:!0});return t.forEach(function(t){t.size={input:t.input.length,output:t.output.length},t.input=a._texturizeInputData(t.input),t.output=r(t.output)}),t}},{key:"toFunction",value:function(){throw new Error("not implemented on NeuralNetworkGPU")}}]),e}(_neuralNetwork2.default);exports.default=NeuralNetworkGPU; -},{"./lookup":3,"./neural-network":5,"gpu.js":75,"thaw.js":100}],5:[function(require,module,exports){ -"use strict";function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}function _toConsumableArray(t){if(Array.isArray(t)){for(var i=0,e=Array(t.length);i0&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,t),Object.assign(this,this.constructor.defaults,i),this.hiddenSizes=i.hiddenLayers,this.trainOpts={},this._updateTrainingOptions(Object.assign({},this.constructor.trainDefaults,i)),this.sizes=null,this.outputLayer=null,this.biases=null,this.weights=null,this.outputs=null,this.deltas=null,this.changes=null,this.errors=null,this.constructor.prototype.hasOwnProperty("runInput")||(this.runInput=null),this.constructor.prototype.hasOwnProperty("calculateDeltas")||(this.calculateDeltas=null)}return _createClass(t,null,[{key:"trainDefaults",get:function(){return{iterations:2e4,errorThresh:.005,log:!1,logPeriod:10,learningRate:.3,momentum:.1,callback:null,callbackPeriod:10,timeout:1/0}}},{key:"defaults",get:function(){return{binaryThresh:.5,hiddenLayers:[3],activation:"sigmoid"}}}]),_createClass(t,[{key:"_initialize",value:function(){if(!this.sizes)throw new Error("Sizes must be set before initializing");this.outputLayer=this.sizes.length-1,this.biases=[],this.weights=[],this.outputs=[],this.deltas=[],this.changes=[],this.errors=[];for(var t=0;t<=this.outputLayer;t++){var i=this.sizes[t];if(this.deltas[t]=(0,_zeros2.default)(i),this.errors[t]=(0,_zeros2.default)(i),this.outputs[t]=(0,_zeros2.default)(i),t>0){this.biases[t]=(0,_randos2.default)(i),this.weights[t]=new Array(i),this.changes[t]=new Array(i);for(var e=0;e=this.trainOpts.iterations||i.error<=this.trainOpts.errorThresh||Date.now()>=e)&&(i.iterations++,this.trainOpts.log&&i.iterations%this.trainOpts.logPeriod==0?(i.error=this._calculateTrainingError(t),this.trainOpts.log("iterations: "+i.iterations+", training error: "+i.error)):i.iterations%100==0?i.error=this._calculateTrainingError(t):this._trainPatterns(t),this.trainOpts.callback&&i.iterations%this.trainOpts.callbackPeriod==0&&this.trainOpts.callback(Object.assign(i)),!0)}},{key:"_prepTraining",value:function(t,i){this._updateTrainingOptions(i),t=this._formatData(t);var e=Date.now()+this.trainOpts.timeout,r={error:1,iterations:0};return this._verifyIsInitialized(t),{data:t,status:r,endTime:e}}},{key:"train",value:function(t){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},e=void 0,r=void 0,s=this._prepTraining(t,i);for(t=s.data,e=s.status,r=s.endTime;this._trainingTick(t,e,r););return e}},{key:"trainAsync",value:function(t){var i=this,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=void 0,s=void 0,a=this._prepTraining(t,e);return t=a.data,r=a.status,s=a.endTime,new Promise(function(e,a){try{var u=new _thaw2.default(new Array(i.trainOpts.iterations),{delay:!0,each:function(){return i._trainingTick(t,r,s)||u.stop()},done:function(){return e(r)}});u.tick()}catch(t){a({trainError:t,status:r})}})}},{key:"_trainPattern",value:function(t,i,e){return this.runInput(t),this.calculateDeltas(i),this._adjustWeights(),e?(0,_mse2.default)(this.errors[this.outputLayer]):null}},{key:"_calculateDeltasSigmoid",value:function(t){for(var i=this.outputLayer;i>=0;i--)for(var e=0;e=0;i--)for(var e=0;e0?s:0}}},{key:"_calculateDeltasLeakyRelu",value:function(t){for(var i=this.outputLayer;i>=0;i--)for(var e=0;e0?s:.01*s}}},{key:"_calculateDeltasTanh",value:function(t){for(var i=this.outputLayer;i>=0;i--)for(var e=0;ei.binaryThresh?1:0,f=p[0]):(c=l.indexOf((0,_max2.default)(l)),f=p.indexOf((0,_max2.default)(p))),c!==f){var v=t[h];Object.assign(v,{actual:c,expected:f}),n.push(v)}e&&(0===c&&0===f?u++:1===c&&1===f?a++:0===c&&1===f?s++:1===c&&0===f&&r++);var g=l.map(function(t,i){return p[i]-t});o+=(0,_mse2.default)(g)}(h);var l=o/t.length,p={error:l,misclasses:n};return e&&Object.assign(p,{trueNeg:u,truePos:a,falseNeg:s,falsePos:r,total:t.length,precision:a/(a+r),recall:a/(a+s),accuracy:(u+a)/t.length}),p}},{key:"toJSON",value:function(){for(var t=[],i=0;i<=this.outputLayer;i++){t[i]={};var e=void 0;e=0===i&&this.inputLookup?Object.keys(this.inputLookup):i===this.outputLayer&&this.outputLookup?Object.keys(this.outputLookup):(0,_range2.default)(0,this.sizes[i]);for(var r=0;r0){t[i][s].bias=this.biases[i][r],t[i][s].weights={};for(var a in t[i-1]){var u=a;1===i&&this.inputLookup&&(u=this.inputLookup[a]),t[i][s].weights[a]=this.weights[i][r][u]}}}}return{sizes:this.sizes,layers:t,outputLookup:!!this.outputLookup,inputLookup:!!this.inputLookup,activation:this.activation,trainOpts:this._getTrainOptsJSON()}}},{key:"fromJSON",value:function(t){this.sizes=t.sizes,this._initialize();for(var i=0;i<=this.outputLayer;i++){var e=t.layers[i];if(0!==i||e[0]&&!t.inputLookup?i!==this.outputLayer||e[0]&&!t.outputLookup||(this.outputLookup=_lookup2.default.lookupFromHash(e)):this.inputLookup=_lookup2.default.lookupFromHash(e),i>0){var r=Object.keys(e);this.sizes[i]=r.length;for(var s in r){var a=r[s];this.biases[i][s]=e[a].bias,this.weights[i][s]=(0,_toArray2.default)(e[a].weights)}}}return this._updateTrainingOptions(t.trainOpts),this.setActivation(),this}},{key:"toFunction",value:function(){function t(e,r,s){if(0===r)return"string"==typeof s?"input['"+s+"']":"input["+s+"]";var a=e[r],u=a[s],n=[u.bias];for(var o in u.weights)u.weights[o]<0?n.push(u.weights[o]+"*("+t(e,r-1,o)+")"):n.push("+"+u.weights[o]+"*("+t(e,r-1,o)+")");switch(i){case"sigmoid":return"1/(1+1/Math.exp("+n.join("")+"))";case"relu":return"var sum = "+n.join("")+";(sum < 0 ? 0 : sum);";case"leaky-relu":return"var sum = "+n.join("")+";(sum < 0 ? 0 : 0.01 * sum);";case"tanh":return"Math.tanh("+n.join("")+");";default:throw new Error("unknown activation type "+i)}}var i=this.activation,e=this.toJSON().layers,r=[],s=void 0;for(var a in e[e.length-1])r.push(t(e,e.length-1,a));return s=this.outputLookup?"{"+Object.keys(this.outputLookup).map(function(t,i){return"'"+t+"':"+r[i]})+"}":"["+r.join(",")+"]",new Function("input","return "+s)}},{key:"createTrainStream",value:function(t){return t=t||{},t.neuralNetwork=this,this.setActivation(),this.trainStream=new _trainStream2.default(t),this.trainStream}},{key:"isRunnable",get:function(){var t=this;if(!this.runInput)return console.error("Activation function has not been initialized, did you run train()?"),!1;var i=["sizes","outputLayer","biases","weights","outputs","deltas","changes","errors"].filter(function(i){return null===t[i]});return!(i.length>0)||(console.error("Some settings have not been initialized correctly, did you run train()? Found issues with: "+i.join(", ")),!1)}}]),t}();exports.default=NeuralNetwork; +},{"./lookup":3,"./neural-network":5,"gpu.js":75}],5:[function(require,module,exports){ +"use strict";function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}function _toConsumableArray(t){if(Array.isArray(t)){for(var i=0,e=Array(t.length);i0&&void 0!==arguments[0]?arguments[0]:{};_classCallCheck(this,t),Object.assign(this,this.constructor.defaults,i),this.hiddenSizes=i.hiddenLayers,this.trainOpts={},this._updateTrainingOptions(Object.assign({},this.constructor.trainDefaults,i)),this.sizes=null,this.outputLayer=null,this.biases=null,this.weights=null,this.outputs=null,this.deltas=null,this.changes=null,this.errors=null,this.constructor.prototype.hasOwnProperty("runInput")||(this.runInput=null),this.constructor.prototype.hasOwnProperty("calculateDeltas")||(this.calculateDeltas=null)}return _createClass(t,null,[{key:"trainDefaults",get:function(){return{iterations:2e4,errorThresh:.005,log:!1,logPeriod:10,learningRate:.3,momentum:.1,callback:null,callbackPeriod:10,timeout:1/0}}},{key:"defaults",get:function(){return{binaryThresh:.5,hiddenLayers:[3],activation:"sigmoid"}}}]),_createClass(t,[{key:"_initialize",value:function(){if(!this.sizes)throw new Error("Sizes must be set before initializing");this.outputLayer=this.sizes.length-1,this.biases=[],this.weights=[],this.outputs=[],this.deltas=[],this.changes=[],this.errors=[];for(var t=0;t<=this.outputLayer;t++){var i=this.sizes[t];if(this.deltas[t]=(0,_zeros2.default)(i),this.errors[t]=(0,_zeros2.default)(i),this.outputs[t]=(0,_zeros2.default)(i),t>0){this.biases[t]=(0,_randos2.default)(i),this.weights[t]=new Array(i),this.changes[t]=new Array(i);for(var e=0;e=this.trainOpts.iterations||i.error<=this.trainOpts.errorThresh||Date.now()>=e)&&(i.iterations++,this.trainOpts.log&&i.iterations%this.trainOpts.logPeriod==0?this.trainOpts.log("iterations: "+i.iterations+", training error: "+i.error):this._trainPatterns(t),this.trainOpts.callback&&i.iterations%this.trainOpts.callbackPeriod==0&&this.trainOpts.callback(Object.assign(i)),!0)}},{key:"_prepTraining",value:function(t,i){this._updateTrainingOptions(i),t=this._formatData(t);var e=Date.now()+this.trainOpts.timeout,s={error:1,iterations:0};return this._verifyIsInitialized(t),{data:t,status:s,endTime:e}}},{key:"train",value:function(t){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},e=void 0,s=void 0,r=this._prepTraining(t,i);for(t=r.data,e=r.status,s=r.endTime;this._trainingTick(t,e,s););return e}},{key:"trainAsync",value:function(t){var i=this,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},s=void 0,r=void 0,a=this._prepTraining(t,e);return t=a.data,s=a.status,r=a.endTime,new Promise(function(e,a){try{var u=new _thaw2.default(new Array(i.trainOpts.iterations),{delay:!0,each:function(){return i._trainingTick(t,s,r)||u.stop()},done:function(){return e(s)}});u.tick()}catch(t){a({trainError:t,status:s})}})}},{key:"_trainPattern",value:function(t,i,e){return this.runInput(t),this.calculateDeltas(i),this._adjustWeights(),e?(0,_mse2.default)(this.errors[this.outputLayer]):null}},{key:"_calculateDeltasSigmoid",value:function(t){for(var i=this.outputLayer;i>=0;i--)for(var e=0;e=0;i--)for(var e=0;e0?r:0}}},{key:"_calculateDeltasLeakyRelu",value:function(t){for(var i=this.outputLayer;i>=0;i--)for(var e=0;e0?r:.01*r}}},{key:"_calculateDeltasTanh",value:function(t){for(var i=this.outputLayer;i>=0;i--)for(var e=0;ei.binaryThresh?1:0,f=p[0]):(c=l.indexOf((0,_max2.default)(l)),f=p.indexOf((0,_max2.default)(p))),c!==f){var v=t[h];Object.assign(v,{actual:c,expected:f}),n.push(v)}e&&(0===c&&0===f?u++:1===c&&1===f?a++:0===c&&1===f?r++:1===c&&0===f&&s++);var g=l.map(function(t,i){return p[i]-t});o+=(0,_mse2.default)(g)}(h);var l=o/t.length,p={error:l,misclasses:n};return e&&Object.assign(p,{trueNeg:u,truePos:a,falseNeg:r,falsePos:s,total:t.length,precision:a/(a+s),recall:a/(a+r),accuracy:(u+a)/t.length}),p}},{key:"toJSON",value:function(){for(var t=[],i=0;i<=this.outputLayer;i++){t[i]={};var e=void 0;e=0===i&&this.inputLookup?Object.keys(this.inputLookup):i===this.outputLayer&&this.outputLookup?Object.keys(this.outputLookup):(0,_range2.default)(0,this.sizes[i]);for(var s=0;s0){t[i][r].bias=this.biases[i][s],t[i][r].weights={};for(var a in t[i-1]){var u=a;1===i&&this.inputLookup&&(u=this.inputLookup[a]),t[i][r].weights[a]=this.weights[i][s][u]}}}}return{sizes:this.sizes,layers:t,outputLookup:!!this.outputLookup,inputLookup:!!this.inputLookup,activation:this.activation,trainOpts:this._getTrainOptsJSON()}}},{key:"fromJSON",value:function(t){this.sizes=t.sizes,this._initialize();for(var i=0;i<=this.outputLayer;i++){var e=t.layers[i];if(0!==i||e[0]&&!t.inputLookup?i!==this.outputLayer||e[0]&&!t.outputLookup||(this.outputLookup=_lookup2.default.lookupFromHash(e)):this.inputLookup=_lookup2.default.lookupFromHash(e),i>0){var s=Object.keys(e);this.sizes[i]=s.length;for(var r in s){var a=s[r];this.biases[i][r]=e[a].bias,this.weights[i][r]=(0,_toArray2.default)(e[a].weights)}}}return this._updateTrainingOptions(t.trainOpts),this.setActivation(),this}},{key:"toFunction",value:function(){function t(e,s,r){if(0===s)return"string"==typeof r?"input['"+r+"']":"input["+r+"]";var a=e[s],u=a[r],n=[u.bias];for(var o in u.weights)u.weights[o]<0?n.push(u.weights[o]+"*("+t(e,s-1,o)+")"):n.push("+"+u.weights[o]+"*("+t(e,s-1,o)+")");switch(i){case"sigmoid":return"1/(1+1/Math.exp("+n.join("")+"))";case"relu":return"var sum = "+n.join("")+";(sum < 0 ? 0 : sum);";case"leaky-relu":return"var sum = "+n.join("")+";(sum < 0 ? 0 : 0.01 * sum);";case"tanh":return"Math.tanh("+n.join("")+");";default:throw new Error("unknown activation type "+i)}}var i=this.activation,e=this.toJSON().layers,s=[],r=void 0;for(var a in e[e.length-1])s.push(t(e,e.length-1,a));return r=this.outputLookup?"{"+Object.keys(this.outputLookup).map(function(t,i){return"'"+t+"':"+s[i]})+"}":"["+s.join(",")+"]",new Function("input","return "+r)}},{key:"createTrainStream",value:function(t){return t=t||{},t.neuralNetwork=this,this.setActivation(),this.trainStream=new _trainStream2.default(t),this.trainStream}},{key:"isRunnable",get:function(){var t=this;if(!this.runInput)return console.error("Activation function has not been initialized, did you run train()?"),!1;var i=["sizes","outputLayer","biases","weights","outputs","deltas","changes","errors"].filter(function(i){return null===t[i]});return!(i.length>0)||(console.error("Some settings have not been initialized correctly, did you run train()? Found issues with: "+i.join(", ")),!1)}}]),t}();exports.default=NeuralNetwork; },{"./lookup":3,"./train-stream":33,"./utilities/max":35,"./utilities/mse":36,"./utilities/randos":40,"./utilities/range":41,"./utilities/to-array":42,"./utilities/zeros":43,"thaw.js":100}],6:[function(require,module,exports){ "use strict";function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _possibleConstructorReturn(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(exports,"__esModule",{value:!0});var _createClass=function(){function e(e,t){for(var r=0;rt)return!1;if((s+=e[i+1])>=t)return!0}}function s(t,s){return t<65?36===t:t<91||(t<97?95===t:t<123||(t<=65535?t>=170&&C.test(String.fromCharCode(t)):!1!==s&&e(t,_)))}function i(t,s){return t<48?36===t:t<58||!(t<65)&&(t<91||(t<97?95===t:t<123||(t<=65535?t>=170&&L.test(String.fromCharCode(t)):!1!==s&&(e(t,_)||e(t,P)))))}function r(t,e){return new T(t,{beforeExpr:!0,binop:e})}function n(t,e){return void 0===e&&(e={}),e.keyword=t,R[t]=new T(t,e)}function a(t){return 10===t||13===t||8232===t||8233===t}function o(t,e){return q.call(t,e)}function h(t,e){for(var s=1,i=0;;){D.lastIndex=i;var r=D.exec(t);if(!(r&&r.index=2015&&(e.ecmaVersion-=2009),null==e.allowReserved&&(e.allowReserved=e.ecmaVersion<5),z(e.onToken)){var i=e.onToken;e.onToken=function(t){return i.push(t)}}return z(e.onComment)&&(e.onComment=c(e,e.onComment)),e}function c(t,e){return function(s,i,r,n,a,o){var h={type:s?"Block":"Line",value:i,start:r,end:n};t.locations&&(h.loc=new W(this,a,o)),t.ranges&&(h.range=[r,n]),e.push(h)}}function l(t){return new RegExp("^(?:"+t.replace(/ /g,"|")+")$")}function u(){this.shorthandAssign=this.trailingComma=this.parenthesizedAssign=this.parenthesizedBind=this.doubleProto=-1}function d(t,e,s,i){return t.type=e,t.end=s,this.options.locations&&(t.loc.end=i),this.options.ranges&&(t.range[1]=s),t}function f(t,e,s,i){try{return new RegExp(t,e)}catch(t){if(void 0!==s)throw t instanceof SyntaxError&&i.raise(s,"Error parsing regular expression: "+t.message),t}}function m(t){return t<=65535?String.fromCharCode(t):(t-=65536,String.fromCharCode(55296+(t>>10),56320+(1023&t)))}function x(t,e){return new $(e,t).parse()}function y(t,e,s){var i=new $(s,t,e);return i.nextToken(),i.parseExpression()}function v(t,e){return new $(e,t)}function b(e,s,i){t.parse_dammit=e,t.LooseParser=s,t.pluginsLoose=i}var g={3:"abstract boolean byte char class double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile",5:"class enum extends super const export import",6:"enum",strict:"implements interface let package private protected public static yield",strictBind:"eval arguments"},k="break case catch continue debugger default do else finally for function if return switch throw try var while with null true false instanceof typeof void delete new in this",w={5:k,6:k+" const class extends export import super"},E=/^in(stanceof)?$/,A="ªµºÀ-ÖØ-öø-ˁˆ-ˑˠ-ˤˬˮͰ-ʹͶͷͺ-ͽͿΆΈ-ΊΌΎ-ΡΣ-ϵϷ-ҁҊ-ԯԱ-Ֆՙա-ևא-תװ-ײؠ-يٮٯٱ-ۓەۥۦۮۯۺ-ۼۿܐܒ-ܯݍ-ޥޱߊ-ߪߴߵߺࠀ-ࠕࠚࠤࠨࡀ-ࡘࢠ-ࢴࢶ-ࢽऄ-हऽॐक़-ॡॱ-ঀঅ-ঌএঐও-নপ-রলশ-হঽৎড়ঢ়য়-ৡৰৱਅ-ਊਏਐਓ-ਨਪ-ਰਲਲ਼ਵਸ਼ਸਹਖ਼-ੜਫ਼ੲ-ੴઅ-ઍએ-ઑઓ-નપ-રલળવ-હઽૐૠૡૹଅ-ଌଏଐଓ-ନପ-ରଲଳଵ-ହଽଡ଼ଢ଼ୟ-ୡୱஃஅ-ஊஎ-ஐஒ-கஙசஜஞடணதந-பம-ஹௐఅ-ఌఎ-ఐఒ-నప-హఽౘ-ౚౠౡಀಅ-ಌಎ-ಐಒ-ನಪ-ಳವ-ಹಽೞೠೡೱೲഅ-ഌഎ-ഐഒ-ഺഽൎൔ-ൖൟ-ൡൺ-ൿඅ-ඖක-නඳ-රලව-ෆก-ะาำเ-ๆກຂຄງຈຊຍດ-ທນ-ຟມ-ຣລວສຫອ-ະາຳຽເ-ໄໆໜ-ໟༀཀ-ཇཉ-ཬྈ-ྌက-ဪဿၐ-ၕၚ-ၝၡၥၦၮ-ၰၵ-ႁႎႠ-ჅჇჍა-ჺჼ-ቈቊ-ቍቐ-ቖቘቚ-ቝበ-ኈኊ-ኍነ-ኰኲ-ኵኸ-ኾዀዂ-ዅወ-ዖዘ-ጐጒ-ጕጘ-ፚᎀ-ᎏᎠ-Ᏽᏸ-ᏽᐁ-ᙬᙯ-ᙿᚁ-ᚚᚠ-ᛪᛮ-ᛸᜀ-ᜌᜎ-ᜑᜠ-ᜱᝀ-ᝑᝠ-ᝬᝮ-ᝰក-ឳៗៜᠠ-ᡷᢀ-ᢨᢪᢰ-ᣵᤀ-ᤞᥐ-ᥭᥰ-ᥴᦀ-ᦫᦰ-ᧉᨀ-ᨖᨠ-ᩔᪧᬅ-ᬳᭅ-ᭋᮃ-ᮠᮮᮯᮺ-ᯥᰀ-ᰣᱍ-ᱏᱚ-ᱽᲀ-ᲈᳩ-ᳬᳮ-ᳱᳵᳶᴀ-ᶿḀ-ἕἘ-Ἕἠ-ὅὈ-Ὅὐ-ὗὙὛὝὟ-ώᾀ-ᾴᾶ-ᾼιῂ-ῄῆ-ῌῐ-ΐῖ-Ίῠ-Ῥῲ-ῴῶ-ῼⁱⁿₐ-ₜℂℇℊ-ℓℕ℘-ℝℤΩℨK-ℹℼ-ℿⅅ-ⅉⅎⅠ-ↈⰀ-Ⱞⰰ-ⱞⱠ-ⳤⳫ-ⳮⳲⳳⴀ-ⴥⴧⴭⴰ-ⵧⵯⶀ-ⶖⶠ-ⶦⶨ-ⶮⶰ-ⶶⶸ-ⶾⷀ-ⷆⷈ-ⷎⷐ-ⷖⷘ-ⷞ々-〇〡-〩〱-〵〸-〼ぁ-ゖ゛-ゟァ-ヺー-ヿㄅ-ㄭㄱ-ㆎㆠ-ㆺㇰ-ㇿ㐀-䶵一-鿕ꀀ-ꒌꓐ-ꓽꔀ-ꘌꘐ-ꘟꘪꘫꙀ-ꙮꙿ-ꚝꚠ-ꛯꜗ-ꜟꜢ-ꞈꞋ-ꞮꞰ-ꞷꟷ-ꠁꠃ-ꠅꠇ-ꠊꠌ-ꠢꡀ-ꡳꢂ-ꢳꣲ-ꣷꣻꣽꤊ-ꤥꤰ-ꥆꥠ-ꥼꦄ-ꦲꧏꧠ-ꧤꧦ-ꧯꧺ-ꧾꨀ-ꨨꩀ-ꩂꩄ-ꩋꩠ-ꩶꩺꩾ-ꪯꪱꪵꪶꪹ-ꪽꫀꫂꫛ-ꫝꫠ-ꫪꫲ-ꫴꬁ-ꬆꬉ-ꬎꬑ-ꬖꬠ-ꬦꬨ-ꬮꬰ-ꭚꭜ-ꭥꭰ-ꯢ가-힣ힰ-ퟆퟋ-ퟻ豈-舘並-龎ff-stﬓ-ﬗיִײַ-ﬨשׁ-זּטּ-לּמּנּסּףּפּצּ-ﮱﯓ-ﴽﵐ-ﶏﶒ-ﷇﷰ-ﷻﹰ-ﹴﹶ-ﻼA-Za-zヲ-하-ᅦᅧ-ᅬᅭ-ᅲᅳ-ᅵ",S="‌‍·̀-ͯ·҃-֑҇-ׇֽֿׁׂׅׄؐ-ًؚ-٩ٰۖ-ۜ۟-۪ۤۧۨ-ۭ۰-۹ܑܰ-݊ަ-ް߀-߉߫-߳ࠖ-࠙ࠛ-ࠣࠥ-ࠧࠩ-࡙࠭-࡛ࣔ-ࣣ࣡-ःऺ-़ा-ॏ॑-ॗॢॣ०-९ঁ-ঃ়া-ৄেৈো-্ৗৢৣ০-৯ਁ-ਃ਼ਾ-ੂੇੈੋ-੍ੑ੦-ੱੵઁ-ઃ઼ા-ૅે-ૉો-્ૢૣ૦-૯ଁ-ଃ଼ା-ୄେୈୋ-୍ୖୗୢୣ୦-୯ஂா-ூெ-ைொ-்ௗ௦-௯ఀ-ఃా-ౄె-ైొ-్ౕౖౢౣ౦-౯ಁ-ಃ಼ಾ-ೄೆ-ೈೊ-್ೕೖೢೣ೦-೯ഁ-ഃാ-ൄെ-ൈൊ-്ൗൢൣ൦-൯ංඃ්ා-ුූෘ-ෟ෦-෯ෲෳัิ-ฺ็-๎๐-๙ັິ-ູົຼ່-ໍ໐-໙༘༙༠-༩༹༵༷༾༿ཱ-྄྆྇ྍ-ྗྙ-ྼ࿆ါ-ှ၀-၉ၖ-ၙၞ-ၠၢ-ၤၧ-ၭၱ-ၴႂ-ႍႏ-ႝ፝-፟፩-፱ᜒ-᜔ᜲ-᜴ᝒᝓᝲᝳ឴-៓៝០-៩᠋-᠍᠐-᠙ᢩᤠ-ᤫᤰ-᤻᥆-᥏᧐-᧚ᨗ-ᨛᩕ-ᩞ᩠-᩿᩼-᪉᪐-᪙᪰-᪽ᬀ-ᬄ᬴-᭄᭐-᭙᭫-᭳ᮀ-ᮂᮡ-ᮭ᮰-᮹᯦-᯳ᰤ-᰷᱀-᱉᱐-᱙᳐-᳔᳒-᳨᳭ᳲ-᳴᳸᳹᷀-᷵᷻-᷿‿⁀⁔⃐-⃥⃜⃡-⃰⳯-⵿⳱ⷠ-〪ⷿ-゙゚〯꘠-꘩꙯ꙴ-꙽ꚞꚟ꛰꛱ꠂ꠆ꠋꠣ-ꠧꢀꢁꢴ-ꣅ꣐-꣙꣠-꣱꤀-꤉ꤦ-꤭ꥇ-꥓ꦀ-ꦃ꦳-꧀꧐-꧙ꧥ꧰-꧹ꨩ-ꨶꩃꩌꩍ꩐-꩙ꩻ-ꩽꪰꪲ-ꪴꪷꪸꪾ꪿꫁ꫫ-ꫯꫵ꫶ꯣ-ꯪ꯬꯭꯰-꯹ﬞ︀-️︠-︯︳︴﹍-﹏0-9_",C=new RegExp("["+A+"]"),L=new RegExp("["+A+S+"]");A=S=null;var _=[0,11,2,25,2,18,2,1,2,14,3,13,35,122,70,52,268,28,4,48,48,31,17,26,6,37,11,29,3,35,5,7,2,4,43,157,19,35,5,35,5,39,9,51,157,310,10,21,11,7,153,5,3,0,2,43,2,1,4,0,3,22,11,22,10,30,66,18,2,1,11,21,11,25,71,55,7,1,65,0,16,3,2,2,2,26,45,28,4,28,36,7,2,27,28,53,11,21,11,18,14,17,111,72,56,50,14,50,785,52,76,44,33,24,27,35,42,34,4,0,13,47,15,3,22,0,2,0,36,17,2,24,85,6,2,0,2,3,2,14,2,9,8,46,39,7,3,1,3,21,2,6,2,1,2,4,4,0,19,0,13,4,159,52,19,3,54,47,21,1,2,0,185,46,42,3,37,47,21,0,60,42,86,25,391,63,32,0,449,56,264,8,2,36,18,0,50,29,881,921,103,110,18,195,2749,1070,4050,582,8634,568,8,30,114,29,19,47,17,3,32,20,6,18,881,68,12,0,67,12,65,0,32,6124,20,754,9486,1,3071,106,6,12,4,8,8,9,5991,84,2,70,2,1,3,0,3,1,3,3,2,11,2,0,2,6,2,64,2,3,3,7,2,6,2,27,2,3,2,4,2,0,4,6,2,339,3,24,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,30,2,24,2,7,4149,196,60,67,1213,3,2,26,2,1,2,0,3,0,2,9,2,3,2,0,2,0,7,0,5,0,2,0,2,0,2,2,2,1,2,0,3,0,2,0,2,0,2,0,2,0,2,1,2,0,3,3,2,6,2,3,2,3,2,0,2,9,2,16,6,2,2,4,2,16,4421,42710,42,4148,12,221,3,5761,10591,541],P=[509,0,227,0,150,4,294,9,1368,2,2,1,6,3,41,2,5,0,166,1,1306,2,54,14,32,9,16,3,46,10,54,9,7,2,37,13,2,9,52,0,13,2,49,13,10,2,4,9,83,11,7,0,161,11,6,9,7,3,57,0,2,6,3,1,3,2,10,0,11,1,3,6,4,4,193,17,10,9,87,19,13,9,214,6,3,8,28,1,83,16,16,9,82,12,9,9,84,14,5,9,423,9,838,7,2,7,17,9,57,21,2,13,19882,9,135,4,60,6,26,9,1016,45,17,3,19723,1,5319,4,4,5,9,7,3,6,31,3,149,2,1418,49,513,54,5,49,9,0,15,0,23,4,2,14,1361,6,2,16,3,6,2,1,2,4,2214,6,110,6,6,9,792487,239],T=function(t,e){void 0===e&&(e={}),this.label=t,this.keyword=e.keyword,this.beforeExpr=!!e.beforeExpr,this.startsExpr=!!e.startsExpr,this.isLoop=!!e.isLoop,this.isAssign=!!e.isAssign,this.prefix=!!e.prefix,this.postfix=!!e.postfix,this.binop=e.binop||null,this.updateContext=null},N={beforeExpr:!0},I={startsExpr:!0},R={},V={num:new T("num",I),regexp:new T("regexp",I),string:new T("string",I),name:new T("name",I),eof:new T("eof"),bracketL:new T("[",{beforeExpr:!0,startsExpr:!0}),bracketR:new T("]"),braceL:new T("{",{beforeExpr:!0,startsExpr:!0}),braceR:new T("}"),parenL:new T("(",{beforeExpr:!0,startsExpr:!0}),parenR:new T(")"),comma:new T(",",N),semi:new T(";",N),colon:new T(":",N),dot:new T("."),question:new T("?",N),arrow:new T("=>",N),template:new T("template"),invalidTemplate:new T("invalidTemplate"),ellipsis:new T("...",N),backQuote:new T("`",I),dollarBraceL:new T("${",{beforeExpr:!0,startsExpr:!0}),eq:new T("=",{beforeExpr:!0,isAssign:!0}),assign:new T("_=",{beforeExpr:!0,isAssign:!0}),incDec:new T("++/--",{prefix:!0,postfix:!0,startsExpr:!0}),prefix:new T("!/~",{beforeExpr:!0,prefix:!0,startsExpr:!0}),logicalOR:r("||",1),logicalAND:r("&&",2),bitwiseOR:r("|",3),bitwiseXOR:r("^",4),bitwiseAND:r("&",5),equality:r("==/!=/===/!==",6),relational:r("/<=/>=",7),bitShift:r("<>/>>>",8),plusMin:new T("+/-",{beforeExpr:!0,binop:9,prefix:!0,startsExpr:!0}),modulo:r("%",10),star:r("*",10),slash:r("/",10),starstar:new T("**",{beforeExpr:!0}),_break:n("break"),_case:n("case",N),_catch:n("catch"),_continue:n("continue"),_debugger:n("debugger"),_default:n("default",N),_do:n("do",{isLoop:!0,beforeExpr:!0}),_else:n("else",N),_finally:n("finally"),_for:n("for",{isLoop:!0}),_function:n("function",I),_if:n("if"),_return:n("return",N),_switch:n("switch"),_throw:n("throw",N),_try:n("try"),_var:n("var"),_const:n("const"),_while:n("while",{isLoop:!0}),_with:n("with"),_new:n("new",{beforeExpr:!0,startsExpr:!0}),_this:n("this",I),_super:n("super",I),_class:n("class",I),_extends:n("extends",N),_export:n("export"),_import:n("import"),_null:n("null",I),_true:n("true",I),_false:n("false",I),_in:n("in",{beforeExpr:!0,binop:7}),_instanceof:n("instanceof",{beforeExpr:!0,binop:7}),_typeof:n("typeof",{beforeExpr:!0,prefix:!0,startsExpr:!0}),_void:n("void",{beforeExpr:!0,prefix:!0,startsExpr:!0}),_delete:n("delete",{beforeExpr:!0,prefix:!0,startsExpr:!0})},F=/\r\n?|\n|\u2028|\u2029/,D=new RegExp(F.source,"g"),B=/[\u1680\u180e\u2000-\u200a\u202f\u205f\u3000\ufeff]/,O=/(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g,M=Object.prototype,q=M.hasOwnProperty,U=M.toString,z=Array.isArray||function(t){return"[object Array]"===U.call(t)},j=function(t,e){this.line=t,this.column=e};j.prototype.offset=function(t){return new j(this.line,this.column+t)};var W=function(t,e,s){this.start=e,this.end=s,null!==t.sourceFile&&(this.source=t.sourceFile)},G={ecmaVersion:7,sourceType:"script",onInsertedSemicolon:null,onTrailingComma:null,allowReserved:null,allowReturnOutsideFunction:!1,allowImportExportEverywhere:!1,allowHashBang:!1,locations:!1,onToken:null,onComment:null,ranges:!1,program:null,sourceFile:null,directSourceFile:null,preserveParens:!1,plugins:{}},Y={},$=function(t,e,s){this.options=t=p(t),this.sourceFile=t.sourceFile,this.keywords=l(w[t.ecmaVersion>=6?6:5]);var i="";if(!t.allowReserved){for(var r=t.ecmaVersion;!(i=g[r]);r--);"module"==t.sourceType&&(i+=" await")}this.reservedWords=l(i);var n=(i?i+" ":"")+g.strict;this.reservedWordsStrict=l(n),this.reservedWordsStrictBind=l(n+" "+g.strictBind),this.input=String(e),this.containsEsc=!1,this.loadPlugins(t.plugins),s?(this.pos=s,this.lineStart=this.input.lastIndexOf("\n",s-1)+1,this.curLine=this.input.slice(0,this.lineStart).split(F).length):(this.pos=this.lineStart=0,this.curLine=1),this.type=V.eof,this.value=null,this.start=this.end=this.pos,this.startLoc=this.endLoc=this.curPosition(),this.lastTokEndLoc=this.lastTokStartLoc=null,this.lastTokStart=this.lastTokEnd=this.pos,this.context=this.initialContext(),this.exprAllowed=!0,this.inModule="module"===t.sourceType,this.strict=this.inModule||this.strictDirective(this.pos),this.potentialArrowAt=-1,this.inFunction=this.inGenerator=this.inAsync=!1,this.yieldPos=this.awaitPos=0,this.labels=[],0===this.pos&&t.allowHashBang&&"#!"===this.input.slice(0,2)&&this.skipLineComment(2),this.scopeStack=[],this.enterFunctionScope()};$.prototype.isKeyword=function(t){return this.keywords.test(t)},$.prototype.isReservedWord=function(t){return this.reservedWords.test(t)},$.prototype.extend=function(t,e){this[t]=e(this[t])},$.prototype.loadPlugins=function(t){var e=this;for(var s in t){var i=Y[s];if(!i)throw new Error("Plugin '"+s+"' not found");i(e,t[s])}},$.prototype.parse=function(){var t=this.options.program||this.startNode();return this.nextToken(),this.parseTopLevel(t)};var Q=$.prototype,H=/^(?:'((?:\\.|[^'])*?)'|"((?:\\.|[^"])*?)"|;)/;Q.strictDirective=function(t){for(var e=this;;){O.lastIndex=t,t+=O.exec(e.input)[0].length;var s=H.exec(e.input.slice(t));if(!s)return!1;if("use strict"==(s[1]||s[2]))return!0;t+=s[0].length}},Q.eat=function(t){return this.type===t&&(this.next(),!0)},Q.isContextual=function(t){return this.type===V.name&&this.value===t&&!this.containsEsc},Q.eatContextual=function(t){return!!this.isContextual(t)&&(this.next(),!0)},Q.expectContextual=function(t){this.eatContextual(t)||this.unexpected()},Q.canInsertSemicolon=function(){return this.type===V.eof||this.type===V.braceR||F.test(this.input.slice(this.lastTokEnd,this.start))},Q.insertSemicolon=function(){if(this.canInsertSemicolon())return this.options.onInsertedSemicolon&&this.options.onInsertedSemicolon(this.lastTokEnd,this.lastTokEndLoc),!0},Q.semicolon=function(){this.eat(V.semi)||this.insertSemicolon()||this.unexpected()},Q.afterTrailingComma=function(t,e){if(this.type==t)return this.options.onTrailingComma&&this.options.onTrailingComma(this.lastTokStart,this.lastTokStartLoc),e||this.next(),!0},Q.expect=function(t){this.eat(t)||this.unexpected()},Q.unexpected=function(t){this.raise(null!=t?t:this.start,"Unexpected token")},Q.checkPatternErrors=function(t,e){if(t){t.trailingComma>-1&&this.raiseRecoverable(t.trailingComma,"Comma is not permitted after the rest element");var s=e?t.parenthesizedAssign:t.parenthesizedBind;s>-1&&this.raiseRecoverable(s,"Parenthesized pattern")}},Q.checkExpressionErrors=function(t,e){if(!t)return!1;var s=t.shorthandAssign,i=t.doubleProto;if(!e)return s>=0||i>=0;s>=0&&this.raise(s,"Shorthand property assignments are valid only in destructuring patterns"),i>=0&&this.raiseRecoverable(i,"Redefinition of __proto__ property")},Q.checkYieldAwaitInDefaultParams=function(){this.yieldPos&&(!this.awaitPos||this.yieldPos=6&&(t.sourceType=this.options.sourceType),this.finishNode(t,"Program")};var K={kind:"loop"},J={kind:"switch"};X.isLet=function(){if(this.options.ecmaVersion<6||!this.isContextual("let"))return!1;O.lastIndex=this.pos;var t=O.exec(this.input),e=this.pos+t[0].length,r=this.input.charCodeAt(e);if(91===r||123==r)return!0;if(s(r,!0)){for(var n=e+1;i(this.input.charCodeAt(n),!0);)++n;var a=this.input.slice(e,n);if(!E.test(a))return!0}return!1},X.isAsyncFunction=function(){if(this.options.ecmaVersion<8||!this.isContextual("async"))return!1;O.lastIndex=this.pos;var t=O.exec(this.input),e=this.pos+t[0].length;return!(F.test(this.input.slice(this.pos,e))||"function"!==this.input.slice(e,e+8)||e+8!=this.input.length&&i(this.input.charAt(e+8)))},X.parseStatement=function(t,e,s){var i,r=this.type,n=this.startNode();switch(this.isLet()&&(r=V._var,i="let"),r){case V._break:case V._continue:return this.parseBreakContinueStatement(n,r.keyword);case V._debugger:return this.parseDebuggerStatement(n);case V._do:return this.parseDoStatement(n);case V._for:return this.parseForStatement(n);case V._function:return!t&&this.options.ecmaVersion>=6&&this.unexpected(),this.parseFunctionStatement(n,!1);case V._class:return t||this.unexpected(),this.parseClass(n,!0);case V._if:return this.parseIfStatement(n);case V._return:return this.parseReturnStatement(n);case V._switch:return this.parseSwitchStatement(n);case V._throw:return this.parseThrowStatement(n);case V._try:return this.parseTryStatement(n);case V._const:case V._var:return i=i||this.value,t||"var"==i||this.unexpected(),this.parseVarStatement(n,i);case V._while:return this.parseWhileStatement(n);case V._with:return this.parseWithStatement(n);case V.braceL:return this.parseBlock();case V.semi:return this.parseEmptyStatement(n);case V._export:case V._import:return this.options.allowImportExportEverywhere||(e||this.raise(this.start,"'import' and 'export' may only appear at the top level"),this.inModule||this.raise(this.start,"'import' and 'export' may appear only with 'sourceType: module'")),r===V._import?this.parseImport(n):this.parseExport(n,s);default:if(this.isAsyncFunction())return t||this.unexpected(),this.next(),this.parseFunctionStatement(n,!0);var a=this.value,o=this.parseExpression();return r===V.name&&"Identifier"===o.type&&this.eat(V.colon)?this.parseLabeledStatement(n,a,o):this.parseExpressionStatement(n,o)}},X.parseBreakContinueStatement=function(t,e){var s=this,i="break"==e;this.next(),this.eat(V.semi)||this.insertSemicolon()?t.label=null:this.type!==V.name?this.unexpected():(t.label=this.parseIdent(),this.semicolon());for(var r=0;r=6?this.eat(V.semi):this.semicolon(),this.finishNode(t,"DoWhileStatement")},X.parseForStatement=function(t){if(this.next(),this.labels.push(K),this.enterLexicalScope(),this.expect(V.parenL),this.type===V.semi)return this.parseFor(t,null);var e=this.isLet();if(this.type===V._var||this.type===V._const||e){var s=this.startNode(),i=e?"let":this.value;return this.next(),this.parseVar(s,!0,i),this.finishNode(s,"VariableDeclaration"),!(this.type===V._in||this.options.ecmaVersion>=6&&this.isContextual("of"))||1!==s.declarations.length||"var"!==i&&s.declarations[0].init?this.parseFor(t,s):this.parseForIn(t,s)}var r=new u,n=this.parseExpression(!0,r);return this.type===V._in||this.options.ecmaVersion>=6&&this.isContextual("of")?(this.toAssignable(n,!1,r),this.checkLVal(n),this.parseForIn(t,n)):(this.checkExpressionErrors(r,!0),this.parseFor(t,n))},X.parseFunctionStatement=function(t,e){return this.next(),this.parseFunction(t,!0,!1,e)},X.parseIfStatement=function(t){return this.next(),t.test=this.parseParenExpression(),t.consequent=this.parseStatement(!this.strict&&this.type==V._function),t.alternate=this.eat(V._else)?this.parseStatement(!this.strict&&this.type==V._function):null,this.finishNode(t,"IfStatement")},X.parseReturnStatement=function(t){return this.inFunction||this.options.allowReturnOutsideFunction||this.raise(this.start,"'return' outside of function"),this.next(),this.eat(V.semi)||this.insertSemicolon()?t.argument=null:(t.argument=this.parseExpression(),this.semicolon()),this.finishNode(t,"ReturnStatement")},X.parseSwitchStatement=function(t){var e=this;this.next(),t.discriminant=this.parseParenExpression(),t.cases=[],this.expect(V.braceL),this.labels.push(J),this.enterLexicalScope();for(var s,i=!1;this.type!=V.braceR;)if(e.type===V._case||e.type===V._default){var r=e.type===V._case;s&&e.finishNode(s,"SwitchCase"),t.cases.push(s=e.startNode()),s.consequent=[],e.next(),r?s.test=e.parseExpression():(i&&e.raiseRecoverable(e.lastTokStart,"Multiple default clauses"),i=!0,s.test=null),e.expect(V.colon)}else s||e.unexpected(),s.consequent.push(e.parseStatement(!0));return this.exitLexicalScope(),s&&this.finishNode(s,"SwitchCase"),this.next(),this.labels.pop(),this.finishNode(t,"SwitchStatement")},X.parseThrowStatement=function(t){return this.next(),F.test(this.input.slice(this.lastTokEnd,this.start))&&this.raise(this.lastTokEnd,"Illegal newline after throw"),t.argument=this.parseExpression(),this.semicolon(),this.finishNode(t,"ThrowStatement")};var Z=[];X.parseTryStatement=function(t){if(this.next(),t.block=this.parseBlock(),t.handler=null,this.type===V._catch){var e=this.startNode();this.next(),this.expect(V.parenL),e.param=this.parseBindingAtom(),this.enterLexicalScope(),this.checkLVal(e.param,"let"),this.expect(V.parenR),e.body=this.parseBlock(!1),this.exitLexicalScope(),t.handler=this.finishNode(e,"CatchClause")}return t.finalizer=this.eat(V._finally)?this.parseBlock():null,t.handler||t.finalizer||this.raise(t.start,"Missing catch or finally clause"),this.finishNode(t,"TryStatement")},X.parseVarStatement=function(t,e){return this.next(),this.parseVar(t,!1,e),this.semicolon(),this.finishNode(t,"VariableDeclaration")},X.parseWhileStatement=function(t){return this.next(),t.test=this.parseParenExpression(),this.labels.push(K),t.body=this.parseStatement(!1),this.labels.pop(),this.finishNode(t,"WhileStatement")},X.parseWithStatement=function(t){return this.strict&&this.raise(this.start,"'with' in strict mode"),this.next(),t.object=this.parseParenExpression(),t.body=this.parseStatement(!1),this.finishNode(t,"WithStatement")},X.parseEmptyStatement=function(t){return this.next(),this.finishNode(t,"EmptyStatement")},X.parseLabeledStatement=function(t,e,s){for(var i=this,r=0,n=i.labels;r=0;o--){var h=i.labels[o];if(h.statementStart!=t.start)break;h.statementStart=i.start,h.kind=a}return this.labels.push({name:e,kind:a,statementStart:this.start}),t.body=this.parseStatement(!0),("ClassDeclaration"==t.body.type||"VariableDeclaration"==t.body.type&&"var"!=t.body.kind||"FunctionDeclaration"==t.body.type&&(this.strict||t.body.generator))&&this.raiseRecoverable(t.body.start,"Invalid labeled declaration"),this.labels.pop(),t.label=s,this.finishNode(t,"LabeledStatement")},X.parseExpressionStatement=function(t,e){return t.expression=e,this.semicolon(),this.finishNode(t,"ExpressionStatement")},X.parseBlock=function(t){var e=this;void 0===t&&(t=!0);var s=this.startNode();for(s.body=[],this.expect(V.braceL),t&&this.enterLexicalScope();!this.eat(V.braceR);){var i=e.parseStatement(!0);s.body.push(i)}return t&&this.exitLexicalScope(),this.finishNode(s,"BlockStatement")},X.parseFor=function(t,e){return t.init=e,this.expect(V.semi),t.test=this.type===V.semi?null:this.parseExpression(),this.expect(V.semi),t.update=this.type===V.parenR?null:this.parseExpression(),this.expect(V.parenR),this.exitLexicalScope(),t.body=this.parseStatement(!1),this.labels.pop(),this.finishNode(t,"ForStatement")},X.parseForIn=function(t,e){var s=this.type===V._in?"ForInStatement":"ForOfStatement";return this.next(),"ForInStatement"==s&&("AssignmentPattern"===e.type||"VariableDeclaration"===e.type&&null!=e.declarations[0].init&&(this.strict||"Identifier"!==e.declarations[0].id.type))&&this.raise(e.start,"Invalid assignment in for-in loop head"),t.left=e,t.right="ForInStatement"==s?this.parseExpression():this.parseMaybeAssign(),this.expect(V.parenR),this.exitLexicalScope(),t.body=this.parseStatement(!1),this.labels.pop(),this.finishNode(t,s)},X.parseVar=function(t,e,s){var i=this;for(t.declarations=[],t.kind=s;;){var r=i.startNode();if(i.parseVarId(r,s),i.eat(V.eq)?r.init=i.parseMaybeAssign(e):"const"!==s||i.type===V._in||i.options.ecmaVersion>=6&&i.isContextual("of")?"Identifier"==r.id.type||e&&(i.type===V._in||i.isContextual("of"))?r.init=null:i.raise(i.lastTokEnd,"Complex binding patterns require an initialization value"):i.unexpected(),t.declarations.push(i.finishNode(r,"VariableDeclarator")),!i.eat(V.comma))break}return t},X.parseVarId=function(t,e){t.id=this.parseBindingAtom(e),this.checkLVal(t.id,e,!1)},X.parseFunction=function(t,e,s,i){this.initFunction(t),this.options.ecmaVersion>=6&&!i&&(t.generator=this.eat(V.star)),this.options.ecmaVersion>=8&&(t.async=!!i),e&&(t.id="nullableID"===e&&this.type!=V.name?null:this.parseIdent(),t.id&&this.checkLVal(t.id,"var"));var r=this.inGenerator,n=this.inAsync,a=this.yieldPos,o=this.awaitPos,h=this.inFunction;return this.inGenerator=t.generator,this.inAsync=t.async,this.yieldPos=0,this.awaitPos=0,this.inFunction=!0,this.enterFunctionScope(),e||(t.id=this.type==V.name?this.parseIdent():null),this.parseFunctionParams(t),this.parseFunctionBody(t,s),this.inGenerator=r,this.inAsync=n,this.yieldPos=a,this.awaitPos=o,this.inFunction=h,this.finishNode(t,e?"FunctionDeclaration":"FunctionExpression")},X.parseFunctionParams=function(t){this.expect(V.parenL),t.params=this.parseBindingList(V.parenR,!1,this.options.ecmaVersion>=8),this.checkYieldAwaitInDefaultParams()},X.parseClass=function(t,e){var s=this;this.next(),this.parseClassId(t,e),this.parseClassSuper(t);var i=this.startNode(),r=!1;for(i.body=[],this.expect(V.braceL);!this.eat(V.braceR);){var n=s.parseClassMember(i);n&&"MethodDefinition"===n.type&&"constructor"===n.kind&&(r&&s.raise(n.start,"Duplicate constructor in the same class"),r=!0)}return t.body=this.finishNode(i,"ClassBody"),this.finishNode(t,e?"ClassDeclaration":"ClassExpression")},X.parseClassMember=function(t){var e=this;if(this.eat(V.semi))return null;var s=this.startNode(),i=function(t,i){void 0===i&&(i=!1);var r=e.start,n=e.startLoc;return!!e.eatContextual(t)&&(!(e.type===V.parenL||i&&e.canInsertSemicolon())||(s.key&&e.unexpected(),s.computed=!1,s.key=e.startNodeAt(r,n),s.key.name=t,e.finishNode(s.key,"Identifier"),!1))};s.kind="method",s.static=i("static");var r=this.eat(V.star),n=!1;r||(this.options.ecmaVersion>=8&&i("async",!0)?n=!0:i("get")?s.kind="get":i("set")&&(s.kind="set")),s.key||this.parsePropertyName(s);var a=s.key;return s.computed||s.static||!("Identifier"===a.type&&"constructor"===a.name||"Literal"===a.type&&"constructor"===a.value)?s.static&&"Identifier"===a.type&&"prototype"===a.name&&this.raise(a.start,"Classes may not have a static property named prototype"):("method"!==s.kind&&this.raise(a.start,"Constructor can't have get/set modifier"),r&&this.raise(a.start,"Constructor can't be a generator"),n&&this.raise(a.start,"Constructor can't be an async method"),s.kind="constructor"),this.parseClassMethod(t,s,r,n),"get"===s.kind&&0!==s.value.params.length&&this.raiseRecoverable(s.value.start,"getter should have no params"),"set"===s.kind&&1!==s.value.params.length&&this.raiseRecoverable(s.value.start,"setter should have exactly one param"),"set"===s.kind&&"RestElement"===s.value.params[0].type&&this.raiseRecoverable(s.value.params[0].start,"Setter cannot use rest params"),s},X.parseClassMethod=function(t,e,s,i){e.value=this.parseMethod(s,i),t.body.push(this.finishNode(e,"MethodDefinition"))},X.parseClassId=function(t,e){t.id=this.type===V.name?this.parseIdent():!0===e?this.unexpected():null},X.parseClassSuper=function(t){t.superClass=this.eat(V._extends)?this.parseExprSubscripts():null},X.parseExport=function(t,e){var s=this;if(this.next(),this.eat(V.star))return this.expectContextual("from"),this.type!==V.string&&this.unexpected(),t.source=this.parseExprAtom(),this.semicolon(),this.finishNode(t,"ExportAllDeclaration");if(this.eat(V._default)){this.checkExport(e,"default",this.lastTokStart);var i;if(this.type===V._function||(i=this.isAsyncFunction())){var r=this.startNode();this.next(),i&&this.next(),t.declaration=this.parseFunction(r,"nullableID",!1,i)}else if(this.type===V._class){var n=this.startNode();t.declaration=this.parseClass(n,"nullableID")}else t.declaration=this.parseMaybeAssign(),this.semicolon();return this.finishNode(t,"ExportDefaultDeclaration")}if(this.shouldParseExportStatement())t.declaration=this.parseStatement(!0),"VariableDeclaration"===t.declaration.type?this.checkVariableExport(e,t.declaration.declarations):this.checkExport(e,t.declaration.id.name,t.declaration.id.start),t.specifiers=[],t.source=null;else{if(t.declaration=null,t.specifiers=this.parseExportSpecifiers(e),this.eatContextual("from"))this.type!==V.string&&this.unexpected(),t.source=this.parseExprAtom();else{for(var a=0,o=t.specifiers;a=6&&t)switch(t.type){case"Identifier":this.inAsync&&"await"===t.name&&this.raise(t.start,"Can not use 'await' as identifier inside an async function");break;case"ObjectPattern":case"ArrayPattern":case"RestElement":break;case"ObjectExpression":t.type="ObjectPattern",s&&this.checkPatternErrors(s,!0);for(var r=0,n=t.properties;r=6&&(t.computed||t.method||t.shorthand))){var i,r=t.key;switch(r.type){case"Identifier":i=r.name;break;case"Literal":i=String(r.value);break;default:return}var n=t.kind;if(this.options.ecmaVersion>=6)return void("__proto__"===i&&"init"===n&&(e.proto&&(s&&s.doubleProto<0?s.doubleProto=r.start:this.raiseRecoverable(r.start,"Redefinition of __proto__ property")),e.proto=!0));i="$"+i;var a=e[i];if(a){var o;o="init"===n?this.strict&&a.init||a.get||a.set:a.init||a[n],o&&this.raiseRecoverable(r.start,"Redefinition of property")}else a=e[i]={init:!1,get:!1,set:!1};a[n]=!0}},et.parseExpression=function(t,e){var s=this,i=this.start,r=this.startLoc,n=this.parseMaybeAssign(t,e);if(this.type===V.comma){var a=this.startNodeAt(i,r);for(a.expressions=[n];this.eat(V.comma);)a.expressions.push(s.parseMaybeAssign(t,e));return this.finishNode(a,"SequenceExpression")}return n},et.parseMaybeAssign=function(t,e,s){if(this.inGenerator&&this.isContextual("yield"))return this.parseYield();var i=!1,r=-1,n=-1;e?(r=e.parenthesizedAssign,n=e.trailingComma,e.parenthesizedAssign=e.trailingComma=-1):(e=new u,i=!0);var a=this.start,o=this.startLoc;this.type!=V.parenL&&this.type!=V.name||(this.potentialArrowAt=this.start);var h=this.parseMaybeConditional(t,e);if(s&&(h=s.call(this,h,a,o)),this.type.isAssign){var p=this.startNodeAt(a,o);return p.operator=this.value,p.left=this.type===V.eq?this.toAssignable(h,!1,e):h,i||u.call(e),e.shorthandAssign=-1,this.checkLVal(h),this.next(),p.right=this.parseMaybeAssign(t),this.finishNode(p,"AssignmentExpression")}return i&&this.checkExpressionErrors(e,!0),r>-1&&(e.parenthesizedAssign=r),n>-1&&(e.trailingComma=n),h},et.parseMaybeConditional=function(t,e){var s=this.start,i=this.startLoc,r=this.parseExprOps(t,e);if(this.checkExpressionErrors(e))return r;if(this.eat(V.question)){var n=this.startNodeAt(s,i);return n.test=r,n.consequent=this.parseMaybeAssign(),this.expect(V.colon),n.alternate=this.parseMaybeAssign(t),this.finishNode(n,"ConditionalExpression")}return r},et.parseExprOps=function(t,e){var s=this.start,i=this.startLoc,r=this.parseMaybeUnary(e,!1);return this.checkExpressionErrors(e)?r:r.start==s&&"ArrowFunctionExpression"===r.type?r:this.parseExprOp(r,s,i,-1,t)},et.parseExprOp=function(t,e,s,i,r){var n=this.type.binop;if(null!=n&&(!r||this.type!==V._in)&&n>i){var a=this.type===V.logicalOR||this.type===V.logicalAND,o=this.value;this.next();var h=this.start,p=this.startLoc,c=this.parseExprOp(this.parseMaybeUnary(null,!1),h,p,n,r),l=this.buildBinary(e,s,t,c,o,a);return this.parseExprOp(l,e,s,i,r)}return t},et.buildBinary=function(t,e,s,i,r,n){var a=this.startNodeAt(t,e);return a.left=s,a.operator=r,a.right=i,this.finishNode(a,n?"LogicalExpression":"BinaryExpression")},et.parseMaybeUnary=function(t,e){var s,i=this,r=this.start,n=this.startLoc;if(this.inAsync&&this.isContextual("await"))s=this.parseAwait(),e=!0;else if(this.type.prefix){var a=this.startNode(),o=this.type===V.incDec;a.operator=this.value,a.prefix=!0,this.next(),a.argument=this.parseMaybeUnary(null,!0),this.checkExpressionErrors(t,!0),o?this.checkLVal(a.argument):this.strict&&"delete"===a.operator&&"Identifier"===a.argument.type?this.raiseRecoverable(a.start,"Deleting local variable in strict mode"):e=!0,s=this.finishNode(a,o?"UpdateExpression":"UnaryExpression")}else{if(s=this.parseExprSubscripts(t),this.checkExpressionErrors(t))return s;for(;this.type.postfix&&!this.canInsertSemicolon();){var h=i.startNodeAt(r,n);h.operator=i.value,h.prefix=!1,h.argument=s,i.checkLVal(s),i.next(),s=i.finishNode(h,"UpdateExpression")}}return!e&&this.eat(V.starstar)?this.buildBinary(r,n,s,this.parseMaybeUnary(null,!1),"**",!1):s},et.parseExprSubscripts=function(t){var e=this.start,s=this.startLoc,i=this.parseExprAtom(t),r="ArrowFunctionExpression"===i.type&&")"!==this.input.slice(this.lastTokStart,this.lastTokEnd);if(this.checkExpressionErrors(t)||r)return i;var n=this.parseSubscripts(i,e,s);return t&&"MemberExpression"===n.type&&(t.parenthesizedAssign>=n.start&&(t.parenthesizedAssign=-1),t.parenthesizedBind>=n.start&&(t.parenthesizedBind=-1)),n},et.parseSubscripts=function(t,e,s,i){for(var r=this,n=this.options.ecmaVersion>=8&&"Identifier"===t.type&&"async"===t.name&&this.lastTokEnd==t.end&&!this.canInsertSemicolon()&&"async"===this.input.slice(t.start,t.end),a=void 0;;)if((a=r.eat(V.bracketL))||r.eat(V.dot)){var o=r.startNodeAt(e,s);o.object=t,o.property=a?r.parseExpression():r.parseIdent(!0),o.computed=!!a,a&&r.expect(V.bracketR),t=r.finishNode(o,"MemberExpression")}else if(!i&&r.eat(V.parenL)){var h=new u,p=r.yieldPos,c=r.awaitPos;r.yieldPos=0,r.awaitPos=0;var l=r.parseExprList(V.parenR,r.options.ecmaVersion>=8,!1,h);if(n&&!r.canInsertSemicolon()&&r.eat(V.arrow))return r.checkPatternErrors(h,!1),r.checkYieldAwaitInDefaultParams(),r.yieldPos=p,r.awaitPos=c,r.parseArrowExpression(r.startNodeAt(e,s),l,!0);r.checkExpressionErrors(h,!0),r.yieldPos=p||r.yieldPos,r.awaitPos=c||r.awaitPos;var d=r.startNodeAt(e,s);d.callee=t,d.arguments=l,t=r.finishNode(d,"CallExpression")}else{if(r.type!==V.backQuote)return t;var f=r.startNodeAt(e,s);f.tag=t,f.quasi=r.parseTemplate({isTagged:!0}),t=r.finishNode(f,"TaggedTemplateExpression")}},et.parseExprAtom=function(t){var e,s=this.potentialArrowAt==this.start;switch(this.type){case V._super:return this.inFunction||this.raise(this.start,"'super' outside of function or class"),e=this.startNode(),this.next(),this.type!==V.dot&&this.type!==V.bracketL&&this.type!==V.parenL&&this.unexpected(),this.finishNode(e,"Super");case V._this:return e=this.startNode(),this.next(),this.finishNode(e,"ThisExpression");case V.name:var i=this.start,r=this.startLoc,n=this.containsEsc,a=this.parseIdent(this.type!==V.name);if(this.options.ecmaVersion>=8&&!n&&"async"===a.name&&!this.canInsertSemicolon()&&this.eat(V._function))return this.parseFunction(this.startNodeAt(i,r),!1,!1,!0);if(s&&!this.canInsertSemicolon()){if(this.eat(V.arrow))return this.parseArrowExpression(this.startNodeAt(i,r),[a],!1);if(this.options.ecmaVersion>=8&&"async"===a.name&&this.type===V.name&&!n)return a=this.parseIdent(),!this.canInsertSemicolon()&&this.eat(V.arrow)||this.unexpected(),this.parseArrowExpression(this.startNodeAt(i,r),[a],!0)}return a;case V.regexp:var o=this.value;return e=this.parseLiteral(o.value),e.regex={pattern:o.pattern,flags:o.flags},e;case V.num:case V.string:return this.parseLiteral(this.value);case V._null:case V._true:case V._false:return e=this.startNode(),e.value=this.type===V._null?null:this.type===V._true,e.raw=this.type.keyword,this.next(),this.finishNode(e,"Literal");case V.parenL:var h=this.start,p=this.parseParenAndDistinguishExpression(s);return t&&(t.parenthesizedAssign<0&&!this.isSimpleAssignTarget(p)&&(t.parenthesizedAssign=h),t.parenthesizedBind<0&&(t.parenthesizedBind=h)),p;case V.bracketL:return e=this.startNode(),this.next(),e.elements=this.parseExprList(V.bracketR,!0,!0,t),this.finishNode(e,"ArrayExpression");case V.braceL:return this.parseObj(!1,t);case V._function:return e=this.startNode(),this.next(),this.parseFunction(e,!1);case V._class:return this.parseClass(this.startNode(),!1);case V._new:return this.parseNew();case V.backQuote:return this.parseTemplate();default:this.unexpected()}},et.parseLiteral=function(t){var e=this.startNode();return e.value=t,e.raw=this.input.slice(this.start,this.end),this.next(),this.finishNode(e,"Literal")},et.parseParenExpression=function(){this.expect(V.parenL);var t=this.parseExpression();return this.expect(V.parenR),t},et.parseParenAndDistinguishExpression=function(t){var e,s=this,i=this.start,r=this.startLoc,n=this.options.ecmaVersion>=8;if(this.options.ecmaVersion>=6){this.next();var a,o=this.start,h=this.startLoc,p=[],c=!0,l=!1,d=new u,f=this.yieldPos,m=this.awaitPos;for(this.yieldPos=0,this.awaitPos=0;this.type!==V.parenR;){if(c?c=!1:s.expect(V.comma),n&&s.afterTrailingComma(V.parenR,!0)){l=!0;break}if(s.type===V.ellipsis){a=s.start,p.push(s.parseParenItem(s.parseRestBinding())),s.type===V.comma&&s.raise(s.start,"Comma is not permitted after the rest element");break}p.push(s.parseMaybeAssign(!1,d,s.parseParenItem))}var x=this.start,y=this.startLoc;if(this.expect(V.parenR),t&&!this.canInsertSemicolon()&&this.eat(V.arrow))return this.checkPatternErrors(d,!1),this.checkYieldAwaitInDefaultParams(),this.yieldPos=f,this.awaitPos=m,this.parseParenArrowList(i,r,p);p.length&&!l||this.unexpected(this.lastTokStart),a&&this.unexpected(a),this.checkExpressionErrors(d,!0),this.yieldPos=f||this.yieldPos,this.awaitPos=m||this.awaitPos,p.length>1?(e=this.startNodeAt(o,h),e.expressions=p,this.finishNodeAt(e,"SequenceExpression",x,y)):e=p[0]}else e=this.parseParenExpression();if(this.options.preserveParens){var v=this.startNodeAt(i,r);return v.expression=e,this.finishNode(v,"ParenthesizedExpression")}return e},et.parseParenItem=function(t){return t},et.parseParenArrowList=function(t,e,s){return this.parseArrowExpression(this.startNodeAt(t,e),s)};var st=[];et.parseNew=function(){var t=this.startNode(),e=this.parseIdent(!0);if(this.options.ecmaVersion>=6&&this.eat(V.dot)){t.meta=e;var s=this.containsEsc;return t.property=this.parseIdent(!0),("target"!==t.property.name||s)&&this.raiseRecoverable(t.property.start,"The only valid meta property for new is new.target"),this.inFunction||this.raiseRecoverable(t.start,"new.target can only be used in functions"),this.finishNode(t,"MetaProperty")}var i=this.start,r=this.startLoc;return t.callee=this.parseSubscripts(this.parseExprAtom(),i,r,!0),this.eat(V.parenL)?t.arguments=this.parseExprList(V.parenR,this.options.ecmaVersion>=8,!1):t.arguments=st,this.finishNode(t,"NewExpression")},et.parseTemplateElement=function(t){var e=t.isTagged,s=this.startNode();return this.type===V.invalidTemplate?(e||this.raiseRecoverable(this.start,"Bad escape sequence in untagged template literal"),s.value={raw:this.value,cooked:null}):s.value={raw:this.input.slice(this.start,this.end).replace(/\r\n?/g,"\n"),cooked:this.value},this.next(),s.tail=this.type===V.backQuote,this.finishNode(s,"TemplateElement")},et.parseTemplate=function(t){var e=this;void 0===t&&(t={});var s=t.isTagged;void 0===s&&(s=!1);var i=this.startNode();this.next(),i.expressions=[];var r=this.parseTemplateElement({isTagged:s});for(i.quasis=[r];!r.tail;)e.expect(V.dollarBraceL),i.expressions.push(e.parseExpression()),e.expect(V.braceR),i.quasis.push(r=e.parseTemplateElement({isTagged:s}));return this.next(),this.finishNode(i,"TemplateLiteral")},et.isAsyncProp=function(t){return!t.computed&&"Identifier"===t.key.type&&"async"===t.key.name&&(this.type===V.name||this.type===V.num||this.type===V.string||this.type===V.bracketL||this.type.keyword)&&!F.test(this.input.slice(this.lastTokEnd,this.start))},et.parseObj=function(t,e){var s=this,i=this.startNode(),r=!0,n={};for(i.properties=[],this.next();!this.eat(V.braceR);){if(r)r=!1;else if(s.expect(V.comma),s.afterTrailingComma(V.braceR))break;var a=s.parseProperty(t,e);t||s.checkPropClash(a,n,e),i.properties.push(a)}return this.finishNode(i,t?"ObjectPattern":"ObjectExpression")},et.parseProperty=function(t,e){var s,i,r,n,a=this.startNode();this.options.ecmaVersion>=6&&(a.method=!1,a.shorthand=!1,(t||e)&&(r=this.start,n=this.startLoc),t||(s=this.eat(V.star)));var o=this.containsEsc;return this.parsePropertyName(a),!t&&!o&&this.options.ecmaVersion>=8&&!s&&this.isAsyncProp(a)?(i=!0,this.parsePropertyName(a,e)):i=!1,this.parsePropertyValue(a,t,s,i,r,n,e,o),this.finishNode(a,"Property")},et.parsePropertyValue=function(t,e,s,i,r,n,a,o){if((s||i)&&this.type===V.colon&&this.unexpected(),this.eat(V.colon))t.value=e?this.parseMaybeDefault(this.start,this.startLoc):this.parseMaybeAssign(!1,a),t.kind="init";else if(this.options.ecmaVersion>=6&&this.type===V.parenL)e&&this.unexpected(),t.kind="init",t.method=!0,t.value=this.parseMethod(s,i);else if(e||o||!(this.options.ecmaVersion>=5)||t.computed||"Identifier"!==t.key.type||"get"!==t.key.name&&"set"!==t.key.name||this.type==V.comma||this.type==V.braceR)this.options.ecmaVersion>=6&&!t.computed&&"Identifier"===t.key.type?(this.checkUnreserved(t.key),t.kind="init",e?t.value=this.parseMaybeDefault(r,n,t.key):this.type===V.eq&&a?(a.shorthandAssign<0&&(a.shorthandAssign=this.start),t.value=this.parseMaybeDefault(r,n,t.key)):t.value=t.key,t.shorthand=!0):this.unexpected();else{(s||i)&&this.unexpected(),t.kind=t.key.name,this.parsePropertyName(t),t.value=this.parseMethod(!1);var h="get"===t.kind?0:1;if(t.value.params.length!==h){var p=t.value.start;"get"===t.kind?this.raiseRecoverable(p,"getter should have no params"):this.raiseRecoverable(p,"setter should have exactly one param")}else"set"===t.kind&&"RestElement"===t.value.params[0].type&&this.raiseRecoverable(t.value.params[0].start,"Setter cannot use rest params")}},et.parsePropertyName=function(t){if(this.options.ecmaVersion>=6){if(this.eat(V.bracketL))return t.computed=!0,t.key=this.parseMaybeAssign(),this.expect(V.bracketR),t.key;t.computed=!1}return t.key=this.type===V.num||this.type===V.string?this.parseExprAtom():this.parseIdent(!0)},et.initFunction=function(t){t.id=null,this.options.ecmaVersion>=6&&(t.generator=!1,t.expression=!1),this.options.ecmaVersion>=8&&(t.async=!1)},et.parseMethod=function(t,e){var s=this.startNode(),i=this.inGenerator,r=this.inAsync,n=this.yieldPos,a=this.awaitPos,o=this.inFunction;return this.initFunction(s),this.options.ecmaVersion>=6&&(s.generator=t),this.options.ecmaVersion>=8&&(s.async=!!e),this.inGenerator=s.generator,this.inAsync=s.async,this.yieldPos=0,this.awaitPos=0,this.inFunction=!0,this.enterFunctionScope(),this.expect(V.parenL),s.params=this.parseBindingList(V.parenR,!1,this.options.ecmaVersion>=8),this.checkYieldAwaitInDefaultParams(),this.parseFunctionBody(s,!1),this.inGenerator=i,this.inAsync=r,this.yieldPos=n,this.awaitPos=a,this.inFunction=o,this.finishNode(s,"FunctionExpression")},et.parseArrowExpression=function(t,e,s){var i=this.inGenerator,r=this.inAsync,n=this.yieldPos,a=this.awaitPos,o=this.inFunction;return this.enterFunctionScope(),this.initFunction(t),this.options.ecmaVersion>=8&&(t.async=!!s),this.inGenerator=!1,this.inAsync=t.async,this.yieldPos=0,this.awaitPos=0,this.inFunction=!0,t.params=this.toAssignableList(e,!0),this.parseFunctionBody(t,!0),this.inGenerator=i,this.inAsync=r,this.yieldPos=n,this.awaitPos=a,this.inFunction=o,this.finishNode(t,"ArrowFunctionExpression")},et.parseFunctionBody=function(t,e){var s=e&&this.type!==V.braceL,i=this.strict,r=!1;if(s)t.body=this.parseMaybeAssign(),t.expression=!0,this.checkParams(t,!1);else{var n=this.options.ecmaVersion>=7&&!this.isSimpleParamList(t.params);i&&!n||(r=this.strictDirective(this.end))&&n&&this.raiseRecoverable(t.start,"Illegal 'use strict' directive in function with non-simple parameter list");var a=this.labels;this.labels=[],r&&(this.strict=!0),this.checkParams(t,!i&&!r&&!e&&this.isSimpleParamList(t.params)),t.body=this.parseBlock(!1),t.expression=!1,this.adaptDirectivePrologue(t.body.body),this.labels=a}this.exitFunctionScope(),this.strict&&t.id&&this.checkLVal(t.id,"none"),this.strict=i},et.isSimpleParamList=function(t){for(var e=0,s=t;e0;)e[s]=arguments[s+1];for(var i=0,r=e;i=1;e--){var s=t.context[e];if("function"===s.token)return s.generator}return!1},ct.updateContext=function(t){var e,s=this.type;s.keyword&&t==V.dot?this.exprAllowed=!1:(e=s.updateContext)?e.call(this,t):this.exprAllowed=s.beforeExpr},V.parenR.updateContext=V.braceR.updateContext=function(){if(1==this.context.length)return void(this.exprAllowed=!0);var t=this.context.pop();t===pt.b_stat&&"function"===this.curContext().token&&(t=this.context.pop()),this.exprAllowed=!t.isExpr},V.braceL.updateContext=function(t){this.context.push(this.braceIsBlock(t)?pt.b_stat:pt.b_expr),this.exprAllowed=!0},V.dollarBraceL.updateContext=function(){this.context.push(pt.b_tmpl),this.exprAllowed=!0},V.parenL.updateContext=function(t){var e=t===V._if||t===V._for||t===V._with||t===V._while;this.context.push(e?pt.p_stat:pt.p_expr),this.exprAllowed=!0},V.incDec.updateContext=function(){},V._function.updateContext=V._class.updateContext=function(t){t.beforeExpr&&t!==V.semi&&t!==V._else&&(t!==V.colon&&t!==V.braceL||this.curContext()!==pt.b_stat)?this.context.push(pt.f_expr):this.context.push(pt.f_stat),this.exprAllowed=!1},V.backQuote.updateContext=function(){this.curContext()===pt.q_tmpl?this.context.pop():this.context.push(pt.q_tmpl),this.exprAllowed=!1},V.star.updateContext=function(t){if(t==V._function){var e=this.context.length-1;this.context[e]===pt.f_expr?this.context[e]=pt.f_expr_gen:this.context[e]=pt.f_gen}this.exprAllowed=!0},V.name.updateContext=function(t){var e=!1;this.options.ecmaVersion>=6&&("of"==this.value&&!this.exprAllowed||"yield"==this.value&&this.inGeneratorContext())&&(e=!0),this.exprAllowed=e};var lt=function(t){this.type=t.type,this.value=t.value,this.start=t.start,this.end=t.end,t.options.locations&&(this.loc=new W(t,t.startLoc,t.endLoc)),t.options.ranges&&(this.range=[t.start,t.end])},ut=$.prototype,dt="object"==typeof Packages&&"[object JavaPackage]"==Object.prototype.toString.call(Packages);ut.next=function(){this.options.onToken&&this.options.onToken(new lt(this)),this.lastTokEnd=this.end,this.lastTokStart=this.start,this.lastTokEndLoc=this.endLoc,this.lastTokStartLoc=this.startLoc,this.nextToken()},ut.getToken=function(){return this.next(),new lt(this)},"undefined"!=typeof Symbol&&(ut[Symbol.iterator]=function(){var t=this;return{next:function(){var e=t.getToken();return{done:e.type===V.eof,value:e}}}}),ut.curContext=function(){return this.context[this.context.length-1]},ut.nextToken=function(){var t=this.curContext();return t&&t.preserveSpace||this.skipSpace(),this.start=this.pos,this.options.locations&&(this.startLoc=this.curPosition()),this.pos>=this.input.length?this.finishToken(V.eof):t.override?t.override(this):void this.readToken(this.fullCharCodeAtPos())},ut.readToken=function(t){return s(t,this.options.ecmaVersion>=6)||92===t?this.readWord():this.getTokenFromCode(t)},ut.fullCharCodeAtPos=function(){var t=this.input.charCodeAt(this.pos);return t<=55295||t>=57344?t:(t<<10)+this.input.charCodeAt(this.pos+1)-56613888},ut.skipBlockComment=function(){var t=this,e=this.options.onComment&&this.curPosition(),s=this.pos,i=this.input.indexOf("*/",this.pos+=2);if(-1===i&&this.raise(this.pos-2,"Unterminated comment"),this.pos=i+2,this.options.locations){D.lastIndex=s;for(var r;(r=D.exec(this.input))&&r.index8&&e<14||e>=5760&&B.test(String.fromCharCode(e))))break t;++t.pos}}},ut.finishToken=function(t,e){this.end=this.pos,this.options.locations&&(this.endLoc=this.curPosition());var s=this.type;this.type=t,this.value=e,this.updateContext(s)},ut.readToken_dot=function(){var t=this.input.charCodeAt(this.pos+1);if(t>=48&&t<=57)return this.readNumber(!0);var e=this.input.charCodeAt(this.pos+2);return this.options.ecmaVersion>=6&&46===t&&46===e?(this.pos+=3,this.finishToken(V.ellipsis)):(++this.pos,this.finishToken(V.dot))},ut.readToken_slash=function(){var t=this.input.charCodeAt(this.pos+1);return this.exprAllowed?(++this.pos,this.readRegexp()):61===t?this.finishOp(V.assign,2):this.finishOp(V.slash,1)},ut.readToken_mult_modulo_exp=function(t){var e=this.input.charCodeAt(this.pos+1),s=1,i=42===t?V.star:V.modulo;return this.options.ecmaVersion>=7&&42==t&&42===e&&(++s,i=V.starstar,e=this.input.charCodeAt(this.pos+2)),61===e?this.finishOp(V.assign,s+1):this.finishOp(i,s)},ut.readToken_pipe_amp=function(t){var e=this.input.charCodeAt(this.pos+1);return e===t?this.finishOp(124===t?V.logicalOR:V.logicalAND,2):61===e?this.finishOp(V.assign,2):this.finishOp(124===t?V.bitwiseOR:V.bitwiseAND,1)},ut.readToken_caret=function(){return 61===this.input.charCodeAt(this.pos+1)?this.finishOp(V.assign,2):this.finishOp(V.bitwiseXOR,1)},ut.readToken_plus_min=function(t){var e=this.input.charCodeAt(this.pos+1);return e===t?45!=e||this.inModule||62!=this.input.charCodeAt(this.pos+2)||0!==this.lastTokEnd&&!F.test(this.input.slice(this.lastTokEnd,this.pos))?this.finishOp(V.incDec,2):(this.skipLineComment(3),this.skipSpace(),this.nextToken()):61===e?this.finishOp(V.assign,2):this.finishOp(V.plusMin,1)},ut.readToken_lt_gt=function(t){var e=this.input.charCodeAt(this.pos+1),s=1;return e===t?(s=62===t&&62===this.input.charCodeAt(this.pos+2)?3:2,61===this.input.charCodeAt(this.pos+s)?this.finishOp(V.assign,s+1):this.finishOp(V.bitShift,s)):33!=e||60!=t||this.inModule||45!=this.input.charCodeAt(this.pos+2)||45!=this.input.charCodeAt(this.pos+3)?(61===e&&(s=2),this.finishOp(V.relational,s)):(this.skipLineComment(4),this.skipSpace(),this.nextToken())},ut.readToken_eq_excl=function(t){var e=this.input.charCodeAt(this.pos+1);return 61===e?this.finishOp(V.equality,61===this.input.charCodeAt(this.pos+2)?3:2):61===t&&62===e&&this.options.ecmaVersion>=6?(this.pos+=2,this.finishToken(V.arrow)):this.finishOp(61===t?V.eq:V.prefix,1)},ut.getTokenFromCode=function(t){switch(t){case 46:return this.readToken_dot();case 40:return++this.pos,this.finishToken(V.parenL);case 41:return++this.pos,this.finishToken(V.parenR);case 59:return++this.pos,this.finishToken(V.semi);case 44:return++this.pos,this.finishToken(V.comma);case 91:return++this.pos,this.finishToken(V.bracketL);case 93:return++this.pos,this.finishToken(V.bracketR);case 123:return++this.pos,this.finishToken(V.braceL);case 125:return++this.pos,this.finishToken(V.braceR);case 58:return++this.pos,this.finishToken(V.colon);case 63:return++this.pos,this.finishToken(V.question);case 96:if(this.options.ecmaVersion<6)break;return++this.pos,this.finishToken(V.backQuote);case 48:var e=this.input.charCodeAt(this.pos+1);if(120===e||88===e)return this.readRadixNumber(16);if(this.options.ecmaVersion>=6){if(111===e||79===e)return this.readRadixNumber(8);if(98===e||66===e)return this.readRadixNumber(2)}case 49:case 50:case 51:case 52:case 53:case 54:case 55:case 56:case 57:return this.readNumber(!1);case 34:case 39:return this.readString(t);case 47:return this.readToken_slash();case 37:case 42:return this.readToken_mult_modulo_exp(t);case 124:case 38:return this.readToken_pipe_amp(t);case 94:return this.readToken_caret();case 43:case 45:return this.readToken_plus_min(t);case 60:case 62:return this.readToken_lt_gt(t);case 61:case 33:return this.readToken_eq_excl(t);case 126:return this.finishOp(V.prefix,1)}this.raise(this.pos,"Unexpected character '"+m(t)+"'")},ut.finishOp=function(t,e){var s=this.input.slice(this.pos,this.pos+e);return this.pos+=e,this.finishToken(t,s)};var ft=!!f("￿","u");ut.readRegexp=function(){for(var t,e,s=this,i=this.pos;;){s.pos>=s.input.length&&s.raise(i,"Unterminated regular expression");var r=s.input.charAt(s.pos);if(F.test(r)&&s.raise(i,"Unterminated regular expression"),t)t=!1;else{if("["===r)e=!0;else if("]"===r&&e)e=!1;else if("/"===r&&!e)break;t="\\"===r}++s.pos}var n=this.input.slice(i,this.pos);++this.pos;var a=this.readWord1(),o=n,h="";if(a){var p=/^[gim]*$/;this.options.ecmaVersion>=6&&(p=/^[gimuy]*$/),this.options.ecmaVersion>=9&&(p=/^[gimsuy]*$/),p.test(a)||this.raise(i,"Invalid regular expression flag"),a.indexOf("u")>=0&&(ft?h="u":(o=o.replace(/\\u\{([0-9a-fA-F]+)\}/g,function(t,e,r){return e=Number("0x"+e),e>1114111&&s.raise(i+r+3,"Code point out of bounds"),"x"}),o=o.replace(/\\u([a-fA-F0-9]{4})|[\uD800-\uDBFF][\uDC00-\uDFFF]/g,"x"),h=h.replace("u","")))}var c=null;return dt||(f(o,h,i,this),c=f(n,a)),this.finishToken(V.regexp,{pattern:n,flags:a,value:c})},ut.readInt=function(t,e){for(var s=this,i=this.pos,r=0,n=0,a=null==e?1/0:e;n=97?o-97+10:o>=65?o-65+10:o>=48&&o<=57?o-48:1/0)>=t)break;++s.pos,r=r*t+h}return this.pos===i||null!=e&&this.pos-i!==e?null:r},ut.readRadixNumber=function(t){this.pos+=2;var e=this.readInt(t);return null==e&&this.raise(this.start+2,"Expected number in radix "+t),s(this.fullCharCodeAtPos())&&this.raise(this.pos,"Identifier directly after number"),this.finishToken(V.num,e)},ut.readNumber=function(t){var e=this.pos;t||null!==this.readInt(10)||this.raise(e,"Invalid number");var i=this.pos-e>=2&&48===this.input.charCodeAt(e);i&&this.strict&&this.raise(e,"Invalid number"),i&&/[89]/.test(this.input.slice(e,this.pos))&&(i=!1);var r=this.input.charCodeAt(this.pos) @@ -402,7 +403,6 @@ function Stream(){EE.call(this)}module.exports=Stream;var EE=require("events").E },{"./":100}],100:[function(require,module,exports){ "use strict";function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.Block=void 0;var _thaw=require("./thaw"),_thaw2=_interopRequireDefault(_thaw),_block=require("./block"),_block2=_interopRequireDefault(_block);exports.default=_thaw2.default,exports.Block=_block2.default,"undefined"!=typeof window&&(window.Thaw=_thaw2.default,window.Thaw.Block=_block2.default); - },{"./block":99,"./thaw":101}],101:[function(require,module,exports){ "use strict";function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function thaw(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return new Thaw(t,e)}Object.defineProperty(exports,"__esModule",{value:!0});var _extends=Object.assign||function(t){for(var e=1;e1&&void 0!==arguments[1]?arguments[1]:{};_classCallCheck(this,t);var s=_extends({},this.constructor.defaultSettings,n),a=s.each,r=s.done;this.items=e,this.i=0,this.options=n;var h=this.tick=function(){if(!(i.i<0||(i.timeout=setTimeout(h,0),thawing))){var t=e[i.i];if(i.i>=e.length)return null!==r&&(thawing=!0,r(t,i.i),thawing=!1),i.i=-1,void clearTimeout(i.timeout);null!==a?(thawing=!0,a(t,i.i),thawing=!1):void 0!==t&&t(),i.i++}};thaws.push(this),n.delay||h()}return _createClass(t,null,[{key:"stopAll",value:function(){for(var t=0;t {\n if (this._trainingTick(data, status, endTime)) {\n options.done();\n } else {\n requestAnimationFrame(train);\n }\n };\n train();\n }\n\n buildRunInput() {\n let weightedSum = null;\n\n switch (this.activation) {\n case 'sigmoid':\n weightedSum = weightedSumSigmoid;\n break;\n case 'relu':\n weightedSum = weightedSumRelu;\n break;\n case 'leaky-relu':\n weightedSum = weightedSumLeakyRelu;\n break;\n case 'tanh':\n weightedSum = weightedSumTanh;\n break;\n default:\n throw new Error('unknown activation ' + this.activation);\n }\n\n for(let layer = 1; layer <= this.outputLayer; layer++){\n this.forwardPropagate[layer] = this.gpu.createKernel(weightedSum, {\n output: [this.sizes[layer]],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true,\n constants: {\n size: this.sizes[layer - 1]\n }\n });\n }\n }\n\n /**\n *\n * @param input\n * @returns {*}\n */\n runInput(input) {\n let output;\n this.outputs[0] = input;\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n this.outputs[layer] = this.forwardPropagate[layer](\n this.weights[layer], \n this.biases[layer], \n input\n );\n\n output = input = this.outputs[layer];\n }\n return output;\n }\n\n buildCalculateDeltas() {\n let calcDeltas = null;\n\n switch (this.activation) {\n case 'sigmoid':\n calcDeltas = calcDeltasSigmoid;\n break;\n case 'relu':\n calcDeltas = calcDeltasRelu;\n break;\n case 'leaky-relu':\n calcDeltas = calcDeltasLeakyRelu;\n break;\n case 'tanh':\n calcDeltas = calcDeltasTanh;\n break;\n default:\n throw new Error('unknown activation ' + this.activation);\n }\n\n for (let layer = this.outputLayer; layer > 0; layer--) {\n if (layer === this.outputLayer) {\n this.backwardPropagate[layer] = this.gpu.createKernelMap({\n error: GPU.alias('calcErrorOutput', calcErrorOutput),\n deltas: GPU.alias('calcDeltas', calcDeltas)\n }, function(outputs, targets) {\n const output = outputs[this.thread.x];\n return calcDeltas(calcErrorOutput(output, targets), output);\n }, {\n output: [this.sizes[layer]],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true\n });\n } else {\n this.backwardPropagate[layer] = this.gpu.createKernelMap({\n error: GPU.alias('calcError', calcError),\n deltas: GPU.alias('calcDeltas', calcDeltas),\n }, function(nextWeights, outputs, nextDeltas){\n let output = outputs[this.thread.x];\n return calcDeltas(calcError(nextWeights, nextDeltas), output);\n }, {\n output: [this.sizes[layer]],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true,\n constants: {\n size: this.deltas[layer + 1].length\n }\n });\n }\n }\n }\n\n calculateDeltas(target) {\n for (let layer = this.outputLayer; layer > 0; layer--) {\n let output;\n if (layer === this.outputLayer) {\n output = this.backwardPropagate[layer](\n this.outputs[layer],\n target);\n } else {\n output = this.backwardPropagate[layer](\n this.weights[layer + 1],\n this.outputs[layer],\n this.deltas[layer + 1],\n );\n }\n\n this.deltas[layer] = output.deltas;\n this.errors[layer] = output.error;\n }\n }\n\n buildGetChanges() {\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n this.changesPropagate[layer] = this.gpu.createKernelMap({\n weights: GPU.alias('addWeights', addWeights),\n changes: GPU.alias('calcChanges', calcChanges)\n },\n function(previousOutputs, deltas, weights, changes) {\n let change = calcChanges(\n changes,\n deltas,\n previousOutputs);\n\n return addWeights(change, weights);\n }, {\n output: [this.sizes[layer - 1], this.sizes[layer]],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true,\n constants:{\n size: this.outputs[layer - 1].length,\n learningRate: this.trainOpts.learningRate,\n momentum: this.trainOpts.momentum\n }\n });\n } \n }\n \n getChanges() {\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n let output = this.changesPropagate[layer](\n this.outputs[layer - 1],\n this.deltas[layer],\n this.weights[layer],\n this.changes[layer]\n );\n this.changes[layer] = output.changes;\n this.weights[layer] = output.weights;\n }\n }\n\n buildChangeBiases() {\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n this.biasesPropagate[layer] = this.gpu.createKernel(addBiases, {\n output: [this.sizes[layer]],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true,\n constants: {\n learningRate: this.trainOpts.learningRate\n }\n });\n }\n }\n\n changeBiases() {\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n this.biases[layer] = this.biasesPropagate[layer](\n this.biases[layer],\n this.deltas[layer]\n );\n }\n }\n\n buildGetMSE() {\n this.getMSE = this.gpu.createKernel(mse, {\n output: [1],\n constants: {\n size: this.sizes[this.outputLayer]\n }\n });\n }\n\n /**\n *\n * @param input\n * @returns {*}\n */\n run(input) {\n if (!this.isRunnable) return null;\n if (this.inputLookup) {\n input = lookup.toArray(this.inputLookup, input);\n }\n const inputTexture = this._texturizeInputData(input);\n const outputTextures = this.runInput(inputTexture);\n let output = outputTextures.toArray(this.gpu);\n\n if (this.outputLookup) {\n output = lookup.toHash(this.outputLookup, output);\n }\n return output;\n }\n\n\n /**\n *\n * @param data\n * Verifies network sizes are initilaized\n * If they are not it will initialize them based off the data set.\n */\n _verifyIsInitialized(data) {\n if (this.sizes) return;\n\n this.sizes = [];\n if (!data[0].size) {\n data[0].size = { input: data[0].input.length, output: data[0].output.length };\n }\n\n this.sizes.push(data[0].size.input);\n if (!this.hiddenSizes) {\n this.sizes.push(Math.max(3, Math.floor(data[0].size.input / 2)));\n } else {\n this.hiddenSizes.forEach(size => {\n this.sizes.push(size);\n });\n }\n this.sizes.push(data[0].size.output);\n\n this._initialize();\n }\n\n /**\n *\n * @param data\n * @returns {*}\n */\n _formatData(data) {\n data = super._formatData(data);\n\n this._texturizeInputData = this.gpu.createKernel(function(value) {\n return value[this.thread.x];\n }, {\n output: [data[0].input.length],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true\n });\n const texturizeOutputData = this.gpu.createKernel(function(value) {\n return value[this.thread.x];\n }, {\n output: [data[0].output.length],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true\n });\n data.forEach(d => {\n d.size = {input: d.input.length, output: d.output.length};\n d.input = this._texturizeInputData(d.input);\n d.output = texturizeOutputData(d.output);\n });\n return data;\n }\n\n toFunction() {\n throw new Error('not implemented on NeuralNetworkGPU');\n }\n\n}\n\nfunction weightedSumSigmoid(weights, biases, inputs) {\n let sum = biases[this.thread.x];\n for (let k = 0; k < this.constants.size; k++) {\n sum += weights[this.thread.x][k] * inputs[k];\n }\n //sigmoid\n return 1 / (1 + Math.exp(-sum));\n}\n\nfunction weightedSumRelu(weights, biases, inputs) {\n let sum = biases[this.thread.x];\n for (let k = 0; k < this.constants.size; k++) {\n sum += weights[this.thread.x][k] * inputs[k];\n }\n //relu\n return (sum < 0 ? 0 : sum);\n}\n\nfunction weightedSumLeakyRelu(weights, biases, inputs) {\n let sum = biases[this.thread.x];\n for (let k = 0; k < this.constants.size; k++) {\n sum += weights[this.thread.x][k] * inputs[k];\n }\n //leaky relu\n return (sum < 0 ? 0 : 0.01 * sum);\n}\n\nfunction weightedSumTanh(weights, biases, inputs) {\n let sum = biases[this.thread.x];\n for (let k = 0; k < this.constants.size; k++) {\n sum += weights[this.thread.x][k] * inputs[k];\n }\n //tanh\n return Math.tanh(sum);\n}\n\nfunction calcErrorOutput(output, targets) {\n return targets[this.thread.x] - output;\n}\n\nfunction calcDeltasSigmoid(error, output) {\n //sigmoid derivative\n return error * output * (1 - output);\n}\n\nfunction calcDeltasRelu(error, output) {\n //relu derivative\n return output > 0 ? error : 0;\n}\n\nfunction calcDeltasLeakyRelu(error, output) {\n //leaky relu derivative\n return output > 0 ? error : 0.01 * error;\n}\n\nfunction calcDeltasTanh(error, output) {\n //tanh derivative\n return (1 - output * output) * error;\n}\n\nfunction calcError(nextWeights, nextDeltas){\n let error = 0;\n for(let k = 0; k < this.constants.size; k++){\n error += nextDeltas[k] * nextWeights[k][this.thread.x];\n }\n return error;\n}\n\nfunction calcChanges(\n previousChanges,\n deltas,\n previousOutputs\n) {\n return (this.constants.learningRate * deltas[this.thread.y] * previousOutputs[this.thread.x])\n + (this.constants.momentum * previousChanges[this.thread.y][this.thread.x]);\n}\n\nfunction addWeights(change, weights){\n return change + weights[this.thread.y][this.thread.x];\n}\n\nfunction addBiases(biases, deltas){\n return biases[this.thread.x] + (deltas[this.thread.x] * this.constants.learningRate);\n}\n\n// mean squared error, reimplemented for GPU\nfunction mse(errors) {\n let sum = 0;\n for (let i = 0; i < this.constants.size; i++) {\n sum += Math.pow(errors[i], 2);\n }\n return sum / this.constants.size;\n}"]} \ No newline at end of file +{"version":3,"sources":["../src/neural-network-gpu.js"],"names":["NeuralNetworkGPU","options","forwardPropagate","backwardPropagate","changesPropagate","biasesPropagate","gpu","mode","buildRunInput","buildCalculateDeltas","buildGetChanges","buildChangeBiases","buildGetMSE","input","target","logErrorRate","runInput","calculateDeltas","getChanges","changeBiases","getMSE","errors","outputLayer","data","status","endTime","_prepTraining","Promise","resolve","reject","train","_trainingTick","requestAnimationFrame","weightedSum","activation","weightedSumSigmoid","weightedSumRelu","weightedSumLeakyRelu","weightedSumTanh","Error","layer","createKernel","output","sizes","outputToTexture","outputImmutable","hardcodeConstants","constants","size","floatTextures","outputs","weights","biases","calcDeltas","calcDeltasSigmoid","calcDeltasRelu","calcDeltasLeakyRelu","calcDeltasTanh","createKernelMap","error","alias","calcErrorOutput","deltas","targets","thread","x","calcError","nextWeights","nextDeltas","length","addWeights","changes","calcChanges","previousOutputs","change","learningRate","trainOpts","momentum","addBiases","mse","isRunnable","inputLookup","toArray","inputTexture","_texturizeInputData","outputTextures","outputLookup","toHash","push","hiddenSizes","Math","max","floor","forEach","_initialize","value","texturizeOutputData","d","inputs","sum","k","exp","tanh","previousChanges","y","i","pow"],"mappings":";;;;;;;;;;AAAA;;;;AACA;;;;AACA;;;;;;;;;;;;AAEA;;;;;IAKqBA,gB;;;AACnB,8BAA0B;AAAA,QAAdC,OAAc,uEAAJ,EAAI;;AAAA;;AAAA,oIAClBA,OADkB;;AAExB,UAAKC,gBAAL,GAAwB,EAAxB;AACA,UAAKC,iBAAL,GAAyB,EAAzB;AACA,UAAKC,gBAAL,GAAwB,EAAxB;AACA,UAAKC,eAAL,GAAuB,EAAvB;AACA,UAAKC,GAAL,GAAW,kBAAQ,EAACC,MAAMN,QAAQM,IAAf,EAAR,CAAX;AANwB;AAOzB;;AAED;;;;;;;;kCAIc;AACZ;AACA,WAAKC,aAAL;AACA,WAAKC,oBAAL;AACA,WAAKC,eAAL;AACA,WAAKC,iBAAL;AACA,WAAKC,WAAL;AACD;;;oCAEe,CAAE;;AAElB;;;;;;;;;kCAMcC,K,EAAOC,M,EAAQC,Y,EAAc;AACzC;AACA,WAAKC,QAAL,CAAcH,KAAd;;AAEA;AACA,WAAKI,eAAL,CAAqBH,MAArB;AACA,WAAKI,UAAL;AACA,WAAKC,YAAL;;AAEA,UAAIJ,YAAJ,EAAkB;AAChB,eAAO,KAAKK,MAAL,CAAY,KAAKC,MAAL,CAAY,KAAKC,WAAjB,CAAZ,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AnC,EAAyCyD,GAAzC,EAA8C;AAC5CN,WAAOT,KAAKgB,GAAL,CAASrF,OAAOoF,CAAP,CAAT,EAAoB,CAApB,CAAP;AACD;AACD,SAAON,MAAM,KAAKpD,SAAL,CAAeC,IAA5B;AACD","file":"neural-network-gpu.js","sourcesContent":["import NeuralNetwork from './neural-network';\nimport lookup from './lookup';\nimport GPU from 'gpu.js';\n\n/**\n *\n * @param {object} options\n * @constructor\n */\nexport default class NeuralNetworkGPU extends NeuralNetwork {\n constructor(options = {}) {\n super(options);\n this.forwardPropagate = [];\n this.backwardPropagate = [];\n this.changesPropagate = [];\n this.biasesPropagate = [];\n this.gpu = new GPU({mode: options.mode});\n }\n\n /**\n *\n * @param {Number[]} sizes\n */\n _initialize() {\n super._initialize();\n this.buildRunInput();\n this.buildCalculateDeltas();\n this.buildGetChanges();\n this.buildChangeBiases();\n this.buildGetMSE();\n }\n\n setActivation() {}\n\n /**\n *\n * @param input\n * @param target\n * @param logErrorRate\n */\n _trainPattern(input, target, logErrorRate) {\n // forward propagate\n this.runInput(input);\n\n // backward propagate\n this.calculateDeltas(target);\n this.getChanges();\n this.changeBiases();\n\n if (logErrorRate) {\n return this.getMSE(this.errors[this.outputLayer])[0];\n } else {\n return null;\n }\n }\n\n trainAsync(data, options) {\n let status, endTime;\n ({ data, status, endTime } = this._prepTraining(data, options));\n\n return new Promise((resolve, reject) => {\n const train = () => {\n if (this._trainingTick(data, status, endTime)) {\n requestAnimationFrame(train);\n } else {\n resolve(status);\n }\n };\n train();\n });\n }\n\n buildRunInput() {\n let weightedSum = null;\n\n switch (this.activation) {\n case 'sigmoid':\n weightedSum = weightedSumSigmoid;\n break;\n case 'relu':\n weightedSum = weightedSumRelu;\n break;\n case 'leaky-relu':\n weightedSum = weightedSumLeakyRelu;\n break;\n case 'tanh':\n weightedSum = weightedSumTanh;\n break;\n default:\n throw new Error('unknown activation ' + this.activation);\n }\n\n for(let layer = 1; layer <= this.outputLayer; layer++){\n this.forwardPropagate[layer] = this.gpu.createKernel(weightedSum, {\n output: [this.sizes[layer]],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true,\n constants: {\n size: this.sizes[layer - 1]\n },\n floatTextures: true\n });\n }\n }\n\n /**\n *\n * @param input\n * @returns {*}\n */\n runInput(input) {\n let output;\n this.outputs[0] = input;\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n this.outputs[layer] = this.forwardPropagate[layer](\n this.weights[layer], \n this.biases[layer], \n input\n );\n\n output = input = this.outputs[layer];\n }\n return output;\n }\n\n buildCalculateDeltas() {\n let calcDeltas = null;\n\n switch (this.activation) {\n case 'sigmoid':\n calcDeltas = calcDeltasSigmoid;\n break;\n case 'relu':\n calcDeltas = calcDeltasRelu;\n break;\n case 'leaky-relu':\n calcDeltas = calcDeltasLeakyRelu;\n break;\n case 'tanh':\n calcDeltas = calcDeltasTanh;\n break;\n default:\n throw new Error('unknown activation ' + this.activation);\n }\n\n for (let layer = this.outputLayer; layer > 0; layer--) {\n if (layer === this.outputLayer) {\n this.backwardPropagate[layer] = this.gpu.createKernelMap({\n error: GPU.alias('calcErrorOutput', calcErrorOutput),\n deltas: GPU.alias('calcDeltas', calcDeltas)\n }, function(outputs, targets) {\n const output = outputs[this.thread.x];\n return calcDeltas(calcErrorOutput(output, targets), output);\n }, {\n output: [this.sizes[layer]],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true,\n floatTextures: true\n });\n } else {\n this.backwardPropagate[layer] = this.gpu.createKernelMap({\n error: GPU.alias('calcError', calcError),\n deltas: GPU.alias('calcDeltas', calcDeltas),\n }, function(nextWeights, outputs, nextDeltas){\n let output = outputs[this.thread.x];\n return calcDeltas(calcError(nextWeights, nextDeltas), output);\n }, {\n output: [this.sizes[layer]],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true,\n constants: {\n size: this.deltas[layer + 1].length\n },\n floatTextures: true\n });\n }\n }\n }\n\n calculateDeltas(target) {\n for (let layer = this.outputLayer; layer > 0; layer--) {\n let output;\n if (layer === this.outputLayer) {\n output = this.backwardPropagate[layer](\n this.outputs[layer],\n target);\n } else {\n output = this.backwardPropagate[layer](\n this.weights[layer + 1],\n this.outputs[layer],\n this.deltas[layer + 1],\n );\n }\n\n this.deltas[layer] = output.deltas;\n this.errors[layer] = output.error;\n }\n }\n\n buildGetChanges() {\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n this.changesPropagate[layer] = this.gpu.createKernelMap({\n weights: GPU.alias('addWeights', addWeights),\n changes: GPU.alias('calcChanges', calcChanges)\n },\n function(previousOutputs, deltas, weights, changes) {\n let change = calcChanges(\n changes,\n deltas,\n previousOutputs);\n\n return addWeights(change, weights);\n }, {\n output: [this.sizes[layer - 1], this.sizes[layer]],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true,\n constants:{\n size: this.outputs[layer - 1].length,\n learningRate: this.trainOpts.learningRate,\n momentum: this.trainOpts.momentum\n },\n floatTextures: true\n });\n } \n }\n \n getChanges() {\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n let output = this.changesPropagate[layer](\n this.outputs[layer - 1],\n this.deltas[layer],\n this.weights[layer],\n this.changes[layer]\n );\n this.changes[layer] = output.changes;\n this.weights[layer] = output.weights;\n }\n }\n\n buildChangeBiases() {\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n this.biasesPropagate[layer] = this.gpu.createKernel(addBiases, {\n output: [this.sizes[layer]],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true,\n constants: {\n learningRate: this.trainOpts.learningRate\n },\n floatTextures: true\n });\n }\n }\n\n changeBiases() {\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n this.biases[layer] = this.biasesPropagate[layer](\n this.biases[layer],\n this.deltas[layer]\n );\n }\n }\n\n buildGetMSE() {\n this.getMSE = this.gpu.createKernel(mse, {\n output: [1],\n constants: {\n size: this.sizes[this.outputLayer]\n },\n floatTextures: true\n });\n }\n\n /**\n *\n * @param input\n * @returns {*}\n */\n run(input) {\n if (!this.isRunnable) return null;\n if (this.inputLookup) {\n input = lookup.toArray(this.inputLookup, input);\n }\n const inputTexture = this._texturizeInputData(input);\n const outputTextures = this.runInput(inputTexture);\n let output = outputTextures.toArray(this.gpu);\n\n if (this.outputLookup) {\n output = lookup.toHash(this.outputLookup, output);\n }\n return output;\n }\n\n\n /**\n *\n * @param data\n * Verifies network sizes are initilaized\n * If they are not it will initialize them based off the data set.\n */\n _verifyIsInitialized(data) {\n if (this.sizes) return;\n\n this.sizes = [];\n if (!data[0].size) {\n data[0].size = { input: data[0].input.length, output: data[0].output.length };\n }\n\n this.sizes.push(data[0].size.input);\n if (!this.hiddenSizes) {\n this.sizes.push(Math.max(3, Math.floor(data[0].size.input / 2)));\n } else {\n this.hiddenSizes.forEach(size => {\n this.sizes.push(size);\n });\n }\n this.sizes.push(data[0].size.output);\n\n this._initialize();\n }\n\n /**\n *\n * @param data\n * @returns {*}\n */\n _formatData(data) {\n data = super._formatData(data);\n\n this._texturizeInputData = this.gpu.createKernel(function(value) {\n return value[this.thread.x];\n }, {\n output: [data[0].input.length],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true\n });\n const texturizeOutputData = this.gpu.createKernel(function(value) {\n return value[this.thread.x];\n }, {\n output: [data[0].output.length],\n outputToTexture: true,\n outputImmutable: true,\n hardcodeConstants: true\n });\n data.forEach(d => {\n d.size = {input: d.input.length, output: d.output.length};\n d.input = this._texturizeInputData(d.input);\n d.output = texturizeOutputData(d.output);\n });\n return data;\n }\n\n toFunction() {\n throw new Error('not implemented on NeuralNetworkGPU');\n }\n\n}\n\nfunction weightedSumSigmoid(weights, biases, inputs) {\n let sum = biases[this.thread.x];\n for (let k = 0; k < this.constants.size; k++) {\n sum += weights[this.thread.x][k] * inputs[k];\n }\n //sigmoid\n return 1 / (1 + Math.exp(-sum));\n}\n\nfunction weightedSumRelu(weights, biases, inputs) {\n let sum = biases[this.thread.x];\n for (let k = 0; k < this.constants.size; k++) {\n sum += weights[this.thread.x][k] * inputs[k];\n }\n //relu\n return (sum < 0 ? 0 : sum);\n}\n\nfunction weightedSumLeakyRelu(weights, biases, inputs) {\n let sum = biases[this.thread.x];\n for (let k = 0; k < this.constants.size; k++) {\n sum += weights[this.thread.x][k] * inputs[k];\n }\n //leaky relu\n return (sum < 0 ? 0 : 0.01 * sum);\n}\n\nfunction weightedSumTanh(weights, biases, inputs) {\n let sum = biases[this.thread.x];\n for (let k = 0; k < this.constants.size; k++) {\n sum += weights[this.thread.x][k] * inputs[k];\n }\n //tanh\n return Math.tanh(sum);\n}\n\nfunction calcErrorOutput(output, targets) {\n return targets[this.thread.x] - output;\n}\n\nfunction calcDeltasSigmoid(error, output) {\n //sigmoid derivative\n return error * output * (1 - output);\n}\n\nfunction calcDeltasRelu(error, output) {\n //relu derivative\n return output > 0 ? error : 0;\n}\n\nfunction calcDeltasLeakyRelu(error, output) {\n //leaky relu derivative\n return output > 0 ? error : 0.01 * error;\n}\n\nfunction calcDeltasTanh(error, output) {\n //tanh derivative\n return (1 - output * output) * error;\n}\n\nfunction calcError(nextWeights, nextDeltas){\n let error = 0;\n for(let k = 0; k < this.constants.size; k++){\n error += nextDeltas[k] * nextWeights[k][this.thread.x];\n }\n return error;\n}\n\nfunction calcChanges(\n previousChanges,\n deltas,\n previousOutputs\n) {\n return (this.constants.learningRate * deltas[this.thread.y] * previousOutputs[this.thread.x])\n + (this.constants.momentum * previousChanges[this.thread.y][this.thread.x]);\n}\n\nfunction addWeights(change, weights){\n return change + weights[this.thread.y][this.thread.x];\n}\n\nfunction addBiases(biases, deltas){\n return biases[this.thread.x] + (deltas[this.thread.x] * this.constants.learningRate);\n}\n\n// mean squared error, reimplemented for GPU\nfunction mse(errors) {\n let sum = 0;\n for (let i = 0; i < this.constants.size; i++) {\n sum += Math.pow(errors[i], 2);\n }\n return sum / this.constants.size;\n}"]} \ No newline at end of file diff --git a/dist/neural-network.js b/dist/neural-network.js index 930fafd0a..8bb883ee4 100644 --- a/dist/neural-network.js +++ b/dist/neural-network.js @@ -434,14 +434,14 @@ var NeuralNetwork = function () { status.iterations++; if (this.trainOpts.log && status.iterations % this.trainOpts.logPeriod === 0) { - status.error = this._calculateTrainingError(data); + // status.error = this._calculateTrainingError(data); this.trainOpts.log('iterations: ' + status.iterations + ', training error: ' + status.error); } else { - if (status.iterations % 100 === 0) { - status.error = this._calculateTrainingError(data); - } else { - this._trainPatterns(data); - } + // if (status.iterations % 100 === 0) { + // status.error = this._calculateTrainingError(data); + // } else { + this._trainPatterns(data); + // } } if (this.trainOpts.callback && status.iterations % this.trainOpts.callbackPeriod === 0) { diff --git a/dist/neural-network.js.map b/dist/neural-network.js.map index 43f470ea0..49c6a5f63 100644 --- a/dist/neural-network.js.map +++ b/dist/neural-network.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/neural-network.js"],"names":["NeuralNetwork","iterations","errorThresh","log","logPeriod","learningRate","momentum","callback","callbackPeriod","timeout","Infinity","binaryThresh","hiddenLayers","activation","options","Object","assign","constructor","defaults","hiddenSizes","trainOpts","_updateTrainingOptions","trainDefaults","sizes","outputLayer","biases","weights","outputs","deltas","changes","errors","prototype","hasOwnProperty","runInput","calculateDeltas","Error","length","layer","size","Array","node","prevSize","setActivation","_runInputSigmoid","_calculateDeltasSigmoid","_runInputRelu","_calculateDeltasRelu","_runInputLeakyRelu","_calculateDeltasLeakyRelu","_runInputTanh","_calculateDeltasTanh","input","isRunnable","inputLookup","toArray","output","outputLookup","toHash","sum","k","Math","exp","tanh","data","push","max","floor","forEach","_initialize","opts","keys","opt","_setLogMethod","reduce","console","i","_trainPattern","status","endTime","error","Date","now","_calculateTrainingError","_trainPatterns","_formatData","_verifyIsInitialized","_prepTraining","_trainingTick","Promise","resolve","reject","thawedTrain","delay","each","stop","done","tick","trainError","target","logErrorRate","_adjustWeights","incoming","delta","change","isArray","tmp","datum","Float32Array","buildLookup","map","value","array","isBinary","falsePos","falseNeg","truePos","trueNeg","misclasses","actual","expected","indexOf","misclass","stats","total","precision","recall","accuracy","layers","nodes","j","bias","index","_getTrainOptsJSON","json","lookupFromHash","nodeHandle","layerNumber","nodeKey","result","w","join","toJSON","layersAsMath","key","Function","neuralNetwork","trainStream","checkFns","filter","c"],"mappings":";;;;;;;;AAAA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;;;;;;;AAEA;;;;IAIqBA,a;;;wBACQ;AACzB,aAAO;AACLC,oBAAY,KADP,EACiB;AACtBC,qBAAa,KAFR,EAEiB;AACtBC,aAAK,KAHA,EAGiB;AACtBC,mBAAW,EAJN,EAIiB;AACtBC,sBAAc,GALT,EAKiB;AACtBC,kBAAU,GANL,EAMiB;AACtBC,kBAAU,IAPL,EAOiB;AACtBC,wBAAgB,EARX,EAQiB;AACtBC,iBAASC,QATJ,CASiB;AATjB,OAAP;AAWD;;;wBAEqB;AACpB,aAAO;AACLC,sBAAc,GADT,EACkB;AACvBC,sBAAc,CAAC,CAAD,CAFT,EAEkB;AACvBC,oBAAY,SAHP,CAGkB;AAHlB,OAAP;AAKD;;;AAED,2BAA0B;AAAA,QAAdC,OAAc,uEAAJ,EAAI;;AAAA;;AACxBC,WAAOC,MAAP,CAAc,IAAd,EAAoB,KAAKC,WAAL,CAAiBC,QAArC,EAA+CJ,OAA/C;AACA,SAAKK,WAAL,GAAmBL,QAAQF,YAA3B;AACA,SAAKQ,SAAL,GAAiB,EAAjB;AACA,SAAKC,sBAAL,CAA4BN,OAAOC,MAAP,CAAc,EAAd,EAAkB,KAAKC,WAAL,CAAiBK,aAAnC,EAAkDR,OAAlD,CAA5B;;AAEA,SAAKS,KAAL,GAAa,IAAb;AACA,SAAKC,WAAL,GAAmB,IAAnB;AACA,SAAKC,MAAL,GAAc,IAAd,CARwB,CAQJ;AACpB,SAAKC,OAAL,GAAe,IAAf;AACA,SAAKC,OAAL,GAAe,IAAf;;AAEA;AACA,SAAKC,MAAL,GAAc,IAAd;AACA,SAAKC,OAAL,GAAe,IAAf,CAdwB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f;AACAA,WAAK4E,aAAL,GAAqB,IAArB;AACA,WAAKvG,aAAL;AACA,WAAKwG,WAAL,GAAmB,0BAAgB7E,IAAhB,CAAnB;AACA,aAAO,KAAK6E,WAAZ;AACD;;;wBA3vBe;AAAA;;AACd,UAAG,CAAC,KAAKjH,QAAT,EAAkB;AAChByC,gBAAQK,KAAR,CAAc,oEAAd;AACA,eAAO,KAAP;AACD;;AAED,UAAMoE,WAAW,CACf,OADe,EAEf,aAFe,EAGf,QAHe,EAIf,SAJe,EAKf,SALe,EAMf,QANe,EAOf,SAPe,EAQf,QARe,EASfC,MATe,CASR;AAAA,eAAK,OAAKC,CAAL,MAAY,IAAjB;AAAA,OATQ,CAAjB;;AAWA,UAAGF,SAAS/G,MAAT,GAAkB,CAArB,EAAuB;AACrBsC,gBAAQK,KAAR,iGAA4GoE,SAASP,IAAT,CAAc,IAAd,CAA5G;AACA,eAAO,KAAP;AACD;AACD,aAAO,IAAP;AACD;;;;;;kBA7IkB5I,a","file":"neural-network.js","sourcesContent":["import lookup from './lookup';\nimport TrainStream from './train-stream';\nimport max from './utilities/max';\nimport mse from './utilities/mse';\nimport randos from './utilities/randos';\nimport range from './utilities/range';\nimport toArray from './utilities/to-array';\nimport zeros from './utilities/zeros';\nimport Thaw from 'thaw.js';\n\n/**\n * @param {object} options\n * @constructor\n */\nexport default class NeuralNetwork {\n static get trainDefaults() {\n return {\n iterations: 20000, // the maximum times to iterate the training data\n errorThresh: 0.005, // the acceptable error percentage from training data\n log: false, // true to use console.log, when a function is supplied it is used\n logPeriod: 10, // iterations between logging out\n learningRate: 0.3, // multiply's against the input and the delta then adds to momentum\n momentum: 0.1, // multiply's against the specified \"change\" then adds to learning rate for change\n callback: null, // a periodic call back that can be triggered while training\n callbackPeriod: 10, // the number of iterations through the training data between callback calls\n timeout: Infinity // the max number of milliseconds to train for\n };\n }\n\n static get defaults() {\n return {\n binaryThresh: 0.5, // ¯\\_(ツ)_/¯\n hiddenLayers: [3], // array of ints for the sizes of the hidden layers in the network\n activation: 'sigmoid' // Supported activation types ['sigmoid', 'relu', 'leaky-relu', 'tanh']\n };\n }\n\n constructor(options = {}) {\n Object.assign(this, this.constructor.defaults, options);\n this.hiddenSizes = options.hiddenLayers;\n this.trainOpts = {};\n this._updateTrainingOptions(Object.assign({}, this.constructor.trainDefaults, options));\n\n this.sizes = null;\n this.outputLayer = null;\n this.biases = null; // weights for bias nodes\n this.weights = null;\n this.outputs = null;\n\n // state for training\n this.deltas = null;\n this.changes = null; // for momentum\n this.errors = null;\n\n if (!this.constructor.prototype.hasOwnProperty('runInput')) {\n this.runInput = null;\n }\n if (!this.constructor.prototype.hasOwnProperty('calculateDeltas')) {\n this.calculateDeltas = null;\n }\n }\n\n /**\n *\n * Expects this.sizes to have been set\n */\n _initialize() {\n if (!this.sizes) throw new Error ('Sizes must be set before initializing')\n\n this.outputLayer = this.sizes.length - 1;\n this.biases = []; // weights for bias nodes\n this.weights = [];\n this.outputs = [];\n\n // state for training\n this.deltas = [];\n this.changes = []; // for momentum\n this.errors = [];\n\n for (let layer = 0; layer <= this.outputLayer; layer++) {\n let size = this.sizes[layer];\n this.deltas[layer] = zeros(size);\n this.errors[layer] = zeros(size);\n this.outputs[layer] = zeros(size);\n\n if (layer > 0) {\n this.biases[layer] = randos(size);\n this.weights[layer] = new Array(size);\n this.changes[layer] = new Array(size);\n\n for (let node = 0; node < size; node++) {\n let prevSize = this.sizes[layer - 1];\n this.weights[layer][node] = randos(prevSize);\n this.changes[layer][node] = zeros(prevSize);\n }\n }\n }\n\n this.setActivation();\n }\n\n /**\n *\n * @param supported input: ['sigmoid', 'relu', 'leaky-relu', 'tanh']\n */\n setActivation(activation) {\n this.activation = (activation) ? activation : this.activation;\n switch (this.activation) {\n case 'sigmoid':\n this.runInput = this.runInput || this._runInputSigmoid;\n this.calculateDeltas = this.calculateDeltas || this._calculateDeltasSigmoid;\n break;\n case 'relu':\n this.runInput = this.runInput || this._runInputRelu;\n this.calculateDeltas = this.calculateDeltas || this._calculateDeltasRelu;\n break;\n case 'leaky-relu':\n this.runInput = this.runInput || this._runInputLeakyRelu;\n this.calculateDeltas = this.calculateDeltas || this._calculateDeltasLeakyRelu;\n break;\n case 'tanh':\n this.runInput = this.runInput || this._runInputTanh;\n this.calculateDeltas = this.calculateDeltas || this._calculateDeltasTanh;\n break;\n default:\n throw new Error('unknown activation ' + this.activation + ', The activation should be one of [\\'sigmoid\\', \\'relu\\', \\'leaky-relu\\', \\'tanh\\']');\n }\n }\n\n /**\n *\n * @returns boolean\n */\n get isRunnable(){\n if(!this.runInput){\n console.error('Activation function has not been initialized, did you run train()?');\n return false;\n }\n\n const checkFns = [\n 'sizes',\n 'outputLayer',\n 'biases',\n 'weights',\n 'outputs',\n 'deltas',\n 'changes',\n 'errors',\n ].filter(c => this[c] === null);\n\n if(checkFns.length > 0){\n console.error(`Some settings have not been initialized correctly, did you run train()? Found issues with: ${checkFns.join(', ')}`);\n return false;\n }\n return true;\n }\n\n\n /**\n *\n * @param input\n * @returns {*}\n */\n run(input) {\n if (!this.isRunnable) return null;\n if (this.inputLookup) {\n input = lookup.toArray(this.inputLookup, input);\n }\n\n let output = [...this.runInput(input)];\n\n if (this.outputLookup) {\n output = lookup.toHash(this.outputLookup, output);\n }\n return output;\n }\n\n /**\n * trains via sigmoid\n * @param input\n * @returns {*}\n */\n _runInputSigmoid(input) {\n this.outputs[0] = input; // set output state of input layer\n\n let output = null;\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let weights = this.weights[layer][node];\n\n let sum = this.biases[layer][node];\n for (let k = 0; k < weights.length; k++) {\n sum += weights[k] * input[k];\n }\n //sigmoid\n this.outputs[layer][node] = 1 / (1 + Math.exp(-sum));\n }\n output = input = this.outputs[layer];\n }\n return output;\n }\n\n _runInputRelu(input) {\n this.outputs[0] = input; // set output state of input layer\n\n let output = null;\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let weights = this.weights[layer][node];\n\n let sum = this.biases[layer][node];\n for (let k = 0; k < weights.length; k++) {\n sum += weights[k] * input[k];\n }\n //relu\n this.outputs[layer][node] = (sum < 0 ? 0 : sum);\n }\n output = input = this.outputs[layer];\n }\n return output;\n }\n\n _runInputLeakyRelu(input) {\n this.outputs[0] = input; // set output state of input layer\n\n let output = null;\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let weights = this.weights[layer][node];\n\n let sum = this.biases[layer][node];\n for (let k = 0; k < weights.length; k++) {\n sum += weights[k] * input[k];\n }\n //leaky relu\n this.outputs[layer][node] = (sum < 0 ? 0 : 0.01 * sum);\n }\n output = input = this.outputs[layer];\n }\n return output;\n }\n\n _runInputTanh(input) {\n this.outputs[0] = input; // set output state of input layer\n\n let output = null;\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let weights = this.weights[layer][node];\n\n let sum = this.biases[layer][node];\n for (let k = 0; k < weights.length; k++) {\n sum += weights[k] * input[k];\n }\n //tanh\n this.outputs[layer][node] = Math.tanh(sum);\n }\n output = input = this.outputs[layer];\n }\n return output;\n }\n\n /**\n *\n * @param data\n * Verifies network sizes are initilaized\n * If they are not it will initialize them based off the data set.\n */\n _verifyIsInitialized(data) {\n if (this.sizes) return;\n\n this.sizes = [];\n this.sizes.push(data[0].input.length);\n if (!this.hiddenSizes) {\n this.sizes.push(Math.max(3, Math.floor(data[0].input.length / 2)));\n } else {\n this.hiddenSizes.forEach(size => {\n this.sizes.push(size);\n });\n }\n this.sizes.push(data[0].output.length)\n\n this._initialize();\n }\n\n /**\n *\n * @param options\n * Supports all `trainDefaults` properties\n * also supports:\n * learningRate: (number),\n * momentum: (number),\n * activation: ['sigmoid', 'relu', 'leaky-relu', 'tanh']\n */\n _updateTrainingOptions(opts) {\n Object.keys(NeuralNetwork.trainDefaults).forEach(opt => this.trainOpts[opt] = opts[opt] || this.trainOpts[opt]);\n this._setLogMethod(opts.log || this.trainOpts.log);\n this.activation = opts.activation || this.activation;\n }\n\n /**\n *\n * Gets JSON of trainOpts object\n * NOTE: Activation is stored directly on JSON object and not in the training options\n */\n _getTrainOptsJSON() {\n return Object.keys(NeuralNetwork.trainDefaults)\n .reduce((opts, opt) => {\n if (this.trainOpts[opt]) opts[opt] = this.trainOpts[opt];\n if (opt === 'log') opts.log = typeof opts.log === 'function';\n return opts;\n }, {});\n }\n\n /**\n *\n * @param log\n * if a method is passed in method is used\n * if false passed in nothing is logged\n * @returns error\n */\n _setLogMethod(log) {\n if (typeof log === 'function'){\n this.trainOpts.log = log;\n } else if (log) {\n this.trainOpts.log = console.log;\n } else {\n this.trainOpts.log = false;\n }\n }\n\n /**\n *\n * @param data\n * @returns error\n */\n _calculateTrainingError(data) {\n let sum = 0;\n for (let i = 0; i < data.length; ++i) {\n sum += this._trainPattern(data[i].input, data[i].output, true);\n }\n return sum / data.length;\n }\n\n /**\n * @param data\n * @private\n */\n _trainPatterns(data) {\n for (let i = 0; i < data.length; ++i) {\n this._trainPattern(data[i].input, data[i].output, false);\n }\n }\n\n /**\n *\n * @param status { iterations: number, error: number}\n * @param options\n */\n _trainingTick(data, status, endTime) {\n if (status.iterations >= this.trainOpts.iterations || status.error <= this.trainOpts.errorThresh || Date.now() >= endTime) {\n return false;\n }\n\n status.iterations++;\n\n if (this.trainOpts.log && (status.iterations % this.trainOpts.logPeriod === 0)) {\n status.error = this._calculateTrainingError(data);\n this.trainOpts.log(`iterations: ${status.iterations}, training error: ${status.error}`);\n } else {\n if (status.iterations % 100 === 0) {\n status.error = this._calculateTrainingError(data);\n } else {\n this._trainPatterns(data);\n }\n }\n\n if (this.trainOpts.callback && (status.iterations % this.trainOpts.callbackPeriod === 0)) {\n this.trainOpts.callback(Object.assign(status));\n }\n return true;\n }\n\n /**\n *\n * @param data\n * @param options\n * @protected\n * @return {{runTrainingTick: function, status: {error: number, iterations: number}}}\n */\n _prepTraining(data, options) {\n this._updateTrainingOptions(options);\n data = this._formatData(data);\n const endTime = Date.now() + this.trainOpts.timeout;\n\n const status = {\n error: 1,\n iterations: 0\n };\n\n this._verifyIsInitialized(data);\n\n return {\n data,\n status,\n endTime\n }\n }\n\n /**\n *\n * @param data\n * @param options\n * @returns {{error: number, iterations: number}}\n */\n train(data, options = {}) {\n let status, endTime;\n ({ data, status, endTime } = this._prepTraining(data, options));\n\n while(this._trainingTick(data, status, endTime));\n return status;\n }\n\n /**\n *\n * @param data\n * @param options\n * @returns {Promise}\n * @resolves {{error: number, iterations: number}}\n * @rejects {{trainError: string, status: {error: number, iterations: number}}\n */\n trainAsync(data, options = {}) {\n let status, endTime;\n ({ data, status, endTime } = this._prepTraining(data, options));\n\n return new Promise((resolve, reject) => {\n try {\n const thawedTrain = new Thaw(new Array(this.trainOpts.iterations), {\n delay: true,\n each: () => this._trainingTick(data, status, endTime) || thawedTrain.stop(),\n done: () => resolve(status)\n });\n thawedTrain.tick();\n } catch (trainError) {\n reject({trainError, status});\n }\n });\n }\n\n /**\n *\n * @param input\n * @param target\n */\n _trainPattern(input, target, logErrorRate) {\n\n // forward propagate\n this.runInput(input);\n\n // back propagate\n this.calculateDeltas(target);\n this._adjustWeights();\n\n if (logErrorRate) {\n return mse(this.errors[this.outputLayer]);\n } else {\n return null;\n }\n }\n\n /**\n *\n * @param target\n */\n _calculateDeltasSigmoid(target) {\n for (let layer = this.outputLayer; layer >= 0; layer--) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let output = this.outputs[layer][node];\n\n let error = 0;\n if (layer === this.outputLayer) {\n error = target[node] - output;\n }\n else {\n let deltas = this.deltas[layer + 1];\n for (let k = 0; k < deltas.length; k++) {\n error += deltas[k] * this.weights[layer + 1][k][node];\n }\n }\n this.errors[layer][node] = error;\n this.deltas[layer][node] = error * output * (1 - output);\n }\n }\n }\n\n /**\n *\n * @param target\n */\n _calculateDeltasRelu(target) {\n for (let layer = this.outputLayer; layer >= 0; layer--) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let output = this.outputs[layer][node];\n\n let error = 0;\n if (layer === this.outputLayer) {\n error = target[node] - output;\n }\n else {\n let deltas = this.deltas[layer + 1];\n for (let k = 0; k < deltas.length; k++) {\n error += deltas[k] * this.weights[layer + 1][k][node];\n }\n }\n this.errors[layer][node] = error;\n this.deltas[layer][node] = output > 0 ? error : 0;\n }\n }\n }\n\n /**\n *\n * @param target\n */\n _calculateDeltasLeakyRelu(target) {\n for (let layer = this.outputLayer; layer >= 0; layer--) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let output = this.outputs[layer][node];\n\n let error = 0;\n if (layer === this.outputLayer) {\n error = target[node] - output;\n }\n else {\n let deltas = this.deltas[layer + 1];\n for (let k = 0; k < deltas.length; k++) {\n error += deltas[k] * this.weights[layer + 1][k][node];\n }\n }\n this.errors[layer][node] = error;\n this.deltas[layer][node] = output > 0 ? error : 0.01 * error;\n }\n }\n }\n\n /**\n *\n * @param target\n */\n _calculateDeltasTanh(target) {\n for (let layer = this.outputLayer; layer >= 0; layer--) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let output = this.outputs[layer][node];\n\n let error = 0;\n if (layer === this.outputLayer) {\n error = target[node] - output;\n }\n else {\n let deltas = this.deltas[layer + 1];\n for (let k = 0; k < deltas.length; k++) {\n error += deltas[k] * this.weights[layer + 1][k][node];\n }\n }\n this.errors[layer][node] = error;\n this.deltas[layer][node] = (1 - output * output) * error;\n }\n }\n }\n\n /**\n *\n * Changes weights of networks\n */\n _adjustWeights() {\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n let incoming = this.outputs[layer - 1];\n\n for (let node = 0; node < this.sizes[layer]; node++) {\n let delta = this.deltas[layer][node];\n\n for (let k = 0; k < incoming.length; k++) {\n let change = this.changes[layer][node][k];\n\n change = (this.trainOpts.learningRate * delta * incoming[k])\n + (this.trainOpts.momentum * change);\n\n this.changes[layer][node][k] = change;\n this.weights[layer][node][k] += change;\n }\n this.biases[layer][node] += this.trainOpts.learningRate * delta;\n }\n }\n }\n\n /**\n *\n * @param data\n * @returns {*}\n */\n _formatData(data) {\n if (!Array.isArray(data)) { // turn stream datum into array\n let tmp = [];\n tmp.push(data);\n data = tmp;\n }\n // turn sparse hash input into arrays with 0s as filler\n let datum = data[0].input;\n if (!Array.isArray(datum) && !(datum instanceof Float32Array)) {\n if (!this.inputLookup) {\n this.inputLookup = lookup.buildLookup(data.map(value => value['input']));\n }\n data = data.map(datum => {\n let array = lookup.toArray(this.inputLookup, datum.input);\n return Object.assign({}, datum, { input: array });\n }, this);\n }\n\n if (!Array.isArray(data[0].output)) {\n if (!this.outputLookup) {\n this.outputLookup = lookup.buildLookup(data.map(value => value['output']));\n }\n data = data.map(datum => {\n let array = lookup.toArray(this.outputLookup, datum.output);\n return Object.assign({}, datum, { output: array });\n }, this);\n }\n return data;\n }\n\n /**\n *\n * @param data\n * @returns {\n * {\n * error: number,\n * misclasses: Array\n * }\n * }\n */\n test(data) {\n data = this._formatData(data);\n\n // for binary classification problems with one output node\n let isBinary = data[0].output.length === 1;\n let falsePos = 0;\n let falseNeg = 0;\n let truePos = 0;\n let trueNeg = 0;\n\n // for classification problems\n let misclasses = [];\n\n // run each pattern through the trained network and collect\n // error and misclassification statistics\n let sum = 0;\n for (let i = 0; i < data.length; i++) {\n let output = this.runInput(data[i].input);\n let target = data[i].output;\n\n let actual, expected;\n if (isBinary) {\n actual = output[0] > this.binaryThresh ? 1 : 0;\n expected = target[0];\n }\n else {\n actual = output.indexOf(max(output));\n expected = target.indexOf(max(target));\n }\n\n if (actual !== expected) {\n let misclass = data[i];\n Object.assign(misclass, {\n actual: actual,\n expected: expected\n });\n misclasses.push(misclass);\n }\n\n if (isBinary) {\n if (actual === 0 && expected === 0) {\n trueNeg++;\n } else if (actual === 1 && expected === 1) {\n truePos++;\n } else if (actual === 0 && expected === 1) {\n falseNeg++;\n } else if (actual === 1 && expected === 0) {\n falsePos++;\n }\n }\n\n let errors = output.map((value, i) => {\n return target[i] - value;\n });\n sum += mse(errors);\n }\n let error = sum / data.length;\n\n let stats = {\n error: error,\n misclasses: misclasses\n };\n\n if (isBinary) {\n Object.assign(stats, {\n trueNeg: trueNeg,\n truePos: truePos,\n falseNeg: falseNeg,\n falsePos: falsePos,\n total: data.length,\n precision: truePos / (truePos + falsePos),\n recall: truePos / (truePos + falseNeg),\n accuracy: (trueNeg + truePos) / data.length\n });\n }\n return stats;\n }\n\n /**\n *\n * @returns\n * {\n * layers: [\n * {\n * x: {},\n * y: {}\n * },\n * {\n * '0': {\n * bias: -0.98771313,\n * weights: {\n * x: 0.8374838,\n * y: 1.245858\n * },\n * '1': {\n * bias: 3.48192004,\n * weights: {\n * x: 1.7825821,\n * y: -2.67899\n * }\n * }\n * },\n * {\n * f: {\n * bias: 0.27205739,\n * weights: {\n * '0': 1.3161821,\n * '1': 2.00436\n * }\n * }\n * }\n * ]\n * }\n */\n toJSON() {\n let layers = [];\n for (let layer = 0; layer <= this.outputLayer; layer++) {\n layers[layer] = {};\n\n let nodes;\n // turn any internal arrays back into hashes for readable json\n if (layer === 0 && this.inputLookup) {\n nodes = Object.keys(this.inputLookup);\n }\n else if (layer === this.outputLayer && this.outputLookup) {\n nodes = Object.keys(this.outputLookup);\n }\n else {\n nodes = range(0, this.sizes[layer]);\n }\n\n for (let j = 0; j < nodes.length; j++) {\n let node = nodes[j];\n layers[layer][node] = {};\n\n if (layer > 0) {\n layers[layer][node].bias = this.biases[layer][j];\n layers[layer][node].weights = {};\n for (let k in layers[layer - 1]) {\n let index = k;\n if (layer === 1 && this.inputLookup) {\n index = this.inputLookup[k];\n }\n layers[layer][node].weights[k] = this.weights[layer][j][index];\n }\n }\n }\n }\n return {\n sizes: this.sizes,\n layers,\n outputLookup:!!this.outputLookup,\n inputLookup:!!this.inputLookup,\n activation: this.activation,\n trainOpts: this._getTrainOptsJSON()\n };\n }\n\n /**\n *\n * @param json\n * @returns {NeuralNetwork}\n */\n fromJSON (json) {\n this.sizes = json.sizes\n this._initialize();\n\n for (let i = 0; i <= this.outputLayer; i++) {\n let layer = json.layers[i];\n if (i === 0 && (!layer[0] || json.inputLookup)) {\n this.inputLookup = lookup.lookupFromHash(layer);\n }\n else if (i === this.outputLayer && (!layer[0] || json.outputLookup)) {\n this.outputLookup = lookup.lookupFromHash(layer);\n }\n if (i > 0) {\n const nodes = Object.keys(layer);\n this.sizes[i] = nodes.length;\n for (let j in nodes) {\n const node = nodes[j];\n this.biases[i][j] = layer[node].bias;\n this.weights[i][j] = toArray(layer[node].weights);\n }\n }\n }\n this._updateTrainingOptions(json.trainOpts)\n this.setActivation();\n return this;\n }\n\n /**\n *\n * @returns {Function}\n */\n toFunction() {\n const activation = this.activation;\n function nodeHandle(layers, layerNumber, nodeKey) {\n if (layerNumber === 0) {\n return (typeof nodeKey === 'string'\n ? `input['${nodeKey}']`\n : `input[${nodeKey}]`);\n }\n\n const layer = layers[layerNumber];\n const node = layer[nodeKey];\n let result = [node.bias];\n for (let w in node.weights) {\n if (node.weights[w] < 0) {\n result.push(`${node.weights[w]}*(${nodeHandle(layers, layerNumber - 1, w)})`);\n } else {\n result.push(`+${node.weights[w]}*(${nodeHandle(layers, layerNumber - 1, w)})`);\n }\n }\n\n switch (activation) {\n case 'sigmoid':\n return `1/(1+1/Math.exp(${result.join('')}))`;\n case 'relu':\n return `var sum = ${result.join('')};(sum < 0 ? 0 : sum);`;\n case 'leaky-relu':\n return `var sum = ${result.join('')};(sum < 0 ? 0 : 0.01 * sum);`;\n case 'tanh':\n return `Math.tanh(${result.join('')});`;\n default:\n throw new Error('unknown activation type ' + activation);\n }\n }\n\n const layers = this.toJSON().layers;\n const layersAsMath = [];\n let result;\n for (let i in layers[layers.length - 1]) {\n layersAsMath.push(nodeHandle(layers, layers.length - 1, i));\n }\n if (this.outputLookup) {\n result = `{${\n Object.keys(this.outputLookup)\n .map((key, i) => `'${key}':${layersAsMath[i]}`)\n }}`;\n } else {\n result = `[${layersAsMath.join(',')}]`;\n }\n return new Function('input', `return ${result}`);\n }\n\n /**\n * This will create a TrainStream (WriteStream) for us to send the training data to.\n * @param opts training options\n * @returns {TrainStream|*}\n */\n createTrainStream(opts) {\n opts = opts || {};\n opts.neuralNetwork = this;\n this.setActivation();\n this.trainStream = new TrainStream(opts);\n return this.trainStream;\n }\n}"]} \ No newline at end of file +{"version":3,"sources":["../src/neural-network.js"],"names":["NeuralNetwork","iterations","errorThresh","log","logPeriod","learningRate","momentum","callback","callbackPeriod","timeout","Infinity","binaryThresh","hiddenLayers","activation","options","Object","assign","constructor","defaults","hiddenSizes","trainOpts","_updateTrainingOptions","trainDefaults","sizes","outputLayer","biases","weights","outputs","deltas","changes","errors","prototype","hasOwnProperty","runInput","calculateDeltas","Error","length","layer","size","Array","node","prevSize","setActivation","_runInputSigmoid","_calculateDeltasSigmoid","_runInputRelu","_calculateDeltasRelu","_runInputLeakyRelu","_calculateDeltasLeakyRelu","_runInputTanh","_calculateDeltasTanh","input","isRunnable","inputLookup","toArray","output","outputLookup","toHash","sum","k","Math","exp","tanh","data","push","max","floor","forEach","_initialize","opts","keys","opt","_setLogMethod","reduce","console","i","_trainPattern","status","endTime","error","Date","now","_trainPatterns","_formatData","_verifyIsInitialized","_prepTraining","_trainingTick","Promise","resolve","reject","thawedTrain","delay","each","stop","done","tick","trainError","target","logErrorRate","_adjustWeights","incoming","delta","change","isArray","tmp","datum","Float32Array","buildLookup","map","value","array","isBinary","falsePos","falseNeg","truePos","trueNeg","misclasses","actual","expected","indexOf","misclass","stats","total","precision","recall","accuracy","layers","nodes","j","bias","index","_getTrainOptsJSON","json","lookupFromHash","nodeHandle","layerNumber","nodeKey","result","w","join","toJSON","layersAsMath","key","Function","neuralNetwork","trainStream","checkFns","filter","c"],"mappings":";;;;;;;;AAAA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;AACA;;;;;;;;;;AAEA;;;;IAIqBA,a;;;wBACQ;AACzB,aAAO;AACLC,oBAAY,KADP,EACiB;AACtBC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AAQK,KAAR,iGAA4GmE,SAASP,IAAT,CAAc,IAAd,CAA5G;AACA,eAAO,KAAP;AACD;AACD,aAAO,IAAP;AACD;;;;;;kBA7IkB3I,a","file":"neural-network.js","sourcesContent":["import lookup from './lookup';\nimport TrainStream from './train-stream';\nimport max from './utilities/max';\nimport mse from './utilities/mse';\nimport randos from './utilities/randos';\nimport range from './utilities/range';\nimport toArray from './utilities/to-array';\nimport zeros from './utilities/zeros';\nimport Thaw from 'thaw.js';\n\n/**\n * @param {object} options\n * @constructor\n */\nexport default class NeuralNetwork {\n static get trainDefaults() {\n return {\n iterations: 20000, // the maximum times to iterate the training data\n errorThresh: 0.005, // the acceptable error percentage from training data\n log: false, // true to use console.log, when a function is supplied it is used\n logPeriod: 10, // iterations between logging out\n learningRate: 0.3, // multiply's against the input and the delta then adds to momentum\n momentum: 0.1, // multiply's against the specified \"change\" then adds to learning rate for change\n callback: null, // a periodic call back that can be triggered while training\n callbackPeriod: 10, // the number of iterations through the training data between callback calls\n timeout: Infinity // the max number of milliseconds to train for\n };\n }\n\n static get defaults() {\n return {\n binaryThresh: 0.5, // ¯\\_(ツ)_/¯\n hiddenLayers: [3], // array of ints for the sizes of the hidden layers in the network\n activation: 'sigmoid' // Supported activation types ['sigmoid', 'relu', 'leaky-relu', 'tanh']\n };\n }\n\n constructor(options = {}) {\n Object.assign(this, this.constructor.defaults, options);\n this.hiddenSizes = options.hiddenLayers;\n this.trainOpts = {};\n this._updateTrainingOptions(Object.assign({}, this.constructor.trainDefaults, options));\n\n this.sizes = null;\n this.outputLayer = null;\n this.biases = null; // weights for bias nodes\n this.weights = null;\n this.outputs = null;\n\n // state for training\n this.deltas = null;\n this.changes = null; // for momentum\n this.errors = null;\n\n if (!this.constructor.prototype.hasOwnProperty('runInput')) {\n this.runInput = null;\n }\n if (!this.constructor.prototype.hasOwnProperty('calculateDeltas')) {\n this.calculateDeltas = null;\n }\n }\n\n /**\n *\n * Expects this.sizes to have been set\n */\n _initialize() {\n if (!this.sizes) throw new Error ('Sizes must be set before initializing')\n\n this.outputLayer = this.sizes.length - 1;\n this.biases = []; // weights for bias nodes\n this.weights = [];\n this.outputs = [];\n\n // state for training\n this.deltas = [];\n this.changes = []; // for momentum\n this.errors = [];\n\n for (let layer = 0; layer <= this.outputLayer; layer++) {\n let size = this.sizes[layer];\n this.deltas[layer] = zeros(size);\n this.errors[layer] = zeros(size);\n this.outputs[layer] = zeros(size);\n\n if (layer > 0) {\n this.biases[layer] = randos(size);\n this.weights[layer] = new Array(size);\n this.changes[layer] = new Array(size);\n\n for (let node = 0; node < size; node++) {\n let prevSize = this.sizes[layer - 1];\n this.weights[layer][node] = randos(prevSize);\n this.changes[layer][node] = zeros(prevSize);\n }\n }\n }\n\n this.setActivation();\n }\n\n /**\n *\n * @param supported input: ['sigmoid', 'relu', 'leaky-relu', 'tanh']\n */\n setActivation(activation) {\n this.activation = (activation) ? activation : this.activation;\n switch (this.activation) {\n case 'sigmoid':\n this.runInput = this.runInput || this._runInputSigmoid;\n this.calculateDeltas = this.calculateDeltas || this._calculateDeltasSigmoid;\n break;\n case 'relu':\n this.runInput = this.runInput || this._runInputRelu;\n this.calculateDeltas = this.calculateDeltas || this._calculateDeltasRelu;\n break;\n case 'leaky-relu':\n this.runInput = this.runInput || this._runInputLeakyRelu;\n this.calculateDeltas = this.calculateDeltas || this._calculateDeltasLeakyRelu;\n break;\n case 'tanh':\n this.runInput = this.runInput || this._runInputTanh;\n this.calculateDeltas = this.calculateDeltas || this._calculateDeltasTanh;\n break;\n default:\n throw new Error('unknown activation ' + this.activation + ', The activation should be one of [\\'sigmoid\\', \\'relu\\', \\'leaky-relu\\', \\'tanh\\']');\n }\n }\n\n /**\n *\n * @returns boolean\n */\n get isRunnable(){\n if(!this.runInput){\n console.error('Activation function has not been initialized, did you run train()?');\n return false;\n }\n\n const checkFns = [\n 'sizes',\n 'outputLayer',\n 'biases',\n 'weights',\n 'outputs',\n 'deltas',\n 'changes',\n 'errors',\n ].filter(c => this[c] === null);\n\n if(checkFns.length > 0){\n console.error(`Some settings have not been initialized correctly, did you run train()? Found issues with: ${checkFns.join(', ')}`);\n return false;\n }\n return true;\n }\n\n\n /**\n *\n * @param input\n * @returns {*}\n */\n run(input) {\n if (!this.isRunnable) return null;\n if (this.inputLookup) {\n input = lookup.toArray(this.inputLookup, input);\n }\n\n let output = [...this.runInput(input)];\n\n if (this.outputLookup) {\n output = lookup.toHash(this.outputLookup, output);\n }\n return output;\n }\n\n /**\n * trains via sigmoid\n * @param input\n * @returns {*}\n */\n _runInputSigmoid(input) {\n this.outputs[0] = input; // set output state of input layer\n\n let output = null;\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let weights = this.weights[layer][node];\n\n let sum = this.biases[layer][node];\n for (let k = 0; k < weights.length; k++) {\n sum += weights[k] * input[k];\n }\n //sigmoid\n this.outputs[layer][node] = 1 / (1 + Math.exp(-sum));\n }\n output = input = this.outputs[layer];\n }\n return output;\n }\n\n _runInputRelu(input) {\n this.outputs[0] = input; // set output state of input layer\n\n let output = null;\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let weights = this.weights[layer][node];\n\n let sum = this.biases[layer][node];\n for (let k = 0; k < weights.length; k++) {\n sum += weights[k] * input[k];\n }\n //relu\n this.outputs[layer][node] = (sum < 0 ? 0 : sum);\n }\n output = input = this.outputs[layer];\n }\n return output;\n }\n\n _runInputLeakyRelu(input) {\n this.outputs[0] = input; // set output state of input layer\n\n let output = null;\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let weights = this.weights[layer][node];\n\n let sum = this.biases[layer][node];\n for (let k = 0; k < weights.length; k++) {\n sum += weights[k] * input[k];\n }\n //leaky relu\n this.outputs[layer][node] = (sum < 0 ? 0 : 0.01 * sum);\n }\n output = input = this.outputs[layer];\n }\n return output;\n }\n\n _runInputTanh(input) {\n this.outputs[0] = input; // set output state of input layer\n\n let output = null;\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let weights = this.weights[layer][node];\n\n let sum = this.biases[layer][node];\n for (let k = 0; k < weights.length; k++) {\n sum += weights[k] * input[k];\n }\n //tanh\n this.outputs[layer][node] = Math.tanh(sum);\n }\n output = input = this.outputs[layer];\n }\n return output;\n }\n\n /**\n *\n * @param data\n * Verifies network sizes are initilaized\n * If they are not it will initialize them based off the data set.\n */\n _verifyIsInitialized(data) {\n if (this.sizes) return;\n\n this.sizes = [];\n this.sizes.push(data[0].input.length);\n if (!this.hiddenSizes) {\n this.sizes.push(Math.max(3, Math.floor(data[0].input.length / 2)));\n } else {\n this.hiddenSizes.forEach(size => {\n this.sizes.push(size);\n });\n }\n this.sizes.push(data[0].output.length)\n\n this._initialize();\n }\n\n /**\n *\n * @param options\n * Supports all `trainDefaults` properties\n * also supports:\n * learningRate: (number),\n * momentum: (number),\n * activation: ['sigmoid', 'relu', 'leaky-relu', 'tanh']\n */\n _updateTrainingOptions(opts) {\n Object.keys(NeuralNetwork.trainDefaults).forEach(opt => this.trainOpts[opt] = opts[opt] || this.trainOpts[opt]);\n this._setLogMethod(opts.log || this.trainOpts.log);\n this.activation = opts.activation || this.activation;\n }\n\n /**\n *\n * Gets JSON of trainOpts object\n * NOTE: Activation is stored directly on JSON object and not in the training options\n */\n _getTrainOptsJSON() {\n return Object.keys(NeuralNetwork.trainDefaults)\n .reduce((opts, opt) => {\n if (this.trainOpts[opt]) opts[opt] = this.trainOpts[opt];\n if (opt === 'log') opts.log = typeof opts.log === 'function';\n return opts;\n }, {});\n }\n\n /**\n *\n * @param log\n * if a method is passed in method is used\n * if false passed in nothing is logged\n * @returns error\n */\n _setLogMethod(log) {\n if (typeof log === 'function'){\n this.trainOpts.log = log;\n } else if (log) {\n this.trainOpts.log = console.log;\n } else {\n this.trainOpts.log = false;\n }\n }\n\n /**\n *\n * @param data\n * @returns error\n */\n _calculateTrainingError(data) {\n let sum = 0;\n for (let i = 0; i < data.length; ++i) {\n sum += this._trainPattern(data[i].input, data[i].output, true);\n }\n return sum / data.length;\n }\n\n /**\n * @param data\n * @private\n */\n _trainPatterns(data) {\n for (let i = 0; i < data.length; ++i) {\n this._trainPattern(data[i].input, data[i].output, false);\n }\n }\n\n /**\n *\n * @param status { iterations: number, error: number}\n * @param options\n */\n _trainingTick(data, status, endTime) {\n if (status.iterations >= this.trainOpts.iterations || status.error <= this.trainOpts.errorThresh || Date.now() >= endTime) {\n return false;\n }\n\n status.iterations++;\n\n if (this.trainOpts.log && (status.iterations % this.trainOpts.logPeriod === 0)) {\n // status.error = this._calculateTrainingError(data);\n this.trainOpts.log(`iterations: ${status.iterations}, training error: ${status.error}`);\n } else {\n // if (status.iterations % 100 === 0) {\n // status.error = this._calculateTrainingError(data);\n // } else {\n this._trainPatterns(data);\n // }\n }\n\n if (this.trainOpts.callback && (status.iterations % this.trainOpts.callbackPeriod === 0)) {\n this.trainOpts.callback(Object.assign(status));\n }\n return true;\n }\n\n /**\n *\n * @param data\n * @param options\n * @protected\n * @return {{runTrainingTick: function, status: {error: number, iterations: number}}}\n */\n _prepTraining(data, options) {\n this._updateTrainingOptions(options);\n data = this._formatData(data);\n const endTime = Date.now() + this.trainOpts.timeout;\n\n const status = {\n error: 1,\n iterations: 0\n };\n\n this._verifyIsInitialized(data);\n\n return {\n data,\n status,\n endTime\n }\n }\n\n /**\n *\n * @param data\n * @param options\n * @returns {{error: number, iterations: number}}\n */\n train(data, options = {}) {\n let status, endTime;\n ({ data, status, endTime } = this._prepTraining(data, options));\n\n while(this._trainingTick(data, status, endTime));\n return status;\n }\n\n /**\n *\n * @param data\n * @param options\n * @returns {Promise}\n * @resolves {{error: number, iterations: number}}\n * @rejects {{trainError: string, status: {error: number, iterations: number}}\n */\n trainAsync(data, options = {}) {\n let status, endTime;\n ({ data, status, endTime } = this._prepTraining(data, options));\n\n return new Promise((resolve, reject) => {\n try {\n const thawedTrain = new Thaw(new Array(this.trainOpts.iterations), {\n delay: true,\n each: () => this._trainingTick(data, status, endTime) || thawedTrain.stop(),\n done: () => resolve(status)\n });\n thawedTrain.tick();\n } catch (trainError) {\n reject({trainError, status});\n }\n });\n }\n\n /**\n *\n * @param input\n * @param target\n */\n _trainPattern(input, target, logErrorRate) {\n\n // forward propagate\n this.runInput(input);\n\n // back propagate\n this.calculateDeltas(target);\n this._adjustWeights();\n\n if (logErrorRate) {\n return mse(this.errors[this.outputLayer]);\n } else {\n return null;\n }\n }\n\n /**\n *\n * @param target\n */\n _calculateDeltasSigmoid(target) {\n for (let layer = this.outputLayer; layer >= 0; layer--) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let output = this.outputs[layer][node];\n\n let error = 0;\n if (layer === this.outputLayer) {\n error = target[node] - output;\n }\n else {\n let deltas = this.deltas[layer + 1];\n for (let k = 0; k < deltas.length; k++) {\n error += deltas[k] * this.weights[layer + 1][k][node];\n }\n }\n this.errors[layer][node] = error;\n this.deltas[layer][node] = error * output * (1 - output);\n }\n }\n }\n\n /**\n *\n * @param target\n */\n _calculateDeltasRelu(target) {\n for (let layer = this.outputLayer; layer >= 0; layer--) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let output = this.outputs[layer][node];\n\n let error = 0;\n if (layer === this.outputLayer) {\n error = target[node] - output;\n }\n else {\n let deltas = this.deltas[layer + 1];\n for (let k = 0; k < deltas.length; k++) {\n error += deltas[k] * this.weights[layer + 1][k][node];\n }\n }\n this.errors[layer][node] = error;\n this.deltas[layer][node] = output > 0 ? error : 0;\n }\n }\n }\n\n /**\n *\n * @param target\n */\n _calculateDeltasLeakyRelu(target) {\n for (let layer = this.outputLayer; layer >= 0; layer--) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let output = this.outputs[layer][node];\n\n let error = 0;\n if (layer === this.outputLayer) {\n error = target[node] - output;\n }\n else {\n let deltas = this.deltas[layer + 1];\n for (let k = 0; k < deltas.length; k++) {\n error += deltas[k] * this.weights[layer + 1][k][node];\n }\n }\n this.errors[layer][node] = error;\n this.deltas[layer][node] = output > 0 ? error : 0.01 * error;\n }\n }\n }\n\n /**\n *\n * @param target\n */\n _calculateDeltasTanh(target) {\n for (let layer = this.outputLayer; layer >= 0; layer--) {\n for (let node = 0; node < this.sizes[layer]; node++) {\n let output = this.outputs[layer][node];\n\n let error = 0;\n if (layer === this.outputLayer) {\n error = target[node] - output;\n }\n else {\n let deltas = this.deltas[layer + 1];\n for (let k = 0; k < deltas.length; k++) {\n error += deltas[k] * this.weights[layer + 1][k][node];\n }\n }\n this.errors[layer][node] = error;\n this.deltas[layer][node] = (1 - output * output) * error;\n }\n }\n }\n\n /**\n *\n * Changes weights of networks\n */\n _adjustWeights() {\n for (let layer = 1; layer <= this.outputLayer; layer++) {\n let incoming = this.outputs[layer - 1];\n\n for (let node = 0; node < this.sizes[layer]; node++) {\n let delta = this.deltas[layer][node];\n\n for (let k = 0; k < incoming.length; k++) {\n let change = this.changes[layer][node][k];\n\n change = (this.trainOpts.learningRate * delta * incoming[k])\n + (this.trainOpts.momentum * change);\n\n this.changes[layer][node][k] = change;\n this.weights[layer][node][k] += change;\n }\n this.biases[layer][node] += this.trainOpts.learningRate * delta;\n }\n }\n }\n\n /**\n *\n * @param data\n * @returns {*}\n */\n _formatData(data) {\n if (!Array.isArray(data)) { // turn stream datum into array\n let tmp = [];\n tmp.push(data);\n data = tmp;\n }\n // turn sparse hash input into arrays with 0s as filler\n let datum = data[0].input;\n if (!Array.isArray(datum) && !(datum instanceof Float32Array)) {\n if (!this.inputLookup) {\n this.inputLookup = lookup.buildLookup(data.map(value => value['input']));\n }\n data = data.map(datum => {\n let array = lookup.toArray(this.inputLookup, datum.input);\n return Object.assign({}, datum, { input: array });\n }, this);\n }\n\n if (!Array.isArray(data[0].output)) {\n if (!this.outputLookup) {\n this.outputLookup = lookup.buildLookup(data.map(value => value['output']));\n }\n data = data.map(datum => {\n let array = lookup.toArray(this.outputLookup, datum.output);\n return Object.assign({}, datum, { output: array });\n }, this);\n }\n return data;\n }\n\n /**\n *\n * @param data\n * @returns {\n * {\n * error: number,\n * misclasses: Array\n * }\n * }\n */\n test(data) {\n data = this._formatData(data);\n\n // for binary classification problems with one output node\n let isBinary = data[0].output.length === 1;\n let falsePos = 0;\n let falseNeg = 0;\n let truePos = 0;\n let trueNeg = 0;\n\n // for classification problems\n let misclasses = [];\n\n // run each pattern through the trained network and collect\n // error and misclassification statistics\n let sum = 0;\n for (let i = 0; i < data.length; i++) {\n let output = this.runInput(data[i].input);\n let target = data[i].output;\n\n let actual, expected;\n if (isBinary) {\n actual = output[0] > this.binaryThresh ? 1 : 0;\n expected = target[0];\n }\n else {\n actual = output.indexOf(max(output));\n expected = target.indexOf(max(target));\n }\n\n if (actual !== expected) {\n let misclass = data[i];\n Object.assign(misclass, {\n actual: actual,\n expected: expected\n });\n misclasses.push(misclass);\n }\n\n if (isBinary) {\n if (actual === 0 && expected === 0) {\n trueNeg++;\n } else if (actual === 1 && expected === 1) {\n truePos++;\n } else if (actual === 0 && expected === 1) {\n falseNeg++;\n } else if (actual === 1 && expected === 0) {\n falsePos++;\n }\n }\n\n let errors = output.map((value, i) => {\n return target[i] - value;\n });\n sum += mse(errors);\n }\n let error = sum / data.length;\n\n let stats = {\n error: error,\n misclasses: misclasses\n };\n\n if (isBinary) {\n Object.assign(stats, {\n trueNeg: trueNeg,\n truePos: truePos,\n falseNeg: falseNeg,\n falsePos: falsePos,\n total: data.length,\n precision: truePos / (truePos + falsePos),\n recall: truePos / (truePos + falseNeg),\n accuracy: (trueNeg + truePos) / data.length\n });\n }\n return stats;\n }\n\n /**\n *\n * @returns\n * {\n * layers: [\n * {\n * x: {},\n * y: {}\n * },\n * {\n * '0': {\n * bias: -0.98771313,\n * weights: {\n * x: 0.8374838,\n * y: 1.245858\n * },\n * '1': {\n * bias: 3.48192004,\n * weights: {\n * x: 1.7825821,\n * y: -2.67899\n * }\n * }\n * },\n * {\n * f: {\n * bias: 0.27205739,\n * weights: {\n * '0': 1.3161821,\n * '1': 2.00436\n * }\n * }\n * }\n * ]\n * }\n */\n toJSON() {\n let layers = [];\n for (let layer = 0; layer <= this.outputLayer; layer++) {\n layers[layer] = {};\n\n let nodes;\n // turn any internal arrays back into hashes for readable json\n if (layer === 0 && this.inputLookup) {\n nodes = Object.keys(this.inputLookup);\n }\n else if (layer === this.outputLayer && this.outputLookup) {\n nodes = Object.keys(this.outputLookup);\n }\n else {\n nodes = range(0, this.sizes[layer]);\n }\n\n for (let j = 0; j < nodes.length; j++) {\n let node = nodes[j];\n layers[layer][node] = {};\n\n if (layer > 0) {\n layers[layer][node].bias = this.biases[layer][j];\n layers[layer][node].weights = {};\n for (let k in layers[layer - 1]) {\n let index = k;\n if (layer === 1 && this.inputLookup) {\n index = this.inputLookup[k];\n }\n layers[layer][node].weights[k] = this.weights[layer][j][index];\n }\n }\n }\n }\n return {\n sizes: this.sizes,\n layers,\n outputLookup:!!this.outputLookup,\n inputLookup:!!this.inputLookup,\n activation: this.activation,\n trainOpts: this._getTrainOptsJSON()\n };\n }\n\n /**\n *\n * @param json\n * @returns {NeuralNetwork}\n */\n fromJSON (json) {\n this.sizes = json.sizes\n this._initialize();\n\n for (let i = 0; i <= this.outputLayer; i++) {\n let layer = json.layers[i];\n if (i === 0 && (!layer[0] || json.inputLookup)) {\n this.inputLookup = lookup.lookupFromHash(layer);\n }\n else if (i === this.outputLayer && (!layer[0] || json.outputLookup)) {\n this.outputLookup = lookup.lookupFromHash(layer);\n }\n if (i > 0) {\n const nodes = Object.keys(layer);\n this.sizes[i] = nodes.length;\n for (let j in nodes) {\n const node = nodes[j];\n this.biases[i][j] = layer[node].bias;\n this.weights[i][j] = toArray(layer[node].weights);\n }\n }\n }\n this._updateTrainingOptions(json.trainOpts)\n this.setActivation();\n return this;\n }\n\n /**\n *\n * @returns {Function}\n */\n toFunction() {\n const activation = this.activation;\n function nodeHandle(layers, layerNumber, nodeKey) {\n if (layerNumber === 0) {\n return (typeof nodeKey === 'string'\n ? `input['${nodeKey}']`\n : `input[${nodeKey}]`);\n }\n\n const layer = layers[layerNumber];\n const node = layer[nodeKey];\n let result = [node.bias];\n for (let w in node.weights) {\n if (node.weights[w] < 0) {\n result.push(`${node.weights[w]}*(${nodeHandle(layers, layerNumber - 1, w)})`);\n } else {\n result.push(`+${node.weights[w]}*(${nodeHandle(layers, layerNumber - 1, w)})`);\n }\n }\n\n switch (activation) {\n case 'sigmoid':\n return `1/(1+1/Math.exp(${result.join('')}))`;\n case 'relu':\n return `var sum = ${result.join('')};(sum < 0 ? 0 : sum);`;\n case 'leaky-relu':\n return `var sum = ${result.join('')};(sum < 0 ? 0 : 0.01 * sum);`;\n case 'tanh':\n return `Math.tanh(${result.join('')});`;\n default:\n throw new Error('unknown activation type ' + activation);\n }\n }\n\n const layers = this.toJSON().layers;\n const layersAsMath = [];\n let result;\n for (let i in layers[layers.length - 1]) {\n layersAsMath.push(nodeHandle(layers, layers.length - 1, i));\n }\n if (this.outputLookup) {\n result = `{${\n Object.keys(this.outputLookup)\n .map((key, i) => `'${key}':${layersAsMath[i]}`)\n }}`;\n } else {\n result = `[${layersAsMath.join(',')}]`;\n }\n return new Function('input', `return ${result}`);\n }\n\n /**\n * This will create a TrainStream (WriteStream) for us to send the training data to.\n * @param opts training options\n * @returns {TrainStream|*}\n */\n createTrainStream(opts) {\n opts = opts || {};\n opts.neuralNetwork = this;\n this.setActivation();\n this.trainStream = new TrainStream(opts);\n return this.trainStream;\n }\n}"]} \ No newline at end of file diff --git a/src/neural-network-gpu.js b/src/neural-network-gpu.js index fd2e4f7ec..b143be5da 100644 --- a/src/neural-network-gpu.js +++ b/src/neural-network-gpu.js @@ -1,7 +1,6 @@ import NeuralNetwork from './neural-network'; import lookup from './lookup'; import GPU from 'gpu.js'; -import Thaw from "thaw.js"; /** * @@ -55,20 +54,20 @@ export default class NeuralNetworkGPU extends NeuralNetwork { } } - train() {} - trainAsync(data, options) { let status, endTime; ({ data, status, endTime } = this._prepTraining(data, options)); - const train = () => { - if (this._trainingTick(data, status, endTime)) { - options.done(); - } else { - requestAnimationFrame(train); - } - }; - train(); + return new Promise((resolve, reject) => { + const train = () => { + if (this._trainingTick(data, status, endTime)) { + requestAnimationFrame(train); + } else { + resolve(status); + } + }; + train(); + }); } buildRunInput() { @@ -99,7 +98,8 @@ export default class NeuralNetworkGPU extends NeuralNetwork { hardcodeConstants: true, constants: { size: this.sizes[layer - 1] - } + }, + floatTextures: true }); } } @@ -156,7 +156,8 @@ export default class NeuralNetworkGPU extends NeuralNetwork { output: [this.sizes[layer]], outputToTexture: true, outputImmutable: true, - hardcodeConstants: true + hardcodeConstants: true, + floatTextures: true }); } else { this.backwardPropagate[layer] = this.gpu.createKernelMap({ @@ -172,7 +173,8 @@ export default class NeuralNetworkGPU extends NeuralNetwork { hardcodeConstants: true, constants: { size: this.deltas[layer + 1].length - } + }, + floatTextures: true }); } } @@ -220,7 +222,8 @@ export default class NeuralNetworkGPU extends NeuralNetwork { size: this.outputs[layer - 1].length, learningRate: this.trainOpts.learningRate, momentum: this.trainOpts.momentum - } + }, + floatTextures: true }); } } @@ -247,7 +250,8 @@ export default class NeuralNetworkGPU extends NeuralNetwork { hardcodeConstants: true, constants: { learningRate: this.trainOpts.learningRate - } + }, + floatTextures: true }); } } @@ -266,7 +270,8 @@ export default class NeuralNetworkGPU extends NeuralNetwork { output: [1], constants: { size: this.sizes[this.outputLayer] - } + }, + floatTextures: true }); } diff --git a/src/neural-network.js b/src/neural-network.js index a87d3429f..ac0be3ca8 100644 --- a/src/neural-network.js +++ b/src/neural-network.js @@ -365,14 +365,14 @@ export default class NeuralNetwork { status.iterations++; if (this.trainOpts.log && (status.iterations % this.trainOpts.logPeriod === 0)) { - status.error = this._calculateTrainingError(data); + // status.error = this._calculateTrainingError(data); this.trainOpts.log(`iterations: ${status.iterations}, training error: ${status.error}`); } else { - if (status.iterations % 100 === 0) { - status.error = this._calculateTrainingError(data); - } else { + // if (status.iterations % 100 === 0) { + // status.error = this._calculateTrainingError(data); + // } else { this._trainPatterns(data); - } + // } } if (this.trainOpts.callback && (status.iterations % this.trainOpts.callbackPeriod === 0)) { diff --git a/test.html b/test.html index 3f5081e58..1a84ab104 100644 --- a/test.html +++ b/test.html @@ -5,7 +5,7 @@ \ No newline at end of file