diff --git a/build/dann.js b/build/dann.js index c0f7fa91..f67e5348 100644 --- a/build/dann.js +++ b/build/dann.js @@ -1,5 +1,5 @@ const isBrowser = typeof process !== 'object'; -const VERSION = 'v2.3.12'; +const VERSION = 'v2.3.13'; /** * Add a new custom function to Dannjs. @@ -2254,73 +2254,6 @@ Dann.prototype.feed = function feed(inputs, options) { return this.feedForward(inputs, options); }; -/** - * Applies a json object to a Dann model. - * @method fromJSON - * @for Dann - * @param {Object} data model data json object, you can get this object from a yourmodel.toJSON(); See docs here. - * @return {Dann} A Dann model. - * @example - * - * const nn = new Dann(24,4); - * nn.addHiddenLayer(18,'tanH'); - * nn.addHiddenLayer(12,'sigmoid'); - * nn.makeWeights(); - * const modelData = nn.toJSON(); - * const newNN = new Dann(); - * newNN.fromJSON(modelData); - * newNN.log(); - * - */ -Dann.prototype.fromJSON = function fromJSON(data) { - this.i = data.arch[0]; - this.inputs = new Matrix(this.i, 1); - this.o = data.arch[data.arch.length - 1]; - this.outputs = new Matrix(this.o, 1); - - let slayers = JSON.parse(data.lstr); - for (let i = 0; i < slayers.length; i++) { - let layerdata = JSON.parse(slayers[i]); - let layerObj = new Layer(layerdata.type, layerdata.size, layerdata.actname); - this.Layers[i] = layerObj; - } - this.makeWeights(); - let sweights = JSON.parse(data.wstr); - for (let i = 0; i < sweights.length; i++) { - this.weights[i].set(JSON.parse(sweights[i])); - } - let sbiases = JSON.parse(data.bstr); - for (let i = 0; i < sbiases.length; i++) { - this.biases[i].set(JSON.parse(sbiases[i])); - } - let serrors = JSON.parse(data.estr); - for (let i = 0; i < serrors.length; i++) { - this.errors[i].set(JSON.parse(serrors[i])); - } - let sgradients = JSON.parse(data.gstr); - for (let i = 0; i < sgradients.length; i++) { - this.gradients[i].set(JSON.parse(sgradients[i])); - } - - this.lossfunc_s = data.lf; - if (isBrowser) { - this.lossfunc = window[data.lf]; - } else { - this.lossfunc = lossfuncs[data.lf]; - } - this.outs = Matrix.toArray(this.Layers[this.Layers.length - 1].layer); - this.loss = data.loss; - this.losses = []; - this.lr = data.lrate; - this.arch = data.arch; - this.epoch = data.e; - this.percentile = data.per; - return this; -}; -Dann.prototype.feed = function feed(inputs, options) { - return this.feedForward(inputs, options); -}; - /** * Applies a json object to a Dann model. * @method fromJSON diff --git a/build/dann.min.js b/build/dann.min.js index 7b16bc87..568adf5c 100644 --- a/build/dann.min.js +++ b/build/dann.min.js @@ -1 +1 @@ -const isBrowser="object"!=typeof process,VERSION="v2.3.12";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;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;r{let e=1-rate;return Math.floor(Math.random()+e)}).toString().replace(/rate/gm,rate),randomMap=eval(func),inactive=[];for(let t=0;t=1)return void DannError.error("The probability value can not be bigger or equal to 1","Dann.prototype.backpropagate");if(a<=0)return void DannError.error("The probability value can not be smaller or equal to 0","Dann.prototype.backpropagate");this.addDropout(a)}for(let t=this.weights.length-1;t>0;t--){let e=Matrix.transpose(this.Layers[t].layer),r=Matrix.mult(this.gradients[t],e);void 0!==a&&(r=r.mult(this.dropout[t])),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 h=Matrix.transpose(this.Layers[0].layer),u=Matrix.mult(this.gradients[0],h);void 0!==a&&(u=u.mult(this.dropout[0])),this.weights[0].add(u),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=e.log||!1,i=e.table||!1,o=!1,s=pow(10,e.decimals)||1e3;if(void 0!==e.decimals&&(o=!0),t.length!==this.i){for(let t=0;tround(t*s)/s))),!0===i?(console.log("Prediction: "),console.table(n)):(console.log("Prediction: "),console.log(n))),n},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?(DannError.error("Maximum number of decimals is 21.","Dann.prototype.log"),h=pow(10,21)):h=pow(10,t.decimals)||h,t.details){let l=t.details;r=l,e=l,i=l,o=l,s=l,n=l,a=l}if(0===this.weights.length&&this.makeWeights(),!0===s&&console.log("Dann Model:"),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),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;r{let e=1-rate;return Math.floor(Math.random()+e)}).toString().replace(/rate/gm,rate),randomMap=eval(func),inactive=[];for(let t=0;t=1)return void DannError.error("The probability value can not be bigger or equal to 1","Dann.prototype.backpropagate");if(a<=0)return void DannError.error("The probability value can not be smaller or equal to 0","Dann.prototype.backpropagate");this.addDropout(a)}for(let t=this.weights.length-1;t>0;t--){let e=Matrix.transpose(this.Layers[t].layer),r=Matrix.mult(this.gradients[t],e);void 0!==a&&(r=r.mult(this.dropout[t])),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 h=Matrix.transpose(this.Layers[0].layer),u=Matrix.mult(this.gradients[0],h);void 0!==a&&(u=u.mult(this.dropout[0])),this.weights[0].add(u),this.biases[0].add(this.gradients[0]),this.loss=this.lossfunc(this.outs,e,this.percentile),!0===n&&this.losses.push(this.loss),!0===i&&(console.log("Prediction: "),s?console.table(this.outs):console.log(this.outs),console.log("target: "),s?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=e.log||!1,i=e.table||!1,o=!1,n=pow(10,e.decimals)||1e3;if(void 0!==e.decimals&&(o=!0),t.length!==this.i){for(let t=0;tround(t*n)/n))),!0===i?(console.log("Prediction: "),console.table(s)):(console.log("Prediction: "),console.log(s))),s},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?(DannError.error("Maximum number of decimals is 21.","Dann.prototype.log"),h=pow(10,21)):h=pow(10,t.decimals)||h,t.details){let l=t.details;r=l,e=l,i=l,o=l,n=l,s=l,a=l}if(0===this.weights.length&&this.makeWeights(),!0===n&&console.log("Dann Model:"),n){console.log("Layers:");for(let t=0;t