diff --git a/README.md b/README.md index 69d0f377..b9f00eba 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ ## Installation ### CDN : ```html - + ``` ### Node : ``` diff --git a/build/dann.js b/build/dann.js index 8b6b6de7..ac6e2e42 100644 --- a/build/dann.js +++ b/build/dann.js @@ -1,5 +1,5 @@ const isBrowser = typeof process !== 'object'; -const VERSION = 'v2.2.10'; +const VERSION = 'v2.2.11'; /** * Add a new custom function to Dannjs. diff --git a/build/dann.min.js b/build/dann.min.js index 7fec0ea8..5ab17d81 100644 --- a/build/dann.min.js +++ b/build/dann.min.js @@ -1 +1 @@ -const isBrowser="object"!=typeof process,VERSION="v2.2.10";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}Add=function(){},Add.activation=function(t,e,r){if("string"==typeof t)return 1!==e.length||1!==r.length?void DannError.error("One of the functions specified does not have only 1 argument.","Add.activation"):(activations[t]=e,void(activations[t+"_d"]=r));DannError.error("The name argument is not a string.","Add.activation")},Add.loss=function(t,e){"string"==typeof t?2===e.length?lossfuncs[t]=e:DannError.error("The loss function specified can only have 2 argument.","newActivation"):DannError.error("The name argument is not a string.","Add.loss")};const XOR=makeXOR(2);function sigmoid(t){return 1/(1+Math.exp(-t))}function sigmoid_d(t){let e=sigmoid(t);return e*(1-e)}function leakySigmoid(t){return 1/(1+Math.exp(-t))+t/100}function leakySigmoid_d(t){let e=leakySigmoid(t);return e*(1-e)}function siLU(t){return t/(1+Math.exp(-t))}function siLU_d(t){return(1+Math.exp(-t)+t*Math.exp(-t))/Math.pow(1+Math.exp(-t),2)}function tanH(t){return(Math.exp(t)-Math.exp(-t))/(Math.exp(t)+Math.exp(-t))}function tanH_d(t){return 1-Math.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 sinc(t){return 0===t?1:Math.sin(t)/t}function sinc_d(t){return 0===t?0:Math.cos(t)/t-Math.sin(t)/(t*t)}function softsign(t){return t/(1+Math.abs(t))}function softsign_d(t){let e=1+Math.abs(t);return 1/(e*e)}function binary(t){return t<=0?0:1}function binary_d(t){return 0}function softplus(t){return Math.log(1+Math.exp(t))}function softplus_d(t){return sigmoid(t)}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:sigmoid,sigmoid_d:sigmoid_d,tanH:tanH,tanH_d:tanH_d,siLU:siLU,siLU_d:siLU_d,reLU:reLU,reLU_d:reLU_d,leakyReLU:leakyReLU,leakyReLU_d:leakyReLU_d,sinc:sinc,sinc_d:sinc_d,softsign:softsign,softsign_d:softsign_d,binary:binary,binary_d:binary_d,softplus:softplus,softplus_d:softplus_d,leakySigmoid:leakySigmoid,leakySigmoid_d:leakySigmoid_d,leakyReLUCapped:leakyReLUCapped,leakyReLUCapped_d:leakyReLUCapped_d};function 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}let lossfuncs={mae:mae,bce:bce,lcl:lcl,mbe:mbe,mce:mce,mse:mse,rmse:rmse,mael:mael,quantile:quantile};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),s=o[0];return r=o[1].indexOf(")"),minify("("+s+")=>"+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;i0;t--){let e=Matrix.transpose(this.Layers[t].layer),r=Matrix.mult(this.gradients[t],e);this.weights[t].add(r),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),this.gradients[t-1].mult(this.errors[t-1]),this.gradients[t-1].mult(this.lr)}let l=Matrix.transpose(this.Layers[0].layer),h=Matrix.mult(this.gradients[0],l);this.weights[0].add(h),this.biases[0].add(this.gradients[0]),this.loss=this.lossfunc(this.outs,e,this.percentile),!0===s&&this.losses.push(this.loss),!0===i&&(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))},Dann.prototype.train=function(t,e,r){return this.backpropagate(t,e,r)},Dann.createFromJSON=function(t){const e=new Dann;return e.fromJSON(t),e},Dann.prototype.feedForward=function(t,e){let r=!1,i="cpu",o=!1,s=!1,n=1e3;if(void 0!==e&&(r=void 0!==e.log&&e.log,void 0!==e.decimals&&(e.decimals>21&&(DannError.warn("Maximum number of decimals is 21, was set to 21 by default.","Dann.prototype.feedForward"),e.decimals=21),n=pow(10,e.decimals),s=!0),void 0!==e.table&&(o=e.table),void 0!==e.mode?(i=e.mode,"gpu"===i&&(DannError.warn("Gpu Support not available yet, mode set to 'cpu'","Dann.prototype.feedForward"),i="cpu")):i="cpu"),t.length!==this.i){for(let t=0;tround(t*n)/n))),!0===o?(console.log("Prediction: "),console.table(a)):(console.log("Prediction: "),console.log(a))),a},Dann.prototype.feed=function(t,e){return this.feedForward(t,e)},Dann.prototype.fromJSON=function(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),h=pow(10,t.decimals))}else s=!0,n=!0;if(0===this.weights.length&&this.makeWeights(),(void 0===t||void 0!==t&&!0===t.details)&&console.log("Dann NeuralNetwork:"),s){console.log("Layers:");for(let t=0;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}Add=function(){},Add.activation=function(t,e,r){if("string"==typeof t)return 1!==e.length||1!==r.length?void DannError.error("One of the functions specified does not have only 1 argument.","Add.activation"):(activations[t]=e,void(activations[t+"_d"]=r));DannError.error("The name argument is not a string.","Add.activation")},Add.loss=function(t,e){"string"==typeof t?2===e.length?lossfuncs[t]=e:DannError.error("The loss function specified can only have 2 argument.","newActivation"):DannError.error("The name argument is not a string.","Add.loss")};const XOR=makeXOR(2);function sigmoid(t){return 1/(1+Math.exp(-t))}function sigmoid_d(t){let e=sigmoid(t);return e*(1-e)}function leakySigmoid(t){return 1/(1+Math.exp(-t))+t/100}function leakySigmoid_d(t){let e=leakySigmoid(t);return e*(1-e)}function siLU(t){return t/(1+Math.exp(-t))}function siLU_d(t){return(1+Math.exp(-t)+t*Math.exp(-t))/Math.pow(1+Math.exp(-t),2)}function tanH(t){return(Math.exp(t)-Math.exp(-t))/(Math.exp(t)+Math.exp(-t))}function tanH_d(t){return 1-Math.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 sinc(t){return 0===t?1:Math.sin(t)/t}function sinc_d(t){return 0===t?0:Math.cos(t)/t-Math.sin(t)/(t*t)}function softsign(t){return t/(1+Math.abs(t))}function softsign_d(t){let e=1+Math.abs(t);return 1/(e*e)}function binary(t){return t<=0?0:1}function binary_d(t){return 0}function softplus(t){return Math.log(1+Math.exp(t))}function softplus_d(t){return sigmoid(t)}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:sigmoid,sigmoid_d:sigmoid_d,tanH:tanH,tanH_d:tanH_d,siLU:siLU,siLU_d:siLU_d,reLU:reLU,reLU_d:reLU_d,leakyReLU:leakyReLU,leakyReLU_d:leakyReLU_d,sinc:sinc,sinc_d:sinc_d,softsign:softsign,softsign_d:softsign_d,binary:binary,binary_d:binary_d,softplus:softplus,softplus_d:softplus_d,leakySigmoid:leakySigmoid,leakySigmoid_d:leakySigmoid_d,leakyReLUCapped:leakyReLUCapped,leakyReLUCapped_d:leakyReLUCapped_d};function 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}let lossfuncs={mae:mae,bce:bce,lcl:lcl,mbe:mbe,mce:mce,mse:mse,rmse:rmse,mael:mael,quantile:quantile};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),s=o[0];return r=o[1].indexOf(")"),minify("("+s+")=>"+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;i0;t--){let e=Matrix.transpose(this.Layers[t].layer),r=Matrix.mult(this.gradients[t],e);this.weights[t].add(r),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),this.gradients[t-1].mult(this.errors[t-1]),this.gradients[t-1].mult(this.lr)}let l=Matrix.transpose(this.Layers[0].layer),h=Matrix.mult(this.gradients[0],l);this.weights[0].add(h),this.biases[0].add(this.gradients[0]),this.loss=this.lossfunc(this.outs,e,this.percentile),!0===s&&this.losses.push(this.loss),!0===i&&(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))},Dann.prototype.train=function(t,e,r){return this.backpropagate(t,e,r)},Dann.createFromJSON=function(t){const e=new Dann;return e.fromJSON(t),e},Dann.prototype.feedForward=function(t,e){let r=!1,i="cpu",o=!1,s=!1,n=1e3;if(void 0!==e&&(r=void 0!==e.log&&e.log,void 0!==e.decimals&&(e.decimals>21&&(DannError.warn("Maximum number of decimals is 21, was set to 21 by default.","Dann.prototype.feedForward"),e.decimals=21),n=pow(10,e.decimals),s=!0),void 0!==e.table&&(o=e.table),void 0!==e.mode?(i=e.mode,"gpu"===i&&(DannError.warn("Gpu Support not available yet, mode set to 'cpu'","Dann.prototype.feedForward"),i="cpu")):i="cpu"),t.length!==this.i){for(let t=0;tround(t*n)/n))),!0===o?(console.log("Prediction: "),console.table(a)):(console.log("Prediction: "),console.log(a))),a},Dann.prototype.feed=function(t,e){return this.feedForward(t,e)},Dann.prototype.fromJSON=function(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),h=pow(10,t.decimals))}else s=!0,n=!0;if(0===this.weights.length&&this.makeWeights(),(void 0===t||void 0!==t&&!0===t.details)&&console.log("Dann NeuralNetwork:"),s){console.log("Layers:");for(let t=0;t
-

Dannjs

2.2.10 +

Dannjs

2.2.11

Browser Test

diff --git a/test/manual-tests/browser/empty-example/browser.html b/test/manual-tests/browser/empty-example/browser.html index 42ca21f2..3aaed76a 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.2.10 +

Dannjs

2.2.11

Browser Test

diff --git a/test/manual-tests/browser/xor-example/browser.html b/test/manual-tests/browser/xor-example/browser.html index 42ca21f2..3aaed76a 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.2.10 +

Dannjs

2.2.11

Browser Test