diff --git a/README.md b/README.md index b01cdcd..8e635a3 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ ## Installation ### CDN : ```html - + ``` ### Node : ``` diff --git a/build/dann.js b/build/dann.js index fed24fd..9b827ed 100644 --- a/build/dann.js +++ b/build/dann.js @@ -1,6 +1,6 @@ /*! Dann.js */ const isBrowser = typeof process !== 'object'; -const VERSION = 'v2.4.1d'; +const VERSION = 'v2.4.1e'; /* * Undisplayed documentation @@ -2894,7 +2894,7 @@ Dann.prototype.toFunction = function toFunction(name = 'myDannFunction') { for (let i = 1; i < this.Layers.length; i++) { let actname = this.Layers[i].actname; if (i !== 0) { - let actfunc = toES6(minify(activations[actname].toString())); + let actfunc = toES6(activations[actname].toString()); let minfunction = ''; for (let u = 0; u < actfunc.length; u++) { minfunction += actfunc[u]; @@ -3028,7 +3028,7 @@ function toES6(fn) { if (isES6(fn)) return fn; let args = fn.match(/\(.*?\)/gm)[0]; - let matches = fn.match(/{.*?}/); + let matches = fn.split('\r').join('').split('\n').join('').match(/{.*?}/); let implementation = ''; if (matches !== null) { diff --git a/build/dann.min.js b/build/dann.min.js index 054189f..7954896 100644 --- a/build/dann.min.js +++ b/build/dann.min.js @@ -1,2 +1,2 @@ /*! Dann.js */ -const isBrowser="object"!=typeof process,VERSION="v2.4.1d";function bitLength(t){return t<1?1:Math.floor(Math.log(t)/Math.log(2))+1}function numberToBinary(t,e){let r=t.toString(2),i=[],o=bitLength(t)-1;for(let t=e-1;t>=0;t--){let e=r.charAt(o);i[t]=""===e?0:JSON.parse(e),o--}return i}function makeBinary(t,e){let r;r=void 0!==e?e:function(t){return t+1};let i=[];for(let e=0;e{delete t.output,t.output=[t.input.reduce(((t,e)=>t+e),0)%2]})),e}const XOR=makeXOR(2);DannError=function(t,e){this.msg=t,this.method=e},DannError.prototype.warn=function(){isBrowser?(console.error("DannWarning: "+this.msg),console.error("> "+this.method)):(console.error("DannWarning: "+this.msg+""),console.error("> "+this.method+"")),console.trace()},DannError.prototype.error=function(){isBrowser?(console.warn("DannError: "+this.msg),console.warn("> "+this.method)):(console.warn("DannError: "+this.msg+""),console.warn("> "+this.method+"")),console.trace()},DannError.warn=function(t,e){isBrowser?(console.warn("DannWarning: "+t),console.warn("> "+e)):(console.warn("DannWarning: "+t+""),console.warn("> "+e+"")),console.trace()},DannError.error=function(t,e){isBrowser?(console.error("DannError: "+t),console.error("> "+e)):(console.error("DannError: "+t+""),console.error("> "+e+"")),console.trace()};let activations={sigmoid:t=>1/(1+Math.exp(-t)),sigmoid_d(t){let e=1/(1+Math.exp(-t));return e*(1-e)},silu:t=>t/(1+Math.exp(-t)),silu_d:t=>(1+Math.exp(-t)+t*Math.exp(-t))/Math.pow(1+Math.exp(-t),2),tanh:t=>(Math.exp(t)-Math.exp(-t))/(Math.exp(t)+Math.exp(-t)),tanh_d:t=>1-Math.pow(Math.exp(2*t)-1,2)/Math.pow(Math.exp(2*t)+1,2),leakyrelu:t=>Math.max(t,.01*t),leakyrelu_d:t=>t>=0?1:.01,relu:t=>Math.max(t,0),relu_d:t=>t>=0?1:0,sinc:t=>0===t?1:Math.sin(t)/t,sinc_d:t=>0===t?0:Math.cos(t)/t-Math.sin(t)/(t*t),softsign:t=>t/(1+Math.abs(t)),softsign_d(t){let e=1+Math.abs(t);return 1/(e*e)},binary:t=>t<=0?0:1,binary_d:t=>0,softplus:t=>Math.log(1+Math.exp(t)),softplus_d:t=>1/(1+Math.exp(-t)),leakyrelucapped:t=>t>=0&&t<=6?t:t<0?.1*t:6,leakyrelucapped_d:t=>t>=0&&t<=6?1:t<0?.1:0,leakysigmoid:t=>1/(1+Math.exp(-t))+t/100,leakysigmoid_d:t=>Math.exp(-t)/Math.pow(Math.exp(-t)+1,2)+.01},lossfuncs={mae(t,e){let r=0,i=0,o=e.length;for(let i=0;i=0?o+=i*(e[r]-t[r]):o+=(i-1)*(e[r]-t[r]);return o/e.length}};const random=(t,e)=>Math.random(1)*(e-t)+t,exp=t=>Math.exp(t),abs=t=>Math.abs(t),log=t=>Math.log(t),pow=(t,e)=>Math.pow(t,e),round=t=>Math.round(t),sqrt=t=>Math.sqrt(t),cosh=t=>(exp(t)+exp(-t))/2;let poolfuncs={max:function(t){let e=0,r=t.length;for(let i=0;ie&&(e=t[i]);return e},min:function(t){let e=1/0,r=t.length;for(let i=0;i{t=t.replace(e,r)})),t}function minify(t){return t=matchReplace(t,/ = /g,"="),t=matchReplace(t,/ \+ /g,"+"),t=matchReplace(t,/ - /g,"-"),t=matchReplace(t,/ \* /g,"*"),t=matchReplace(t,/ \/ /g,"/"),t=matchReplace(t,/for \(/g,"for("),t=matchReplace(t,/; /g,";"),t=matchReplace(t,/\) {/g,"){"),t=matchReplace(t,/ < /g,"<"),t=matchReplace(t,/ > /g,">"),t=matchReplace(t,/ \+= /g,"+="),t=matchReplace(t,/;\}/g,"}"),t=matchReplace(t,/\{ /g,"{"),t=matchReplace(t,/ \{/g,"{"),t=matchReplace(t,/\} /g,"}"),t=matchReplace(t,/\t/g,""),t=matchReplace(t,/\n/g,""),t=matchReplace(t,/; /g,";"),t=matchReplace(t,/[ ]{2,}/g," ")}function isES6(t){return null!==t.match(/([a-z] {0,1}=> {0,1}).*/gm)}function toES6(t){if(isES6(t))return t;let e=t.match(/\(.*?\)/gm)[0],r=t.match(/{.*?}/),i="";return null!==r?i=r[0].trimEnd().trimStart():DannError.error("Something went wrong, we couldn't find the function definition. ","toFunction call to toES6"),i=matchReplace(i,/{ /g,"{"),i=matchReplace(i,/ }/g,"}"),`${e}=>${i}`}Matrix=function(t=0,e=0){this.rows=t,this.cols=e;let r=[[]];for(let i=0;i1)DannError.error("Probability argument must be between 0 and 1","Matrix.prototype.addRandom");else for(let i=0;i=this.cols)){for(let r=0;r=this.rows))return this.matrix[t].fill(e),this;DannError.error("The row index specified is too large for this matrix.","Matrix.prototype.fillRow")},Matrix.fromArray=function(t){let e=new Matrix(t.length,1);for(let r=0;r21?(DannError.warn("Maximum number of decimals is 21.","Matrix.prototype.log"),r=pow(10,21)):r=pow(10,t.decimals)||r,e.map((t=>round(t*r)/r))}t.table?console.table(e.matrix):console.log(e)},Matrix.make=function(t=0,e=0){let r=[[]];for(let i=0;i=1)||(DannError.error("The learning rate specified is greater or equal to 1","Dann.prototype.backpropagate"),!1)},Dann.prototype.checkDropoutRate=function(t){return t>=1?(DannError.error("The probability value can not be bigger or equal to 1","Dann.prototype.backpropagate"),!1):!(t<=0)||(DannError.error("The probability value can not be smaller or equal to 0","Dann.prototype.backpropagate"),!1)},Dann.prototype.addDropout=function addDropout(rate){if(0===this.weights.length)return void DannError.error("You need to initialize weights before using this function, use Dann.prototype.makeWeights();","Dann.prototype.addDropout");let func=(t=>{let e=1-rate;return Math.floor(Math.random()+e)}).toString().replace(/rate/gm,rate),randomMap=eval(func),inactive=[];for(let t=0;tround(t*i)/i))),!0===e.log&&(Dann.print("Prediction: "),Dann.print(o,e.table)),o},Dann.prototype.feed=function(){return this.feedForward.apply(this,arguments)},Dann.prototype.log=function(t=Dann.logDefaults()){let e=1e3;if(t.decimals>21?(DannError.error("Maximum number of decimals is 21.","Dann.prototype.log"),e=pow(10,21)):e=pow(10,t.decimals)||e,t.details){let e=t.details;t.gradients=e,t.weights=e,t.errors=e,t.biases=e,t.struct=e,t.misc=e,t.layers=e}if(0===this.weights.length&&this.makeWeights(),t.struct){console.log("Dann Model:"),console.log("Layers:");for(let e=0;e0;t--){let e=Matrix.mult(this.gradients[t],Matrix.transpose(this.Layers[t].layer));void 0!==r.dropout&&(e=e.mult(this.dropout[t])),this.weights[t].add(e),this.biases[t].add(this.gradients[t]);let i=Matrix.transpose(this.weights[t]);this.errors[t-1]=Matrix.mult(i,this.errors[t]),this.gradients[t-1]=Matrix.map(this.Layers[t].layer,this.Layers[t].actfunc_d).mult(this.errors[t-1]).mult(this.lr)}let o=Matrix.transpose(this.Layers[0].layer),n=Matrix.mult(this.gradients[0],o);void 0!==r.dropout&&(n=n.mult(this.dropout[0])),this.weights[0].add(n),this.biases[0].add(this.gradients[0]),this.loss=this.lossfunc(this.outs,e,this.percentile),!0===r.log&&(Dann.print("Prediction: "),Dann.print(this.outs,r.table),Dann.print("target: "),Dann.print(e,r.table),Dann.print(`Loss: ${this.loss}`))},Dann.prototype.train=function(){return this.backpropagate.apply(this,arguments)},Dann.prototype.mapWeights=function(t){if("function"==typeof t)for(let e=0;e=0;t--){let e=r.charAt(o);i[t]=""===e?0:JSON.parse(e),o--}return i}function makeBinary(t,e){let r;r=void 0!==e?e:function(t){return t+1};let i=[];for(let e=0;e{delete t.output,t.output=[t.input.reduce(((t,e)=>t+e),0)%2]})),e}const XOR=makeXOR(2);DannError=function(t,e){this.msg=t,this.method=e},DannError.prototype.warn=function(){isBrowser?(console.error("DannWarning: "+this.msg),console.error("> "+this.method)):(console.error("DannWarning: "+this.msg+""),console.error("> "+this.method+"")),console.trace()},DannError.prototype.error=function(){isBrowser?(console.warn("DannError: "+this.msg),console.warn("> "+this.method)):(console.warn("DannError: "+this.msg+""),console.warn("> "+this.method+"")),console.trace()},DannError.warn=function(t,e){isBrowser?(console.warn("DannWarning: "+t),console.warn("> "+e)):(console.warn("DannWarning: "+t+""),console.warn("> "+e+"")),console.trace()},DannError.error=function(t,e){isBrowser?(console.error("DannError: "+t),console.error("> "+e)):(console.error("DannError: "+t+""),console.error("> "+e+"")),console.trace()};let activations={sigmoid:t=>1/(1+Math.exp(-t)),sigmoid_d(t){let e=1/(1+Math.exp(-t));return e*(1-e)},silu:t=>t/(1+Math.exp(-t)),silu_d:t=>(1+Math.exp(-t)+t*Math.exp(-t))/Math.pow(1+Math.exp(-t),2),tanh:t=>(Math.exp(t)-Math.exp(-t))/(Math.exp(t)+Math.exp(-t)),tanh_d:t=>1-Math.pow(Math.exp(2*t)-1,2)/Math.pow(Math.exp(2*t)+1,2),leakyrelu:t=>Math.max(t,.01*t),leakyrelu_d:t=>t>=0?1:.01,relu:t=>Math.max(t,0),relu_d:t=>t>=0?1:0,sinc:t=>0===t?1:Math.sin(t)/t,sinc_d:t=>0===t?0:Math.cos(t)/t-Math.sin(t)/(t*t),softsign:t=>t/(1+Math.abs(t)),softsign_d(t){let e=1+Math.abs(t);return 1/(e*e)},binary:t=>t<=0?0:1,binary_d:t=>0,softplus:t=>Math.log(1+Math.exp(t)),softplus_d:t=>1/(1+Math.exp(-t)),leakyrelucapped:t=>t>=0&&t<=6?t:t<0?.1*t:6,leakyrelucapped_d:t=>t>=0&&t<=6?1:t<0?.1:0,leakysigmoid:t=>1/(1+Math.exp(-t))+t/100,leakysigmoid_d:t=>Math.exp(-t)/Math.pow(Math.exp(-t)+1,2)+.01},lossfuncs={mae(t,e){let r=0,i=0,o=e.length;for(let i=0;i=0?o+=i*(e[r]-t[r]):o+=(i-1)*(e[r]-t[r]);return o/e.length}};const random=(t,e)=>Math.random(1)*(e-t)+t,exp=t=>Math.exp(t),abs=t=>Math.abs(t),log=t=>Math.log(t),pow=(t,e)=>Math.pow(t,e),round=t=>Math.round(t),sqrt=t=>Math.sqrt(t),cosh=t=>(exp(t)+exp(-t))/2;let poolfuncs={max:function(t){let e=0,r=t.length;for(let i=0;ie&&(e=t[i]);return e},min:function(t){let e=1/0,r=t.length;for(let i=0;i{t=t.replace(e,r)})),t}function minify(t){return t=matchReplace(t,/ = /g,"="),t=matchReplace(t,/ \+ /g,"+"),t=matchReplace(t,/ - /g,"-"),t=matchReplace(t,/ \* /g,"*"),t=matchReplace(t,/ \/ /g,"/"),t=matchReplace(t,/for \(/g,"for("),t=matchReplace(t,/; /g,";"),t=matchReplace(t,/\) {/g,"){"),t=matchReplace(t,/ < /g,"<"),t=matchReplace(t,/ > /g,">"),t=matchReplace(t,/ \+= /g,"+="),t=matchReplace(t,/;\}/g,"}"),t=matchReplace(t,/\{ /g,"{"),t=matchReplace(t,/ \{/g,"{"),t=matchReplace(t,/\} /g,"}"),t=matchReplace(t,/\t/g,""),t=matchReplace(t,/\n/g,""),t=matchReplace(t,/; /g,";"),t=matchReplace(t,/[ ]{2,}/g," ")}function isES6(t){return null!==t.match(/([a-z] {0,1}=> {0,1}).*/gm)}function toES6(t){if(isES6(t))return t;let e=t.match(/\(.*?\)/gm)[0],r=t.split("\r").join("").split("\n").join("").match(/{.*?}/),i="";return null!==r?i=r[0].trimEnd().trimStart():DannError.error("Something went wrong, we couldn't find the function definition. ","toFunction call to toES6"),i=matchReplace(i,/{ /g,"{"),i=matchReplace(i,/ }/g,"}"),`${e}=>${i}`}Matrix=function(t=0,e=0){this.rows=t,this.cols=e;let r=[[]];for(let i=0;i1)DannError.error("Probability argument must be between 0 and 1","Matrix.prototype.addRandom");else for(let i=0;i=this.cols)){for(let r=0;r=this.rows))return this.matrix[t].fill(e),this;DannError.error("The row index specified is too large for this matrix.","Matrix.prototype.fillRow")},Matrix.fromArray=function(t){let e=new Matrix(t.length,1);for(let r=0;r21?(DannError.warn("Maximum number of decimals is 21.","Matrix.prototype.log"),r=pow(10,21)):r=pow(10,t.decimals)||r,e.map((t=>round(t*r)/r))}t.table?console.table(e.matrix):console.log(e)},Matrix.make=function(t=0,e=0){let r=[[]];for(let i=0;i=1)||(DannError.error("The learning rate specified is greater or equal to 1","Dann.prototype.backpropagate"),!1)},Dann.prototype.checkDropoutRate=function(t){return t>=1?(DannError.error("The probability value can not be bigger or equal to 1","Dann.prototype.backpropagate"),!1):!(t<=0)||(DannError.error("The probability value can not be smaller or equal to 0","Dann.prototype.backpropagate"),!1)},Dann.prototype.addDropout=function addDropout(rate){if(0===this.weights.length)return void DannError.error("You need to initialize weights before using this function, use Dann.prototype.makeWeights();","Dann.prototype.addDropout");let func=(t=>{let e=1-rate;return Math.floor(Math.random()+e)}).toString().replace(/rate/gm,rate),randomMap=eval(func),inactive=[];for(let t=0;tround(t*i)/i))),!0===e.log&&(Dann.print("Prediction: "),Dann.print(o,e.table)),o},Dann.prototype.feed=function(){return this.feedForward.apply(this,arguments)},Dann.prototype.log=function(t=Dann.logDefaults()){let e=1e3;if(t.decimals>21?(DannError.error("Maximum number of decimals is 21.","Dann.prototype.log"),e=pow(10,21)):e=pow(10,t.decimals)||e,t.details){let e=t.details;t.gradients=e,t.weights=e,t.errors=e,t.biases=e,t.struct=e,t.misc=e,t.layers=e}if(0===this.weights.length&&this.makeWeights(),t.struct){console.log("Dann Model:"),console.log("Layers:");for(let e=0;e0;t--){let e=Matrix.mult(this.gradients[t],Matrix.transpose(this.Layers[t].layer));void 0!==r.dropout&&(e=e.mult(this.dropout[t])),this.weights[t].add(e),this.biases[t].add(this.gradients[t]);let i=Matrix.transpose(this.weights[t]);this.errors[t-1]=Matrix.mult(i,this.errors[t]),this.gradients[t-1]=Matrix.map(this.Layers[t].layer,this.Layers[t].actfunc_d).mult(this.errors[t-1]).mult(this.lr)}let o=Matrix.transpose(this.Layers[0].layer),n=Matrix.mult(this.gradients[0],o);void 0!==r.dropout&&(n=n.mult(this.dropout[0])),this.weights[0].add(n),this.biases[0].add(this.gradients[0]),this.loss=this.lossfunc(this.outs,e,this.percentile),!0===r.log&&(Dann.print("Prediction: "),Dann.print(this.outs,r.table),Dann.print("target: "),Dann.print(e,r.table),Dann.print(`Loss: ${this.loss}`))},Dann.prototype.train=function(){return this.backpropagate.apply(this,arguments)},Dann.prototype.mapWeights=function(t){if("function"==typeof t)for(let e=0;e
-

Dannjs

2.4.1d +

Dannjs

2.4.1e

Browser Test

diff --git a/test/manual-tests/browser/empty-example/browser.html b/test/manual-tests/browser/empty-example/browser.html index e52b19e..355fb48 100644 --- a/test/manual-tests/browser/empty-example/browser.html +++ b/test/manual-tests/browser/empty-example/browser.html @@ -9,7 +9,7 @@
-

Dannjs

2.4.1d +

Dannjs

2.4.1e

Browser Test

diff --git a/test/manual-tests/browser/xor-example/browser.html b/test/manual-tests/browser/xor-example/browser.html index 10fe2c7..24aca1f 100644 --- a/test/manual-tests/browser/xor-example/browser.html +++ b/test/manual-tests/browser/xor-example/browser.html @@ -9,7 +9,7 @@
-

Dannjs

2.4.1d +

Dannjs

2.4.1e

Browser Test