/** * @license * Copyright 2023 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@tensorflow/tfjs-core")):"function"==typeof define&&define.amd?define(["exports","@tensorflow/tfjs-core"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).tf=t.tf||{},t.tf)}(this,(function(t,e){"use strict";function n(t){var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,e}function r(t,e){return e.forEach((function(e){e&&"string"!=typeof e&&!Array.isArray(e)&&Object.keys(e).forEach((function(n){if("default"!==n&&!(n in t)){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}}))})),t}var i=n(e),a=function(t,e){return a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},a(t,e)};function o(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}a(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function s(t,e,n,r){return new(n||(n=Promise))((function(i,a){function o(t){try{u(r.next(t))}catch(t){a(t)}}function s(t){try{u(r.throw(t))}catch(t){a(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(o,s)}u((r=r.apply(t,e||[])).next())}))}function u(t,e){var n,r,i,a,o={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return a={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(a){return function(s){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;o;)try{if(n=1,r&&(i=2&a[0]?r.return:a[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,a[1])).done)return i;switch(r=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,r=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(i=o.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function c(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,a=n.call(t),o=[];try{for(;(void 0===e||e-- >0)&&!(r=a.next()).done;)o.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(n=a.return)&&n.call(a)}finally{if(i)throw i.error}}return o}function h(t,e,n){if(n||2===arguments.length)for(var r,i=0,a=e.length;i=this.maxEntries){var n=this.cache.keys().next().value;this.cache.delete(n)}this.cache.set(t,e)},t.prototype.getMaxEntries=function(){return this.maxEntries},t.prototype.setMaxEntries=function(t){if(t<0)throw new Error("The maxEntries of LRU caches must be at least 0, but got ".concat(t,"."));if(this.maxEntries>t)for(var e=0;ee?1:0}(t,e)}function C(t){var e,n;if(null==t)return t;var r=[];try{for(var i=l(t),a=i.next();!a.done;a=i.next()){var o=a.value;-1===r.indexOf(o)&&r.push(o)}}catch(t){e={error:t}}finally{try{a&&!a.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}return r}function F(t){if(null==t)throw new d("Invalid value in obj: ".concat(JSON.stringify(t)));for(var e in t)if(t.hasOwnProperty(e))return!1;return!0}function D(t,e,n){if(null!=n&&t.indexOf(n)<0)throw new d("".concat(n," is not a valid ").concat(e,". Valid values are ").concat(t," or null/undefined."))}function _(t,e,n,r){return void 0===n&&(n=0),void 0===r&&(r=1/0),b(n>=0),b(r>=n),Array.isArray(t)&&t.length>=n&&t.length<=r&&t.every((function(t){return typeof t===e}))}function L(t,n){Array.isArray(t)?(e.util.assert(t.length>0,(function(){return"".concat(n," is unexpectedly an empty array.")})),t.forEach((function(t,e){return L(t,"element ".concat(e+1," of ").concat(n))}))):e.util.assert(Number.isInteger(t)&&t>0,(function(){return"Expected ".concat(n," to be a positive integer, but got ")+"".concat(R(t),".")}))}function R(t){return null===t?"null":Array.isArray(t)?"["+t.map((function(t){return R(t)})).join(",")+"]":"string"==typeof t?'"'.concat(t,'"'):"".concat(t)}function O(t){return"relu"===t?"relu":"linear"===t?"linear":"elu"===t?"elu":null}var M=0;function B(){return M++}var P={};function j(t){return void 0===t&&(t=""),t in P||(P[t]=0),P[t]+=1,t+P[t].toString()}var U=["channelsFirst","channelsLast"],W=["nearest","bilinear"],V=["valid","same","causal"],q=["max","avg"],G=["sum","mul","concat","ave"],K=new Map;function H(t){D(U,"DataFormat",t)}function J(t){D(V,"PaddingMode",t)}function Z(t){D(q,"PoolMode",t)}var Y=[];function X(t,e){Y.push(t);try{var n=e();return Y.pop(),n}catch(t){throw Y.pop(),t}}function $(t){if(!nt(t))throw new Error("Not a valid tensor name: '"+t+"'");return(0===Y.length?"":Y.join("/")+"/")+t}function Q(t){if(!nt(t))throw new Error("Not a valid tensor name: '"+t+"'");K.has(t)||K.set(t,0);var e=K.get(t);if(K.set(t,K.get(t)+1),e>0){var n="".concat(t,"_").concat(e);return K.set(n,1),n}return t}var tt,et=new RegExp(/^[A-Za-z0-9][-A-Za-z0-9\._\/]*$/);function nt(t){return!!t.match(et)}function rt(t,e,n){null==e&&(e=0),null==n&&(n=t.length);for(var r=1,i=e;ie&&(e=r)}return e}function ot(t,e){if(e= 2"+" but got x shape = ".concat(t.shape," and y shape = ").concat(e.shape));if(e.rank>=3&&t.shape.slice(-1)[0]!==(f=e.shape.slice(-2)[0]))throw new g("If rank y >= 3, then the second last dim"+" of y must equal the last dim of x but got x shape = ".concat(t.shape," and ")+" y shape = ".concat(e.shape));if(2===t.rank&&2===e.rank){var a=!1,o=!1;return i.fused.matMul({a:t,b:e,transposeA:a,transposeB:o,bias:r?wt(t.rank,r,"channelsLast"):null,activation:n})}var s=t.shape.slice(),u=s.pop();t=i.reshape(t,[-1,u]);var l=e.shape.slice(),p=l.pop(),f=l.pop(),d=h(h([],c(l),!1),[p],!1),v=Array.from({length:e.rank},(function(t,n){return 0===n?e.rank-2:n<=e.rank-2?n-1:n}));e=i.reshape(i.transpose(e,v),[f,-1]);var y=h(h([],c(s),!1),c(d),!1);a=!1,o=!1;return i.reshape(i.fused.matMul({a:t,b:e,transposeA:a,transposeB:o,bias:r?wt(t.rank,r,"channelsLast"):null,activation:n}),y)}function mt(t,n,r){return e.tidy((function(){return n=Array.isArray(n)?e.tensor1d(n,"int32"):i.cast(n,"int32"),i.gather(t,n,r)}))}function bt(t){return i.mul(t,t)}function wt(t,e,n){var r=e.shape;if(1!==e.rank&&e.rank!==t)throw new d("Unexpected bias dimensions: ".concat(e.rank)+"; expected it to be 1 or ".concat(t));if(5===t){if("channelsFirst"===n)return 1===r.length?i.reshape(e,[1,r[0],1,1,1]):i.reshape(e,[1,r[3],r[0],r[1],r[2]]);if("channelsLast"===n)return 1===r.length?i.reshape(e,[1,1,1,1,r[0]]):i.reshape(e,[1].concat(r))}else if(4===t){if("channelsFirst"===n)return 1===r.length?i.reshape(e,[1,r[0],1,1]):i.reshape(e,[1,r[2],r[0],r[1]]);if("channelsLast"===n)return 1===r.length?i.reshape(e,[1,1,1,r[0]]):i.reshape(e,[1].concat(r))}else if(3===t){if("channelsFirst"===n)return 1===r.length?i.reshape(e,[1,r[0],1]):i.reshape(e,[1,r[1],r[0]]);if("channelsLast"===n)return 1===r.length?i.reshape(e,[1,1,r[0]]):i.reshape(e,[1].concat(r))}else if(t<3)return e;throw new d("Unsupported input rank by biasAdd: ".concat(e.rank))}function kt(t,n,r){return e.tidy((function(){return null==r&&(r="channelsLast"),H(r),i.add(t,wt(t.rank,n,r))}))}function xt(t,n,r,a){return e.tidy((function(){return i.dropout(t,n,r,a)}))}function St(t,e,n){return void 0===n&&(n=!1),n?t():e()}var Nt=["fanIn","fanOut","fanAvg"],It=["normal","uniform","truncatedNormal"];var At=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.fromConfigUsesCustomObjects=function(){return!1},e.prototype.getConfig=function(){return{}},e}(e.serialization.Serializable),zt=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.apply=function(t,n){return e.zeros(t,n)},n}(At);zt.className="Zeros",e.serialization.registerClass(zt);var Et=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.apply=function(t,n){return e.ones(t,n)},n}(At);Et.className="Ones",e.serialization.registerClass(Et);var Tt=function(t){function n(e){var n=t.call(this)||this;if("object"!=typeof e)throw new d("Expected argument of type ConstantConfig but got ".concat(e));if(void 0===e.value)throw new d("config must have value set but got ".concat(e));return n.value=e.value,n}return o(n,t),n.prototype.apply=function(t,n){var r=this;return e.tidy((function(){return e.mul(e.scalar(r.value),e.ones(t,n))}))},n.prototype.getConfig=function(){return{value:this.value}},n}(At);Tt.className="Constant",e.serialization.registerClass(Tt);var Ct=function(t){function n(e){var n=t.call(this)||this;return n.DEFAULT_MINVAL=-.05,n.DEFAULT_MAXVAL=.05,n.minval=e.minval||n.DEFAULT_MINVAL,n.maxval=e.maxval||n.DEFAULT_MAXVAL,n.seed=e.seed,n}return o(n,t),n.prototype.apply=function(t,n){return e.randomUniform(t,this.minval,this.maxval,n,this.seed)},n.prototype.getConfig=function(){return{minval:this.minval,maxval:this.maxval,seed:this.seed}},n}(At);Ct.className="RandomUniform",e.serialization.registerClass(Ct);var Ft=function(t){function e(e){var n=t.call(this)||this;return n.DEFAULT_MEAN=0,n.DEFAULT_STDDEV=.05,n.mean=e.mean||n.DEFAULT_MEAN,n.stddev=e.stddev||n.DEFAULT_STDDEV,n.seed=e.seed,n}return o(e,t),e.prototype.apply=function(t,e){if("float32"!==(e=e||"float32")&&"int32"!==e)throw new g("randomNormal does not support dType ".concat(e,"."));return vt(t,this.mean,this.stddev,e,this.seed)},e.prototype.getConfig=function(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}},e}(At);Ft.className="RandomNormal",e.serialization.registerClass(Ft);var Dt=function(t){function n(e){var n=t.call(this)||this;return n.DEFAULT_MEAN=0,n.DEFAULT_STDDEV=.05,n.mean=e.mean||n.DEFAULT_MEAN,n.stddev=e.stddev||n.DEFAULT_STDDEV,n.seed=e.seed,n}return o(n,t),n.prototype.apply=function(t,n){if("float32"!==(n=n||"float32")&&"int32"!==n)throw new g("truncatedNormal does not support dType ".concat(n,"."));return e.truncatedNormal(t,this.mean,this.stddev,n,this.seed)},n.prototype.getConfig=function(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}},n}(At);Dt.className="TruncatedNormal",e.serialization.registerClass(Dt);var _t=function(t){function n(e){var n=t.call(this)||this;return n.gain=null!=e.gain?e.gain:1,n}return o(n,t),n.prototype.apply=function(t,n){var r=this;return e.tidy((function(){if(2!==t.length||t[0]!==t[1])throw new d("Identity matrix initializer can only be used for 2D square matrices.");return e.mul(r.gain,e.eye(t[0]))}))},n.prototype.getConfig=function(){return{gain:this.gain}},n}(At);_t.className="Identity",e.serialization.registerClass(_t);var Lt=function(t){function n(e){var n,r=t.call(this)||this;if(e.scale<0)throw new d("scale must be a positive float. Got: ".concat(e.scale));return r.scale=null==e.scale?1:e.scale,r.mode=null==e.mode?"fanIn":e.mode,n=r.mode,D(Nt,"FanMode",n),r.distribution=null==e.distribution?"normal":e.distribution,function(t){D(It,"Distribution",t)}(r.distribution),r.seed=e.seed,r}return o(n,t),n.prototype.apply=function(t,n){var r=function(t,e){var n,r;if(void 0===e&&(e="channelsLast"),H(e),2===t.length)n=t[0],r=t[1];else if(-1!==[3,4,5].indexOf(t.length))if("channelsFirst"===e){var i=rt(t,2);n=t[1]*i,r=t[0]*i}else"channelsLast"===e&&(i=rt(t,0,t.length-2),n=t[t.length-2]*i,r=t[t.length-1]*i);else{var a=rt(t);n=Math.sqrt(a),r=Math.sqrt(a)}return[n,r]}(t),i=r[0],a=r[1],o=this.scale;if("fanIn"===this.mode?o/=Math.max(1,i):"fanOut"===this.mode?o/=Math.max(1,a):o/=Math.max(1,(i+a)/2),"normal"===this.distribution){var s=Math.sqrt(o);if("float32"!==(n=n||"float32")&&"int32"!==n)throw new g("".concat(this.getClassName()," does not support dType ").concat(n,"."));return e.truncatedNormal(t,0,s,n,this.seed)}var u=Math.sqrt(3*o);return e.randomUniform(t,-u,u,n,this.seed)},n.prototype.getConfig=function(){return{scale:this.scale,mode:this.mode,distribution:this.distribution,seed:this.seed}},n}(At);Lt.className="VarianceScaling",e.serialization.registerClass(Lt);var Rt=function(t){function e(e){return t.call(this,{scale:1,mode:"fanAvg",distribution:"uniform",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);Rt.className="GlorotUniform",e.serialization.registerClass(Rt);var Ot=function(t){function e(e){return t.call(this,{scale:1,mode:"fanAvg",distribution:"normal",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);Ot.className="GlorotNormal",e.serialization.registerClass(Ot);var Mt=function(t){function e(e){return t.call(this,{scale:2,mode:"fanIn",distribution:"normal",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);Mt.className="HeNormal",e.serialization.registerClass(Mt);var Bt=function(t){function e(e){return t.call(this,{scale:2,mode:"fanIn",distribution:"uniform",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);Bt.className="HeUniform",e.serialization.registerClass(Bt);var Pt=function(t){function e(e){return t.call(this,{scale:1,mode:"fanIn",distribution:"normal",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);Pt.className="LeCunNormal",e.serialization.registerClass(Pt);var jt=function(t){function e(e){return t.call(this,{scale:1,mode:"fanIn",distribution:"uniform",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);jt.className="LeCunUniform",e.serialization.registerClass(jt);var Ut=function(t){function n(e){var n=t.call(this)||this;return n.DEFAULT_GAIN=1,n.ELEMENTS_WARN_SLOW=2e3,n.gain=null==e.gain?n.DEFAULT_GAIN:e.gain,n.seed=e.seed,n}return o(n,t),n.prototype.apply=function(t,n){var r=this;return e.tidy((function(){if(t.length<2)throw new g("Shape must be at least 2D.");if("int32"!==n&&"float32"!==n&&void 0!==n)throw new TypeError("Unsupported data type ".concat(n,"."));var i=e.util.sizeFromShape(t.slice(0,-1)),a=t[t.length-1],o=i*a;o>r.ELEMENTS_WARN_SLOW&&console.warn("Orthogonal initializer is being called on a matrix with more "+"than ".concat(r.ELEMENTS_WARN_SLOW," (").concat(o,") elements: ")+"Slowness may result.");var s=vt([Math.max(a,i),Math.min(a,i)],0,1,n,r.seed),u=e.linalg.qr(s,!1),l=u[0],c=u[1].flatten().stridedSlice([0],[Math.min(a,i)*Math.min(a,i)],[Math.min(a,i)+1]);return l=e.mul(l,c.sign()),i1)throw new p("Layer ".concat(this.name)+' has multiple inbound nodes, hence the notion of "layer input" is ill-defined. Use `getInputAt(nodeIndex)` instead.');if(0===this.inboundNodes.length)throw new p("Layer ".concat(this.name)+" is not connected, no input to return.");return k(this.getNodeAtIndex(0,"input").inputTensors)},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"output",{get:function(){if(0===this.inboundNodes.length)throw new p("Layer ".concat(this.name)+" has no inbound nodes.");if(this.inboundNodes.length>1)throw new p("Layer ".concat(this.name)+' has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use `getOutputAt(nodeIndex)` instead.');return k(this.getNodeAtIndex(0,"output").outputTensors)},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"losses",{get:function(){return this._losses},enumerable:!1,configurable:!0}),n.prototype.calculateLosses=function(){return this.losses.map((function(t){return t()}))},Object.defineProperty(n.prototype,"updates",{get:function(){return this._updates},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"built",{get:function(){return this._built},set:function(t){this._built=t},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"trainable",{get:function(){return this.trainable_},set:function(t){this._trainableWeights.forEach((function(e){return e.trainable=t})),this.trainable_=t},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"trainableWeights",{get:function(){return this.trainable_?this._trainableWeights.filter((function(t){return t.trainable})):[]},set:function(t){this._trainableWeights=t},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"nonTrainableWeights",{get:function(){return this.trainable?this._trainableWeights.filter((function(t){return!t.trainable})).concat(this._nonTrainableWeights):this._trainableWeights.concat(this._nonTrainableWeights)},set:function(t){this._nonTrainableWeights=t},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"weights",{get:function(){return this.trainableWeights.concat(this.nonTrainableWeights)},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"stateful",{get:function(){return this._stateful},enumerable:!1,configurable:!0}),n.prototype.resetStates=function(){if(!this.stateful)throw new Error("Cannot call the resetStates() method of a non-stateful Layer object.")},n.prototype.assertInputCompatibility=function(t){var e=x(t);if(null!=this.inputSpec&&0!==this.inputSpec.length){var n=x(this.inputSpec);if(e.length!==n.length)throw new d("Layer ".concat(this.name," expects ").concat(n.length," inputs, ")+"but it received ".concat(e.length," input tensors. ")+"Input received: ".concat(t));for(var r=0;r=0?s[l]:s[s.length+l];if(null!=c&&-1===[c,null].indexOf(h))throw new d("Input ".concat(r," is incompatible with layer ")+"".concat(this.name,": expected axis ").concat(l," of input shape to ")+"have value ".concat(c," but got shape ").concat(s,"."))}}if(null!=a.shape)for(var p=0;p0&&Array.isArray(N[0])?N.map((function(r,i){return new ne(I,r,n,x(t),e,n.name,i)})):new ne(I,N,n,x(t),e,n.name),n.addInboundNode(t,d,null,null,S,N,e),n._refCount++,null!=n.activityRegularizer)throw new g("Layer invocation in the presence of activity regularizer(s) is not supported yet.");return d}))},n.prototype.warnOnIncompatibleInputShape=function(t){if(null!=this.batchInputShape)if(t.length!==this.batchInputShape.length)console.warn("The rank of the input tensor provided (shape: "+"".concat(JSON.stringify(t),") does not match that of the ")+"batchInputShape (".concat(JSON.stringify(this.batchInputShape),") ")+"of the layer ".concat(this.name));else{var e=!1;this.batchInputShape.forEach((function(n,r){null!=n&&null!=t[r]&&t[r]!==n&&(e=!0)})),e&&console.warn("The shape of the input tensor "+"(".concat(JSON.stringify(t),") does not ")+"match the expectation of layer ".concat(this.name,": ")+"".concat(JSON.stringify(this.batchInputShape)))}},Object.defineProperty(n.prototype,"outputShape",{get:function(){var t,e;if(null==this.inboundNodes||0===this.inboundNodes.length)throw new p("The layer ".concat(this.name," has never been called and thus has no ")+"defined output shape.");var n=[];try{for(var r=l(this.inboundNodes),i=r.next();!i.done;i=r.next()){var a=i.value,o=JSON.stringify(a.outputShapes);-1===n.indexOf(o)&&n.push(o)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(e=r.return)&&e.call(r)}finally{if(t)throw t.error}}if(1===n.length){var s=this.inboundNodes[0].outputShapes;return Array.isArray(s)&&Array.isArray(s[0])&&1===s.length?s[0]:s}throw new p("The layer ".concat(this.name," has multiple inbound nodes with different ")+'output shapes. Hence the notion of "output shape" is ill-defined for the layer.')},enumerable:!1,configurable:!0}),n.prototype.countParams=function(){if(!this.built)throw new f("You tried to call countParams() on ".concat(this.name,", ")+"but the layer is not built yet. Build it first by calling build(batchInputShape).");return Yt(this.weights)},n.prototype.build=function(t){this.built=!0},n.prototype.getWeights=function(t){return void 0===t&&(t=!1),Qt(t?this.trainableWeights:this.weights)},n.prototype.setWeights=function(t){var n=this;e.tidy((function(){var r=n.weights;if(r.length!==t.length)throw new d('You called setWeights(weights) on layer "'.concat(n.name,'" ')+"with a weight list of length ".concat(t.length,", ")+"but the layer was expecting ".concat(r.length," weights. ")+"Provided weights: ".concat(t,"..."));if(0!==r.length){for(var i=[],a=Qt(r),o=0;o0)&&(e=t.sourceLayer,n=t.nodeIndex),0===e.inboundNodes.length)return[t];var a=e.inboundNodes[n];if(0===a.inboundLayers.length)return a.inputTensors;for(var o=[],s=0;s0,(function(){return"Expected at least one fetch, got none"}));var s=[],u={};if(1===t.length){var c=ge(t[0],n);s=c.sorted,u=c.recipientMap}else{var h=new Set;try{for(var p=l(t),f=p.next();!f.done;f=p.next()){var d=ge(f.value,n),g=d.sorted,v=d.recipientMap;try{for(var y=(a=void 0,l(g)),m=y.next();!m.done;m=y.next()){var b=m.value;h.has(b.name)||(s.push(b),h.add(b.name))}}catch(t){a={error:t}}finally{try{m&&!m.done&&(o=y.return)&&o.call(y)}finally{if(a)throw a.error}}var w=function(t){null==u[t]&&(u[t]=new Set),v[t].forEach((function(e){return u[t].add(e)}))};for(var k in v)w(k)}}catch(t){r={error:t}}finally{try{f&&!f.done&&(i=p.return)&&i.call(p)}finally{if(r)throw r.error}}}return{sorted:s,recipientCounts:de(u)}}(p,n);k=S.sorted,b=S.recipientCounts,he.put(w,k),pe.put(w,b)}b={},c||Object.assign(b,pe.get(w));for(var N=new ce(n),I=0;Ii.maxNumTensors&&(i.maxNumTensors=A),A0;){var g=f[f.length-1];if(o.has(g.name))f.pop();else{var v=d[d.length-1]===f.length-1;if(0===g.inputs.length||v)f.pop(),s.push(g),o.add(g.name),v&&d.pop();else{d.push(f.length-1);try{for(var y=(i=void 0,l(g.inputs)),m=y.next();!m.done;m=y.next()){var b=m.value;null==u[b.name]&&(u[b.name]=new Set),u[b.name].add(g.name),o.has(b.name)||f.push(b)}}catch(t){i={error:t}}finally{try{m&&!m.done&&(a=y.return)&&a.call(y)}finally{if(i)throw i.error}}}}}return{sorted:s,recipientMap:u}}function ve(t){var e,n,r;if(1===t.sourceLayer.inboundNodes.length)r=t.sourceLayer.output;else{for(var i=null,a=0;a=-n&&t=0;--r)n[r]=n[r+1]*t[r+1];return n}function dn(t,e,n,r){void 0===r&&(r=!1);var i=new Array;if(1===e.length)for(var a=e[0]*(r?2:1),o=0;o=0,(function(){return"Tensor must have a shape comprised of positive integers but got "+"shape [".concat(t,"].")}))}))}function bn(t){return t&&t.then&&"function"==typeof t.then}var wn="tfjsflags",kn=function(){function t(t){this.global=t,this.flags={},this.flagRegistry={},this.urlFlags={},this.getQueryParams=xn,this.populateURLFlags()}return t.prototype.setPlatform=function(t,e){null!=this.platform&&(Nn().getBool("IS_TEST")||Nn().getBool("PROD")||console.warn("Platform ".concat(this.platformName," has already been set. ")+"Overwriting the platform with ".concat(t,"."))),this.platformName=t,this.platform=e},t.prototype.registerFlag=function(t,e,n){if(this.flagRegistry[t]={evaluationFn:e,setHook:n},null!=this.urlFlags[t]){var r=this.urlFlags[t];Nn().getBool("IS_TEST")||Nn().getBool("PROD")||console.warn("Setting feature override from URL ".concat(t,": ").concat(r,".")),this.set(t,r)}},t.prototype.getAsync=function(t){return s(this,void 0,void 0,(function(){var e,n;return u(this,(function(r){switch(r.label){case 0:return t in this.flags?[2,this.flags[t]]:(e=this.flags,n=t,[4,this.evaluateFlag(t)]);case 1:return e[n]=r.sent(),[2,this.flags[t]]}}))}))},t.prototype.get=function(t){if(t in this.flags)return this.flags[t];var e=this.evaluateFlag(t);if(bn(e))throw new Error("Flag ".concat(t," cannot be synchronously evaluated. ")+"Please use getAsync() instead.");return this.flags[t]=e,this.flags[t]},t.prototype.getNumber=function(t){return this.get(t)},t.prototype.getBool=function(t){return this.get(t)},t.prototype.getString=function(t){return this.get(t)},t.prototype.getFlags=function(){return this.flags},Object.defineProperty(t.prototype,"features",{get:function(){return this.flags},enumerable:!1,configurable:!0}),t.prototype.set=function(t,e){if(null==this.flagRegistry[t])throw new Error("Cannot set flag ".concat(t," as it has not been registered."));this.flags[t]=e,null!=this.flagRegistry[t].setHook&&this.flagRegistry[t].setHook(e)},t.prototype.evaluateFlag=function(t){if(null==this.flagRegistry[t])throw new Error("Cannot evaluate flag '".concat(t,"': no evaluation function found."));return this.flagRegistry[t].evaluationFn()},t.prototype.setFlags=function(t){this.flags=Object.assign({},t)},t.prototype.reset=function(){this.flags={},this.urlFlags={},this.populateURLFlags()},t.prototype.populateURLFlags=function(){var t=this;if("undefined"!=typeof this.global&&"undefined"!=typeof this.global.location&&"undefined"!=typeof this.global.location.search){var e=this.getQueryParams(this.global.location.search);if(wn in e)e.tfjsflags.split(",").forEach((function(e){var n=c(e.split(":"),2),r=n[0],i=n[1];t.urlFlags[r]=function(t,e){var n=e.toLowerCase();return"true"===n||"false"===n?"true"===n:"".concat(+n)===n?+n:e}(0,i)}))}},t}();function xn(t){var e={};return t.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,(function(t){for(var n=[],r=1;r>>=0)&&t<256)&&(r=Vn[t])?r:(n=Kn(t,(0|t)<0?-1:0,!0),i&&(Vn[t]=n),n):(i=-128<=(t|=0)&&t<128)&&(r=Wn[t])?r:(n=Kn(t,t<0?-1:0,!1),i&&(Wn[t]=n),n)}function Gn(t,e){if(isNaN(t))return e?er:tr;if(e){if(t<0)return er;if(t>=Xn)return or}else{if(t<=-$n)return sr;if(t+1>=$n)return ar}return t<0?Gn(-t,e).neg():Kn(t%Yn|0,t/Yn|0,e)}function Kn(t,e,n){return new jn(t,e,n)}jn.fromInt=qn,jn.fromNumber=Gn,jn.fromBits=Kn;var Hn=Math.pow;function Jn(t,e,n){if(0===t.length)throw Error("empty string");if("NaN"===t||"Infinity"===t||"+Infinity"===t||"-Infinity"===t)return tr;if("number"==typeof e?(n=e,e=!1):e=!!e,(n=n||10)<2||360)throw Error("interior hyphen");if(0===r)return Jn(t.substring(1),e,n).neg();for(var i=Gn(Hn(n,8)),a=tr,o=0;o>>0:this.low},ur.toNumber=function(){return this.unsigned?(this.high>>>0)*Yn+(this.low>>>0):this.high*Yn+(this.low>>>0)},ur.toString=function(t){if((t=t||10)<2||36>>0).toString(t);if((a=s).isZero())return u+o;for(;u.length<6;)u="0"+u;o=""+u+o}},ur.getHighBits=function(){return this.high},ur.getHighBitsUnsigned=function(){return this.high>>>0},ur.getLowBits=function(){return this.low},ur.getLowBitsUnsigned=function(){return this.low>>>0},ur.getNumBitsAbs=function(){if(this.isNegative())return this.eq(sr)?64:this.neg().getNumBitsAbs();for(var t=0!=this.high?this.high:this.low,e=31;e>0&&0==(t&1<=0},ur.isOdd=function(){return 1==(1&this.low)},ur.isEven=function(){return 0==(1&this.low)},ur.equals=function(t){return Un(t)||(t=Zn(t)),(this.unsigned===t.unsigned||this.high>>>31!=1||t.high>>>31!=1)&&(this.high===t.high&&this.low===t.low)},ur.eq=ur.equals,ur.notEquals=function(t){return!this.eq(t)},ur.neq=ur.notEquals,ur.ne=ur.notEquals,ur.lessThan=function(t){return this.comp(t)<0},ur.lt=ur.lessThan,ur.lessThanOrEqual=function(t){return this.comp(t)<=0},ur.lte=ur.lessThanOrEqual,ur.le=ur.lessThanOrEqual,ur.greaterThan=function(t){return this.comp(t)>0},ur.gt=ur.greaterThan,ur.greaterThanOrEqual=function(t){return this.comp(t)>=0},ur.gte=ur.greaterThanOrEqual,ur.ge=ur.greaterThanOrEqual,ur.compare=function(t){if(Un(t)||(t=Zn(t)),this.eq(t))return 0;var e=this.isNegative(),n=t.isNegative();return e&&!n?-1:!e&&n?1:this.unsigned?t.high>>>0>this.high>>>0||t.high===this.high&&t.low>>>0>this.low>>>0?-1:1:this.sub(t).isNegative()?-1:1},ur.comp=ur.compare,ur.negate=function(){return!this.unsigned&&this.eq(sr)?sr:this.not().add(nr)},ur.neg=ur.negate,ur.add=function(t){Un(t)||(t=Zn(t));var e=this.high>>>16,n=65535&this.high,r=this.low>>>16,i=65535&this.low,a=t.high>>>16,o=65535&t.high,s=t.low>>>16,u=0,l=0,c=0,h=0;return c+=(h+=i+(65535&t.low))>>>16,l+=(c+=r+s)>>>16,u+=(l+=n+o)>>>16,u+=e+a,Kn((c&=65535)<<16|(h&=65535),(u&=65535)<<16|(l&=65535),this.unsigned)},ur.subtract=function(t){return Un(t)||(t=Zn(t)),this.add(t.neg())},ur.sub=ur.subtract,ur.multiply=function(t){if(this.isZero())return tr;if(Un(t)||(t=Zn(t)),Pn)return Kn(Pn.mul(this.low,this.high,t.low,t.high),Pn.get_high(),this.unsigned);if(t.isZero())return tr;if(this.eq(sr))return t.isOdd()?sr:tr;if(t.eq(sr))return this.isOdd()?sr:tr;if(this.isNegative())return t.isNegative()?this.neg().mul(t.neg()):this.neg().mul(t).neg();if(t.isNegative())return this.mul(t.neg()).neg();if(this.lt(Qn)&&t.lt(Qn))return Gn(this.toNumber()*t.toNumber(),this.unsigned);var e=this.high>>>16,n=65535&this.high,r=this.low>>>16,i=65535&this.low,a=t.high>>>16,o=65535&t.high,s=t.low>>>16,u=65535&t.low,l=0,c=0,h=0,p=0;return h+=(p+=i*u)>>>16,c+=(h+=r*u)>>>16,h&=65535,c+=(h+=i*s)>>>16,l+=(c+=n*u)>>>16,c&=65535,l+=(c+=r*s)>>>16,c&=65535,l+=(c+=i*o)>>>16,l+=e*u+n*s+r*o+i*a,Kn((h&=65535)<<16|(p&=65535),(l&=65535)<<16|(c&=65535),this.unsigned)},ur.mul=ur.multiply,ur.divide=function(t){if(Un(t)||(t=Zn(t)),t.isZero())throw Error("division by zero");var e,n,r;if(Pn)return this.unsigned||-2147483648!==this.high||-1!==t.low||-1!==t.high?Kn((this.unsigned?Pn.div_u:Pn.div_s)(this.low,this.high,t.low,t.high),Pn.get_high(),this.unsigned):this;if(this.isZero())return this.unsigned?er:tr;if(this.unsigned){if(t.unsigned||(t=t.toUnsigned()),t.gt(this))return er;if(t.gt(this.shru(1)))return rr;r=er}else{if(this.eq(sr))return t.eq(nr)||t.eq(ir)?sr:t.eq(sr)?nr:(e=this.shr(1).div(t).shl(1)).eq(tr)?t.isNegative()?nr:ir:(n=this.sub(t.mul(e)),r=e.add(n.div(t)));if(t.eq(sr))return this.unsigned?er:tr;if(this.isNegative())return t.isNegative()?this.neg().div(t.neg()):this.neg().div(t).neg();if(t.isNegative())return this.div(t.neg()).neg();r=tr}for(n=this;n.gte(t);){e=Math.max(1,Math.floor(n.toNumber()/t.toNumber()));for(var i=Math.ceil(Math.log(e)/Math.LN2),a=i<=48?1:Hn(2,i-48),o=Gn(e),s=o.mul(t);s.isNegative()||s.gt(n);)s=(o=Gn(e-=a,this.unsigned)).mul(t);o.isZero()&&(o=nr),r=r.add(o),n=n.sub(s)}return r},ur.div=ur.divide,ur.modulo=function(t){return Un(t)||(t=Zn(t)),Pn?Kn((this.unsigned?Pn.rem_u:Pn.rem_s)(this.low,this.high,t.low,t.high),Pn.get_high(),this.unsigned):this.sub(this.div(t).mul(t))},ur.mod=ur.modulo,ur.rem=ur.modulo,ur.not=function(){return Kn(~this.low,~this.high,this.unsigned)},ur.and=function(t){return Un(t)||(t=Zn(t)),Kn(this.low&t.low,this.high&t.high,this.unsigned)},ur.or=function(t){return Un(t)||(t=Zn(t)),Kn(this.low|t.low,this.high|t.high,this.unsigned)},ur.xor=function(t){return Un(t)||(t=Zn(t)),Kn(this.low^t.low,this.high^t.high,this.unsigned)},ur.shiftLeft=function(t){return Un(t)&&(t=t.toInt()),0==(t&=63)?this:t<32?Kn(this.low<>>32-t,this.unsigned):Kn(0,this.low<>>t|this.high<<32-t,this.high>>t,this.unsigned):Kn(this.high>>t-32,this.high>=0?0:-1,this.unsigned)},ur.shr=ur.shiftRight,ur.shiftRightUnsigned=function(t){if(Un(t)&&(t=t.toInt()),0===(t&=63))return this;var e=this.high;return t<32?Kn(this.low>>>t|e<<32-t,e>>>t,this.unsigned):Kn(32===t?e:e>>>t-32,0,this.unsigned)},ur.shru=ur.shiftRightUnsigned,ur.shr_u=ur.shiftRightUnsigned,ur.toSigned=function(){return this.unsigned?Kn(this.low,this.high,!1):this},ur.toUnsigned=function(){return this.unsigned?this:Kn(this.low,this.high,!0)},ur.toBytes=function(t){return t?this.toBytesLE():this.toBytesBE()},ur.toBytesLE=function(){var t=this.high,e=this.low;return[255&e,e>>>8&255,e>>>16&255,e>>>24,255&t,t>>>8&255,t>>>16&255,t>>>24]},ur.toBytesBE=function(){var t=this.high,e=this.low;return[t>>>24,t>>>16&255,t>>>8&255,255&t,e>>>24,e>>>16&255,e>>>8&255,255&e]},jn.fromBytes=function(t,e,n){return n?jn.fromBytesLE(t,e):jn.fromBytesBE(t,e)},jn.fromBytesLE=function(t,e){return new jn(t[0]|t[1]<<8|t[2]<<16|t[3]<<24,t[4]|t[5]<<8|t[6]<<16|t[7]<<24,e)},jn.fromBytesBE=function(t,e){return new jn(t[4]<<24|t[5]<<16|t[6]<<8|t[7],t[0]<<24|t[1]<<16|t[2]<<8|t[3],e)};var lr=On(Bn),cr=lr||r({__proto__:null,default:lr},[Bn]);function hr(t){return cr.fromString(t,!0,16)}function pr(t,e){if("string"===e)throw new Error("Cannot convert a string[] to a TypedArray");if(Array.isArray(t)&&(t=vr(t)),Nn().getBool("DEBUG")&&function(t,e){for(var n=0;n0?d:""," ")}}console.log("%c".concat(s,"\t%c").concat(o,"\t%c").concat(u,"D ").concat(c,"\t%c").concat(l,"\t%c").concat(h,"\t%c").concat(a),"font-weight:bold","color:red","color:blue","color: orange","color: green","color: steelblue")},t}();function br(t,e,n,r){var i=fn(e),a=function(t,e,n,r){var i=rn(e),a=r[r.length-1],o=new Array(a).fill(0),s=e.length,u="complex64"===n?Sr(t):t;if(s>1)for(var l=0;l20){var l=3*o,p=Array.from(t.slice(0,l)),f=Array.from(t.slice((s-3)*o,s*o));return"complex64"===n&&(p=Sr(p),f=Sr(f)),["["+p.map((function(t,e){return wr(t,i[e],n)})).join(", ")+", ..., "+f.map((function(t,e){return wr(t,i[s-3+e],n)})).join(", ")+"]"]}return["["+("complex64"===n?Sr(t):Array.from(t)).map((function(t,e){return wr(t,i[e],n)})).join(", ")+"]"]}var d=e.slice(1),g=r.slice(1),v=r[0]*o,y=[];if(s>20){for(var m=0;m<3;m++){var b=(w=m*v)+v;y.push.apply(y,h([],c(xr(t.slice(w,b),d,n,g,i,!1)),!1))}y.push("...");for(m=s-3;m0?y[0]+k:"");for(m=1;m=this.shape[i]){var u="Requested out of range element at ".concat(n,". ")+" Buffer shape=".concat(this.shape);throw new Error(u)}i++}}catch(e){t={error:e}}finally{try{o&&!o.done&&(e=a.return)&&e.call(a)}finally{if(t)throw t.error}}for(var c=n[n.length-1],h=0;h0)throw new Error("Backend '".concat(this.backendName,"' has an internal memory leak ")+"(".concat(o," data ids) after running '").concat(t,"'"))},t.prototype.runKernelFunc=function(t){var e,n,r,i=this,a=[],o=this.isTapeOn(),s=this.state.numBytes,u=this.state.numTensors;this.shouldCheckForMemLeaks()&&this.state.numDataMovesStack.push(0),null==this.backendName&&this.backend;var l=jr(t)?t.kernelName:null!=this.state.activeScope?this.state.activeScope.name:"";if(jr(t)){var c=t.kernelName,h=t.inputs,p=t.attrs;null==this.backendName&&this.backend;var f=Dn(c,this.backendName);nn(null!=f,(function(){return"Cannot find registered kernel '".concat(c,"' for backend '").concat(i.backendName,"'")})),n=function(){var t=i.backend.numDataIds();r=f.kernelFunc({inputs:h,attrs:p,backend:i.backend});var e=Array.isArray(r)?r:[r];i.shouldCheckForMemLeaks()&&i.checkKernelForMemLeak(c,t,e);var n=e.map((function(t){return null!=t.rank?t:i.makeTensorFromTensorInfo(t)}));if(o){var s=i.getTensorsForGradient(c,h,n);a=i.saveTensorsForBackwardMode(s)}return n}}else{var d=t.forwardFunc,g=function(t){o&&(a=t.map((function(t){return i.keep(i.clone(t))})))};n=function(){var t=i.backend.numDataIds();r=i.tidy((function(){return d(i.backend,g)}));var e=Array.isArray(r)?r:[r];return i.shouldCheckForMemLeaks()&&i.checkKernelForMemLeak(l,t,e),e}}var v,y=t.inputs,m=t.attrs,b=jr(t)?null:t.backwardsFunc;return this.scopedRun((function(){return i.state.kernelDepth++}),(function(){return i.state.kernelDepth--}),(function(){i.ENV.getBool("DEBUG")||i.state.profiling?(v=i.profiler.profileKernel(l,y,(function(){return n()})),i.ENV.getBool("DEBUG")&&i.profiler.logKernelProfile(v),e=v.outputs):e=n()})),o&&this.addTapeNode(l,y,e,b,a,m),this.state.profiling&&this.state.activeProfile.kernels.push({name:l,bytesAdded:this.state.numBytes-s,totalBytesSnapshot:this.state.numBytes,tensorsAdded:this.state.numTensors-u,totalTensorsSnapshot:this.state.numTensors,inputShapes:Object.keys(y).map((function(t){return null!=y[t]?y[t].shape:null})),outputShapes:e.map((function(t){return t.shape})),kernelTimeMs:v.timeMs,extraInfo:v.extraInfo}),Array.isArray(r)?e:e[0]},t.prototype.saveTensorsForBackwardMode=function(t){var e=this;return t.map((function(t){return e.keep(e.clone(t))}))},t.prototype.getTensorsForGradient=function(t,e,n){var r=_n(t);if(null!=r){var i=r.inputsToSave||[],a=r.outputsToSave||[],o=void 0;r.saveAllInputs?(nn(Array.isArray(e),(function(){return"saveAllInputs is true, expected inputs to be an array."})),o=Object.keys(e).map((function(t){return e[t]}))):o=i.map((function(t){return e[t]}));var s=n.filter((function(t,e){return a[e]}));return o.concat(s)}return[]},t.prototype.makeTensor=function(t,e,n,r){if(null==t)throw new Error("Values passed to engine.makeTensor() are null");n=n||"float32",r=r||this.backend;var i=t;"string"===n&&cn(t[0])&&(i=t.map((function(t){return e=t,void 0===n&&(n="utf-8"),n=n||"utf-8",Nn().platform.encode(e,n);var e,n})));var a=r.write(i,e,n),o=new Ar(e,n,a,this.nextTensorId());if(this.trackTensor(o,r),"string"===n){var s=this.state.tensorInfo.get(a),u=function(t){if(null==t)return 0;var e=0;return t.forEach((function(t){return e+=t.length})),e}(i);this.state.numBytes+=u-s.bytes,s.bytes=u}return o},t.prototype.makeTensorFromDataId=function(t,e,n,r){var i={dataId:t,shape:e,dtype:n=n||"float32"};return this.makeTensorFromTensorInfo(i,r)},t.prototype.makeTensorFromTensorInfo=function(t,e){var n=t.dataId,r=t.shape,i=t.dtype,a=new Ar(r,i,n,this.nextTensorId());return this.trackTensor(a,e),a},t.prototype.makeVariable=function(t,e,n,r){void 0===e&&(e=!0),n=n||this.nextVariableId().toString(),null!=r&&r!==t.dtype&&(t=t.cast(r));var i=new _r(t,e,n,this.nextTensorId());if(null!=this.state.registeredVariables[i.name])throw new Error("Variable with name ".concat(i.name," was already registered"));return this.state.registeredVariables[i.name]=i,this.incRef(i,this.backend),i},t.prototype.trackTensor=function(t,e){this.state.numTensors++,"string"===t.dtype&&this.state.numStringTensors++;var n=0;"complex64"!==t.dtype&&"string"!==t.dtype&&(n=t.size*ln(t.dtype)),this.state.numBytes+=n,this.state.tensorInfo.has(t.dataId)||(this.state.numDataBuffers++,this.state.tensorInfo.set(t.dataId,{backend:e||this.backend,dtype:t.dtype,shape:t.shape,bytes:n})),t instanceof _r||this.track(t)},t.prototype.incRef=function(t,e){this.trackTensor(t,e),this.backend.incRef(t.dataId)},t.prototype.removeDataId=function(t,e){this.state.tensorInfo.has(t)&&this.state.tensorInfo.get(t).backend===e&&(this.state.tensorInfo.delete(t),this.state.numDataBuffers--)},t.prototype.disposeTensor=function(t){if(this.state.tensorInfo.has(t.dataId)){var e=this.state.tensorInfo.get(t.dataId);if(this.state.numTensors--,"string"===t.dtype&&(this.state.numStringTensors--,this.state.numBytes-=e.bytes),"complex64"!==t.dtype&&"string"!==t.dtype){var n=t.size*ln(t.dtype);this.state.numBytes-=n}e.backend.disposeData(t.dataId)&&this.removeDataId(t.dataId,e.backend)}},t.prototype.disposeVariables=function(){for(var t in this.state.registeredVariables){var e=this.state.registeredVariables[t];this.disposeVariable(e)}},t.prototype.disposeVariable=function(t){this.disposeTensor(t),null!=this.state.registeredVariables[t.name]&&delete this.state.registeredVariables[t.name]},t.prototype.memory=function(){var t=this.backend.memory();return t.numTensors=this.state.numTensors,t.numDataBuffers=this.state.numDataBuffers,t.numBytes=this.state.numBytes,this.state.numStringTensors>0&&(t.unreliable=!0,null==t.reasons&&(t.reasons=[]),t.reasons.push("Memory usage by string tensors is approximate (2 bytes per character)")),t},t.prototype.profile=function(t){return s(this,void 0,void 0,(function(){var e,n,r,i,a,o,s,p,f,d,g;return u(this,(function(u){switch(u.label){case 0:return this.state.profiling=!0,e=this.state.numBytes,n=this.state.numTensors,this.state.activeProfile.kernels=[],r=this.state.activeProfile,[4,t()];case 1:r.result=u.sent(),this.state.profiling=!1,this.state.activeProfile.peakBytes=Math.max.apply(Math,h([],c(this.state.activeProfile.kernels.map((function(t){return t.totalBytesSnapshot}))),!1)),this.state.activeProfile.newBytes=this.state.numBytes-e,this.state.activeProfile.newTensors=this.state.numTensors-n,u.label=2;case 2:u.trys.push([2,8,9,10]),i=l(this.state.activeProfile.kernels),a=i.next(),u.label=3;case 3:return a.done?[3,7]:(o=a.value,s=o,[4,o.kernelTimeMs]);case 4:return s.kernelTimeMs=u.sent(),p=o,[4,o.extraInfo];case 5:p.extraInfo=u.sent(),u.label=6;case 6:return a=i.next(),[3,3];case 7:return[3,10];case 8:return f=u.sent(),d={error:f},[3,10];case 9:try{a&&!a.done&&(g=i.return)&&g.call(i)}finally{if(d)throw d.error}return[7];case 10:return[2,this.state.activeProfile]}}))}))},t.prototype.isTapeOn=function(){return this.state.gradientDepth>0&&0===this.state.kernelDepth},t.prototype.addTapeNode=function(t,e,n,r,i,a){var o=this,s={id:this.state.nextTapeNodeId++,kernelName:t,inputs:e,outputs:n,saved:i},u=_n(t);null!=u&&(r=u.gradFunc),null!=r&&(s.gradient=function(t){return t=t.map((function(t,e){if(null==t){var r=n[e],i=yn(r.size,r.dtype);return o.makeTensor(i,r.shape,r.dtype)}return t})),r(t.length>1?t:t[0],i,a)}),this.state.activeTape.push(s)},t.prototype.keep=function(t){return t.kept=!0,t},t.prototype.startTape=function(){0===this.state.gradientDepth&&(this.state.activeTape=[]),this.state.gradientDepth++},t.prototype.endTape=function(){this.state.gradientDepth--},t.prototype.startScope=function(t){var e={track:[],name:"unnamed scope",id:this.state.nextScopeId++};t&&(e.name=t),this.state.scopeStack.push(e),this.state.activeScope=e},t.prototype.endScope=function(t){for(var e=this,n=Br(t),r=new Set(n.map((function(t){return t.id}))),i=0;i0,(function(){return"gradients() received an empty list of xs."})),null!=n&&"float32"!==n.dtype)throw new Error("dy must have 'float32' dtype, but has '".concat(n.dtype,"'"));var a=this.scopedRun((function(){return i.startTape()}),(function(){return i.endTape()}),(function(){return i.tidy("forward",t)}));nn(a instanceof Ar,(function(){return"The result y returned by f() must be a tensor."}));var o=function(t,e,n){for(var r={},i={},a=0;a=0;a--)for(o=(d=t[a]).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.");return this.tidy("backward",(function(){var t,r,s={};s[a.id]=null==n?(t=a.shape,r=vn(rn(t),"float32"),Vr.makeTensor(r,t,"float32")):n,function(t,e,n,r){for(var i=function(i){var a=e[i],o=[];if(a.outputs.forEach((function(e){var n=t[e.id];null!=n?o.push(n):o.push(null)})),null==a.gradient)throw new Error("Cannot compute gradient: gradient function not found "+"for ".concat(a.kernelName,"."));var s=a.gradient(o),u=function(e){if(!(e in s))throw new Error("Cannot backprop through input ".concat(e,". ")+"Available gradients found: ".concat(Object.keys(s),"."));var i=n((function(){return s[e]()}));if("float32"!==i.dtype)throw new Error("Error in gradient for op ".concat(a.kernelName,". The gradient of input ")+"".concat(e," must have 'float32' dtype, but has '").concat(i.dtype,"'"));var o=a.inputs[e];if(!an(i.shape,o.shape))throw new Error("Error in gradient for op ".concat(a.kernelName,". The gradient of input ")+"'".concat(e,"' has shape '").concat(i.shape,"', which does not match ")+"the shape of the input '".concat(o.shape,"'"));if(null==t[o.id])t[o.id]=i;else{var u=t[o.id];t[o.id]=r(u,i),u.dispose()}};for(var l in a.inputs)u(l)},a=e.length-1;a>=0;a--)i(a)}(s,o,(function(t){return i.tidy(t)}),qr);var u=e.map((function(t){return s[t.id]}));return 0===i.state.gradientDepth&&(i.state.activeTape.forEach((function(t){var e,n;try{for(var r=l(t.saved),i=r.next();!i.done;i=r.next()){i.value.dispose()}}catch(t){e={error:t}}finally{try{i&&!i.done&&(n=r.return)&&n.call(r)}finally{if(e)throw e.error}}})),i.state.activeTape=null),{value:a,grads:u}}))},t.prototype.customGrad=function(t){var e=this;return nn(pn(t),(function(){return"The f passed in customGrad(f) must be a function."})),function(){for(var n,r=[],i=0;i0,(function(){return"Element arr[".concat(n.join("]["),"] should be a primitive, ")+"but is an array of ".concat(t.length," elements")})),nn(t.length===e[0],(function(){return"Element arr[".concat(n.join("]["),"] should have ").concat(e[0]," ")+"elements, but has ".concat(t.length," elements")}));for(var r=e.slice(1),i=0;i=0&&(i=r),Hr(r,i,e,n),null==t||!gr(t)&&!Array.isArray(t)&&"number"!=typeof t&&"boolean"!=typeof t&&"string"!=typeof t){var a=null==t?"null":t.constructor.name;throw new Error("Argument '".concat(e,"' passed to '").concat(n,"' must be a ")+"Tensor or TensorLike, but got '".concat(a,"'"))}var o=Gr(t,i);gr(t)||Array.isArray(t)||(t=[t]);var s="string"!==i?pr(t,i):vr(t,[],!0);return Vr.makeTensor(s,o,i)}function Zr(t,e,n,r){if(void 0===r&&(r="numeric"),!Array.isArray(t))throw new Error("Argument ".concat(e," passed to ").concat(n," must be a ")+"`Tensor[]` or `TensorLike[]`");return t.map((function(t,i){return Jr(t,"".concat(e,"[").concat(i,"]"),n,r)}))}function Yr(t){var e=Object.keys(t);if(1!==e.length)throw new Error("Please provide an object with a single key (operation name) mapping to a function. Got an object with "+"".concat(e.length," keys."));var n=e[0],r=t[n];n.endsWith("_")&&(n=n.substring(0,n.length-1)),n+="__op";var i=function(){for(var t=[],e=0;e1)&&n.unshift(a)}return n}function hi(t,e){for(var n=Math.max(t.length,e.length),r=new Array(n),i=0;i0&&(e=fi(e,n)),pi(e,r.shape)},b:function(){var e=t,n=ci(i.shape,a);return n.length>0&&(e=fi(e,n)),pi(e,i.shape)}}}},gi={kernelName:"AddN",saveAllInputs:!0,gradFunc:function(t,e){var n={};return e.forEach((function(e,r){n[r]=function(){return t.clone()}})),n}};var vi=Yr({zerosLike_:function(t){var e={x:Jr(t,"x","zerosLike")};return Vr.runKernel($e,e)}}),yi={kernelName:"ArgMax",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return vi(n)}}}},mi={kernelName:"ArgMin",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return vi(n)}}}},bi={kernelName:"Asin",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return ni(t,ai(si(ii(1),oi(Xr(n,"float32")))))}}}};var wi=Yr({add_:function(t,e){var n,r=Jr(t,"a","add"),i=Jr(e,"b","add"),a={a:r=(n=c(Mr(r,i),2))[0],b:i=n[1]};return Vr.runKernel(ye,a)}}),ki={kernelName:"Asinh",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){var e=ai(wi(ii(1),oi(Xr(n,"float32"))));return ni(t,e)}}}},xi={kernelName:"Atan2",inputsToSave:["a","b"],gradFunc:function(t,e){var n=c(e,2),r=n[0],i=n[1],a=hi(r.shape,i.shape);return{a:function(){var e=wi(oi(r),oi(i)),n=$r(t,ni(i,e)),o=ci(r.shape,a);return o.length>0&&(n=fi(n,o)),pi(n,r.shape)},b:function(){var e=wi(oi(r),oi(i)),n=ri($r(t,ni(r,e))),o=ci(i.shape,a);return o.length>0&&(n=fi(n,o)),pi(n,i.shape)}}}},Si={kernelName:"Atan",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return ni(t,wi(oi(Xr(n,"float32")),1))}}}},Ni={kernelName:"Atanh",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return ni(t,si(ii(1),oi(Xr(n,"float32"))))}}}};function Ii(t){return"number"==typeof t?[t,t,t]:2===t.length?[t[0],t[1],1]:t}function Ai(t){var e=c(Ii(t),3),n=e[0],r=e[1],i=e[2];return 1===n&&1===r&&1===i}function zi(t,e){return Ai(t)||Ai(e)}function Ei(t){return Ii(t).every((function(t){return t>0}))}function Ti(t,e,n){if(null!=n){if("string"==typeof e)throw Error("Error in ".concat(t,": pad must be an integer when using ")+"dimRoundingMode ".concat(n," but got pad ").concat(e,"."));if("number"==typeof e)nn(on(e),(function(){return"Error in ".concat(t,": pad must be an integer when using ")+"dimRoundingMode ".concat(n," but got pad ").concat(e,".")}));else{if("object"!=typeof e)throw Error("Error in ".concat(t,": Unknown padding parameter: ").concat(e));e.forEach((function(e){e.forEach((function(e){nn(on(e),(function(){return"Error in ".concat(t,": pad must be an integer when using ")+"dimRoundingMode ".concat(n," but got pad ").concat(e,".")}))}))}))}}}var Ci=Yr({avgPool3dGrad_:function(t,e,n,r,i,a){var o=Jr(t,"dy","avgPool3dGrad"),s=Jr(e,"input","avgPool3dGrad"),u=o,l=s,c=!1;4===s.rank&&(c=!0,u=pi(o,[1,o.shape[0],o.shape[1],o.shape[2],o.shape[3]]),l=pi(s,[1,s.shape[0],s.shape[1],s.shape[2],s.shape[3]])),nn(5===u.rank,(function(){return"Error in avgPool3dGrad: dy must be rank 5 but got rank "+"".concat(u.rank,".")})),nn(5===l.rank,(function(){return"Error in avgPool3dGrad: input must be rank 5 but got rank "+"".concat(l.rank,".")})),Ti("avgPool3dGrad",i,a);var h={dy:u,input:l},p={filterSize:n,strides:r,pad:i,dimRoundingMode:a},f=Vr.runKernel("AvgPool3DGrad",h,p);return c?pi(f,[f.shape[1],f.shape[2],f.shape[3],f.shape[4]]):f}}),Fi={kernelName:"AvgPool3D",inputsToSave:["x"],gradFunc:function(t,e,n){var r=c(e,1)[0],i=n.filterSize,a=n.strides,o=n.pad,s=n.dimRoundingMode;return{x:function(){return Ci(t,r,i,a,o,s)}}}};var Di=Yr({avgPoolGrad_:function(t,e,n,r,i){var a=Jr(t,"dy","avgPoolGrad"),o=Jr(e,"input","avgPoolGrad");nn(o.rank===a.rank,(function(){return"Rank of input (".concat(o.rank,") does not match rank of dy (").concat(a.rank,")")}));var s=o,u=a,l=!1;3===o.rank&&(l=!0,s=pi(o,[1,o.shape[0],o.shape[1],o.shape[2]]),u=pi(a,[1,a.shape[0],a.shape[1],a.shape[2]])),nn(4===u.rank,(function(){return"Error in avgPoolGrad: dy must be rank 4 but got rank "+"".concat(u.rank,".")})),nn(4===s.rank,(function(){return"Error in avgPoolGrad: input must be rank 4 but got rank "+"".concat(s.rank,".")}));var c={dy:u,input:s},h={filterSize:n,strides:r,pad:i},p=Vr.runKernel("AvgPoolGrad",c,h);return l?pi(p,[p.shape[1],p.shape[2],p.shape[3]]):p}}),_i={kernelName:"AvgPool",inputsToSave:["x"],gradFunc:function(t,e,n){var r=c(e,1)[0],i=n.filterSize,a=n.strides,o=n.pad;return{x:function(){return Di(t,r,i,a,o)}}}};var Li=Yr({matMul_:function(t,e,n,r){var i;void 0===n&&(n=!1),void 0===r&&(r=!1);var a=Jr(t,"a","matMul"),o=Jr(e,"b","matMul"),s={a:a=(i=c(Mr(a,o),2))[0],b:o=i[1]},u={transposeA:n,transposeB:r};return Vr.runKernel(me,s,u)}}),Ri={kernelName:me,inputsToSave:["a","b"],gradFunc:function(t,e,n){var r=c(e,2),i=r[0],a=r[1],o=n.transposeA,s=n.transposeB;return o||s?!o&&s?{a:function(){return Li(t,a,!1,!1)},b:function(){return Li(t,i,!0,!1)}}:o&&!s?{a:function(){return Li(a,t,!1,!0)},b:function(){return Li(i,t,!1,!1)}}:{a:function(){return Li(a,t,!0,!0)},b:function(){return Li(t,i,!0,!0)}}:{a:function(){return Li(t,a,!1,!0)},b:function(){return Li(i,t,!0,!1)}}}};var Oi=Yr({spaceToBatchND_:function(t,e,n){var r=Jr(t,"x","spaceToBatchND");nn(r.rank>=1+e.length,(function(){return"input rank ".concat(r.rank," should be > than [blockShape] ").concat(e.length)})),nn(n.length===e.length,(function(){return"paddings.shape[0] ".concat(n.length," must be equal to [blockShape] ").concat(e.length)})),nn(r.shape.reduce((function(t,r,i){return i>0&&i<=e.length?t&&(r+n[i-1][0]+n[i-1][1])%e[i-1]==0:t}),!0),(function(){return"input spatial dimensions ".concat(r.shape.slice(1)," with paddings ").concat(n.toString()," must be divisible by blockShapes ").concat(e.toString())}));var i={x:r},a={blockShape:e,paddings:n};return Vr.runKernel(Ke,i,a)}}),Mi={kernelName:be,gradFunc:function(t,e,n){var r=n.blockShape,i=n.crops;return{x:function(){return Oi(t,r,i)}}}},Bi={kernelName:"BroadcastTo",gradFunc:function(t,e,n){for(var r=n,i=r.inputShape,a=r.shape,o=Array.from(a),s=i.length-1;s>=0;s--)if(i[s]===a[s])o[s]=1;else if(1!==i[s])throw new Error("broadcastTo(): [".concat(i,"] cannot be broadcast to [").concat(a,"]."));var u=[];for(s=0;s1&&u.push(s);return{x:function(){return fi(t,u,!0)}}}},Pi={kernelName:we,gradFunc:function(t){return{x:function(){return t.clone()}}}},ji={kernelName:"Ceil",gradFunc:function(t){return{x:function(){return vi(t)}}}};var Ui=Yr({greaterEqual_:function(t,e){var n,r=Jr(t,"a","greaterEqual","string_or_numeric"),i=Jr(e,"b","greaterEqual","string_or_numeric");r=(n=c(Mr(r,i),2))[0],i=n[1],hi(r.shape,i.shape);var a={a:r,b:i};return Vr.runKernel(De,a)}});var Wi=Yr({lessEqual_:function(t,e){var n,r=Jr(t,"a","lessEqual","string_or_numeric"),i=Jr(e,"b","lessEqual","string_or_numeric");r=(n=c(Mr(r,i),2))[0],i=n[1],hi(r.shape,i.shape);var a={a:r,b:i};return Vr.runKernel("LessEqual",a)}});var Vi=Yr({logicalAnd_:function(t,e){var n=Jr(t,"a","logicalAnd","bool"),r=Jr(e,"b","logicalAnd","bool");hi(n.shape,r.shape);var i={a:n,b:r};return Vr.runKernel("LogicalAnd",i)}});var qi=Yr({clone_:function(t){var e={x:Jr(t,"x","clone","string_or_numeric")};return Vr.runKernel(_e,e)}});var Gi=Yr({broadcastTo_:function(t,e){var n=Jr(t,"broadcastTo","x"),r=n.shape;if(mn(e),e.lengthn.rank){for(var i=n.shape.slice();i.length=0;s--)if(a[s]===e[s])o[s]=1;else if(1!==n.shape[s])throw new Error("broadcastTo(): [".concat(r,"] cannot be broadcast to [").concat(e,"]."));var u=o.map((function(t,e){return t>1?e:-1})).filter((function(t){return t>=0}));if(0===u.length)return qi(n);var l={x:n},c={reps:o};return Vr.runKernel(Je,l,c)}});var Ki=Yr({where_:function(t,e,n){var r=Jr(e,"a","where"),i=Jr(n,"b","where"),a=Jr(t,"condition","where","bool"),o=hi(hi(a.shape,r.shape),i.shape),s={condition:Gi(a,o),t:Gi(r,o),e:Gi(i,o)};return Vr.runKernel(Ue,s)}}),Hi={kernelName:"ClipByValue",inputsToSave:["x"],gradFunc:function(t,e,n){var r=c(e,1)[0],i=n.clipValueMin,a=n.clipValueMax;return{x:function(){return Ki(Vi(Ui(r,i),Wi(r,a)),t,vi(t))}}}},Ji={kernelName:ke,inputsToSave:["x"],gradFunc:ti.gradFunc};var Zi=Yr({split_:function(t,e,n){void 0===n&&(n=0);var r={x:Jr(t,"x","split")},i={numOrSizeSplits:e,axis:n};return Vr.runKernel(He,r,i)}}),Yi={kernelName:xe,saveAllInputs:!0,gradFunc:function(t,e,n){var r=e.map((function(t){return t.shape})),i=un(n.axis,e[0].shape)[0],a=r.map((function(t){return t[i]}));return Zi(t,a,i).map((function(t){return function(){return t}}))}};var Xi=Yr({conv2DBackpropFilter_:function(t,e,n,r,i,a,o){void 0===a&&(a="NHWC");var s=t;3===t.rank&&(s=pi(t,[1,t.shape[0],t.shape[1],t.shape[2]]));var u=e;3===u.rank&&(u=pi(e,[1,e.shape[0],e.shape[1],e.shape[2]])),nn(4===s.rank,(function(){return"Error in conv2dDerFilter: input must be rank 4, but got shape "+"".concat(s.shape,".")})),nn(4===u.rank,(function(){return"Error in conv2dDerFilter: dy must be rank 4, but got shape "+"".concat(u.shape,".")})),nn(4===n.length,(function(){return"Error in conv2dDerFilter: filterShape must be length 4, but got "+"".concat(n,".")}));var l="NHWC"===a?s.shape[3]:s.shape[1],c="NHWC"===a?u.shape[3]:u.shape[1];nn(l===n[2],(function(){return"Error in conv2dDerFilter: depth of input ".concat(l,") must ")+"match input depth in filter (".concat(n[2],".")})),nn(c===n[3],(function(){return"Error in conv2dDerFilter: depth of dy (".concat(c,") must ")+"match output depth for filter (".concat(n[3],").")})),Ti("conv2dDerFilter",i,o);var h={x:s,dy:u},p={strides:r,pad:i,dataFormat:a,dimRoundingMode:o,filterShape:n};return Vr.runKernel("Conv2DBackpropFilter",h,p)}});var $i=Yr({conv2DBackpropInput_:function(t,e,n,r,i,a,o){void 0===a&&(a="NHWC"),nn(t.length===e.rank,(function(){return"Length of inShape "+"(".concat(t.length,") and rank of dy (").concat(e.rank,") must match")}));var s=t,u=e,l=!1;3===e.rank&&(l=!0,u=pi(e,[1,e.shape[0],e.shape[1],e.shape[2]]),s=[1,t[0],t[1],t[2]]),nn(4===s.length,(function(){return"Error in conv2dDerInput: inShape must be length 4, but got length "+"".concat(s.length,".")})),nn(4===u.rank,(function(){return"Error in conv2dDerInput: dy must be rank 4, but got "+"rank ".concat(u.rank)})),nn(4===n.rank,(function(){return"Error in conv2dDerInput: filter must be rank 4, but got "+"rank ".concat(n.rank)}));var c="NHWC"===a?s[3]:s[1],h="NHWC"===a?u.shape[3]:u.shape[1];nn(c===n.shape[2],(function(){return"Error in conv2dDerInput: depth of input (".concat(c,") must ")+"match input depth for filter ".concat(n.shape[2],".")})),nn(h===n.shape[3],(function(){return"Error in conv2dDerInput: depth of output (".concat(h,") must ")+"match output depth for filter ".concat(n.shape[3],".")})),Ti("conv2dDerInput",i,o);var p={dy:u,filter:n},f={strides:r,pad:i,dataFormat:a,dimRoundingMode:o,inputShape:s},d=Vr.runKernel(Ne,p,f);return l?pi(d,[d.shape[1],d.shape[2],d.shape[3]]):d}}),Qi={kernelName:Se,inputsToSave:["x","filter"],gradFunc:function(t,e,n){var r=c(e,2),i=r[0],a=r[1],o=n.dilations,s=n.strides,u=n.pad,l=n.dataFormat;return nn(Ai(o),(function(){return"Error in gradient of conv2D: dilation rates greater than 1 "+"are not yet supported in gradients. Got dilations '".concat(o,"'")})),{x:function(){return $i(i.shape,t,a,s,u,l)},filter:function(){return Xi(i,t,a.shape,s,u,l)}}}};var ta=Yr({conv2d_:function(t,e,n,r,i,a,o){void 0===i&&(i="NHWC"),void 0===a&&(a=[1,1]);var s=Jr(t,"x","conv2d","float32"),u=Jr(e,"filter","conv2d","float32"),l=s,c=!1;3===s.rank&&(c=!0,l=pi(s,[1,s.shape[0],s.shape[1],s.shape[2]])),nn(4===l.rank,(function(){return"Error in conv2d: input must be rank 4, but got rank ".concat(l.rank,".")})),nn(4===u.rank,(function(){return"Error in conv2d: filter must be rank 4, but got rank "+"".concat(u.rank,".")})),Ti("conv2d",r,o);var h="NHWC"===i?l.shape[3]:l.shape[1];nn(h===u.shape[2],(function(){return"Error in conv2d: depth of input (".concat(h,") must match ")+"input depth for filter ".concat(u.shape[2],".")})),nn(zi(n,a),(function(){return"Error in conv2D: Either strides or dilations must be 1. "+"Got strides ".concat(n," and dilations '").concat(a,"'")})),nn(Ei(a),(function(){return"Error in conv2D: Dilated rates should be larger than 0."})),nn(Ei(n),(function(){return"Error in conv2D: Strides should be larger than 0."}));var p={x:l,filter:u},f={strides:n,pad:r,dataFormat:i,dilations:a,dimRoundingMode:o},d=Vr.runKernel(Se,p,f);return c?pi(d,[d.shape[1],d.shape[2],d.shape[3]]):d}}),ea={kernelName:Ne,inputsToSave:["dy","filter"],gradFunc:function(t,e,n){var r=c(e,2),i=r[0],a=r[1],o=n.strides,s=n.pad,u=n.dataFormat,l=n.dimRoundingMode;return{dy:function(){return ta(t,a,o,s,u,1,l)},filter:function(){return Xi(t,i,a.shape,o,s,u,l)}}}};var na=Yr({conv3DBackpropFilter_:function(t,e,n,r,i){var a=t;4===t.rank&&(a=pi(t,[1,t.shape[0],t.shape[1],t.shape[2],t.shape[3]]));var o=e;4===o.rank&&(o=pi(e,[1,e.shape[0],e.shape[1],e.shape[2],e.shape[3]])),nn(5===a.rank,(function(){return"Error in conv3dDerFilter: input must be rank 5, but got shape "+"".concat(a.shape,".")})),nn(5===o.rank,(function(){return"Error in conv3dDerFilter: dy must be rank 5, but got shape "+"".concat(o.shape,".")})),nn(5===n.length,(function(){return"Error in conv3dDerFilter: filterShape must be length 5, but got "+"".concat(n,".")})),nn(a.shape[4]===n[3],(function(){return"Error in conv3dDerFilter: depth of input ".concat(a.shape[4],") must ")+"match input depth in filter (".concat(n[3],".")})),nn(o.shape[4]===n[4],(function(){return"Error in conv3dDerFilter: depth of dy (".concat(o.shape[4],") must ")+"match output depth for filter (".concat(n[4],").")}));var s={x:a,dy:o},u={strides:r,pad:i,filterShape:n};return Vr.runKernel("Conv3DBackpropFilterV2",s,u)}});var ra=Yr({conv3DBackpropInput_:function(t,e,n,r,i){nn(t.length===e.rank,(function(){return"Length of inShape "+"(".concat(t.length,") and rank of dy (").concat(e.rank,") must match")}));var a=t,o=e,s=!1;4===e.rank&&(s=!0,o=pi(e,[1,e.shape[0],e.shape[1],e.shape[2],e.shape[3]]),a=[1,t[0],t[1],t[2],t[3]]);var u=a[4],l=o.shape[4];nn(5===a.length,(function(){return"Error in conv3dDerInput: inShape must be length 5, but got length "+"".concat(a.length,".")})),nn(5===o.rank,(function(){return"Error in conv3dDerInput: dy must be rank 5, but got "+"rank ".concat(o.rank)})),nn(5===n.rank,(function(){return"Error in conv3dDerInput: filter must be rank 5, but got "+"rank ".concat(n.rank)})),nn(u===n.shape[3],(function(){return"Error in conv3dDerInput: depth of input (".concat(u,") must ")+"match input depth for filter ".concat(n.shape[3],".")})),nn(l===n.shape[4],(function(){return"Error in conv3dDerInput: depth of output (".concat(l,") must ")+"match output depth for filter ".concat(n.shape[4],".")}));var c={dy:o,filter:n},h={pad:i,strides:r,inputShape:a},p=Vr.runKernel("Conv3DBackpropInputV2",c,h);return s?pi(p,[p.shape[1],p.shape[2],p.shape[3],p.shape[4]]):p}}),ia={kernelName:"Conv3D",inputsToSave:["x","filter"],gradFunc:function(t,e,n){var r=n.dilations,i=n.strides,a=n.pad;nn(Ai(r),(function(){return"Error in gradient of conv3D: dilation rates greater than 1 are "+"not yet supported in gradients. Got dilations '".concat(r,"'")}));var o=c(e,2),s=o[0],u=o[1];return{x:function(){return ra(s.shape,t,u,i,a)},filter:function(){return na(s,t,u.shape,i,a)}}}};var aa=Yr({sin_:function(t){var e={x:Jr(t,"x","sin","float32")};return Vr.runKernel("Sin",e)}}),oa={kernelName:"Cos",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return $r(ri(aa(Xr(n,"float32"))),t)}}}};var sa=Yr({sinh_:function(t){var e={x:Jr(t,"x","sinh")};return Vr.runKernel(Ve,e)}}),ua={kernelName:Ie,inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return $r(sa(Xr(n,"float32")),t)}}}};function la(t,e){return function(t,e,n){for(var r=t.length+e.length,i=[],a=0,o=0,s=0;s=0&&t0?pi(fi(e,n),r.shape):e},b:function(){var e=$r(t,Xr(r,"float32")),n=ci(i.shape,a);n.length>0&&(e=pi(fi(e,n),i.shape));var o=oi(i);return ri(ni(e,Xr(o,"float32")))}}}};var Da=Yr({rsqrt_:function(t){var e={x:Jr(t,"x","rsqrt","float32")};return Vr.runKernel(je,e)}});var _a=Yr({tile_:function(t,e){var n=Jr(t,"x","tile","string_or_numeric");nn(n.rank===e.length,(function(){return"Error in transpose: rank of input ".concat(n.rank," ")+"must match length of reps ".concat(e,".")}));var r={x:n},i={reps:e};return Vr.runKernel(Je,r,i)}}),La={kernelName:"FusedBatchNorm",inputsToSave:["x","mean","variance","scale"],gradFunc:function(t,e,n){var r=n.varianceEpsilon,i=c(e,4),a=i[0],o=i[1],s=i[2],u=i[3],l=null==u?ii(1):u,h=ci(o.shape,a.shape),p=[];if(1===o.rank){for(var f=0;f=1,(function(){return"Pass at least one tensor to tf.stack"})),n.length>0&&nn(e<=n[0].rank,(function(){return"Axis must be <= rank of the tensor"}));var r=n,i={axis:e};return Vr.runKernel(Oe,r,i)}});var Oa=Yr({unsortedSegmentSum_:function(t,e,n){var r=Jr(t,"x","unsortedSegmentSum"),i=Jr(e,"segmentIds","unsortedSegmentSum","int32");nn(on(n),(function(){return"numSegments must be of dtype int"}));var a={x:r,segmentIds:i},o={numSegments:n};return Vr.runKernel(Xe,a,o)}}),Ma={kernelName:Fe,inputsToSave:["x","indices"],gradFunc:function(t,e,n){var r=c(e,2),i=r[0],a=r[1],o=n.axis,s=n.batchDims,u=un(o,i.shape)[0],l=function(t,e,n){return function(){var r=t.shape,i=e.size,a=r.slice(0,u),s=a.length,l=r.slice(o,r.length).slice(1),c=l.length,h=Ba(0,s),p=Ba(s+1,s+1+c),f=Pa([a,[i],l]),d=pi(n,f),g=pi(e,[i]),v=Pa([[s],h,p]),y=ma(d,v),m=Oa(y,g,t.shape[u]),b=ha(v);return m=ma(m,b)}};if(1===s){var h=i.shape[0],p=i.split(h,0);return{x:function(){var e=Ra(p.map((function(e,n){return l(e,a.slice(n,1),t.slice(n,1))()})));return e.reshape(i.shape)},indices:function(){return a}}}return{x:l(i,a,t),indices:function(){return a}}}};function Ba(t,e){for(var n=[],r=t;r0?pi(fi(t,e),r.shape):t},b:function(){var e=$r(t,ri(go(ni(r,i)))),n=ci(i.shape,a);return n.length>0?pi(fi(e,n),i.shape):e}}}},yo={kernelName:Re,inputsToSave:["a","b"],gradFunc:function(t,e){var n=c(e,2),r=n[0],i=n[1],a=hi(r.shape,i.shape);return{a:function(){var e=$r(t,Xr(i,"float32")),n=ci(r.shape,a);return n.length>0?pi(fi(e,n),r.shape):e},b:function(){var e=$r(t,Xr(r,"float32")),n=ci(i.shape,a);return n.length>0?pi(fi(e,n),i.shape):e}}}},mo={kernelName:"Neg",gradFunc:function(t){return{x:function(){return ri(t)}}}},bo={kernelName:"OneHot",inputsToSave:["indices"],gradFunc:function(t,e){var n=e[0];return{indices:function(){return so(n.shape,"float32")}}}},wo={kernelName:"OnesLike",gradFunc:function(t){return{x:function(){return vi(t)}}}};var ko=Yr({unstack_:function(t,e){void 0===e&&(e=0);var n=Jr(t,"x","unstack","string_or_numeric");nn(e>=-n.shape.length&&e0&&(n=fi(n,r)),pi(n,o.shape)},b:function(){var e=Ga(o,0),n=Ki(e,No(o),vi(o)),r=$r(t,$r(a,n)),i=ci(s.shape,u);return i.length>0&&(r=fi(r,i)),pi(r,s.shape)}}}},zo={kernelName:"Prelu",inputsToSave:["x","alpha"],gradFunc:function(t,e){var n=c(e,2),r=n[0],i=n[1],a=Ga(r,0);return{x:function(){return Ki(a,t,$r(t,i))},alpha:function(){var e=Ki(a,vi(t),$r(t,r)),n=ci(i.shape,t.shape);return n.length>0&&(e=fi(e,n)),pi(e,i.shape)}}}};var Eo=Nn();Eo.registerFlag("DEBUG",(function(){return!1}),(function(t){t&&console.warn("Debugging mode is ON. The output of every math call will be downloaded to CPU and checked for NaNs. This significantly impacts performance.")})),Eo.registerFlag("IS_BROWSER",(function(){return"undefined"!=typeof window&&null!=window.document||"undefined"!=typeof WorkerGlobalScope})),Eo.registerFlag("IS_NODE",(function(){return"undefined"!=typeof process&&"undefined"!=typeof process.versions&&"undefined"!=typeof process.versions.node})),Eo.registerFlag("IS_CHROME",(function(){return"undefined"!=typeof navigator&&null!=navigator&&null!=navigator.userAgent&&/Chrome/.test(navigator.userAgent)&&/Google Inc/.test(navigator.vendor)})),Eo.registerFlag("IS_SAFARI",(function(){return"undefined"!=typeof navigator&&null!=navigator&&null!=navigator.userAgent&&/Safari/.test(navigator.userAgent)&&/Apple/.test(navigator.vendor)})),Eo.registerFlag("PROD",(function(){return!1})),Eo.registerFlag("TENSORLIKE_CHECK_SHAPE_CONSISTENCY",(function(){return Eo.getBool("DEBUG")})),Eo.registerFlag("DEPRECATION_WARNINGS_ENABLED",(function(){return!0})),Eo.registerFlag("IS_TEST",(function(){return!1})),Eo.registerFlag("CHECK_COMPUTATION_FOR_ERRORS",(function(){return Eo.getBool("DEBUG")})),Eo.registerFlag("WRAP_TO_IMAGEBITMAP",(function(){return!1})),Eo.registerFlag("CANVAS2D_WILL_READ_FREQUENTLY_FOR_GPU",(function(){return!1})),Eo.registerFlag("USE_SETTIMEOUTCUSTOM",(function(){return!1}));var To=function(){function t(t){if(this.shards=[],this.previousShardIndex=0,null!=t&&(t instanceof Array||(t=[t]),0!==(t=t.map((function(t){return gr(t)?t.buffer:t}))).length)){this.bufferUniformSize=t[0].byteLength;for(var e=0,n=0;n=this.byteLength)return-1;if(null!=this.bufferUniformSize)return this.previousShardIndex=Math.floor(t/this.bufferUniformSize),this.previousShardIndex;function e(e){return t=e.end?1:0}if(0===e(this.shards[this.previousShardIndex]))return this.previousShardIndex;var n=function(t,e){var n=0,r=t.length;for(;n<=r;){var i=Math.floor((r-n)/2)+n,a=e(t[i]);if(0===a)return i;a<0?r=i:n=i+1}return-1}(this.shards,e);return-1===n?-1:(this.previousShardIndex=n,this.previousShardIndex)},t}();var Co="undefined"!=typeof Buffer&&("undefined"==typeof Blob||"undefined"==typeof atob||"undefined"==typeof btoa);function Fo(t){return Co?Buffer.byteLength(t,"utf8"):new Blob([t]).size}function Do(t,e){var n={modelTopology:t.modelTopology,format:t.format,generatedBy:t.generatedBy,convertedBy:t.convertedBy,weightsManifest:e};return null!=t.signature&&(n.signature=t.signature),null!=t.userDefinedMetadata&&(n.userDefinedMetadata=t.userDefinedMetadata),null!=t.modelInitializer&&(n.modelInitializer=t.modelInitializer),null!=t.initializerSignature&&(n.initializerSignature=t.initializerSignature),null!=t.trainingConfig&&(n.trainingConfig=t.trainingConfig),n}function _o(t,e,n){var r={modelTopology:t.modelTopology,format:t.format,generatedBy:t.generatedBy,convertedBy:t.convertedBy};if(null!=t.trainingConfig&&(r.trainingConfig=t.trainingConfig),null!=t.weightsManifest){if(!e)throw new Error("modelJSON has weightsManifest but weightSpecs is null");if(!n)throw new Error("modelJSON has weightsManifest but weightData is null");r.weightSpecs=e,r.weightData=n}return null!=t.signature&&(r.signature=t.signature),null!=t.userDefinedMetadata&&(r.userDefinedMetadata=t.userDefinedMetadata),null!=t.modelInitializer&&(r.modelInitializer=t.modelInitializer),null!=t.initializerSignature&&(r.initializerSignature=t.initializerSignature),r}function Lo(t,e){return s(this,void 0,void 0,(function(){var n,r,i;return u(this,(function(a){switch(a.label){case 0:return null==t.weightsManifest?[3,2]:[4,e(t.weightsManifest)];case 1:i=c.apply(void 0,[a.sent(),2]),n=i[0],r=i[1],a.label=2;case 2:return[2,_o(t,n,r)]}}))}))}function Ro(t){if(t.modelTopology instanceof ArrayBuffer)throw new Error("Expected JSON model topology, received ArrayBuffer.");return{dateSaved:new Date,modelTopologyType:"JSON",modelTopologyBytes:null==t.modelTopology?0:Fo(JSON.stringify(t.modelTopology)),weightSpecsBytes:null==t.weightSpecs?0:Fo(JSON.stringify(t.weightSpecs)),weightDataBytes:null==t.weightData?0:new To(t.weightData).byteLength}}function Oo(t){var e,n,r=[];try{for(var i=l(t),a=i.next();!a.done;a=i.next()){var o=a.value;r.push.apply(r,h([],c(o.weights),!1))}}catch(t){e={error:t}}finally{try{a&&!a.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}return r}var Mo=function(){function t(){this.saveRouters=[],this.loadRouters=[]}return t.getInstance=function(){return null==t.instance&&(t.instance=new t),t.instance},t.registerSaveRouter=function(e){t.getInstance().saveRouters.push(e)},t.registerLoadRouter=function(e){t.getInstance().loadRouters.push(e)},t.getSaveHandlers=function(e){return t.getHandlers(e,"save")},t.getLoadHandlers=function(e,n){return t.getHandlers(e,"load",n)},t.getHandlers=function(e,n,r){var i=[];return("load"===n?t.getInstance().loadRouters:t.getInstance().saveRouters).forEach((function(t){var n=t(e,r);null!==n&&i.push(n)})),i},t}(),Bo="models_store",Po="model_info_store";var jo=function(){function t(t){if(this.indexedDB=function(){if(!Nn().getBool("IS_BROWSER"))throw new Error("Failed to obtain IndexedDB factory because the current environmentis not a web browser.");var t="undefined"==typeof window?self:window,e=t.indexedDB||t.mozIndexedDB||t.webkitIndexedDB||t.msIndexedDB||t.shimIndexedDB;if(null==e)throw new Error("The current browser does not appear to support IndexedDB.");return e}(),null==t||!t)throw new Error("For IndexedDB, modelPath must not be null, undefined or empty.");this.modelPath=t}return t.prototype.save=function(t){return s(this,void 0,void 0,(function(){return u(this,(function(e){if(t.modelTopology instanceof ArrayBuffer)throw new Error("BrowserLocalStorage.save() does not support saving model topology in binary formats yet.");return[2,this.databaseAction(this.modelPath,t)]}))}))},t.prototype.load=function(){return s(this,void 0,void 0,(function(){return u(this,(function(t){return[2,this.databaseAction(this.modelPath)]}))}))},t.prototype.databaseAction=function(t,e){var n=this;return new Promise((function(t,r){var i=n.indexedDB.open("tensorflowjs",1);i.onupgradeneeded=function(){return function(t){var e=t.result;e.createObjectStore(Bo,{keyPath:"modelPath"}),e.createObjectStore(Po,{keyPath:"modelPath"})}(i)},i.onsuccess=function(){var a=i.result;if(null==e){var o=a.transaction(Bo,"readonly"),s=o.objectStore(Bo).get(n.modelPath);s.onsuccess=function(){if(null==s.result)return a.close(),r(new Error("Cannot find model with path '".concat(n.modelPath,"' ")+"in IndexedDB."));t(s.result.modelArtifacts)},s.onerror=function(t){return a.close(),r(s.error)},o.oncomplete=function(){return a.close()}}else{e.weightData=To.join(e.weightData);var u,l,c=Ro(e),h=a.transaction(Po,"readwrite"),p=h.objectStore(Po);try{u=p.put({modelPath:n.modelPath,modelArtifactsInfo:c})}catch(t){return r(t)}u.onsuccess=function(){var i,o=(l=a.transaction(Bo,"readwrite")).objectStore(Bo);try{i=o.put({modelPath:n.modelPath,modelArtifacts:e,modelArtifactsInfo:c})}catch(t){return r(t)}i.onsuccess=function(){return t({modelArtifactsInfo:c})},i.onerror=function(t){var e=(p=h.objectStore(Po)).delete(n.modelPath);e.onsuccess=function(){return a.close(),r(i.error)},e.onerror=function(t){return a.close(),r(i.error)}}},u.onerror=function(t){return a.close(),r(u.error)},h.oncomplete=function(){null==l?a.close():l.oncomplete=function(){return a.close()}}}},i.onerror=function(t){return r(i.error)}}))},t}();jo.URL_SCHEME="indexeddb://";var Uo=function(t){return Nn().getBool("IS_BROWSER")&&!Array.isArray(t)&&t.startsWith(jo.URL_SCHEME)?(e=t.slice(jo.URL_SCHEME.length),new jo(e)):null;var e};Mo.registerSaveRouter(Uo),Mo.registerLoadRouter(Uo);var Wo="/",Vo="tensorflowjs_models",qo="info",Go="model_topology",Ko="weight_specs",Ho="weight_data",Jo="model_metadata";var Zo=function(){function t(t){if(!Nn().getBool("IS_BROWSER")||"undefined"==typeof window||"undefined"==typeof window.localStorage)throw new Error("The current environment does not support local storage.");if(this.LS=window.localStorage,null==t||!t)throw new Error("For local storage, modelPath must not be null, undefined or empty.");var e;this.modelPath=t,this.keys=(e=this.modelPath,{info:[Vo,e,qo].join(Wo),topology:[Vo,e,Go].join(Wo),weightSpecs:[Vo,e,Ko].join(Wo),weightData:[Vo,e,Ho].join(Wo),modelMetadata:[Vo,e,Jo].join(Wo)})}return t.prototype.save=function(t){return s(this,void 0,void 0,(function(){var e,n,r,i,a;return u(this,(function(o){if(t.modelTopology instanceof ArrayBuffer)throw new Error("BrowserLocalStorage.save() does not support saving model topology in binary formats yet.");e=JSON.stringify(t.modelTopology),n=JSON.stringify(t.weightSpecs),r=Ro(t),i=To.join(t.weightData);try{return this.LS.setItem(this.keys.info,JSON.stringify(r)),this.LS.setItem(this.keys.topology,e),this.LS.setItem(this.keys.weightSpecs,n),this.LS.setItem(this.keys.weightData,function(t){if(Co)return Buffer.from(t).toString("base64");for(var e=new Uint8Array(t),n="",r=0,i=e.length;r0,(function(){return"promises must be a none empty array"}))}(t),function(t,e){nn(t>=0&&t<=1,(function(){return"Progress fraction must be in range [0, 1], but "+"got startFraction ".concat(t)})),nn(e>=0&&e<=1,(function(){return"Progress fraction must be in range [0, 1], but "+"got endFraction ".concat(e)})),nn(e>=t,(function(){return"startFraction must be no more than endFraction, but "+"got startFraction ".concat(t," and endFraction ")+"".concat(e)}))}(n=null==n?0:n,r=null==r?1:r);var i=0;return Promise.all(t.map((function(a){return a.then((function(a){var o=n+ ++i/t.length*(r-n);return e(o),a})),a})))}function ts(t,e){return s(this,void 0,void 0,(function(){var n,r,i,a,o,s,l,c,h;return u(this,(function(u){switch(u.label){case 0:return null==e&&(e={}),n=null==e.fetchFunc?Nn().platform.fetch:e.fetchFunc,r=t.map((function(t){return n(t,e.requestInit,{isBinary:!0})})),i=0,a=.5,null!=e.onProgress?[3,2]:[4,Promise.all(r)];case 1:return o=u.sent(),[3,4];case 2:return[4,Qo(r,e.onProgress,i,a)];case 3:o=u.sent(),u.label=4;case 4:return s=o.map((function(t){return t.arrayBuffer()})),l=.5,c=1,null!=e.onProgress?[3,6]:[4,Promise.all(s)];case 5:return h=u.sent(),[3,8];case 6:return[4,Qo(s,e.onProgress,l,c)];case 7:h=u.sent(),u.label=8;case 8:return[2,h]}}))}))}Mo.registerSaveRouter((function(t){return Nn().getBool("IS_BROWSER")&&!Array.isArray(t)&&t.startsWith($o.URL_SCHEME)?function(t){void 0===t&&(t="model");return new $o(t)}(t.slice($o.URL_SCHEME.length)):null}));var es=function(){function t(t,e){if(this.DEFAULT_METHOD="POST",null==e&&(e={}),this.weightPathPrefix=e.weightPathPrefix,this.weightUrlConverter=e.weightUrlConverter,null!=e.fetchFunc?(nn("function"==typeof e.fetchFunc,(function(){return"Must pass a function that matches the signature of `fetch` (see https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)"})),this.fetch=e.fetchFunc):this.fetch=Nn().platform.fetch,nn(null!=t&&t.length>0,(function(){return"URL path for http must not be null, undefined or empty."})),Array.isArray(t)&&nn(2===t.length,(function(){return"URL paths for http must have a length of 2, "+"(actual length is ".concat(t.length,").")})),this.path=t,null!=e.requestInit&&null!=e.requestInit.body)throw new Error("requestInit is expected to have no pre-existing body, but has one.");this.requestInit=e.requestInit||{},this.loadOptions=e}return t.prototype.save=function(t){return s(this,void 0,void 0,(function(){var e,n,r,i,a;return u(this,(function(o){switch(o.label){case 0:if(t.modelTopology instanceof ArrayBuffer)throw new Error("BrowserHTTPRequest.save() does not support saving model topology in binary formats yet.");return(e=Object.assign({method:this.DEFAULT_METHOD},this.requestInit)).body=new FormData,n=[{paths:["./model.weights.bin"],weights:t.weightSpecs}],r=Do(t,n),e.body.append("model.json",new Blob([JSON.stringify(r)],{type:"application/json"}),"model.json"),null!=t.weightData&&(i=To.join(t.weightData),e.body.append("model.weights.bin",new Blob([i],{type:"application/octet-stream"}),"model.weights.bin")),[4,this.fetch(this.path,e)];case 1:if((a=o.sent()).ok)return[2,{modelArtifactsInfo:Ro(t),responses:[a]}];throw new Error("BrowserHTTPRequest.save() failed due to HTTP response status "+"".concat(a.status,"."))}}))}))},t.prototype.loadModelJSON=function(){return s(this,void 0,void 0,(function(){var t,e,n,r,i;return u(this,(function(a){switch(a.label){case 0:return[4,this.fetch(this.path,this.requestInit)];case 1:if(!(t=a.sent()).ok)throw new Error("Request to ".concat(this.path," failed with status code ")+"".concat(t.status,". Please verify this URL points to ")+"the model JSON of the model to load.");a.label=2;case 2:return a.trys.push([2,4,,5]),[4,t.json()];case 3:return e=a.sent(),[3,5];case 4:throw a.sent(),n="Failed to parse model JSON of response from ".concat(this.path,"."),this.path.endsWith(".pb")?n+=" Your path contains a .pb file extension. Support for .pb models have been removed in TensorFlow.js 1.0 in favor of .json models. You can re-convert your Python TensorFlow model using the TensorFlow.js 1.0 conversion scripts or you can convert your.pb models with the 'pb2json'NPM script in the tensorflow/tfjs-converter repository.":n+=" Please make sure the server is serving valid JSON for this request.",new Error(n);case 5:if(r=e.modelTopology,i=e.weightsManifest,null==r&&null==i)throw new Error("The JSON from HTTP path ".concat(this.path," contains neither model ")+"topology or manifest for weights.");return[2,e]}}))}))},t.prototype.load=function(){return s(this,void 0,void 0,(function(){var t=this;return u(this,(function(e){switch(e.label){case 0:return this.loadOptions.streamWeights?[2,this.loadStream()]:[4,this.loadModelJSON()];case 1:return[2,Lo(e.sent(),(function(e){return t.loadWeights(e)}))]}}))}))},t.prototype.loadStream=function(){return s(this,void 0,void 0,(function(){var t,e,n,r,i=this;return u(this,(function(a){switch(a.label){case 0:return[4,this.loadModelJSON()];case 1:return t=a.sent(),[4,this.getWeightUrls(t.weightsManifest)];case 2:return e=a.sent(),n=Oo(t.weightsManifest),r=function(){return function(t,e){var n,r,i=this,a=null==e.fetchFunc?Nn().platform.fetch:e.fetchFunc,o=0;return null===(n=e.onProgress)||void 0===n||n.call(e,0),new ReadableStream({pull:function(n){return s(i,void 0,void 0,(function(){var i,s,l,c,h;return u(this,(function(u){switch(u.label){case 0:return oe?t.substring(n):"";return[r+"/",i]}(e),2),r=n[0],i=n[1],a=this.weightPathPrefix||r,o=[],s=[];try{for(p=l(t),f=p.next();!f.done;f=p.next()){d=f.value;try{for(N=void 0,g=l(d.paths),v=g.next();!v.done;v=g.next())y=v.value,null!=this.weightUrlConverter?s.push(this.weightUrlConverter(y)):o.push(a+y+i)}catch(t){N={error:t}}finally{try{v&&!v.done&&(I=g.return)&&I.call(g)}finally{if(N)throw N.error}}}}catch(t){x={error:t}}finally{try{f&&!f.done&&(S=p.return)&&S.call(p)}finally{if(x)throw x.error}}return this.weightUrlConverter?(b=(m=o.push).apply,w=[o],k=[[]],[4,Promise.all(s)]):[3,2];case 1:b.apply(m,w.concat([h.apply(void 0,k.concat([c.apply(void 0,[u.sent()]),!1]))])),u.label=2;case 2:return[2,o]}}))}))},t.prototype.loadWeights=function(t){return s(this,void 0,void 0,(function(){var e,n,r;return u(this,(function(i){switch(i.label){case 0:return[4,this.getWeightUrls(t)];case 1:return e=i.sent(),n=Oo(t),[4,ts(e,this.loadOptions)];case 2:return r=i.sent(),[2,[n,r]]}}))}))},t}();function ns(t){return null!=t.match(es.URL_SCHEME_REGEX)}es.URL_SCHEME_REGEX=/^https?:\/\//;var rs=function(t,e){if("undefined"==typeof fetch&&(null==e||null==e.fetchFunc))return null;return(Array.isArray(t)?t.every((function(t){return ns(t)})):ns(t))?function(t,e){return new es(t,e)}(t,e):null};function is(t,e,n){if(e.rank<1)throw new Error("tf.scatterND() expects the indices to be rank 1 or higher,"+" but the rank was ".concat(e.rank,"."));if(t.rank<1)throw new Error("tf.scatterND() expects the updates to be rank 1 or higher,"+" but the rank was ".concat(t.rank,"."));if("int32"!==e.dtype)throw new Error("The dtype of 'indices' should be int32, but got dtype: ".concat(e.dtype));if(n.length<1)throw new Error("Output rank must be greater or equal to 1, but got shape: ".concat(n));if(0===n.length){if(0===e.size)throw new Error("Indices specified for empty output. indices shape: ".concat(e.shape));if(0===t.size)throw new Error("Updates specified for empty output. updates shape: ".concat(t.shape))}!function(t,e,n){var r=e.rank>1?e.shape[e.rank-1]:1,i=e.rank>1?e.rank-1:1,a="Must have updates.shape = indices.shape[:batchDim] + "+"shape[sliceDim:], got updates.shape: ".concat(n.shape)+", indices.shape: ".concat(e.shape,", shape: ").concat(t)+", sliceDim: ".concat(r,", and batchDim: ").concat(i,".");if(n.rank=1,(function(){return"Pass at least one tensor to concat"}));var n=Zr(t,"tensors","concat","string_or_numeric");if("complex64"===n[0].dtype&&n.forEach((function(t){if("complex64"!==t.dtype)throw new Error("Cannot concatenate complex64 tensors with a tensor\n with dtype ".concat(t.dtype,". "))})),1===n.length)return qi(n[0]);var r=n,i={axis:e};return Vr.runKernel(xe,r,i)}});var us=Yr({sigmoid_:function(t){var e={x:Jr(t,"x","sigmoid","float32")};return Vr.runKernel(qe,e)}});var ls=Yr({batchToSpaceND_:function(t,e,n){var r=Jr(t,"x","batchToSpaceND"),i=e.reduce((function(t,e){return t*e}));nn(r.rank>=1+e.length,(function(){return"input rank is ".concat(r.rank," but should be > than blockShape.length ").concat(e.length)})),nn(n.length===e.length,(function(){return"crops.length is ".concat(n.length," but should be equal to blockShape.length ").concat(e.length)})),nn(r.shape[0]%i==0,(function(){return"input tensor batch is ".concat(r.shape[0]," but is not divisible by the product of ")+"the elements of blockShape ".concat(e.join(" * ")," === ").concat(i)}));var a={x:r},o={blockShape:e,crops:n};return Vr.runKernel(be,a,o)}});var cs=Yr({cos_:function(t){var e={x:Jr(t,"x","cos","float32")};return Vr.runKernel("Cos",e)}});var hs=Yr({cosh_:function(t){var e={x:Jr(t,"x","cosh","float32")};return Vr.runKernel(Ie,e)}});var ps=Yr({cumprod_:function(t,e,n,r){void 0===e&&(e=0),void 0===n&&(n=!1),void 0===r&&(r=!1);var i={x:Jr(t,"x","cumprod")},a={axis:e,exclusive:n,reverse:r};return Vr.runKernel("Cumprod",i,a)}});var fs=Yr({expandDims_:function(t,e){void 0===e&&(e=0);var n=Jr(t,"x","expandDims","string_or_numeric");nn(e<=n.rank,(function(){return"Axis must be <= rank of the tensor"}));var r={input:n},i={dim:e};return Vr.runKernel(Ee,r,i)}});var ds=Yr({gather_:function(t,e,n,r){void 0===n&&(n=0),void 0===r&&(r=0);var i={x:Jr(t,"x","gather"),indices:Jr(e,"indices","gather","int32")},a={axis:n,batchDims:r};return Vr.runKernel(Fe,i,a)}});var gs=Yr({logicalNot_:function(t){var e={x:Jr(t,"x","logicalNot","bool")};return Vr.runKernel("LogicalNot",e)}});var vs=Yr({maximum_:function(t,e){var n,r=Jr(t,"a","maximum"),i=Jr(e,"b","maximum");r=(n=c(Mr(r,i),2))[0],i=n[1],"bool"===r.dtype&&(r=Xr(r,"int32"),i=Xr(i,"int32")),hi(r.shape,i.shape);var a={a:r,b:i};return Vr.runKernel(Le,a)}});var ys=Yr({pad_:function(t,e,n){void 0===n&&(n=0);var r=Jr(t,"x","pad");if(0===r.rank)throw new Error("pad(scalar) is not defined. Pass non-scalar to pad");var i={paddings:e,constantValue:n},a={x:r};return Vr.runKernel(Me,a,i)}}),ms={exports:{}};!function(t){!function(t,e,n){function r(t){var e,n=this,r=(e=4022871197,function(t){t=String(t);for(var n=0;n>>0,e=(r*=e)>>>0,e+=4294967296*(r-=e)}return 2.3283064365386963e-10*(e>>>0)});n.next=function(){var t=2091639*n.s0+2.3283064365386963e-10*n.c;return n.s0=n.s1,n.s1=n.s2,n.s2=t-(n.c=0|t)},n.c=1,n.s0=r(" "),n.s1=r(" "),n.s2=r(" "),n.s0-=r(t),n.s0<0&&(n.s0+=1),n.s1-=r(t),n.s1<0&&(n.s1+=1),n.s2-=r(t),n.s2<0&&(n.s2+=1),r=null}function i(t,e){return e.c=t.c,e.s0=t.s0,e.s1=t.s1,e.s2=t.s2,e}function a(t,e){var n=new r(t),a=e&&e.state,o=n.next;return o.int32=function(){return 4294967296*n.next()|0},o.double=function(){return o()+11102230246251565e-32*(2097152*o()|0)},o.quick=o,a&&("object"==typeof a&&i(a,n),o.state=function(){return i(n,{})}),o}e&&e.exports?e.exports=a:n&&n.amd?n((function(){return a})):this.alea=a}(0,t,!1)}(ms);var bs=ms.exports,ws={exports:{}};!function(t){!function(t,e,n){function r(t){var e=this,n="";e.x=0,e.y=0,e.z=0,e.w=0,e.next=function(){var t=e.x^e.x<<11;return e.x=e.y,e.y=e.z,e.z=e.w,e.w^=e.w>>>19^t^t>>>8},t===(0|t)?e.x=t:n+=t;for(var r=0;r>>0)/4294967296};return o.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},o.int32=n.next,o.quick=o,a&&("object"==typeof a&&i(a,n),o.state=function(){return i(n,{})}),o}e&&e.exports?e.exports=a:n&&n.amd?n((function(){return a})):this.xor128=a}(0,t,!1)}(ws);var ks=ws.exports,xs={exports:{}};!function(t){!function(t,e,n){function r(t){var e=this,n="";e.next=function(){var t=e.x^e.x>>>2;return e.x=e.y,e.y=e.z,e.z=e.w,e.w=e.v,(e.d=e.d+362437|0)+(e.v=e.v^e.v<<4^t^t<<1)|0},e.x=0,e.y=0,e.z=0,e.w=0,e.v=0,t===(0|t)?e.x=t:n+=t;for(var r=0;r>>4),e.next()}function i(t,e){return e.x=t.x,e.y=t.y,e.z=t.z,e.w=t.w,e.v=t.v,e.d=t.d,e}function a(t,e){var n=new r(t),a=e&&e.state,o=function(){return(n.next()>>>0)/4294967296};return o.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},o.int32=n.next,o.quick=o,a&&("object"==typeof a&&i(a,n),o.state=function(){return i(n,{})}),o}e&&e.exports?e.exports=a:n&&n.amd?n((function(){return a})):this.xorwow=a}(0,t,!1)}(xs);var Ss=xs.exports,Ns={exports:{}};!function(t){!function(t,e,n){function r(t){var e=this;e.next=function(){var t,n,r=e.x,i=e.i;return t=r[i],n=(t^=t>>>7)^t<<24,n^=(t=r[i+1&7])^t>>>10,n^=(t=r[i+3&7])^t>>>3,n^=(t=r[i+4&7])^t<<7,t=r[i+7&7],n^=(t^=t<<13)^t<<9,r[i]=n,e.i=i+1&7,n},function(t,e){var n,r=[];if(e===(0|e))r[0]=e;else for(e=""+e,n=0;n0;--n)t.next()}(e,t)}function i(t,e){return e.x=t.x.slice(),e.i=t.i,e}function a(t,e){null==t&&(t=+new Date);var n=new r(t),a=e&&e.state,o=function(){return(n.next()>>>0)/4294967296};return o.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},o.int32=n.next,o.quick=o,a&&(a.x&&i(a,n),o.state=function(){return i(n,{})}),o}e&&e.exports?e.exports=a:n&&n.amd?n((function(){return a})):this.xorshift7=a}(0,t,!1)}(Ns);var Is=Ns.exports,As={exports:{}};!function(t){!function(t,e,n){function r(t){var e=this;e.next=function(){var t,n,r=e.w,i=e.X,a=e.i;return e.w=r=r+1640531527|0,n=i[a+34&127],t=i[a=a+1&127],n^=n<<13,t^=t<<17,n^=n>>>15,t^=t>>>12,n=i[a]=n^t,e.i=a,n+(r^r>>>16)|0},function(t,e){var n,r,i,a,o,s=[],u=128;for(e===(0|e)?(r=e,e=null):(e+="\0",r=0,u=Math.max(u,e.length)),i=0,a=-32;a>>15,r^=r<<4,r^=r>>>13,a>=0&&(o=o+1640531527|0,i=0==(n=s[127&a]^=r+o)?i+1:0);for(i>=128&&(s[127&(e&&e.length||0)]=-1),i=127,a=512;a>0;--a)r=s[i+34&127],n=s[i=i+1&127],r^=r<<13,n^=n<<17,r^=r>>>15,n^=n>>>12,s[i]=r^n;t.w=o,t.X=s,t.i=i}(e,t)}function i(t,e){return e.i=t.i,e.w=t.w,e.X=t.X.slice(),e}function a(t,e){null==t&&(t=+new Date);var n=new r(t),a=e&&e.state,o=function(){return(n.next()>>>0)/4294967296};return o.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},o.int32=n.next,o.quick=o,a&&(a.X&&i(a,n),o.state=function(){return i(n,{})}),o}e&&e.exports?e.exports=a:n&&n.amd?n((function(){return a})):this.xor4096=a}(0,t,!1)}(As);var zs=As.exports,Es={exports:{}};!function(t){!function(t,e,n){function r(t){var e=this,n="";e.next=function(){var t=e.b,n=e.c,r=e.d,i=e.a;return t=t<<25^t>>>7^n,n=n-r|0,r=r<<24^r>>>8^i,i=i-t|0,e.b=t=t<<20^t>>>12^n,e.c=n=n-r|0,e.d=r<<16^n>>>16^i,e.a=i-t|0},e.a=0,e.b=0,e.c=-1640531527,e.d=1367130551,t===Math.floor(t)?(e.a=t/4294967296|0,e.b=0|t):n+=t;for(var r=0;r>>0)/4294967296};return o.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},o.int32=n.next,o.quick=o,a&&("object"==typeof a&&i(a,n),o.state=function(){return i(n,{})}),o}e&&e.exports?e.exports=a:n&&n.amd?n((function(){return a})):this.tychei=a}(0,t,!1)}(Es);var Ts=Es.exports,Cs={exports:{}},Fs=Mn({__proto__:null,default:{}});!function(t){!function(e,n,r){var i,a=256,o=r.pow(a,6),s=r.pow(2,52),u=2*s,l=255;function c(t,l,c){var v=[],y=d(f((l=1==l?{entropy:!0}:l||{}).entropy?[t,g(n)]:null==t?function(){try{var t;return i&&(t=i.randomBytes)?t=t(a):(t=new Uint8Array(a),(e.crypto||e.msCrypto).getRandomValues(t)),g(t)}catch(t){var r=e.navigator,o=r&&r.plugins;return[+new Date,e,o,e.screen,g(n)]}}():t,3),v),m=new h(v),b=function(){for(var t=m.g(6),e=o,n=0;t=u;)t/=2,e/=2,n>>>=1;return(t+n)/e};return b.int32=function(){return 0|m.g(4)},b.quick=function(){return m.g(4)/4294967296},b.double=b,d(g(m.S),n),(l.pass||c||function(t,e,n,i){return i&&(i.S&&p(i,m),t.state=function(){return p(m,{})}),n?(r.random=t,e):t})(b,y,"global"in l?l.global:this==r,l.state)}function h(t){var e,n=t.length,r=this,i=0,o=r.i=r.j=0,s=r.S=[];for(n||(t=[n++]);i0,(function(){return"variableGrads() expects at least one of the input variables to "+"be trainable, but none of the ".concat(a," variables is ")+"trainable."}));var o=Vr.gradients(t,e,null,!0),s=o.value,u=o.grads;nn(u.some((function(t){return null!=t})),(function(){return"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()."})),nn(0===s.rank,(function(){return"The f passed in variableGrads(f) must return a scalar, but it "+"returned a rank-".concat(s.rank," tensor")}));var l={};return e.forEach((function(t,e){null!=u[e]&&(l[t.name]=u[e])})),null!=i&&i.forEach((function(t){return l[t.name]=null})),{value:s,grads:l}}(t,e)},e.prototype.dispose=function(){null!=this.iterations_&&da(this.iterations_)},e.prototype.saveIterations=function(){return s(this,void 0,void 0,(function(){return u(this,(function(t){return null==this.iterations_&&(this.iterations_=0),[2,{name:"iter",tensor:ii(this.iterations_,"int32")}]}))}))},e.prototype.getWeights=function(){return s(this,void 0,void 0,(function(){return u(this,(function(t){throw new Error("getWeights() is not implemented for this optimizer yet.")}))}))},e.prototype.setWeights=function(t){return s(this,void 0,void 0,(function(){return u(this,(function(t){throw new Error("setWeights() is not implemented for this optimizer class "+"".concat(this.getClassName()))}))}))},e.prototype.extractIterations=function(t){return s(this,void 0,void 0,(function(){var e;return u(this,(function(n){switch(n.label){case 0:return e=this,[4,t[0].tensor.data()];case 1:return e.iterations_=n.sent()[0],[2,t.slice(1)]}}))}))},e}(as);Object.defineProperty(Us,Symbol.hasInstance,{value:function(t){return null!=t.minimize&&null!=t.computeGradients&&null!=t.applyGradients}}),function(t){function e(e,n,r){void 0===r&&(r=null);var i=t.call(this)||this;return i.learningRate=e,i.rho=n,i.epsilon=r,i.accumulatedGrads=[],i.accumulatedUpdates=[],null==r&&(i.epsilon=Vr.backend.epsilon()),i}o(e,t),Object.defineProperty(e,"className",{get:function(){return"Adadelta"},enumerable:!1,configurable:!0}),e.prototype.applyGradients=function(t){var e=this;(Array.isArray(t)?t.map((function(t){return t.name})):Object.keys(t)).forEach((function(n,r){var i=Vr.registeredVariables[n];null==e.accumulatedGrads[r]&&(e.accumulatedGrads[r]={originalName:"".concat(n,"/accum_grad"),variable:fa((function(){return vi(i).variable(false)}))}),null==e.accumulatedUpdates[r]&&(e.accumulatedUpdates[r]={originalName:"".concat(n,"/accum_var"),variable:fa((function(){return vi(i).variable(false)}))});var a=Array.isArray(t)?t[r].tensor:t[n];if(null!=a){var o=e.accumulatedGrads[r].variable,s=e.accumulatedUpdates[r].variable;fa((function(){var t=wi($r(o,e.rho),$r(oi(a),1-e.rho)),n=$r(ni(ai(wi(s,e.epsilon)),ai(wi(o,e.epsilon))),a),r=wi($r(s,e.rho),$r(oi(n),1-e.rho));o.assign(t),s.assign(r);var u=wi($r(n,-e.learningRate),i);i.assign(u)}))}})),this.incrementIterations()},e.prototype.dispose=function(){null!=this.accumulatedUpdates&&(da(this.accumulatedGrads.map((function(t){return t.variable}))),da(this.accumulatedUpdates.map((function(t){return t.variable}))))},e.prototype.getWeights=function(){return s(this,void 0,void 0,(function(){var t;return u(this,(function(e){switch(e.label){case 0:return t=h(h([],c(this.accumulatedGrads),!1),c(this.accumulatedUpdates),!1),[4,this.saveIterations()];case 1:return[2,[e.sent()].concat(t.map((function(t){return{name:t.originalName,tensor:t.variable}})))]}}))}))},e.prototype.setWeights=function(t){return s(this,void 0,void 0,(function(){var e;return u(this,(function(n){switch(n.label){case 0:return[4,this.extractIterations(t)];case 1:return t=n.sent(),e=t.length/2,!1,this.accumulatedGrads=t.slice(0,e).map((function(t){return{originalName:t.name,variable:t.tensor.variable(false)}})),this.accumulatedUpdates=t.slice(e,2*e).map((function(t){return{originalName:t.name,variable:t.tensor.variable(false)}})),[2]}}))}))},e.prototype.getConfig=function(){return{learningRate:this.learningRate,rho:this.rho,epsilon:this.epsilon}},e.fromConfig=function(t,e){return new t(e.learningRate,e.rho,e.epsilon)}}(Us),function(t){function e(e,n){void 0===n&&(n=.1);var r=t.call(this)||this;return r.learningRate=e,r.initialAccumulatorValue=n,r.accumulatedGrads=[],r}o(e,t),Object.defineProperty(e,"className",{get:function(){return"Adagrad"},enumerable:!1,configurable:!0}),e.prototype.applyGradients=function(t){var e=this;(Array.isArray(t)?t.map((function(t){return t.name})):Object.keys(t)).forEach((function(n,r){var i=Vr.registeredVariables[n];if(null==e.accumulatedGrads[r]){e.accumulatedGrads[r]={originalName:"".concat(n,"/accumulator"),variable:fa((function(){return function(t,e,n){mn(t);var r={shape:t,value:e,dtype:n=n||hn(e)};return Vr.runKernel("Fill",{},r)}(i.shape,e.initialAccumulatorValue).variable(false)}))}}var a=Array.isArray(t)?t[r].tensor:t[n];if(null!=a){var o=e.accumulatedGrads[r].variable;fa((function(){var t=wi(o,oi(a));o.assign(t);var n=wi($r(ni(a,ai(wi(t,Vr.backend.epsilon()))),-e.learningRate),i);i.assign(n)}))}})),this.incrementIterations()},e.prototype.dispose=function(){null!=this.accumulatedGrads&&da(this.accumulatedGrads.map((function(t){return t.variable})))},e.prototype.getWeights=function(){return s(this,void 0,void 0,(function(){return u(this,(function(t){switch(t.label){case 0:return[4,this.saveIterations()];case 1:return[2,[t.sent()].concat(this.accumulatedGrads.map((function(t){return{name:t.originalName,tensor:t.variable}})))]}}))}))},e.prototype.setWeights=function(t){return s(this,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.extractIterations(t)];case 1:return t=e.sent(),!1,this.accumulatedGrads=t.map((function(t){return{originalName:t.name,variable:t.tensor.variable(false)}})),[2]}}))}))},e.prototype.getConfig=function(){return{learningRate:this.learningRate,initialAccumulatorValue:this.initialAccumulatorValue}},e.fromConfig=function(t,e){return new t(e.learningRate,e.initialAccumulatorValue)}}(Us),function(t){function e(e,n,r,i){void 0===i&&(i=null);var a=t.call(this)||this;return a.learningRate=e,a.beta1=n,a.beta2=r,a.epsilon=i,a.accumulatedFirstMoment=[],a.accumulatedSecondMoment=[],fa((function(){a.accBeta1=ii(n).variable(),a.accBeta2=ii(r).variable()})),null==i&&(a.epsilon=Vr.backend.epsilon()),a}o(e,t),Object.defineProperty(e,"className",{get:function(){return"Adam"},enumerable:!1,configurable:!0}),e.prototype.applyGradients=function(t){var e=this,n=Array.isArray(t)?t.map((function(t){return t.name})):Object.keys(t);fa((function(){var r=si(1,e.accBeta1),i=si(1,e.accBeta2);n.forEach((function(n,a){var o=Vr.registeredVariables[n];null==e.accumulatedFirstMoment[a]&&(e.accumulatedFirstMoment[a]={originalName:"".concat(n,"/m"),variable:fa((function(){return vi(o).variable(false)}))}),null==e.accumulatedSecondMoment[a]&&(e.accumulatedSecondMoment[a]={originalName:"".concat(n,"/v"),variable:fa((function(){return vi(o).variable(false)}))});var s=Array.isArray(t)?t[a].tensor:t[n];if(null!=s){var u=e.accumulatedFirstMoment[a].variable,l=e.accumulatedSecondMoment[a].variable,c=wi($r(u,e.beta1),$r(s,1-e.beta1)),h=wi($r(l,e.beta2),$r(oi(s),1-e.beta2)),p=ni(c,r),f=ni(h,i);u.assign(c),l.assign(h);var d=wi($r(ni(p,wi(ai(f),e.epsilon)),-e.learningRate),o);o.assign(d)}})),e.accBeta1.assign($r(e.accBeta1,e.beta1)),e.accBeta2.assign($r(e.accBeta2,e.beta2))})),this.incrementIterations()},e.prototype.dispose=function(){this.accBeta1.dispose(),this.accBeta2.dispose(),null!=this.accumulatedFirstMoment&&da(this.accumulatedFirstMoment.map((function(t){return t.variable}))),null!=this.accumulatedSecondMoment&&da(this.accumulatedSecondMoment.map((function(t){return t.variable})))},e.prototype.getWeights=function(){return s(this,void 0,void 0,(function(){var t;return u(this,(function(e){switch(e.label){case 0:return t=h(h([],c(this.accumulatedFirstMoment),!1),c(this.accumulatedSecondMoment),!1),[4,this.saveIterations()];case 1:return[2,[e.sent()].concat(t.map((function(t){return{name:t.originalName,tensor:t.variable}})))]}}))}))},e.prototype.setWeights=function(t){return s(this,void 0,void 0,(function(){var e,n=this;return u(this,(function(r){switch(r.label){case 0:return[4,this.extractIterations(t)];case 1:return t=r.sent(),fa((function(){n.accBeta1.assign(Io(n.beta1,n.iterations_+1)),n.accBeta2.assign(Io(n.beta2,n.iterations_+1))})),e=t.length/2,!1,this.accumulatedFirstMoment=t.slice(0,e).map((function(t){return{originalName:t.name,variable:t.tensor.variable(false)}})),this.accumulatedSecondMoment=t.slice(e,2*e).map((function(t){return{originalName:t.name,variable:t.tensor.variable(false)}})),[2]}}))}))},e.prototype.getConfig=function(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon}},e.fromConfig=function(t,e){return new t(e.learningRate,e.beta1,e.beta2,e.epsilon)}}(Us),function(t){function e(e,n,r,i,a){void 0===i&&(i=null),void 0===a&&(a=0);var o=t.call(this)||this;return o.learningRate=e,o.beta1=n,o.beta2=r,o.epsilon=i,o.decay=a,o.accumulatedFirstMoment=[],o.accumulatedWeightedInfNorm=[],fa((function(){o.iteration=ii(0).variable(),o.accBeta1=ii(n).variable()})),null==i&&(o.epsilon=Vr.backend.epsilon()),o}o(e,t),Object.defineProperty(e,"className",{get:function(){return"Adamax"},enumerable:!1,configurable:!0}),e.prototype.applyGradients=function(t){var e=this,n=Array.isArray(t)?t.map((function(t){return t.name})):Object.keys(t);fa((function(){var r=si(1,e.accBeta1),i=ni(-e.learningRate,wi($r(e.iteration,e.decay),1));n.forEach((function(n,a){var o=Vr.registeredVariables[n];null==e.accumulatedFirstMoment[a]&&(e.accumulatedFirstMoment[a]={originalName:"".concat(n,"/m"),variable:vi(o).variable(false)}),null==e.accumulatedWeightedInfNorm[a]&&(e.accumulatedWeightedInfNorm[a]={originalName:"".concat(n,"/v"),variable:vi(o).variable(false)});var s=Array.isArray(t)?t[a].tensor:t[n];if(null!=s){var u=e.accumulatedFirstMoment[a].variable,l=e.accumulatedWeightedInfNorm[a].variable,c=wi($r(u,e.beta1),$r(s,1-e.beta1)),h=$r(l,e.beta2),p=os(s),f=vs(h,p);u.assign(c),l.assign(f);var d=wi($r(ni(i,r),ni(c,wi(f,e.epsilon))),o);o.assign(d)}})),e.iteration.assign(wi(e.iteration,1)),e.accBeta1.assign($r(e.accBeta1,e.beta1))})),this.incrementIterations()},e.prototype.dispose=function(){this.accBeta1.dispose(),this.iteration.dispose(),null!=this.accumulatedFirstMoment&&da(this.accumulatedFirstMoment.map((function(t){return t.variable}))),null!=this.accumulatedWeightedInfNorm&&da(this.accumulatedWeightedInfNorm.map((function(t){return t.variable})))},e.prototype.getWeights=function(){return s(this,void 0,void 0,(function(){return u(this,(function(t){throw new Error("getWeights() is not implemented for Adamax yet.")}))}))},e.prototype.setWeights=function(t){return s(this,void 0,void 0,(function(){return u(this,(function(t){throw new Error("setWeights() is not implemented for Adamax yet.")}))}))},e.prototype.getConfig=function(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon,decay:this.decay}},e.fromConfig=function(t,e){return new t(e.learningRate,e.beta1,e.beta2,e.epsilon,e.decay)}}(Us);var Ws,Vs=function(t){function e(e){var n=t.call(this)||this;return n.learningRate=e,n.setLearningRate(e),n}return o(e,t),Object.defineProperty(e,"className",{get:function(){return"SGD"},enumerable:!1,configurable:!0}),e.prototype.applyGradients=function(t){var e=this;(Array.isArray(t)?t.map((function(t){return t.name})):Object.keys(t)).forEach((function(n,r){var i=Array.isArray(t)?t[r].tensor:t[n];if(null!=i){var a=Vr.registeredVariables[n];fa((function(){var t=wi($r(e.c,i),a);a.assign(t)}))}})),this.incrementIterations()},e.prototype.setLearningRate=function(t){var e;this.learningRate=t,null!=this.c&&this.c.dispose(),this.c=(e=ii(-t),Vr.keep(e))},e.prototype.dispose=function(){this.c.dispose()},e.prototype.getWeights=function(){return s(this,void 0,void 0,(function(){return u(this,(function(t){switch(t.label){case 0:return[4,this.saveIterations()];case 1:return[2,[t.sent()]]}}))}))},e.prototype.setWeights=function(t){return s(this,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.extractIterations(t)];case 1:if(0!==(t=e.sent()).length)throw new Error("SGD optimizer does not have settable weights.");return[2]}}))}))},e.prototype.getConfig=function(){return{learningRate:this.learningRate}},e.fromConfig=function(t,e){return new t(e.learningRate)},e}(Us);!function(t){function e(e,n,r){void 0===r&&(r=!1);var i=t.call(this,e)||this;return i.learningRate=e,i.momentum=n,i.useNesterov=r,i.accumulations=[],i.m=ii(i.momentum),i}o(e,t),Object.defineProperty(e,"className",{get:function(){return"Momentum"},enumerable:!1,configurable:!0}),e.prototype.applyGradients=function(t){var e=this;(Array.isArray(t)?t.map((function(t){return t.name})):Object.keys(t)).forEach((function(n,r){var i=Vr.registeredVariables[n];if(null==e.accumulations[r]){e.accumulations[r]={originalName:"".concat(n,"/momentum"),variable:fa((function(){return vi(i).variable(false)}))}}var a=e.accumulations[r].variable,o=Array.isArray(t)?t[r].tensor:t[n];null!=o&&fa((function(){var t,n=wi($r(e.m,a),o);t=e.useNesterov?wi($r(e.c,wi(o,$r(n,e.m))),i):wi($r(e.c,n),i),a.assign(n),i.assign(t)}))})),this.incrementIterations()},e.prototype.dispose=function(){this.m.dispose(),null!=this.accumulations&&da(this.accumulations.map((function(t){return t.variable})))},e.prototype.setMomentum=function(t){this.momentum=t},e.prototype.getWeights=function(){return s(this,void 0,void 0,(function(){return u(this,(function(t){switch(t.label){case 0:return[4,this.saveIterations()];case 1:return[2,[t.sent()].concat(this.accumulations.map((function(t){return{name:t.originalName,tensor:t.variable}})))]}}))}))},e.prototype.setWeights=function(t){return s(this,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return[4,this.extractIterations(t)];case 1:return t=e.sent(),!1,this.accumulations=t.map((function(t){return{originalName:t.name,variable:t.tensor.variable(false)}})),[2]}}))}))},e.prototype.getConfig=function(){return{learningRate:this.learningRate,momentum:this.momentum,useNesterov:this.useNesterov}},e.fromConfig=function(t,e){return new t(e.learningRate,e.momentum,e.useNesterov)}}(Vs),function(t){function e(e,n,r,i,a){void 0===n&&(n=.9),void 0===r&&(r=0),void 0===i&&(i=null),void 0===a&&(a=!1);var o=t.call(this)||this;if(o.learningRate=e,o.decay=n,o.momentum=r,o.epsilon=i,o.accumulatedMeanSquares=[],o.accumulatedMoments=[],o.accumulatedMeanGrads=[],o.centered=a,null==i&&(o.epsilon=Vr.backend.epsilon()),null==e)throw new Error("learningRate for RMSPropOptimizer must be defined.");return o}o(e,t),Object.defineProperty(e,"className",{get:function(){return"RMSProp"},enumerable:!1,configurable:!0}),e.prototype.applyGradients=function(t){var e=this;(Array.isArray(t)?t.map((function(t){return t.name})):Object.keys(t)).forEach((function(n,r){var i=Vr.registeredVariables[n],a=!1;null==e.accumulatedMeanSquares[r]&&(e.accumulatedMeanSquares[r]={originalName:"".concat(n,"/rms"),variable:fa((function(){return vi(i).variable(a)}))}),null==e.accumulatedMoments[r]&&(e.accumulatedMoments[r]={originalName:"".concat(n,"/momentum"),variable:fa((function(){return vi(i).variable(a)}))}),null==e.accumulatedMeanGrads[r]&&e.centered&&(e.accumulatedMeanGrads[r]={originalName:"".concat(n,"/mg"),variable:fa((function(){return vi(i).variable(a)}))});var o=Array.isArray(t)?t[r].tensor:t[n];if(null!=o){var s=e.accumulatedMeanSquares[r].variable,u=e.accumulatedMoments[r].variable;fa((function(){var t=wi($r(s,e.decay),$r(oi(o),1-e.decay));if(e.centered){var n=e.accumulatedMeanGrads[r].variable,a=wi($r(n,e.decay),$r(o,1-e.decay)),l=ni($r(o,e.learningRate),ai(si(t,wi(oi(a),e.epsilon)))),c=wi($r(u,e.momentum),l);s.assign(t),n.assign(a),u.assign(c);var h=si(i,c);i.assign(h)}else{var p=wi($r(s,e.decay),$r(oi(o),1-e.decay));c=wi($r(u,e.momentum),ni($r(o,e.learningRate),ai(wi(p,e.epsilon))));s.assign(p),u.assign(c);h=si(i,c);i.assign(h)}}))}})),this.incrementIterations()},e.prototype.dispose=function(){null!=this.accumulatedMeanSquares&&da(this.accumulatedMeanSquares.map((function(t){return t.variable}))),null!=this.accumulatedMeanGrads&&this.centered&&da(this.accumulatedMeanGrads.map((function(t){return t.variable}))),null!=this.accumulatedMoments&&da(this.accumulatedMoments.map((function(t){return t.variable})))},e.prototype.getWeights=function(){return s(this,void 0,void 0,(function(){var t;return u(this,(function(e){switch(e.label){case 0:return t=h(h([],c(this.accumulatedMeanSquares),!1),c(this.accumulatedMoments),!1),this.centered&&t.push.apply(t,h([],c(this.accumulatedMeanGrads),!1)),[4,this.saveIterations()];case 1:return[2,[e.sent()].concat(t.map((function(t){return{name:t.originalName,tensor:t.variable}})))]}}))}))},e.prototype.setWeights=function(t){return s(this,void 0,void 0,(function(){var e,n;return u(this,(function(r){switch(r.label){case 0:return[4,this.extractIterations(t)];case 1:return t=r.sent(),e=this.centered?t.length/3:t.length/2,n=!1,this.accumulatedMeanSquares=t.slice(0,e).map((function(t){return{originalName:t.name,variable:t.tensor.variable(n)}})),this.accumulatedMoments=t.slice(e,2*e).map((function(t){return{originalName:t.name,variable:t.tensor.variable(n)}})),this.centered&&(this.accumulatedMeanGrads=t.slice(2*e,3*e).map((function(t){return{originalName:t.name,variable:t.tensor.variable(n)}}))),[2]}}))}))},e.prototype.getConfig=function(){return{learningRate:this.learningRate,decay:this.decay,momentum:this.momentum,epsilon:this.epsilon,centered:this.centered}},e.fromConfig=function(t,e){return new t(e.learningRate,e.decay,e.momentum,e.epsilon,e.centered)}}(Us),"undefined"!=typeof requestAnimationFrame?requestAnimationFrame:"undefined"!=typeof setImmediate&&setImmediate,function(t){t[t.FIRST_DIM_SIZE=0]="FIRST_DIM_SIZE",t[t.VALUE_ROWIDS=1]="VALUE_ROWIDS",t[t.ROW_LENGTHS=2]="ROW_LENGTHS",t[t.ROW_SPLITS=3]="ROW_SPLITS",t[t.ROW_LIMITS=4]="ROW_LIMITS",t[t.ROW_STARTS=5]="ROW_STARTS"}(Ws||(Ws={}));function qs(t,e,n){var r=t.shape.length,i=r-n.length,a=ca(n,r),o=t;null!=a&&(o=ma(t,a));var s=o.shape.slice(),u=s.splice(r-n.length,n.length).reduce((function(t,e){return t*e}),1);s.push(u);var l=function(t,e,n){var r=t.shape.slice();r[n]=1;var i=pi(e,r),a=ps(t,n,!0,!1),o=ps(t,n,!0,!0),s=$r(a,o);return $r(i,s)}(o.reshape(s),e,i);if(l=l.reshape(o.shape),null!=a){var c=ha(a);l=ma(l,c)}return l}var Gs={kernelName:ze,inputsToSave:["a","b"],gradFunc:function(t,e){var n=c(e,2),r=n[0],i=n[1],a=hi(r.shape,i.shape);return{a:function(){var e=ni(t,Xr(i,"float32")),n=ci(r.shape,a);return n.length>0?pi(fi(e,n),r.shape):e},b:function(){var e=$r(t,Xr(r,"float32")),n=ci(i.shape,a);n.length>0&&(e=pi(fi(e,n),i.shape));var o=oi(i);return ri(ni(e,Xr(o,"float32")))}}}},Ks={kernelName:Ke,gradFunc:function(t,e,n){var r=n.blockShape,i=n.paddings;return{x:function(){return ls(t,r,i)}}}},Hs={kernelName:He,gradFunc:function(t,e,n){var r=n.axis;return{x:function(){return ss(t,r)}}}},Js={kernelName:"SquaredDifference",inputsToSave:["a","b"],gradFunc:function(t,e){var n=c(e,2),r=n[0],i=n[1],a=ii(2);return{a:function(){return $r(t,$r(a,si(r,i)))},b:function(){return $r(t,$r(a,si(i,r)))}}}},Zs={kernelName:"Sub",inputsToSave:["a","b"],gradFunc:function(t,e){var n=c(e,2),r=n[0],i=n[1],a=hi(r.shape,i.shape);return{a:function(){var e=t,n=ci(r.shape,a);return n.length>0&&(e=fi(e,n)),pi(e,r.shape)},b:function(){var e=t,n=ci(i.shape,a);return n.length>0&&(e=fi(e,n)),pi(ri(e),i.shape)}}}};var Ys,Xs,$s,Qs,tu=[ti,ui,li,di,gi,yi,mi,bi,ki,xi,Si,Ni,Fi,_i,Ri,Mi,Bi,Pi,ji,Hi,Ji,Yi,ea,Qi,ia,oa,ua,ba,xa,Sa,Gs,Na,Aa,za,Ea,Ta,Fa,Ca,La,Ma,ja,Ua,Wa,Va,qa,Ka,Ha,Ja,Za,Xa,to,to,no,io,oo,lo,co,ho,fo,vo,yo,mo,bo,wo,xo,So,So,Ao,zo,{kernelName:"Prod",inputsToSave:["x"],gradFunc:function(t,e,n){var r=c(e,1)[0],i=n.axis,a=[];return a=null==i?r.shape.map((function(t,e){return e})):"number"==typeof i?[i]:i,{x:function(){return qs(r,t,a)}}}},{kernelName:"Reciprocal",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return ni(t,ri(oi(n)))}}}},{kernelName:"Relu6",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0],r=$r(Wi(n,6),Qr(n));return{x:function(){return $r(t,Xr(r,"float32"))}}}},{kernelName:"Relu",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return $r(t,Xr(Qr(n),"float32"))}}}},{kernelName:Be,inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return pi(t,n.shape)}}}},{kernelName:"ResizeBilinear",inputsToSave:["images"],gradFunc:function(t,e,n){var r={dy:t,images:c(e,1)[0]};return{images:function(){return Vr.runKernel("ResizeBilinearGrad",r,n)}}}},{kernelName:"ResizeNearestNeighbor",inputsToSave:["images"],gradFunc:function(t,e,n){var r={dy:t,images:c(e,1)[0]};return{images:function(){return Vr.runKernel("ResizeNearestNeighborGrad",r,n)}}}},{kernelName:Pe,gradFunc:function(t,e,n){var r=un(n.dims,t.shape);return{x:function(){return js(t,r)}}}},{kernelName:"Round",gradFunc:function(t){return{x:function(){return vi(t)}}}},{kernelName:je,inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return ri(ni(t,$r(Io(n,1.5),2)))}}}},{kernelName:Ue,inputsToSave:["condition"],gradFunc:function(t,e){var n=c(e,1)[0];return{condition:function(){return Xr(vi(n),"float32")},t:function(){return $r(t,Xr(n,t.dtype))},e:function(){return $r(t,Xr(gs(n),t.dtype))}}}},{kernelName:"Selu",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){var e=Ga(n,ii(0)),r=ii(1.7580993408473768),i=ii(1.0507009873554805),a=$r(t,i),o=$r($r(t,r),Ia(Xr(n,"float32")));return Ki(e,a,o)}}}},{kernelName:qe,outputsToSave:[!0],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return $r(t,$r(n,si(ii(1),n)))}}}},{kernelName:"Sign",gradFunc:function(t){return{x:function(){return vi(t)}}}},{kernelName:"Sin",inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return $r(cs(Xr(n,"float32")),t)}}}},{kernelName:Ve,inputsToSave:["x"],gradFunc:function(t,e){var n=c(e,1)[0];return{x:function(){return $r(hs(Xr(n,"float32")),t)}}}},{kernelName:We,inputsToSave:["x"],gradFunc:function(t,e,n){for(var r=c(e,1)[0],i=n.begin,a=n.size,o=r.shape,s=c(function(t,e,n){var r,i,a=t.shape.length;return(r="number"==typeof e?h([e],c(new Array(a-1).fill(0)),!1):e.length=0?e:(nn(-1===e,(function(){return"Negative size values should be exactly -1 but got "+"".concat(e," for the slice() size at index ").concat(n,".")})),t.shape[n]-r[n])})),[r,i]}(r,i,a),2),u=s[0],l=s[1],p=[],f=0;f0?[4,Promise.all(n)]:[3,2];case 1:for(l=u.sent(),c=0;c=0&&Number.isInteger(n),(function(){return"Verbosity level is expected to be an integer >= 0, "+"but got ".concat(n)})),t.checkForDuplicate(r),null==t.constructors[n]&&(t.constructors[n]=[]),t.constructors[n].push(r)},t.checkForDuplicate=function(e){for(var n in t.constructors){t.constructors[+n].forEach((function(t){if(t===e)throw new d("Duplicate callback constructor.")}))}},t.clear=function(){t.constructors={}},t.createCallbacks=function(e){var n=[];for(var r in t.constructors){var i=+r;e>=i&&n.push.apply(n,h([],c(t.constructors[i]),!1))}return n.map((function(t){return new t}))},t}();function Au(t,e,n,r,i,a,o,s,u){var l=new xu,p=h([new ku],c(Iu.createCallbacks(e)),!1);null!=t&&p.push.apply(p,h([],c(t),!1)),p.push(l);var f=new wu(p);return f.setParams({epochs:n,initialEpoch:r,samples:i,steps:a,batchSize:o,verbose:e,doValidation:s,metrics:u}),{callbackList:f,history:l}}function zu(t,n,r){return void 0===n&&(n={}),void 0===r&&(r=!1),E(t,e.serialization.SerializationMap.getMap().classNameMap,n,"layer",r)}function Eu(t,n){return e.tidy((function(){"float32"!==t.dtype&&(t=i.cast(t,"float32"));var e=i.sum(bt(t),n,!0),r=i.fill(e.shape,st()),a=i.sqrt(i.maximum(e,r));return i.div(t,a)}))}function Tu(t,n){return e.tidy((function(){return i.mean(bt(i.sub(n,t)),-1)}))}function Cu(t,n){return e.tidy((function(){return i.mean(i.abs(i.sub(n,t)),-1)}))}function Fu(t,n){return e.tidy((function(){var e=i.sub(t,n),r=i.clipByValue(i.abs(t),st(),Number.MAX_VALUE),a=i.abs(i.div(e,r));return i.mul(100,i.mean(a,-1))}))}function Du(t,n,r){return void 0===r&&(r=!1),e.tidy((function(){if(r)n=i.softmax(n);else{var e=i.sum(n,n.shape.length-1,!0);n=i.div(n,e)}return n=i.clipByValue(n,st(),1-st()),i.neg(i.sum(i.mul(i.cast(t,"float32"),i.log(n)),n.shape.length-1))}))}function _u(t,n,r){return void 0===r&&(r=!1),e.tidy((function(){var e,a,o=i.cast(i.floor((a=[rt((e=t).shape)],i.reshape(e,a))),"int32"),s=(n=i.clipByValue(n,st(),1-st())).shape;return Du(i.reshape(i.oneHot(o,s[s.length-1]),s),n,r)}))}function Lu(t,n){return e.tidy((function(){var r;return r=i.clipByValue(n,st(),1-st()),r=i.log(i.div(r,i.sub(1,r))),i.mean(function(t,n){if(!e.util.arraysEqual(t.shape,n.shape))throw new d("logits and labels must have the same shape, but got shapes "+"".concat(JSON.stringify(t.shape)," and ").concat(JSON.stringify(n.shape)));return e.tidy((function(){var e=i.relu(n),r=i.neg(i.abs(n));return i.add(i.sub(e,i.mul(n,t)),i.log1p(i.exp(r)))}))}(t,r),-1)}))}function Ru(t,n){return e.tidy((function(){var e=Eu(t,-1),r=Eu(n,-1),a=i.mul(e,r);return i.neg(i.sum(a,-1))}))}Iu.constructors={};var Ou={meanSquaredError:Tu,meanAbsoluteError:Cu,meanAbsolutePercentageError:Fu,meanSquaredLogarithmicError:function(t,n){return e.tidy((function(){var e=i.clipByValue(n,st(),Number.MAX_VALUE),r=i.log(i.add(1,e)),a=i.clipByValue(t,st(),Number.MAX_VALUE),o=i.log(i.add(1,a));return i.mean(bt(i.sub(r,o)),-1)}))},squaredHinge:function(t,n){return e.tidy((function(){var e=i.maximum(0,i.sub(1,i.mul(t,n)));return i.mean(bt(e),-1)}))},hinge:function(t,n){return e.tidy((function(){var e=i.maximum(0,i.sub(1,i.mul(t,n)));return i.mean(e,-1)}))},categoricalHinge:function(t,n){return e.tidy((function(){var e=i.sum(i.mul(t,n),-1),r=i.max(i.mul(i.sub(1,t),n),-1);return i.maximum(0,i.add(1,i.sub(r,e)))}))},logcosh:function(t,n){return e.tidy((function(){var e=Math.log(2),r=i.sub(n,t),a=i.sub(i.add(r,i.softplus(i.mul(-2,r))),e);return i.mean(a,-1)}))},categoricalCrossentropy:Du,sparseCategoricalCrossentropy:_u,binaryCrossentropy:Lu,kullbackLeiblerDivergence:function(t,n){return e.tidy((function(){var e=i.clipByValue(t,st(),1),r=i.clipByValue(n,st(),1);return i.sum(i.mul(t,i.log(i.div(e,r))),-1)}))},poisson:function(t,n){return e.tidy((function(){var e=i.log(i.add(st(),n));return i.mean(i.sub(n,i.mul(t,e)),-1)}))},cosineProximity:Ru};function Mu(t){if("string"==typeof t){if(t in Ou)return Ou[t];var e="Unknown loss ".concat(t);throw t.toLowerCase().includes("softmaxcrossentropy")&&(e="Unknown loss ".concat(t,". ")+'Use "categoricalCrossentropy" as the string name for tf.losses.softmaxCrossEntropy'),new d(e)}return t}function Bu(t,n){return e.tidy((function(){var e=i.mul(.5,i.onesLike(n)),r=ut(i.greater(n,e),t.dtype);return i.mean(i.equal(t,r),-1)}))}function Pu(t,n){return e.tidy((function(){return ut(i.equal(i.argMax(t,-1),i.argMax(n,-1)),"float32")}))}function ju(t,n){return e.tidy((function(){return i.cast(i.sum(i.logicalAnd(i.equal(t,1),i.equal(n,1))),"float32")}))}function Uu(t,n){return e.tidy((function(){var r=ju(t,n),a=function(t,n){return e.tidy((function(){return i.cast(i.sum(i.logicalAnd(i.equal(t,0),i.equal(n,1))),"float32")}))}(t,n),o=i.add(r,a);return i.cast(i.where(i.greater(o,0),i.div(r,o),0),"float32")}))}function Wu(t,n){return e.tidy((function(){var r=ju(t,n),a=function(t,n){return e.tidy((function(){return i.cast(i.sum(i.logicalAnd(i.equal(t,1),i.equal(n,0))),"float32")}))}(t,n),o=i.add(r,a);return i.cast(i.where(i.greater(o,0),i.div(r,o),0),"float32")}))}function Vu(t,e){return Lu(t,e)}function qu(t,e){return t.rank===e.rank&&(t=i.squeeze(t,[t.rank-1])),(e=i.argMax(e,-1)).dtype!==t.dtype&&(e=i.cast(e,t.dtype)),i.cast(i.equal(t,e),"float32")}var Gu=Du,Ku=_u,Hu={binaryAccuracy:Bu,categoricalAccuracy:Pu,precision:Uu,categoricalCrossentropy:Gu,sparseCategoricalCrossentropy:Ku,mse:Tu,MSE:Tu,mae:Cu,MAE:Cu,mape:Fu,MAPE:Fu,cosine:Ru};function Ju(t){var e,n,r,i;if(b(null!==t,"Unknown LossOrMetricFn ".concat(t)),"string"==typeof t)return t;var a=void 0;try{for(var o=l(Object.keys(Ou)),s=o.next();!s.done;s=o.next()){var u=s.value;if(Ou[u]===t){a=u;break}}}catch(t){e={error:t}}finally{try{s&&!s.done&&(n=o.return)&&n.call(o)}finally{if(e)throw e.error}}if(void 0!==a)return a;try{for(var c=l(Object.keys(Hu)),h=c.next();!h.done;h=c.next()){u=h.value;if(Hu[u]===t){a=u;break}}}catch(t){r={error:t}}finally{try{h&&!h.done&&(i=c.return)&&i.call(c)}finally{if(r)throw r.error}}return void 0!==a?a:t.name}var Zu=1048576;function Yu(t,e,n){if(void 0===n&&(n=!1),null==t||"object"!=typeof t||Object.getPrototypeOf(t)!==Object.prototype||!Xu(t))throw new Error("User-defined metadata is expected to be a JSON object, but is not.");if(n){var r=JSON.stringify(t);r.length>Zu&&console.warn('User-defined metadata of model "'.concat(e,'" is too large in ')+"size (length=".concat(r.length," when serialized). It is not ")+"recommended to store such large objects in user-defined metadata. Please make sure its serialized length is <= "+"".concat(Zu,"."))}}function Xu(t){var e,n,r,i;if(null===t)return!0;if("object"==typeof t){if(Object.getPrototypeOf(t)===Object.prototype){var a=Object.keys(t);try{for(var o=l(a),s=o.next();!s.done;s=o.next()){var u=s.value;if("string"!=typeof u)return!1;if(!Xu(t[u]))return!1}}catch(t){e={error:t}}finally{try{s&&!s.done&&(n=o.return)&&n.call(o)}finally{if(e)throw e.error}}return!0}if(Array.isArray(t)){try{for(var c=l(t),h=c.next();!h.done;h=c.next()){if(!Xu(h.value))return!1}}catch(t){r={error:t}}finally{try{h&&!h.done&&(i=c.return)&&i.call(c)}finally{if(r)throw r.error}}return!0}return!1}var p=typeof t;return"string"===p||"number"===p||"boolean"===p}function $u(t,e,n,r){void 0===r&&(r=console.log);var i,a=function(t){var e,n,r,i,a,o,s=!0,u=[],p=[];for(var f in t.nodesByDepth)u.push(t.nodesByDepth[f]);try{for(var d=l(u),g=d.next();!g.done;g=d.next()){var v=g.value;if(v.length>1||1===v.length&&v[0].inboundLayers.length>1){s=!1;break}p.push.apply(p,h([],c(v),!1))}}catch(t){e={error:t}}finally{try{g&&!g.done&&(n=d.return)&&n.call(d)}finally{if(e)throw e.error}}if(s)try{for(var y=l(t.layers),m=y.next();!m.done;m=y.next()){var b=m.value,w=!1;try{for(var k=(a=void 0,l(b.inboundNodes)),x=k.next();!x.done;x=k.next()){var S=x.value;if(-1!==p.indexOf(S)){if(w){s=!1;break}w=!0}}}catch(t){a={error:t}}finally{try{x&&!x.done&&(o=k.return)&&o.call(k)}finally{if(a)throw a.error}}if(!s)break}}catch(t){r={error:t}}finally{try{m&&!m.done&&(i=y.return)&&i.call(y)}finally{if(r)throw r.error}}return s}(t),o=["Layer (type)","Input Shape","Output shape","Param #"];if(a?(e=e||90,n=n||[.32,.61,.89,1]):(e=e||115,n=n||[.24,.48,.7,.8,1]),n[n.length-1]<=1&&(n=n.map((function(t){return Math.floor(e*t)}))),!a)for(var s in o.push("Receives inputs"),i=[],t.nodesByDepth)i.push.apply(i,h([],c(t.nodesByDepth[s]),!1));r("_".repeat(e)),Qu(o,n,r),r("=".repeat(e));for(var u=t.layers,p=0;p0&&(r=r.slice(0,r.length-1)+" "),r=(r+=t[i]).slice(0,e[i]),r+=" ".repeat(e[i]-r.length);n(r)}function tl(t,e,n){var r,i;try{i=t.inboundNodes.map((function(t){return JSON.stringify(t.inputShapes)})).join(",")}catch(t){i="multiple"}try{r=JSON.stringify(t.outputShape)}catch(t){r="multiple"}var a=t.name,o=t.getClassName();Qu(["".concat(a," (").concat(o,")"),i,r,t.countParams().toString()],e,n)}function el(t,e,n,r){var i,a,o,s;try{s=t.inboundNodes.map((function(t){return JSON.stringify(t.inputShapes)})).join(",")}catch(t){s="multiple"}try{o=JSON.stringify(t.outputShape)}catch(t){o="multiple"}var u=[];try{for(var c=l(t.inboundNodes),h=c.next();!h.done;h=c.next()){var p=h.value;if(!(null!=n&&n.length>0&&-1===n.indexOf(p)))for(var f=0;f1 nodes"),b(0===P,"input layer has >1 tensors"),_.inputLayers.push(M),_.inputLayersNodeIndices.push(B),_.inputLayersTensorIndices.push(P)}}catch(t){a={error:t}}finally{try{W&&!W.done&&(o=U.return)&&o.call(U)}finally{if(a)throw a.error}}_.inputNames=[],_.outputNames=[],_.feedInputShapes=[],_.feedInputNames=[],_.feedOutputNames=[];for(var V=0;V<_.inputLayers.length;V++){if(!((M=_.inputLayers[V])instanceof ue))throw new TypeError("Input layers to a LayersModel must be InputLayer objects. "+"Received inputs: ".concat(e.inputs,". ")+"Input ".concat(V," (0-based) originates ")+"from layer type ".concat(M.getClassName(),"."));_.inputNames.push(M.name),_.feedInputShapes.push(M.batchInputShape),_.feedInputNames.push(M.name)}try{for(var q=l(_.outputLayers),G=q.next();!G.done;G=q.next()){M=G.value;_.outputNames.push(M.name)}}catch(t){s={error:t}}finally{try{G&&!G.done&&(u=q.return)&&u.call(q)}finally{if(s)throw s.error}}_.internalInputShapes=_.inputs.map((function(t){return t.shape})),_.internalOutputShapes=_.outputs.map((function(t){return t.shape}));var K={},H={},J={},Z={},Y={},X=[],$=function(t,e,r,i,a,o){null!=i&&null!=a&&null!=o||(i=t.sourceLayer,a=t.nodeIndex,o=t.tensorIndex);var s=i.inboundNodes[a];if(-1!==r.indexOf(s))throw new f("The tensor ".concat(t.name,' at layer "').concat(i.name,'" ')+"is part of a cycle.");if(-1===e.indexOf(s)){_.containerNodes.add(n.nodeKey(i,a)),i.id in Y||(Y[i.id]=Object.keys(Y).length),-1===r.indexOf(s)&&r.push(s);for(var u=s.inboundLayers.length,l=0;l=0;)r.splice(r.indexOf(s),1);X.push(s)}},Q=[],tt=[];try{for(var et=l(_.outputs),nt=et.next();!nt.done;nt=et.next()){var rt=nt.value;$(rt,Q,tt)}}catch(t){c={error:t}}finally{try{nt&&!nt.done&&(h=et.return)&&h.call(et)}finally{if(c)throw c.error}}var it=X.slice().reverse();try{for(var at=l(it),ot=at.next();!ot.done;ot=at.next()){H[(Et=ot.value).id]=Et,Et.id in K||(K[Et.id]=0);var st=K[Et.id],ut=null==J[Et.outboundLayer.id]?0:J[Et.outboundLayer.id];st=Math.max(st,ut),J[Et.outboundLayer.id]=st,Z[Et.outboundLayer.id]=Et.outboundLayer,K[Et.id]=st;for(V=0;Vr?1:0}));try{for(var wt=(m=void 0,l(bt)),kt=wt.next();!kt.done;kt=wt.next()){(M=kt.value)instanceof n&&_.internalContainerRefs.push(M),_.layers.push(M)}}catch(t){m={error:t}}finally{try{kt&&!kt.done&&(w=wt.return)&&w.call(wt)}finally{if(m)throw m.error}}}}catch(t){v={error:t}}finally{try{mt&&!mt.done&&(y=yt.return)&&y.call(yt)}finally{if(v)throw v.error}}_.layersByDepth=dt,vt=Object.keys(pt).map((function(t){return parseInt(t,10)})).sort(T);var xt=_.inputs.slice(),St=[];try{for(var Nt=l(vt),It=Nt.next();!It.done;It=Nt.next()){st=It.value;try{for(var At=(S=void 0,l(pt[st])),zt=At.next();!zt.done;zt=At.next()){var Et;if(null!=(M=(Et=zt.value).outboundLayer)){try{for(var Tt=(I=void 0,l(Et.inputTensors)),Ct=Tt.next();!Ct.done;Ct=Tt.next()){rt=Ct.value;if(-1===xt.indexOf(rt))throw new f("Graph disconnected: cannot obtain value for tensor ".concat(rt)+' at layer "'.concat(M.name,'". ')+"The following previous layers were accessed without "+"issue: ".concat(St))}}catch(t){I={error:t}}finally{try{Ct&&!Ct.done&&(A=Tt.return)&&A.call(Tt)}finally{if(I)throw I.error}}try{for(var Ft=(z=void 0,l(Et.outputTensors)),Dt=Ft.next();!Dt.done;Dt=Ft.next()){rt=Dt.value;xt.push(rt)}}catch(t){z={error:t}}finally{try{Dt&&!Dt.done&&(E=Ft.return)&&E.call(Ft)}finally{if(z)throw z.error}}St.push(M.name)}}}catch(t){S={error:t}}finally{try{zt&&!zt.done&&(N=At.return)&&N.call(At)}finally{if(S)throw S.error}}}}catch(t){k={error:t}}finally{try{It&&!It.done&&(x=Nt.return)&&x.call(Nt)}finally{if(k)throw k.error}}_.nodesByDepth=pt;var _t=_.layers.map((function(t){return t.name})),Lt=function(t){var e=_t.filter((function(e){return e===t})).length;if(1!==e)throw new f('The name "'.concat(t,'" is used ').concat(e," times ")+"in the model. All layer names should be unique. Layer names: "+JSON.stringify(_t))};try{for(var Rt=l(_t),Ot=Rt.next();!Ot.done;Ot=Rt.next()){Lt(Ot.value)}}catch(t){F={error:t}}finally{try{Ot&&!Ot.done&&(D=Rt.return)&&D.call(Rt)}finally{if(F)throw F.error}}return _.outboundNodes=[],_.inboundNodes=[],new ie({outboundLayer:_,inboundLayers:[],nodeIndices:[],tensorIndices:[],inputTensors:_.inputs,outputTensors:_.outputs,inputMasks:_.inputs.map((function(t){return null})),outputMasks:_.outputs.map((function(t){return null})),inputShapes:_.inputs.map((function(t){return t.shape})),outputShapes:_.outputs.map((function(t){return t.shape}))}),_.built=!0,_._refCount=1,_}return o(n,t),n.prototype.assertNotDisposed=function(){if(0===this._refCount)throw new Error("Container '".concat(this.name,"' is already disposed."))},n.prototype.dispose=function(){var t,e,n,r;this.assertNotDisposed();var i={refCountAfterDispose:null,numDisposedVariables:0};if(0==--this._refCount){try{for(var a=l(this.layers),o=a.next();!o.done;o=a.next()){var s=o.value;i.numDisposedVariables+=s.dispose().numDisposedVariables}}catch(e){t={error:e}}finally{try{o&&!o.done&&(e=a.return)&&e.call(a)}finally{if(t)throw t.error}}try{for(var u=l(this.internalContainerRefs),c=u.next();!c.done;c=u.next()){var h=c.value;i.numDisposedVariables+=h.dispose().numDisposedVariables}}catch(t){n={error:t}}finally{try{c&&!c.done&&(r=u.return)&&r.call(u)}finally{if(n)throw n.error}}}return i.refCountAfterDispose=this._refCount,i},Object.defineProperty(n.prototype,"trainable",{get:function(){return this.trainable_},set:function(t){this.layers.forEach((function(e){e._trainableWeights.forEach((function(e){return e.trainable=t}))})),this.trainable_=t},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"trainableWeights",{get:function(){var t,e;if(this._trainableWeights.length>0)throw new d("Container instance unexpectedly contains _trainableWeights.The trainable weights of a Container are a union of the trainable weights of its consituent Layers. Its own _trainableWeights must remain an empty Array.");if(!this.trainable)return[];var n=[];try{for(var r=l(this.layers),i=r.next();!i.done;i=r.next()){var a=i.value;n=n.concat(a.trainableWeights)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(e=r.return)&&e.call(r)}finally{if(t)throw t.error}}return n},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"nonTrainableWeights",{get:function(){var t,e,n,r,i=[];try{for(var a=l(this.layers),o=a.next();!o.done;o=a.next()){var s=o.value;i.push.apply(i,h([],c(s.nonTrainableWeights),!1))}}catch(e){t={error:e}}finally{try{o&&!o.done&&(e=a.return)&&e.call(a)}finally{if(t)throw t.error}}if(!this.trainable){var u=[];try{for(var p=l(this.layers),f=p.next();!f.done;f=p.next()){s=f.value;u.push.apply(u,h([],c(s.trainableWeights),!1))}}catch(t){n={error:t}}finally{try{f&&!f.done&&(r=p.return)&&r.call(p)}finally{if(n)throw n.error}}return u.concat(i)}return i},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"weights",{get:function(){return this.trainableWeights.concat(this.nonTrainableWeights)},enumerable:!1,configurable:!0}),n.prototype.loadWeights=function(t,e){var n,r,i,a;void 0===e&&(e=!0);var o={},s=0,u=function(t){var e=Object.keys(t);if(0===e.length)return!1;var n=e[0].split("/");return!isNaN(parseInt(n[n.length-1],10))}(t);u&&this.parseWeights(t);try{for(var h=l(this.layers),p=h.next();!p.done;p=h.next()){var f=p.value;try{for(var g=(i=void 0,l(f.weights.entries())),v=g.next();!v.done;v=g.next()){var y=c(v.value,2),m=y[0],b=y[1],w=u?"".concat(b.name.split("/").slice(0,-1).join("/")+"/").concat(m):b.originalName;if(null!=o[w])throw new d("Duplicate weight name: ".concat(w));o[w]=b,s++}}catch(t){i={error:t}}finally{try{v&&!v.done&&(a=g.return)&&a.call(g)}finally{if(i)throw i.error}}}}catch(t){n={error:t}}finally{try{p&&!p.done&&(r=h.return)&&r.call(h)}finally{if(n)throw n.error}}var k=[];for(var x in t){var S=x;if(null==o[x]){var N=x.split("/");S=N.slice(0,-2).concat([N[N.length-1]]).join("/")}if(null!=o[S])k.push([o[S],t[x]]);else if(e)throw new d("Provided weight data has no target variable: ".concat(x));delete o[S]}if(e){var I=[];for(var x in o)I.push(x);if(I.length>0)throw new d("".concat(I.length," of ").concat(s," weights are not set: ")+"".concat(I))}te(k)},n.prototype.parseWeights=function(t){var e=function(e){var n=e.split("/"),r=["vars","layer_checkpoint_dependencies"],i=n.map((function(t){return t.startsWith("_")?t.slice(1):t})).filter((function(t){return!r.includes(t)})).join("/");i!==e&&(t[i]=t[e],delete t[e])};for(var n in Object.keys(t))e(n)},n.prototype.updatedConfig=function(){var t=this.getConfig(),e={};return e.className=this.getClassName(),e.config=t,e.kerasVersion="tfjs-layers ".concat(al),e.backend="TensorFlow.js",e},n.prototype.toJSON=function(t,e){void 0===e&&(e=!0);var n=il(this.updatedConfig());return e?JSON.stringify(n):n},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){t=x(t);for(var e=new ce,i=0;i1)try{for(var p=l(h),f=p.next();!f.done;f=p.next()){var g=f.value,v=this.nodesByDepth[g];try{for(var y=(r=void 0,l(v)),m=y.next();!m.done;m=y.next()){var w=m.value;u=w.outboundLayer;if(-1===this.inputLayers.map((function(t){return t.id})).indexOf(u.id)){for(var x=[],S=0;S0){for(var v=[],y=0;y0&&t.apply(k(a),i)}function x(t){var n=t.name,i=zu(t,null!=e.customObjects?e.customObjects:{});i.setFastWeightInitDuringBuild(r),v[n]=i,t.inboundNodes.forEach((function(t){if(!(t instanceof Array))throw new d("Corrupted configuration, expected array for nodeData: ".concat(t));m(i,t)}))}var S=e.name,N=e.layers;try{for(var I=l(N),A=I.next();!A.done;A=I.next()){x(T=A.value)}}catch(t){i={error:t}}finally{try{A&&!A.done&&(a=I.return)&&a.call(I)}finally{if(i)throw i.error}}for(;!F(y);)try{for(var z=(o=void 0,l(N)),E=z.next();!E.done;E=z.next()){var T=E.value;if((K=v[T.name]).name in y){var C=y[K.name];delete y[K.name];try{for(var D=(u=void 0,l(C)),_=D.next();!_.done;_=D.next()){w(K,_.value)}}catch(t){u={error:t}}finally{try{_&&!_.done&&(c=D.return)&&c.call(D)}finally{if(u)throw u.error}}}}}catch(t){o={error:t}}finally{try{E&&!E.done&&(s=z.return)&&s.call(z)}finally{if(o)throw o.error}}var L=[],R=[],O=e.inputLayers;try{for(var M=l(O),B=M.next();!B.done;B=M.next()){var P=(T=B.value)[0],j=T[1],U=T[2];b(P in v);var W=(K=v[P]).inboundNodes[j].outputTensors;L.push(W[U])}}catch(t){h={error:t}}finally{try{B&&!B.done&&(p=M.return)&&p.call(M)}finally{if(h)throw h.error}}var V=e.outputLayers;try{for(var q=l(V),G=q.next();!G.done;G=q.next()){P=(T=G.value)[0],j=T[1],U=T[2];b(P in v);var K;W=(K=v[P]).inboundNodes[j].outputTensors;R.push(W[U])}}catch(t){f={error:t}}finally{try{G&&!G.done&&(g=q.return)&&g.call(q)}finally{if(f)throw f.error}}return new t({inputs:L,outputs:R,name:S})},Object.defineProperty(n.prototype,"stateful",{get:function(){var t,e;if(this._stateful)throw new d("Container instance unexpectedly has _stateful = true. The statefulness of a Container is determined by the Layers it contains. Its _stateful property must remain the default false.");try{for(var n=l(this.layers),r=n.next();!r.done;r=n.next()){if(r.value.stateful)return!0}}catch(e){t={error:e}}finally{try{r&&!r.done&&(e=n.return)&&e.call(n)}finally{if(t)throw t.error}}return!1},enumerable:!1,configurable:!0}),n.prototype.resetStates=function(){var t=this;e.tidy((function(){t.layers.forEach((function(t){t.stateful&&t.resetStates()}))}))},n}(oe);function sl(t,e){return function(t,e,n){var r=e.length;if(null==t||Array.isArray(t)&&0===t.length)return e.map((function(t){return null}));if(1===r)return Array.isArray(t)&&1===t.length?t:"object"==typeof t&&e[0]in t?[t[e[0]]]:[t];if(Array.isArray(t)){if(t.length!==r)throw new Error("Provided ".concat(n," is an array of ").concat(t.length," ")+"element(s), but the model has ".concat(r," outputs. ")+"Make sure a set of weights is provided for each model output.");return t}if("object"==typeof t&&Object.keys(t).length>0&&"object"==typeof t[Object.keys(t)[0]]){var i=[];return e.forEach((function(e){e in t?i.push(t[e]):i.push(null)})),i}throw new Error("The model has multiple (".concat(r,") outputs, ")+"so ".concat(n," must be either an array with ")+"".concat(r," elements or an object with ").concat(e," keys. ")+"Provided ".concat(n," not understood: ").concat(JSON.stringify(t)))}(t,e,"classWeight")}function ul(t,n,r,i){return s(this,void 0,void 0,(function(){var a,o,s,l,c;return u(this,(function(u){switch(u.label){case 0:if(null!=n||null!=i)throw new Error("Support sampleWeight is not implemented yet");return null==r?[3,2]:(a=e.tidy((function(){if(1===t.shape.length)return e.clone(t);if(2===t.shape.length){if(t.shape[1]>1){return e.argMax(t,1)}if(1===t.shape[1])return e.reshape(t,[t.shape[0]]);throw new Error("Encountered unexpected last-dimension size (".concat(t.shape[1],") ")+"during handling of class weights. The size is expected to be >= 1.")}throw new Error("Unexpected rank of target (y) tensor (".concat(t.rank,") during ")+"handling of class weights. The rank is expected to be 1 or 2.")})),l=(s=Array).from,[4,a.data()]);case 1:return o=l.apply(s,[u.sent()]),e.dispose(a),c=[],o.forEach((function(t){if(null==r[t])throw new Error("classWeight must contain all classes in the training data. "+"The class ".concat(t," exists in the data but not in ")+"classWeight");c.push(r[t])})),[2,e.tensor1d(c,"float32")];case 2:return[2,null]}}))}))}function ll(t,n){return e.mul(t,n)}function cl(t,e){var n,r,a=e;n=a.xs,r=a.ys,i.util.assert(null!=n&&null!=r,(function(){return"A Dataset iterator for fitDataset() is expected to generate objects of the form `{xs: xVal, ys: yVal}`, where the two values may be `tf.Tensor`, an array of Tensors, or a map of string to Tensor. The provided Dataset instead generates "+"".concat(e)}));var o=hl("input",t.inputNames,n),s=hl("output",t.outputNames,r),u=o[0].shape[0];i.util.assert(o.length===t.inputs.length,(function(){return"LayersModel has ".concat(t.inputs.length," inputs, but the dataset ")+"provides ".concat(o.length," inputs. (Expected input keys: ")+"".concat(JSON.stringify(t.inputNames),")")})),i.util.assert(s.length===t.outputs.length,(function(){return"LayersModel has ".concat(t.outputs.length," outputs, but the dataset ")+"provides ".concat(s.length," outputs. (Expected output keys: ")+"".concat(JSON.stringify(t.outputNames),")")}));for(var l=function(e){i.util.assert(o[e].shape[0]===u,(function(){return"Batch size mismatch: input "+"".concat(t.inputNames[e]," has ").concat(o[e].shape[0],"; ")+"expected ".concat(u," based on input ").concat(t.inputNames[0],".")}))},c=0;c0&&Number.isInteger(n.epochs),(function(){return"For fitDataset(), config.epochs is expected to be a positive "+"integer, but got ".concat(n.epochs)})),i.util.assert(!r||n.batchesPerEpoch>0&&Number.isInteger(n.batchesPerEpoch),(function(){return"For fitDataset(), config.batchesPerEpoch is expected to be a "+"positive integer if specified, but got ".concat(n.batchesPerEpoch)})),i.util.assert(null==n.validationSplit,(function(){return"`validationSplit` is not supported by `fitDataset()`. Use validationData instead."})),t.isTraining)throw new Error("Cannot start training because another fit() call is ongoing.");t.isTraining=!0,u.label=1;case 1:return u.trys.push([1,,26,27]),a=null!=n.validationData,o=void 0,s=void 0,a&&(fl(n.validationData)?i.util.assert(null==n.validationBatches||n.validationBatches>0&&Number.isInteger(n.validationBatches),(function(){return"For fitDataset() with dataset-based validation, config.validationBatches is expected not to be provided, or to be a positive integer, "+"but got ".concat(n.validationBatches)})):(l=function(t){if(3===t.length)throw new g("Validation with sample weights is not implemented yet.");return{xs:t[0],ys:t[1]}}(n.validationData),o=l.xs,s=l.ys)),c=t.makeTrainFunction(),h=t.getDedupedMetricsNames(),p=void 0,p=a?h.slice().concat(h.map((function(t){return"val_"+t}))):h.slice(),f=Nu(n.callbacks,n.yieldEvery),d=null==n.verbose?1:n.verbose,v=Au(f,d,n.epochs,null,null,function(t,e){var n=null;null!=e.batchesPerEpoch?n=e.batchesPerEpoch:Number.isFinite(t.size)&&(n=t.size);return n}(e,n),null,a,p),y=v.callbackList,m=v.history,y.setModel(t),t.history=m,[4,y.onTrainBegin()];case 2:return u.sent(),t.stopTraining_=!1,b=null==n.initialEpoch?0:n.initialEpoch,[4,e.iterator()];case 3:w=u.sent(),u.label=4;case 4:return b=n.batchesPerEpoch:I.done)?a?(B=void 0,fl(n.validationData)?(P=x,[4,t.evaluateDataset(n.validationData,{batches:n.validationBatches})]):[3,17]):[3,19]:[3,20];case 16:return B=P.apply(void 0,[u.sent()]),[3,18];case 17:B=x(t.evaluate(o,s,{batchSize:null==n.validationBatchSize?32:n.validationBatchSize,verbose:0})),u.label=18;case 18:for(j=0;j0)throw new g("Verbose mode is not implemented yet.");return i.util.assert(!a||r.batches>0&&Number.isInteger(r.batches),(function(){return"Test loop expects `batches` to be a positive integer, but "+"received ".concat(JSON.stringify(r.batches))})),"function"!=typeof n.next?[3,1]:(c=n,[3,3]);case 1:return[4,n.iterator()];case 2:c=y.sent(),y.label=3;case 3:l=c,h=0,p=0,f=function(){var n;return u(this,(function(u){switch(u.label){case 0:return[4,l.next()];case 1:return n=u.sent(),s=i.tidy((function(){if(n.value){var r=cl(t,n.value),a=r.xs,u=r.ys,l=a.concat(u),c=i.tidy((function(){return o(l)}));if(i.dispose(l),0===p)for(var f=0;f0&&i.dispose(n)};for(f=0;f0&&Number.isInteger(t),(function(){return"batchSize is required to be a positive integer, but got ".concat(t)}))}function vl(t,e,n){return null==t?[null]:Array.isArray(t)?t.map((function(t){return ct(t,e,n-e)})):ct(t,e,n-e)}function yl(t,e){return i.tidy((function(){return null==t?null:Array.isArray(t)?t.map((function(t){return yl(t,e)})):mt(t,"int32"===e.dtype?e:i.cast(e,"int32"))}))}function ml(t,e){for(var n=[],r=0,i=null;r=t&&(i=t),n.push([r,i]),r=i;return n}function bl(t){var n=[];t instanceof e.Tensor&&(t=[t]);for(var r=0;r0)u=!0;else if(xl(t)){for(var c in t)if(t.hasOwnProperty(c)){u=!0;break}}else u=!0;if(u)throw new d("Error when checking model ".concat(i," expected no data, ")+"but got ".concat(t))}return[]}if(null==t)return e.map((function(t){return null}));if(xl(t)){s=[];try{for(var h=l(e),p=h.next();!p.done;p=h.next()){var f=p.value;if(null==t[f])throw new d('No data provided for "'.concat(f,'". Need data for each key in: ')+"".concat(e));s.push(t[f])}}catch(t){a={error:t}}finally{try{p&&!p.done&&(o=h.return)&&o.call(h)}finally{if(a)throw a.error}}}else if(kl(t)){if(t.length!==e.length)throw new d("Error when checking model ".concat(i,": the Array of ")+"Tensors that you are passing to your model is not the size the "+"model expected. Expected to see ".concat(e.length," Tensor(s), but ")+"instead got the following list of Tensor(s): ".concat(t));s=t}else{if(e.length>1)throw new d("The model ".concat(i," expects ").concat(e.length," Tensor(s), ")+"but only received one Tensor. Found: Tensor with shape ".concat(t.shape));s=[t]}if(s=bl(s),null!=n)for(var g=0;g=0&&m!==b)throw new d("".concat(i," expected a batch of elements where each ")+"example has shape [".concat(n[g].slice(1,n[g].length),"] ")+"(i.e.,tensor shape [*,".concat(n[g].slice(1,n[g].length),"])")+" but the ".concat(i," received an input with ").concat(v.shape[0])+" examples, each with shape [".concat(v.shape.slice(1,v.shape.length),"]")+" (tensor shape [".concat(v.shape,"])"))}}return s}function Nl(t,e,n,r,i){var a;if(void 0===r&&(r=!0),void 0===i&&(i=""),Array.isArray(t)){if(t.length!==e.length)throw new d("Error when checking model ".concat(i,": the Array of ")+"Tensors that you are passing to your model is not the size the "+"the model expected. Expected to see ".concat(e.length," Tensor(s),")+" but instead got ".concat(t.length," Tensors(s)."));a=t}else{if(e.length>1)throw new d("The model expects ".concat(e.length," ").concat(i," Tensors, ")+"but only received one Tensor. Found: array with shape "+"".concat(JSON.stringify(t.shape),"."));a=[t]}if(null!=n)for(var o=0;o1&&(i.metricsTensors.push([e,t]),i.metricsNames.push(i.outputNames[t]+"_loss"))}}));var v=function(t,e){var n,r,i;if(null==t||Array.isArray(t)&&0===t.length)return e.map((function(t){return[]}));if("string"==typeof t||"function"==typeof t)i=[t];else{if(!Array.isArray(t)&&"object"!=typeof t)throw new TypeError("Type of metrics argument not understood. Expected an string,"+"function, Array, or Object, found: ".concat(t));i=t}if(Array.isArray(i))return e.map((function(t){return i}));var a=[];try{for(var o=l(e),s=o.next();!s.done;s=o.next()){var u=s.value,c=i.hasOwnProperty(u)?i[u]:[];Array.isArray(c)||(c=[c]),a.push(c)}}catch(t){n={error:t}}finally{try{s&&!s.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}return a}(t.metrics,this.outputNames);X("metric",(function(){for(var t=function(t){if(-1!==g.indexOf(t))return"continue";!function(e){var n,r,a,o,s,u=function(e){if("string"==typeof e&&-1!==["accuracy","acc","crossentropy","ce"].indexOf(e)){var n=i.internalOutputShapes[t];1===n[n.length-1]||i.lossFunctions[t]===Lu?-1!==["accuracy","acc"].indexOf(e)?o=Bu:-1!==["crossentropy","ce"].indexOf(e)&&(o=Vu):i.lossFunctions[t]===_u?-1!==["accuracy","acc"].indexOf(e)?o=qu:-1!==["crossentropy","ce"].indexOf(e)&&(o=Ku):-1!==["accuracy","acc"].indexOf(e)?o=Pu:-1!==["crossentropy","ce"].indexOf(e)&&(o=Gu);var r=void 0;-1!==["accuracy","acc"].indexOf(e)?r="acc":-1!==["crossentropy","ce"].indexOf(e)&&(r="ce"),s=o,a=""+r}else{var u=function(t){if("string"==typeof t&&t in Hu)return Hu[t];if("string"!=typeof t&&null!=t)return t;throw new d("Unknown metric ".concat(t))}(e);s=u,a=""+Ju(e)}var l;X(a,(function(){l=s})),function(t,e,n){i.outputNames.length>1&&(e=i.outputNames[t]+"_"+e),i.metricsNames.push(e),i.metricsTensors.push([n,t])}(t,a,l)};try{for(var c=(n=void 0,l(e)),h=c.next();!h.done;h=c.next()){u(h.value)}}catch(t){n={error:t}}finally{try{h&&!h.done&&(r=c.return)&&r.call(c)}finally{if(n)throw n.error}}}(v[t])},e=0;e0){var f=[];throw r.forEach((function(e,n){null==e&&f.push(t[n])})),new d("Cannot find SymbolicTensors for output name(s): "+"".concat(JSON.stringify(f)))}return r},n.prototype.predictLoop=function(t,e,n){var r=this;return void 0===e&&(e=32),void 0===n&&(n=!1),i.tidy((function(){var a=r.checkNumSamples(t);if(n)throw new g("Verbose predictLoop() is not implemented yet.");for(var o=ml(a,e),s=r.outputs.map((function(t){return[]})),u=function(e){i.tidy((function(){var n=o[e][0],i=o[e][1],a=vl(t,n,i),s=[];if(Array.isArray(a))for(var u=0;u1)throw new d("All input Tensors (x) should have the same number of samples. Got array shapes: "+"".concat(JSON.stringify(t.map((function(t){return t.shape})))));if(a.length>1)throw new d("All target Tensors (y) should have the same number of samples. Got array shapes: "+"".concat(JSON.stringify(n.map((function(t){return t.shape})))));if(i.length>0&&a.length>0&&!e.util.arraysEqual(i,a))throw new d("Input Tensors should have the same number of samples as target "+"Tensors. Found ".concat(i[0]," input sample(s) and ").concat(a[0]," target ")+"sample(s).")}(t=Sl(t,this.feedInputNames,this.feedInputShapes,!1,"input"),n=Sl(n,this.feedOutputNames,a,!1,"target")),function(t,e,n){for(var r=[Tu,Lu,Du],i=0;i0&&t[0].shape[0]%i!=0)throw new d("In a stateful network, you should only pass inputs with a number of samples that is divisible by the batch size "+"".concat(i,". Found: ").concat(t[0].shape[0]," sample(s)."));return[t,n]},n.prototype.standardizeUserData=function(t,e,n,r,i,a){return void 0===i&&(i=!0),s(this,void 0,void 0,(function(){var o,s,l,h,p,f,d,g;return u(this,(function(u){switch(u.label){case 0:if(o=c(this.standardizeUserDataXY(t,e,i,a),2),s=o[0],l=o[1],null!=n)throw new Error("sample weight is not supported yet.");if(h=null,null==r)return[3,4];p=sl(r,this.outputNames),h=[],f=0,u.label=1;case 1:return f0)throw new g("Verbose mode is not implemented yet.");if(null!=o)throw new g("steps mode in testLoop() is not implemented yet");for(var c=ml(u,r),h=e.tensor1d(ot(0,u)),p=0;p1){var a=w(t.slice(0,n),r);i+="_".concat(a)}e.push(i)}return e},n.prototype.makeTrainFunction=function(){var t=this;return function(e){var n=[],r=e.slice(0,t.inputs.length),a=e.slice(t.inputs.length,t.inputs.length+t.outputs.length),o=e.slice(t.inputs.length+t.outputs.length,t.inputs.length+2*t.outputs.length),s=[],u=t.collectedTrainableWeights.map((function(t){return t.read()}));return[t.optimizer_.minimize((function(){for(var e=[],u=0;u1&&u0))return[3,4];if(m=!0,2!==n.validationData.length)throw 3===n.validationData.length?new g("validationData including sample weights is not supported yet."):new d("When passing validation data, it must contain 2 (valX, valY) or 3 (valX, valY, valSampleWeight) items; "+"".concat(n.validationData," is invalid."));return l=n.validationData[0],c=n.validationData[1],!0,[4,this.standardizeUserData(l,c,null,null,true,v)];case 3:return w=u.sent(),h=w[0],p=w[1],b=h.concat(p),[3,5];case 4:null!=n.validationSplit&&n.validationSplit>0&&n.validationSplit<1?(m=!0,k=Math.floor(r[0].shape[0]*(1-n.validationSplit)),x=r[0].shape[0],h=vl(r,k,x),o=r,r=vl(r,0,k),p=vl(a,k,x),s=a,a=vl(a,0,k),b=h.concat(p)):null!=n.validationSteps&&(m=!0),u.label=5;case 5:return S=r.concat(a).concat(f),this.checkTrainableWeightsConsistency(),N=this.makeTrainFunction(),I=this.getDedupedMetricsNames(),A=void 0,z=void 0,m?(this.makeTestFunction(),A=this.testFunction,z=I.slice().concat(I.map((function(t){return"val_"+t})))):(A=null,b=[],z=I.slice()),E=Nu(n.callbacks,n.yieldEvery),[4,this.fitLoop(N,S,I,v,n.epochs,n.verbose,E,A,b,n.shuffle,z,n.initialEpoch,null,null)];case 6:return[2,u.sent()];case 7:return this.isTraining=!1,wl(r,t),wl(a,e),wl(o,t),wl(s,e),wl(h,l),wl(p,c),null!=f&&i.dispose(f),[7];case 8:return[2]}}))}))},n.prototype.fitLoop=function(t,n,r,a,o,l,c,h,p,f,v,y,m,b){return s(this,void 0,void 0,(function(){var s,w,k,x,S,N,I,A,z,E=this;return u(this,(function(T){switch(T.label){case 0:if(null==a&&(a=32),null==o&&(o=1),null==f&&(f=!0),null==y&&(y=0),s=!1,null!=h&&null!=p&&(s=!0),null!=b&&(s=!0,null==m))throw new d("Can only use `validationSteps` when doing step-wise training, i.e., `stepsPerEpoch` must be set.");return null!=(w=this.checkNumSamples(n,a,m,"steps_per_epoch"))&&(k=ot(0,w)),null==l&&(l=1),x=Au(c,l,o,y,w,m,a,s,v),S=x.callbackList,N=x.history,S.setModel(this),this.history=N,[4,S.onTrainBegin()];case 1:T.sent(),this.stopTraining_=!1,I=function(o){var l,c,d,v,y;return u(this,(function(b){switch(b.label){case 0:return[4,S.onEpochBegin(o)];case 1:if(b.sent(),l={},null==m)return[3,2];throw new g("stepsPerEpoch mode is not implemented yet.");case 2:if("batch"===f)throw new g("batch shuffling is not implemneted yet");f&&e.util.shuffle(k),c=e.tensor1d(k),d=ml(w,a),v=function(e){var o;return u(this,(function(u){switch(u.label){case 0:return o={},[4,S.onBatchBegin(e,o)];case 1:return u.sent(),i.tidy((function(){var u=d[e][0],f=d[e][1],g=ct(c,u,f-u);o.batch=e,o.size=f-u;for(var v=yl(n,g),y=t(v),m=0;m1)throw new d("Found more than one (".concat(r.length,") save handlers for ")+"URL '".concat(t,"'"));t=r[0]}if(null==t.save)throw new d("LayersModel.save() cannot proceed because the IOHandler provided does not have the `save` attribute defined.");return[4,e.io.encodeWeights(this.getNamedWeights(n))];case 1:return i=u.sent(),!1,null,a=this.toJSON(null,false),o={modelTopology:a,format:"layers-model",generatedBy:"TensorFlow.js tfjs-layers v".concat(al),convertedBy:null},null!=n&&n.includeOptimizer&&null!=this.optimizer?(o.trainingConfig=this.getTrainingConfig(),s="optimizer",v=(g=e.io).encodeWeights,[4,this.optimizer.getWeights()]):[3,4];case 2:return[4,v.apply(g,[u.sent(),s])];case 3:l=u.sent(),p=l.data,f=l.specs,(y=i.specs).push.apply(y,h([],c(f),!1)),i.data=e.io.concatenateArrayBuffers([i.data,p]),u.label=4;case 4:return null!=this.userDefinedMetadata&&(!0,Yu(this.userDefinedMetadata,this.name,true),o.userDefinedMetadata=this.userDefinedMetadata),o.weightData=i.data,o.weightSpecs=i.specs,[2,t.save(o)]}}))}))},n.prototype.setUserDefinedMetadata=function(t){Yu(t,this.name),this.userDefinedMetadata=t},n.prototype.getUserDefinedMetadata=function(){return this.userDefinedMetadata},n}(ol);Il.className="Model",e.serialization.registerClass(Il);var Al=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e}(Il);function zl(t,n,r){return s(this,void 0,void 0,(function(){var i,a,o,s,l,c,h,p,f;return u(this,(function(u){switch(u.label){case 0:if(null==r&&(r={}),null==t.load)throw new d("Cannot proceed with model loading because the IOHandler provided does not have the `load` method implemented.");return[4,t.load()];case 1:if(i=u.sent(),null!=(a=i.modelTopology).model_config&&(a=a.model_config),o=null==r.strict||r.strict,s=null!=i.weightData&&null!=i.weightSpecs&&o,l=zu(rl(a),n,s),null!=(c=i.trainingConfig)&&l.loadTrainingConfig(c),null!=i.userDefinedMetadata&&l.setUserDefinedMetadata(i.userDefinedMetadata),null==i.weightData)return[3,4];if(null==i.weightSpecs)throw new d("LayersModel artifacts contains weight data, but not weight specs. Therefore loading of weights cannot proceed.");return h=function(t,n){var r=e.io.decodeWeights(t,n),i={},a=[];return n.forEach((function(t){"optimizer"===t.group?a.push({name:t.name,tensor:r[t.name]}):i[t.name]=r[t.name]})),{modelWeights:i,optimizerWeights:a}}(i.weightData,i.weightSpecs),p=h.modelWeights,f=h.optimizerWeights,l.loadWeights(p,o),null!=l.optimizer&&f.length>0?[4,l.optimizer.setWeights(f)]:[3,3];case 2:u.sent(),u.label=3;case 3:e.dispose(p),e.dispose(f.map((function(t){return t.tensor}))),u.label=4;case 4:return[2,l]}}))}))}Al.className="Functional",e.serialization.registerClass(Al);var El=function(t){function n(e){var n,r,i=t.call(this,{inputs:[],outputs:[]})||this;if(e=e||{},i.trainable=!0,i.built=!1,i.name=null!=e.name?e.name:j("sequential_"),null!=e.layers)try{for(var a=l(e.layers),o=a.next();!o.done;o=a.next()){var s=o.value;i.add(s)}}catch(t){n={error:t}}finally{try{o&&!o.done&&(r=a.return)&&r.call(a)}finally{if(n)throw n.error}}return i}return o(n,t),n.prototype.checkShape=function(t){if(t.inboundNodes[0].outputTensors[0].shape.some((function(t){return t<0})))throw new d("Negative dimension size caused by adding layer "+"".concat(t.name," with input shape [")+"".concat(t.inboundNodes[0].inputTensors[0].shape,"]"))},n.prototype.add=function(t){var e,r=t instanceof n||t instanceof Il;if(r){if(1!==(e=t).outputs.length)throw new d("All layers in a Sequential model should have a single output tensor. For multi-output layers, use the functional API.");if(1!==e.inputs.length)throw new d("All layers in a Sequential model should have a single input tensor. For multi-input layers, use the functional API.")}if(0===this.outputs.length){if(0===t.inboundNodes.length){if(null==t.batchInputShape)throw new d("The first layer in a Sequential model must get an `inputShape` or `batchInputShape` argument.");var i=le({batchShape:t.batchInputShape,dtype:t.dtype,name:t.name+"_input"});t.apply(i)}if(r)this.outputs=e.outputs,this.inputs=e.inputs;else{if(1!==t.inboundNodes.length)throw new d("A layer added to a Sequential model must not already be "+"connected somewhere else. LayersModel received layer ".concat(t.name," ")+"which has ".concat(t.inboundNodes.length," pre-existing inbound ")+"connections.");if(1!==t.inboundNodes[0].outputTensors.length)throw new d("All layers in a Sequential model should have a single output tensor. For multi-output layers, use the functional API.");this.checkShape(t),this.outputs=[t.inboundNodes[0].outputTensors[0]],this.inputs=se(this.outputs[0])}this.inboundNodes=[],new ie({outboundLayer:this,inboundLayers:[],nodeIndices:[],tensorIndices:[],inputTensors:this.inputs,outputTensors:this.outputs,inputMasks:m(null,this.inputs.length),outputMasks:[null],inputShapes:this.inputs.map((function(t){return t.shape})),outputShapes:this.outputs[0].shape})}else{var a=t.apply(this.outputs[0]);if(Array.isArray(a))throw new TypeError("All layers in a Sequential model should have a single output tensor. For multi-output layers, use the functional API.");this.checkShape(t),this.outputs=[a],this.inboundNodes[0].outputTensors=this.outputs,this.inboundNodes[0].outputShapes=[this.outputs[0].shape]}this.layers.push(t),this.built=!1},n.prototype.pop=function(){if(0===this.layers.length)throw new TypeError("There are no layers in the model.");if(this.layers.pop(),0===this.layers.length)this.outputs=[],this.inboundNodes=[],this.outboundNodes=[];else{var t=this.layers.length-1;this.layers[t].outboundNodes=[],this.outputs=[this.layers[t].output],this.inboundNodes[0].outputTensors=this.outputs,this.inboundNodes[0].outputShapes=[this.outputs[0].shape]}},n.prototype.call=function(t,e){return null==this.model&&this.build(),this.model.call(t,e)},n.prototype.build=function(t){if(Zt(t),0===this.inputs.length||0===this.outputs.length)throw new TypeError("Sequential model cannot be built: model is empty. Add some layers first.");this.model=new Il({inputs:this.inputs,outputs:this.outputs[0],name:this.name+"_model"}),this.model.trainable=this.trainable,this.supportsMasking=this.model.supportsMasking,this.inputLayers=this.model.inputLayers,this.inputLayersNodeIndices=this.model.inputLayersNodeIndices,this.inputLayersTensorIndices=this.model.inputLayersTensorIndices,this.outputLayers=this.model.outputLayers,this.outputLayersNodeIndices=this.model.outputLayersNodeIndices,this.outputLayersTensorIndices=this.model.outputLayersTensorIndices,this.nodesByDepth=this.model.nodesByDepth,this.containerNodes=this.model.containerNodes,this.outputNames=this.model.outputNames,this.inputNames=this.model.inputNames,this.built=!0},n.prototype.countParams=function(){return this.built||this.build(),t.prototype.countParams.call(this)},n.prototype.summary=function(e,n,r){void 0===r&&(r=console.log),this.built||this.build(),t.prototype.summary.call(this,e,n,r)},n.prototype.setWeights=function(t){null==this.model&&this.build(),this.model.setWeights(t)},n.prototype.evaluate=function(t,e,n){if(void 0===n&&(n={}),!this.built)throw new f("The model needs to be compiled before being used.");return this.model.evaluate(t,e,n)},n.prototype.evaluateDataset=function(t,e){return s(this,void 0,void 0,(function(){return u(this,(function(n){if(!this.built)throw new f("The model needs to be compiled before being used.");return[2,this.model.evaluateDataset(t,e)]}))}))},n.prototype.predict=function(t,e){return void 0===e&&(e={}),null==this.model&&this.build(),this.model.predict(t,e)},n.prototype.predictOnBatch=function(t){return null==this.model&&this.build(),this.model.predictOnBatch(t)},n.prototype.compile=function(t){this.build(),this.model.compile(t),this.optimizer_=this.model.optimizer,this.isOptimizerOwned=this.model.isOptimizerOwned,this.loss=this.model.loss,this.metrics=this.model.metrics,this.metricsTensors=this.model.metricsTensors,this.metricsNames=this.model.metricsNames},Object.defineProperty(n.prototype,"optimizer",{get:function(){return null==this.model?void 0:this.model.optimizer},set:function(t){this.model.optimizer=t},enumerable:!1,configurable:!0}),n.prototype.fit=function(t,e,n){return void 0===n&&(n={}),s(this,void 0,void 0,(function(){return u(this,(function(r){if(!this.built)throw new f("The model needs to be compiled before being used.");return[2,this.model.fit(t,e,n)]}))}))},n.prototype.fitDataset=function(t,e){return s(this,void 0,void 0,(function(){return u(this,(function(n){if(!this.built)throw new f("The model needs to be compiled before being used.");return[2,this.model.fitDataset(t,e)]}))}))},n.prototype.trainOnBatch=function(t,e){return s(this,void 0,void 0,(function(){return u(this,(function(n){return[2,this.model.trainOnBatch(t,e)]}))}))},n.fromConfig=function(t,r,i,a){var o,s,u;void 0===a&&(a=!1);var c={};if(r instanceof Array){if(null==r[0].className||"Merge"===r[0].className)throw new d("Legacy serialization format not supported yet.");u=r}else e.util.assert(null!=r.layers,(function(){return"When the config data for a Sequential model is not an Array, it must be an Object that contains the 'layers' field."})),u=r.layers,delete r.layers,c=r;var h=new t(c);if(!(h instanceof n))throw new g("Sequential.fromConfig called on non-Sequential input: ".concat(h));try{for(var p=l(u),f=p.next();!f.done;f=p.next()){var v=f.value,y=zu(v,undefined,a);a&&y.setFastWeightInitDuringBuild(!0),h.add(y)}}catch(t){o={error:t}}finally{try{f&&!f.done&&(s=p.return)&&s.call(p)}finally{if(o)throw o.error}}return h},Object.defineProperty(n.prototype,"stopTraining",{get:function(){if(null==this.model)throw new d("Cannot get the stopTraining property of a sequential model before it is compiled.");return this.model.stopTraining},set:function(t){if(null==this.model)throw new d("Cannot set the stopTraining property of a sequential model before it is compiled.");this.model.stopTraining=t},enumerable:!1,configurable:!0}),n.prototype.getConfig=function(){var t,e,n=[];try{for(var r=l(this.layers),i=r.next();!i.done;i=r.next()){var a=i.value,o={};o.className=a.getClassName(),o.config=a.getConfig(),n.push(o)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(e=r.return)&&e.call(r)}finally{if(t)throw t.error}}return{name:this.name,layers:n}},n}(Il);function Tl(t){return le(t)}El.className="Sequential",e.serialization.registerClass(El);var Cl=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.getConfig=function(){return{}},e}(e.serialization.Serializable),Fl=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t,e){return void 0===e&&(e=1),function(t,e){if(void 0===e&&(e=1),1!==e)throw new g("Support for alpha values other than 1 (".concat(e,") is not implemented ")+"yet.");return i.elu(t)}(t,e)},e}(Cl);Fl.className="elu",e.serialization.registerClass(Fl);var Dl=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return i.selu(t)},e}(Cl);Dl.className="selu",e.serialization.registerClass(Dl);var _l=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return i.relu(t)},e}(Cl);_l.className="relu",e.serialization.registerClass(_l);var Ll=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.apply=function(t){return e.tidy((function(){return i.minimum(6,i.relu(t))}))},n}(Cl);Ll.className="relu6",e.serialization.registerClass(Ll);var Rl=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return t},e}(Cl);Rl.className="linear",e.serialization.registerClass(Rl);var Ol=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return i.sigmoid(t)},e}(Cl);Ol.className="sigmoid",e.serialization.registerClass(Ol);var Ml=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.apply=function(t){return function(t){return e.tidy((function(){var e=i.add(.5,i.mul(.2,t));return i.clipByValue(e,0,1)}))}(t)},n}(Cl);Ml.className="hardSigmoid",e.serialization.registerClass(Ml);var Bl=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return i.softplus(t)},e}(Cl);Bl.className="softplus",e.serialization.registerClass(Bl);var Pl=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.apply=function(t){return function(t){return e.tidy((function(){return i.div(t,i.add(i.abs(t),1))}))}(t)},n}(Cl);Pl.className="softsign",e.serialization.registerClass(Pl);var jl=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return i.tanh(t)},e}(Cl);jl.className="tanh",e.serialization.registerClass(jl);var Ul=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t,e){return void 0===e&&(e=-1),i.softmax(t,e)},e}(Cl);Ul.className="softmax",e.serialization.registerClass(Ul);var Wl=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t,e){return void 0===e&&(e=-1),i.logSoftmax(t,e)},e}(Cl);Wl.className="logSoftmax",e.serialization.registerClass(Wl);var Vl=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.apply=function(t,n){return void 0===n&&(n=1),e.tidy((function(){return i.mul(i.sigmoid(i.mul(t,n)),t)}))},n}(Cl);Vl.className="swish",e.serialization.registerClass(Vl);var ql=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.apply=function(t){return e.tidy((function(){return i.mul(t,i.tanh(i.softplus(t)))}))},n}(Cl);function Gl(t){return t.getClassName()}function Kl(t,n){return void 0===n&&(n={}),E(t,e.serialization.SerializationMap.getMap().classNameMap,n,"activation")}function Hl(t){var e;return null==t?Kl(e={className:"linear",config:{}}):"string"==typeof t?((e={}).className=t,e.config={},Kl(e)):t instanceof Cl?t:Kl(t)}function Jl(t){if(null!=t&&"object"!=typeof t)throw new Error("Argument to L1L2 regularizer's constructor is expected to be an "+"object, but received: ".concat(t))}ql.className="mish",e.serialization.registerClass(ql);var Zl=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e}(e.serialization.Serializable),Yl=function(t){function n(e){var n=t.call(this)||this;return Jl(e),n.l1=null==e||null==e.l1?.01:e.l1,n.l2=null==e||null==e.l2?.01:e.l2,n.hasL1=0!==n.l1,n.hasL2=0!==n.l2,n}return o(n,t),n.prototype.apply=function(t){var n=this;return e.tidy((function(){var r=e.zeros([1]);return n.hasL1&&(r=e.add(r,e.sum(i.mul(n.l1,e.abs(t))))),n.hasL2&&(r=e.add(r,e.sum(i.mul(n.l2,bt(t))))),i.reshape(r,[])}))},n.prototype.getConfig=function(){return{l1:this.l1,l2:this.l2}},n.fromConfig=function(t,e){return new t({l1:e.l1,l2:e.l2})},n}(Zl);Yl.className="L1L2",e.serialization.registerClass(Yl);var Xl={l1l2:"L1L2"};function $l(t){return A(t)}function Ql(t,n){return void 0===n&&(n={}),E(t,e.serialization.SerializationMap.getMap().classNameMap,n,"regularizer")}function tc(t){return null==t?null:"string"==typeof t?Ql({className:t in Xl?Xl[t]:t,config:{}}):t instanceof Zl?t:Ql(t)}var ec=function(t){function n(e){var n=t.call(this,null==e?{}:e)||this;return n.supportsMasking=!0,null!=e&&(n.maxValue=e.maxValue),n}return o(n,t),n.prototype.call=function(t,n){t=Jt(t);var r=e.relu(t);return null!=this.maxValue&&(r=e.clipByValue(r,0,this.maxValue)),r},n.prototype.computeOutputShape=function(t){return t},n.prototype.getConfig=function(){var e={maxValue:this.maxValue},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n}(oe);ec.className="ReLU",e.serialization.registerClass(ec);var nc=function(t){function n(e){var n=t.call(this,null==e?{}:e)||this;return n.DEFAULT_ALPHA=.3,null==e&&(e={}),n.alpha=null==e.alpha?n.DEFAULT_ALPHA:e.alpha,n}return o(n,t),n.prototype.call=function(t,n){var r=Jt(t);return e.leakyRelu(r,this.alpha)},n.prototype.computeOutputShape=function(t){return t},n.prototype.getConfig=function(){var e={alpha:this.alpha},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n}(oe);nc.className="LeakyReLU",e.serialization.registerClass(nc);var rc=function(t){function n(e){var n=t.call(this,null==e?{}:e)||this;if(n.DEFAULT_ALPHA_INITIALIZER="zeros",null==e&&(e={}),n.supportsMasking=!0,n.alphaInitializer=Gt(e.alphaInitializer||n.DEFAULT_ALPHA_INITIALIZER),n.alphaRegularizer=tc(e.alphaRegularizer),n.alphaConstraint=fu(e.alphaConstraint),null==e.sharedAxes)n.sharedAxes=null;else if(Array.isArray(e.sharedAxes))n.sharedAxes=e.sharedAxes;else{if("number"!=typeof e.sharedAxes)throw new d("Expected sharedAxes to be a number or an array of numbers, "+"but got ".concat(e.sharedAxes));n.sharedAxes=[e.sharedAxes]}return n}return o(n,t),n.prototype.build=function(t){var e,n,r=(t=Zt(t)).slice(1);if(null!=this.sharedAxes)try{for(var i=l(this.sharedAxes),a=i.next();!a.done;a=i.next()){r[(s=a.value)-1]=1}}catch(t){e={error:t}}finally{try{a&&!a.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}this.alpha=this.addWeight("alpha",r,"float32",this.alphaInitializer,this.alphaRegularizer,!0,this.alphaConstraint);var o={};if(null!=this.sharedAxes)for(var s=1;s1?e.exp(e.sub(i,e.logSumExp(i,r.axis,!0))):r.softmax(i,r.axis[0]):r.softmax(i,r.axis)}))},n.prototype.computeOutputShape=function(t){return t},n.prototype.getConfig=function(){var e={axis:this.axis},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n}(oe);function sc(t,e,n){if("number"==typeof t)return m(t,e);if(t.length!==e)throw new d("The ".concat(n," argument must be an integer or tuple of ").concat(e," integers.")+" Received: ".concat(t.length," elements."));for(var r=0;r 0 "+"but got ".concat(JSON.stringify(t.filters)))},n}(fc),gc=function(t){function e(n){var r=t.call(this,2,n)||this;return e.verifyArgs(n),r}return o(e,t),e.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this);return delete e.rank,e},e.verifyArgs=function(t){if("number"!=typeof t.kernelSize&&!_(t.kernelSize,"number",1,2))throw new d("Conv2D expects config.kernelSize to be number or number[] with "+"length 1 or 2, but received ".concat(JSON.stringify(t.kernelSize),"."))},e}(dc);gc.className="Conv2D",e.serialization.registerClass(gc);var vc=function(t){function e(n){var r=t.call(this,3,n)||this;return e.verifyArgs(n),r}return o(e,t),e.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this);return delete e.rank,e},e.verifyArgs=function(t){if("number"!=typeof t.kernelSize&&(!Array.isArray(t.kernelSize)||1!==t.kernelSize.length&&3!==t.kernelSize.length))throw new d("Conv3D expects config.kernelSize to be number or"+" [number, number, number], but received ".concat(JSON.stringify(t.kernelSize),"."))},e}(dc);vc.className="Conv3D",e.serialization.registerClass(vc);var yc=function(t){function e(e){var n=t.call(this,e)||this;if(n.inputSpec=[new ee({ndim:4})],"same"!==n.padding&&"valid"!==n.padding)throw new d("Conv2DTranspose currently supports only padding modes 'same' "+"and 'valid', but received padding mode ".concat(n.padding));return n}return o(e,t),e.prototype.build=function(t){var e;if(4!==(t=Zt(t)).length)throw new d("Input should have rank 4; Received input shape: "+JSON.stringify(t));var n="channelsFirst"===this.dataFormat?1:t.length-1;if(null==t[n])throw new d("The channel dimension of the inputs should be defined. Found `None`.");var r=t[n],i=this.kernelSize.concat([this.filters,r]);this.kernel=this.addWeight("kernel",i,"float32",this.kernelInitializer,this.kernelRegularizer,!0,this.kernelConstraint),this.useBias&&(this.bias=this.addWeight("bias",[this.filters],"float32",this.biasInitializer,this.biasRegularizer,!0,this.biasConstraint)),this.inputSpec=[new ee({ndim:4,axes:(e={},e[n]=r,e)})],this.built=!0},e.prototype.call=function(t,e){var n=this;return i.tidy((function(){var e=Jt(t);if(4!==e.shape.length)throw new d("Conv2DTranspose.call() expects input tensor to be rank-4, but "+"received a tensor of rank-".concat(e.shape.length));var r,a,o=e.shape,s=o[0];"channelsFirst"===n.dataFormat?(r=2,a=3):(r=1,a=2);var u=o[r],l=o[a],c=n.kernelSize[0],h=n.kernelSize[1],p=n.strides[0],f=n.strides[1],g=[s,lc(u,p,c,n.padding),lc(l,f,h,n.padding),n.filters];"channelsLast"!==n.dataFormat&&(e=i.transpose(e,[0,2,3,1]));var v=i.conv2dTranspose(e,n.kernel.read(),g,n.strides,n.padding);return"channelsLast"!==n.dataFormat&&(v=i.transpose(v,[0,3,1,2])),null!=n.bias&&(v=kt(v,n.bias.read(),n.dataFormat)),null!=n.activation&&(v=n.activation.apply(v)),v}))},e.prototype.computeOutputShape=function(t){var e,n,r,i=(t=Zt(t)).slice();"channelsFirst"===this.dataFormat?(e=1,n=2,r=3):(e=3,n=1,r=2);var a=this.kernelSize[0],o=this.kernelSize[1],s=this.strides[0],u=this.strides[1];return i[e]=this.filters,i[n]=lc(i[n],s,a,this.padding),i[r]=lc(i[r],u,o,this.padding),i},e.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this);return delete e.dilationRate,e},e}(gc);yc.className="Conv2DTranspose",e.serialization.registerClass(yc);var mc=function(t){function e(e){var n=t.call(this,e)||this;if(n.inputSpec=[new ee({ndim:5})],"same"!==n.padding&&"valid"!==n.padding)throw new d("Conv3DTranspose currently supports only padding modes 'same' "+"and 'valid', but received padding mode ".concat(n.padding));return n}return o(e,t),e.prototype.build=function(t){var e;if(5!==(t=Zt(t)).length)throw new d("Input should have rank 5; Received input shape: "+JSON.stringify(t));var n="channelsFirst"===this.dataFormat?1:t.length-1;if(null==t[n])throw new d("The channel dimension of the inputs should be defined. Found `None`.");var r=t[n],i=this.kernelSize.concat([this.filters,r]);this.kernel=this.addWeight("kernel",i,"float32",this.kernelInitializer,this.kernelRegularizer,!0,this.kernelConstraint),this.useBias&&(this.bias=this.addWeight("bias",[this.filters],"float32",this.biasInitializer,this.biasRegularizer,!0,this.biasConstraint)),this.inputSpec=[new ee({ndim:5,axes:(e={},e[n]=r,e)})],this.built=!0},e.prototype.call=function(t,e){var n=this;return i.tidy((function(){var e=Jt(t);if(5!==e.shape.length)throw new d("Conv3DTranspose.call() expects input tensor to be rank-4, but "+"received a tensor of rank-".concat(e.shape.length));var r,a,o,s=e.shape,u=s[0];"channelsFirst"===n.dataFormat?(o=2,r=3,a=4):(o=1,r=2,a=3);var l=s[o],c=s[r],h=s[a],p=n.kernelSize[0],f=n.kernelSize[1],g=n.kernelSize[2],v=n.strides[0],y=n.strides[1],m=n.strides[2],b=[u,lc(l,v,p,n.padding),lc(c,y,f,n.padding),lc(h,m,g,n.padding),n.filters];"channelsLast"!==n.dataFormat&&(e=i.transpose(e,[0,2,3,4,1]));var w=i.conv3dTranspose(e,n.kernel.read(),b,n.strides,n.padding);return"channelsLast"!==n.dataFormat&&(w=i.transpose(w,[0,4,1,2,3])),null!==n.bias&&(w=kt(w,n.bias.read(),n.dataFormat)),null!==n.activation&&(w=n.activation.apply(w)),w}))},e.prototype.computeOutputShape=function(t){var e,n,r,i,a=(t=Zt(t)).slice();"channelsFirst"===this.dataFormat?(e=1,n=2,r=3,i=4):(e=4,n=1,r=2,i=3);var o=this.kernelSize[0],s=this.kernelSize[1],u=this.kernelSize[2],l=this.strides[0],c=this.strides[1],h=this.strides[2];return a[e]=this.filters,a[n]=lc(a[n],l,o,this.padding),a[r]=lc(a[r],c,s,this.padding),a[i]=lc(a[i],h,u,this.padding),a},e.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this);return delete e.dilationRate,e},e}(vc);mc.className="Conv3DTranspose",e.serialization.registerClass(mc);var bc=function(t){function n(e,n){var r=t.call(this,e,n)||this;if(r.DEFAULT_DEPTHWISE_INITIALIZER="glorotUniform",r.DEFAULT_POINTWISE_INITIALIZER="glorotUniform",r.depthwiseKernel=null,r.pointwiseKernel=null,null==n.filters)throw new d("The `filters` configuration field is required by SeparableConv, but is unspecified.");if(null!=n.kernelInitializer||null!=n.kernelRegularizer||null!=n.kernelConstraint)throw new d("Fields kernelInitializer, kernelRegularizer and kernelConstraint are invalid for SeparableConv2D. Use depthwiseInitializer, depthwiseRegularizer, depthwiseConstraint, pointwiseInitializer, pointwiseRegularizer and pointwiseConstraint instead.");if(null!=n.padding&&"same"!==n.padding&&"valid"!==n.padding)throw new d("SeparableConv".concat(r.rank,"D supports only padding modes: ")+"'same' and 'valid', but received ".concat(JSON.stringify(n.padding)));return r.depthMultiplier=null==n.depthMultiplier?1:n.depthMultiplier,r.depthwiseInitializer=Gt(n.depthwiseInitializer||r.DEFAULT_DEPTHWISE_INITIALIZER),r.depthwiseRegularizer=tc(n.depthwiseRegularizer),r.depthwiseConstraint=fu(n.depthwiseConstraint),r.pointwiseInitializer=Gt(n.depthwiseInitializer||r.DEFAULT_POINTWISE_INITIALIZER),r.pointwiseRegularizer=tc(n.pointwiseRegularizer),r.pointwiseConstraint=fu(n.pointwiseConstraint),r}return o(n,t),n.prototype.build=function(t){var e;if((t=Zt(t)).length1&&(e=t.slice(1,t.length)),t=t[0]}function i(t){return null==t||Array.isArray(t)?t:[t]}return{inputs:t,initialState:e=i(e),constants:n=i(n)}}function Ac(t,e,n,r,a,o,s,u){return void 0===r&&(r=!1),void 0===s&&(s=!1),void 0===u&&(u=!1),i.tidy((function(){var l=e.shape.length;if(l<3)throw new d("Input should be at least 3D, but is ".concat(l,"D."));var c=[1,0].concat(ot(2,l));if(e=i.transpose(e,c),null!=o)throw new g("The rnn() functoin of the deeplearn.js backend does not support constants yet.");s&&console.warn("Backend rnn(): the unroll = true option is not applicable to the imperative deeplearn.js backend."),null!=a&&((a=i.cast(i.cast(a,"bool"),"float32")).rank===l-1&&(a=i.expandDims(a,-1)),a=i.transpose(a,c)),r&&(e=i.reverse(e,0),null!=a&&(a=i.reverse(a,0)));var h,p,f=[],v=n,y=e.shape[0],m=i.unstack(e);null!=a&&(p=i.unstack(a));for(var b,w=function(e){var n=m[e],r=i.tidy((function(){return t(n,v)}));if(null==a)h=r[0],v=r[1];else{var o=i.tidy((function(){var t=p[e],n=i.sub(i.onesLike(t),t);return{output:i.add(i.mul(r[0],t),i.mul(v[0],n)),newStates:v.map((function(e,a){return i.add(i.mul(r[1][a],t),i.mul(e,n))}))}}));h=o.output,v=o.newStates}u&&f.push(h)},k=0;k1?gt(e,[1,t]):e})):n.cell.stateSize>1?[gt(e,[1,n.cell.stateSize])]:[e]}))},Object.defineProperty(n.prototype,"trainableWeights",{get:function(){return this.trainable?this.cell.trainableWeights:[]},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"nonTrainableWeights",{get:function(){return this.trainable?this.cell.nonTrainableWeights:this.cell.weights},enumerable:!1,configurable:!0}),n.prototype.setFastWeightInitDuringBuild=function(e){t.prototype.setFastWeightInitDuringBuild.call(this,e),null!=this.cell&&this.cell.setFastWeightInitDuringBuild(e)},n.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this),r={returnSequences:this.returnSequences,returnState:this.returnState,goBackwards:this.goBackwards,stateful:this.stateful,unroll:this.unroll};null!=this.numConstants&&(r.numConstants=this.numConstants);var i=this.cell.getConfig();return this.getClassName()===n.className&&(r.cell={className:this.cell.getClassName(),config:i}),Object.assign(Object.assign(Object.assign({},i),e),r)},n.fromConfig=function(t,e,n){void 0===n&&(n={});var r=zu(e.cell,n);return new t(Object.assign(e,{cell:r}))},n}(oe);zc.className="RNN",e.serialization.registerClass(zc);var Ec=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e}(oe),Tc=function(t){function n(e){var n=t.call(this,e)||this;return n.DEFAULT_ACTIVATION="tanh",n.DEFAULT_KERNEL_INITIALIZER="glorotNormal",n.DEFAULT_RECURRENT_INITIALIZER="orthogonal",n.DEFAULT_BIAS_INITIALIZER="zeros",n.units=e.units,L(n.units,"units"),n.activation=Hl(null==e.activation?n.DEFAULT_ACTIVATION:e.activation),n.useBias=null==e.useBias||e.useBias,n.kernelInitializer=Gt(e.kernelInitializer||n.DEFAULT_KERNEL_INITIALIZER),n.recurrentInitializer=Gt(e.recurrentInitializer||n.DEFAULT_RECURRENT_INITIALIZER),n.biasInitializer=Gt(e.biasInitializer||n.DEFAULT_BIAS_INITIALIZER),n.kernelRegularizer=tc(e.kernelRegularizer),n.recurrentRegularizer=tc(e.recurrentRegularizer),n.biasRegularizer=tc(e.biasRegularizer),n.kernelConstraint=fu(e.kernelConstraint),n.recurrentConstraint=fu(e.recurrentConstraint),n.biasConstraint=fu(e.biasConstraint),n.dropout=it([1,at([0,null==e.dropout?0:e.dropout])]),n.recurrentDropout=it([1,at([0,null==e.recurrentDropout?0:e.recurrentDropout])]),n.dropoutFunc=e.dropoutFunc,n.stateSize=n.units,n.dropoutMask=null,n.recurrentDropoutMask=null,n}return o(n,t),n.prototype.build=function(t){t=Zt(t),this.kernel=this.addWeight("kernel",[t[t.length-1],this.units],null,this.kernelInitializer,this.kernelRegularizer,!0,this.kernelConstraint),this.recurrentKernel=this.addWeight("recurrent_kernel",[this.units,this.units],null,this.recurrentInitializer,this.recurrentRegularizer,!0,this.recurrentConstraint),this.useBias?this.bias=this.addWeight("bias",[this.units],null,this.biasInitializer,this.biasRegularizer,!0,this.biasConstraint):this.bias=null,this.built=!0},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){if(2!==t.length)throw new d("SimpleRNNCell expects 2 input Tensors, got ".concat(t.length,"."));var e=t[1];t=t[0];var a,o=null!=n.training&&n.training;01){for(var o=[0],s=2;s1)throw new d("Can not merge tensors with different batch sizes. "+"Got tensors with shapes: ".concat(JSON.stringify(t),"."));for(var o=null==t[0]?null:t[0].slice(1),s=1;s1){var S=ot(1,f).concat([0]);s.push(i.transpose(m,S)),g=!0}else s.push(m)}}catch(t){a={error:t}}finally{try{y&&!y.done&&(o=v.return)&&o.call(v)}finally{if(a)throw a.error}}var N=r.mergeFunction(s),I=N.rank;if(g)if(null==I){var A=N.shape;k=[w=A[A.length-1]].concat(A.slice(0,A.length-1));N=i.reshape(i.transpose(i.reshape(N,[-1,w]),[1,0]),k)}else if(I>1){S=[I-1].concat(ot(0,I-1));N=i.transpose(N,S)}return N}return r.mergeFunction(t)}))},n.prototype.computeOutputShape=function(t){var e,n,r;r=null==t[0]?null:t[0].slice(1);for(var i=1;i1)throw new d("A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got input shapes: "+JSON.stringify(t))}},n.prototype.mergeFunction=function(t){var n=this;return e.tidy((function(){return ft(t,n.axis)}))},n.prototype.computeOutputShape=function(t){var e,n;if(!Array.isArray(t)||!Array.isArray(t[0]))throw new d("A `Concatenate` layer should be called on a list of inputs.");var r=t,i=r[0].slice(),a=this.axis<0?i.length+this.axis:this.axis;try{for(var o=l(r.slice(1)),s=o.next();!s.done;s=o.next()){var u=s.value;if(null==i[a]||null==u[a]){i[a]=null;break}i[a]+=u[a]}}catch(t){e={error:t}}finally{try{s&&!s.done&&(n=o.return)&&n.call(o)}finally{if(e)throw e.error}}return i},n.prototype.computeMask=function(t,e){var n=this;if(null==e)return null;if(!Array.isArray(e))throw new d("`mask` should be an array for Concatenate");if(!Array.isArray(t))throw new d("`inputs` should be an array for Concatenate");if(e.length!==t.length)throw new d("Mismatch in the length of mask (".concat(e.length,") ")+"and the legnth of inputs (".concat(t.length,")"));return i.tidy((function(){var r=!0;if(e.forEach((function(t){null==t||(r=!1)})),r)return null;for(var a=[],o=0;o3||n.length>3)throw new g("Dot layer does not support tensors of 4D or higher rank yet.");var r=this.interpretAxes(e,n);if(e[r[0]]!==n[r[1]])throw new d("Dimension incompatibility: "+"".concat(e[r[0]]," !== ").concat(n[r[1]]))},e.prototype.mergeFunction=function(t){if(2!==t.length)throw new d("A `Dot` layer must be called on exactly 2 inputs, "+"but received ".concat(t.length," input(s)."));var e,n=t[0],r=t[1];return e=Array.isArray(this.axes)?this.axes.map((function(e,n){return ih(e,t[n].shape.length)})):[ih(this.axes,n.shape.length),ih(this.axes,r.shape.length)],this.normalize&&(n=Eu(n,e[0]),r=Eu(r,e[1])),function(t,e,n){if(t.shape.length>3||e.shape.length>3)throw new g("batchDot is not implemented for tensors of 4D or higher rank yet");if(i.util.assert(t.shape.length>=2,(function(){return"batchDot requires the rank of x to be >= 2, "+"but got ".concat(t.shape.length)})),i.util.assert(t.shape.length>=2,(function(){return"batchDot requires the rank of y to be >= 2, "+"but got ".concat(e.shape.length)})),"number"==typeof n&&(n=[n,n]),"complex64"===t.dtype||"complex64"===e.dtype)throw new g("batchDot is not implemented for complex64-type Tensors yet.");var r=t.shape.length,a=e.shape.length;null==n&&(n=[r-1,a-2]);var o=n;return i.tidy((function(){var n,s;if(r>a){n=r-a;for(var u=[],l=0;lr){for(n=a-r,u=[],l=0;l0){var p=void 0,f=[];for(l=p=r>a?r+a-3:r-1;l3||n.length>3)throw new g("Dot layer does not support tensors of 4D or higher rank yet.");var r=this.interpretAxes(e,n);e.splice(r[0],1),n.splice(r[1],1),n.splice(0,1);var a=e.concat(n);return 1===a.length&&a.push(1),a},e.prototype.computeMask=function(t,e){return null},e.prototype.getConfig=function(){var e={axes:this.axes,normalize:this.normalize},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},e}(Xc);ah.className="Dot",e.serialization.registerClass(ah);var oh=function(t){function n(e){var n=t.call(this,e)||this;return n.supportsMasking=!0,n.stddev=e.stddev,n}return o(n,t),n.prototype.computeOutputShape=function(t){return t},n.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this),n={stddev:this.stddev};return Object.assign(n,e),n},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){r.invokeCallHook(t,n);var i=Jt(t);return St((function(){return e.add(vt(i.shape,0,r.stddev),i)}),(function(){return i}),n.training||!1)}))},n}(oe);oh.className="GaussianNoise",e.serialization.registerClass(oh);var sh=function(t){function n(e){var n=t.call(this,e)||this;return n.supportsMasking=!0,n.rate=e.rate,n}return o(n,t),n.prototype.computeOutputShape=function(t){return t},n.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this),n={rate:this.rate};return Object.assign(n,e),n},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){r.invokeCallHook(t,n);var i=Jt(t);if(r.rate>0&&r.rate<1){return St((function(){var t=Math.sqrt(r.rate/(1-r.rate));return e.mul(i,vt(i.shape,1,t))}),(function(){return i}),n.training||!1)}return i}))},n}(oe);sh.className="GaussianDropout",e.serialization.registerClass(sh);var uh=function(t){function n(e){var n=t.call(this,e)||this;return n.supportsMasking=!0,n.rate=e.rate,n.noiseShape=e.noiseShape,n}return o(n,t),n.prototype._getNoiseShape=function(t){return this.noiseShape||Jt(t).shape},n.prototype.computeOutputShape=function(t){return t},n.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this),n={rate:this.rate};return Object.assign(n,e),n},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){if(r.rate<1&&r.rate>0){var i=r._getNoiseShape(t);return St((function(){var n=Jt(t),a=-1.7580993408473766,o=e.greaterEqual(e.randomUniform(i),r.rate);o=ut(o,"float32");var s=Math.pow((1-r.rate)*(1+r.rate*Math.pow(a,2)),-.5),u=-s*a*r.rate,l=e.add(e.mul(n,o),e.mul(e.add(o,-1),a));return e.add(e.mul(l,s),u)}),(function(){return Jt(t)}),n.training||!1)}return t}))},n}(oe);function lh(t,e,n,r,a,o){var s;if(void 0===o&&(o=.001),2===t.rank)s=i.batchNorm2d(t,e,n,r,a,o);else if(3===t.rank)s=i.batchNorm3d(t,e,n,r,a,o);else{if(4!==t.rank)throw new g("batchNormalization is not implemented for array of rank ".concat(t.rank," ")+"yet");s=i.batchNorm4d(t,e,n,r,a,o)}return s}function ch(t,n,r,a,o){return void 0===o&&(o=.001),e.util.arraysEqual(a.slice().sort(),ot(0,t.rank-1))?function(t,n,r,a,o){return void 0===o&&(o=.001),e.tidy((function(){var e=i.moments(t,a),s=e.mean,u=e.variance;return[lh(t,s,u,r,n,o),s,u]}))}(t,n,r,a,o):function(t,n,r,a,o){return void 0===o&&(o=.001),e.tidy((function(){var s,u,c=i.moments(t,a),h=c.mean,p=c.variance,f=[];try{for(var d=l(ot(0,t.rank)),g=d.next();!g.done;g=d.next()){var v=g.value;-1!==a.indexOf(v)?f.push(1):f.push(t.shape[v])}}catch(t){s={error:t}}finally{try{g&&!g.done&&(u=d.return)&&u.call(d)}finally{if(s)throw s.error}}var y=e.reshape(h,f),m=e.reshape(p,f),b=null==n?null:e.reshape(n,f),w=null==r?null:e.reshape(r,f);return[lh(t,y,m,w,b,o),h,p]}))}(t,n,r,a,o)}uh.className="AlphaDropout",e.serialization.registerClass(uh);var hh=function(t){function n(e){var n=this;return null==e&&(e={}),(n=t.call(this,e)||this).supportsMasking=!0,n.axis=null==e.axis?-1:e.axis,n.momentum=null==e.momentum?.99:e.momentum,n.epsilon=null==e.epsilon?.001:e.epsilon,n.center=null==e.center||e.center,n.scale=null==e.scale||e.scale,n.betaInitializer=Gt(e.betaInitializer||"zeros"),n.gammaInitializer=Gt(e.gammaInitializer||"ones"),n.movingMeanInitializer=Gt(e.movingMeanInitializer||"zeros"),n.movingVarianceInitializer=Gt(e.movingVarianceInitializer||"ones"),n.betaConstraint=fu(e.betaConstraint),n.gammaConstraint=fu(e.gammaConstraint),n.betaRegularizer=tc(e.betaRegularizer),n.gammaRegularizer=tc(e.gammaRegularizer),n}return o(n,t),n.prototype.build=function(t){var e;t=Zt(t);var n=this.axis>=0?this.axis:this.axis+t.length,r=t[n];if(null==r)throw new d("Axis ".concat(n," of input tensor should have a defined dimension but ")+"the layer received an input with shape "+"".concat(JSON.stringify(t),"."));this.inputSpec=[new ee({ndim:t.length,axes:(e={},e[n]=r,e)})];var i=[r];this.scale&&(this.gamma=this.addWeight("gamma",i,null,this.gammaInitializer,this.gammaRegularizer,!0,this.gammaConstraint)),this.center&&(this.beta=this.addWeight("beta",i,null,this.betaInitializer,this.betaRegularizer,!0,this.betaConstraint)),this.movingMean=this.addWeight("moving_mean",i,null,this.movingMeanInitializer,null,!1),this.movingVariance=this.addWeight("moving_variance",i,null,this.movingVarianceInitializer,null,!1),this.built=!0},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){var a=null!=n.training&&n.training,o=Jt(t),s=o.shape,u=s.length,l=ot(0,u),h=r.axis>=0?r.axis:r.axis+u;l.splice(h,1);var p=m(1,u);p[h]=s[h];var f=l.slice();f.sort();var d=!e.util.arraysEqual(f,ot(0,u).slice(0,u-1));if(!a)return function(){if(d){var t=e.reshape(r.movingMean.read(),p),n=e.reshape(r.movingVariance.read(),p),i=r.center?e.reshape(r.beta.read(),p):null,a=r.scale?e.reshape(r.gamma.read(),p):null;return lh(o,t,n,i,a,r.epsilon)}return lh(o,r.movingMean.read(),r.movingVariance.read(),null==r.beta?null:r.beta.read(),null==r.gamma?null:r.gamma.read(),r.epsilon)}();var g=c(ch(o,r.gamma.read(),r.beta.read(),l,r.epsilon),3),v=g[0],y=g[1],b=g[2],w=function(t,e,n){i.tidy((function(){var r=1-n,a=t.read(),o=i.mul(i.sub(a,e),r);t.write(i.sub(a,o))}))};return w(r.movingMean,y,r.momentum),w(r.movingVariance,b,r.momentum),v}))},n.prototype.getConfig=function(){var e={axis:this.axis,momentum:this.momentum,epsilon:this.epsilon,center:this.center,scale:this.scale,betaInitializer:qt(this.betaInitializer),gammaInitializer:qt(this.gammaInitializer),movingMeanInitializer:qt(this.movingMeanInitializer),movingVarianceInitializer:qt(this.movingVarianceInitializer),betaRegularizer:$l(this.betaRegularizer),gammaRegularizer:$l(this.gammaRegularizer),betaConstraint:hu(this.betaConstraint),gammaConstraint:hu(this.gammaConstraint)},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n}(oe);hh.className="BatchNormalization",e.serialization.registerClass(hh);var ph=function(t){function n(e){var n,r,i=this;if(null==e&&(e={}),(i=t.call(this,e)||this).axis=null==e.axis?-1:e.axis,"number"==typeof i.axis){if(!Number.isInteger(i.axis))throw new Error("Expected axis to be an integer, but received ".concat(i.axis))}else{if(!Array.isArray(i.axis))throw new Error("Expected axis to be an integer or an array of integers, "+"but received ".concat(JSON.stringify(i.axis)));try{for(var a=l(i.axis),o=a.next();!o.done;o=a.next()){var s=o.value;if(!Number.isInteger(s))throw new Error("Expected axis to be an array of integers, "+"but received ".concat(JSON.stringify(i.axis)))}}catch(t){n={error:t}}finally{try{o&&!o.done&&(r=a.return)&&r.call(a)}finally{if(n)throw n.error}}}return i.epsilon=null==e.epsilon?.001:e.epsilon,i.center=null==e.center||e.center,i.scale=null==e.scale||e.scale,i.betaInitializer=Gt(e.betaInitializer||"zeros"),i.gammaInitializer=Gt(e.gammaInitializer||"ones"),i.betaRegularizer=tc(e.betaRegularizer),i.gammaRegularizer=tc(e.gammaRegularizer),i.supportsMasking=!0,i}return o(n,t),n.prototype.build=function(t){var e,n,r=(t=Zt(t)).length;"number"==typeof this.axis&&(this.axis=[this.axis]);for(var i=0;i=r)throw new Error("Invalid axis: ".concat(s))}}catch(t){e={error:t}}finally{try{o&&!o.done&&(n=a.return)&&n.call(a)}finally{if(e)throw e.error}}if(this.axis.length!==C(this.axis).length)throw new Error("Found duplicate axes in: ".concat(this.axis));var u=this.axis.map((function(e){return t[e]}));this.scale?this.gamma=this.addWeight("gamma",u,"float32",this.gammaInitializer,this.gammaRegularizer,true):this.gamma=null,this.center?this.beta=this.addWeight("beta",u,"float32",this.betaInitializer,this.betaRegularizer,true):this.beta=null,this.built=!0},n.prototype.call=function(t,n){var r=this,a=Jt(t),o=a.shape,s=o.length;return e.tidy((function(){var t,n,u=e.moments(a,r.axis,!0),c=u.mean,h=u.variance,p=m(1,s);try{for(var f=l(r.axis),d=f.next();!d.done;d=f.next()){var g=d.value;p[g]=o[g]}}catch(e){t={error:e}}finally{try{d&&!d.done&&(n=f.return)&&n.call(f)}finally{if(t)throw t.error}}for(var v=function(t){return null!=t&&t.shape.length!==s?i.reshape(t,p):t},y=r.scale?v(r.gamma.read()):null,b=r.center?v(r.beta.read()):null,w=[],k=[],x=0;x=0?t[2]+this.padding[0][0]+this.padding[0][1]:null,n=null!=t[3]&&t[3]>=0?t[3]+this.padding[1][0]+this.padding[1][1]:null,[t[0],t[1],e,n]):(e=null!=t[1]&&t[1]>=0?t[1]+this.padding[0][0]+this.padding[0][1]:null,n=null!=t[2]&&t[2]>=0?t[2]+this.padding[1][0]+this.padding[1][1]:null,[t[0],e,n,t[3]])},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){return n=Jt(t),a=r.padding,o=r.dataFormat,e.tidy((function(){if(4!==n.rank)throw new d("temporalPadding expects input tensor to be 4-D, but received a "+"".concat(n.rank,"-D tensor."));if(null==a&&(a=[[1,1],[1,1]]),2!==a.length||2!==a[0].length||2!==a[1].length)throw new d("spatial2dPadding expects `padding` to be an Array of two Arrays, each of which is an Array of two integers.");if(null==o&&(o="channelsLast"),"channelsLast"!==o&&"channelsFirst"!==o)throw new d("Unknown data format: ".concat(o,". ")+"Supported data formats are 'channelsLast' and 'channelsFirst.");var t;return t="channelsFirst"===o?[[0,0],[0,0],a[0],a[1]]:[[0,0],a[0],a[1],[0,0]],i.pad(n,t)}));var n,a,o}))},n.prototype.getConfig=function(){var e={padding:this.padding,dataFormat:this.dataFormat},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n}(oe);function dh(t,n,r,a,o,s){return e.tidy((function(){var e;H(o),Z(s),J(a),null==r&&(r=[1,1]),null==a&&(a="valid"),null==o&&(o="channelsLast"),null==s&&(s="max"),t=cc(t,o);var u="same"===a?"same":"valid";return e="max"===s?i.maxPool(t,n,r,u):i.avgPool(t,n,r,u),"channelsFirst"===o&&(e=i.transpose(e,[0,3,1,2])),e}))}function gh(t,n,r,a,o,s){return e.tidy((function(){var e;H(o),Z(s),J(a),null==r&&(r=[1,1,1]),null==a&&(a="valid"),null==o&&(o="channelsLast"),null==s&&(s="max"),t=hc(t,o);var u="same"===a?"same":"valid";return e="max"===s?i.maxPool3d(t,n,r,u):i.avgPool3d(t,n,r,u),"channelsFirst"===o&&(e=i.transpose(e,[0,4,1,2,3])),e}))}fh.className="ZeroPadding2D",e.serialization.registerClass(fh);var vh=function(t){function n(e){var n=this;if(null==e.poolSize&&(e.poolSize=2),n=t.call(this,e)||this,"number"==typeof e.poolSize)n.poolSize=[e.poolSize];else{if(!Array.isArray(e.poolSize)||1!==e.poolSize.length||"number"!=typeof e.poolSize[0])throw new d("poolSize for 1D convolutional layer must be a number or an Array of a single number, but received "+"".concat(JSON.stringify(e.poolSize)));n.poolSize=e.poolSize}if(L(n.poolSize,"poolSize"),null==e.strides)n.strides=n.poolSize;else if("number"==typeof e.strides)n.strides=[e.strides];else{if(!Array.isArray(e.strides)||1!==e.strides.length||"number"!=typeof e.strides[0])throw new d("strides for 1D convolutional layer must be a number or an Array of a single number, but received "+"".concat(JSON.stringify(e.strides)));n.strides=e.strides}return L(n.strides,"strides"),n.padding=null==e.padding?"valid":e.padding,J(n.padding),n.inputSpec=[new ee({ndim:3})],n}return o(n,t),n.prototype.computeOutputShape=function(t){var e=uc((t=Zt(t))[1],this.poolSize[0],this.padding,this.strides[0]);return[t[0],e,t[2]]},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){r.invokeCallHook(t,n),t=lt(Jt(t),2);var e=r.poolingFunction(Jt(t),[r.poolSize[0],1],[r.strides[0],1],r.padding,"channelsLast");return i.squeeze(e,[2])}))},n.prototype.getConfig=function(){var e={poolSize:this.poolSize,padding:this.padding,strides:this.strides},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n}(oe),yh=function(t){function e(e){return t.call(this,e)||this}return o(e,t),e.prototype.poolingFunction=function(t,e,n,r,i){return H(i),J(r),dh(t,e,n,r,i,"max")},e}(vh);yh.className="MaxPooling1D",e.serialization.registerClass(yh);var mh=function(t){function e(e){return t.call(this,e)||this}return o(e,t),e.prototype.poolingFunction=function(t,e,n,r,i){return H(i),J(r),dh(t,e,n,r,i,"avg")},e}(vh);mh.className="AveragePooling1D",e.serialization.registerClass(mh);var bh=function(t){function n(e){var n=this;if(null==e.poolSize&&(e.poolSize=[2,2]),(n=t.call(this,e)||this).poolSize=Array.isArray(e.poolSize)?e.poolSize:[e.poolSize,e.poolSize],null==e.strides)n.strides=n.poolSize;else if(Array.isArray(e.strides)){if(2!==e.strides.length)throw new d("If the strides property of a 2D pooling layer is an Array, it is expected to have a length of 2, but received length "+"".concat(e.strides.length,"."));n.strides=e.strides}else n.strides=[e.strides,e.strides];return L(n.poolSize,"poolSize"),L(n.strides,"strides"),n.padding=null==e.padding?"valid":e.padding,n.dataFormat=null==e.dataFormat?"channelsLast":e.dataFormat,H(n.dataFormat),J(n.padding),n.inputSpec=[new ee({ndim:4})],n}return o(n,t),n.prototype.computeOutputShape=function(t){t=Zt(t);var e="channelsFirst"===this.dataFormat?t[2]:t[1],n="channelsFirst"===this.dataFormat?t[3]:t[2];return e=uc(e,this.poolSize[0],this.padding,this.strides[0]),n=uc(n,this.poolSize[1],this.padding,this.strides[1]),"channelsFirst"===this.dataFormat?[t[0],t[1],e,n]:[t[0],e,n,t[3]]},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){return r.invokeCallHook(t,n),r.poolingFunction(Jt(t),r.poolSize,r.strides,r.padding,r.dataFormat)}))},n.prototype.getConfig=function(){var e={poolSize:this.poolSize,padding:this.padding,strides:this.strides,dataFormat:this.dataFormat},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n}(oe),wh=function(t){function e(e){return t.call(this,e)||this}return o(e,t),e.prototype.poolingFunction=function(t,e,n,r,i){return H(i),J(r),dh(t,e,n,r,i,"max")},e}(bh);wh.className="MaxPooling2D",e.serialization.registerClass(wh);var kh=function(t){function e(e){return t.call(this,e)||this}return o(e,t),e.prototype.poolingFunction=function(t,e,n,r,i){return H(i),J(r),dh(t,e,n,r,i,"avg")},e}(bh);kh.className="AveragePooling2D",e.serialization.registerClass(kh);var xh=function(t){function n(e){var n=this;if(null==e.poolSize&&(e.poolSize=[2,2,2]),(n=t.call(this,e)||this).poolSize=Array.isArray(e.poolSize)?e.poolSize:[e.poolSize,e.poolSize,e.poolSize],null==e.strides)n.strides=n.poolSize;else if(Array.isArray(e.strides)){if(3!==e.strides.length)throw new d("If the strides property of a 3D pooling layer is an Array, it is expected to have a length of 3, but received length "+"".concat(e.strides.length,"."));n.strides=e.strides}else n.strides=[e.strides,e.strides,e.strides];return L(n.poolSize,"poolSize"),L(n.strides,"strides"),n.padding=null==e.padding?"valid":e.padding,n.dataFormat=null==e.dataFormat?"channelsLast":e.dataFormat,H(n.dataFormat),J(n.padding),n.inputSpec=[new ee({ndim:5})],n}return o(n,t),n.prototype.computeOutputShape=function(t){t=Zt(t);var e="channelsFirst"===this.dataFormat?t[2]:t[1],n="channelsFirst"===this.dataFormat?t[3]:t[2],r="channelsFirst"===this.dataFormat?t[4]:t[3];return e=uc(e,this.poolSize[0],this.padding,this.strides[0]),n=uc(n,this.poolSize[1],this.padding,this.strides[1]),r=uc(r,this.poolSize[2],this.padding,this.strides[2]),"channelsFirst"===this.dataFormat?[t[0],t[1],e,n,r]:[t[0],e,n,r,t[4]]},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){return r.invokeCallHook(t,n),r.poolingFunction(Jt(t),r.poolSize,r.strides,r.padding,r.dataFormat)}))},n.prototype.getConfig=function(){var e={poolSize:this.poolSize,padding:this.padding,strides:this.strides,dataFormat:this.dataFormat},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n}(oe),Sh=function(t){function e(e){return t.call(this,e)||this}return o(e,t),e.prototype.poolingFunction=function(t,e,n,r,i){return H(i),J(r),gh(t,e,n,r,i,"max")},e}(xh);Sh.className="MaxPooling3D",e.serialization.registerClass(Sh);var Nh=function(t){function e(e){return t.call(this,e)||this}return o(e,t),e.prototype.poolingFunction=function(t,e,n,r,i){return H(i),J(r),gh(t,e,n,r,i,"avg")},e}(xh);Nh.className="AveragePooling3D",e.serialization.registerClass(Nh);var Ih=function(t){function e(e){var n=t.call(this,e)||this;return n.inputSpec=[new ee({ndim:3})],n}return o(e,t),e.prototype.computeOutputShape=function(t){return[t[0],t[2]]},e.prototype.call=function(t,e){throw new g},e}(oe),Ah=function(t){function n(e){return t.call(this,e||{})||this}return o(n,t),n.prototype.call=function(t,n){return e.tidy((function(){var e=Jt(t);return i.mean(e,1)}))},n}(Ih);Ah.className="GlobalAveragePooling1D",e.serialization.registerClass(Ah);var zh=function(t){function n(e){return t.call(this,e||{})||this}return o(n,t),n.prototype.call=function(t,n){return e.tidy((function(){var e=Jt(t);return i.max(e,1)}))},n}(Ih);zh.className="GlobalMaxPooling1D",e.serialization.registerClass(zh);var Eh=function(t){function e(e){var n=t.call(this,e)||this;return n.dataFormat=null==e.dataFormat?"channelsLast":e.dataFormat,H(n.dataFormat),n.inputSpec=[new ee({ndim:4})],n}return o(e,t),e.prototype.computeOutputShape=function(t){return"channelsLast"===this.dataFormat?[t[0],t[3]]:[t[0],t[1]]},e.prototype.call=function(t,e){throw new g},e.prototype.getConfig=function(){var e={dataFormat:this.dataFormat},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},e}(oe),Th=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.call=function(t,n){var r=this;return e.tidy((function(){var e=Jt(t);return"channelsLast"===r.dataFormat?i.mean(e,[1,2]):i.mean(e,[2,3])}))},n}(Eh);Th.className="GlobalAveragePooling2D",e.serialization.registerClass(Th);var Ch=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.call=function(t,n){var r=this;return e.tidy((function(){var e=Jt(t);return"channelsLast"===r.dataFormat?i.max(e,[1,2]):i.max(e,[2,3])}))},n}(Eh);Ch.className="GlobalMaxPooling2D",e.serialization.registerClass(Ch);var Fh=function(t){function e(e){var n=t.call(this,e)||this;return n.layer=e.layer,n}return o(e,t),e.prototype.build=function(t){this.built=!0},Object.defineProperty(e.prototype,"trainable",{get:function(){return null!=this.layer&&this.layer.trainable},set:function(t){null!=this.layer&&(this.layer.trainable=t)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"trainableWeights",{get:function(){return this.layer.trainableWeights},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"nonTrainableWeights",{get:function(){return this.layer.nonTrainableWeights},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"updates",{get:function(){return this.layer._updates},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"losses",{get:function(){return this.layer.losses},enumerable:!1,configurable:!0}),e.prototype.getWeights=function(){return this.layer.getWeights()},e.prototype.setWeights=function(t){this.layer.setWeights(t)},e.prototype.getConfig=function(){var e={layer:{className:this.layer.getClassName(),config:this.layer.getConfig()}},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},e.prototype.setFastWeightInitDuringBuild=function(e){t.prototype.setFastWeightInitDuringBuild.call(this,e),null!=this.layer&&this.layer.setFastWeightInitDuringBuild(e)},e.fromConfig=function(t,e,n){void 0===n&&(n={});var r=zu(e.layer,n);delete e.layer;var i={layer:r};return Object.assign(i,e),new t(i)},e}(oe),Dh=function(t){function n(e){var n=t.call(this,e)||this;return n.supportsMasking=!0,n}return o(n,t),n.prototype.build=function(e){if((e=Zt(e)).length<3)throw new d("TimeDistributed layer expects an input shape >= 3D, but received "+"input shape ".concat(JSON.stringify(e)));this.inputSpec=[{shape:e}];var n=[e[0]].concat(e.slice(2));this.layer.built||(this.layer.build(n),this.layer.built=!0),t.prototype.build.call(this,e)},n.prototype.computeOutputShape=function(t){var e=[(t=Zt(t))[0]].concat(t.slice(2)),n=this.layer.computeOutputShape(e),r=t[1];return[n[0],r].concat(n.slice(1))},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){return Ac((function(t,e){return[Jt(r.layer.call(t,n)),[]]}),t=Jt(t),[],!1,null,null,!1,!0)[1]}))},n}(Fh);Dh.className="TimeDistributed",e.serialization.registerClass(Dh);var _h=function(t){function n(e){var n=t.call(this,e)||this,r=e.layer.getConfig(),i={};i.className=e.layer.getClassName(),i.config=r,n.forwardLayer=zu(i),r.goBackwards=!0!==r.goBackwards;var a,o={};if(o.className=e.layer.getClassName(),o.config=r,n.backwardLayer=zu(o),n.forwardLayer.name="forward_"+n.forwardLayer.name,n.backwardLayer.name="backward_"+n.backwardLayer.name,n.mergeMode=void 0===e.mergeMode?"concat":e.mergeMode,a=n.mergeMode,D(G,"BidirectionalMergeMode",a),e.weights)throw new g("weights support is not implemented for Bidirectional layer yet.");return n._stateful=e.layer.stateful,n.returnSequences=e.layer.returnSequences,n.returnState=e.layer.returnState,n.supportsMasking=!0,n._trainable=!0,n.inputSpec=e.layer.inputSpec,n.numConstants=null,n}return o(n,t),Object.defineProperty(n.prototype,"trainable",{get:function(){return this._trainable},set:function(t){this._trainable=t,null!=this.forwardLayer&&(this.forwardLayer.trainable=t),null!=this.backwardLayer&&(this.backwardLayer.trainable=t)},enumerable:!1,configurable:!0}),n.prototype.getWeights=function(){return this.forwardLayer.getWeights().concat(this.backwardLayer.getWeights())},n.prototype.setWeights=function(t){var e=t.length,n=Math.floor(e/2);this.forwardLayer.setWeights(t.slice(0,n)),this.backwardLayer.setWeights(t.slice(n))},n.prototype.computeOutputShape=function(t){var e,n,r,i=this.forwardLayer.computeOutputShape(t);return Array.isArray(i)&&Array.isArray(i[0])||(i=[i]),this.returnState?(r=i.slice(1),e=i[0]):e=i[0],"concat"===this.mergeMode?(e[e.length-1]*=2,n=[e]):n=null==this.mergeMode?[e,e.slice()]:[e],this.returnState?null==this.mergeMode?n.concat(r).concat(r.slice()):[e].concat(r).concat(r.slice()):k(n)},n.prototype.apply=function(e,n){var r,i,a=null==n?null:n.initialState,o=null==n?null:n.constants;null==n&&(n={});var s=Ic(e,a,o,this.numConstants);if(e=s.inputs,a=s.initialState,o=s.constants,Array.isArray(e)&&(a=e.slice(1),e=e[0]),(null==a||0===a.length)&&null==o)return t.prototype.apply.call(this,e,n);var u=[],p=[];if(null!=a){var f=a.length;if(f%2>0)throw new d("When passing `initialState` to a Bidrectional RNN, the state should be an Array containing the states of the underlying RNNs.");n.initialState=a,u.push.apply(u,h([],c(a),!1));var v=a.map((function(t){return new ee({shape:t.shape})}));this.forwardLayer.stateSpec=v.slice(0,f/2),this.backwardLayer.stateSpec=v.slice(f/2),p.push.apply(p,h([],c(v),!1))}if(null!=o)throw new g("Support for constants in Bidirectional layers is not implemented yet.");var y=u[0]instanceof ne;try{for(var m=l(u),b=m.next();!b.done;b=m.next()){if(b.value instanceof ne!==y)throw new d("The initial state of a Bidirectional layer cannot be specified as a mix of symbolic and non-symbolic tensors")}}catch(t){r={error:t}}finally{try{b&&!b.done&&(i=m.return)&&i.call(m)}finally{if(r)throw r.error}}if(y){var w=[e].concat(u),k=this.inputSpec.concat(p),x=this.inputSpec;this.inputSpec=k;var S=t.prototype.apply.call(this,w,n);return this.inputSpec=x,S}return t.prototype.apply.call(this,e,n)},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){var e,a,o,s,u=n.initialState;if(null==u)e=r.forwardLayer.call(t,n),a=r.backwardLayer.call(t,n);else{var l=u.slice(0,u.length/2),c=u.slice(u.length/2);e=r.forwardLayer.call(t,Object.assign(n,{initialState:l})),a=r.backwardLayer.call(t,Object.assign(n,{initialState:c}))}return r.returnState&&(Array.isArray(e)&&(o=e.slice(1).concat(a.slice(1))),e=e[0],a=a[0]),r.returnSequences&&(a=i.reverse(a,1)),"concat"===r.mergeMode?s=ft([e,a]):"sum"===r.mergeMode?s=i.add(e,a):"ave"===r.mergeMode?s=i.mul(.5,i.add(e,a)):"mul"===r.mergeMode?s=i.mul(e,a):null==r.mergeMode&&(s=[e,a]),r.returnState?null==r.mergeMode?s.concat(o):[s].concat(o):s}))},n.prototype.resetStates=function(t){this.forwardLayer.resetStates(),this.backwardLayer.resetStates()},n.prototype.build=function(t){var e=this;X(this.forwardLayer.name,(function(){e.forwardLayer.build(t)})),X(this.backwardLayer.name,(function(){e.backwardLayer.build(t)})),this.built=!0},n.prototype.computeMask=function(t,e){var n;if(Array.isArray(e)&&(e=e[0]),n=this.returnSequences?null==this.mergeMode?[e,e]:e:null==this.mergeMode?[null,null]:null,this.returnState){var r=this.forwardLayer.states.map((function(t){return null}));return Array.isArray(n)?n.concat(r).concat(r):[n].concat(r).concat(r)}return n},Object.defineProperty(n.prototype,"trainableWeights",{get:function(){return this.forwardLayer.trainableWeights.concat(this.backwardLayer.trainableWeights)},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"nonTrainableWeights",{get:function(){return this.forwardLayer.nonTrainableWeights.concat(this.backwardLayer.nonTrainableWeights)},enumerable:!1,configurable:!0}),n.prototype.setFastWeightInitDuringBuild=function(e){t.prototype.setFastWeightInitDuringBuild.call(this,e),null!=this.forwardLayer&&this.forwardLayer.setFastWeightInitDuringBuild(e),null!=this.backwardLayer&&this.backwardLayer.setFastWeightInitDuringBuild(e)},n.prototype.getConfig=function(){var e={mergeMode:this.mergeMode},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n.fromConfig=function(t,e){var n=zu(e.layer);if(delete e.layer,null!=e.numConstants)throw new g("Deserialization of a Bidirectional layer with numConstants present is not supported yet.");var r=e;return r.layer=n,new t(r)},n}(Fh);_h.className="Bidirectional",e.serialization.registerClass(_h);var Lh=function(t){function n(e){var n=t.call(this,e)||this;return n.scale=e.scale,e.offset?n.offset=e.offset:n.offset=0,n}return o(n,t),n.prototype.getConfig=function(){var e={scale:this.scale,offset:this.offset},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){return"float32"!==(t=Jt(t)).dtype&&(t=ut(t,"float32")),e.add(e.mul(t,r.scale),r.offset)}))},n}(oe);Lh.className="Rescaling",e.serialization.registerClass(Lh);var Rh=e.image.resizeBilinear,Oh=e.image.cropAndResize,Mh=function(t){function n(e){var n=t.call(this,e)||this;return n.height=e.height,n.width=e.width,n}return o(n,t),n.prototype.centerCrop=function(t,n,r,i,a,o,s,u){return e.tidy((function(){var l,c=!1,h=[n/o,r/s,(i+n)/o,(a+r)/s],p=[];3===t.rank?(c=!0,l=e.stack([t])):l=t;for(var f=0;f=0&&u>=0?r.centerCrop(e,s,u,r.height,r.width,a,o,n):r.upsize(t,r.height,r.width,n)}))},n.prototype.getConfig=function(){var e={height:this.height,width:this.width},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n.prototype.computeOutputShape=function(t){var e=(t=Zt(t)).length-3,n=t.length-2;return t[e]=this.height,t[n]=this.width,t},n}(oe);Mh.className="CenterCrop",e.serialization.registerClass(Mh);var Bh=function(t){function n(e){var n=t.call(this,e)||this;return n.numTokens=e.numTokens,e.outputMode?n.outputMode=e.outputMode:n.outputMode="multiHot",n}return o(n,t),n.prototype.getConfig=function(){var e={numTokens:this.numTokens,outputMode:this.outputMode},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n.prototype.computeOutputShape=function(t){return null==(t=Zt(t))?[this.numTokens]:"oneHot"===this.outputMode&&1!==t[t.length-1]?(t.push(this.numTokens),t):(t[t.length-1]=this.numTokens,t)},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){var i;if("int32"!==(t=Jt(t)).dtype&&(t=ut(t,"int32")),"undefined"!=typeof n.countWeights){if("count"!==r.outputMode)throw new d("countWeights is not used when outputMode !== count.\n Received countWeights=".concat(n.countWeights));i=Jt(n.countWeights)}var a=e.max(t),o=e.min(t),s=e.greater(r.numTokens,a).bufferSync().get(0),u=e.greaterEqual(o,0).bufferSync().get(0);if(!s||!u)throw new d("Input values must be between 0 < values <="+" numTokens with numTokens=".concat(r.numTokens));return function(t,n,r,i){var a=Jt(t);if("int32"!==a.dtype&&(a=ut(a,"int32")),"int"===n)return a;var o=a.shape;if(0===a.rank&&(a=e.expandDims(a,-1)),"oneHot"===n&&1!==a.shape[a.shape.length-1]&&(a=e.expandDims(a,-1)),a.rank>2)throw new d("When outputMode is not int, maximum output rank is 2"+" Received outputMode ".concat(n," and input shape ").concat(o)+" which would result in output rank ".concat(a.rank,"."));var s,u=["multiHot","oneHot"].includes(n),l=a;if(s="undefined"!=typeof i&&"count"===n?e.denseBincount(l,i,r,u):e.denseBincount(l,[],r,u),"tfIdf"!==n)return s;if(i)return e.mul(s,i);throw new d("When outputMode is 'tfIdf', weights must be provided.")}(t,r.outputMode,r.numTokens,i)}))},n}(oe);Bh.className="CategoryEncoding",e.serialization.registerClass(Bh);var Ph=new Set(["bilinear","nearest"]),jh=function(t){function n(e){var n=t.call(this,e)||this;if(n.height=e.height,n.width=e.width,e.interpolation){if(!Ph.has(e.interpolation))throw new d("Invalid interpolation parameter: ".concat(e.interpolation," is not implemented"));n.interpolation=e.interpolation}else n.interpolation="bilinear";return n.cropToAspectRatio=Boolean(e.cropToAspectRatio),n}return o(n,t),n.prototype.computeOutputShape=function(t){var e=(t=Zt(t))[2];return[this.height,this.width,e]},n.prototype.getConfig=function(){var e={height:this.height,width:this.width,interpolation:this.interpolation,cropToAspectRatio:this.cropToAspectRatio},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},n.prototype.call=function(t,n){var r=this;return e.tidy((function(){var n=[r.height,r.width];if("bilinear"===r.interpolation)return e.image.resizeBilinear(t,n,!r.cropToAspectRatio);if("nearest"===r.interpolation)return e.image.resizeNearestNeighbor(t,n,!r.cropToAspectRatio);throw new Error("Interpolation is ".concat(r.interpolation," but only ").concat(h([],c(Ph),!1)," are supported"))}))},n}(oe);jh.className="Resizing",e.serialization.registerClass(jh);var Uh=function(){function t(t){this.seed=t}return t.prototype.next=function(){if(void 0!==this.seed)return this.seed++},t}();Uh.className="RandomSeed";var Wh=function(t){function e(e){var n=t.call(this,e)||this;return n.randomGenerator=new Uh(e.seed),n}return o(e,t),e.prototype.getConfig=function(){var e={seed:this.randomGenerator.seed},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},e}(oe);Wh.className="BaseRandomLayer";var Vh=new Set(["bilinear","nearest"]),qh=function(t){function n(e){var n=t.call(this,e)||this,r=e.factor,i=e.interpolation,a=void 0===i?"bilinear":i;if(n.factor=r,Array.isArray(n.factor)&&2===n.factor.length)n.widthLower=n.factor[0],n.widthUpper=n.factor[1];else{if(Array.isArray(n.factor)||!(n.factor>0))throw new d("Invalid factor: ".concat(n.factor,". Must be positive number or tuple of 2 numbers"));n.widthLower=-n.factor,n.widthUpper=n.factor}if(n.widthLower<-1||n.widthUpper<-1)throw new d("factor must have values larger than -1. Got: ".concat(n.factor));if(n.widthUppere}var ap=function(t){function e(e){var n=t.call(this)||this;if(null==e&&(e={}),e.restoreBestWeights)throw new g("restoreBestWeights = True is not implemented in EarlyStopping yet.");return n.monitor=e.monitor||"val_loss",n.minDelta=Math.abs(e.minDelta||0),n.patience=e.patience||0,n.verbose=e.verbose||0,n.mode=e.mode||"auto",n.baseline=e.baseline,-1===["auto","min","max"].indexOf(n.mode)&&(console.warn("EarlyStopping mode '".concat(n.mode,"' is invalid. ")+"Falling back to mode 'auto'."),n.mode="auto"),"min"===n.mode?n.monitorFunc=rp:"max"===n.mode||-1!==n.monitor.indexOf("acc")?n.monitorFunc=ip:n.monitorFunc=rp,n.monitorFunc===rp&&(n.minDelta*=-1),n}return o(e,t),e.prototype.onTrainBegin=function(t){return s(this,void 0,void 0,(function(){return u(this,(function(t){return this.wait=0,this.stoppedEpoch=0,null!=this.baseline?this.best=this.baseline:this.best=this.monitorFunc===rp?1/0:-1/0,[2]}))}))},e.prototype.onEpochEnd=function(t,e){return s(this,void 0,void 0,(function(){var n;return u(this,(function(r){switch(r.label){case 0:return[4,yu(e)];case 1:return r.sent(),null==(n=this.getMonitorValue(e))?[2]:(this.monitorFunc(n-this.minDelta,this.best)?(this.best=n,this.wait=0):(this.wait++,this.wait>=this.patience&&(this.stoppedEpoch=t,this.model.stopTraining=!0)),[2])}}))}))},e.prototype.onTrainEnd=function(t){return s(this,void 0,void 0,(function(){return u(this,(function(t){return this.stoppedEpoch>0&&this.verbose&&console.log("Epoch ".concat(this.stoppedEpoch,": early stopping.")),[2]}))}))},e.prototype.getMonitorValue=function(t){null==t&&(t={});var e=t[this.monitor];return null==e&&console.warn("Metric for EarlyStopping ".concat(this.monitor," is not available. ")+"Available metrics are: ".concat(Object.keys(t))),e},e}(np);var op={earlyStopping:function(t){return new ap(t)}};t.Callback=np,t.CallbackList=wu,t.CustomCallback=Su,t.EarlyStopping=ap,t.History=xu,t.InputSpec=ee,t.LayerVariable=$t,t.LayersModel=Il,t.RNN=zc,t.Sequential=El,t.SymbolicTensor=ne,t.callbacks=op,t.constraints=du,t.initializers=vu,t.input=Tl,t.layers=$h,t.loadLayersModel=function(t,n){return s(this,void 0,void 0,(function(){var r;return u(this,(function(i){if(null==n&&(n={}),"string"==typeof t){if(0===(r=e.io.getLoadHandlers(t,n)).length)r.push(e.io.browserHTTPRequest(t,n));else if(r.length>1)throw new d("Found more than one (".concat(r.length,") load handlers for ")+"URL '".concat(t,"'"));t=r[0]}return[2,zl(t,void 0,n)]}))}))},t.metrics=Qh,t.model=function(t){return new Il(t)},t.models=tp,t.registerCallbackConstructor=function(t,e){Iu.registerCallbackConstructor(t,e)},t.regularizers=ep,t.sequential=function(t){return new El(t)},t.version_layers=al})); //# sourceMappingURL=tf-layers.min.js.map