From e89ab4d9b07b744bee5312ad7d389800bd0227de Mon Sep 17 00:00:00 2001 From: matiasvlevi Date: Sun, 31 Jan 2021 17:51:45 -0500 Subject: [PATCH] build --- build/dann.js | 8 ++++---- build/dann.min.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/build/dann.js b/build/dann.js index 6c0fdd3c..57fbfb1f 100644 --- a/build/dann.js +++ b/build/dann.js @@ -1467,19 +1467,19 @@ class Dann { const XOR = [ { input: [1,0], - output: [0] + output: [1] }, { input: [0,1], - output: [0] + output: [1] }, { input: [0,0], - output: [1] + output: [0] }, { input: [1,1], - output: [1] + output: [0] } ]; diff --git a/build/dann.min.js b/build/dann.min.js index 5ad88243..89cd134a 100644 --- a/build/dann.min.js +++ b/build/dann.min.js @@ -1 +1 @@ -const isBrowser="object"!=typeof process;let fs,w;function addCDNdependencies(){let t=document.createElement("script");t.setAttribute("type","text/javascript"),t.setAttribute("src","https://cdn.jsdelivr.net/npm/mathjs@8.1.0/lib/browser/math.min.js"),document.head.insertBefore(t,document.head.children[0])}function downloadSTR(t,e){let s="data:text/json;charset=utf-8,"+encodeURIComponent(JSON.stringify(t)),r=document.createElement("a");r.setAttribute("href",s),r.setAttribute("download",e+".json"),document.body.appendChild(r),r.click(),r.remove()}function upload(t,e,s){window.addEventListener("load",(function(){let r="";void 0!==s&&(r=","+s.toString());let o=document.createElement("input");o.setAttribute("type","file"),o.setAttribute("id","upload"),o.setAttribute("onChange","clickedUpload("+t+r+")");try{void 0!==e?document.getElementById(e).appendChild(o):document.body.appendChild(o)}catch(t){console.error("Dann Error: the target id specified is not valid")}}))}function clickedUpload(nn,callback){let callfunc=eval(callback),element=document.getElementById("upload"),file=element.files[0],reader=new FileReader,newNN;reader.readAsText(file),reader.onload=function(){if("{"==reader.result[0]){let t=JSON.parse(reader.result);newNN=t,nn.applyToModel(newNN),void 0!==callfunc?callfunc(!1):console.log("Succesfully loaded the file.")}else void 0!==callfunc?callfunc(!0):console.log("Error loading the file.")},reader.onerror=function(){void 0!==callfunc?callfunc(!0):console.log(reader.error)},element.remove()}isBrowser?addCDNdependencies():(fs=require("fs"),require("mathjs"),w=require("@fast-csv/format"));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),logn=(t,e)=>log(t)/log(e),pow=(t,e)=>Math.pow(t,e),sin=t=>Math.sin(t),cos=t=>Math.cos(t),tan=t=>Math.tan(t),round=t=>Math.round(t),sqrt=t=>Math.sqrt(t),cosh=t=>(exp(t)+exp(-t))/2,sech=t=>1/cosh(t);function sigmoid(t){return 1/(1+exp(-t))}function sigmoid_d(t){let e=sigmoid(t);return e*(1-e)}function leakySigmoid(t){return 1/(1+exp(-t))+t/100}function leakySigmoid_d(t){let e=leakySigmoid(t);return e*(1-e)}function siLU(t){return t/(1+exp(-t))}function siLU_d(t){return(1+exp(-t)+t*exp(-t))/pow(1+exp(-t),2)}function tanH(t){return(exp(t)-exp(-t))/(exp(t)+exp(-t))}function tanH_d(t){return 1-pow(tanH(t),2)}function leakyReLUCapped(t){return t>=0&&t<=6?t:t<0?.1*t:6}function leakyReLUCapped_d(t){return t>=0&&t<=6?1:t<0?.1:0}function leakyReLU(t){return t>=0?1*t:.01*t}function leakyReLU_d(t){return t>=0?1:.01}function reLU(t){return t>=0?1*t:0}function reLU_d(t){return t>=0?1:0}function mae(t,e){let s=0,r=0,o=e.length;for(let r=0;re&&(e=t[r]);return e}function min(t){let e=1/0,s=t.length;for(let r=0;r21&&(console.error("Dann Error: Maximum number of decimals is 21."),console.trace(),e.decimals=21),n=pow(10,e.decimals),i=!0),void 0!==e.table&&(o=e.table),void 0!==e.mode?(r=e.mode,"gpu"==r&&(console.warn("Gpu support in the works."),r="cpu")):r="cpu"),t.length!=this.i){for(let t=0;tround(t*n)/n)),1==o?(console.log("Prediction: "),console.table(a)):console.log("Prediction: ",a)),a}backpropagate(t,e,s){let r=!1,o="cpu",i=!1,n=!1;void 0!==s&&(r=void 0!==s.log&&s.log,void 0!==s.table&&(n=s.table),void 0!==s.mode?(o=s.mode,"gpu"==o&&console.log("gpu version coming soon")):o="cpu",i=void 0===s.saveLoss||s.saveLoss);let a=new Matrix(0,0);if(e.length!=this.o)return console.error("Dann Error: The target array length does not match the number of ouputs the dannjs model has."),void console.trace();if(a=Matrix.fromArray(e),"number"!=typeof this.lr)return console.error("Dann Error: The learning rate specified (Dann.lr property) is not a number."),void console.trace();this.outs=this.feedForward(t,{log:!1,mode:o}),this.errors[this.errors.length-1]=Matrix.subtract(a,this.Layers[this.Layers.length-1].layer),this.gradients[this.gradients.length-1]=Matrix.map(this.Layers[this.Layers.length-1].layer,this.Layers[this.Layers.length-1].actfunc_d),this.gradients[this.gradients.length-1].mult(this.errors[this.errors.length-1]),this.gradients[this.gradients.length-1].mult(this.lr);for(let t=this.weights.length-1;t>0;t--){let e=Matrix.transpose(this.Layers[t].layer),s=Matrix.multiply(this.gradients[t],e);this.weights[t].add(s),this.biases[t].add(this.gradients[t]);let r=Matrix.transpose(this.weights[t]);this.errors[t-1]=Matrix.multiply(r,this.errors[t]),this.gradients[t-1]=Matrix.map(this.Layers[t].layer,this.Layers[t].actfunc_d),this.gradients[t-1].mult(this.errors[t-1]),this.gradients[t-1].mult(this.lr)}let l=Matrix.transpose(this.Layers[0].layer),c=Matrix.multiply(this.gradients[0],l);this.weights[0].add(c),this.biases[0].add(this.gradients[0]),this.loss=this.lossfunc(this.outs,e),1==i&&this.losses.push(this.loss),1==r&&(console.log("Prediction: "),n?console.table(this.outs):console.log(this.outs),console.log("target: "),n?console.table(e):console.log(e),console.log("Loss: ",this.loss))}mutateRandom(t,e){if("number"!=typeof t)return console.error("Dann Error: Dann.mutateRandom(); range argument must be a number."),void console.trace();if(void 0!==e){if("number"!=typeof e)return console.error("Dann Error: Dann.mutateRandom(); probability argument must be a number."),void console.trace()}else e=1;for(let s=0;sconsole.error(t)).on("finish",()=>console.log("saved training report at ./savedDanns/"+t+"/report.csv"))}fs.writeFileSync(s,JSON.stringify(a)),1==r?(console.log("",""),this.log(),console.log("Succesfully overwritten the Dann Model at ./savedDanns/"+t+"/dannData.json "),console.log("","")):(console.log("",""),this.log(),console.log("Succesfully saved the Dann Model at ./savedDanns/"+t+"/dannData.json "),console.log("",""))}}applyToModel(t){this.i=t.arch[0],this.inputs=new Matrix(this.i,1),this.o=t.arch[t.arch.length-1],this.outputs=new Matrix(this.o,1);let e=JSON.parse(t.lstr);for(let t=0;t21&&(console.error("Dann Error: Maximum number of decimals is 21."),console.trace(),t.decimals=21),c=pow(10,t.decimals))}else i=!0,n=!0;if(0===this.weights.length&&this.makeWeights(),(null==t||void 0!==t&&1==t.details)&&console.log("Dann NeuralNetwork:"),i){console.log(" "),console.log(" Layers:");for(let t=0;t=0;t--){let e=s.charAt(o);r[t]=""==e?0:JSON.parse(e),o--}return r}function makeBinary(t,e){let s;s=void 0!==e?e:function(t){return t+1};let r=[];Math.pow(2,t),bitLength(t);for(let e=0;eMath.random(1)*(e-t)+t,exp=t=>Math.exp(t),abs=t=>Math.abs(t),log=t=>Math.log(t),logn=(t,e)=>log(t)/log(e),pow=(t,e)=>Math.pow(t,e),sin=t=>Math.sin(t),cos=t=>Math.cos(t),tan=t=>Math.tan(t),round=t=>Math.round(t),sqrt=t=>Math.sqrt(t),cosh=t=>(exp(t)+exp(-t))/2,sech=t=>1/cosh(t);function sigmoid(t){return 1/(1+exp(-t))}function sigmoid_d(t){let e=sigmoid(t);return e*(1-e)}function leakySigmoid(t){return 1/(1+exp(-t))+t/100}function leakySigmoid_d(t){let e=leakySigmoid(t);return e*(1-e)}function siLU(t){return t/(1+exp(-t))}function siLU_d(t){return(1+exp(-t)+t*exp(-t))/pow(1+exp(-t),2)}function tanH(t){return(exp(t)-exp(-t))/(exp(t)+exp(-t))}function tanH_d(t){return 1-pow(tanH(t),2)}function leakyReLUCapped(t){return t>=0&&t<=6?t:t<0?.1*t:6}function leakyReLUCapped_d(t){return t>=0&&t<=6?1:t<0?.1:0}function leakyReLU(t){return t>=0?1*t:.01*t}function leakyReLU_d(t){return t>=0?1:.01}function reLU(t){return t>=0?1*t:0}function reLU_d(t){return t>=0?1:0}function mae(t,e){let s=0,r=0,o=e.length;for(let r=0;re&&(e=t[r]);return e}function min(t){let e=1/0,s=t.length;for(let r=0;r21&&(console.error("Dann Error: Maximum number of decimals is 21."),console.trace(),e.decimals=21),n=pow(10,e.decimals),i=!0),void 0!==e.table&&(o=e.table),void 0!==e.mode?(r=e.mode,"gpu"==r&&(console.warn("Gpu support in the works."),r="cpu")):r="cpu"),t.length!=this.i){for(let t=0;tround(t*n)/n)),1==o?(console.log("Prediction: "),console.table(a)):console.log("Prediction: ",a)),a}backpropagate(t,e,s){let r=!1,o="cpu",i=!1,n=!1;void 0!==s&&(r=void 0!==s.log&&s.log,void 0!==s.table&&(n=s.table),void 0!==s.mode?(o=s.mode,"gpu"==o&&console.log("gpu version coming soon")):o="cpu",i=void 0===s.saveLoss||s.saveLoss);let a=new Matrix(0,0);if(e.length!=this.o)return console.error("Dann Error: The target array length does not match the number of ouputs the dannjs model has."),void console.trace();if(a=Matrix.fromArray(e),"number"!=typeof this.lr)return console.error("Dann Error: The learning rate specified (Dann.lr property) is not a number."),void console.trace();this.outs=this.feedForward(t,{log:!1,mode:o}),this.errors[this.errors.length-1]=Matrix.subtract(a,this.Layers[this.Layers.length-1].layer),this.gradients[this.gradients.length-1]=Matrix.map(this.Layers[this.Layers.length-1].layer,this.Layers[this.Layers.length-1].actfunc_d),this.gradients[this.gradients.length-1].mult(this.errors[this.errors.length-1]),this.gradients[this.gradients.length-1].mult(this.lr);for(let t=this.weights.length-1;t>0;t--){let e=Matrix.transpose(this.Layers[t].layer),s=Matrix.multiply(this.gradients[t],e);this.weights[t].add(s),this.biases[t].add(this.gradients[t]);let r=Matrix.transpose(this.weights[t]);this.errors[t-1]=Matrix.multiply(r,this.errors[t]),this.gradients[t-1]=Matrix.map(this.Layers[t].layer,this.Layers[t].actfunc_d),this.gradients[t-1].mult(this.errors[t-1]),this.gradients[t-1].mult(this.lr)}let l=Matrix.transpose(this.Layers[0].layer),c=Matrix.multiply(this.gradients[0],l);this.weights[0].add(c),this.biases[0].add(this.gradients[0]),this.loss=this.lossfunc(this.outs,e),1==i&&this.losses.push(this.loss),1==r&&(console.log("Prediction: "),n?console.table(this.outs):console.log(this.outs),console.log("target: "),n?console.table(e):console.log(e),console.log("Loss: ",this.loss))}mutateRandom(t,e){if("number"!=typeof t)return console.error("Dann Error: Dann.mutateRandom(); range argument must be a number."),void console.trace();if(void 0!==e){if("number"!=typeof e)return console.error("Dann Error: Dann.mutateRandom(); probability argument must be a number."),void console.trace()}else e=1;for(let s=0;sconsole.error(t)).on("finish",()=>console.log("saved training report at ./savedDanns/"+t+"/report.csv"))}fs.writeFileSync(s,JSON.stringify(a)),1==r?(console.log("",""),this.log(),console.log("Succesfully overwritten the Dann Model at ./savedDanns/"+t+"/dannData.json "),console.log("","")):(console.log("",""),this.log(),console.log("Succesfully saved the Dann Model at ./savedDanns/"+t+"/dannData.json "),console.log("",""))}}applyToModel(t){this.i=t.arch[0],this.inputs=new Matrix(this.i,1),this.o=t.arch[t.arch.length-1],this.outputs=new Matrix(this.o,1);let e=JSON.parse(t.lstr);for(let t=0;t21&&(console.error("Dann Error: Maximum number of decimals is 21."),console.trace(),t.decimals=21),c=pow(10,t.decimals))}else i=!0,n=!0;if(0===this.weights.length&&this.makeWeights(),(null==t||void 0!==t&&1==t.details)&&console.log("Dann NeuralNetwork:"),i){console.log(" "),console.log(" Layers:");for(let t=0;t=0;t--){let e=s.charAt(o);r[t]=""==e?0:JSON.parse(e),o--}return r}function makeBinary(t,e){let s;s=void 0!==e?e:function(t){return t+1};let r=[];Math.pow(2,t),bitLength(t);for(let e=0;e