UNPKG

@tensorflow/tfjs-layers

Version:

TensorFlow layers API in JavaScript

17 lines 331 kB
/** * @license * Copyright 2024 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. * ============================================================================= */ import*as t from"@tensorflow/tfjs-core";import{util as e,backend as n,tidy as s,tensor1d as i,serialization as r,zeros as a,ones as o,mul as l,scalar as u,randomUniform as h,truncatedNormal as c,eye as p,linalg as d,dispose as f,memory as g,cast as m,env as y,nextFrame as b,add as w,div as k,keep as v,train as S,clone as x,argMax as N,reshape as I,Tensor as A,Optimizer as T,io as E,sum as C,abs as z,relu as $,clipByValue as F,leakyRelu as D,prelu as L,elu as _,greater as R,sub as M,exp as O,logSumExp as B,transpose as P,any as U,notEqual as W,zerosLike as j,greaterEqual as q,moments as V,stack as K,tensor as G,range as H,unstack as J,image as Z,expandDims as Y,denseBincount as X,max as Q,min as tt}from"@tensorflow/tfjs-core";function et(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 s=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,s.get?s:{enumerable:!0,get:function(){return e[n]}})}}))})),t}class nt extends Error{constructor(t){super(t),Object.setPrototypeOf(this,nt.prototype)}}class st extends Error{constructor(t){super(t),Object.setPrototypeOf(this,st.prototype)}}class it extends Error{constructor(t){super(t),Object.setPrototypeOf(this,it.prototype)}}class rt extends Error{constructor(t){super(t),Object.setPrototypeOf(this,rt.prototype)}}class at extends Error{constructor(t){super(t),Object.setPrototypeOf(this,at.prototype)}}class ot{constructor(t){this.maxEntries=t||100,this.cache=new Map}get(t){let e;return this.cache.has(t)&&(e=this.cache.get(t),this.cache.delete(t),this.cache.set(t,e)),e}put(t,e){if(this.cache.has(t))this.cache.delete(t);else if(this.cache.size>=this.maxEntries){const t=this.cache.keys().next().value;this.cache.delete(t)}this.cache.set(t,e)}getMaxEntries(){return this.maxEntries}setMaxEntries(t){if(t<0)throw new Error(`The maxEntries of LRU caches must be at least 0, but got ${t}.`);if(this.maxEntries>t)for(let e=0;e<this.maxEntries-t;e++){const t=this.cache.keys().next().value;this.cache.delete(t)}this.maxEntries=t}}function lt(t,e){if(Array.isArray(t)){let n=[];for(let s=0;s<e;s++)n=n.concat(t);return n}{const n=new Array(e);return n.fill(t),n}}function ut(t,e){if(!t)throw new at(e)}function ht(t,e){let n=0;for(const s of t)s===e&&n++;return n}function ct(t){return 1===t.length?t[0]:t}function pt(t){return Array.isArray(t)?t:[t]}function dt(t){const e=t.replace(/(.)([A-Z][a-z0-9]+)/g,"$1_$2").replace(/([a-z])([A-Z])/g,"$1_$2").toLowerCase();return"_"!==e[0]?e:"private"+e}function ft(t){return t.length<=1||-1===t.indexOf("_")?t:t.replace(/[_]+(\w|$)/g,((t,e)=>e.toUpperCase()))}let gt={};function mt(t){if(null==t)return null;const e={};return e.className=t.getClassName(),e.config=t.getConfig(),e}function yt(t){if(null!=t&&"object"==typeof t)if(Array.isArray(t))t.forEach((t=>yt(t)));else{const e=Object.keys(t);for(const n of e){const e=t[n];null!=e&&"object"==typeof e&&(Array.isArray(e)||"ndarray"!==e.type||"number"!=typeof e.value?yt(e):t[n]=e.value)}}}function bt(t,e={},n={},s="object",i=!1){if("string"==typeof t){const i=t;let r;if(i in n)r=n[i];else if(i in gt)r=gt[i];else if(r=e[i],null==r)throw new it(`Unknown ${s}: ${t}. This may be due to one of the following reasons:\n1. The ${s} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.\n2. The custom ${s} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);return r}{const r=t;if(null==r.className||null==r.config)throw new it(`${s}: Improper config format: ${JSON.stringify(r)}.\n'className' and 'config' must set.`);const a=r.className;let o,l;if(a in n?[o,l]=n[a]:a in gt?[o,l]=gt.className:a in e&&([o,l]=e[a]),null==o)throw new it(`Unknown ${s}: ${a}. This may be due to one of the following reasons:\n1. The ${s} is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.\n2. The custom ${s} is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().`);if(null!=l){const t={};for(const e of Object.keys(gt))t[e]=gt[e];for(const e of Object.keys(n))t[e]=n[e];r.config.customObjects=t;const e=Object.assign({},gt);for(const t of Object.keys(n))gt[t]=n[t];yt(r.config);const s=l(o,r.config,n,i);return gt=Object.assign({},e),s}{const t=Object.assign({},gt);for(const t of Object.keys(n))gt[t]=n[t];const e=new o(r.config);return gt=Object.assign({},t),e}}}function wt(t,e){return-1*function(t,e){return t<e?-1:t>e?1:0}(t,e)}function kt(t){if(null==t)return t;const e=[];for(const n of t)-1===e.indexOf(n)&&e.push(n);return e}function vt(t){if(null==t)throw new it(`Invalid value in obj: ${JSON.stringify(t)}`);for(const e in t)if(t.hasOwnProperty(e))return!1;return!0}function St(t,e,n){if(null!=n&&t.indexOf(n)<0)throw new it(`${n} is not a valid ${e}. Valid values are ${t} or null/undefined.`)}function xt(t,e,n=0,s=1/0){return ut(n>=0),ut(s>=n),Array.isArray(t)&&t.length>=n&&t.length<=s&&t.every((t=>typeof t===e))}function Nt(t,n){Array.isArray(t)?(e.assert(t.length>0,(()=>`${n} is unexpectedly an empty array.`)),t.forEach(((t,e)=>Nt(t,`element ${e+1} of ${n}`)))):e.assert(Number.isInteger(t)&&t>0,(()=>`Expected ${n} to be a positive integer, but got ${It(t)}.`))}function It(t){return null===t?"null":Array.isArray(t)?"["+t.map((t=>It(t))).join(",")+"]":"string"==typeof t?`"${t}"`:`${t}`}function At(t){return"relu"===t?"relu":"linear"===t?"linear":"elu"===t?"elu":null}let Tt=0;function Et(){return Tt++}const Ct={};function zt(t=""){return t in Ct||(Ct[t]=0),Ct[t]+=1,t+Ct[t].toString()}const $t=["channelsFirst","channelsLast"],Ft=["nearest","bilinear"],Dt=["valid","same","causal"],Lt=["max","avg"],_t=["sum","mul","concat","ave"],Rt=new Map;function Mt(t){St($t,"DataFormat",t)}function Ot(t){St(Dt,"PaddingMode",t)}function Bt(t){St(Lt,"PoolMode",t)}const Pt=[];function Ut(t,e){Pt.push(t);try{const t=e();return Pt.pop(),t}catch(t){throw Pt.pop(),t}}function Wt(t){if(!Vt(t))throw new Error("Not a valid tensor name: '"+t+"'");return(0===Pt.length?"":Pt.join("/")+"/")+t}function jt(t){if(!Vt(t))throw new Error("Not a valid tensor name: '"+t+"'");Rt.has(t)||Rt.set(t,0);const e=Rt.get(t);if(Rt.set(t,Rt.get(t)+1),e>0){const n=`${t}_${e}`;return Rt.set(n,1),n}return t}const qt=new RegExp(/^[A-Za-z0-9][-A-Za-z0-9\._\/]*$/);function Vt(t){return!!t.match(qt)}function Kt(t,e,n){null==e&&(e=0),null==n&&(n=t.length);let s=1;for(let i=e;i<n;++i)s*=t[i];return s}function Gt(t){if(0===t.length)return Number.NaN;let e=Number.POSITIVE_INFINITY;for(let n=0;n<t.length;n++){const s=t[n];s<e&&(e=s)}return e}function Ht(t){if(0===t.length)return Number.NaN;let e=Number.NEGATIVE_INFINITY;for(let n=0;n<t.length;n++){const s=t[n];s>e&&(e=s)}return e}function Jt(t,e){if(e<t)throw new it(`end (${e}) < begin (${t}) is forbidden.`);const n=[];for(let s=t;s<e;++s)n.push(s);return n}let Zt;function Yt(){return null==Zt&&(Zt=n().epsilon()),Zt}function Xt(e,n){return t.cast(e,n)}function Qt(e,n=-1){const s=e.shape.slice();return n<0&&(n=s.length+n+1),s.splice(n,0,1),t.reshape(e,s)}function te(e,n,i){return s((()=>{switch(e.rank){case 1:return t.slice1d(e,n,i);case 2:return t.slice2d(e,[n,0],[i,e.shape[1]]);case 3:return t.slice3d(e,[n,0,0],[i,e.shape[1],e.shape[2]]);case 4:return t.slice4d(e,[n,0,0,0],[i,e.shape[1],e.shape[2],e.shape[3]]);case 5:return t.slice(e,[n,0,0,0,0],[i,e.shape[1],e.shape[2],e.shape[3],e.shape[4]]);case 6:return t.slice(e,[n,0,0,0,0,0],[i,e.shape[1],e.shape[2],e.shape[3],e.shape[4],e.shape[5]]);default:throw new it(`sliceAlongFirstAxis() received an unsupported tensor rank: ${e.rank}`)}}))}function ee(e,n,i){return s((()=>{switch(e.rank){case 1:return t.slice1d(e,n,i);case 2:return t.slice2d(e,[0,n],[e.shape[0],i]);case 3:return t.slice3d(e,[0,0,n],[e.shape[0],e.shape[1],i]);case 4:return t.slice4d(e,[0,0,0,n],[e.shape[0],e.shape[1],e.shape[2],i]);default:throw new it(`sliceAlongLastAxis() received an unsupported tensor rank: ${e.rank}`)}}))}function ne(e,n,i,r){return s((()=>{switch(e.rank){case 1:return t.slice1d(e,n,i);case 2:switch(r){case 1:return te(e,n,i);case 2:return ee(e,n,i);default:throw new it(`The axis is not within the rank of the tensor ${r}`)}case 3:switch(r){case 1:return te(e,n,i);case 2:return t.slice3d(e,[0,n,0],[e.shape[0],i,e.shape[2]]);case 3:return ee(e,n,i);default:throw new it(`The axis is not within the rank of the tensor ${r}`)}case 4:switch(r){case 1:return te(e,n,i);case 2:return t.slice4d(e,[0,n,0,0],[e.shape[0],i,e.shape[2],e.shape[3]]);case 3:return t.slice4d(e,[0,0,n,0],[e.shape[0],e.shape[1],i,e.shape[3]]);case 4:return ee(e,n,i);default:throw new it(`The axis is not within the rank of the tensor ${r}`)}default:throw new it(`sliceAlongLastAxis() received an unsupported tensor rank: ${e.rank}`)}}))}function se(e,n=-1){let s;return n<0&&(s=e[0].rank,n=0!==s?s:0),n===e[0].rank&&(n=-1),t.concat(e,n)}function ie(e,n){switch(e.rank){case 1:return t.concat1d([e,n]);case 2:return t.concat2d([e,n],0);case 3:return t.concat3d([e,n],0);case 4:return t.concat4d([e,n],0);default:throw new it(`concatAlongFirstAxis() received an unsupported tensor rank: ${e.rank}`)}}function re(e,n){if(Array.isArray(n)||(n=[n]),e.rank!==n.length)throw new it(`The length of input n (${n.length}) does not match the number of dimensions in input x (${e.rank})`);return t.tile(e,n)}function ae(e,n=0,s=1,i,r){return t.randomNormal(e,n,s,i,r)}function oe(e,n,s,i){if(e.rank<2||n.rank<2)throw new rt(`dot requires both inputs to be rank >= 2 but got x shape = ${e.shape} and y shape = ${n.shape}`);if(n.rank>=3){if(e.shape.slice(-1)[0]!==n.shape.slice(-2)[0])throw new rt(`If rank y >= 3, then the second last dim of y must equal the last dim of x but got x shape = ${e.shape} and y shape = ${n.shape}`)}if(2===e.rank&&2===n.rank){const r=!1,a=!1;return t.fused.matMul({a:e,b:n,transposeA:r,transposeB:a,bias:i?he(e.rank,i,"channelsLast"):null,activation:s})}{const r=e.shape.slice(),a=r.pop();e=t.reshape(e,[-1,a]);const o=n.shape.slice(),l=o.pop(),u=o.pop(),h=[...o,l],c=Array.from({length:n.rank},((t,e)=>0===e?n.rank-2:e<=n.rank-2?e-1:e));n=t.reshape(t.transpose(n,c),[u,-1]);const p=[...r,...h],d=!1,f=!1;return t.reshape(t.fused.matMul({a:e,b:n,transposeA:d,transposeB:f,bias:i?he(e.rank,i,"channelsLast"):null,activation:s}),p)}}function le(e,n,r){return s((()=>(n=Array.isArray(n)?i(n,"int32"):t.cast(n,"int32"),t.gather(e,n,r))))}function ue(e){return t.mul(e,e)}function he(e,n,s){const i=n.shape;if(1!==n.rank&&n.rank!==e)throw new it(`Unexpected bias dimensions: ${n.rank}; expected it to be 1 or ${e}`);if(5===e){if("channelsFirst"===s)return 1===i.length?t.reshape(n,[1,i[0],1,1,1]):t.reshape(n,[1,i[3],i[0],i[1],i[2]]);if("channelsLast"===s)return 1===i.length?t.reshape(n,[1,1,1,1,i[0]]):t.reshape(n,[1].concat(i))}else if(4===e){if("channelsFirst"===s)return 1===i.length?t.reshape(n,[1,i[0],1,1]):t.reshape(n,[1,i[2],i[0],i[1]]);if("channelsLast"===s)return 1===i.length?t.reshape(n,[1,1,1,i[0]]):t.reshape(n,[1].concat(i))}else if(3===e){if("channelsFirst"===s)return 1===i.length?t.reshape(n,[1,i[0],1]):t.reshape(n,[1,i[1],i[0]]);if("channelsLast"===s)return 1===i.length?t.reshape(n,[1,1,i[0]]):t.reshape(n,[1].concat(i))}else if(e<3)return n;throw new it(`Unsupported input rank by biasAdd: ${n.rank}`)}function ce(e,n,i){return s((()=>(null==i&&(i="channelsLast"),Mt(i),t.add(e,he(e.rank,n,i)))))}function pe(e,n,i,r){return s((()=>t.dropout(e,n,i,r)))}function de(t,e,n=!1){return n?t():e()}const fe=["fanIn","fanOut","fanAvg"],ge=["normal","uniform","truncatedNormal"];class me extends r.Serializable{fromConfigUsesCustomObjects(){return!1}getConfig(){return{}}}class ye extends me{apply(t,e){return a(t,e)}}ye.className="Zeros",r.registerClass(ye);class be extends me{apply(t,e){return o(t,e)}}be.className="Ones",r.registerClass(be);class we extends me{constructor(t){if(super(),"object"!=typeof t)throw new it(`Expected argument of type ConstantConfig but got ${t}`);if(void 0===t.value)throw new it(`config must have value set but got ${t}`);this.value=t.value}apply(t,e){return s((()=>l(u(this.value),o(t,e))))}getConfig(){return{value:this.value}}}we.className="Constant",r.registerClass(we);class ke extends me{constructor(t){super(),this.DEFAULT_MINVAL=-.05,this.DEFAULT_MAXVAL=.05,this.minval=t.minval||this.DEFAULT_MINVAL,this.maxval=t.maxval||this.DEFAULT_MAXVAL,this.seed=t.seed}apply(t,e){return h(t,this.minval,this.maxval,e,this.seed)}getConfig(){return{minval:this.minval,maxval:this.maxval,seed:this.seed}}}ke.className="RandomUniform",r.registerClass(ke);class ve extends me{constructor(t){super(),this.DEFAULT_MEAN=0,this.DEFAULT_STDDEV=.05,this.mean=t.mean||this.DEFAULT_MEAN,this.stddev=t.stddev||this.DEFAULT_STDDEV,this.seed=t.seed}apply(t,e){if("float32"!==(e=e||"float32")&&"int32"!==e)throw new rt(`randomNormal does not support dType ${e}.`);return ae(t,this.mean,this.stddev,e,this.seed)}getConfig(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}}}ve.className="RandomNormal",r.registerClass(ve);class Se extends me{constructor(t){super(),this.DEFAULT_MEAN=0,this.DEFAULT_STDDEV=.05,this.mean=t.mean||this.DEFAULT_MEAN,this.stddev=t.stddev||this.DEFAULT_STDDEV,this.seed=t.seed}apply(t,e){if("float32"!==(e=e||"float32")&&"int32"!==e)throw new rt(`truncatedNormal does not support dType ${e}.`);return c(t,this.mean,this.stddev,e,this.seed)}getConfig(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}}}Se.className="TruncatedNormal",r.registerClass(Se);let xe=class extends me{constructor(t){super(),this.gain=null!=t.gain?t.gain:1}apply(t,e){return s((()=>{if(2!==t.length||t[0]!==t[1])throw new it("Identity matrix initializer can only be used for 2D square matrices.");return l(this.gain,p(t[0]))}))}getConfig(){return{gain:this.gain}}};xe.className="Identity",r.registerClass(xe);class Ne extends me{constructor(t){if(super(),t.scale<0)throw new it(`scale must be a positive float. Got: ${t.scale}`);var e;this.scale=null==t.scale?1:t.scale,this.mode=null==t.mode?"fanIn":t.mode,e=this.mode,St(fe,"FanMode",e),this.distribution=null==t.distribution?"normal":t.distribution,function(t){St(ge,"Distribution",t)}(this.distribution),this.seed=t.seed}apply(t,e){const n=function(t,e="channelsLast"){let n,s;if(Mt(e),2===t.length)n=t[0],s=t[1];else if(-1!==[3,4,5].indexOf(t.length)){if("channelsFirst"===e){const e=Kt(t,2);n=t[1]*e,s=t[0]*e}else if("channelsLast"===e){const e=Kt(t,0,t.length-2);n=t[t.length-2]*e,s=t[t.length-1]*e}}else{const e=Kt(t);n=Math.sqrt(e),s=Math.sqrt(e)}return[n,s]}(t),s=n[0],i=n[1];let r=this.scale;if("fanIn"===this.mode?r/=Math.max(1,s):"fanOut"===this.mode?r/=Math.max(1,i):r/=Math.max(1,(s+i)/2),"normal"===this.distribution){const n=Math.sqrt(r);if("float32"!==(e=e||"float32")&&"int32"!==e)throw new rt(`${this.getClassName()} does not support dType ${e}.`);return c(t,0,n,e,this.seed)}{const n=Math.sqrt(3*r);return h(t,-n,n,e,this.seed)}}getConfig(){return{scale:this.scale,mode:this.mode,distribution:this.distribution,seed:this.seed}}}Ne.className="VarianceScaling",r.registerClass(Ne);class Ie extends Ne{constructor(t){super({scale:1,mode:"fanAvg",distribution:"uniform",seed:null==t?null:t.seed})}getClassName(){return Ne.className}}Ie.className="GlorotUniform",r.registerClass(Ie);class Ae extends Ne{constructor(t){super({scale:1,mode:"fanAvg",distribution:"normal",seed:null==t?null:t.seed})}getClassName(){return Ne.className}}Ae.className="GlorotNormal",r.registerClass(Ae);class Te extends Ne{constructor(t){super({scale:2,mode:"fanIn",distribution:"normal",seed:null==t?null:t.seed})}getClassName(){return Ne.className}}Te.className="HeNormal",r.registerClass(Te);class Ee extends Ne{constructor(t){super({scale:2,mode:"fanIn",distribution:"uniform",seed:null==t?null:t.seed})}getClassName(){return Ne.className}}Ee.className="HeUniform",r.registerClass(Ee);class Ce extends Ne{constructor(t){super({scale:1,mode:"fanIn",distribution:"normal",seed:null==t?null:t.seed})}getClassName(){return Ne.className}}Ce.className="LeCunNormal",r.registerClass(Ce);class ze extends Ne{constructor(t){super({scale:1,mode:"fanIn",distribution:"uniform",seed:null==t?null:t.seed})}getClassName(){return Ne.className}}ze.className="LeCunUniform",r.registerClass(ze);class $e extends me{constructor(t){super(),this.DEFAULT_GAIN=1,this.ELEMENTS_WARN_SLOW=2e3,this.gain=null==t.gain?this.DEFAULT_GAIN:t.gain,this.seed=t.seed}apply(t,n){return s((()=>{if(t.length<2)throw new rt("Shape must be at least 2D.");if("int32"!==n&&"float32"!==n&&void 0!==n)throw new TypeError(`Unsupported data type ${n}.`);const s=e.sizeFromShape(t.slice(0,-1)),i=t[t.length-1],r=s*i;r>this.ELEMENTS_WARN_SLOW&&console.warn(`Orthogonal initializer is being called on a matrix with more than ${this.ELEMENTS_WARN_SLOW} (${r}) elements: Slowness may result.`);const a=ae([Math.max(i,s),Math.min(i,s)],0,1,n,this.seed),o=d.qr(a,!1);let h=o[0];const c=o[1].flatten().stridedSlice([0],[Math.min(i,s)*Math.min(i,s)],[Math.min(i,s)+1]);return h=l(h,c.sign()),s<i&&(h=h.transpose()),l(u(this.gain),h.reshape(t))}))}getConfig(){return{gain:this.gain,seed:this.seed}}}$e.className="Orthogonal",r.registerClass($e);const Fe={constant:"Constant",glorotNormal:"GlorotNormal",glorotUniform:"GlorotUniform",heNormal:"HeNormal",heUniform:"HeUniform",identity:"Identity",leCunNormal:"LeCunNormal",leCunUniform:"LeCunUniform",ones:"Ones",orthogonal:"Orthogonal",randomNormal:"RandomNormal",randomUniform:"RandomUniform",truncatedNormal:"TruncatedNormal",varianceScaling:"VarianceScaling",zeros:"Zeros"};function De(t,e={}){return bt(t,r.SerializationMap.getMap().classNameMap,e,"initializer")}function Le(t){return mt(t)}function _e(t){if("string"==typeof t){const e=t in Fe?Fe[t]:t;if("GlorotNormal"===e)return new Ae;if("GlorotUniform"===e)return new Ie;if("HeNormal"===e)return new Te;if("HeUniform"===e)return new Ee;if("LeCunNormal"===e)return new Ce;if("LeCunUniform"===e)return new ze;{const t={};return t.className=e,t.config={},De(t)}}return t instanceof me?t:De(t)}function Re(t){return Array.isArray(t)&&Array.isArray(t[0])}function Me(t){return 0===t.length?[]:Array.isArray(t[0])?t:[t]}function Oe(t){let e;if(Array.isArray(t)){if(1!==t.length)throw new it(`Expected Tensor length to be 1; got ${t.length}`);e=t[0]}else e=t;return e}function Be(t){if(Array.isArray(t)&&Array.isArray(t[0])){if(1===t.length)return t[0];throw new it(`Expected exactly 1 Shape; got ${t.length}`)}return t}function Pe(t){let e=0;for(const n of t)0===n.shape.length?e+=1:e+=n.shape.reduce(((t,e)=>t*e));return e}class Ue{constructor(e,n="float32",s="Variable",i=!0,r=null){this.dtype=null==n?"float32":n,this.shape=e.shape,this.id=Et(),s=null==s?"Variable":s,this.originalName=Wt(s),this.name=jt(this.originalName),this.trainable_=i,this.constraint=r,this.val=t.variable(e,this.trainable_,this.name,this.dtype)}read(){return this.assertNotDisposed(),this.val}write(t){return this.assertNotDisposed(),function(t,e){if(t.shape.toString()!==e.shape.toString())throw new Error("Shape mismatch: "+JSON.stringify(t.shape)+" vs. "+JSON.stringify(e.shape))}(this.val,t),this.val.id!==t.id&&(this.val.assign(t),null!=this.constraint&&this.val.assign(this.constraint.apply(this.val))),this}dispose(){this.assertNotDisposed(),this.val.dispose()}assertNotDisposed(){if(this.val.isDisposed)throw new Error(`LayersVariable ${this.name} is already disposed.`)}get trainable(){return this.trainable_}set trainable(t){this.trainable_=t,this.val.trainable=t}}function We(t){return t.map((t=>t.read()))}function je(t){t.forEach((t=>{t[0].write(t[1])}))}class qe{constructor(t){this.dtype=t.dtype,this.shape=t.shape,null!=t.shape?this.ndim=t.shape.length:this.ndim=t.ndim,this.maxNDim=t.maxNDim,this.minNDim=t.minNDim,this.axes=t.axes||{}}}class Ve{constructor(t,e,n,s,i,r,a){this.dtype=t,this.shape=e,this.sourceLayer=n,this.inputs=s,this.callArgs=i,this.outputTensorIndex=a,this.id=Et(),null!=r&&(this.originalName=Wt(r),this.name=jt(this.originalName)),this.rank=e.length}}let Ke=0;class Ge{constructor(t,e){this.callArgs=e,this.id=Ke++,this.outboundLayer=t.outboundLayer,this.inboundLayers=t.inboundLayers,this.nodeIndices=t.nodeIndices,this.tensorIndices=t.tensorIndices,this.inputTensors=t.inputTensors,this.outputTensors=t.outputTensors,this.inputMasks=t.inputMasks,this.outputMasks=t.outputMasks,this.inputShapes=t.inputShapes,this.outputShapes=t.outputShapes;for(const e of t.inboundLayers)null!=e&&e.outboundNodes.push(this);t.outboundLayer.inboundNodes.push(this)}getConfig(){const t=[];for(const e of this.inboundLayers)null!=e?t.push(e.name):t.push(null);return{outboundLayer:this.outboundLayer?this.outboundLayer.name:null,inboundLayers:t,nodeIndices:this.nodeIndices,tensorIndices:this.tensorIndices}}}let He=0;class Je extends r.Serializable{constructor(t={}){super(),this._callHook=null,this._addedWeightNames=[],this._stateful=!1,this.id=He++,this.activityRegularizer=null,this.inputSpec=null,this.supportsMasking=!1,this._trainableWeights=[],this._nonTrainableWeights=[],this._losses=[],this._updates=[],this._built=!1,this.inboundNodes=[],this.outboundNodes=[];let e=t.name;if(!e){const t=this.getClassName();e=dt(t)+"_"+zt(t)}if(this.name=e,this.trainable_=null==t.trainable||t.trainable,null!=t.inputShape||null!=t.batchInputShape){let e;if(null!=t.batchInputShape)e=t.batchInputShape;else if(null!=t.inputShape){let n=null;null!=t.batchSize&&(n=t.batchSize),e=[n].concat(t.inputShape)}this.batchInputShape=e;let n=t.dtype;null==n&&(n=t.inputDType),null==n&&(n="float32"),this.dtype=n}null!=t.weights?this.initialWeights=t.weights:this.initialWeights=null,this._refCount=null,this.fastWeightInitDuringBuild=!1}static nodeKey(t,e){return t.name+"_ib-"+e.toString()}getNodeAtIndex(t,e){if(0===this.inboundNodes.length)throw new st(`The layer has never been called and thus has no defined ${e}.`);if(this.inboundNodes.length<=t)throw new it(`Asked to get ${e} at node ${t}, but the layer has only ${this.inboundNodes.length} inbound nodes.`);return this.inboundNodes[t]}getInputAt(t){return ct(this.getNodeAtIndex(t,"input").inputTensors)}getOutputAt(t){return ct(this.getNodeAtIndex(t,"output").outputTensors)}get input(){if(this.inboundNodes.length>1)throw new nt(`Layer ${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 nt(`Layer ${this.name} is not connected, no input to return.`);return ct(this.getNodeAtIndex(0,"input").inputTensors)}get output(){if(0===this.inboundNodes.length)throw new nt(`Layer ${this.name} has no inbound nodes.`);if(this.inboundNodes.length>1)throw new nt(`Layer ${this.name} has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use \`getOutputAt(nodeIndex)\` instead.`);return ct(this.getNodeAtIndex(0,"output").outputTensors)}get losses(){return this._losses}calculateLosses(){return this.losses.map((t=>t()))}get updates(){return this._updates}get built(){return this._built}set built(t){this._built=t}get trainable(){return this.trainable_}set trainable(t){this._trainableWeights.forEach((e=>e.trainable=t)),this.trainable_=t}get trainableWeights(){return this.trainable_?this._trainableWeights.filter((t=>t.trainable)):[]}set trainableWeights(t){this._trainableWeights=t}get nonTrainableWeights(){return this.trainable?this._trainableWeights.filter((t=>!t.trainable)).concat(this._nonTrainableWeights):this._trainableWeights.concat(this._nonTrainableWeights)}set nonTrainableWeights(t){this._nonTrainableWeights=t}get weights(){return this.trainableWeights.concat(this.nonTrainableWeights)}get stateful(){return this._stateful}resetStates(){if(!this.stateful)throw new Error("Cannot call the resetStates() method of a non-stateful Layer object.")}assertInputCompatibility(t){const e=pt(t);if(null==this.inputSpec||0===this.inputSpec.length)return;const n=pt(this.inputSpec);if(e.length!==n.length)throw new it(`Layer ${this.name} expects ${n.length} inputs, but it received ${e.length} input tensors. Input received: ${t}`);for(let t=0;t<e.length;t++){const s=e[t],i=n[t];if(null==i)continue;const r=s.rank;if(null!=i.ndim&&r!==i.ndim)throw new it(`Input ${t} is incompatible with layer ${this.name}: expected ndim=${i.ndim}, found ndim=${r}`);if(null!=i.maxNDim&&r>i.maxNDim)throw new it(`Input ${t} is incompatible with layer ${this.name}: expected max_ndim=${i.maxNDim}, found ndim=${r}`);if(null!=i.minNDim&&r<i.minNDim)throw new it(`Input ${t} is incompatible with layer ${this.name}: expected min_ndim=${i.minNDim}, found ndim=${r}.`);if(null!=i.dtype&&s.dtype!==i.dtype)throw new it(`Input ${t} is incompatible with layer ${this.name} : expected dtype=${i.dtype}, found dtype=${s.dtype}.`);if(i.axes){const e=s.shape;for(const n in i.axes){const s=Number(n),r=i.axes[n],a=s>=0?e[s]:e[e.length+s];if(null!=r&&-1===[r,null].indexOf(a))throw new it(`Input ${t} is incompatible with layer ${this.name}: expected axis ${s} of input shape to have value ${r} but got shape ${e}.`)}}if(null!=i.shape)for(let e=0;e<i.shape.length;++e){const n=i.shape[e],r=s.shape[e];if(null!=n&&null!=r&&n!==r)throw new it(`Input ${t} is incompatible with layer ${this.name}: expected shape=${i.shape}, found shape=${s.shape}.`)}}}call(t,e){return t}invokeCallHook(t,e){null!=this._callHook&&this._callHook(t,e)}setCallHook(t){this._callHook=t}clearCallHook(){this._callHook=null}apply(t,e){e=e||{},this.assertNotDisposed();const n=pt(t),s=function(t){let e=!0;for(const n of pt(t))if(!(n instanceof Ve)){e=!1;break}return e}(t),i=function(t){let e=!0;for(const n of pt(t))if(n instanceof Ve){e=!1;break}return e}(t);if(s===i)throw new it("Arguments to apply() must be all SymbolicTensors or all Tensors");return Ut(this.name,(()=>{if(!this.built){this.assertInputCompatibility(t);const e=[];for(const n of pt(t))e.push(n.shape);this.build(ct(e)),this.built=!0,this.initialWeights&&this.setWeights(this.initialWeights),null===this._refCount&&i&&(this._refCount=1)}if(this.assertInputCompatibility(t),i){let s=this.call(t,e);this.supportsMasking&&this.setMaskMetadata(t,s);const i=pt(s),r=[];for(let t of i)-1!==n.indexOf(t)&&(t=t.clone()),r.push(t);if(s=ct(r),null!=this.activityRegularizer)throw new rt("Layer invocation in the presence of activity regularizer(s) is not supported yet.");return s}{const n=function(t){t=pt(t);const e=[];for(const n of t)e.push(n.shape);return ct(e)}(t),s=this.computeOutputShape(n);let i;const r="float32";if(this.warnOnIncompatibleInputShape(Array.isArray(t)?n[0]:n),i=null!=s&&s.length>0&&Array.isArray(s[0])?s.map(((n,s)=>new Ve(r,n,this,pt(t),e,this.name,s))):new Ve(r,s,this,pt(t),e,this.name),this.addInboundNode(t,i,null,null,n,s,e),this._refCount++,null!=this.activityRegularizer)throw new rt("Layer invocation in the presence of activity regularizer(s) is not supported yet.");return i}}))}warnOnIncompatibleInputShape(t){if(null!=this.batchInputShape)if(t.length!==this.batchInputShape.length)console.warn(`The rank of the input tensor provided (shape: ${JSON.stringify(t)}) does not match that of the batchInputShape (${JSON.stringify(this.batchInputShape)}) of the layer ${this.name}`);else{let e=!1;this.batchInputShape.forEach(((n,s)=>{null!=n&&null!=t[s]&&t[s]!==n&&(e=!0)})),e&&console.warn(`The shape of the input tensor (${JSON.stringify(t)}) does not match the expectation of layer ${this.name}: ${JSON.stringify(this.batchInputShape)}`)}}get outputShape(){if(null==this.inboundNodes||0===this.inboundNodes.length)throw new nt(`The layer ${this.name} has never been called and thus has no defined output shape.`);const t=[];for(const e of this.inboundNodes){const n=JSON.stringify(e.outputShapes);-1===t.indexOf(n)&&t.push(n)}if(1===t.length){const t=this.inboundNodes[0].outputShapes;return Array.isArray(t)&&Array.isArray(t[0])&&1===t.length?t[0]:t}throw new nt(`The layer ${this.name} has multiple inbound nodes with different output shapes. Hence the notion of "output shape" is ill-defined for the layer.`)}countParams(){if(!this.built)throw new st(`You tried to call countParams() on ${this.name}, but the layer is not built yet. Build it first by calling build(batchInputShape).`);return Pe(this.weights)}build(t){this.built=!0}getWeights(t=!1){return We(t?this.trainableWeights:this.weights)}setWeights(t){s((()=>{const n=this.weights;if(n.length!==t.length)throw new it(`You called setWeights(weights) on layer "${this.name}" with a weight list of length ${t.length}, but the layer was expecting ${n.length} weights. Provided weights: ${t}...`);if(0===n.length)return;const s=[],i=We(n);for(let r=0;r<i.length;++r){const a=i[r],o=n[r],l=t[r];if(!e.arraysEqual(a.shape,l.shape))throw new it(`Layer weight shape ${a.shape} not compatible with provided weight shape ${l.shape}`);s.push([o,l])}je(s)}))}addWeight(t,e,n,s,i,r,a,o){if(-1!==this._addedWeightNames.indexOf(t))throw new it(`Duplicate weight name ${t} for layer ${this.name}`);this._addedWeightNames.push(t),null==n&&(n="float32"),this.fastWeightInitDuringBuild&&(s=null!=o?o():_e("zeros"));const l=s.apply(e,n),u=new Ue(l,n,t,r,a);return l.dispose(),null!=i&&this.addLoss((()=>i.apply(u.read()))),null==r&&(r=!0),r?this._trainableWeights.push(u):this._nonTrainableWeights.push(u),u}setFastWeightInitDuringBuild(t){this.fastWeightInitDuringBuild=t}addLoss(t){null==t||Array.isArray(t)&&0===t.length||(t=pt(t),void 0!==this._losses&&null!==this._losses&&this.losses.push(...t))}computeOutputShape(t){return t}computeMask(t,e){if(!this.supportsMasking){if(null!=e){if(!Array.isArray(e))throw new TypeError(`Layer ${this.name} does not support masking, but was passed an inputMask.`);e.forEach((t=>{if(null!=t)throw new TypeError(`Layer ${this.name} does not support masking, but was passed an inputMask.`)}))}return null}return e}setMaskMetadata(t,e,n){if(!this.supportsMasking)return;const s=this.computeMask(t,n),i=pt(e),r=pt(s);if(i.length!==r.length)throw new Error(`${this.name} outputs ${i.length} tensors but ${i.length} masks for those tensors`);for(let t=0;t<i.length;t++)i[t].kerasMask=r[t]}addInboundNode(t,e,n,s,i,r,a=null){const o=pt(t);e=pt(e),n=pt(n),s=pt(s),i=Me(i),r=Me(r);const l=[],u=[],h=[];for(const t of o)l.push(t.sourceLayer),u.push(t.nodeIndex),h.push(t.tensorIndex);new Ge({outboundLayer:this,inboundLayers:l,nodeIndices:u,tensorIndices:h,inputTensors:o,outputTensors:e,inputMasks:n,outputMasks:s,inputShapes:i,outputShapes:r},a);for(let t=0;t<e.length;t++)e[t].sourceLayer=this,e[t].nodeIndex=this.inboundNodes.length-1,e[t].tensorIndex=t}getConfig(){const t={name:this.name,trainable:this.trainable};return null!=this.batchInputShape&&(t.batchInputShape=this.batchInputShape),null!=this.dtype&&(t.dtype=this.dtype),t}disposeWeights(){return this.weights.forEach((t=>t.dispose())),this.weights.length}assertNotDisposed(){if(0===this._refCount)throw new Error(`Layer '${this.name}' is already disposed.`)}dispose(){if(!this.built)throw new Error(`Cannot dispose Layer ${this.name} because it has not been built yet.`);if(null===this._refCount)throw new Error(`Cannot dispose Layer ${this.name} because it has not been used yet.`);this.assertNotDisposed();let t=0;return 0==--this._refCount&&(t=this.disposeWeights()),{refCountAfterDispose:this._refCount,numDisposedVariables:t}}}function Ze(t,e,n){if((null==e||null!=n&&n>0)&&(e=t.sourceLayer,n=t.nodeIndex),0===e.inboundNodes.length)return[t];{const t=e.inboundNodes[n];if(0===t.inboundLayers.length)return t.inputTensors;{const e=[];for(let n=0;n<t.inboundLayers.length;n++){const s=Ze(t.inputTensors[n],t.inboundLayers[n],t.nodeIndices[n]);for(const t of s)-1===e.indexOf(t)&&e.push(t)}return e}}}class Ye extends Je{constructor(t){if(super({dtype:t.dtype,name:null!=t.name?t.name:zt("input").toString()}),null==t.batchSize&&(t.batchSize=null),null==t.sparse&&(t.sparse=!1),this.trainable=!1,this.built=!0,this.sparse=t.sparse,null!=t.inputShape&&null!=t.batchInputShape)throw new it("Only provide the inputShape OR batchInputShape argument to inputLayer, not both at the same time.");let e=t.batchInputShape;if(null==e){if(null==t.inputShape)throw new it("An InputLayer should be passed either a `batchInputShape` or an `inputShape`.");e=[t.batchSize].concat(t.inputShape)}else if(null!=t.batchSize)throw new it("Cannot specify batchSize if batchInputShape is specified when creating an InputLayer.");const n=t.dtype||"float32";this.batchInputShape=e,this.dtype=n,this.inputSpec=[{shape:e}];const s=new Ve(this.dtype,this.batchInputShape,this,[],{},this.name);s.nodeIndex=0,s.tensorIndex=0,new Ge({outboundLayer:this,inboundLayers:[],nodeIndices:[],tensorIndices:[],inputTensors:[s],outputTensors:[s],inputMasks:[null],outputMasks:[null],inputShapes:[e],outputShapes:[e]})}apply(t,e){throw new it(`Cannot pass any input to an InputLayer's apply() method. InputLayer name: ${this.name}`)}dispose(){return{refCountAfterDispose:this._refCount,numDisposedVariables:0}}getConfig(){return{batchInputShape:this.batchInputShape,dtype:this.dtype,sparse:this.sparse,name:this.name}}}function Xe(t){if(null==t.batchShape&&null==t.shape)throw new Error("Please provide to Input either a `shape` or a `batchShape` argument. Note that `shape` does not include the batch dimension.");if(null!=t.batchShape&&null!=t.shape)throw new it("Please provide either a `shape` or `batchShape` argument to Input, but not both.");let e=t.batchShape;null!=t.shape&&null==e&&(e=[null].concat(t.shape));let n=t.dtype;null==n&&(n="float32");return new Ye({batchInputShape:e,name:t.name,dtype:n,sparse:t.sparse}).inboundNodes[0].outputTensors[0]}Ye.className="InputLayer",r.registerClass(Ye);class Qe{constructor(t){if(this.id2Value={},this.id2Mask={},this.name2Id={},t instanceof Qe)for(const e in t.id2Value)this.id2Value[e]=t.id2Value[e],e in t.id2Mask&&(this.id2Mask[e]=t.id2Mask[e]);else{if(null==t)return;for(const e of t)this.add(e.key,e.value)}}add(t,e,n){if(null!=this.id2Value[t.id])throw new it(`Duplicate key: name=${t.name}, id=${t.id}`);return this.id2Value[t.id]=function(t,e){if(null==t.dtype||t.dtype===e.dtype)return e;try{return m(e,t.dtype)}catch(n){throw new it(`The dtype of the feed (${e.dtype}) can not be cast to the dtype of the key '${t.name}' (${t.dtype}).`)}}(t,e),this.name2Id[t.name]=t.id,null!=n&&(this.id2Mask[t.id]=n),this}addFeed(t){this.add(t.key,t.value)}hasKey(t){return null!=this.id2Value[t.id]}names(){return Object.keys(this.name2Id)}getValue(t){if(t instanceof Ve){if(null==this.id2Value[t.id])throw new it(`Nonexistent key: ${t.name}`);return this.id2Value[t.id]}{const e=this.name2Id[t];if(null==e)throw new it(`Feed dict has no SymbolicTensor name: ${t}`);return this.id2Value[e]}}getMask(t){if(t instanceof Ve){if(null==this.id2Value[t.id])throw new it(`Nonexistent key: ${t.name}`);return this.id2Mask[t.id]}{const e=this.name2Id[t];if(null==e)throw new it(`Feed dict has no SymbolicTensor name: ${t}`);return this.id2Mask[e]}}disposeMasks(){null!=this.id2Mask&&f(this.id2Mask)}}const tn=new ot,en=new ot;function nn(t,n,s,i){const r=null!=s&&s.training,a=Array.isArray(t),o=a?t:[t],l=o.map((t=>t.name)),u=[],h=n.names();for(const t of l)-1!==h.indexOf(t)?u.push(n.getValue(t)):u.push(null);null!=i&&(i.maxNumTensors=-1/0,i.minNumTensors=1/0);const c=l.join(",")+"|"+n.names().sort().join(",");let p,d=tn.get(c);if(null==d){const t=function(t,n){e.assert(null!=t&&t.length>0,(()=>"Expected at least one fetch, got none"));let s=[],i={};if(1===t.length){const e=rn(t[0],n);s=e.sorted,i=e.recipientMap}else{const e=new Set;for(const r of t){const{sorted:t,recipientMap:a}=rn(r,n);for(const n of t)e.has(n.name)||(s.push(n),e.add(n.name));for(const t in a)null==i[t]&&(i[t]=new Set),a[t].forEach((e=>i[t].add(e)))}}return{sorted:s,recipientCounts:sn(i)}}(o,n);d=t.sorted,p=t.recipientCounts,tn.put(c,d),en.put(c,p)}p={},r||Object.assign(p,en.get(c));const m=new Qe(n);for(let t=0;t<d.length;++t){if(null!=i){const t=g().numTensors;t>i.maxNumTensors&&(i.maxNumTensors=t),t<i.minNumTensors&&(i.minNumTensors=t)}const e=d[t],a=e.sourceLayer;if(a instanceof Ye)continue;const o=[],h=[],c=[];let y=!1;for(const t of e.inputs){const e=m.getValue(t),s=m.getMask(t);o.push(e),h.push(s),null!=s&&(y=!0),r||(p[t.name]--,0!==p[t.name]||n.hasKey(t)||-1!==l.indexOf(t.name)||e.isDisposed||!0===t.sourceLayer.stateful||c.push(e))}y&&((s=s||{}).mask=h[0]);const b=pt(a.apply(o,s));let w=null;a.supportsMasking&&(w=a.computeMask(o,h));const k=an(e),v=Array.isArray(k)?k:[k];for(let t=0;t<v.length;++t){m.hasKey(v[t])||m.add(v[t],b[t],Array.isArray(w)?w[0]:w);const e=l.indexOf(v[t].name);-1!==e&&(u[e]=b[t])}r||f(c)}return m.disposeMasks(),a?u:u[0]}function sn(t){const e={};for(const n in t)e[n]=t[n].size;return e}function rn(t,e){const n=new Set,s=[],i={};for(const t of e.names())n.add(t);const r=[],a=[];for(r.push(t);r.length>0;){const t=r[r.length-1];if(n.has(t.name)){r.pop();continue}const e=a[a.length-1]===r.length-1;if(0===t.inputs.length||e)r.pop(),s.push(t),n.add(t.name),e&&a.pop();else{a.push(r.length-1);for(const e of t.inputs)null==i[e.name]&&(i[e.name]=new Set),i[e.name].add(t.name),n.has(e.name)||r.push(e)}}return{sorted:s,recipientMap:i}}function an(t){let e;if(1===t.sourceLayer.inboundNodes.length)e=t.sourceLayer.output;else{let n=null;for(let e=0;e<t.sourceLayer.inboundNodes.length;++e)for(const s of t.sourceLayer.inboundNodes[e].outputTensors)if(s.id===t.id){n=e;break}e=t.sourceLayer.getOutputAt(n)}return e}y().registerFlag("TOPOLOGICAL_SORT_CACHE_MAX_ENTRIES",(()=>100),(function(t){null!=tn&&tn.setMaxEntries(t),null!=en&&en.setMaxEntries(t)}));function on(t){throw new Error(`'${t}' not yet implemented or not found in the registry. This kernel may not be supported by the tfjs backend you have chosen`)}function ln(t,e){if(!t)throw new Error("string"==typeof e?e:e())}function un(t){if(0===t.length)return 1;let e=t[0];for(let n=1;n<t.length;n++)e*=t[n];return e}function hn(t,e){if(t===e)return!0;if(null==t||null==e)return!1;if(t.length!==e.length)return!1;for(let n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0}function cn(t){return t%1==0}function pn(t,e){return e<=t.length?t:t+" ".repeat(e-t.length)}function dn(t,e){const n=e.length;return ln((t=null==t?e.map(((t,e)=>e)):[].concat(t)).every((t=>t>=-n&&t<n)),(()=>`All values in axis param must be in range [-${n}, ${n}) but got axis ${t}`)),ln(t.every((t=>cn(t))),(()=>`All values in axis param must be integers but got axis ${t}`)),t.map((t=>t<0?n+t:t))}function fn(t){if("float32"===t||"int32"===t)return 4;if("complex64"===t)return 8;if("bool"===t)return 1;throw new Error(`Unknown dtype ${t}`)}function gn(t){return"string"==typeof t||t instanceof String}function mn(t){return Array.isArray(t)?mn(t[0]):t instanceof Float32Array?"float32":t instanceof Int32Array||t instanceof Uint8Array||t instanceof Uint8ClampedArray?"int32":"number"==typeof t?"float32":gn(t)?"string":function(t){return"boolean"==typeof t}(t)?"bool":"float32"}function yn(t){return!!(t&&t.constructor&&t.call&&t.apply)}function bn(t){const e=t.length;if(e<2)return[];const n=new Array(e-1);n[e-2]=t[e-1];for(let s=e-3;s>=0;--s)n[s]=n[s+1]*t[s+1];return n}function wn(t,e,n,s=!1){const i=new Array;if(1===e.length){const r=e[0]*(s?2:1);for(let e=0;e<r;e++)i[e]=n[t+e]}else{const r=e[0],a=e.slice(1),o=a.reduce(((t,e)=>t*e))*(s?2:1);for(let e=0;e<r;e++)i[e]=wn(t+e*o,a,n,s)}return i}function kn(t,e,n=!1){if(0===t.length)return e[0];const s=t.reduce(((t,e)=>t*e))*(n?2:1);if(0===s)return[];if(s!==e.length)throw new Error(`[${t}] does not match the input size ${e.length}${n?" for a complex tensor":""}.`);return wn(0,t,e,n)}function vn(t,e){const n=Sn(t,e);for(let t=0;t<n.length;t++)n[t]=1;return n}function Sn(t,e){if(null==e||"float32"===e||"complex64"===e)return new Float32Array(t);if("int32"===e)return new Int32Array(t);if("bool"===e)return new Uint8Array(t);throw new Error(`Unknown data type ${e}`)}function xn(t){t.forEach((e=>{ln(Number.isInteger(e)&&e>=0,(()=>`Tensor must have a shape comprised of positive integers but got shape [${t}].`))}))}function Nn(t){return t&&t.then&&"function"==typeof t.then}class In{constructor(t){this.global=t,this.flags={},this.flagRegistry={},this.urlFlags={},this.getQueryParams=An,this.populateURLFlags()}setPlatform(t,e){null!=this.platform&&(Tn().getBool("IS_TEST")||Tn().getBool("PROD")||console.warn(`Platform ${this.platformName} has already been set. Overwriting the platform with ${t}.`)),this.platformName=t,this.platform=e}registerFlag(t,e,n){if(this.flagRegistry[t]={evaluationFn:e,setHook:n},null!=this.urlFlags[t]){const e=this.urlFlags[t];Tn().getBool("IS_TEST")||Tn().getBool("PROD")||console.warn(`Setting feature override from URL ${t}: ${e}.`),this.set(t,e)}}async getAsync(t){return t in this.flags||(this.flags[t]=await this.evaluateFlag(t)),this.flags[t]}get(t){if(t in this.flags)return this.flags[t];const e=this.evaluateFlag(t);if(Nn(e))throw new Error(`Flag ${t} cannot be synchronously evaluated. Please use getAsync() instead.`);return this.flags[t]=e,this.flags[t]}getNumber(t){return this.get(t)}getBool(t){return this.get(t)}getString(t){return this.get(t)}getFlags(){return this.flags}get features(){return this.flags}set(t,e){if(null==this.flagRegistry[t])throw new Error(`Cannot set flag ${t} as it has not been registered.`);this.flags[t]=e,null!=this.flagRegistry[t].setHook&&this.flagRegistry[t].setHook(e)}evaluateFlag(t){if(null==this.flagRegistry[t])throw new Error(`Cannot evaluate flag '${t}': no evaluation function found.`);return this.flagRegistry[t].evaluationFn()}setFlags(t){this.flags=Object.assign({},t)}reset(){this.flags={},this.urlFlags={},this.populateURLFlags()}populateURLFlags(){if("undefined"==typeof this.global||"undefined"==typeof this.global.location||"undefined"==typeof this.global.location.search)return;const t=this.getQueryParams(this.global.location.search);if("tfjsflags"in t){t.tfjsflags.split(",").forEach((t=>{const[e,n]=t.split(":");this.urlFlags[e]=function(t,e){const n=e.toLowerCase();return"true"===n||"false"===n?"true"===n:""+ +n===n?+n:e}(0,n)}))}}}function An(t){const e={};return t.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,((t,...n)=>(function(t,e,n){t[decodeURIComponent(e)]=decodeURIComponent(n||"")}(e,n[0],n[1]),n.join("=")))),e}function Tn(){return Cn}let En,Cn=null;function zn(){if(null==En){let t;if("undefined"!=typeof window)t=window;else if("undefined"!=typeof global)t=global;else if("undefined"!=typeof process)t=process;else{if("undefined"==typeof self)throw new Error("Could not find a global object");t=self}En=t}return En}function $n(t,e){const n=function(){const t=zn();return null==t._tfGlobals&&(t._tfGlobals=new Map),t._tfGlobals}();if(n.has(t))return n.get(t);{const s=e();return n.set(t,s),n.get(t)}}function Fn(...t){Tn().getBool("IS_TEST")||Tn().getBool("PROD")||console.warn(...t)}const Dn=$n("kernelRegistry",(()=>new Map)),Ln=$n("gradRegistry",(()=>new Map));function _n(t,e){const n=function(t,e){return`${e}_${t}`}(t,e);return Dn.get(n)}function Rn(t){return Ln.get(t)}function Mn(t){const e=Dn.entries(),n=[];for(;;){const{done:s,value:i}=e.next();if(s)break;const[r,a]=i,[o]=r.split("_");o===t&&n.push(a)}return n}function On(t){const{kernelName:e}=t;Ln.has(e)&&Tn().getBool("DEBUG")&&Fn(`Overriding the gradient for '${e}'`),Ln.set(e,t)}var Bn="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function Pn(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function Un(t){if(t.__esModule)return t;var e=t.default;if("function"==typeof e){var n=function t(){if(this instanceof t){var n=[null];n.push.apply(n,arguments);var s=Function.bind.apply(e,n);return new s}return e.apply(this,arguments)};n.prototype=e.prototype}else n={};return Object.defineProperty(n,"__esModule",{value:!0}),Object.keys(t).forEach((function(e){var s=Object.getOwnPropertyDescriptor(t,e);Object.defineProperty(n,e,s.get?s:{enumerable:!0,get:function(){return t[e]}})})),n}var Wn=qn,jn=null;try{jn=new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([0,97,115,109,1,0,0,0,1,13,2,96,0,1,127,96,4,127,127,127,127,1,127,3,7,6,0,1,1,1,1,1,6,6,1,127,1,65,0,11,7,50,6,3,109,117,108,0,1,5,100,105,118,95,115,0,2,5,100,105,118,95,117,0,3,5,114,101,109,95,115,0,4,5,114,101,109,95,117,0,5,8,103,101,116,95,104,105,103,104,0,0,10,191,1,6,4,0,35,0,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,126,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,127,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,128,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,129,34,4,66,32,135,167,36,0,32,4,167,11,36,1,1,126,32,0,173,32,1,173,66,32,134,132,32,2,173,32,3,173,66,32,134,132,130,34,4,66,32,135,167,36,0,32,4,167,11])),{}).exports}catch(t){}function qn(t,e,n){this.low=0|t,this.high=0|e,this.unsigned=!!n}function Vn(t){return!0===(t&&t.__isLong__)}qn.prototype.__isLong__,Object.defineProperty(qn.prototype,"__isLong__",{value:!0}),qn.isLong=Vn;var Kn={},Gn={};function Hn(t,e){var n,s,i;return e?(i=0<=(t>>>=0)&&t<256)&&(s=Gn[t])?s:(n=Zn(t,(0|t)<0?-1:0,!0),i&&(Gn[t]=n),n):(i=-128<=(t|=0)&&t<128)&&(s=Kn[t])?s:(n=Zn(t,t<0?-1:0,!1),i&&(Kn[t]=n),n)}function Jn(t,e){if(isNaN(t))return e?rs:is;if(e){if(t<0)return rs;if(t>=es)return hs}else{if(t<=-ns)return cs;if(t+1>=ns)return us}return t<0?Jn(-t,e).neg():Zn(t%ts|0,t/ts|0,e)}function Zn(t,e,n){return new qn(t,e,n)}qn.fromInt=Hn,qn.fromNumber=Jn,qn.fromBits=Zn;var Yn=Math.pow;function Xn(t,e,n){if(0===t.length)throw Error("empty string");if("NaN"===t||"Infinity"===t||"+Infinity"===t||"-Infinity"===t)return is;if("number"==typeof e?(n=e,e=!1):e=!!e,(n=n||10)<2||36<n)throw RangeError("radix");var s;if((s=t.indexOf("-"))>0)throw Error("interior hyphen");if(0===s)return Xn(t.substring(1),e,n).neg();for(var i=Jn(Yn(n,8)),r=is,a=0;a<t.length;a+=8){var o=Math.min(8,t.length-a),l=parseInt(t.substring(a,a+o),n);if(o<8){var u=Jn(Yn(n,o));r=r.mul(u).add(Jn(l))}else r=(r=r.mul(i)).add(Jn(l))}return r.unsigned=e,r}function Qn(t,e){return"number"==typeof t?Jn(t,e):"string"==typeof t?Xn(t,e):Zn(t.low,t.high,"boolean"==typeof e?e:t.unsigned)}qn.fromString=Xn,qn.fromValue=Qn;var ts=4294967296,es=ts*ts,ns=es/2,ss=Hn(1<<24),is=Hn(0);qn.ZERO=is;var rs=Hn(0,!0);qn.UZERO=rs;var as=Hn(1);qn.ONE=as;var os=Hn(1,!0);qn.UONE=os;var ls=Hn(-1);qn.NEG_ONE=ls;var us=Zn(-1,2147483647,!1);qn.MAX_VALUE=us;var hs=Zn(-1,-1,!0);qn.MAX_UNSIGNED_VALUE=hs;var cs=Zn(0,-2147483648,!1);qn.MIN_VALUE=cs;var ps=qn.prototype;ps.toInt=function(){return this.unsigned?this.low>>>0:this.low},ps.toNumber=function(){return this.unsigned?(this.high>>>0)*ts+(this.low>>>0):this.high*ts+(this.low>>>0)},ps.toString=function(t){if((t=t||10)<2||36<t)throw RangeError("radix");if(this.isZero())return"0";if(this.isNegative()){if(this.eq(cs)){var e=Jn(t),n=this.div(e),s=n.mul(e).sub(this);return n.toString(t)+s.toInt().toString(t)}return"-"+this.neg().toString(t)}for(var i=Jn(Yn(t,6),this.unsigned),r=this,a="";;){var o=r.div(i),l=(r.sub(o.mul(i)).toInt()>>>0).toString(t);if((r=o).isZero())return l+a;for(;l.length<6;)l="0"+l;a=""+l+a}},ps.getHighBits=function(){return this.high},ps.getHighBitsUnsigned=function(){return this.high>>>0},ps.getLowBits=function(){return this.low},ps.getLowBitsUnsigned=function(){return this.low>>>0},ps.getNumBitsAbs=function(){if(this.isNegative())return this.eq(cs)?64:this.neg().getNumBitsAbs();for(var t=0!=this.high?this.high:this.low,e=31;e>0&&0==(t&1<<e);e--);return 0!=this.high?e+33:e+1},ps.isZero=function(){return 0===this.high&&0===this.low},ps.eqz=ps.isZero,ps.isNegative=function(){return!this.unsigned&&this.high<0},ps.isPositive=function(){return this.unsigned||this.high>=0},ps.isOdd=function(){return 1==(1&this.low)},ps.isEven=function(){return 0==(1&this.low)},ps.equals=function(t){return Vn(t)||(t=Qn(t)),(this.unsigned===t.unsigned||this.high>>>31!=1||t.high>>>31!=1)&&(this.high===t.high&&this.low===t.low)},ps.eq=ps.equals,ps.notEquals=function(t){return!this.eq(t)},ps.neq=ps.notEquals,ps.ne=ps.notEquals,ps.lessThan=function(t){return this.comp(t)<0},ps.lt=ps.lessThan,ps.lessThanOrEqual=function(t){return this.comp(t)<=0},ps.lte=ps.lessThanOrEqual,ps.le=ps.lessThanOrEqual,ps.greaterThan=function(t){return this.comp(t)>0},ps.gt=ps.greaterThan,ps.greaterThanOrEqual=function(t){return this.comp(t)>=0},ps.gte=ps.greaterThanOrEqual,ps.ge=ps.greaterThanOrEqual,ps.compare=function(t){if(Vn(t)||(t=Qn(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},ps.comp=ps.compare,ps.negate=function(){return!this.unsigned&&this.eq(cs)?cs:this.not().add(as)},ps.neg=ps.negate,ps.add=function(t){Vn(t)||(t=Qn(t));var e=this.high>>>16,n=65535&this.high,s=this.low>>>16,i=65535&this.low,r=t.high>>>16,a=65535&t.high,o=t.low>>>16,l=0,u=0,h=0,c=0;return h+=(c+=i+(65535&t.low))>>>16,u+=(h+=s+o)>>>16,l+=(u+=n+a)>>>16,l+=e+r,Zn((h&=65535)<<16|(c&=65535),(l&=65535)<<16|(u&=65535),this.unsigned)},ps.subtract=function(t){return Vn(t)||(t=Qn(t)),this.add(t.neg())},ps.sub=ps.subtract,ps.multiply=function(t){if(thi