1,"blockSize should be > 1 for depthToSpace, but was: "+t);for(var r=e.shape[0],a=e.shape[1],i=e.shape[2],o=e.shape[3],s=a*t,u=i*t,l=o/(t*t),c=e.dataSync(),f=new Float32Array(r*s*u*l),p=0,h=0;h=s))for(var k=p>1?(N-_)*(u-1)/(p-1):0,I=h>1?(S-E)*(l-1)/(h-1):0,C=0;C1?_*(u-1)+C*k:.5*(_+N)*(u-1);if(P<0||P>u-1)for(var A=0;A1?E*(l-1)+A*I:.5*(E+S)*(l-1))<0||$>l-1)for(T=0;T1?E*(l-1)+A*I:.5*(E+S)*(l-1))<0||$>l-1)for(T=0;T=e.size/s)throw new Error("Invalid indices: "+h+" does not index into "+e.shape);for(var y=0;y=r/a)throw new Error("Invalid indices: "+m+" does not index into "+n);for(var b=0;b0,"scheme must not be an empty string.");var r=e.getInstance();b(null==r.managers[t],"A model store manager is already registered for scheme '"+t+"'."),r.managers[t]=n},e.getManager=function(e){var t=this.getInstance().managers[e];if(null==t)throw new Error("Cannot find model manager for scheme '"+e+"'");return t},e.getSchemes=function(){return Object.keys(this.getInstance().managers)},e}();function Zs(e){if(-1===e.indexOf(Ys))throw new Error("The url string provided does not contain a scheme. Supported schemes are: "+Js.getSchemes().join(","));return{scheme:e.split(Ys)[0],path:e.split(Ys)[1]}}function Qs(e,t,n){return void 0===n&&(n=!1),l(this,void 0,void 0,function(){var r,a,i,o,s,u,l,f,p;return c(this,function(c){switch(c.label){case 0:return b(e!==t,"Old path and new path are the same: '"+e+"'"),b((r=Xs.getLoadHandlers(e)).length>0,"Copying failed because no load handler is found for source URL "+e+"."),b(r.length<2,"Copying failed because more than one ("+r.length+") load handlers for source URL "+e+"."),a=r[0],b((i=Xs.getSaveHandlers(t)).length>0,"Copying failed because no save handler is found for destination URL "+t+"."),b(i.length<2,"Copying failed because more than one ("+r.length+") save handlers for destination URL "+t+"."),o=i[0],s=Zs(e).scheme,u=Zs(e).path,l=s===Zs(e).scheme,[4,a.load()];case 1:return f=c.sent(),n&&l?[4,Js.getManager(s).removeModel(u)]:[3,3];case 2:c.sent(),c.label=3;case 3:return[4,o.save(f)];case 4:return p=c.sent(),!n||l?[3,6]:[4,Js.getManager(s).removeModel(u)];case 5:c.sent(),c.label=6;case 6:return[2,p.modelArtifactsInfo]}})})}var eu="models_store",tu="model_info_store";function nu(){if(!ve.get("IS_BROWSER"))throw new Error("Failed to obtain IndexedDB factory because the current environmentis not a web browser.");var e=window,t=e.indexedDB||e.mozIndexedDB||e.webkitIndexedDB||e.msIndexedDB||e.shimIndexedDB;if(null==t)throw new Error("The current browser does not appear to support IndexedDB.");return t}function ru(e){var t=e.result;t.createObjectStore(eu,{keyPath:"modelPath"}),t.createObjectStore(tu,{keyPath:"modelPath"})}var au=function(){function e(e){if(this.indexedDB=nu(),null==e||!e)throw new Error("For IndexedDB, modelPath must not be null, undefined or empty.");this.modelPath=e}return e.prototype.save=function(e){return l(this,void 0,void 0,function(){return c(this,function(t){if(e.modelTopology instanceof ArrayBuffer)throw new Error("BrowserLocalStorage.save() does not support saving model topology in binary formats yet.");return[2,this.databaseAction(this.modelPath,e)]})})},e.prototype.load=function(){return l(this,void 0,void 0,function(){return c(this,function(e){return[2,this.databaseAction(this.modelPath)]})})},e.prototype.databaseAction=function(e,t){var n=this;return new Promise(function(e,r){var a=n.indexedDB.open("tensorflowjs",1);a.onupgradeneeded=function(){return ru(a)},a.onsuccess=function(){var i=a.result;if(null==t){var o=i.transaction(eu,"readonly"),s=o.objectStore(eu).get(n.modelPath);s.onsuccess=function(){if(null==s.result)return i.close(),r(new Error("Cannot find model with path '"+n.modelPath+"' in IndexedDB."));e(s.result.modelArtifacts)},s.onerror=function(e){return i.close(),r(s.error)},o.oncomplete=function(){return i.close()}}else{var u,l=Ks(t),c=i.transaction(tu,"readwrite"),f=c.objectStore(tu),p=f.put({modelPath:n.modelPath,modelArtifactsInfo:l});p.onsuccess=function(){var a=(u=i.transaction(eu,"readwrite")).objectStore(eu).put({modelPath:n.modelPath,modelArtifacts:t,modelArtifactsInfo:l});a.onsuccess=function(){return e({modelArtifactsInfo:l})},a.onerror=function(e){var t=(f=c.objectStore(tu)).delete(n.modelPath);t.onsuccess=function(){return i.close(),r(a.error)},t.onerror=function(e){return i.close(),r(a.error)}}},p.onerror=function(e){return i.close(),r(p.error)},c.oncomplete=function(){null==u?i.close():u.oncomplete=function(){return i.close()}}}},a.onerror=function(e){return r(a.error)}})},e.URL_SCHEME="indexeddb://",e}(),iu=function(e){return ve.get("IS_BROWSER")&&!Array.isArray(e)&&e.startsWith(au.URL_SCHEME)?function(e){return new au(e)}(e.slice(au.URL_SCHEME.length)):null};Xs.registerSaveRouter(iu),Xs.registerLoadRouter(iu);var ou=function(){function e(){this.indexedDB=nu()}return e.prototype.listModels=function(){return l(this,void 0,void 0,function(){var e=this;return c(this,function(t){return[2,new Promise(function(t,n){var r=e.indexedDB.open("tensorflowjs",1);r.onupgradeneeded=function(){return ru(r)},r.onsuccess=function(){var e=r.result,a=e.transaction(tu,"readonly"),i=a.objectStore(tu).getAll();i.onsuccess=function(){for(var e={},n=0,r=i.result;n0,"URL path for browserHTTPRequest must not be null, undefined or empty."),Array.isArray(e)&&b(2===e.length,"URL paths for browserHTTPRequest must have a length of 2, (actual length is "+e.length+")."),this.path=e,null!=t&&null!=t.body)throw new Error("requestInit is expected to have no pre-existing body, but has one.");this.requestInit=t||{}}return e.prototype.save=function(e){return l(this,void 0,void 0,function(){var t,n,r,a;return c(this,function(i){switch(i.label){case 0:if(e.modelTopology instanceof ArrayBuffer)throw new Error("BrowserHTTPRequest.save() does not support saving model topology in binary formats yet.");return(t=Object.assign({method:this.DEFAULT_METHOD},this.requestInit)).body=new FormData,n=[{paths:["./model.weights.bin"],weights:e.weightSpecs}],r={modelTopology:e.modelTopology,weightsManifest:n},t.body.append("model.json",new Blob([JSON.stringify(r)],{type:"application/json"}),"model.json"),null!=e.weightData&&t.body.append("model.weights.bin",new Blob([e.weightData],{type:"application/octet-stream"}),"model.weights.bin"),[4,fetch(this.path,t)];case 1:if((a=i.sent()).ok)return[2,{modelArtifactsInfo:Ks(e),responses:[a]}];throw new Error("BrowserHTTPRequest.save() failed due to HTTP response status "+a.status+".")}})})},e.prototype.load=function(){return l(this,void 0,void 0,function(){return c(this,function(e){return[2,Array.isArray(this.path)?this.loadBinaryModel():this.loadJSONModel()]})})},e.prototype.loadBinaryTopology=function(){return l(this,void 0,void 0,function(){var e,t;return c(this,function(n){switch(n.label){case 0:return n.trys.push([0,3,,4]),[4,fetch(this.path[0],this.requestInit)];case 1:if(!(e=n.sent()).ok)throw new Error("BrowserHTTPRequest.load() failed due to HTTP response: "+e.statusText);return[4,e.arrayBuffer()];case 2:return[2,n.sent()];case 3:throw t=n.sent(),new Error(this.path[0]+" not found. "+t);case 4:return[2]}})})},e.prototype.loadBinaryModel=function(){return l(this,void 0,void 0,function(){var e,t,n,r,a,i,o,s;return c(this,function(u){switch(u.label){case 0:return e=this.loadBinaryTopology(),[4,fetch(this.path[1],this.requestInit)];case 1:if(!(t=u.sent()).ok)throw new Error("BrowserHTTPRequest.load() failed due to HTTP response: "+t.statusText);return[4,Promise.all([e,t])];case 2:return n=u.sent(),r=n[0],[4,n[1].json()];case 3:return null==(a=u.sent())?[3,5]:[4,this.loadWeights(a)];case 4:s=u.sent(),i=s[0],o=s[1],u.label=5;case 5:return[2,{modelTopology:r,weightSpecs:i,weightData:o}]}})})},e.prototype.loadJSONModel=function(){return l(this,void 0,void 0,function(){var e,t,n,r,a,i,o,s;return c(this,function(u){switch(u.label){case 0:return[4,fetch(this.path,this.requestInit)];case 1:if(!(e=u.sent()).ok)throw new Error("BrowserHTTPRequest.load() failed due to HTTP response: "+e.statusText);return[4,e.json()];case 2:if(t=u.sent(),n=t.modelTopology,r=t.weightsManifest,null==n&&null==r)throw new Error("The JSON from HTTP path "+this.path+" contains neither model topology or manifest for weights.");return null==r?[3,4]:(o=t.weightsManifest,[4,this.loadWeights(o)]);case 3:s=u.sent(),a=s[0],i=s[1],u.label=4;case 4:return[2,{modelTopology:n,weightSpecs:a,weightData:i}]}})})},e.prototype.loadWeights=function(e){return l(this,void 0,void 0,function(){var t,n,r,a,i,o,s,u,l,f,p,h;return c(this,function(c){switch(c.label){case 0:for(t=Array.isArray(this.path)?this.path[1]:this.path,n=function(e){var t=e.lastIndexOf("/"),n=e.lastIndexOf("?");return[e.substring(0,t)+"/",n>t?e.substring(n):""]}(t),r=n[0],a=n[1],i=this.weightPathPrefix||r,o=[],s=0,u=e;s0&&(e=u({weightSpecs:this.weightSpecs},e)),null!=this.weightData&&this.weightData.byteLength>0&&(e=u({weightData:this.weightData},e)),[2,e]})})},e}(),Cu=function(){function e(e){this.saveHandler=e}return e.prototype.save=function(e){return l(this,void 0,void 0,function(){return c(this,function(t){return[2,this.saveHandler(e)]})})},e}(),Pu=Xs.registerSaveRouter,Au=Xs.registerLoadRouter,Tu=Xs.getSaveHandlers,Mu=Xs.getLoadHandlers,Ru=Object.freeze({browserFiles:function(e){return new _u(e)},browserHTTPRequest:ku,concatenateArrayBuffers:Gs,decodeWeights:Ws,encodeWeights:function(e){return l(this,void 0,void 0,function(){var t,n,r,a;return c(this,function(i){switch(i.label){case 0:for(r in t=[],n=[],e){if("float32"!==(a=e[r]).dtype&&"int32"!==a.dtype&&"bool"!==a.dtype)throw new Error("Unsupported dtype in weight '"+r+"': "+a.dtype);t.push({name:r,shape:a.shape,dtype:a.dtype}),n.push(a.data())}return[4,Promise.all(n)];case 1:return[2,{data:function(e){if(null===e)throw new Error("Invalid input value: "+JSON.stringify(e));var t=0,n=[];e.forEach(function(e){if(t+=e.byteLength,n.push(e.byteLength===e.buffer.byteLength?e:new e.constructor(e)),!(e instanceof Float32Array||e instanceof Int32Array||e instanceof Uint8Array))throw new Error("Unsupported TypedArray subtype: "+e.constructor.name)});var r=new Uint8Array(t),a=0;return n.forEach(function(e){r.set(new Uint8Array(e.buffer),a),a+=e.byteLength}),r.buffer}(i.sent()),specs:t}]}})})},fromMemory:function(e,t,n){return new Iu(e,t,n)},getLoadHandlers:Mu,getModelArtifactsInfoForJSON:Ks,getSaveHandlers:Tu,loadWeights:function(e,t,n,r){return void 0===t&&(t=""),l(this,void 0,void 0,function(){var a,i,o,s,u,l,f,p,h,d;return c(this,function(c){switch(c.label){case 0:if(a=e.map(function(){return!1}),i={},o=null!=n?n.map(function(){return!1}):[],s=[],e.forEach(function(e,t){var r=0;e.weights.forEach(function(e){var u="quantization"in e?e.quantization.dtype:e.dtype,l=Us[u]*E(e.shape),c=function(){a[t]=!0,null==i[t]&&(i[t]=[]),i[t].push({manifestEntry:e,groupOffset:r,sizeBytes:l})};null!=n?n.forEach(function(t,n){t===e.name&&(c(),o[n]=!0)}):c(),s.push(e.name),r+=l})}),!o.every(function(e){return e}))throw u=n.filter(function(e,t){return!o[t]}),new Error("Could not find weights in manifest with names: "+u.join(", ")+". \nManifest JSON has weights with names: "+s.join(", ")+".");return l=a.reduce(function(e,t,n){return t&&e.push(n),e},[]),f=[],l.forEach(function(n){e[n].paths.forEach(function(e){var n=t+(t.endsWith("/")?"":"/")+e;f.push(n)})}),[4,Eu(f,r)];case 1:return p=c.sent(),h={},d=0,l.forEach(function(t){for(var n=e[t].paths.length,r=0,a=0;a0&&Number.isInteger(n),"If provided, numClasses must be a positive integer, but got "+n),b(1===r.rank,"Expected the rank of labels to be 1, but got "+r.rank),b(1===a.rank,"Expected the rank of predictions to be 1, but got "+a.rank),b(r.shape[0]===a.shape[0],"Mismatch in the number of examples: "+r.shape[0]+" vs. "+a.shape[0]+". Labels and predictions should have the same number of elements."),b(n>0&&Number.isInteger(n),"numClasses is required to be a positive integer, but got "+n);var i=ha(r.asType("int32"),n),o=ha(a.asType("int32"),n);return i.transpose().matMul(o).asType("int32")}}),ju=Object.freeze({confusionMatrix:Du}),Lu=function(){function e(){}return e.prototype.getClassName=function(){return this.constructor.className},e.fromConfig=function(e,t){return new e(t)},e}(),Bu=function(){function e(){this.classNameMap={}}return e.getMap=function(){return null==e.instance&&(e.instance=new e),e.instance},e.register=function(t){e.getMap().classNameMap[t.className]=[t,t.fromConfig]},e}();function zu(e){b(null!=e.className,"Class being registered does not have the static className property defined."),b("string"==typeof e.className,"className is required to be a string, but got type "+typeof e.className),b(e.className.length>0,"Class being registered has an empty-string as its className, which is disallowed."),Bu.register(e)}var Fu=Object.freeze({Serializable:Lu,SerializationMap:Bu,registerClass:zu});function Vu(e,t,n){if(null==n&&(n=ve.get("TEST_EPSILON")),e instanceof re||t instanceof re){if(e instanceof re&&t instanceof re){if(e.dtype!==t.dtype)throw new Error("Arrays are of different type actual: "+e.dtype+" vs expected: "+t.dtype+".");if(!N(e.shape,t.shape))throw new Error("Arrays are of different shape actual: "+e.shape+" vs expected: "+t.shape+".")}}else{var r=e.constructor.name,a=t.constructor.name;if(r!==a)throw new Error("Arrays are of different type actual: "+r+" vs expected: "+a)}var i,o;if(i=e instanceof re?e.dataSync():e,o=t instanceof re?t.dataSync():t,i.length!==o.length)throw new Error("Arrays have different lengths actual: "+i.length+" vs expected: "+o.length+".\nActual: "+i+".\nExpected: "+o+".");for(var s=0;sn)}var Wu=Object.freeze({WEBGL_ENVS:{HAS_WEBGL:!0},NODE_ENVS:{IS_NODE:!0},CHROME_ENVS:{IS_CHROME:!0},BROWSER_ENVS:{IS_BROWSER:!0},CPU_ENVS:{HAS_WEBGL:!1},BROWSER_CPU_ENVS:{BACKEND:"test-cpu"},ALL_ENVS:{},expectArraysClose:Vu,expectPromiseToFail:function(e,t){e().then(function(){return t.fail()},function(){return t()})},expectArraysEqual:function(e,t){return Vu(e,t,0)},expectNumbersClose:function(e,t,n){if(null==n&&(n=ve.get("TEST_EPSILON")),!Uu(e,t,n))throw new Error("Numbers differ: actual === "+e+", expected === "+t)},expectValuesInRange:function(e,t,n){var r;r=e instanceof re?e.dataSync():e;for(var a=0;an)throw new Error("Value out of range:"+r[a]+" low: "+t+", high: "+n)},expectArrayBuffersEqual:function(e,t){expect(new Float32Array(e)).toEqual(new Float32Array(t))}}),qu="0.13.11",$u=Object.freeze({gpgpu_util:_r,webgl_util:tr,MathBackendWebGL:Ta,GPGPUContext:Er}),Gu=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return s(t,e),t.prototype.minimize=function(e,t,n){void 0===t&&(t=!1);var r=this.computeGradients(e,n),a=r.value,i=r.grads;return this.applyGradients(i),Object.keys(i).forEach(function(e){return i[e].dispose()}),t?a:(a.dispose(),null)},t.prototype.computeGradients=function(e,t){return Ne(e,t)},t}(Lu),Hu=function(e){function t(t,n,r){void 0===r&&(r=null);var a=e.call(this)||this;return a.learningRate=t,a.rho=n,a.epsilon=r,a.accumulatedGrads={},a.accumulatedUpdates={},a.c=Ie(ct(-t)),a.rhoScalar=Ie(ct(n)),a.oneMinusRho=Ie(ct(1-n)),null===r&&(r=ve.get("EPSILON")),a.epsilonScalar=Ie(ct(r)),a}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=ve.engine.registeredVariables[n];null==r.accumulatedGrads[n]&&ke(function(){t.accumulatedGrads[n]=Ct(a).variable(!1)}),null==r.accumulatedUpdates[n]&&ke(function(){t.accumulatedUpdates[n]=Ct(a).variable(!1)});var i=e[n],o=r.accumulatedGrads[n],s=r.accumulatedUpdates[n];ke(function(){var e=t.rhoScalar.mul(o).add(t.oneMinusRho.mul(i.square())),r=s.add(t.epsilonScalar).sqrt().div(o.add(t.epsilonScalar).sqrt()).mul(i),u=t.rhoScalar.mul(s).add(t.oneMinusRho.mul(r.square()));t.accumulatedGrads[n].assign(e),t.accumulatedUpdates[n].assign(u);var l=t.c.mul(r).add(a);a.assign(l)})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsilonScalar.dispose(),this.rhoScalar.dispose(),this.oneMinusRho.dispose(),null!=this.accumulatedUpdates&&(Object.keys(this.accumulatedUpdates).forEach(function(t){return e.accumulatedUpdates[t].dispose()}),Object.keys(this.accumulatedGrads).forEach(function(t){return e.accumulatedGrads[t].dispose()}))},t.prototype.getConfig=function(){return{learningRate:this.learningRate,rho:this.rho,epsilon:this.epsilon}},t.fromConfig=function(e,t){return new e(t.learningRate,t.rho,t.epsilon)},t.className="AdadeltaOptimizer",t}(Gu);zu(Hu);var Ku=function(e){function t(t,n){void 0===n&&(n=.1);var r=e.call(this)||this;return r.learningRate=t,r.initialAccumulatorValue=n,r.accumulatedGrads={},r.c=Ie(ct(-t)),r.epsilon=Ie(ct(ve.get("EPSILON"))),r}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=ve.engine.registeredVariables[n];null==r.accumulatedGrads[n]&&ke(function(){t.accumulatedGrads[n]=bt(a.shape,t.initialAccumulatorValue).variable(!1)});var i=e[n],o=r.accumulatedGrads[n];ke(function(){var e=o.add(i.square());t.accumulatedGrads[n].assign(e);var r=t.c.mul(i.div(e.add(t.epsilon).sqrt())).add(a);a.assign(r)})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){var e=this;this.epsilon.dispose(),this.c.dispose(),null!=this.accumulatedGrads&&Object.keys(this.accumulatedGrads).forEach(function(t){return e.accumulatedGrads[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,initialAccumulatorValue:this.initialAccumulatorValue}},t.fromConfig=function(e,t){return new e(t.learningRate,t.initialAccumulatorValue)},t.className="AdagradOptimizer",t}(Gu);zu(Ku);var Xu=function(e){function t(t,n,r,a){void 0===a&&(a=null);var i=e.call(this)||this;return i.learningRate=t,i.beta1=n,i.beta2=r,i.epsilon=a,i.accumulatedFirstMoment={},i.accumulatedSecondMoment={},i.c=Ie(ct(-t)),i.beta1Scalar=Ie(ct(n)),i.beta2Scalar=Ie(ct(r)),ke(function(){i.accBeta1=ct(n).variable(),i.accBeta2=ct(r).variable()}),i.oneMinusBeta1=Ie(ct(1-n)),i.oneMinusBeta2=Ie(ct(1-r)),i.one=Ie(ct(1)),null===a&&(a=ve.get("EPSILON")),i.epsScalar=Ie(ct(a)),i}return s(t,e),t.prototype.applyGradients=function(e){var t=this;ke(function(){var n=t.one.sub(t.accBeta1),r=t.one.sub(t.accBeta2);for(var a in e){var i=ve.engine.registeredVariables[a];if(null==t.accumulatedFirstMoment[a]){var o=!1;t.accumulatedFirstMoment[a]=Ct(i).variable(o)}null==t.accumulatedSecondMoment[a]&&(o=!1,t.accumulatedSecondMoment[a]=Ct(i).variable(o));var s=e[a],u=t.accumulatedFirstMoment[a],l=t.accumulatedSecondMoment[a],c=t.beta1Scalar.mul(u).add(t.oneMinusBeta1.mul(s)),f=t.beta2Scalar.mul(l).add(t.oneMinusBeta2.mul(s.square())),p=c.div(n),h=f.div(r);t.accumulatedFirstMoment[a].assign(c),t.accumulatedSecondMoment[a].assign(f);var d=t.c.mul(p.div(t.epsScalar.add(h.sqrt()))).add(i);i.assign(d)}t.accBeta1.assign(t.accBeta1.mul(t.beta1Scalar)),t.accBeta2.assign(t.accBeta2.mul(t.beta2Scalar))})},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsScalar.dispose(),this.beta1Scalar.dispose(),this.beta2Scalar.dispose(),this.accBeta1.dispose(),this.accBeta2.dispose(),this.oneMinusBeta1.dispose(),this.oneMinusBeta2.dispose(),this.one.dispose(),null!=this.accumulatedFirstMoment&&Object.keys(this.accumulatedFirstMoment).forEach(function(t){return e.accumulatedFirstMoment[t].dispose()}),null!=this.accumulatedSecondMoment&&Object.keys(this.accumulatedSecondMoment).forEach(function(t){return e.accumulatedSecondMoment[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon}},t.fromConfig=function(e,t){return new e(t.learningRate,t.beta1,t.beta2,t.epsilon)},t.className="AdamOptimizer",t}(Gu);zu(Xu);var Yu=function(e){function t(t,n,r,a,i){void 0===a&&(a=null),void 0===i&&(i=0);var o=e.call(this)||this;return o.learningRate=t,o.beta1=n,o.beta2=r,o.epsilon=a,o.decay=i,o.accumulatedFirstMoment={},o.accumulatedWeightedInfNorm={},o.c=Ie(ct(-t)),o.beta1Scalar=Ie(ct(n)),o.beta2Scalar=Ie(ct(r)),o.decayScalar=Ie(ct(i)),ke(function(){o.iteration=ct(0).variable(),o.accBeta1=ct(n).variable()}),o.oneMinusBeta1=Ie(ct(1-n)),o.one=Ie(ct(1)),null===a&&(a=ve.get("EPSILON")),o.epsScalar=Ie(ct(a)),o}return s(t,e),t.prototype.applyGradients=function(e){var t=this;ke(function(){var n=t.one.sub(t.accBeta1),r=t.c.div(t.one.add(t.decayScalar.mul(t.iteration)));for(var a in e){var i=ve.engine.registeredVariables[a];if(null==t.accumulatedFirstMoment[a]){var o=!1;t.accumulatedFirstMoment[a]=Ct(i).variable(o)}null==t.accumulatedWeightedInfNorm[a]&&(o=!1,t.accumulatedWeightedInfNorm[a]=Ct(i).variable(o));var s=e[a],u=t.accumulatedFirstMoment[a],l=t.accumulatedWeightedInfNorm[a],c=t.beta1Scalar.mul(u).add(t.oneMinusBeta1.mul(s)),f=t.beta2Scalar.mul(l),p=s.abs(),h=f.maximum(p);t.accumulatedFirstMoment[a].assign(c),t.accumulatedWeightedInfNorm[a].assign(h);var d=r.div(n).mul(c.div(t.epsScalar.add(h))).add(i);i.assign(d)}t.iteration.assign(t.iteration.add(t.one)),t.accBeta1.assign(t.accBeta1.mul(t.beta1Scalar))})},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsScalar.dispose(),this.accBeta1.dispose(),this.beta1Scalar.dispose(),this.beta2Scalar.dispose(),this.oneMinusBeta1.dispose(),this.decayScalar.dispose(),this.iteration.dispose(),this.one.dispose(),null!=this.accumulatedFirstMoment&&Object.keys(this.accumulatedFirstMoment).forEach(function(t){return e.accumulatedFirstMoment[t].dispose()}),null!=this.accumulatedWeightedInfNorm&&Object.keys(this.accumulatedWeightedInfNorm).forEach(function(t){return e.accumulatedWeightedInfNorm[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon,decay:this.decay}},t.fromConfig=function(e,t){return new e(t.learningRate,t.beta1,t.beta2,t.epsilon,t.decay)},t.className="AdamaxOptimizer",t}(Gu);zu(Yu);var Ju=function(e){function t(t){var n=e.call(this)||this;return n.learningRate=t,n.setLearningRate(t),n}return s(t,e),t.prototype.applyGradients=function(e){var t=this;Object.keys(e).forEach(function(n){var r=e[n],a=ve.engine.registeredVariables[n];ke(function(){var e=t.c.mul(r).add(a);a.assign(e)})})},t.prototype.setLearningRate=function(e){this.learningRate=e,null!=this.c&&this.c.dispose(),this.c=Ie(ct(-e))},t.prototype.dispose=function(){this.c.dispose()},t.prototype.getConfig=function(){return{learningRate:this.learningRate}},t.fromConfig=function(e,t){return new e(t.learningRate)},t.className="SGDOptimizer",t}(Gu);zu(Ju);var Zu=function(e){function t(t,n,r){void 0===r&&(r=!1);var a=e.call(this,t)||this;return a.learningRate=t,a.momentum=n,a.useNesterov=r,a.m=ct(a.momentum),a.accumulations={},a}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=ve.engine.registeredVariables[n];null==r.accumulations[n]&&ke(function(){t.accumulations[n]=Ct(a).variable(!1)});var i=r.accumulations[n],o=e[n];ke(function(){var e,r=t.m.mul(i).add(o);e=t.useNesterov?t.c.mul(o.add(r.mul(t.m))).add(a):t.c.mul(r).add(a),t.accumulations[n].assign(r),a.assign(e)})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){if(e.prototype.dispose.call(this),this.m.dispose(),null!=this.accumulations)for(var t in this.accumulations)this.accumulations[t].dispose()},t.prototype.setMomentum=function(e){this.momentum=e},t.prototype.getConfig=function(){return{learningRate:this.learningRate,momentum:this.momentum,useNesterov:this.useNesterov}},t.fromConfig=function(e,t){return new e(t.learningRate,t.momentum,t.useNesterov)},t.className="MomentumOptimizer",t}(Ju);zu(Zu);var Qu=function(e){function t(t,n,r,a,i){void 0===n&&(n=.9),void 0===r&&(r=0),void 0===a&&(a=null),void 0===i&&(i=!1);var o=e.call(this)||this;return o.learningRate=t,o.decay=n,o.momentum=r,o.epsilon=a,o.accumulatedMeanSquares={},o.accumulatedMeanGrads={},o.accumulatedMoments={},o.c=Ie(ct(t)),o.decayScalar=Ie(ct(n)),o.momentumScalar=Ie(ct(r)),o.oneMinusDecay=Ie(ct(1-n)),o.centered=i,null===a&&(a=ve.get("EPSILON")),o.epsilonScalar=Ie(ct(a)),o}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=ve.engine.registeredVariables[n];null==r.accumulatedMeanSquares[n]&&ke(function(){t.accumulatedMeanSquares[n]=Ct(a).variable(!1)}),null==r.accumulatedMeanGrads[n]&&r.centered&&ke(function(){t.accumulatedMeanGrads[n]=Ct(a).variable(!1)}),null==r.accumulatedMoments[n]&&ke(function(){t.accumulatedMoments[n]=Ct(a).variable(!1)});var i=r.accumulatedMeanSquares[n],o=r.accumulatedMeanGrads[n],s=r.accumulatedMoments[n],u=e[n];ke(function(){var e=t.decayScalar.mul(i).add(t.oneMinusDecay.mul(u.square()));if(t.centered){var r=t.decayScalar.mul(o).add(t.oneMinusDecay.mul(u)),l=t.momentumScalar.mul(s).add(t.c.mul(u).div(e.sub(r.square().add(t.epsilonScalar)).sqrt()));t.accumulatedMeanSquares[n].assign(e),t.accumulatedMeanGrads[n].assign(r),t.accumulatedMoments[n].assign(l);var c=a.sub(l);a.assign(c)}else{var f=t.decayScalar.mul(i).add(t.oneMinusDecay.mul(u.square()));l=t.momentumScalar.mul(s).add(t.c.mul(u).div(f.add(t.epsilonScalar).sqrt())),t.accumulatedMeanSquares[n].assign(f),t.accumulatedMoments[n].assign(l),c=a.sub(l),a.assign(c)}})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsilonScalar.dispose(),this.decayScalar.dispose(),this.momentumScalar.dispose(),this.oneMinusDecay.dispose(),null!=this.accumulatedMeanSquares&&Object.keys(this.accumulatedMeanSquares).forEach(function(t){return e.accumulatedMeanSquares[t].dispose()}),null!=this.accumulatedMeanGrads&&this.centered&&Object.keys(this.accumulatedMeanGrads).forEach(function(t){return e.accumulatedMeanGrads[t].dispose()}),null!=this.accumulatedMoments&&Object.keys(this.accumulatedMoments).forEach(function(t){return e.accumulatedMoments[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,decay:this.decay,momentum:this.momentum,epsilon:this.epsilon,centered:this.centered}},t.fromConfig=function(e,t){return new e(t.learningRate,t.decay,t.momentum,t.epsilon,t.centered)},t.className="RMSPropOptimizer",t}(Gu);zu(Qu);var el=function(){function e(){}return e.sgd=function(e){return new Ju(e)},e.momentum=function(e,t,n){return void 0===n&&(n=!1),new Zu(e,t,n)},e.rmsprop=function(e,t,n,r,a){return void 0===t&&(t=.9),void 0===n&&(n=0),void 0===r&&(r=null),void 0===a&&(a=!1),new Qu(e,t,n,r,a)},e.adam=function(e,t,n,r){return void 0===e&&(e=.001),void 0===t&&(t=.9),void 0===n&&(n=.999),void 0===r&&(r=null),new Xu(e,t,n,r)},e.adadelta=function(e,t,n){return void 0===e&&(e=.001),void 0===t&&(t=.95),void 0===n&&(n=null),new Hu(e,t,n)},e.adamax=function(e,t,n,r,a){return void 0===e&&(e=.002),void 0===t&&(t=.9),void 0===n&&(n=.999),void 0===r&&(r=null),void 0===a&&(a=0),new Yu(e,t,n,r,a)},e.adagrad=function(e,t){return void 0===t&&(t=.1),new Ku(e,t)},e}(),tl={sgd:el.sgd,momentum:el.momentum,adadelta:el.adadelta,adagrad:el.adagrad,rmsprop:el.rmsprop,adamax:el.adamax,adam:el.adam},nl=ye.setBackend,rl=ye.getBackend,al=ye.disposeVariables,il=ye.memory;te=Bs}.call(this,n(46),n(26),n(158).setImmediate,n(16).Buffer)},function(e,t,n){"use strict";(function(e,r,a,i){n.d(t,"Fc",function(){return vu}),n.d(t,"Ya",function(){return bu}),n.d(t,"Ga",function(){return wu}),n.d(t,"Gb",function(){return xu}),n.d(t,"Dd",function(){return ou}),n.d(t,"Tb",function(){return is}),n.d(t,"La",function(){return ge}),n.d(t,"hb",function(){return Zs}),n.d(t,"Ec",function(){return nu}),n.d(t,"nd",function(){return iu}),n.d(t,"yd",function(){return V}),n.d(t,"Ed",function(){return su}),n.d(t,"a",function(){return lu}),n.d(t,"b",function(){return cu}),n.d(t,"c",function(){return fu}),n.d(t,"d",function(){return pu}),n.d(t,"g",function(){return du}),n.d(t,"h",function(){return uu}),n.d(t,"i",function(){return mu}),n.d(t,"l",function(){return hu}),n.d(t,"m",function(){return Q}),n.d(t,"n",function(){return X}),n.d(t,"Bd",function(){return te}),n.d(t,"o",function(){return ee}),n.d(t,"j",function(){return gt}),n.d(t,"k",function(){return Co}),n.d(t,"e",function(){return me}),n.d(t,"f",function(){return de}),n.d(t,"gb",function(){return ts}),n.d(t,"ob",function(){return Yo}),n.d(t,"zb",function(){return Go}),n.d(t,"ac",function(){return Je}),n.d(t,"H",function(){return qa}),n.d(t,"I",function(){return $a}),n.d(t,"J",function(){return Ga}),n.d(t,"G",function(){return Ha}),n.d(t,"Q",function(){return Qe}),n.d(t,"qc",function(){return et}),n.d(t,"fb",function(){return tt}),n.d(t,"R",function(){return vr}),n.d(t,"S",function(){return br}),n.d(t,"T",function(){return wr}),n.d(t,"U",function(){return xr}),n.d(t,"V",function(){return _r}),n.d(t,"Sc",function(){return Er}),n.d(t,"W",function(){return ii}),n.d(t,"X",function(){return oi}),n.d(t,"Ea",function(){return si}),n.d(t,"Dc",function(){return ui}),n.d(t,"Y",function(){return li}),n.d(t,"Ab",function(){return Qa}),n.d(t,"Ja",function(){return ei}),n.d(t,"bc",function(){return ti}),n.d(t,"uc",function(){return ci}),n.d(t,"vc",function(){return fi}),n.d(t,"wc",function(){return pi}),n.d(t,"xc",function(){return hi}),n.d(t,"yc",function(){return di}),n.d(t,"Cb",function(){return mi}),n.d(t,"E",function(){return gi}),n.d(t,"Kc",function(){return yi}),n.d(t,"Lc",function(){return vi}),n.d(t,"Mc",function(){return bi}),n.d(t,"Nc",function(){return wi}),n.d(t,"Oc",function(){return xi}),n.d(t,"p",function(){return fa}),n.d(t,"q",function(){return pa}),n.d(t,"r",function(){return ha}),n.d(t,"z",function(){return da}),n.d(t,"A",function(){return ma}),n.d(t,"B",function(){return ga}),n.d(t,"D",function(){return ya}),n.d(t,"N",function(){return va}),n.d(t,"O",function(){return ba}),n.d(t,"Z",function(){return wa}),n.d(t,"Aa",function(){return xa}),n.d(t,"Oa",function(){return _a}),n.d(t,"Pa",function(){return Ea}),n.d(t,"Ra",function(){return Na}),n.d(t,"Ua",function(){return Sa}),n.d(t,"rb",function(){return Oa}),n.d(t,"sb",function(){return ka}),n.d(t,"tb",function(){return Ia}),n.d(t,"Sb",function(){return Ca}),n.d(t,"rc",function(){return Pa}),n.d(t,"zc",function(){return Aa}),n.d(t,"Ac",function(){return Ta}),n.d(t,"Gc",function(){return Ma}),n.d(t,"Hc",function(){return Ra}),n.d(t,"Ic",function(){return Da}),n.d(t,"Jc",function(){return ja}),n.d(t,"Qc",function(){return La}),n.d(t,"Tc",function(){return Ba}),n.d(t,"Uc",function(){return za}),n.d(t,"Zc",function(){return Fa}),n.d(t,"ed",function(){return Va}),n.d(t,"fd",function(){return Ua}),n.d(t,"v",function(){return Ei}),n.d(t,"w",function(){return Ni}),n.d(t,"x",function(){return Si}),n.d(t,"y",function(){return Oi}),n.d(t,"ub",function(){return ki}),n.d(t,"Bb",function(){return Ii}),n.d(t,"Fb",function(){return Ci}),n.d(t,"Hb",function(){return Pi}),n.d(t,"Mb",function(){return Ai}),n.d(t,"dd",function(){return Ti}),n.d(t,"Ma",function(){return Mi}),n.d(t,"Na",function(){return Ri}),n.d(t,"bb",function(){return Di}),n.d(t,"cb",function(){return ji}),n.d(t,"db",function(){return Li}),n.d(t,"eb",function(){return Bi}),n.d(t,"kb",function(){return zi}),n.d(t,"lb",function(){return Fi}),n.d(t,"mb",function(){return Vi}),n.d(t,"nb",function(){return Ui}),n.d(t,"Vb",function(){return Wi}),n.d(t,"Wb",function(){return qi}),n.d(t,"s",function(){return $i}),n.d(t,"t",function(){return Gi}),n.d(t,"u",function(){return Hi}),n.d(t,"C",function(){return Ki}),n.d(t,"Ha",function(){return Xi}),n.d(t,"Ia",function(){return Yi}),n.d(t,"Va",function(){return Ji}),n.d(t,"Db",function(){return Zi}),n.d(t,"Eb",function(){return Qi}),n.d(t,"Ib",function(){return eo}),n.d(t,"Jb",function(){return to}),n.d(t,"Kb",function(){return no}),n.d(t,"Lb",function(){return ro}),n.d(t,"Ob",function(){return ao}),n.d(t,"Pb",function(){return io}),n.d(t,"hc",function(){return oo}),n.d(t,"ic",function(){return so}),n.d(t,"Vc",function(){return uo}),n.d(t,"Wc",function(){return lo}),n.d(t,"bd",function(){return co}),n.d(t,"cd",function(){return fo}),n.d(t,"Ka",function(){return bo}),n.d(t,"jb",function(){return wo}),n.d(t,"jc",function(){return xo}),n.d(t,"sc",function(){return _o}),n.d(t,"Cc",function(){return Eo}),n.d(t,"vb",function(){return po}),n.d(t,"wb",function(){return ho}),n.d(t,"xb",function(){return mo}),n.d(t,"yb",function(){return go}),n.d(t,"Fd",function(){return yo}),n.d(t,"Gd",function(){return vo}),n.d(t,"L",function(){return Dr}),n.d(t,"rd",function(){return Rr}),n.d(t,"kc",function(){return jr}),n.d(t,"K",function(){return Lr}),n.d(t,"M",function(){return Br}),n.d(t,"P",function(){return zr}),n.d(t,"Ba",function(){return Fr}),n.d(t,"Da",function(){return Vr}),n.d(t,"Qa",function(){return Ur}),n.d(t,"Sa",function(){return Wr}),n.d(t,"Wa",function(){return qr}),n.d(t,"Rb",function(){return $r}),n.d(t,"Xb",function(){return Gr}),n.d(t,"cc",function(){return Hr}),n.d(t,"dc",function(){return Kr}),n.d(t,"ec",function(){return Xr}),n.d(t,"fc",function(){return Yr}),n.d(t,"gc",function(){return Jr}),n.d(t,"mc",function(){return Zr}),n.d(t,"nc",function(){return Qr}),n.d(t,"oc",function(){return ea}),n.d(t,"tc",function(){return ta}),n.d(t,"Rc",function(){return na}),n.d(t,"Xc",function(){return ra}),n.d(t,"Yc",function(){return aa}),n.d(t,"pd",function(){return ia}),n.d(t,"vd",function(){return oa}),n.d(t,"xd",function(){return sa}),n.d(t,"Ta",function(){return pt}),n.d(t,"pb",function(){return ht}),n.d(t,"Yb",function(){return ct}),n.d(t,"pc",function(){return dt}),n.d(t,"Bc",function(){return rt}),n.d(t,"gd",function(){return nt}),n.d(t,"hd",function(){return at}),n.d(t,"id",function(){return it}),n.d(t,"jd",function(){return ot}),n.d(t,"kd",function(){return st}),n.d(t,"ld",function(){return ut}),n.d(t,"md",function(){return lt}),n.d(t,"Hd",function(){return ft}),n.d(t,"Zb",function(){return xt}),n.d(t,"Id",function(){return _t}),n.d(t,"ud",function(){return No}),n.d(t,"Pc",function(){return Ze}),n.d(t,"qb",function(){return So}),n.d(t,"Ub",function(){return Oo}),n.d(t,"Xa",function(){return Po}),n.d(t,"wd",function(){return Ao}),n.d(t,"F",function(){return To}),n.d(t,"Qb",function(){return Mo}),n.d(t,"Nb",function(){return Ro}),n.d(t,"ad",function(){return Do}),n.d(t,"sd",function(){return jo}),n.d(t,"td",function(){return yu}),n.d(t,"od",function(){return Ne}),n.d(t,"ib",function(){return Se}),n.d(t,"Fa",function(){return Oe}),n.d(t,"qd",function(){return ke}),n.d(t,"lc",function(){return Ie}),n.d(t,"Ca",function(){return _e}),n.d(t,"Za",function(){return ye}),n.d(t,"ab",function(){return ve}),n.d(t,"zd",function(){return be}),n.d(t,"Ad",function(){return we}),n.d(t,"Cd",function(){return xe});
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
@@ -32,7 +32,7 @@ var o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__p
* limitations under the License.
* =============================================================================
*/
-var o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])};function s(e,t){function n(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var u=Object.assign||function(e){for(var t,n=1,r=arguments.length;n0&&a[a.length-1])&&(6===i[0]||2===i[0])){o=0;continue}if(3===i[0]&&(!a||i[1]>a[0]&&i[1]0;)r=Math.random()*t|0,n=e[--t],e[t]=e[r],e[r]=n}function p(e,t,n){return Math.max(e,Math.min(t,n))}function h(e,t){var n=Math.random();return t*n+(1-n)*e}function d(e,t){if(!e)throw new Error("string"==typeof t?t:t())}function m(e,t,n){void 0===n&&(n=""),d(w(e,t),n+" Shapes "+e+" and "+t+" must match")}function g(e){d(null!=e,"The input to the tensor constructor must be a non-null value.")}function y(e,t){if(void 0===t&&(t=[]),Array.isArray(e))for(var n=0;n0,function(){return"Element arr["+r.join("][")+"] should be a primitive, but is an array of "+t.length+" elements"}),d(t.length===n[0],function(){return"Element arr["+r.join("][")+"] should have "+n[0]+" elements, but has "+t.length+" elements"});for(var a=n.slice(1),i=0;i1;--t)if(e%t==0)return[t,e/t];return[1,e]}function N(e,t){return t<=e.length?e:e+" ".repeat(t-e.length)}function S(e,t,n){return void 0===t&&(t=function(e){return 0}),new Promise(function(r,a){var i=0,o=function(){if(e())r();else{var s=t(++i);null!=n&&i>=n?a():setTimeout(o,s)}};o()})}function O(e,t){for(var n=1,r=-1,a=0;a=0)n*=e[a];else if(-1===e[a]){if(-1!==r)throw Error("Shapes can only have 1 implicit size. Found -1 at dim "+r+" and dim "+a);r=a}else if(e[a]<0)throw Error("Shapes can not be < 0. Found "+e[a]+" at dim "+a);if(-1===r){if(t>0&&t!==n)throw Error("Size("+t+") must match the product of shape "+e);return e}if(0===n)throw Error("Cannot infer the missing size in ["+e+"] when there are 0 elements");if(t%n!=0)throw Error("The implicit shape can't be a fractional number. Got "+t+" / "+n);var i=e.slice();return i[r]=t/n,i}function k(e,t){for(var n=[],r=[],a=0,i=0;ii)&&1===e[i]&&(n.push(e[i]),r.push(i)),t[a]<=i&&a++}1!==e[i]&&(n.push(e[i]),r.push(i))}return{newShape:n,keptDims:r}}function I(e,t){var n=null;if(null==e||"float32"===e)n=new Float32Array(t);else if("int32"===e)n=new Int32Array(t);else{if("bool"!==e)throw new Error("Unknown data type "+e);n=new Uint8Array(t)}return n}function C(e,t,n){if("float32"===t)for(var r=0;r=0;--r)n[r]=n[r+1]*e[r+1];return n}function L(e,t,n){return function(e,t){return e instanceof Float32Array&&"float32"===t||e instanceof Int32Array&&"int32"===t||e instanceof Uint8Array&&"bool"===t}(e,t)?e:(Array.isArray(e)&&(e=y(e)),function(e,t,n){if(null==t||"float32"===t||"complex64"===t)return new Float32Array(e);if("int32"===t)return n&&P(e,t),new Int32Array(e);if("bool"===t){for(var r=new Uint8Array(e.length),a=0;a1)for(var l=0;lq){var c=$*s,f=Array.from(t.subarray(0,c)),p=Array.from(t.subarray(u-$*s,u));return"complex64"===r&&(f=K(f),p=K(p)),["["+f.map(function(e,t){return H(e,i[t])}).join(", ")+", ..., "+p.map(function(e,t){return H(e,i[u-$+t])}).join(", ")+"]"]}return["["+("complex64"===r?K(t):Array.from(t)).map(function(e,t){return H(e,i[t])}).join(", ")+"]"]}var h=n.slice(1),d=a.slice(1),m=a[0]*s,g=[];if(u>q){for(var y=0;y<$;y++){var v=(b=y*m)+m;g.push.apply(g,e(t.subarray(b,v),h,r,d,i,!1))}for(g.push("..."),y=u-$;y {...}) to avoid memory leaks.");return this.keepTensors.add(e.id),e},e.prototype.startScope=function(e,t){void 0===t&&(t=!1),t&&0===this.gradientScopeCount&&(this.activeTape=[]),t&&this.gradientScopeCount++;var n={track:[],name:"unnamed scope"};e&&(n.name=e),this.scopeStack.push(n),this.activeScope=n},e.prototype.endScope=function(e,t){var n=this;void 0===t&&(t=!1),t&&(this.gradientScopeCount--,0===this.gradientScopeCount&&(this.activeTape=null));var r=new Set(this.keepTensors),a=re(e);a.forEach(function(e){return r.add(e.id)});for(var i=0;i0,"gradients() received an empty list of xs."),this.tidy("gradients",function(){var i=e();d(i instanceof Q,"The result y returned by f() must be a tensor.");var o=function(e,t,n){for(var r={},a={},i=0;i=0;i--)for(o=(d=e[i]).inputs,c=0;c0)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that the f you passed encloses all operations that lead from x to y.");var s={};return s[i.id]=null==n?function(e){var t=B(b(e),"float32");return Q.make(e,{values:t})}(i.shape):n,function(e,t){for(var n=function(n){var r=t[n],a=[];if(r.outputs.forEach(function(t){var n=e[t.id];if(null!=n)a.push(n);else{var r=Q.make(t.shape,{values:z(t.size,t.dtype)},t.dtype);a.push(r)}}),null==r.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+r.name+".");var i=r.gradient(1===r.outputs.length?a[0]:a);for(var o in r.inputs){if(!(o in i))throw new Error("Cannot backprop through input "+o+". Available gradients found: "+Object.keys(i)+".");var s=i[o](),u=r.inputs[o];if(!w(s.shape,u.shape))throw new Error("Error in gradient for op "+r.name+". The gradient of input '"+o+"' has shape '"+s.shape+"', which does not match the shape of the input '"+u.shape+"'");if(null==e[u.id])e[u.id]=s;else{var l=e[u.id];e[u.id]=l.add(s),l.dispose()}}},r=t.length-1;r>=0;r--)n(r)}(s,o),{value:i,grads:t.map(function(e){return s[e.id]})}},!0)},e.prototype.customGrad=function(e){var t=this;return d(R(e),"The f passed in customGrad(f) must be a function."),function(){for(var n,r,a=[],i=0;i {op();...}); to avoid memory leaks.");return this.activeScope.track.push(e),e},e}();!function(e){e[e.NUMBER=0]="NUMBER",e[e.BOOLEAN=1]="BOOLEAN",e[e.STRING=2]="STRING"}(ae||(ae={}));var oe=[{name:"DEBUG",type:ae.BOOLEAN},{name:"IS_BROWSER",type:ae.BOOLEAN},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION",type:ae.NUMBER},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE",type:ae.BOOLEAN},{name:"WEBGL_VERSION",type:ae.NUMBER},{name:"WEBGL_RENDER_FLOAT32_ENABLED",type:ae.BOOLEAN},{name:"WEBGL_DOWNLOAD_FLOAT_ENABLED",type:ae.BOOLEAN},{name:"WEBGL_FENCE_API_ENABLED",type:ae.BOOLEAN},{name:"BACKEND",type:ae.STRING},{name:"EPSILON",type:ae.NUMBER}];function se(e,t){var n;try{n=fe(e,t)}catch(e){return!1}return null!=n&&(pe(n),!0)}var ue="tfjsflags";function le(){var e={};if("undefined"==typeof window||void 0===window.location)return e;var t=function(e){var t={};return window.location.search.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,function(e){for(var n=[],r=1;r0?0:function(e,t){if(0===e)return 0;var n,r=fe(e,t);return n=ce(r,"EXT_disjoint_timer_query_webgl2")&&2===e?2:ce(r,"EXT_disjoint_timer_query")?1:0,null!=r&&pe(r),n}(n,this.get("IS_BROWSER"))}if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE"===t)return this.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0&&!function(){var e=navigator.userAgent||navigator.vendor||window.opera;return/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(e)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(e.substr(0,4))}();if("HAS_WEBGL"===t)return this.get("WEBGL_VERSION")>0;if("WEBGL_VERSION"===t)return se(2,this.get("IS_BROWSER"))?2:se(1,this.get("IS_BROWSER"))?1:0;if("WEBGL_RENDER_FLOAT32_ENABLED"===t)return function(e,t){if(0===e)return!1;var n=fe(e,t);if(1===e){if(!ce(n,"OES_texture_float"))return!1}else if(!ce(n,"EXT_color_buffer_float"))return!1;var r=he(n,e);return pe(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("WEBGL_DOWNLOAD_FLOAT_ENABLED"===t)return function(e,t){if(0===e)return!1;var n=fe(e,t);if(1===e){if(!ce(n,"OES_texture_float"))return!1;if(!ce(n,"WEBGL_color_buffer_float"))return!1}else if(!ce(n,"EXT_color_buffer_float"))return!1;var r=he(n,e);return pe(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("WEBGL_FENCE_API_ENABLED"===t)return function(e,t){if(2!==e)return!1;var n=fe(e,t),r=null!=n.fenceSync;return pe(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("TEST_EPSILON"===t)return 32===this.backend.floatPrecision()?.001:.1;if("EPSILON"===t)return 32===this.backend.floatPrecision()?1e-7:.001;throw new Error("Unknown feature "+t+".")},t.prototype.setFeatures=function(e){this.features=Object.assign({},e)},t.prototype.reset=function(){this.features=le(),null!=this.globalEngine&&(this.globalEngine=null)},t.prototype.initBackend=function(e,t){var n=this;if(void 0===t&&(t=!1),this.backendName=e,this.engines[e])this.globalEngine=this.engines[e];else{var r=this.findBackend(e);this.globalEngine=new ie(r,t,function(){return n.get("DEBUG")}),this.engines[e]=this.globalEngine}},Object.defineProperty(t.prototype,"backend",{get:function(){return this.engine.backend},enumerable:!0,configurable:!0}),t.prototype.findBackend=function(e){return e in this.registry?this.registry[e].backend:null},t.prototype.registerBackend=function(e,t,n,r){var a=this;if(void 0===n&&(n=1),e in this.registry)return console.warn(e+" backend was already registered. Reusing existing backend"),null!=r&&r(function(){return a.engine}),!1;try{var i=t();return this.registry[e]={backend:i,priority:n},!0}catch(t){return console.warn("Registration of backend "+e+" failed"),console.warn(t.stack||t.message),!1}},t.prototype.removeBackend=function(e){if(!(e in this.registry))throw new Error(e+" backend not found in registry");this.registry[e].backend.dispose(),delete this.registry[e],e in this.engines&&delete this.engines[e]},Object.defineProperty(t.prototype,"engine",{get:function(){return this.initDefaultBackend(),this.globalEngine},enumerable:!0,configurable:!0}),t.prototype.initDefaultBackend=function(){null==this.globalEngine&&this.initBackend(this.get("BACKEND"),!1)},t}(),me=function(){var t=function(){var t;if("undefined"!=typeof window)t=window;else{if(void 0===e)throw new Error("Could not find a global object");t=e}return t}();return null==t.ENV&&(t.ENV=new de(le()),Z(function(){return t.ENV.engine})),t.ENV}(),ge=Object.freeze({Environment:de,ENV:me});function ye(e){return d(R(e),"The f passed in grad(f) must be a function"),function(t,n){return d(t instanceof Q,"The x passed in grad(f)(x) must be a tensor"),d(null==n||n instanceof Q,"The dy passed in grad(f)(x, dy) must be a tensor"),me.engine.tidy(function(){var r=me.engine.gradients(function(){return e(t)},[t],n),a=r.value,i=r.grads;return null!=n&&m(a.shape,n.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),Ee(i),i[0]})}}function ve(e){return d(R(e),"The f passed in grads(f) must be a function"),function(t,n){return d(Array.isArray(t)&&t.every(function(e){return e instanceof Q}),"The args passed in grads(f)(args) must be an array of tensors"),d(null==n||n instanceof Q,"The dy passed in grads(f)(args, dy) must be a tensor"),me.engine.tidy(function(){var r=me.engine.gradients(function(){return e.apply(void 0,t)},t,n),a=r.value,i=r.grads;return null!=n&&m(a.shape,n.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Ee(i),i})}}function be(e){return d(R(e),"The f passed in valueAndGrad(f) must be a function"),function(t,n){d(t instanceof Q,"The x passed in valueAndGrad(f)(x) must be a tensor"),d(null==n||n instanceof Q,"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");var r=me.engine.gradients(function(){return e(t)},[t],n),a=r.grads,i=r.value;return Ee(a),{grad:a[0],value:i}}}function we(e){return d(R(e),"The f passed in valueAndGrads(f) must be a function"),function(t,n){d(Array.isArray(t)&&t.every(function(e){return e instanceof Q}),"The args passed in valueAndGrads(f)(args) must be array of tensors"),d(null==n||n instanceof Q,"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");var r=me.engine.gradients(function(){return e.apply(void 0,t)},t,n);return null!=n&&m(r.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Ee(r.grads),r}}function xe(e,t){if(d(R(e),"The f passed in variableGrads(f) must be a function"),d(null==t||Array.isArray(t)&&t.every(function(e){return e instanceof ee}),"The varList passed in variableGrads(f, varList) must be an array of variables"),null==t)for(var n in t=[],me.engine.registeredVariables)t.push(me.engine.registeredVariables[n]);var r=t.length;d((t=t.filter(function(e){return e.trainable})).length>0,"variableGrads() expects at least one of the input variables to be trainable, but none of the "+r+" variables is trainable.");var a=me.engine.gradients(e,t,null,!0),i=a.value,o=a.grads;d(o.some(function(e){return null!=e}),"Cannot find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize()."),d(0===i.rank,"The f passed in variableGrads(f) must return a scalar, but it returned a rank-"+i.rank+" tensor");var s={};return t.forEach(function(e,t){null!=o[t]&&(s[e.name]=o[t])}),{value:i,grads:s}}function _e(e){return me.engine.customGrad(e)}function Ee(e){if(e.filter(function(e){return null==e}).length>0)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that\n the f you passed encloses all operations that lead from x to y.")}var Ne=de.tidy,Se=de.keep,Oe=de.dispose,ke=de.time,Ie=de.profile;function Ce(){for(var e=[],t=0;t=2*t+1||a%2==1?o.push(a):i.push(a);r.push.apply(r,i),r.push(0),r.push.apply(r,o)}return r}function Te(e,t,n,r){void 0===r&&(r=!0);var a=[];r?a.push(e[0]/n):a.push(e[0]*n);for(var i=1;i=-n&&e0?a>=c[t]:a<=c[t]);a+=r[t])n+=1;return n}),[l,h,f]}function He(e,t,n,r,a){var i=t[a];e&1<0?Number.MIN_SAFE_INTEGER:Number.MAX_SAFE_INTEGER);var o=r[a];return i<0&&(i+=o),p(0,i,o-1)}function Ke(e,t,n,r,a){var i=t[a];e&1<0?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER);var o=r[a];return i<0&&(i+=o),n[a]>0?p(0,i,o):p(-1,i,o-1)}function Xe(e,t,n,r){if(void 0===r&&(r="float32"),r=r||"float32",e instanceof Q)return e;if(!T(e)&&!Array.isArray(e)&&"number"!=typeof e&&"boolean"!=typeof e)throw new Error("Argument '"+t+"' passed to '"+n+"' must be a Tensor or TensorLike, but got "+e.constructor.name);var a=v(e);return T(e)||Array.isArray(e)||(e=[e]),Q.make(a,{values:L(e,r,me.get("DEBUG"))},r)}function Ye(e,t,n){if(!Array.isArray(e))throw new Error("Argument "+t+" passed to "+n+" must be a `Tensor[]` or `TensorLike[]`");return e.map(function(e,r){return Xe(e,t+"["+r+"]",n)})}function Je(e){var t=Object.keys(e);if(1!==t.length)throw new Error("Please provide an object with a single key (operation name) mapping to a function. Got an object with "+t.length+" keys.");var n=t[0],r=e[n];n.endsWith("_")&&(n=n.substring(0,n.length-1));var a=function(){for(var e=[],t=0;t1)return ft([0],r);var a=z(Math.abs(Math.ceil((t-e)/n)),r);ta}).sort(function(e,t){return t.score-e.score}),o=[],s=0;s=0;--p)if(Pt(e,c,o[p])>=r){f=!0;break}if(!f&&(o.push(c),o.length>=n))break}return at(o,"int32")}function Pt(e,t,n){var r=e.subarray(4*t,4*t+4),a=e.subarray(4*n,4*n+4),i=Math.min(r[0],r[2]),o=Math.min(r[1],r[3]),s=Math.max(r[0],r[2]),u=Math.max(r[1],r[3]),l=Math.min(a[0],a[2]),c=Math.min(a[1],a[3]),f=Math.max(a[0],a[2]),p=Math.max(a[1],a[3]),h=(s-i)*(u-o),d=(f-l)*(p-c);if(h<=0||d<=0)return 0;var m=Math.max(i,l),g=Math.max(o,c),y=Math.min(s,f),v=Math.min(u,p),b=Math.max(y-m,0)*Math.max(v-g,0);return b/(h+d-b)}function At(e,t,n){var r=Array(e.rank).fill(0),a=e.shape.slice();return t.map(function(t){a[n]=t;var i=e.slice(r,a);return r[n]+=t,i})}function Tt(e,t,n,r,a){for(var i=t[t.length-1],o=[e.length/i,i],s=o[0],u=o[1],l=I(n,s*r),c=I("int32",s*r),f=0;f1&&1===o&&r.unshift(i)}return r}function Rt(e,t){for(var n=[],r=0;r1)&&n.unshift(i)}return n}function Dt(e,t){for(var n=[],r=Math.max(e.length,t.length),a=0;a