/
synaptic.js
3 lines (3 loc) · 35.7 KB
/
synaptic.js
1
2
3
!function(t){function e(n){if(i[n])return i[n].exports;var r=i[n]={exports:{},id:n,loaded:!1};return t[n].call(r.exports,r,r.exports,e),r.loaded=!0,r.exports}var i={};return e.m=t,e.c=i,e.p="",e(0)}([function(t,e,i){t.exports=i(1)},function(t,e,i){var n,r,o={Neuron:i(2),Layer:i(4),Network:i(5),Trainer:i(6),Architect:i(7)};n=[],r=function(){return o}.apply(e,n),!(void 0!==r&&(t.exports=r)),"undefined"!=typeof t&&t.exports&&(t.exports=o),"object"==typeof window&&(!function(){var t=window.synaptic;o.ninja=function(){return window.synaptic=t,o}}(),window.synaptic=o)},function(t,e,i){(function(t){function e(){this.ID=e.uid(),this.label=null,this.connections={inputs:{},projected:{},gated:{}},this.error={responsibility:0,projected:0,gated:0},this.trace={elegibility:{},extended:{},influences:{}},this.state=0,this.old=0,this.activation=0,this.selfconnection=new e.connection(this,this,0),this.squash=e.squash.LOGISTIC,this.neighboors={},this.bias=.2*Math.random()-.1}t&&(t.exports=e),e.prototype={activate:function(t){if("undefined"!=typeof t)return this.activation=t,this.derivative=0,this.bias=0,this.activation;this.old=this.state,this.state=this.selfconnection.gain*this.selfconnection.weight*this.state+this.bias;for(var e in this.connections.inputs){var t=this.connections.inputs[e];this.state+=t.from.activation*t.weight*t.gain}this.activation=this.squash(this.state),this.derivative=this.squash(this.state,!0);var i=[];for(var n in this.trace.extended){var r=this.neighboors[n],o=r.selfconnection.gater==this?r.old:0;for(var s in this.trace.influences[r.ID])o+=this.trace.influences[r.ID][s].weight*this.trace.influences[r.ID][s].from.activation;i[r.ID]=o}for(var e in this.connections.inputs){var t=this.connections.inputs[e];this.trace.elegibility[t.ID]=this.selfconnection.gain*this.selfconnection.weight*this.trace.elegibility[t.ID]+t.gain*t.from.activation;for(var n in this.trace.extended){var a=this.trace.extended[n],r=this.neighboors[n],o=i[r.ID];a[t.ID]=r.selfconnection.gain*r.selfconnection.weight*a[t.ID]+this.derivative*this.trace.elegibility[t.ID]*o}}for(var c in this.connections.gated)this.connections.gated[c].gain=this.activation;return this.activation},propagate:function(t,e){var i=0,n="undefined"!=typeof e;if(n)this.error.responsibility=this.error.projected=e-this.activation;else{for(var r in this.connections.projected){var o=this.connections.projected[r],s=o.to;i+=s.error.responsibility*o.gain*o.weight}this.error.projected=this.derivative*i,i=0;for(var r in this.trace.extended){var s=this.neighboors[r],a=s.selfconnection.gater==this?s.old:0;for(var c in this.trace.influences[r])a+=this.trace.influences[r][c].weight*this.trace.influences[s.ID][c].from.activation;i+=s.error.responsibility*a}this.error.gated=this.derivative*i,this.error.responsibility=this.error.projected+this.error.gated}t=t||.1;for(var r in this.connections.inputs){var c=this.connections.inputs[r],h=this.error.projected*this.trace.elegibility[c.ID];for(var r in this.trace.extended){var s=this.neighboors[r];h+=s.error.responsibility*this.trace.extended[s.ID][c.ID]}c.weight+=t*h}this.bias+=t*this.error.responsibility},project:function(t,i){if(t==this)return this.selfconnection.weight=1,this.selfconnection;var n=this.connected(t);if(n&&"projected"==n.type)return"undefined"!=typeof i&&(n.connection.weight=i),n.connection;var r=new e.connection(this,t,i);this.connections.projected[r.ID]=r,this.neighboors[t.ID]=t,t.connections.inputs[r.ID]=r,t.trace.elegibility[r.ID]=0;for(var o in t.trace.extended){var s=t.trace.extended[o];s[r.ID]=0}return r},gate:function(t){this.connections.gated[t.ID]=t;var e=t.to;if(!(e.ID in this.trace.extended)){this.neighboors[e.ID]=e;var i=this.trace.extended[e.ID]={};for(var n in this.connections.inputs){var r=this.connections.inputs[n];i[r.ID]=0}}e.ID in this.trace.influences?this.trace.influences[e.ID].push(t):this.trace.influences[e.ID]=[t],t.gater=this},selfconnected:function(){return 0!==this.selfconnection.weight},connected:function(t){var e={type:null,connection:!1};if(this==t)return this.selfconnected()?(e.type="selfconnection",e.connection=this.selfconnection,e):!1;for(var i in this.connections)for(var n in this.connections[i]){var n=this.connections[i][n];if(n.to==t)return e.type=i,e.connection=n,e;if(n.from==t)return e.type=i,e.connection=n,e}return!1},clear:function(){for(var t in this.trace.elegibility)this.trace.elegibility[t]=0;for(var t in this.trace.extended)for(var e in this.trace.extended[t])this.trace.extended[t][e]=0;this.error.responsibility=this.error.projected=this.error.gated=0},reset:function(){this.clear();for(var t in this.connections)for(var e in this.connections[t])this.connections[t][e].weight=.2*Math.random()-.1;this.bias=.2*Math.random()-.1,this.old=this.state=this.activation=0},optimize:function(t,i){t=t||{};var n=[],r=[],o=[],s=t.memory||0,a=t.neurons||1,c=t.inputs||[],h=t.targets||[],u=t.outputs||[],p=t.variables||{},l=t.activation_sentences||[],f=t.trace_sentences||[],v=t.propagation_sentences||[],d=t.layers||{__count:0,__neuron:0},g=function(t){var e=i in d&&t[d.__count];e||(d.__count=t.push([])-1,d[i]=d.__count)};g(l),g(f),g(v);var y=d.__count,m=function(){var t=Array.prototype.slice.call(arguments);if(1==t.length){if("target"==t[0]){var e="target_"+h.length;h.push(s)}else var e=t[0];return e in p?p[e]:p[e]={value:0,id:s++}}var i=t.length>2;if(i)var n=t.pop();var r=t.shift(),o=t.pop();if(!i)var n=r[o];var e=o+"_";for(var a in t)e+=t[a]+"_";return e+=r.ID,e in p?p[e]:p[e]={value:n,id:s++}},w=function(){var t=Array.prototype.slice.call(arguments),e=t.pop(),i="";for(var n in t)i+="string"==typeof t[n]?t[n]:"F["+t[n].id+"]";e.push(i+";")},T=function(t){for(var e in t)if(t.hasOwnProperty(e))return!1;return!0},I=T(this.connections.projected),b=T(this.connections.gated),D="input"==i?!0:T(this.connections.inputs),O="output"==i?!0:I&&b,_=m("rate"),E=m(this,"activation");if(D)c.push(E.id);else{l[y].push(n),f[y].push(r),v[y].push(o);var z=m(this,"old"),j=m(this,"state"),L=m(this,"bias");if(this.selfconnection.gater)var S=m(this.selfconnection,"gain");if(this.selfconnected())var N=m(this.selfconnection,"weight");w(z," = ",j,n),this.selfconnected()?this.selfconnection.gater?w(j," = ",S," * ",N," * ",j," + ",L,n):w(j," = ",N," * ",j," + ",L,n):w(j," = ",L,n);for(var x in this.connections.inputs){var A=this.connections.inputs[x],M=m(A.from,"activation"),k=m(A,"weight");if(A.gater)var q=m(A,"gain");this.connections.inputs[x].gater?w(j," += ",M," * ",k," * ",q,n):w(j," += ",M," * ",k,n)}var R=m(this,"derivative");switch(this.squash){case e.squash.LOGISTIC:w(E," = (1 / (1 + Math.exp(-",j,")))",n),w(R," = ",E," * (1 - ",E,")",n);break;case e.squash.TANH:var F=m("aux"),P=m("aux_2");w(F," = Math.exp(",j,")",n),w(P," = 1 / ",F,n),w(E," = (",F," - ",P,") / (",F," + ",P,")",n),w(R," = 1 - (",E," * ",E,")",n);break;case e.squash.IDENTITY:w(E," = ",j,n),w(R," = 1",n);break;case e.squash.HLIM:w(E," = +(",j," > 0)",n),w(R," = 1",n);case e.squash.RELU:w(E," = ",j," > 0 ? ",j," : 0",n),w(R," = ",j," > 0 ? 1 : 0",n)}for(var H in this.trace.extended){var U=this.neighboors[H],V=m("influences["+U.ID+"]"),C=m(U,"old"),G=!1;U.selfconnection.gater==this&&(w(V," = ",C,r),G=!0);for(var Y in this.trace.influences[U.ID]){var B=m(this.trace.influences[U.ID][Y],"weight"),W=m(this.trace.influences[U.ID][Y].from,"activation");G?w(V," += ",B," * ",W,r):(w(V," = ",B," * ",W,r),G=!0)}}for(var x in this.connections.inputs){var A=this.connections.inputs[x];if(A.gater)var q=m(A,"gain");var M=m(A.from,"activation"),J=m(this,"trace","elegibility",A.ID,this.trace.elegibility[A.ID]);this.selfconnected()?this.selfconnection.gater?A.gater?w(J," = ",S," * ",N," * ",J," + ",q," * ",M,r):w(J," = ",S," * ",N," * ",J," + ",M,r):A.gater?w(J," = ",N," * ",J," + ",q," * ",M,r):w(J," = ",N," * ",J," + ",M,r):A.gater?w(J," = ",q," * ",M,r):w(J," = ",M,r);for(var H in this.trace.extended){var U=this.neighboors[H],V=m("influences["+U.ID+"]"),J=m(this,"trace","elegibility",A.ID,this.trace.elegibility[A.ID]),K=m(this,"trace","extended",U.ID,A.ID,this.trace.extended[U.ID][A.ID]);if(U.selfconnected())var X=m(U.selfconnection,"weight");if(U.selfconnection.gater)var Q=m(U.selfconnection,"gain");U.selfconnected()?U.selfconnection.gater?w(K," = ",Q," * ",X," * ",K," + ",R," * ",J," * ",V,r):w(K," = ",X," * ",K," + ",R," * ",J," * ",V,r):w(K," = ",R," * ",J," * ",V,r)}}for(var Z in this.connections.gated){var $=m(this.connections.gated[Z],"gain");w($," = ",E,n)}}if(!D){var tt=m(this,"error","responsibility",this.error.responsibility);if(O){var et=m("target");w(tt," = ",et," - ",E,o);for(var H in this.connections.inputs){var A=this.connections.inputs[H],J=m(this,"trace","elegibility",A.ID,this.trace.elegibility[A.ID]),k=m(A,"weight");w(k," += ",_," * (",tt," * ",J,")",o)}u.push(E.id)}else if(I||b){if(b){w(tt," = 0",o);for(var H in this.connections.projected){var Z=this.connections.projected[H],U=Z.to,it=m(Z,"weight"),nt=m(U,"error","responsibility",U.error.responsibility);if(Z.gater){var rt=m(Z,"gain");w(tt," += ",nt," * ",rt," * ",it,o)}else w(tt," += ",nt," * ",it,o)}w(tt," *= ",R,o);for(var H in this.connections.inputs){var A=this.connections.inputs[H],J=m(this,"trace","elegibility",A.ID,this.trace.elegibility[A.ID]),k=m(A,"weight");w(k," += ",_," * (",tt," * ",J,")",o)}}else if(I){w(tt," = 0",o);for(var H in this.trace.extended){var U=this.neighboors[H],V=m("aux"),C=m(U,"old");U.selfconnection.gater==this?w(V," = ",C,o):w(V," = 0",o);for(var A in this.trace.influences[U.ID]){var Z=this.trace.influences[U.ID][A],it=m(Z,"weight"),ot=m(Z.from,"activation");w(V," += ",it," * ",ot,o)}var nt=m(U,"error","responsibility",U.error.responsibility);w(tt," += ",nt," * ",V,o)}w(tt," *= ",R,o);for(var H in this.connections.inputs){var A=this.connections.inputs[H],st=m("aux");w(st," = 0",o);for(var H in this.trace.extended){var U=this.neighboors[H],nt=m(U,"error","responsibility",U.error.responsibility),K=m(this,"trace","extended",U.ID,A.ID,this.trace.extended[U.ID][A.ID]);w(st," += ",nt," * ",K,o)}var k=m(A,"weight");w(k," += ",_," * ",st,o)}}}else{var at=m("aux");for(var H in this.connections.projected){var Z=this.connections.projected[H],U=Z.to,it=m(Z,"weight"),nt=m(U,"error","responsibility",U.error.responsibility);if(Z.gater){var rt=m(Z,"gain");w(at," += ",nt," * ",rt," * ",it,o)}else w(at," += ",nt," * ",it,o)}var ct=m(this,"error","projected",this.error.projected);w(ct," = ",R," * ",at,o),w(at," = 0",o);for(var H in this.trace.extended){var U=this.neighboors[H],V=m("aux_2"),C=m(U,"old");U.selfconnection.gater==this?w(V," = ",C,o):w(V," = 0",o);for(var A in this.trace.influences[U.ID]){var Z=this.trace.influences[U.ID][A],it=m(Z,"weight"),ot=m(Z.from,"activation");w(V," += ",it," * ",ot,o)}var nt=m(U,"error","responsibility",U.error.responsibility);w(at," += ",nt," * ",V,o)}var ht=m(this,"error","gated",this.error.gated);w(ht," = ",R," * ",at,o),w(tt," = ",ct," + ",ht,o);for(var H in this.connections.inputs){var A=this.connections.inputs[H],st=m("aux"),J=m(this,"trace","elegibility",A.ID,this.trace.elegibility[A.ID]);w(st," = ",ct," * ",J,o);for(var H in this.trace.extended){var U=this.neighboors[H],nt=m(U,"error","responsibility",U.error.responsibility),K=m(this,"trace","extended",U.ID,A.ID,this.trace.extended[U.ID][A.ID]);w(st," += ",nt," * ",K,o)}var k=m(A,"weight");w(k," += ",_," * ",st,o)}}w(L," += ",_," * ",tt,o)}return{memory:s,neurons:a+1,inputs:c,outputs:u,targets:h,variables:p,activation_sentences:l,trace_sentences:f,propagation_sentences:v,layers:d}}},e.connection=function(t,i,n){if(!t||!i)throw new Error("Connection Error: Invalid neurons");this.ID=e.connection.uid(),this.from=t,this.to=i,this.weight="undefined"==typeof n?.2*Math.random()-.1:n,this.gain=1,this.gater=null},e.squash={},e.squash.LOGISTIC=function(t,i){if(!i)return 1/(1+Math.exp(-t));var n=e.squash.LOGISTIC(t);return n*(1-n)},e.squash.TANH=function(t,i){if(i)return 1-Math.pow(e.squash.TANH(t),2);var n=Math.exp(t),r=1/n;return(n-r)/(n+r)},e.squash.IDENTITY=function(t,e){return e?1:t},e.squash.HLIM=function(t,e){return e?1:t>0?1:0},e.squash.RELU=function(t,e){return e?t>0?1:0:t>0?t:0},function(){var t=0,i=0;e.uid=function(){return t++},e.connection.uid=function(){return i++},e.quantity=function(){return{neurons:t,connections:i}}}()}).call(e,i(3)(t))},function(t,e){t.exports=function(t){return t.webpackPolyfill||(t.deprecate=function(){},t.paths=[],t.children=[],t.webpackPolyfill=1),t}},function(t,e,i){(function(t){function e(t,e){for(this.size=0|t,this.list=[],this.label=e||null,this.connectedTo=[];t--;){var i=new n;this.list.push(i)}}t&&(t.exports=e);var n=i(2),r=i(5);e.prototype={activate:function(t){var e=[];if("undefined"!=typeof t){if(t.length!=this.size)throw new Error("INPUT size and LAYER size must be the same to activate!");for(var i in this.list){var n=this.list[i],r=n.activate(t[i]);e.push(r)}}else for(var i in this.list){var n=this.list[i],r=n.activate();e.push(r)}return e},propagate:function(t,e){if("undefined"!=typeof e){if(e.length!=this.size)throw new Error("TARGET size and LAYER size must be the same to propagate!");for(var i=this.list.length-1;i>=0;i--){var n=this.list[i];n.propagate(t,e[i])}}else for(var i=this.list.length-1;i>=0;i--){var n=this.list[i];n.propagate(t)}},project:function(t,i,n){if(t instanceof r&&(t=t.layers.input),!(t instanceof e))throw new Error("Invalid argument, you can only project connections to LAYERS and NETWORKS!");return this.connected(t)?void 0:new e.connection(this,t,i,n)},gate:function(t,i){if(i==e.gateType.INPUT){if(t.to.size!=this.size)throw new Error("GATER layer and CONNECTION.TO layer must be the same size in order to gate!");for(var n in t.to.list){var r=t.to.list[n],o=this.list[n];for(var s in r.connections.inputs){var a=r.connections.inputs[s];a.ID in t.connections&&o.gate(a)}}}else if(i==e.gateType.OUTPUT){if(t.from.size!=this.size)throw new Error("GATER layer and CONNECTION.FROM layer must be the same size in order to gate!");for(var n in t.from.list){var r=t.from.list[n],o=this.list[n];for(var c in r.connections.projected){var a=r.connections.projected[c];a.ID in t.connections&&o.gate(a)}}}else if(i==e.gateType.ONE_TO_ONE){if(t.size!=this.size)throw new Error("The number of GATER UNITS must be the same as the number of CONNECTIONS to gate!");for(var n in t.list){var o=this.list[n],a=t.list[n];o.gate(a)}}t.gatedfrom.push({layer:this,type:i})},selfconnected:function(){for(var t in this.list){var e=this.list[t];if(!e.selfconnected())return!1}return!0},connected:function(t){var i=0;for(var n in this.list)for(var r in t.list){var o=this.list[n],s=t.list[r],a=o.connected(s);"projected"==a.type&&i++}if(i==this.size*t.size)return e.connectionType.ALL_TO_ALL;i=0;for(var c in this.list){var o=this.list[c],s=t.list[c],a=o.connected(s);"projected"==a.type&&i++}return i==this.size?e.connectionType.ONE_TO_ONE:void 0},clear:function(){for(var t in this.list){var e=this.list[t];e.clear()}},reset:function(){for(var t in this.list){var e=this.list[t];e.reset()}},neurons:function(){return this.list},add:function(t){this.neurons[t.ID]=t||new n,this.list.push(t),this.size++},set:function(t){t=t||{};for(var e in this.list){var i=this.list[e];t.label&&(i.label=t.label+"_"+i.ID),t.squash&&(i.squash=t.squash),t.bias&&(i.bias=t.bias)}return this}},e.connection=function(t,i,n,r){if(this.ID=e.connection.uid(),this.from=t,this.to=i,this.selfconnection=i==t,this.type=n,this.connections={},this.list=[],this.size=0,this.gatedfrom=[],"undefined"==typeof this.type&&(t==i?this.type=e.connectionType.ONE_TO_ONE:this.type=e.connectionType.ALL_TO_ALL),this.type==e.connectionType.ALL_TO_ALL||this.type==e.connectionType.ALL_TO_ELSE)for(var o in this.from.list)for(var s in this.to.list){var a=this.from.list[o],c=this.to.list[s];if(this.type!=e.connectionType.ALL_TO_ELSE||a!=c){var h=a.project(c,r);this.connections[h.ID]=h,this.size=this.list.push(h)}}else if(this.type==e.connectionType.ONE_TO_ONE)for(var u in this.from.list){var a=this.from.list[u],c=this.to.list[u],h=a.project(c,r);this.connections[h.ID]=h,this.size=this.list.push(h)}t.connectedTo.push(this)},e.connectionType={},e.connectionType.ALL_TO_ALL="ALL TO ALL",e.connectionType.ONE_TO_ONE="ONE TO ONE",e.connectionType.ALL_TO_ELSE="ALL TO ELSE",e.gateType={},e.gateType.INPUT="INPUT",e.gateType.OUTPUT="OUTPUT",e.gateType.ONE_TO_ONE="ONE TO ONE",function(){var t=0;e.connection.uid=function(){return t++}}()}).call(e,i(3)(t))},function(t,e,i){(function(t){function e(t){"undefined"!=typeof t&&(this.layers=t||{input:null,hidden:{},output:null},this.optimized=null)}t&&(t.exports=e);var n=i(2),r=i(4),o=i(6);e.prototype={activate:function(t){if(this.optimized===!1){this.layers.input.activate(t);for(var e in this.layers.hidden)this.layers.hidden[e].activate();return this.layers.output.activate()}return null==this.optimized&&this.optimize(),this.optimized.activate(t)},propagate:function(t,e){if(this.optimized===!1){this.layers.output.propagate(t,e);var i=[];for(var n in this.layers.hidden)i.push(this.layers.hidden[n]);i.reverse();for(var n in i)i[n].propagate(t)}else null==this.optimized&&this.optimize(),this.optimized.propagate(t,e)},project:function(t,i,n){if(this.optimized&&this.optimized.reset(),t instanceof e)return this.layers.output.project(t.layers.input,i,n);if(t instanceof r)return this.layers.output.project(t,i,n);throw new Error("Invalid argument, you can only project connections to LAYERS and NETWORKS!")},gate:function(t,e){this.optimized&&this.optimized.reset(),this.layers.output.gate(t,e)},clear:function(){this.restore();var t=this.layers.input,e=this.layers.output;t.clear();for(var i in this.layers.hidden){var n=this.layers.hidden[i];n.clear()}e.clear(),this.optimized&&this.optimized.reset()},reset:function(){this.restore();var t=this.layers.input,e=this.layers.output;t.reset();for(var i in this.layers.hidden){var n=this.layers.hidden[i];n.reset()}e.reset(),this.optimized&&this.optimized.reset()},optimize:function(){var t=this,e={},i=this.neurons();for(var n in i){for(var r=i[n].neuron,o=i[n].layer;r.neuron;)r=r.neuron;e=r.optimize(e,o)}for(var n in e.propagation_sentences)e.propagation_sentences[n].reverse();e.propagation_sentences.reverse();var s="";s+="var F = Float64Array ? new Float64Array("+e.memory+") : []; ";for(var n in e.variables)s+="F["+e.variables[n].id+"] = "+(e.variables[n].value||0)+"; ";s+="var activate = function(input){\n";for(var n in e.inputs)s+="F["+e.inputs[n]+"] = input["+n+"]; ";for(var a in e.activation_sentences)if(e.activation_sentences[a].length>0)for(var c in e.activation_sentences[a])s+=e.activation_sentences[a][c].join(" "),s+=e.trace_sentences[a][c].join(" ");s+=" var output = []; ";for(var n in e.outputs)s+="output["+n+"] = F["+e.outputs[n]+"]; ";s+="return output; }; ",s+="var propagate = function(rate, target){\n",s+="F["+e.variables.rate.id+"] = rate; ";for(var n in e.targets)s+="F["+e.targets[n]+"] = target["+n+"]; ";for(var a in e.propagation_sentences)for(var c in e.propagation_sentences[a])s+=e.propagation_sentences[a][c].join(" ")+" ";s+=" };\n",s+="var ownership = function(memoryBuffer){\nF = memoryBuffer;\nthis.memory = F;\n};\n",s+="return {\nmemory: F,\nactivate: activate,\npropagate: propagate,\nownership: ownership\n};",s=s.split(";").join(";\n");var h=new Function(s),u=h();u.data={variables:e.variables,activate:e.activation_sentences,propagate:e.propagation_sentences,trace:e.trace_sentences,inputs:e.inputs,outputs:e.outputs,check_activation:this.activate,check_propagation:this.propagate},u.reset=function(){t.optimized&&(t.optimized=null,t.activate=u.data.check_activation,t.propagate=u.data.check_propagation)},this.optimized=u,this.activate=u.activate,this.propagate=u.propagate},restore:function(){if(this.optimized){var t=this.optimized,e=function(){var e=Array.prototype.slice.call(arguments),i=e.shift(),n=e.pop(),r=n+"_";for(var o in e)r+=e[o]+"_";r+=i.ID;var s=t.memory,a=t.data.variables;return r in a?s[a[r].id]:0},i=this.neurons();for(var n in i){for(var r=i[n].neuron;r.neuron;)r=r.neuron;r.state=e(r,"state"),r.old=e(r,"old"),r.activation=e(r,"activation"),r.bias=e(r,"bias");for(var o in r.trace.elegibility)r.trace.elegibility[o]=e(r,"trace","elegibility",o);for(var s in r.trace.extended)for(var o in r.trace.extended[s])r.trace.extended[s][o]=e(r,"trace","extended",s,o)}for(var n in i){for(var r=i[n].neuron;r.neuron;)r=r.neuron;for(var a in r.connections.projected){var c=r.connections.projected[a];c.weight=e(c,"weight"),c.gain=e(c,"gain")}}}},neurons:function(){var t=[],e=this.layers.input.neurons(),i=this.layers.output.neurons();for(var n in e)t.push({neuron:e[n],layer:"input"});for(var r in this.layers.hidden){var o=this.layers.hidden[r].neurons();for(var n in o)t.push({neuron:o[n],layer:r})}for(var n in i)t.push({neuron:i[n],layer:"output"});return t},inputs:function(){return this.layers.input.size},outputs:function(){return this.layers.output.size},set:function(t){this.layers=t,this.optimized&&this.optimized.reset()},setOptimize:function(t){this.restore(),this.optimized&&this.optimized.reset(),this.optimized=t?null:!1},toJSON:function(t){this.restore();var e=this.neurons(),i=[],r=[],o={};for(var s in e){for(var a=e[s].neuron;a.neuron;)a=a.neuron;o[a.ID]=s;var c={trace:{elegibility:{},extended:{}},state:a.state,old:a.old,activation:a.activation,bias:a.bias,layer:e[s].layer};c.squash=a.squash==n.squash.LOGISTIC?"LOGISTIC":a.squash==n.squash.TANH?"TANH":a.squash==n.squash.IDENTITY?"IDENTITY":a.squash==n.squash.HLIM?"HLIM":null,i.push(c)}for(var s in e){for(var a=e[s].neuron;a.neuron;)a=a.neuron;for(var h in a.connections.projected){var u=a.connections.projected[h];r.push({from:o[u.from.ID],to:o[u.to.ID],weight:u.weight,gater:u.gater?o[u.gater.ID]:null})}a.selfconnected()&&r.push({from:o[a.ID],to:o[a.ID],weight:a.selfconnection.weight,gater:a.selfconnection.gater?o[a.selfconnection.gater.ID]:null})}return{neurons:i,connections:r}},toDot:function(t){var e="digraph nn {\n rankdir = BT\n",i=[this.layers.input].concat(this.layers.hidden,this.layers.output);for(var n in i)for(var r in i[n].connectedTo){var o=i[n].connectedTo[r],s=o.to,a=o.size,c=i.indexOf(i[n]),h=i.indexOf(s);if(t){if(o.gatedfrom.length){var u="fake"+c+"_"+h;e+=" "+u+' [label = "", shape = point, width = 0.01, height = 0.01]\n',e+=" "+c+" -> "+u+" [label = "+a+", arrowhead = none]\n",e+=" "+u+" -> "+h+"\n"}else e+=" "+c+" -> "+h+" [label = "+a+"]\n";for(var p in o.gatedfrom){var l=o.gatedfrom[p].layer,f=i.indexOf(l);e+=" "+f+" -> "+u+" [color = blue]\n"}}else{e+=" "+c+" -> "+h+" [label = "+a+"]\n";for(var p in o.gatedfrom){var l=o.gatedfrom[p].layer,f=i.indexOf(l);e+=" "+f+" -> "+h+" [color = blue]\n"}}}return e+="}\n",{code:e,link:"https://chart.googleapis.com/chart?chl="+escape(e.replace("/ /g","+"))+"&cht=gv"}},standalone:function(){this.optimized||this.optimize();var t=this.optimized.data,e="function (input) {\n";for(var i in t.inputs)e+="F["+t.inputs[i]+"] = input["+i+"];\n";for(var n in t.activate)for(var r in t.activate[n])e+=t.activate[n][r].join("")+"\n";e+="var output = [];\n";for(var i in t.outputs)e+="output["+i+"] = F["+t.outputs[i]+"];\n";e+="return output;\n}";var o=e.match(/F\[(\d+)\]/g),s=0,a={};for(var c in o){var h=o[c].match(/\d+/)[0];h in a||(a[h]=s++)}var u="F = {\n";for(var i in a)u+=a[i]+": "+this.optimized.memory[i]+",\n";return u=u.substring(0,u.length-2)+"\n};\n",u="var run = "+e.replace(/F\[(\d+)]/g,function(t){return"F["+a[t.match(/\d+/)[0]]+"]"}).replace("{\n","{\n"+u)+";\n",u+="return run",new Function(u)()},worker:function(t,i,n){var r={};n&&(r=n),r.rate=n.rate||.2,r.iterations=n.iterations||1e5,r.error=n.error||.005,r.cost=n.cost||null,r.crossValidate=n.crossValidate||null,costFunction="var cost = "+(n&&n.cost||this.cost||o.cost.MSE)+";\n";var s=e.getWorkerSharedFunctions();s=s.replace(/var cost = options && options\.cost \|\| this\.cost \|\| Trainer\.cost\.MSE;/g,costFunction),s=s.replace("return results;",'postMessage({action: "done", message: results, memoryBuffer: F}, [F.buffer]);'),s=s.replace("console.log('iterations', iterations, 'error', error, 'rate', currentRate)","postMessage({action: 'log', message: {\niterations: iterations,\nerror: error,\nrate: currentRate\n}\n})"),s=s.replace("abort = this.schedule.do({ error: error, iterations: iterations, rate: currentRate })","postMessage({action: 'schedule', message: {\niterations: iterations,\nerror: error,\nrate: currentRate\n}\n})"),this.optimized||this.optimize();var a="var inputs = "+this.optimized.data.inputs.length+";\n";a+="var outputs = "+this.optimized.data.outputs.length+";\n",a+="var F = new Float64Array(["+this.optimized.memory.toString()+"]);\n",a+="var activate = "+this.optimized.activate.toString()+";\n",a+="var propagate = "+this.optimized.propagate.toString()+";\n",a+="onmessage = function(e) {\nif (e.data.action == 'startTraining') {\ntrain("+JSON.stringify(i)+","+JSON.stringify(r)+");\n}\n}";var c=s+"\n"+a,h=new Blob([c]),u=window.URL.createObjectURL(h);return new Worker(u)},clone:function(){return e.fromJSON(this.toJSON())}},e.getWorkerSharedFunctions=function(){if("undefined"!=typeof e._SHARED_WORKER_FUNCTIONS)return e._SHARED_WORKER_FUNCTIONS;var t=o.prototype.train.toString();t=t.replace("function (set","function train(set")+"\n";var i=o.prototype._trainSet.toString().replace(/this.network./g,"");i=i.replace("function (set","function _trainSet(set")+"\n",i=i.replace("this.crossValidate","crossValidate"),i=i.replace("crossValidate = true","crossValidate = { }");var n=o.prototype.test.toString().replace(/this.network./g,"");return n=n.replace("function (set","function test(set")+"\n",e._SHARED_WORKER_FUNCTIONS=t+i+n},e.fromJSON=function(t){var i=[],o={input:new r,hidden:[],output:new r};for(var s in t.neurons){var a=t.neurons[s],c=new n;c.trace.elegibility={},c.trace.extended={},c.state=a.state,c.old=a.old,c.activation=a.activation,c.bias=a.bias,c.squash=a.squash in n.squash?n.squash[a.squash]:n.squash.LOGISTIC,i.push(c),"input"==a.layer?o.input.add(c):"output"==a.layer?o.output.add(c):("undefined"==typeof o.hidden[a.layer]&&(o.hidden[a.layer]=new r),o.hidden[a.layer].add(c))}for(var s in t.connections){var a=t.connections[s],h=i[a.from],u=i[a.to],p=a.weight,l=i[a.gater],f=h.project(u,p);l&&l.gate(f)}return new e(o)}}).call(e,i(3)(t))},function(t,e,i){(function(t){function e(t,e){e=e||{},this.network=t,this.rate=e.rate||.2,this.iterations=e.iterations||1e5,this.error=e.error||.005,this.cost=e.cost||null,this.crossValidate=e.crossValidate||null}t&&(t.exports=e),e.prototype={train:function(t,i){function n(t){for(var e,i,n=t.length;n;e=Math.floor(Math.random()*n),i=t[--n],t[n]=t[e],t[e]=i);return t}var r,o,s,a=1,c=f=0,h=!1,u=i&&i.cost||this.cost||e.cost.MSE,p=!1,l=Date.now();if(i&&(i.shuffle,i.iterations&&(this.iterations=i.iterations),i.error&&(this.error=i.error),i.rate&&(this.rate=i.rate),i.cost&&(this.cost=i.cost),i.schedule&&(this.schedule=i.schedule),i.customLog&&(console.log("Deprecated: use schedule instead of customLog"),this.schedule=i.customLog),(this.crossValidate||i.crossValidate)&&(this.crossValidate||(this.crossValidate={}),p=!0,i.crossValidate.testSize&&(this.crossValidate.testSize=i.crossValidate.testSize),i.crossValidate.testError&&(this.crossValidate.testError=i.crossValidate.testError))),r=this.rate,Array.isArray(this.rate))var f=Math.floor(this.iterations/this.rate.length);if(p){var v=Math.ceil((1-this.crossValidate.testSize)*t.length);s=t.slice(0,v),o=t.slice(v)}for(;!h&&c<this.iterations&&a>this.error&&!(p&&a<=this.crossValidate.testError);){var d=t.length;if(a=0,c++,f>0){var g=Math.floor(c/f);r=this.rate[g]||r}"function"==typeof this.rate&&(r=this.rate(c,a)),p?(this._trainSet(s,r,u),a+=this.test(o).error,d=1):(a+=this._trainSet(t,r,u),d=t.length),a/=d,i&&(this.schedule&&this.schedule.every&&c%this.schedule.every==0?h=this.schedule["do"]({error:a,iterations:c,rate:r}):i.log&&c%i.log==0&&console.log("iterations",c,"error",a,"rate",r),i.shuffle&&n(t))}var y={error:a,iterations:c,time:Date.now()-l};return y},trainAsync:function(t,e){var i=this.workerTrain.bind(this);return new Promise(function(n,r){try{i(t,n,e,!0)}catch(o){r(o)}})},_trainSet:function(t,e,i){var n=0;for(var r in t){var o=t[r].input,s=t[r].output,a=this.network.activate(o);this.network.propagate(e,s),n+=i(s,a)}return n},test:function(t,i){var n,r,o,s=0,a=i&&i.cost||this.cost||e.cost.MSE,c=Date.now();for(var h in t)n=t[h].input,o=t[h].output,r=this.network.activate(n),s+=a(o,r);s/=t.length;var u={error:s,time:Date.now()-c};return u},workerTrain:function(t,e,i,n){n||console.warn("Deprecated: do not use `workerTrain`, use `trainAsync` instead.");var r=this;this.network.optimized||this.network.optimize();var o=this.network.worker(this.network.optimized.memory,t,i);o.onmessage=function(t){switch(t.data.action){case"done":var n=t.data.message.iterations,s=t.data.message.error,a=t.data.message.time;r.network.optimized.ownership(t.data.memoryBuffer),e({error:s,iterations:n,time:a}),o.terminate();break;case"log":console.log(t.data.message);case"schedule":if(i&&i.schedule&&"function"==typeof i.schedule["do"]){var c=i.schedule["do"];c(t.data.message)}}},o.postMessage({action:"startTraining"})},XOR:function(t){if(2!=this.network.inputs()||1!=this.network.outputs())throw new Error("Incompatible network (2 inputs, 1 output)");var i={iterations:1e5,log:!1,shuffle:!0,cost:e.cost.MSE};if(t)for(var n in t)i[n]=t[n];return this.train([{input:[0,0],output:[0]},{input:[1,0],output:[1]},{input:[0,1],output:[1]},{input:[1,1],output:[0]}],i)},DSR:function(t){t=t||{};var i,n,r,o,s,a=t.targets||[2,4,7,8],c=t.distractors||[3,5,6,9],h=t.prompts||[0,1],u=t.length||24,p=t.success||.95,l=t.iterations||1e5,f=t.rate||.1,v=t.log||0,d=t.schedule||{},g=t.cost||this.cost||e.cost.CROSS_ENTROPY;i=n=r=o=s=0;for(var y=1,m=a.length+c.length+h.length,w=function(t,e){var i=Math.random()*t|0,n=!1;for(var r in e)i==e[r]&&(n=!0);return n?w(t,e):i},T=function(t,e){for(var i in t)if(Math.round(t[i])!=e[i])return!1;return!0},I=Date.now();l>i&&(p>s||i%1e3!=0);){var b=[],D=u-h.length;for(r=0;D>r;r++){var O=Math.random()*c.length|0;b.push(c[O])}var _=[],E=[];for(r=0;r<h.length;r++)_.push(Math.random()*a.length|0),E.push(w(D,E));for(E=E.sort(),r=0;r<h.length;r++)b[E[r]]=a[_[r]],b.push(h[r]);var z,j=z=0;for(y=0,r=0;u>r;r++){var L=[];for(o=0;m>o;o++)L[o]=0;L[b[r]]=1;var S=[];for(o=0;o<a.length;o++)S[o]=0;if(r>=D){var N=r-D;S[_[N]]=1}var x=this.network.activate(L);T(x,S)?D>r?z++:j++:this.network.propagate(f,S),y+=g(S,x),z+j==u&&n++}i%1e3==0&&(n=0),i++;var A=i%1e3;A=0==A?1e3:A,s=n/A,y/=u,v&&i%v==0&&console.log("iterations:",i," success:",s," correct:",n," time:",Date.now()-I," error:",y),d["do"]&&d.every&&i%d.every==0&&d["do"]({iterations:i,success:s,error:y,time:Date.now()-I,correct:n})}return{iterations:i,success:s,error:y,time:Date.now()-I}},ERG:function(t){t=t||{};var i=t.iterations||15e4,n=t.error||.05,r=t.rate||.1,o=t.log||500,s=t.cost||this.cost||e.cost.CROSS_ENTROPY,a=function(){this.paths=[]};a.prototype={connect:function(t,e){return this.paths.push({node:t,value:e}),this},any:function(){if(0==this.paths.length)return!1;var t=Math.random()*this.paths.length|0;return this.paths[t]},test:function(t){for(var e in this.paths)if(this.paths[e].value==t)return this.paths[e];return!1}};for(var c=function(){var t=new a,e=(new a).connect(t,"E"),i=(new a).connect(e,"S"),n=(new a).connect(e,"V").connect(i,"P"),r=(new a).connect(i,"X");r.connect(r,"S");var o=(new a).connect(n,"V");o.connect(o,"T"),i.connect(o,"X");var s=(new a).connect(r,"T").connect(o,"P"),c=(new a).connect(s,"B");return{input:c,output:t}},h=function(){var t=c(),e=c(),i=new a,n=(new a).connect(i,"E");t.output.connect(n,"T"),e.output.connect(n,"P");var r=(new a).connect(t.input,"P").connect(e.input,"T"),o=(new a).connect(r,"B");return{input:o,output:i}},u=function(){for(var t=h().input,e=t.any(),i="";e;)i+=e.value,e=e.node.any();return i},p=function(t){for(var e=h().input,i=0,n=t.charAt(i);i<t.length;){var r=e.test(n);if(!r)return!1;e=r.node,n=t.charAt(++i);
}return!0},l=function(t,e){var i=0,n=-1,r=0,o=-1;for(var s in t)t[s]>i&&(i=t[s],n=s),e[s]>r&&(r=e[s],o=s);return n!=o},f=0,v=1,d={B:0,P:1,T:2,X:3,S:4,E:5},g=Date.now();i>f&&v>n;){var y=0;v=0;for(var m=u(),w=m.charAt(y),T=m.charAt(y+1);y<m.length-1;){for(var I=[],b=[],D=0;6>D;D++)I[D]=0,b[D]=0;I[d[w]]=1,b[d[T]]=1;var O=this.network.activate(I);l(O,b)&&this.network.propagate(r,b),w=m.charAt(++y),T=m.charAt(y+1),v+=s(b,O)}v/=m.length,f++,f%o==0&&console.log("iterations:",f," time:",Date.now()-g," error:",v)}return{iterations:f,error:v,time:Date.now()-g,test:p,generate:u}},timingTask:function(t){function i(t,e){for(var i=t+e,n=0,r=[],o=0;i>o;o++)r.push({input:[0,0],output:[0]});for(;i-20>n;){var s=Math.round(20*Math.random());r[n].input[0]=1;for(var a=n;n+s>=a;a++)r[a].input[1]=s/20,r[a].output[0]=.5;n+=s,s=Math.round(20*Math.random());for(var c=n+1;n+s>=c&&i>c;c++)r[c].input[1]=r[n].input[1];n+=s}for(var h=[],u=[],p=0;i>p;p++)(t>p?h:u).push(r[p]);return{train:h,test:u}}if(2!=this.network.inputs()||1!=this.network.outputs())throw new Error("Invalid Network: must have 2 inputs and one output");"undefined"==typeof t&&(t={});var n=t.iterations||200,r=t.error||.005,o=t.rate||[.03,.02],s=t.log===!1?!1:t.log||10,a=t.cost||this.cost||e.cost.MSE,c=t.trainSamples||7e3,h=t.trainSamples||1e3,u=i(c,h),p=this.train(u.train,{rate:o,log:s,iterations:n,error:r,cost:a});return{train:p,test:this.test(u.test)}}},e.cost={CROSS_ENTROPY:function(t,e){var i=0;for(var n in e)i-=t[n]*Math.log(e[n]+1e-15)+(1-t[n])*Math.log(1+1e-15-e[n]);return i},MSE:function(t,e){var i=0;for(var n in e)i+=Math.pow(t[n]-e[n],2);return i/e.length},BINARY:function(t,e){var i=0;for(var n in e)i+=Math.round(2*t[n])!=Math.round(2*e[n]);return i}}}).call(e,i(3)(t))},function(t,e,i){(function(t){var e=i(4),n=i(5),r=i(6),o={Perceptron:function(){var t=Array.prototype.slice.call(arguments);if(t.length<3)throw new Error("not enough layers (minimum 3) !!");var i=t.shift(),n=t.pop(),o=t,s=new e(i),a=[],c=new e(n),h=s;for(var u in o){var p=o[u],l=new e(p);a.push(l),h.project(l),h=l}h.project(c),this.set({input:s,hidden:a,output:c}),this.trainer=new r(this)},LSTM:function(){var t=Array.prototype.slice.call(arguments);if(t.length<3)throw new Error("not enough layers (minimum 3) !!");var i=t.pop(),n={peepholes:e.connectionType.ALL_TO_ALL,hiddenToHidden:!1,outputToHidden:!1,outputToGates:!1,inputToOutput:!0};if("number"!=typeof i){var o=t.pop();i.hasOwnProperty("peepholes")&&(n.peepholes=i.peepholes),i.hasOwnProperty("hiddenToHidden")&&(n.hiddenToHidden=i.hiddenToHidden),i.hasOwnProperty("outputToHidden")&&(n.outputToHidden=i.outputToHidden),i.hasOwnProperty("outputToGates")&&(n.outputToGates=i.outputToGates),i.hasOwnProperty("inputToOutput")&&(n.inputToOutput=i.inputToOutput)}else var o=i;var s=t.shift(),a=t,c=new e(s),h=[],u=new e(o),p=null;for(var l in a){var f=a[l],v=new e(f).set({bias:1}),d=new e(f).set({bias:1}),g=new e(f),y=new e(f).set({bias:1});h.push(v),h.push(d),h.push(g),h.push(y);var m=c.project(g);if(c.project(v),c.project(d),c.project(y),null!=p){var w=p.project(g);p.project(v),p.project(d),p.project(y)}var T=g.project(u),I=g.project(g);n.hiddenToHidden&&g.project(g,e.connectionType.ALL_TO_ELSE),n.outputToHidden&&u.project(g),n.outputToGates&&(u.project(v),u.project(y),u.project(d)),g.project(v,n.peepholes),g.project(d,n.peepholes),g.project(y,n.peepholes),v.gate(m,e.gateType.INPUT),d.gate(I,e.gateType.ONE_TO_ONE),y.gate(T,e.gateType.OUTPUT),null!=p&&v.gate(w,e.gateType.INPUT),p=g}n.inputToOutput&&c.project(u),this.set({input:c,hidden:h,output:u}),this.trainer=new r(this)},Liquid:function(t,i,n,o,s){for(var a=new e(t),c=new e(i),h=new e(n),u=c.neurons(),p=[],l=0;o>l;l++){var f=Math.random()*u.length|0,v=Math.random()*u.length|0,d=u[f].project(u[v]);p.push(d)}for(var g=0;s>g;g++){var y=Math.random()*u.length|0,d=Math.random()*p.length|0;u[y].gate(p[d])}a.project(c),c.project(h),this.set({input:a,hidden:[c],output:h}),this.trainer=new r(this)},Hopfield:function(t){var i=new e(t),n=new e(t);i.project(n,e.connectionType.ALL_TO_ALL),this.set({input:i,hidden:[],output:n});var s=new r(this),a=o.Hopfield.prototype;a.learn=a.learn||function(t){var e=[];for(var i in t)e.push({input:t[i],output:t[i]});return s.train(e,{iterations:5e5,error:5e-5,rate:1})},a.feed=a.feed||function(t){var e=this.activate(t),t=[];for(var i in e)t[i]=e[i]>.5?1:0;return t}}};for(var s in o)o[s].prototype=new n,o[s].prototype.constructor=o[s];t&&(t.exports=o)}).call(e,i(3)(t))}]);
//# sourceMappingURL=synaptic.min.js.map