From eccbac2a9b241e8c9d0934e3e33dd21c8c364b2f Mon Sep 17 00:00:00 2001 From: Matias Vazquez-Levi Date: Sun, 14 Nov 2021 11:34:56 -0500 Subject: [PATCH] build --- build/dann.js | 48 +++++++++++++++++++++++++---------------------- build/dann.min.js | 2 +- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/build/dann.js b/build/dann.js index afbabd9c..d8ada28e 100644 --- a/build/dann.js +++ b/build/dann.js @@ -1,6 +1,6 @@ /*! Dann.js */ const isBrowser = typeof process !== 'object'; -const VERSION = 'v2.3.14'; +const VERSION = 'v2.4.0'; /* * Undisplayed documentation @@ -834,27 +834,27 @@ Matrix.prototype.insert = function insert(value, x, y) { Matrix.prototype.log = function log( options = { table: false, - decimals: 21, + decimals: undefined, } ) { - // Limit decimals to maximum of 21 - let dec = 1000; - if (options.decimals > 21) { - DannError.error( - 'Maximum number of decimals is 21.', - 'Matrix.prototype.log' - ); - dec = pow(10, 21); - } else { - dec = pow(10, options.decimals) || dec; - } - // Copy matrix let m = new Matrix(this.rows, this.cols); m.set(this.matrix); // round the values - m.map((x) => round(x * dec) / dec); + if (options.decimals !== undefined) { + let dec = 1000; + if (options.decimals > 21) { + DannError.warn( + 'Maximum number of decimals is 21.', + 'Matrix.prototype.log' + ); + dec = pow(10, 21); + } else { + dec = pow(10, options.decimals) || dec; + } + m.map((x) => round(x * dec) / dec); + } // Log if (options.table) { @@ -1122,9 +1122,12 @@ Matrix.prototype.set = function set(matrix) { typeof matrix[0].length === 'number' && typeof matrix === 'object' ) { - this.matrix = matrix; this.rows = matrix.length; this.cols = matrix[0].length; + + for (let i = 0; i < this.rows; i++) { + this.matrix[i] = [...matrix[i]]; + } } else { DannError.error( 'the argument of set(); must be an array within an array. Here is an example: [[1,0],[0,1]]', @@ -1714,7 +1717,7 @@ Dann.logDefaults = function logDefaults() { errors: false, layers: false, table: false, - decimals: 3, + decimals: undefined, details: false, }; }; @@ -2291,22 +2294,23 @@ Dann.prototype.feedForward = function feedForward( layerObj.layer.add(this.biases[i]); layerObj.layer.map(layerObj.actfunc); } + // Untransformed output this.outs = Matrix.toArray(this.Layers[this.Layers.length - 1].layer); - // Optional logs + // Output transformations let out = this.outs; if (roundData && options.asLabel) { DannError.warn( 'Cannot round if output is a label', 'Dann.prototype.feedForward' ); - } - if (options.asLabel === true) { + } else if (options.asLabel) { out = Dann.asLabel(out); - } - if (roundData === true && options.asLabel !== true) { + } else if (roundData && !options.asLabel) { out = out.map((x) => round(x * dec) / dec); } + + // Optional logs if (options.log === true) { Dann.print('Prediction: '); Dann.print(out, options.table); diff --git a/build/dann.min.js b/build/dann.min.js index eef2da57..2541922c 100644 --- a/build/dann.min.js +++ b/build/dann.min.js @@ -1,2 +1,2 @@ /*! Dann.js */ -const isBrowser="object"!=typeof process,VERSION="v2.3.14";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){return this.sigmoid(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){let e=leakySigmoid(t);return e*(1-e)}},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 /g,">")).replace(/ \+= /g,"+=")).replace(/;\}/g,"}");for(let e=0;e<5;e++)t=(t=(t=(t=(t=t.replace(/\{ /g,"{")).replace(/ \{/g,"{")).replace(/\} /g,"}")).replace(/\t/g,"")).replace(/\n/g,"");for(let e=0;e<5;e++)t=t.replace(/; /g,";");return t}function slicestring(t,e){return[t.slice(0,e+1),t.slice(e+1,t.length)]}function toEs6(t){let e=t.toString(),r=e.indexOf("("),i=slicestring(e,r)[1];r=i.indexOf(")");let o=slicestring(i,r-1),n=o[0];return r=o[1].indexOf(")"),minify("("+n+")=>"+slicestring(o[1],r)[1])}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.error("Maximum number of decimals is 21.","Matrix.prototype.log"),e=pow(10,21)):e=pow(10,t.decimals)||e;let r=new Matrix(this.rows,this.cols);r.set(this.matrix),r.map((t=>round(t*e)/e)),t.table?console.table(r.matrix):console.log(r)},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.saveLoss&&this.losses.push(this.loss),!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){return this.sigmoid(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){let e=leakySigmoid(t);return e*(1-e)}},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 /g,">")).replace(/ \+= /g,"+=")).replace(/;\}/g,"}");for(let e=0;e<5;e++)t=(t=(t=(t=(t=t.replace(/\{ /g,"{")).replace(/ \{/g,"{")).replace(/\} /g,"}")).replace(/\t/g,"")).replace(/\n/g,"");for(let e=0;e<5;e++)t=t.replace(/; /g,";");return t}function slicestring(t,e){return[t.slice(0,e+1),t.slice(e+1,t.length)]}function toEs6(t){let e=t.toString(),r=e.indexOf("("),i=slicestring(e,r)[1];r=i.indexOf(")");let o=slicestring(i,r-1),n=o[0];return r=o[1].indexOf(")"),minify("("+n+")=>"+slicestring(o[1],r)[1])}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.saveLoss&&this.losses.push(this.loss),!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