@tensorflow/tfjs-layers
Version:
TensorFlow layers API in JavaScript
17 lines • 334 kB
JavaScript
/**
* @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.
* =============================================================================
*/
!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 s=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,s.get?s:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,e}function s(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}var i=n(e);class r extends Error{constructor(t){super(t),Object.setPrototypeOf(this,r.prototype)}}class a extends Error{constructor(t){super(t),Object.setPrototypeOf(this,a.prototype)}}class o extends Error{constructor(t){super(t),Object.setPrototypeOf(this,o.prototype)}}class l extends Error{constructor(t){super(t),Object.setPrototypeOf(this,l.prototype)}}class u extends Error{constructor(t){super(t),Object.setPrototypeOf(this,u.prototype)}}class h{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 c(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 p(t,e){if(!t)throw new u(e)}function d(t,e){let n=0;for(const s of t)s===e&&n++;return n}function f(t){return 1===t.length?t[0]:t}function g(t){return Array.isArray(t)?t:[t]}function m(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 y(t){return t.length<=1||-1===t.indexOf("_")?t:t.replace(/[_]+(\w|$)/g,((t,e)=>e.toUpperCase()))}let b={};function w(t){if(null==t)return null;const e={};return e.className=t.getClassName(),e.config=t.getConfig(),e}function k(t){if(null!=t&&"object"==typeof t)if(Array.isArray(t))t.forEach((t=>k(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?k(e):t[n]=e.value)}}}function v(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 b)r=b[i];else if(r=e[i],null==r)throw new o(`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 o(`${s}: Improper config format: ${JSON.stringify(r)}.\n'className' and 'config' must set.`);const a=r.className;let l,u;if(a in n?[l,u]=n[a]:a in b?[l,u]=b.className:a in e&&([l,u]=e[a]),null==l)throw new o(`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!=u){const t={};for(const e of Object.keys(b))t[e]=b[e];for(const e of Object.keys(n))t[e]=n[e];r.config.customObjects=t;const e=Object.assign({},b);for(const t of Object.keys(n))b[t]=n[t];k(r.config);const s=u(l,r.config,n,i);return b=Object.assign({},e),s}{const t=Object.assign({},b);for(const t of Object.keys(n))b[t]=n[t];const e=new l(r.config);return b=Object.assign({},t),e}}}function S(t,e){return-1*function(t,e){return t<e?-1:t>e?1:0}(t,e)}function x(t){if(null==t)return t;const e=[];for(const n of t)-1===e.indexOf(n)&&e.push(n);return e}function N(t){if(null==t)throw new o(`Invalid value in obj: ${JSON.stringify(t)}`);for(const e in t)if(t.hasOwnProperty(e))return!1;return!0}function I(t,e,n){if(null!=n&&t.indexOf(n)<0)throw new o(`${n} is not a valid ${e}. Valid values are ${t} or null/undefined.`)}function z(t,e,n=0,s=1/0){return p(n>=0),p(s>=n),Array.isArray(t)&&t.length>=n&&t.length<=s&&t.every((t=>typeof t===e))}function A(t,n){Array.isArray(t)?(e.util.assert(t.length>0,(()=>`${n} is unexpectedly an empty array.`)),t.forEach(((t,e)=>A(t,`element ${e+1} of ${n}`)))):e.util.assert(Number.isInteger(t)&&t>0,(()=>`Expected ${n} to be a positive integer, but got ${E(t)}.`))}function E(t){return null===t?"null":Array.isArray(t)?"["+t.map((t=>E(t))).join(",")+"]":"string"==typeof t?`"${t}"`:`${t}`}function T(t){return"relu"===t?"relu":"linear"===t?"linear":"elu"===t?"elu":null}let C=0;function $(){return C++}const F={};function D(t=""){return t in F||(F[t]=0),F[t]+=1,t+F[t].toString()}const L=["channelsFirst","channelsLast"],_=["nearest","bilinear"],R=["valid","same","causal"],O=["max","avg"],M=["sum","mul","concat","ave"],B=new Map;function P(t){I(L,"DataFormat",t)}function U(t){I(R,"PaddingMode",t)}function W(t){I(O,"PoolMode",t)}const j=[];function q(t,e){j.push(t);try{const t=e();return j.pop(),t}catch(t){throw j.pop(),t}}function V(t){if(!H(t))throw new Error("Not a valid tensor name: '"+t+"'");return(0===j.length?"":j.join("/")+"/")+t}function K(t){if(!H(t))throw new Error("Not a valid tensor name: '"+t+"'");B.has(t)||B.set(t,0);const e=B.get(t);if(B.set(t,B.get(t)+1),e>0){const n=`${t}_${e}`;return B.set(n,1),n}return t}const G=new RegExp(/^[A-Za-z0-9][-A-Za-z0-9\._\/]*$/);function H(t){return!!t.match(G)}function J(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 Z(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 Y(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 X(t,e){if(e<t)throw new o(`end (${e}) < begin (${t}) is forbidden.`);const n=[];for(let s=t;s<e;++s)n.push(s);return n}let Q;function tt(){return null==Q&&(Q=e.backend().epsilon()),Q}function et(t,e){return i.cast(t,e)}function nt(t,e=-1){const n=t.shape.slice();return e<0&&(e=n.length+e+1),n.splice(e,0,1),i.reshape(t,n)}function st(t,n,s){return e.tidy((()=>{switch(t.rank){case 1:return i.slice1d(t,n,s);case 2:return i.slice2d(t,[n,0],[s,t.shape[1]]);case 3:return i.slice3d(t,[n,0,0],[s,t.shape[1],t.shape[2]]);case 4:return i.slice4d(t,[n,0,0,0],[s,t.shape[1],t.shape[2],t.shape[3]]);case 5:return i.slice(t,[n,0,0,0,0],[s,t.shape[1],t.shape[2],t.shape[3],t.shape[4]]);case 6:return i.slice(t,[n,0,0,0,0,0],[s,t.shape[1],t.shape[2],t.shape[3],t.shape[4],t.shape[5]]);default:throw new o(`sliceAlongFirstAxis() received an unsupported tensor rank: ${t.rank}`)}}))}function it(t,n,s){return e.tidy((()=>{switch(t.rank){case 1:return i.slice1d(t,n,s);case 2:return i.slice2d(t,[0,n],[t.shape[0],s]);case 3:return i.slice3d(t,[0,0,n],[t.shape[0],t.shape[1],s]);case 4:return i.slice4d(t,[0,0,0,n],[t.shape[0],t.shape[1],t.shape[2],s]);default:throw new o(`sliceAlongLastAxis() received an unsupported tensor rank: ${t.rank}`)}}))}function rt(t,n,s,r){return e.tidy((()=>{switch(t.rank){case 1:return i.slice1d(t,n,s);case 2:switch(r){case 1:return st(t,n,s);case 2:return it(t,n,s);default:throw new o(`The axis is not within the rank of the tensor ${r}`)}case 3:switch(r){case 1:return st(t,n,s);case 2:return i.slice3d(t,[0,n,0],[t.shape[0],s,t.shape[2]]);case 3:return it(t,n,s);default:throw new o(`The axis is not within the rank of the tensor ${r}`)}case 4:switch(r){case 1:return st(t,n,s);case 2:return i.slice4d(t,[0,n,0,0],[t.shape[0],s,t.shape[2],t.shape[3]]);case 3:return i.slice4d(t,[0,0,n,0],[t.shape[0],t.shape[1],s,t.shape[3]]);case 4:return it(t,n,s);default:throw new o(`The axis is not within the rank of the tensor ${r}`)}default:throw new o(`sliceAlongLastAxis() received an unsupported tensor rank: ${t.rank}`)}}))}function at(t,e=-1){let n;return e<0&&(n=t[0].rank,e=0!==n?n:0),e===t[0].rank&&(e=-1),i.concat(t,e)}function ot(t,e){switch(t.rank){case 1:return i.concat1d([t,e]);case 2:return i.concat2d([t,e],0);case 3:return i.concat3d([t,e],0);case 4:return i.concat4d([t,e],0);default:throw new o(`concatAlongFirstAxis() received an unsupported tensor rank: ${t.rank}`)}}function lt(t,e){if(Array.isArray(e)||(e=[e]),t.rank!==e.length)throw new o(`The length of input n (${e.length}) does not match the number of dimensions in input x (${t.rank})`);return i.tile(t,e)}function ut(t,e=0,n=1,s,r){return i.randomNormal(t,e,n,s,r)}function ht(t,e,n,s){if(t.rank<2||e.rank<2)throw new l(`dot requires both inputs to be rank >= 2 but got x shape = ${t.shape} and y shape = ${e.shape}`);if(e.rank>=3){if(t.shape.slice(-1)[0]!==e.shape.slice(-2)[0])throw new l(`If rank y >= 3, then the second last dim of y must equal the last dim of x but got x shape = ${t.shape} and y shape = ${e.shape}`)}if(2===t.rank&&2===e.rank){const r=!1,a=!1;return i.fused.matMul({a:t,b:e,transposeA:r,transposeB:a,bias:s?dt(t.rank,s,"channelsLast"):null,activation:n})}{const r=t.shape.slice(),a=r.pop();t=i.reshape(t,[-1,a]);const o=e.shape.slice(),l=o.pop(),u=o.pop(),h=[...o,l],c=Array.from({length:e.rank},((t,n)=>0===n?e.rank-2:n<=e.rank-2?n-1:n));e=i.reshape(i.transpose(e,c),[u,-1]);const p=[...r,...h],d=!1,f=!1;return i.reshape(i.fused.matMul({a:t,b:e,transposeA:d,transposeB:f,bias:s?dt(t.rank,s,"channelsLast"):null,activation:n}),p)}}function ct(t,n,s){return e.tidy((()=>(n=Array.isArray(n)?e.tensor1d(n,"int32"):i.cast(n,"int32"),i.gather(t,n,s))))}function pt(t){return i.mul(t,t)}function dt(t,e,n){const s=e.shape;if(1!==e.rank&&e.rank!==t)throw new o(`Unexpected bias dimensions: ${e.rank}; expected it to be 1 or ${t}`);if(5===t){if("channelsFirst"===n)return 1===s.length?i.reshape(e,[1,s[0],1,1,1]):i.reshape(e,[1,s[3],s[0],s[1],s[2]]);if("channelsLast"===n)return 1===s.length?i.reshape(e,[1,1,1,1,s[0]]):i.reshape(e,[1].concat(s))}else if(4===t){if("channelsFirst"===n)return 1===s.length?i.reshape(e,[1,s[0],1,1]):i.reshape(e,[1,s[2],s[0],s[1]]);if("channelsLast"===n)return 1===s.length?i.reshape(e,[1,1,1,s[0]]):i.reshape(e,[1].concat(s))}else if(3===t){if("channelsFirst"===n)return 1===s.length?i.reshape(e,[1,s[0],1]):i.reshape(e,[1,s[1],s[0]]);if("channelsLast"===n)return 1===s.length?i.reshape(e,[1,1,s[0]]):i.reshape(e,[1].concat(s))}else if(t<3)return e;throw new o(`Unsupported input rank by biasAdd: ${e.rank}`)}function ft(t,n,s){return e.tidy((()=>(null==s&&(s="channelsLast"),P(s),i.add(t,dt(t.rank,n,s)))))}function gt(t,n,s,r){return e.tidy((()=>i.dropout(t,n,s,r)))}function mt(t,e,n=!1){return n?t():e()}const yt=["fanIn","fanOut","fanAvg"],bt=["normal","uniform","truncatedNormal"];class wt extends e.serialization.Serializable{fromConfigUsesCustomObjects(){return!1}getConfig(){return{}}}class kt extends wt{apply(t,n){return e.zeros(t,n)}}kt.className="Zeros",e.serialization.registerClass(kt);class vt extends wt{apply(t,n){return e.ones(t,n)}}vt.className="Ones",e.serialization.registerClass(vt);class St extends wt{constructor(t){if(super(),"object"!=typeof t)throw new o(`Expected argument of type ConstantConfig but got ${t}`);if(void 0===t.value)throw new o(`config must have value set but got ${t}`);this.value=t.value}apply(t,n){return e.tidy((()=>e.mul(e.scalar(this.value),e.ones(t,n))))}getConfig(){return{value:this.value}}}St.className="Constant",e.serialization.registerClass(St);class xt extends wt{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,n){return e.randomUniform(t,this.minval,this.maxval,n,this.seed)}getConfig(){return{minval:this.minval,maxval:this.maxval,seed:this.seed}}}xt.className="RandomUniform",e.serialization.registerClass(xt);class Nt extends wt{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 l(`randomNormal does not support dType ${e}.`);return ut(t,this.mean,this.stddev,e,this.seed)}getConfig(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}}}Nt.className="RandomNormal",e.serialization.registerClass(Nt);class It extends wt{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,n){if("float32"!==(n=n||"float32")&&"int32"!==n)throw new l(`truncatedNormal does not support dType ${n}.`);return e.truncatedNormal(t,this.mean,this.stddev,n,this.seed)}getConfig(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}}}It.className="TruncatedNormal",e.serialization.registerClass(It);let zt=class extends wt{constructor(t){super(),this.gain=null!=t.gain?t.gain:1}apply(t,n){return e.tidy((()=>{if(2!==t.length||t[0]!==t[1])throw new o("Identity matrix initializer can only be used for 2D square matrices.");return e.mul(this.gain,e.eye(t[0]))}))}getConfig(){return{gain:this.gain}}};zt.className="Identity",e.serialization.registerClass(zt);class At extends wt{constructor(t){if(super(),t.scale<0)throw new o(`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,I(yt,"FanMode",e),this.distribution=null==t.distribution?"normal":t.distribution,function(t){I(bt,"Distribution",t)}(this.distribution),this.seed=t.seed}apply(t,n){const s=function(t,e="channelsLast"){let n,s;if(P(e),2===t.length)n=t[0],s=t[1];else if(-1!==[3,4,5].indexOf(t.length)){if("channelsFirst"===e){const e=J(t,2);n=t[1]*e,s=t[0]*e}else if("channelsLast"===e){const e=J(t,0,t.length-2);n=t[t.length-2]*e,s=t[t.length-1]*e}}else{const e=J(t);n=Math.sqrt(e),s=Math.sqrt(e)}return[n,s]}(t),i=s[0],r=s[1];let a=this.scale;if("fanIn"===this.mode?a/=Math.max(1,i):"fanOut"===this.mode?a/=Math.max(1,r):a/=Math.max(1,(i+r)/2),"normal"===this.distribution){const s=Math.sqrt(a);if("float32"!==(n=n||"float32")&&"int32"!==n)throw new l(`${this.getClassName()} does not support dType ${n}.`);return e.truncatedNormal(t,0,s,n,this.seed)}{const s=Math.sqrt(3*a);return e.randomUniform(t,-s,s,n,this.seed)}}getConfig(){return{scale:this.scale,mode:this.mode,distribution:this.distribution,seed:this.seed}}}At.className="VarianceScaling",e.serialization.registerClass(At);class Et extends At{constructor(t){super({scale:1,mode:"fanAvg",distribution:"uniform",seed:null==t?null:t.seed})}getClassName(){return At.className}}Et.className="GlorotUniform",e.serialization.registerClass(Et);class Tt extends At{constructor(t){super({scale:1,mode:"fanAvg",distribution:"normal",seed:null==t?null:t.seed})}getClassName(){return At.className}}Tt.className="GlorotNormal",e.serialization.registerClass(Tt);class Ct extends At{constructor(t){super({scale:2,mode:"fanIn",distribution:"normal",seed:null==t?null:t.seed})}getClassName(){return At.className}}Ct.className="HeNormal",e.serialization.registerClass(Ct);class $t extends At{constructor(t){super({scale:2,mode:"fanIn",distribution:"uniform",seed:null==t?null:t.seed})}getClassName(){return At.className}}$t.className="HeUniform",e.serialization.registerClass($t);class Ft extends At{constructor(t){super({scale:1,mode:"fanIn",distribution:"normal",seed:null==t?null:t.seed})}getClassName(){return At.className}}Ft.className="LeCunNormal",e.serialization.registerClass(Ft);class Dt extends At{constructor(t){super({scale:1,mode:"fanIn",distribution:"uniform",seed:null==t?null:t.seed})}getClassName(){return At.className}}Dt.className="LeCunUniform",e.serialization.registerClass(Dt);class Lt extends wt{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 e.tidy((()=>{if(t.length<2)throw new l("Shape must be at least 2D.");if("int32"!==n&&"float32"!==n&&void 0!==n)throw new TypeError(`Unsupported data type ${n}.`);const s=e.util.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=ut([Math.max(i,s),Math.min(i,s)],0,1,n,this.seed),o=e.linalg.qr(a,!1);let u=o[0];const h=o[1].flatten().stridedSlice([0],[Math.min(i,s)*Math.min(i,s)],[Math.min(i,s)+1]);return u=e.mul(u,h.sign()),s<i&&(u=u.transpose()),e.mul(e.scalar(this.gain),u.reshape(t))}))}getConfig(){return{gain:this.gain,seed:this.seed}}}Lt.className="Orthogonal",e.serialization.registerClass(Lt);const _t={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 Rt(t,n={}){return v(t,e.serialization.SerializationMap.getMap().classNameMap,n,"initializer")}function Ot(t){return w(t)}function Mt(t){if("string"==typeof t){const e=t in _t?_t[t]:t;if("GlorotNormal"===e)return new Tt;if("GlorotUniform"===e)return new Et;if("HeNormal"===e)return new Ct;if("HeUniform"===e)return new $t;if("LeCunNormal"===e)return new Ft;if("LeCunUniform"===e)return new Dt;{const t={};return t.className=e,t.config={},Rt(t)}}return t instanceof wt?t:Rt(t)}function Bt(t){return Array.isArray(t)&&Array.isArray(t[0])}function Pt(t){return 0===t.length?[]:Array.isArray(t[0])?t:[t]}function Ut(t){let e;if(Array.isArray(t)){if(1!==t.length)throw new o(`Expected Tensor length to be 1; got ${t.length}`);e=t[0]}else e=t;return e}function Wt(t){if(Array.isArray(t)&&Array.isArray(t[0])){if(1===t.length)return t[0];throw new o(`Expected exactly 1 Shape; got ${t.length}`)}return t}function jt(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}const qt="Variable";class Vt{constructor(t,e="float32",n="Variable",s=!0,r=null){this.dtype=null==e?"float32":e,this.shape=t.shape,this.id=$(),n=null==n?qt:n,this.originalName=V(n),this.name=K(this.originalName),this.trainable_=s,this.constraint=r,this.val=i.variable(t,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 Kt(t){return t.map((t=>t.read()))}function Gt(t){t.forEach((t=>{t[0].write(t[1])}))}class Ht{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 Jt{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=$(),null!=r&&(this.originalName=V(r),this.name=K(this.originalName)),this.rank=e.length}}let Zt=0;class Yt{constructor(t,e){this.callArgs=e,this.id=Zt++,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 Xt=0;class Qt extends e.serialization.Serializable{constructor(t={}){super(),this._callHook=null,this._addedWeightNames=[],this._stateful=!1,this.id=Xt++,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=m(t)+"_"+D(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 a(`The layer has never been called and thus has no defined ${e}.`);if(this.inboundNodes.length<=t)throw new o(`Asked to get ${e} at node ${t}, but the layer has only ${this.inboundNodes.length} inbound nodes.`);return this.inboundNodes[t]}getInputAt(t){return f(this.getNodeAtIndex(t,"input").inputTensors)}getOutputAt(t){return f(this.getNodeAtIndex(t,"output").outputTensors)}get input(){if(this.inboundNodes.length>1)throw new r(`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 r(`Layer ${this.name} is not connected, no input to return.`);return f(this.getNodeAtIndex(0,"input").inputTensors)}get output(){if(0===this.inboundNodes.length)throw new r(`Layer ${this.name} has no inbound nodes.`);if(this.inboundNodes.length>1)throw new r(`Layer ${this.name} has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use \`getOutputAt(nodeIndex)\` instead.`);return f(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=g(t);if(null==this.inputSpec||0===this.inputSpec.length)return;const n=g(this.inputSpec);if(e.length!==n.length)throw new o(`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 o(`Input ${t} is incompatible with layer ${this.name}: expected ndim=${i.ndim}, found ndim=${r}`);if(null!=i.maxNDim&&r>i.maxNDim)throw new o(`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 o(`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 o(`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 o(`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 o(`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=g(t),s=function(t){let e=!0;for(const n of g(t))if(!(n instanceof Jt)){e=!1;break}return e}(t),i=function(t){let e=!0;for(const n of g(t))if(n instanceof Jt){e=!1;break}return e}(t);if(s===i)throw new o("Arguments to apply() must be all SymbolicTensors or all Tensors");return q(this.name,(()=>{if(!this.built){this.assertInputCompatibility(t);const e=[];for(const n of g(t))e.push(n.shape);this.build(f(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=g(s),r=[];for(let t of i)-1!==n.indexOf(t)&&(t=t.clone()),r.push(t);if(s=f(r),null!=this.activityRegularizer)throw new l("Layer invocation in the presence of activity regularizer(s) is not supported yet.");return s}{const n=function(t){t=g(t);const e=[];for(const n of t)e.push(n.shape);return f(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 Jt(r,n,this,g(t),e,this.name,s))):new Jt(r,s,this,g(t),e,this.name),this.addInboundNode(t,i,null,null,n,s,e),this._refCount++,null!=this.activityRegularizer)throw new l("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 r(`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 r(`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 a(`You tried to call countParams() on ${this.name}, but the layer is not built yet. Build it first by calling build(batchInputShape).`);return jt(this.weights)}build(t){this.built=!0}getWeights(t=!1){return Kt(t?this.trainableWeights:this.weights)}setWeights(t){e.tidy((()=>{const n=this.weights;if(n.length!==t.length)throw new o(`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=Kt(n);for(let r=0;r<i.length;++r){const a=i[r],l=n[r],u=t[r];if(!e.util.arraysEqual(a.shape,u.shape))throw new o(`Layer weight shape ${a.shape} not compatible with provided weight shape ${u.shape}`);s.push([l,u])}Gt(s)}))}addWeight(t,e,n,s,i,r,a,l){if(-1!==this._addedWeightNames.indexOf(t))throw new o(`Duplicate weight name ${t} for layer ${this.name}`);this._addedWeightNames.push(t),null==n&&(n="float32"),this.fastWeightInitDuringBuild&&(s=null!=l?l():Mt("zeros"));const u=s.apply(e,n),h=new Vt(u,n,t,r,a);return u.dispose(),null!=i&&this.addLoss((()=>i.apply(h.read()))),null==r&&(r=!0),r?this._trainableWeights.push(h):this._nonTrainableWeights.push(h),h}setFastWeightInitDuringBuild(t){this.fastWeightInitDuringBuild=t}addLoss(t){null==t||Array.isArray(t)&&0===t.length||(t=g(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=g(e),r=g(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=g(t);e=g(e),n=g(n),s=g(s),i=Pt(i),r=Pt(r);const l=[],u=[],h=[];for(const t of o)l.push(t.sourceLayer),u.push(t.nodeIndex),h.push(t.tensorIndex);new Yt({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 te(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=te(t.inputTensors[n],t.inboundLayers[n],t.nodeIndices[n]);for(const t of s)-1===e.indexOf(t)&&e.push(t)}return e}}}class ee extends Qt{constructor(t){if(super({dtype:t.dtype,name:null!=t.name?t.name:D("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 o("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 o("An InputLayer should be passed either a `batchInputShape` or an `inputShape`.");e=[t.batchSize].concat(t.inputShape)}else if(null!=t.batchSize)throw new o("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 Jt(this.dtype,this.batchInputShape,this,[],{},this.name);s.nodeIndex=0,s.tensorIndex=0,new Yt({outboundLayer:this,inboundLayers:[],nodeIndices:[],tensorIndices:[],inputTensors:[s],outputTensors:[s],inputMasks:[null],outputMasks:[null],inputShapes:[e],outputShapes:[e]})}apply(t,e){throw new o(`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 ne(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 o("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 ee({batchInputShape:e,name:t.name,dtype:n,sparse:t.sparse}).inboundNodes[0].outputTensors[0]}ee.className="InputLayer",e.serialization.registerClass(ee);class se{constructor(t){if(this.id2Value={},this.id2Mask={},this.name2Id={},t instanceof se)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,n,s){if(null!=this.id2Value[t.id])throw new o(`Duplicate key: name=${t.name}, id=${t.id}`);return this.id2Value[t.id]=function(t,n){if(null==t.dtype||t.dtype===n.dtype)return n;try{return e.cast(n,t.dtype)}catch(e){throw new o(`The dtype of the feed (${n.dtype}) can not be cast to the dtype of the key '${t.name}' (${t.dtype}).`)}}(t,n),this.name2Id[t.name]=t.id,null!=s&&(this.id2Mask[t.id]=s),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 Jt){if(null==this.id2Value[t.id])throw new o(`Nonexistent key: ${t.name}`);return this.id2Value[t.id]}{const e=this.name2Id[t];if(null==e)throw new o(`Feed dict has no SymbolicTensor name: ${t}`);return this.id2Value[e]}}getMask(t){if(t instanceof Jt){if(null==this.id2Value[t.id])throw new o(`Nonexistent key: ${t.name}`);return this.id2Mask[t.id]}{const e=this.name2Id[t];if(null==e)throw new o(`Feed dict has no SymbolicTensor name: ${t}`);return this.id2Mask[e]}}disposeMasks(){null!=this.id2Mask&&e.dispose(this.id2Mask)}}const ie=new h,re=new h;function ae(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=ie.get(c);if(null==d){const t=function(t,n){e.util.assert(null!=t&&t.length>0,(()=>"Expected at least one fetch, got none"));let s=[],i={};if(1===t.length){const e=le(t[0],n);s=e.sorted,i=e.recipientMap}else{const e=new Set;for(const r of t){const{sorted:t,recipientMap:a}=le(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:oe(i)}}(o,n);d=t.sorted,p=t.recipientCounts,ie.put(c,d),re.put(c,p)}p={},r||Object.assign(p,re.get(c));const f=new se(n);for(let t=0;t<d.length;++t){if(null!=i){const t=e.memory().numTensors;t>i.maxNumTensors&&(i.maxNumTensors=t),t<i.minNumTensors&&(i.minNumTensors=t)}const a=d[t],o=a.sourceLayer;if(o instanceof ee)continue;const h=[],c=[],m=[];let y=!1;for(const t of a.inputs){const e=f.getValue(t),s=f.getMask(t);h.push(e),c.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||m.push(e))}y&&((s=s||{}).mask=c[0]);const b=g(o.apply(h,s));let w=null;o.supportsMasking&&(w=o.computeMask(h,c));const k=ue(a),v=Array.isArray(k)?k:[k];for(let t=0;t<v.length;++t){f.hasKey(v[t])||f.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||e.dispose(m)}return f.disposeMasks(),a?u:u[0]}function oe(t){const e={};for(const n in t)e[n]=t[n].size;return e}function le(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 ue(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}e.env().registerFlag("TOPOLOGICAL_SORT_CACHE_MAX_ENTRIES",(()=>100),(function(t){null!=ie&&ie.setMaxEntries(t),null!=re&&re.setMaxEntries(t)}));const he="Add",ce="BatchMatMul",pe="BatchToSpaceND",de="Cast",fe="Concat",ge="Conv2D",me="Conv2DBackpropInput",ye="Cosh",be="Cumsum",we="RealDiv",ke="ExpandDims",ve="Floor",Se="FloorDiv",xe="GatherV2",Ne="GreaterEqual",Ie="Identity",ze="Maximum",Ae="Multiply",Ee="Pack",Te="PadV2",Ce="Reshape",$e="Reverse",Fe="Rsqrt",De="Select",Le="Slice",_e="Sinh",Re="Sigmoid",Oe="Sqrt",Me="SpaceToBatchND",Be="SplitV",Pe="Tile",Ue="Transpose",We="Unpack",je="UnsortedSegmentSum",qe="ZerosLike",Ve="Step";function Ke(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 Ge(t,e){if(!t)throw new Error("string"==typeof e?e:e())}function He(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 Je(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 Ze(t){return t%1==0}function Ye(t,e){return e<=t.length?t:t+" ".repeat(e-t.length)}function Xe(t,e){const n=e.length;return Ge((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}`)),Ge(t.every((t=>Ze(t))),(()=>`All values in axis param must be integers but got axis ${t}`)),t.map((t=>t<0?n+t:t))}function Qe(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 tn(t){return"string"==typeof t||t instanceof String}function en(t){return Array.isArray(t)?en(t[0]):t instanceof Float32Array?"float32":t instanceof Int32Array||t instanceof Uint8Array||t instanceof Uint8ClampedArray?"int32":"number"==typeof t?"float32":tn(t)?"string":function(t){return"boolean"==typeof t}(t)?"bool":"float32"}function nn(t){return!!(t&&t.constructor&&t.call&&t.apply)}function sn(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 rn(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]=rn(t+e*o,a,n,s)}return i}function an(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 rn(0,t,e,n)}function on(t,e){const n=ln(t,e);for(let t=0;t<n.length;t++)n[t]=1;return n}function ln(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 un(t){t.forEach((e=>{Ge(Number.isInteger(e)&&e>=0,(()=>`Tensor must have a shape comprised of positive integers but got shape [${t}].`))}))}function hn(t){return t&&t.then&&"function"==typeof t.then}const cn="tfjsflags";class pn{constructor(t){this.global=t,this.flags={},this.flagRegistry={},this.urlFlags={},this.getQueryParams=dn,this.populateURLFlags()}setPlatform(t,e){null!=this.platform&&(fn().getBool("IS_TEST")||fn().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];fn().getBool("IS_TEST")||fn().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(hn(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(cn 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 dn(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 fn(){return mn}let gn,mn=null;function yn(){if(null==gn){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}gn=t}return gn}function bn(t,e){const n=function(){const t=yn();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 wn(...t){fn().getBool("IS_TEST")||fn().getBool("PROD")||console.warn(...t)}const kn=bn("kernelRegistry",(()=>new Map)),vn=bn("gradRegistry",(()=>new Map));function Sn(t,e){const n=function(t,e){return`${e}_${t}`}(t,e);return kn.get(n)}function xn(t){return vn.get(t)}function Nn(t){const e=kn.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 In(t){const{kernelName:e}=t;vn.has(e)&&fn().getBool("DEBUG")&&wn(`Overriding the gradient for '${e}'`),vn.set(e,t)}var zn="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function An(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function En(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 Tn=$n,Cn=null;try{Cn=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 $n(t,e,n){this.low=0|t,this.high=0|e,this.unsigned=!!n}function Fn(t){return!0===(t&&t.__isLong__)}$n.prototype.__isLong__,Object.defineProperty($n.prototype,"__isLong__",{value:!0}),$n.isLong=Fn;var Dn={},Ln={};function _n(t,e){var n,s,i;return e?(i=0<=(t>>>=0)&&t<256)&&(s=Ln[t])?s:(n=On(t,(0|t)<0?-1:0,!0),i&&(Ln[t]=n),n):(i=-128<=(t|=0)&&t<128)&&(s=Dn[t])?s:(n=On(t,t<0?-1:0,!1),i&&(Dn[t]=n),n)}function Rn(t,e){if(isNaN(t))return e?Kn:Vn;if(e){if(t<0)return Kn;if(t>=Wn)return Yn}else{if(t<=-jn)return Xn;if(t+1>=jn)return Zn}return t<0?Rn(-t,e).neg():On(t%Un|0,t/Un|0,e)}function On(t,e,n){return new $n(t,e,n)}$n.fromInt=_n,$n.fromNumber=Rn,$n.fromBits=On;var Mn=Math.pow;function Bn(t,e,n){if(0===t.length)throw Error("empty string");if("NaN"===t||"Infinity"===t||"+Infinity"===t||"-Infinity"===t)return Vn;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 Bn(t.substring(1),e,n).neg();for(var i=Rn(Mn(n,8)),r=Vn,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=Rn(Mn(n,o));r=r.mul(u).add(Rn(l))}else r=(r=r.mul(i)).add(Rn(l))}return r.unsigned=e,r}function Pn(t,e){return"number"==typeof t?Rn(t,e):"string"==typeof t?Bn(t,e):On(t.low,t.high,"boolean"==typeof e?e:t.unsigned)}$n.fromString=Bn,$n.fromValue=Pn;var Un=4294967296,Wn=Un*Un,jn=Wn/2,qn=_n(1<<24),Vn=_n(0);$n.ZERO=Vn;var Kn=_n(0,!0);$n.UZERO=Kn;var Gn=_n(1);$n.ONE=Gn;var Hn=_n(1,!0);$n.UONE=Hn;var Jn=_n(-1);$n.NEG_ONE=Jn;var Zn=On(-1,2147483647,!1);$n.MAX_VALUE=Zn;var Yn=On(-1,-1,!0);$n.MAX_UNSIGNED_VALUE=Yn;var Xn=On(0,-2147483648,!1);$n.MIN_VALUE=Xn;var Qn=$n.prototype;Qn.toInt=function(){return this.unsigned?this.low>>>0:this.low},Qn.toNumber=function(){return this.unsigned?(this.high>>>0)*Un+(this.low>>>0):this.high*Un+(this.low>>>0)},Qn.toString=function(t){if((t=t||10)<2||36<t)throw RangeError("radix");if(this.isZero())return"0";if(this.isNegative()){if(this.eq(Xn)){var e=Rn(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=Rn(Mn(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}},Qn.getHighBits=function(){return this.high},Qn.getHighBitsUnsigned=function(){return this.high>>>0},Qn.getLowBits=function(){return this.low},Qn.getLowBitsUnsigned=function(){return this.low>>>0},Qn.getNumBitsAbs=function(){if(this.isNegative())return this.eq(Xn)?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},Qn.isZero=function(){return 0===this.high&&0===this.low},Qn.eqz=Qn.isZero,Qn.isNegative=function(){return!this.unsigned&&this.high<0},Qn.isPositive=function(){return this.unsigned||this.high>=0},Qn.isOdd=function(){return 1==(1&this.low)},Qn.isEven=function(){return 0==(1&this.low)},Qn.equals=function(t){return Fn(t)||(t=Pn(t)),(this.unsigned===t.unsigned||this.high>>>31!=1||t.high>>>31!=1)&&(this.high===t.high&&this.low===t.low)},Qn.eq=Qn.equals,Qn.notEquals=function(t){return!this.eq(t)},Qn.neq=Qn.notEquals,Qn.ne=Qn.notEquals,Qn.lessThan=function(t){return this.comp(t)<0},Qn.lt=Qn.lessThan,Qn.lessThanOrEqual=function(t){return this.comp(t)<=0},Qn.lte=Qn.lessThanOrEqual,Qn.le=Qn.lessThanOrEqual,Qn.greaterThan=function(t){return this.comp(t)>0},Qn.gt=Qn.greaterThan,Qn.greaterThanOrEqual=function(t){return this.comp(t)>=0},Qn.gte=Qn.greaterThanOrEqual,Qn.ge=Qn.greaterThanOrEqual,Qn.compare=function(t){if(Fn(t)||(t=Pn(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>t