From b4a0d7634e55286d8bb59af0562c79ed023e31c5 Mon Sep 17 00:00:00 2001 From: Joey Lee Date: Fri, 24 May 2019 11:41:17 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=88=20New=20Release:=20ml5.js=20v0.3.0?= =?UTF-8?q?=20=F0=9F=8E=89=20(#386)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add ImageData as valid image type * add CVAE * add latent dim * add random generate * fix p5Image support * fix CVAE parameter * Added a parameter to the save function so that it is possible to add a custom filename to the model that is saved. * Unet fix (#357) Add uNet model and additional fixes - adds uNet model from @zaidalyafeai ✨ - adds preload() for uNet - uses loadImage on window.loadImage vs. window.p5.loadImage * Added sentiment analysis (#339) * Added sentiment analysis * delete files * fixed issues for pull request * add p5 utils (#358) * fix charRNN tests (#349) * add tests to CharRNN * test(CharRNN): add tests to CharRNN added descriptive tests to ensure CharRNN behaves like its example * remove dist * Add tests to CharRNN (#307) * add tests to CharRNN * test(CharRNN): add tests to CharRNN added descriptive tests to ensure CharRNN behaves like its example * remove dist * check preload support for other nets and classifiers (#313) Adds specified nets to support preload // TODO: add examples showing appropriate use of preload * change CharRNN specs to meet time limit, add initial code for videoClassifier * videoClassifier functioning * charRNN functional * fix out of date file * add preload support for cvae (#360) * Update TensorFlow.js to 1.0.2 (#336) * upgrade to tfjs1.0.0 * fix loadModel * fix buffer * fix getLayer * Adds fixes to PR #332 for tfjs 1.0.2 updates (#366) * upgrade to tfjs1.0.0 * fix loadModel * fix buffer * fix getLayer * updated package lock * added @tensorflow/tfjs-core as dependency * add graphmodel for infer (#365) * Add DCGAN Model into ml5 (#351) * Create index.js * updated index.js and DCGAN/index.js * DCGAN updates and fixes (#362) * Create index.js * fixed DCGAN errors * updates p5Utils destructuring, fixes linting issues, and updates tfjs to 1.0.2 to match dcgan reqs * fixed cvae * use this.model instead of using model as param to this.compute() * Makes UNET compatible with tfjs 1.0.2 (#367) * added package-lock * updated UNET for use with tfjs 1.0.2 * Makes Sentiment compatible with tfjs 1.0.2 (#368) * added package-lock * rm sentiment-node * changed loadModel to loadLayersModel * Makes CVAE compatible with tfjs 1.0.2 (#369) * added package-lock * updates cvae to tfjs 1.0.2 api * update tfjs to 1.1.2 (#373) * featureExtractor: accept HTML canvas or p5 canvas when addImage(), classify() or predict() * fix: KNNClassifier accepts a number as class index when addExample(features, number) * added check for moz browser ref:https://stackoverflow.com/questions/48623376/typeerror-capturestream-is-not-a-function (#375) This addresses the video capture breaking in YOLO and potentially other video based functions that require the use of .captureStream(). As the .captureStream() function is still experimental, this adds the moz prefix and a browser check to see if we are using firefox or not. * rm todo * updated package-lock.json * Adds label number option to featureExtractor.classification() (#376) * changed numClasses to numLabels * added num label as option to classification() * updated FeatureExtractor Test with numLabels * adds object as param to .classificaiton() * moved options into this.config * fix feature extractor test - add .config * added pose:poseWithParts into .singlePose() (#381) * Adds jsdoc inline-documentation - work in progress (#378) * added jsdoc documentation for imageClassifier * adds dcgan documentation - needs checking * Add jsdoc (#382) * Add jsdocs for CharRNN * Add jsdocs for CVAE * Add jsdocs for FeatureExtractor * Add jsdocs for KNN * Add jsdocs for PitchDetection * Add jsdocs for Pix2pix * Add jsdocs for posenet * Add jsdocs for Sentiment * Add jsdocs for styletransfer * add linebreaks to long lines * added basic docs to sketchRnn * added basic docs to unet * added basic docs to word2vec * added basic yolo docs * Adds V0.3.0 to package.json and Readme for new release (#385) * changed package.json to v0.3.0 * added latest version reference in readme * added lib min - will remove after this release --- .gitignore | 3 +- README.md | 11 +- dist/ml5.min.js | 53 +- dist/ml5.min.js.map | 2 +- package-lock.json | 272 +- package.json | 12 +- src/CVAE/index.js | 140 + src/CharRNN/index.js | 62 +- src/CharRNN/index_test.js | 14 +- src/DCGAN/index.js | 121 + src/FeatureExtractor/Mobilenet.js | 236 +- src/FeatureExtractor/index.js | 21 + src/FeatureExtractor/index_test.js | 12 +- src/ImageClassifier/darknet.js | 8 +- src/ImageClassifier/index.js | 36 + src/KNNClassifier/index.js | 43 +- src/PitchDetection/index.js | 39 +- src/Pix2pix/index.js | 19 +- src/PoseNet/index.js | 42 +- src/Sentiment/index.js | 127 + src/SketchRNN/index.js | 7 + src/StyleTransfer/index.js | 18 +- src/UNET/index.js | 144 + src/Word2vec/index.js | 6 + src/YOLO/index.js | 15 +- src/index.js | 8 + src/utils/Video.js | 8 +- src/utils/imageUtilities.js | 2 +- src/utils/p5Utils.js | 66 + yarn.lock | 9269 ---------------------------- 30 files changed, 1248 insertions(+), 9568 deletions(-) create mode 100644 src/CVAE/index.js create mode 100644 src/DCGAN/index.js create mode 100644 src/Sentiment/index.js create mode 100644 src/UNET/index.js create mode 100644 src/utils/p5Utils.js delete mode 100644 yarn.lock diff --git a/.gitignore b/.gitignore index f3a9fa55b..b1b6f2356 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.env dev examples/es6/node_modules experiments/node_modules @@ -19,4 +20,4 @@ website/node_modules website/i18n/* !website/i18n/en.json -yarn-error.log \ No newline at end of file +yarn-error.log diff --git a/README.md b/README.md index a3adc1e1f..11ec80150 100644 --- a/README.md +++ b/README.md @@ -19,15 +19,20 @@ ml5.js is heavily inspired by [Processing](https://processing.org/) and [p5.js]( There are several ways you can use the ml5.js library: -* You can use the latest version (0.2.3) by adding it to the head section of your HTML document: +* You can use the latest version (0.3.0) by adding it to the head section of your HTML document: -**v0.2.3** +**v0.3.0** ```javascript - + ``` * If you need to use an earlier version for any reason, you can change the version number. +**v0.2.3** +```javascript + +``` + **v0.1.3** ```javascript diff --git a/dist/ml5.min.js b/dist/ml5.min.js index 07bf81499..7c200dbfc 100644 --- a/dist/ml5.min.js +++ b/dist/ml5.min.js @@ -1,7 +1,7 @@ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.ml5=t():e.ml5=t()}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var a=t[r]={i:r,l:!1,exports:{}};return e[r].call(a.exports,a,a.exports,n),a.l=!0,a.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.r=function(e){Object.defineProperty(e,"__esModule",{value:!0})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="/",n(n.s=682)}([function(e,t,n){"use strict";(function(e,r,a,i){n.d(t,"Fc",function(){return vu}),n.d(t,"Ya",function(){return bu}),n.d(t,"Ga",function(){return wu}),n.d(t,"Gb",function(){return xu}),n.d(t,"Dd",function(){return ou}),n.d(t,"Tb",function(){return is}),n.d(t,"La",function(){return ge}),n.d(t,"hb",function(){return Zs}),n.d(t,"Ec",function(){return nu}),n.d(t,"nd",function(){return iu}),n.d(t,"yd",function(){return V}),n.d(t,"Ed",function(){return su}),n.d(t,"a",function(){return lu}),n.d(t,"b",function(){return cu}),n.d(t,"c",function(){return fu}),n.d(t,"d",function(){return pu}),n.d(t,"g",function(){return du}),n.d(t,"h",function(){return uu}),n.d(t,"i",function(){return mu}),n.d(t,"l",function(){return hu}),n.d(t,"m",function(){return Q}),n.d(t,"n",function(){return X}),n.d(t,"Bd",function(){return te}),n.d(t,"o",function(){return ee}),n.d(t,"j",function(){return gt}),n.d(t,"k",function(){return Co}),n.d(t,"e",function(){return me}),n.d(t,"f",function(){return de}),n.d(t,"gb",function(){return ts}),n.d(t,"ob",function(){return Yo}),n.d(t,"zb",function(){return Go}),n.d(t,"ac",function(){return Je}),n.d(t,"H",function(){return qa}),n.d(t,"I",function(){return $a}),n.d(t,"J",function(){return Ga}),n.d(t,"G",function(){return Ha}),n.d(t,"Q",function(){return Qe}),n.d(t,"qc",function(){return et}),n.d(t,"fb",function(){return tt}),n.d(t,"R",function(){return vr}),n.d(t,"S",function(){return br}),n.d(t,"T",function(){return wr}),n.d(t,"U",function(){return xr}),n.d(t,"V",function(){return _r}),n.d(t,"Sc",function(){return Sr}),n.d(t,"W",function(){return ii}),n.d(t,"X",function(){return oi}),n.d(t,"Ea",function(){return si}),n.d(t,"Dc",function(){return ui}),n.d(t,"Y",function(){return li}),n.d(t,"Ab",function(){return Qa}),n.d(t,"Ja",function(){return ei}),n.d(t,"bc",function(){return ti}),n.d(t,"uc",function(){return ci}),n.d(t,"vc",function(){return fi}),n.d(t,"wc",function(){return pi}),n.d(t,"xc",function(){return hi}),n.d(t,"yc",function(){return di}),n.d(t,"Cb",function(){return mi}),n.d(t,"E",function(){return gi}),n.d(t,"Kc",function(){return yi}),n.d(t,"Lc",function(){return vi}),n.d(t,"Mc",function(){return bi}),n.d(t,"Nc",function(){return wi}),n.d(t,"Oc",function(){return xi}),n.d(t,"p",function(){return fa}),n.d(t,"q",function(){return pa}),n.d(t,"r",function(){return ha}),n.d(t,"z",function(){return da}),n.d(t,"A",function(){return ma}),n.d(t,"B",function(){return ga}),n.d(t,"D",function(){return ya}),n.d(t,"N",function(){return va}),n.d(t,"O",function(){return ba}),n.d(t,"Z",function(){return wa}),n.d(t,"Aa",function(){return xa}),n.d(t,"Oa",function(){return _a}),n.d(t,"Pa",function(){return Sa}),n.d(t,"Ra",function(){return Ea}),n.d(t,"Ua",function(){return Na}),n.d(t,"rb",function(){return Oa}),n.d(t,"sb",function(){return ka}),n.d(t,"tb",function(){return Ia}),n.d(t,"Sb",function(){return Ca}),n.d(t,"rc",function(){return Aa}),n.d(t,"zc",function(){return Pa}),n.d(t,"Ac",function(){return Ta}),n.d(t,"Gc",function(){return Ma}),n.d(t,"Hc",function(){return Ra}),n.d(t,"Ic",function(){return Da}),n.d(t,"Jc",function(){return ja}),n.d(t,"Qc",function(){return La}),n.d(t,"Tc",function(){return za}),n.d(t,"Uc",function(){return Ba}),n.d(t,"Zc",function(){return Fa}),n.d(t,"ed",function(){return Va}),n.d(t,"fd",function(){return Ua}),n.d(t,"v",function(){return Si}),n.d(t,"w",function(){return Ei}),n.d(t,"x",function(){return Ni}),n.d(t,"y",function(){return Oi}),n.d(t,"ub",function(){return ki}),n.d(t,"Bb",function(){return Ii}),n.d(t,"Fb",function(){return Ci}),n.d(t,"Hb",function(){return Ai}),n.d(t,"Mb",function(){return Pi}),n.d(t,"dd",function(){return Ti}),n.d(t,"Ma",function(){return Mi}),n.d(t,"Na",function(){return Ri}),n.d(t,"bb",function(){return Di}),n.d(t,"cb",function(){return ji}),n.d(t,"db",function(){return Li}),n.d(t,"eb",function(){return zi}),n.d(t,"kb",function(){return Bi}),n.d(t,"lb",function(){return Fi}),n.d(t,"mb",function(){return Vi}),n.d(t,"nb",function(){return Ui}),n.d(t,"Vb",function(){return Wi}),n.d(t,"Wb",function(){return qi}),n.d(t,"s",function(){return $i}),n.d(t,"t",function(){return Gi}),n.d(t,"u",function(){return Hi}),n.d(t,"C",function(){return Ki}),n.d(t,"Ha",function(){return Xi}),n.d(t,"Ia",function(){return Yi}),n.d(t,"Va",function(){return Ji}),n.d(t,"Db",function(){return Zi}),n.d(t,"Eb",function(){return Qi}),n.d(t,"Ib",function(){return eo}),n.d(t,"Jb",function(){return to}),n.d(t,"Kb",function(){return no}),n.d(t,"Lb",function(){return ro}),n.d(t,"Ob",function(){return ao}),n.d(t,"Pb",function(){return io}),n.d(t,"hc",function(){return oo}),n.d(t,"ic",function(){return so}),n.d(t,"Vc",function(){return uo}),n.d(t,"Wc",function(){return lo}),n.d(t,"bd",function(){return co}),n.d(t,"cd",function(){return fo}),n.d(t,"Ka",function(){return bo}),n.d(t,"jb",function(){return wo}),n.d(t,"jc",function(){return xo}),n.d(t,"sc",function(){return _o}),n.d(t,"Cc",function(){return So}),n.d(t,"vb",function(){return po}),n.d(t,"wb",function(){return ho}),n.d(t,"xb",function(){return mo}),n.d(t,"yb",function(){return go}),n.d(t,"Fd",function(){return yo}),n.d(t,"Gd",function(){return vo}),n.d(t,"L",function(){return Dr}),n.d(t,"rd",function(){return Rr}),n.d(t,"kc",function(){return jr}),n.d(t,"K",function(){return Lr}),n.d(t,"M",function(){return zr}),n.d(t,"P",function(){return Br}),n.d(t,"Ba",function(){return Fr}),n.d(t,"Da",function(){return Vr}),n.d(t,"Qa",function(){return Ur}),n.d(t,"Sa",function(){return Wr}),n.d(t,"Wa",function(){return qr}),n.d(t,"Rb",function(){return $r}),n.d(t,"Xb",function(){return Gr}),n.d(t,"cc",function(){return Hr}),n.d(t,"dc",function(){return Kr}),n.d(t,"ec",function(){return Xr}),n.d(t,"fc",function(){return Yr}),n.d(t,"gc",function(){return Jr}),n.d(t,"mc",function(){return Zr}),n.d(t,"nc",function(){return Qr}),n.d(t,"oc",function(){return ea}),n.d(t,"tc",function(){return ta}),n.d(t,"Rc",function(){return na}),n.d(t,"Xc",function(){return ra}),n.d(t,"Yc",function(){return aa}),n.d(t,"pd",function(){return ia}),n.d(t,"vd",function(){return oa}),n.d(t,"xd",function(){return sa}),n.d(t,"Ta",function(){return pt}),n.d(t,"pb",function(){return ht}),n.d(t,"Yb",function(){return ct}),n.d(t,"pc",function(){return dt}),n.d(t,"Bc",function(){return rt}),n.d(t,"gd",function(){return nt}),n.d(t,"hd",function(){return at}),n.d(t,"id",function(){return it}),n.d(t,"jd",function(){return ot}),n.d(t,"kd",function(){return st}),n.d(t,"ld",function(){return ut}),n.d(t,"md",function(){return lt}),n.d(t,"Hd",function(){return ft}),n.d(t,"Zb",function(){return xt}),n.d(t,"Id",function(){return _t}),n.d(t,"ud",function(){return Eo}),n.d(t,"Pc",function(){return Ze}),n.d(t,"qb",function(){return No}),n.d(t,"Ub",function(){return Oo}),n.d(t,"Xa",function(){return Ao}),n.d(t,"wd",function(){return Po}),n.d(t,"F",function(){return To}),n.d(t,"Qb",function(){return Mo}),n.d(t,"Nb",function(){return Ro}),n.d(t,"ad",function(){return Do}),n.d(t,"sd",function(){return jo}),n.d(t,"td",function(){return yu}),n.d(t,"od",function(){return Ee}),n.d(t,"ib",function(){return Ne}),n.d(t,"Fa",function(){return Oe}),n.d(t,"qd",function(){return ke}),n.d(t,"lc",function(){return Ie}),n.d(t,"Ca",function(){return _e}),n.d(t,"Za",function(){return ye}),n.d(t,"ab",function(){return ve}),n.d(t,"zd",function(){return be}),n.d(t,"Ad",function(){return we}),n.d(t,"Cd",function(){return xe}); +!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ml5=e():t.ml5=e()}(window,function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var i=e[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:r})},n.r=function(t){Object.defineProperty(t,"__esModule",{value:!0})},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="/",n(n.s=689)}([function(t,e,n){"use strict";n.r(e),function(t,r,i,o){n.d(e,"AdadeltaOptimizer",function(){return ml}),n.d(e,"AdagradOptimizer",function(){return vl}),n.d(e,"AdamOptimizer",function(){return gl}),n.d(e,"AdamaxOptimizer",function(){return yl}),n.d(e,"DataStorage",function(){return Zn}),n.d(e,"ENV",function(){return f}),n.d(e,"Environment",function(){return h}),n.d(e,"KernelBackend",function(){return Qn}),n.d(e,"MomentumOptimizer",function(){return wl}),n.d(e,"Optimizer",function(){return pl}),n.d(e,"RMSPropOptimizer",function(){return xl}),n.d(e,"Rank",function(){return yt}),n.d(e,"Reduction",function(){return Mu}),n.d(e,"SGDOptimizer",function(){return bl}),n.d(e,"Tensor",function(){return vt}),n.d(e,"TensorBuffer",function(){return ht}),n.d(e,"Variable",function(){return gt}),n.d(e,"abs",function(){return Io}),n.d(e,"acos",function(){return To}),n.d(e,"acosh",function(){return Mo}),n.d(e,"add",function(){return Ms}),n.d(e,"addN",function(){return Rs}),n.d(e,"addStrict",function(){return Ds}),n.d(e,"all",function(){return cs}),n.d(e,"any",function(){return ls}),n.d(e,"argMax",function(){return fs}),n.d(e,"argMin",function(){return hs}),n.d(e,"asin",function(){return Ro}),n.d(e,"asinh",function(){return Do}),n.d(e,"atan",function(){return jo}),n.d(e,"atan2",function(){return js}),n.d(e,"atanh",function(){return Po}),n.d(e,"avgPool",function(){return es}),n.d(e,"backend",function(){return $e}),n.d(e,"basicLSTMCell",function(){return gu}),n.d(e,"batchNorm",function(){return xa}),n.d(e,"batchNorm2d",function(){return ka}),n.d(e,"batchNorm3d",function(){return Sa}),n.d(e,"batchNorm4d",function(){return _a}),n.d(e,"batchNormalization",function(){return wa}),n.d(e,"batchNormalization2d",function(){return ga}),n.d(e,"batchNormalization3d",function(){return ya}),n.d(e,"batchNormalization4d",function(){return ba}),n.d(e,"batchToSpaceND",function(){return Ir}),n.d(e,"browser",function(){return el}),n.d(e,"buffer",function(){return Ar}),n.d(e,"cast",function(){return Tr}),n.d(e,"ceil",function(){return zo}),n.d(e,"clipByValue",function(){return Lo}),n.d(e,"clone",function(){return Mr}),n.d(e,"complex",function(){return Dn}),n.d(e,"concat",function(){return fr}),n.d(e,"concat1d",function(){return hr}),n.d(e,"concat2d",function(){return dr}),n.d(e,"concat3d",function(){return pr}),n.d(e,"concat4d",function(){return mr}),n.d(e,"conv1d",function(){return za}),n.d(e,"conv2d",function(){return La}),n.d(e,"conv2dDerFilter",function(){return Ba}),n.d(e,"conv2dTranspose",function(){return Wa}),n.d(e,"conv3d",function(){return Fa}),n.d(e,"cos",function(){return Fo}),n.d(e,"cosh",function(){return Bo}),n.d(e,"cumsum",function(){return Rr}),n.d(e,"customGrad",function(){return On}),n.d(e,"deprecationWarn",function(){return De}),n.d(e,"depthToSpace",function(){return Dr}),n.d(e,"depthwiseConv2d",function(){return Ua}),n.d(e,"disableDeprecationWarnings",function(){return Re}),n.d(e,"dispose",function(){return Fe}),n.d(e,"disposeVariables",function(){return je}),n.d(e,"div",function(){return Ps}),n.d(e,"divStrict",function(){return zs}),n.d(e,"dot",function(){return Ha}),n.d(e,"dropout",function(){return Iu}),n.d(e,"elu",function(){return ou}),n.d(e,"enableDebugMode",function(){return Me}),n.d(e,"enableProdMode",function(){return Te}),n.d(e,"environment",function(){return m}),n.d(e,"equal",function(){return ws}),n.d(e,"equalStrict",function(){return xs}),n.d(e,"erf",function(){return Uo}),n.d(e,"exp",function(){return Vo}),n.d(e,"expandDims",function(){return jr}),n.d(e,"expm1",function(){return Wo}),n.d(e,"eye",function(){return Pr}),n.d(e,"fft",function(){return Su}),n.d(e,"fill",function(){return Kn}),n.d(e,"findBackend",function(){return Ge}),n.d(e,"findBackendFactory",function(){return Ke}),n.d(e,"floor",function(){return qo}),n.d(e,"floorDiv",function(){return Ls}),n.d(e,"fused",function(){return nc}),n.d(e,"gather",function(){return mu}),n.d(e,"gatherND",function(){return Ou}),n.d(e,"getBackend",function(){return qe}),n.d(e,"grad",function(){return _n}),n.d(e,"grads",function(){return En}),n.d(e,"greater",function(){return ks}),n.d(e,"greaterEqual",function(){return Ss}),n.d(e,"greaterEqualStrict",function(){return _s}),n.d(e,"greaterStrict",function(){return Es}),n.d(e,"hammingWindow",function(){return Du}),n.d(e,"hannWindow",function(){return Ru}),n.d(e,"ifft",function(){return _u}),n.d(e,"imag",function(){return Pn}),n.d(e,"image",function(){return tc}),n.d(e,"io",function(){return Jc}),n.d(e,"irfft",function(){return Cu}),n.d(e,"isFinite",function(){return na}),n.d(e,"isInf",function(){return ea}),n.d(e,"isNaN",function(){return ta}),n.d(e,"keep",function(){return Be}),n.d(e,"leakyRelu",function(){return au}),n.d(e,"less",function(){return Cs}),n.d(e,"lessEqual",function(){return Ns}),n.d(e,"lessEqualStrict",function(){return As}),n.d(e,"lessStrict",function(){return Os}),n.d(e,"linalg",function(){return Xu}),n.d(e,"linspace",function(){return Xn}),n.d(e,"localResponseNormalization",function(){return fu}),n.d(e,"log",function(){return Ho}),n.d(e,"log1p",function(){return Go}),n.d(e,"logSigmoid",function(){return Ko}),n.d(e,"logSoftmax",function(){return Rn}),n.d(e,"logSumExp",function(){return ds}),n.d(e,"logicalAnd",function(){return Qs}),n.d(e,"logicalNot",function(){return tu}),n.d(e,"logicalOr",function(){return eu}),n.d(e,"logicalXor",function(){return nu}),n.d(e,"losses",function(){return qu}),n.d(e,"matMul",function(){return qa}),n.d(e,"math",function(){return Qc}),n.d(e,"max",function(){return ps}),n.d(e,"maxPool",function(){return ts}),n.d(e,"maximum",function(){return Fs}),n.d(e,"maximumStrict",function(){return Bs}),n.d(e,"mean",function(){return ms}),n.d(e,"memory",function(){return Pe}),n.d(e,"min",function(){return vs}),n.d(e,"minimum",function(){return Us}),n.d(e,"minimumStrict",function(){return Vs}),n.d(e,"mod",function(){return Ws}),n.d(e,"modStrict",function(){return qs}),n.d(e,"moments",function(){return gs}),n.d(e,"movingAverage",function(){return bu}),n.d(e,"mul",function(){return Hs}),n.d(e,"mulStrict",function(){return Gs}),n.d(e,"multiRNNCell",function(){return yu}),n.d(e,"multinomial",function(){return zr}),n.d(e,"neg",function(){return Xo}),n.d(e,"nextFrame",function(){return El}),n.d(e,"norm",function(){return hu}),n.d(e,"notEqual",function(){return Is}),n.d(e,"notEqualStrict",function(){return Ts}),n.d(e,"oneHot",function(){return Lr}),n.d(e,"ones",function(){return Hn}),n.d(e,"onesLike",function(){return Yn}),n.d(e,"op",function(){return Tn}),n.d(e,"outerProduct",function(){return Ga}),n.d(e,"pad",function(){return Fr}),n.d(e,"pad1d",function(){return Br}),n.d(e,"pad2d",function(){return Ur}),n.d(e,"pad3d",function(){return Vr}),n.d(e,"pad4d",function(){return Wr}),n.d(e,"pool",function(){return ns}),n.d(e,"pow",function(){return Ks}),n.d(e,"powStrict",function(){return Xs}),n.d(e,"prelu",function(){return su}),n.d(e,"print",function(){return Or}),n.d(e,"prod",function(){return bs}),n.d(e,"profile",function(){return ze}),n.d(e,"rand",function(){return qr}),n.d(e,"randomNormal",function(){return Hr}),n.d(e,"randomUniform",function(){return Gr}),n.d(e,"range",function(){return $n}),n.d(e,"ready",function(){return We}),n.d(e,"real",function(){return jn}),n.d(e,"reciprocal",function(){return $o}),n.d(e,"registerBackend",function(){return Xe}),n.d(e,"relu",function(){return uu}),n.d(e,"removeBackend",function(){return He}),n.d(e,"reshape",function(){return Kr}),n.d(e,"reverse",function(){return Ka}),n.d(e,"reverse1d",function(){return Xa}),n.d(e,"reverse2d",function(){return $a}),n.d(e,"reverse3d",function(){return Ya}),n.d(e,"reverse4d",function(){return Ja}),n.d(e,"rfft",function(){return Eu}),n.d(e,"round",function(){return Yo}),n.d(e,"rsqrt",function(){return Jo}),n.d(e,"scalar",function(){return Ln}),n.d(e,"scatterND",function(){return ku}),n.d(e,"selu",function(){return cu}),n.d(e,"separableConv2d",function(){return Va}),n.d(e,"serialization",function(){return ol}),n.d(e,"setBackend",function(){return Ve}),n.d(e,"setdiff1dAsync",function(){return ti}),n.d(e,"sigmoid",function(){return Zo}),n.d(e,"sign",function(){return Qo}),n.d(e,"sin",function(){return ra}),n.d(e,"sinh",function(){return ia}),n.d(e,"slice",function(){return rs}),n.d(e,"slice1d",function(){return is}),n.d(e,"slice2d",function(){return os}),n.d(e,"slice3d",function(){return as}),n.d(e,"slice4d",function(){return ss}),n.d(e,"softmax",function(){return Mn}),n.d(e,"softplus",function(){return oa}),n.d(e,"spaceToBatchND",function(){return Xr}),n.d(e,"sparseToDense",function(){return Au}),n.d(e,"spectral",function(){return Nu}),n.d(e,"split",function(){return vr}),n.d(e,"sqrt",function(){return aa}),n.d(e,"square",function(){return sa}),n.d(e,"squaredDifference",function(){return $s}),n.d(e,"squaredDifferenceStrict",function(){return Ys}),n.d(e,"squeeze",function(){return $r}),n.d(e,"stack",function(){return Yr}),n.d(e,"step",function(){return ua}),n.d(e,"stridedSlice",function(){return wu}),n.d(e,"sub",function(){return Js}),n.d(e,"subStrict",function(){return Zs}),n.d(e,"sum",function(){return ys}),n.d(e,"tan",function(){return ca}),n.d(e,"tanh",function(){return la}),n.d(e,"tensor",function(){return zn}),n.d(e,"tensor1d",function(){return Fn}),n.d(e,"tensor2d",function(){return Bn}),n.d(e,"tensor3d",function(){return Un}),n.d(e,"tensor4d",function(){return Vn}),n.d(e,"tensor5d",function(){return Wn}),n.d(e,"tensor6d",function(){return qn}),n.d(e,"tensor_util",function(){return Tt}),n.d(e,"test_util",function(){return fl}),n.d(e,"tidy",function(){return Le}),n.d(e,"tile",function(){return Jr}),n.d(e,"time",function(){return Ue}),n.d(e,"topk",function(){return xu}),n.d(e,"train",function(){return Sl}),n.d(e,"transpose",function(){return lu}),n.d(e,"truncatedNormal",function(){return Zr}),n.d(e,"unsortedSegmentSum",function(){return vu}),n.d(e,"unstack",function(){return Qr}),n.d(e,"util",function(){return ot}),n.d(e,"valueAndGrad",function(){return Cn}),n.d(e,"valueAndGrads",function(){return Nn}),n.d(e,"variable",function(){return St}),n.d(e,"variableGrads",function(){return An}),n.d(e,"version_core",function(){return hl}),n.d(e,"webgl",function(){return dl}),n.d(e,"where",function(){return ru}),n.d(e,"whereAsync",function(){return iu}),n.d(e,"zeros",function(){return Gn}),n.d(e,"zerosLike",function(){return Jn}); /** * @license - * Copyright 2018 Google LLC. All Rights Reserved. + * Copyright 2019 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 @@ -15,10 +15,10 @@ * limitations under the License. * ============================================================================= */ -var o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])};function s(e,t){function n(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var u=Object.assign||function(e){for(var t,n=1,r=arguments.length;n0&&a[a.length-1])&&(6===i[0]||2===i[0])){o=0;continue}if(3===i[0]&&(!a||i[1]>a[0]&&i[1]0;)r=Math.random()*t|0,n=e[--t],e[t]=e[r],e[r]=n}function p(e,t,n){return Math.max(e,Math.min(t,n))}function h(e,t){var n=Math.random();return t*n+(1-n)*e}function d(e,t){if(!e)throw new Error("string"==typeof t?t:t())}function m(e,t,n){void 0===n&&(n=""),d(w(e,t),n+" Shapes "+e+" and "+t+" must match")}function g(e){d(null!=e,"The input to the tensor constructor must be a non-null value.")}function y(e,t){if(void 0===t&&(t=[]),Array.isArray(e))for(var n=0;n0,function(){return"Element arr["+r.join("][")+"] should be a primitive, but is an array of "+t.length+" elements"}),d(t.length===n[0],function(){return"Element arr["+r.join("][")+"] should have "+n[0]+" elements, but has "+t.length+" elements"});for(var a=n.slice(1),i=0;i1;--t)if(e%t==0)return[t,e/t];return[1,e]}function E(e,t){return t<=e.length?e:e+" ".repeat(t-e.length)}function N(e,t,n){return void 0===t&&(t=function(e){return 0}),new Promise(function(r,a){var i=0,o=function(){if(e())r();else{var s=t(++i);null!=n&&i>=n?a():setTimeout(o,s)}};o()})}function O(e,t){for(var n=1,r=-1,a=0;a=0)n*=e[a];else if(-1===e[a]){if(-1!==r)throw Error("Shapes can only have 1 implicit size. Found -1 at dim "+r+" and dim "+a);r=a}else if(e[a]<0)throw Error("Shapes can not be < 0. Found "+e[a]+" at dim "+a);if(-1===r){if(t>0&&t!==n)throw Error("Size("+t+") must match the product of shape "+e);return e}if(0===n)throw Error("Cannot infer the missing size in ["+e+"] when there are 0 elements");if(t%n!=0)throw Error("The implicit shape can't be a fractional number. Got "+t+" / "+n);var i=e.slice();return i[r]=t/n,i}function k(e,t){for(var n=[],r=[],a=0,i=0;ii)&&1===e[i]&&(n.push(e[i]),r.push(i)),t[a]<=i&&a++}1!==e[i]&&(n.push(e[i]),r.push(i))}return{newShape:n,keptDims:r}}function I(e,t){var n=null;if(null==e||"float32"===e)n=new Float32Array(t);else if("int32"===e)n=new Int32Array(t);else{if("bool"!==e)throw new Error("Unknown data type "+e);n=new Uint8Array(t)}return n}function C(e,t,n){if("float32"===t)for(var r=0;r=0;--r)n[r]=n[r+1]*e[r+1];return n}function L(e,t,n){return function(e,t){return e instanceof Float32Array&&"float32"===t||e instanceof Int32Array&&"int32"===t||e instanceof Uint8Array&&"bool"===t}(e,t)?e:(Array.isArray(e)&&(e=y(e)),function(e,t,n){if(null==t||"float32"===t||"complex64"===t)return new Float32Array(e);if("int32"===t)return n&&A(e,t),new Int32Array(e);if("bool"===t){for(var r=new Uint8Array(e.length),a=0;a1)for(var l=0;lq){var c=$*s,f=Array.from(t.subarray(0,c)),p=Array.from(t.subarray(u-$*s,u));return"complex64"===r&&(f=K(f),p=K(p)),["["+f.map(function(e,t){return H(e,i[t])}).join(", ")+", ..., "+p.map(function(e,t){return H(e,i[u-$+t])}).join(", ")+"]"]}return["["+("complex64"===r?K(t):Array.from(t)).map(function(e,t){return H(e,i[t])}).join(", ")+"]"]}var h=n.slice(1),d=a.slice(1),m=a[0]*s,g=[];if(u>q){for(var y=0;y<$;y++){var v=(b=y*m)+m;g.push.apply(g,e(t.subarray(b,v),h,r,d,i,!1))}for(g.push("..."),y=u-$;y {...}) to avoid memory leaks.");return this.keepTensors.add(e.id),e},e.prototype.startScope=function(e,t){void 0===t&&(t=!1),t&&0===this.gradientScopeCount&&(this.activeTape=[]),t&&this.gradientScopeCount++;var n={track:[],name:"unnamed scope"};e&&(n.name=e),this.scopeStack.push(n),this.activeScope=n},e.prototype.endScope=function(e,t){var n=this;void 0===t&&(t=!1),t&&(this.gradientScopeCount--,0===this.gradientScopeCount&&(this.activeTape=null));var r=new Set(this.keepTensors),a=re(e);a.forEach(function(e){return r.add(e.id)});for(var i=0;i0,"gradients() received an empty list of xs."),this.tidy("gradients",function(){var i=e();d(i instanceof Q,"The result y returned by f() must be a tensor.");var o=function(e,t,n){for(var r={},a={},i=0;i=0;i--)for(o=(d=e[i]).inputs,c=0;c0)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that the f you passed encloses all operations that lead from x to y.");var s={};return s[i.id]=null==n?function(e){var t=z(b(e),"float32");return Q.make(e,{values:t})}(i.shape):n,function(e,t){for(var n=function(n){var r=t[n],a=[];if(r.outputs.forEach(function(t){var n=e[t.id];if(null!=n)a.push(n);else{var r=Q.make(t.shape,{values:B(t.size,t.dtype)},t.dtype);a.push(r)}}),null==r.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+r.name+".");var i=r.gradient(1===r.outputs.length?a[0]:a);for(var o in r.inputs){if(!(o in i))throw new Error("Cannot backprop through input "+o+". Available gradients found: "+Object.keys(i)+".");var s=i[o](),u=r.inputs[o];if(!w(s.shape,u.shape))throw new Error("Error in gradient for op "+r.name+". The gradient of input '"+o+"' has shape '"+s.shape+"', which does not match the shape of the input '"+u.shape+"'");if(null==e[u.id])e[u.id]=s;else{var l=e[u.id];e[u.id]=l.add(s),l.dispose()}}},r=t.length-1;r>=0;r--)n(r)}(s,o),{value:i,grads:t.map(function(e){return s[e.id]})}},!0)},e.prototype.customGrad=function(e){var t=this;return d(R(e),"The f passed in customGrad(f) must be a function."),function(){for(var n,r,a=[],i=0;i {op();...}); to avoid memory leaks.");return this.activeScope.track.push(e),e},e}();!function(e){e[e.NUMBER=0]="NUMBER",e[e.BOOLEAN=1]="BOOLEAN",e[e.STRING=2]="STRING"}(ae||(ae={}));var oe=[{name:"DEBUG",type:ae.BOOLEAN},{name:"IS_BROWSER",type:ae.BOOLEAN},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION",type:ae.NUMBER},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE",type:ae.BOOLEAN},{name:"WEBGL_VERSION",type:ae.NUMBER},{name:"WEBGL_RENDER_FLOAT32_ENABLED",type:ae.BOOLEAN},{name:"WEBGL_DOWNLOAD_FLOAT_ENABLED",type:ae.BOOLEAN},{name:"WEBGL_FENCE_API_ENABLED",type:ae.BOOLEAN},{name:"BACKEND",type:ae.STRING},{name:"EPSILON",type:ae.NUMBER}];function se(e,t){var n;try{n=fe(e,t)}catch(e){return!1}return null!=n&&(pe(n),!0)}var ue="tfjsflags";function le(){var e={};if("undefined"==typeof window||void 0===window.location)return e;var t=function(e){var t={};return window.location.search.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,function(e){for(var n=[],r=1;r0?0:function(e,t){if(0===e)return 0;var n,r=fe(e,t);return n=ce(r,"EXT_disjoint_timer_query_webgl2")&&2===e?2:ce(r,"EXT_disjoint_timer_query")?1:0,null!=r&&pe(r),n}(n,this.get("IS_BROWSER"))}if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE"===t)return this.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0&&!function(){var e=navigator.userAgent||navigator.vendor||window.opera;return/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(e)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(e.substr(0,4))}();if("HAS_WEBGL"===t)return this.get("WEBGL_VERSION")>0;if("WEBGL_VERSION"===t)return se(2,this.get("IS_BROWSER"))?2:se(1,this.get("IS_BROWSER"))?1:0;if("WEBGL_RENDER_FLOAT32_ENABLED"===t)return function(e,t){if(0===e)return!1;var n=fe(e,t);if(1===e){if(!ce(n,"OES_texture_float"))return!1}else if(!ce(n,"EXT_color_buffer_float"))return!1;var r=he(n,e);return pe(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("WEBGL_DOWNLOAD_FLOAT_ENABLED"===t)return function(e,t){if(0===e)return!1;var n=fe(e,t);if(1===e){if(!ce(n,"OES_texture_float"))return!1;if(!ce(n,"WEBGL_color_buffer_float"))return!1}else if(!ce(n,"EXT_color_buffer_float"))return!1;var r=he(n,e);return pe(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("WEBGL_FENCE_API_ENABLED"===t)return function(e,t){if(2!==e)return!1;var n=fe(e,t),r=null!=n.fenceSync;return pe(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("TEST_EPSILON"===t)return 32===this.backend.floatPrecision()?.001:.1;if("EPSILON"===t)return 32===this.backend.floatPrecision()?1e-7:.001;throw new Error("Unknown feature "+t+".")},t.prototype.setFeatures=function(e){this.features=Object.assign({},e)},t.prototype.reset=function(){this.features=le(),null!=this.globalEngine&&(this.globalEngine=null)},t.prototype.initBackend=function(e,t){var n=this;if(void 0===t&&(t=!1),this.backendName=e,this.engines[e])this.globalEngine=this.engines[e];else{var r=this.findBackend(e);this.globalEngine=new ie(r,t,function(){return n.get("DEBUG")}),this.engines[e]=this.globalEngine}},Object.defineProperty(t.prototype,"backend",{get:function(){return this.engine.backend},enumerable:!0,configurable:!0}),t.prototype.findBackend=function(e){return e in this.registry?this.registry[e].backend:null},t.prototype.registerBackend=function(e,t,n,r){var a=this;if(void 0===n&&(n=1),e in this.registry)return console.warn(e+" backend was already registered. Reusing existing backend"),null!=r&&r(function(){return a.engine}),!1;try{var i=t();return this.registry[e]={backend:i,priority:n},!0}catch(t){return console.warn("Registration of backend "+e+" failed"),console.warn(t.stack||t.message),!1}},t.prototype.removeBackend=function(e){if(!(e in this.registry))throw new Error(e+" backend not found in registry");this.registry[e].backend.dispose(),delete this.registry[e],e in this.engines&&delete this.engines[e]},Object.defineProperty(t.prototype,"engine",{get:function(){return this.initDefaultBackend(),this.globalEngine},enumerable:!0,configurable:!0}),t.prototype.initDefaultBackend=function(){null==this.globalEngine&&this.initBackend(this.get("BACKEND"),!1)},t}(),me=function(){var t=function(){var t;if("undefined"!=typeof window)t=window;else{if(void 0===e)throw new Error("Could not find a global object");t=e}return t}();return null==t.ENV&&(t.ENV=new de(le()),Z(function(){return t.ENV.engine})),t.ENV}(),ge=Object.freeze({Environment:de,ENV:me});function ye(e){return d(R(e),"The f passed in grad(f) must be a function"),function(t,n){return d(t instanceof Q,"The x passed in grad(f)(x) must be a tensor"),d(null==n||n instanceof Q,"The dy passed in grad(f)(x, dy) must be a tensor"),me.engine.tidy(function(){var r=me.engine.gradients(function(){return e(t)},[t],n),a=r.value,i=r.grads;return null!=n&&m(a.shape,n.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),Se(i),i[0]})}}function ve(e){return d(R(e),"The f passed in grads(f) must be a function"),function(t,n){return d(Array.isArray(t)&&t.every(function(e){return e instanceof Q}),"The args passed in grads(f)(args) must be an array of tensors"),d(null==n||n instanceof Q,"The dy passed in grads(f)(args, dy) must be a tensor"),me.engine.tidy(function(){var r=me.engine.gradients(function(){return e.apply(void 0,t)},t,n),a=r.value,i=r.grads;return null!=n&&m(a.shape,n.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Se(i),i})}}function be(e){return d(R(e),"The f passed in valueAndGrad(f) must be a function"),function(t,n){d(t instanceof Q,"The x passed in valueAndGrad(f)(x) must be a tensor"),d(null==n||n instanceof Q,"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");var r=me.engine.gradients(function(){return e(t)},[t],n),a=r.grads,i=r.value;return Se(a),{grad:a[0],value:i}}}function we(e){return d(R(e),"The f passed in valueAndGrads(f) must be a function"),function(t,n){d(Array.isArray(t)&&t.every(function(e){return e instanceof Q}),"The args passed in valueAndGrads(f)(args) must be array of tensors"),d(null==n||n instanceof Q,"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");var r=me.engine.gradients(function(){return e.apply(void 0,t)},t,n);return null!=n&&m(r.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Se(r.grads),r}}function xe(e,t){if(d(R(e),"The f passed in variableGrads(f) must be a function"),d(null==t||Array.isArray(t)&&t.every(function(e){return e instanceof ee}),"The varList passed in variableGrads(f, varList) must be an array of variables"),null==t)for(var n in t=[],me.engine.registeredVariables)t.push(me.engine.registeredVariables[n]);var r=t.length;d((t=t.filter(function(e){return e.trainable})).length>0,"variableGrads() expects at least one of the input variables to be trainable, but none of the "+r+" variables is trainable.");var a=me.engine.gradients(e,t,null,!0),i=a.value,o=a.grads;d(o.some(function(e){return null!=e}),"Cannot find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize()."),d(0===i.rank,"The f passed in variableGrads(f) must return a scalar, but it returned a rank-"+i.rank+" tensor");var s={};return t.forEach(function(e,t){null!=o[t]&&(s[e.name]=o[t])}),{value:i,grads:s}}function _e(e){return me.engine.customGrad(e)}function Se(e){if(e.filter(function(e){return null==e}).length>0)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that\n the f you passed encloses all operations that lead from x to y.")}var Ee=de.tidy,Ne=de.keep,Oe=de.dispose,ke=de.time,Ie=de.profile;function Ce(){for(var e=[],t=0;t=2*t+1||a%2==1?o.push(a):i.push(a);r.push.apply(r,i),r.push(0),r.push.apply(r,o)}return r}function Te(e,t,n,r){void 0===r&&(r=!0);var a=[];r?a.push(e[0]/n):a.push(e[0]*n);for(var i=1;i=-n&&e0?a>=c[t]:a<=c[t]);a+=r[t])n+=1;return n}),[l,h,f]}function He(e,t,n,r,a){var i=t[a];e&1<0?Number.MIN_SAFE_INTEGER:Number.MAX_SAFE_INTEGER);var o=r[a];return i<0&&(i+=o),p(0,i,o-1)}function Ke(e,t,n,r,a){var i=t[a];e&1<0?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER);var o=r[a];return i<0&&(i+=o),n[a]>0?p(0,i,o):p(-1,i,o-1)}function Xe(e,t,n,r){if(void 0===r&&(r="float32"),r=r||"float32",e instanceof Q)return e;if(!T(e)&&!Array.isArray(e)&&"number"!=typeof e&&"boolean"!=typeof e)throw new Error("Argument '"+t+"' passed to '"+n+"' must be a Tensor or TensorLike, but got "+e.constructor.name);var a=v(e);return T(e)||Array.isArray(e)||(e=[e]),Q.make(a,{values:L(e,r,me.get("DEBUG"))},r)}function Ye(e,t,n){if(!Array.isArray(e))throw new Error("Argument "+t+" passed to "+n+" must be a `Tensor[]` or `TensorLike[]`");return e.map(function(e,r){return Xe(e,t+"["+r+"]",n)})}function Je(e){var t=Object.keys(e);if(1!==t.length)throw new Error("Please provide an object with a single key (operation name) mapping to a function. Got an object with "+t.length+" keys.");var n=t[0],r=e[n];n.endsWith("_")&&(n=n.substring(0,n.length-1));var a=function(){for(var e=[],t=0;t1)return ft([0],r);var a=B(Math.abs(Math.ceil((t-e)/n)),r);ta}).sort(function(e,t){return t.score-e.score}),o=[],s=0;s=0;--p)if(At(e,c,o[p])>=r){f=!0;break}if(!f&&(o.push(c),o.length>=n))break}return at(o,"int32")}function At(e,t,n){var r=e.subarray(4*t,4*t+4),a=e.subarray(4*n,4*n+4),i=Math.min(r[0],r[2]),o=Math.min(r[1],r[3]),s=Math.max(r[0],r[2]),u=Math.max(r[1],r[3]),l=Math.min(a[0],a[2]),c=Math.min(a[1],a[3]),f=Math.max(a[0],a[2]),p=Math.max(a[1],a[3]),h=(s-i)*(u-o),d=(f-l)*(p-c);if(h<=0||d<=0)return 0;var m=Math.max(i,l),g=Math.max(o,c),y=Math.min(s,f),v=Math.min(u,p),b=Math.max(y-m,0)*Math.max(v-g,0);return b/(h+d-b)}function Pt(e,t,n){var r=Array(e.rank).fill(0),a=e.shape.slice();return t.map(function(t){a[n]=t;var i=e.slice(r,a);return r[n]+=t,i})}function Tt(e,t,n,r,a){for(var i=t[t.length-1],o=[e.length/i,i],s=o[0],u=o[1],l=I(n,s*r),c=I("int32",s*r),f=0;f1&&1===o&&r.unshift(i)}return r}function Rt(e,t){for(var n=[],r=0;r1)&&n.unshift(i)}return n}function Dt(e,t){for(var n=[],r=Math.max(e.length,t.length),a=0;a1?"["+t+"]":"")+";":"uniform sampler2D "+e.name+";"});a=a.join("\n");var i,o=e.map(function(e){return function(e,t,n){var r=function(e){var t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1)+"Flat",r=b(e.shapeInfo.logicalShape);if(e.shapeInfo.isUniform)return 1===r?"float "+n+"(int index) {return "+t+";}":"\n float "+n+"(int index) {\n for (int i = 0; i < "+r+"; i++) {\n if (i == index) {\n return "+t+"[i];\n }\n }\n }\n ";var a=e.shapeInfo.texShape,i=a[0],o=a[1];return 1===o&&1===i?"\n float "+n+"(int index) {\n return sampleTexture("+t+", halfCR);\n }\n ":1===o?"\n float "+n+"(int index) {\n vec2 uv = vec2(0.5, (float(index) + 0.5) / "+i+".0);\n return sampleTexture("+t+", uv);\n }\n ":1===i?"\n float "+n+"(int index) {\n vec2 uv = vec2((float(index) + 0.5) / "+o+".0, 0.5);\n return sampleTexture("+t+", uv);\n }\n ":"\n float "+n+"(int index) {\n vec2 uv = UVfrom1D("+i+", "+o+", index);\n return sampleTexture("+t+", uv);\n }\n "}(e);return r+=function e(t){var n=t.shapeInfo.logicalShape;switch(n.length){case 0:return function(e){var t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1);return e.shapeInfo.isUniform?"float "+n+"() {return "+t+";}":"\n float "+n+"() {\n return sampleTexture("+t+", halfCR);\n }\n "}(t);case 1:return function(e){var t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1);return"\n float "+n+"(int index) {\n return "+n+"Flat(index);\n }\n "}(t);case 2:return function(t){var n=t.shapeInfo.logicalShape,r=t.name,a="get"+r.charAt(0).toUpperCase()+r.slice(1),i=t.shapeInfo.texShape;if(null!=i&&w(n,i)){var o=i[0];return"\n float "+a+"(int row, int col) {\n vec2 uv = (vec2(col, row) + halfCR) / vec2("+i[1]+".0, "+o+".0);\n return sampleTexture("+r+", uv);\n }\n "}var s=k(n),u=s.newShape,l=s.keptDims,c=u;if(c.lengths||o.length>0),c=function(e){for(var t=0;t=1?"coords = 0;":s.map(function(e){return"coords["+(e+u)+"] = 0;"}).join("\n"))+"\n return get"+n+"("+(i<2&&a>0?"coords":e.shapeInfo.logicalShape.map(function(e,t){return"coords["+(t+u)+"]"}).join(", "))+");\n }\n "}(e,t,a,i);var p=b(e.shapeInfo.logicalShape),h="";l&&c&&(h="\n int mainPart = index / "+p+";\n index -= mainPart * "+p+";\n ");var d=t.texShape;if(f)return 1===p?"float "+i+"() {return "+r+";}":"\n float "+i+"() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+d[0]+", "+d[1]+"));\n int index = resTexRC.x * "+d[1]+" + resTexRC.y;\n "+h+"\n return get"+a+"Flat(index);\n }\n ";var m=e.shapeInfo.texShape;return w(m,d)?"\n float "+i+"() {\n return sampleTexture("+r+", resultUV);\n }\n ":"\n float "+i+"() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+d[0]+", "+d[1]+"));\n int index = resTexRC.x * "+d[1]+" + resTexRC.y;\n "+h+"\n int texR = index / "+m[1]+";\n int texC = index - texR * "+m[1]+";\n vec2 uv = (vec2(texC, texR) + halfCR) /\n vec2("+m[1]+".0, "+m[0]+".0);\n\n return sampleTexture("+r+", uv);\n }\n "}(e,t,n)),r}(e,t,r)}).join("\n"),s=t.texShape;return i=t.isPacked?function(e,t){switch(e.length){case 0:return"\n int getOutputCoords() {\n return 0;\n }\n ";case 2:return function(e,t){return"\n ivec2 getOutputCoords() {\n return 2 * ivec2(resultUV.yx * vec2("+Math.ceil(t[0]/2)+", "+Math.ceil(t[1]/2)+"));\n }\n "}(0,t);default:throw new Error(e.length+"-D output packed sampling is not yet supported")}}(t.logicalShape,s):function(e,t){switch(e.length){case 0:return"\n int getOutputCoords() {\n return 0;\n }\n ";case 1:return function(e,t){return 1===t[0]?"\n int getOutputCoords() {\n return int(resultUV.x * "+t[1]+".0);\n }\n ":1===t[1]?"\n int getOutputCoords() {\n return int(resultUV.y * "+t[0]+".0);\n }\n ":"\n int getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n return resTexRC.x * "+t[1]+" + resTexRC.y;\n }\n "}(0,t);case 2:return function(e,t){return w(e,t)?"\n ivec2 getOutputCoords() {\n return ivec2(resultUV.yx * vec2("+t[0]+", "+t[1]+"));\n }\n ":1===e[1]?"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n return ivec2(index, 0);\n }\n ":1===e[0]?"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n return ivec2(0, index);\n }\n ":"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n int r = index / "+e[1]+";\n int c = index - r * "+e[1]+";\n return ivec2(r, c);\n }\n "}(e,t);case 3:return function(e,t){var n=e[1]*e[2],r=e[2];return"\n ivec3 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n int r = index / "+n+";\n index -= r * "+n+";\n int c = index / "+r+";\n int d = index - c * "+r+";\n return ivec3(r, c, d);\n }\n "}(e,t);case 4:return function(e,t){var n=e[3],r=e[2]*n,a=e[1]*r;return"\n ivec4 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n\n int r = index / "+a+";\n index -= r * "+a+";\n\n int c = index / "+r+";\n index -= c * "+r+";\n\n int d = index / "+n+";\n int d2 = index - d * "+n+";\n\n return ivec4(r, c, d, d2);\n }\n "}(e,t);case 5:return function(e,t){var n=e[4],r=e[3]*n,a=e[2]*r,i=e[1]*a;return"\n ivec5 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx * vec2("+t[0]+",\n "+t[1]+"));\n\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n\n int r = index / "+i+";\n index -= r * "+i+";\n\n int c = index / "+a+";\n index -= c * "+a+";\n\n int d = index / "+r+";\n index -= d * "+r+";\n\n int d2 = index / "+n+";\n int d3 = index - d2 * "+n+";\n\n ivec5 outShape = ivec5(r, c, d, d2, d3);\n return outShape;\n }\n "}(e,t);case 6:return function(e,t){var n=e[5],r=e[4]*n,a=e[3]*r,i=e[2]*a,o=e[1]*i;return"\n ivec6 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n\n int r = index / "+o+";\n index -= r * "+o+";\n\n int c = index / "+i+";\n index -= c * "+i+";\n\n int d = index / "+a+";\n index -= d * "+a+";\n\n int d2 = index / "+r+";\n index -= d2 * "+r+";\n\n int d3 = index / "+n+";\n int d4 = index - d3 * "+n+";\n\n ivec6 result = ivec6(r, c, d, d2, d3, d4);\n return result;\n }\n "}(e,t);default:throw new Error(e.length+"-D output sampling is not yet supported")}}(t.logicalShape,s),[Wt,Vt,Ut,a,i,o,n].join("\n")}var Vt="\n float sampleTexture(sampler2D textureSampler, vec2 uv) {\n return texture2D(textureSampler, uv).r;\n }\n",Ut="\n void setOutput(float val) {\n gl_FragColor = vec4(val, 0, 0, 0);\n }\n",Wt="\n precision highp float;\n precision highp int;\n varying vec2 resultUV;\n const vec2 halfCR = vec2(0.5, 0.5);\n\n struct ivec5\n {\n int x;\n int y;\n int z;\n int w;\n int u;\n };\n\n struct ivec6\n {\n int x;\n int y;\n int z;\n int w;\n int u;\n int v;\n };\n\n bool isNaN(float val) {\n return (val < 0.0 || 0.0 < val || val == 0.0) ? false : true;\n }\n\n bool hasNaN(vec4 values) {\n vec4 v1 = values * values;\n vec4 v2 = values * values;\n return any(notEqual(v1, v2));\n }\n\n float getNaN(vec4 values) {\n return dot(vec4(1), values);\n }\n\n int round(float value) {\n return int(floor(value + 0.5));\n }\n\n int imod(int x, int y) {\n return x - y * (x / y);\n }\n\n //Based on the work of Dave Hoskins\n //https://www.shadertoy.com/view/4djSRW\n #define HASHSCALE1 443.8975\n float random(float seed){\n vec2 p = resultUV * seed;\n vec3 p3 = fract(vec3(p.xyx) * HASHSCALE1);\n p3 += dot(p3, p3.yzx + 19.19);\n return fract((p3.x + p3.y) * p3.z);\n }\n\n \nvec2 UVfrom1D(int texNumR, int texNumC, int index) {\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n \nvec2 UVfrom2D(int texNumR, int texNumC, int numC, int row, int col) {\n int index = row * numC + col;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n \nvec2 UVfrom3D(int texNumR, int texNumC, int stride0,\n int stride1, int row, int col, int depth) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 + depth;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n \nvec2 UVfrom4D(int texNumR, int texNumC, int stride0,\n int stride1, int stride2, int row, int col, int depth,\n int depth2) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 + depth * stride2 + depth2;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n \nvec2 UVfrom5D(int texNumR, int texNumC, int stride0,\n int stride1, int stride2, int stride3, int row, int col, int depth,\n int depth2, int depth3) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 +\n depth * stride2 + depth2 * stride3 + depth3;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n \nvec2 UVfrom6D(int texNumR, int texNumC, int stride0,\n int stride1, int stride2, int stride3, int stride4,\n int row, int col, int depth, int depth2, int depth3, int depth4) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 + depth * stride2 + depth2 *\n stride3 + depth3 * stride4 + depth4;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n";function qt(e){if(e<=1)return"int";if(2===e)return"ivec2";if(3===e)return"ivec3";if(4===e)return"ivec4";if(5===e)return"ivec5";if(6===e)return"ivec6";throw Error("GPU for rank "+e+" is not yet supported")}function $t(e,t){var n=JSON.parse(JSON.stringify(e));return n.shapeInfo.logicalShape=t,n}function Gt(e,t){return t.map(function(t){return e[t]}).join(", ")}function Ht(e,t){if(1===e)return""+t;if(2===e)return t+".y";if(3===e)return t+".z";if(4===e)return t+".w";throw Error("Cumulative sum for rank "+e+" is not yet supported")}var Kt,Xt,Yt=function(){function e(e,t,n){this.variableNames=["x"],this.outputShape=[],this.outputShape=e,this.blockSize=t,this.dataFormat=n,this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int h = "+this.getHeightCoordString()+";\n int w = "+this.getWidthCoordString()+";\n int d = "+this.getDepthCoordString()+";\n\n int in_h = h / "+t+";\n int offset_h = imod(h, "+t+");\n int in_w = w / "+t+";\n int offset_w = imod(w, "+t+");\n int offset_d = (offset_h * "+t+" + offset_w) *\n "+this.getOutputDepthSize()+";\n int in_d = d + offset_d;\n\n float result = "+this.getInputSamplingString()+";\n setOutput(result);\n }\n "}return e.prototype.getHeightCoordString=function(){return"NHWC"===this.dataFormat?"coords[1]":"coords[2]"},e.prototype.getWidthCoordString=function(){return"NHWC"===this.dataFormat?"coords[2]":"coords[3]"},e.prototype.getDepthCoordString=function(){return"NHWC"===this.dataFormat?"coords[3]":"coords[1]"},e.prototype.getOutputDepthSize=function(){return"NHWC"===this.dataFormat?this.outputShape[3]:this.outputShape[1]},e.prototype.getInputSamplingString=function(){return"NHWC"===this.dataFormat?"getX(b, in_h, in_w, in_d)":"getX(b, in_d, in_h, in_w)"},e}();function Jt(e,t){return[t,e]}function Zt(e,t){return e*t}function Qt(e,t,n){var r=function(e,t){if(e%t!=0)throw new Error("unpackedSize ("+e+") must be a multiple of "+t);return e/t}(e.length,n);if(t.length= "+r);for(var a=0,i=0;ir||n>r)throw a="["+t+"x"+n+"]",new Error("Requested texture size "+a+" greater than WebGL maximum on this browser / GPU ["+r+"x"+r+"].")}function En(e){return Rn(e,function(){return e.createFramebuffer()},"Unable to create WebGLFramebuffer.")}function Nn(e,t,n,r,a,i,o){var s=e.getAttribLocation(t,n);return-1!==s&&(on(e,function(){return e.bindBuffer(e.ARRAY_BUFFER,r)}),on(e,function(){return e.vertexAttribPointer(s,a,e.FLOAT,!1,i,o)}),on(e,function(){return e.enableVertexAttribArray(s)}),!0)}function On(e,t,n){Dn(e,n),on(e,function(){return e.activeTexture(e.TEXTURE0+n)}),on(e,function(){return e.bindTexture(e.TEXTURE_2D,t)})}function kn(e,t,n){return Rn(e,function(){return e.getUniformLocation(t,n)},'uniform "'+n+'" not present in program.')}function In(e,t,n){return e.getUniformLocation(t,n)}function Cn(e,t,n,r,a){on(e,function(){return On(e,n,a)}),on(e,function(){return e.uniform1i(r,a)})}function An(e,t,n){on(e,function(){return e.bindFramebuffer(e.FRAMEBUFFER,n)}),on(e,function(){return e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,t,0)})}function Pn(e,t){on(e,function(){return e.bindFramebuffer(e.FRAMEBUFFER,t)}),on(e,function(){return e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,null,0)})}function Tn(e){var t=e.checkFramebufferStatus(e.FRAMEBUFFER);if(t!==e.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+Mn(e,t))}function Mn(e,t){switch(t){case e.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_ATTACHMENT";case e.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";case e.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:return"FRAMEBUFFER_INCOMPLETE_DIMENSIONS";case e.FRAMEBUFFER_UNSUPPORTED:return"FRAMEBUFFER_UNSUPPORTED";default:return"unknown error "+t}}function Rn(e,t,n){var r=on(e,function(){return t()});if(null==r)throw new Error(n);return r}function Dn(e,t){var n=e.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,r=t+e.TEXTURE0;if(rn)throw new Error("textureUnit must be in [gl.TEXTURE0, gl.TEXTURE"+n+"].")}function jn(e,t){2!==t.length&&(t=k(t).newShape);var n=wn(e),r=b(t);return t.length<=1&&r<=n?[r,1]:2===t.length&&t[0]<=n&&t[1]<=n?t:3===t.length&&t[0]<=n&&t[1]*t[2]<=n?[t[0],t[1]*t[2]]:4===t.length&&t[0]<=n&&t[1]*t[2]*t[3]<=n?[t[0],t[1]*t[2]*t[3]]:S(r)}var Ln=Object.freeze({createWebGLRenderingContext:rn,createWebGLRenderingContextFromCanvas:an,callAndCheck:on,enableDebugWebGLErrorChecking:un,checkWebGLError:ln,getWebGLErrorMessage:cn,getExtensionOrThrow:fn,createVertexShader:pn,createFragmentShader:hn,createProgram:mn,linkProgram:gn,validateProgram:yn,createStaticVertexBuffer:vn,createStaticIndexBuffer:bn,queryMaxTextureSize:wn,getNumChannels:xn,createTexture:_n,validateTextureSize:Sn,createFramebuffer:En,bindVertexBufferToProgramAttribute:Nn,bindTextureUnit:On,unbindTextureUnit:function(e,t){Dn(e,t),on(e,function(){return e.activeTexture(e.TEXTURE0+t)}),on(e,function(){return e.bindTexture(e.TEXTURE_2D,null)})},getProgramUniformLocationOrThrow:kn,getProgramUniformLocation:In,bindTextureToProgramUniformSampler:Cn,bindCanvasToFramebuffer:function(e){on(e,function(){return e.bindFramebuffer(e.FRAMEBUFFER,null)}),on(e,function(){return e.viewport(0,0,e.canvas.width,e.canvas.height)}),on(e,function(){return e.scissor(0,0,e.canvas.width,e.canvas.height)})},bindColorTextureToFramebuffer:An,unbindColorTextureFromFramebuffer:Pn,validateFramebuffer:Tn,getFramebufferErrorMessage:Mn,getTextureShapeFromLogicalShape:jn});function zn(e){var t,n={alpha:!1,antialias:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!1,depth:!1,stencil:!1,failIfMajorPerformanceCaveat:!0};return on(t=null!=e?an(e,n):rn(n),function(){return t.disable(t.DEPTH_TEST)}),on(t,function(){return t.disable(t.STENCIL_TEST)}),on(t,function(){return t.disable(t.BLEND)}),on(t,function(){return t.disable(t.DITHER)}),on(t,function(){return t.disable(t.POLYGON_OFFSET_FILL)}),on(t,function(){return t.disable(t.SAMPLE_COVERAGE)}),on(t,function(){return t.enable(t.SCISSOR_TEST)}),on(t,function(){return t.enable(t.CULL_FACE)}),on(t,function(){return t.cullFace(t.BACK)}),t}function Bn(e){return pn(e,"\n precision highp float;\n attribute vec3 clipSpacePos;\n attribute vec2 uv;\n varying vec2 resultUV;\n\n void main() {\n gl_Position = vec4(clipSpacePos, 1);\n resultUV = uv;\n }")}function Fn(e){return vn(e,new Float32Array([-1,1,0,0,1,-1,-1,0,0,0,1,1,0,1,1,1,-1,0,1,0]))}function Vn(e){return bn(e,new Uint16Array([0,1,2,2,1,3]))}function Un(e,t){var n,r,a,i,o,s,u,l=e;return 2===me.get("WEBGL_VERSION")?(n=l.R32F,r=l.R16F,a=l.RGBA32F,i=l.RED,o=4,s=1,u=l.HALF_FLOAT):(n=e.RGBA,r=e.RGBA,a=l.RGBA,i=e.RGBA,o=4,s=4,u=null!=t?t.HALF_FLOAT_OES:null),{internalFormatFloat:n,internalFormatHalfFloat:r,internalFormatPackedFloat:a,textureFormatFloat:i,downloadTextureFormat:e.RGBA,downloadUnpackNumChannels:o,defaultNumChannels:s,textureTypeHalfFloat:u}}function Wn(e,t,n,r,a,i){Sn(e,t,n);var o=_n(e),s=e.TEXTURE_2D;return on(e,function(){return e.bindTexture(s,o)}),on(e,function(){return e.texParameteri(s,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE)}),on(e,function(){return e.texParameteri(s,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)}),on(e,function(){return e.texParameteri(s,e.TEXTURE_MIN_FILTER,e.NEAREST)}),on(e,function(){return e.texParameteri(s,e.TEXTURE_MAG_FILTER,e.NEAREST)}),on(e,function(){return e.texImage2D(s,0,r,t,n,0,a,i,null)}),on(e,function(){return e.bindTexture(e.TEXTURE_2D,null)}),o}function qn(e,t,n,r){var a=Jt(t,n);return Wn(e,a[0],a[1],r.internalFormatFloat,r.textureFormatFloat,e.FLOAT)}function $n(e,t,n,r){var a=Jt(t,n);return Wn(e,a[0],a[1],r.internalFormatFloat,r.textureFormatFloat,r.textureTypeHalfFloat)}function Gn(e,t,n,r){var a=Jt(t,n);return Wn(e,a[0],a[1],e.RGBA,e.RGBA,e.UNSIGNED_BYTE)}function Hn(e,t,n,r){var a=en(t,n);return Wn(e,a[0],a[1],r.internalFormatPackedFloat,e.RGBA,e.FLOAT)}function Kn(e,t,n){return on(e,function(){return e.bindBuffer(e.ARRAY_BUFFER,n)}),Nn(e,t,"clipSpacePos",n,3,20,0)&&Nn(e,t,"uv",n,2,20,12)}function Xn(e,t,n){on(e,function(){return e.bindTexture(e.TEXTURE_2D,t)}),on(e,function(){return e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n)}),on(e,function(){return e.bindTexture(e.TEXTURE_2D,null)})}function Yn(e,t,n,r,a,i){Sn(e,n,r),on(e,function(){return e.bindTexture(e.TEXTURE_2D,t)}),on(e,function(){return e.texSubImage2D(e.TEXTURE_2D,0,0,0,n,r,i,e.FLOAT,a)}),on(e,function(){return e.bindTexture(e.TEXTURE_2D,null)})}function Jn(e,t,n,r,a,i,o){var s,u=Jt(n,r),l=u[0],c=u[1];1===o.defaultNumChannels?s=a:function(e,t,n){var r=Zt(e.length,n);if(t.length= "+r);for(var a=0,i=0;i= "+a);for(var i=en(t,n),o=i[0],s=i[1],u=n%2==1,l=t%2==1,c=Math.floor(n/2),f=Math.floor(t/2),p=u?4:0,h=n,d=0,m=0;m= "+a);for(var i=n%2==1,o=t%2==1,s=Math.floor(n/2),u=Math.floor(t/2),l=en(t,n),c=l[0],f=l[1],p=i?4:0,h=n+(i?1:0),d=0,m=0,g=n,y=0;y0?(t=this.beginQuery(),this.endQuery(),n=function(){return r.isQueryAvailable(t,me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))}):n=function(){return!0};return{query:t,isFencePassed:n}},e.prototype.downloadMatrixFromPackedTexture=function(e,t,n){var r=this;return this.downloadMatrixDriver(e,function(){return rr(r.gl,t,n,r.textureConfig)})},e.prototype.createProgram=function(e){this.throwIfDisposed();var t=this.gl,n=hn(t,e),r=Bn(t),a=mn(t);return on(t,function(){return t.attachShader(a,r)}),on(t,function(){return t.attachShader(a,n)}),gn(t,a),this.autoDebugValidate&&yn(t,a),this.vertexAttrsAreBound||(this.setProgram(a),this.vertexAttrsAreBound=Kn(t,this.program,this.vertexBuffer)),a},e.prototype.deleteProgram=function(e){var t=this;this.throwIfDisposed(),e===this.program&&(this.program=null),null!=e&&on(this.gl,function(){return t.gl.deleteProgram(e)})},e.prototype.setProgram=function(e){var t=this;this.throwIfDisposed(),this.program=e,null!=this.program&&this.autoDebugValidate&&yn(this.gl,this.program),on(this.gl,function(){return t.gl.useProgram(e)})},e.prototype.getUniformLocation=function(e,t,n){return void 0===n&&(n=!0),this.throwIfDisposed(),n?kn(this.gl,e,t):In(this.gl,e,t)},e.prototype.getAttributeLocation=function(e,t){var n=this;return this.throwIfDisposed(),on(this.gl,function(){return n.gl.getAttribLocation(e,t)})},e.prototype.getUniformLocationNoThrow=function(e,t){return this.throwIfDisposed(),this.gl.getUniformLocation(e,t)},e.prototype.setInputMatrixTexture=function(e,t,n){this.throwIfDisposed(),this.throwIfNoProgram(),Cn(this.gl,this.program,e,t,n)},e.prototype.setOutputMatrixTexture=function(e,t,n){this.setOutputMatrixTextureDriver(e,n,t)},e.prototype.setOutputPackedMatrixTexture=function(e,t,n){this.throwIfDisposed();var r=en(t,n),a=r[0],i=r[1];this.setOutputMatrixTextureDriver(e,a,i)},e.prototype.setOutputMatrixWriteRegion=function(e,t,n,r){this.setOutputMatrixWriteRegionDriver(n,e,r,t)},e.prototype.setOutputPackedMatrixWriteRegion=function(e,t,n,r){throw new Error("setOutputPackedMatrixWriteRegion not implemented.")},e.prototype.debugValidate=function(){null!=this.program&&yn(this.gl,this.program),Tn(this.gl)},e.prototype.executeProgram=function(){this.throwIfDisposed(),this.throwIfNoProgram();var e=this.gl;this.autoDebugValidate&&this.debugValidate(),on(e,function(){return e.drawElements(e.TRIANGLES,6,e.UNSIGNED_SHORT,0)})},e.prototype.blockUntilAllProgramsCompleted=function(){var e=this;this.throwIfDisposed(),on(this.gl,function(){return e.gl.finish()})},e.prototype.getQueryTimerExtension=function(){return null==this.disjointQueryTimerExtension&&(this.disjointQueryTimerExtension=fn(this.gl,2===me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")?"EXT_disjoint_timer_query_webgl2":"EXT_disjoint_timer_query")),this.disjointQueryTimerExtension},e.prototype.getQueryTimerExtensionWebGL2=function(){return this.getQueryTimerExtension()},e.prototype.getQueryTimerExtensionWebGL1=function(){return this.getQueryTimerExtension()},e.prototype.beginQuery=function(){if(2===me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")){var e=this.gl,t=this.getQueryTimerExtensionWebGL2(),n=e.createQuery();return e.beginQuery(t.TIME_ELAPSED_EXT,n),n}var r=this.getQueryTimerExtensionWebGL1(),a=r.createQueryEXT();return r.beginQueryEXT(r.TIME_ELAPSED_EXT,a),a},e.prototype.endQuery=function(){if(2!==me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")){var e=this.getQueryTimerExtensionWebGL1();e.endQueryEXT(e.TIME_ELAPSED_EXT)}else{var t=this.gl,n=this.getQueryTimerExtensionWebGL2();t.endQuery(n.TIME_ELAPSED_EXT)}},e.prototype.waitForQueryAndGetTime=function(e){return l(this,void 0,void 0,function(){var t=this;return c(this,function(n){switch(n.label){case 0:return[4,N(function(){return t.isQueryAvailable(e,me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))})];case 1:return n.sent(),[2,this.getQueryTime(e,me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))]}})})},e.prototype.getQueryTime=function(e,t){if(0===t)return null;if(2===t){var n=this.gl;return n.getQueryParameter(e,n.QUERY_RESULT)/1e6}var r=this.getQueryTimerExtensionWebGL1();return r.getQueryObjectEXT(e,r.QUERY_RESULT_EXT)/1e6},e.prototype.isQueryAvailable=function(e,t){if(0===t)return!0;if(2===t){var n=this.gl,r=this.getQueryTimerExtensionWebGL2(),a=n.getQueryParameter(e,n.QUERY_RESULT_AVAILABLE);return null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),a&&!this.disjoint}return a=(r=this.getQueryTimerExtensionWebGL1()).getQueryObjectEXT(e,r.QUERY_RESULT_AVAILABLE_EXT),null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),a&&!this.disjoint},e.prototype.pollFence=function(e){var t=this;return new Promise(function(n){t.addItemToPoll(function(){return e.isFencePassed()},function(){return n()})})},e.prototype.pollItems=function(){for(var e=function(e){for(var t=0,n=e.length-1,r=-1;t<=n;){var a=t+n>>1;e[a]()?(r=a,t=a+1):n=a-1}return r}(this.itemsToPoll.map(function(e){return e.isDoneFn})),t=0;t<=e;++t)(0,this.itemsToPoll[t].resolveFn)();this.itemsToPoll=this.itemsToPoll.slice(e+1)},e.prototype.addItemToPoll=function(e,t){var n=this;this.itemsToPoll.push({isDoneFn:e,resolveFn:t}),this.itemsToPoll.length>1||N(function(){return n.pollItems(),0===n.itemsToPoll.length})},e.prototype.bindTextureToFrameBuffer=function(e){this.throwIfDisposed(),An(this.gl,e,this.framebuffer),this.autoDebugValidate&&Tn(this.gl)},e.prototype.unbindTextureToFrameBuffer=function(){null!=this.outputTexture?(An(this.gl,this.outputTexture,this.framebuffer),this.autoDebugValidate&&Tn(this.gl)):Pn(this.gl,this.framebuffer)},e.prototype.downloadMatrixDriver=function(e,t){this.bindTextureToFrameBuffer(e);var n=t();return this.unbindTextureToFrameBuffer(),n},e.prototype.setOutputMatrixTextureDriver=function(e,t,n){this.throwIfDisposed();var r=this.gl;An(r,e,this.framebuffer),this.autoDebugValidate&&Tn(r),this.outputTexture=e,on(r,function(){return r.viewport(0,0,t,n)}),on(r,function(){return r.scissor(0,0,t,n)})},e.prototype.setOutputMatrixWriteRegionDriver=function(e,t,n,r){var a=this;this.throwIfDisposed(),on(this.gl,function(){return a.gl.scissor(e,t,n,r)})},e.prototype.throwIfDisposed=function(){if(this.disposed)throw new Error("Attempted to use disposed GPGPUContext.")},e.prototype.throwIfNoProgram=function(){if(null==this.program)throw new Error("No GPU program is currently set.")},e}();function or(e,t){if(e.length!==t.length)throw Error("Binary was compiled with "+e.length+" inputs, but was executed with "+t.length+" inputs");e.forEach(function(e,n){var r=e.logicalShape,a=t[n],i=a.shape;if(!w(r,i))throw Error("Binary was compiled with different shapes than the current args. Shapes "+r+" and "+i+" must match");if(!e.isUniform||!a.isUniform){var o=e.texShape,s=a.isUniform?null:a.texData.texShape;if(!w(o,s))throw Error("Binary was compiled with different texture shapes than the current args. Shape "+o+" and "+s+" must match")}})}var sr=function(){function e(e,t,n){this.variableNames=["probs"],this.outputShape=[e,n],this.userCode="\n uniform float seed;\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n\n float r = random(seed);\n float cdf = 0.0;\n\n for (int i = 0; i < "+(t-1)+"; i++) {\n cdf += getProbs(batch, i);\n\n if (r < cdf) {\n setOutput(float(i));\n return;\n }\n }\n\n // If no other event happened, last event happened.\n setOutput(float("+(t-1)+"));\n }\n "}return e.prototype.getCustomSetupFunc=function(e){var t=this;return function(n,r){null==t.seedLoc&&(t.seedLoc=n.getUniformLocation(r,"seed")),n.gl.uniform1f(t.seedLoc,e)}},e}(),ur=function(e){this.variableNames=["A"],this.outputShape=e,this.userCode="\n void main() {\n ivec2 rc = getOutputCoords();\n\n int r = rc.x;\n int c = rc.y;\n int rp1 = r + 1;\n int cp1 = c + 1;\n\n bool cEdge = cp1 >= "+e[1]+";\n bool rEdge = rp1 >= "+e[0]+";\n\n gl_FragColor = vec4(\n getA(r, c),\n cEdge ? 0. : getA(r, cp1),\n rEdge ? 0. : getA(rp1, c),\n rEdge || cEdge ? 0. : getA(rp1, cp1)\n );\n }\n "},lr=function(e,t,n){if(this.variableNames=["x"],"avg"===t&&n)throw new Error("Cannot compute positions for average pool.");var r=e.filterHeight,a=e.filterWidth,i=e.strideHeight,o=e.strideWidth,s=e.padInfo.top,u=e.padInfo.left;this.outputShape=e.outShape;var l="avg"===t,c="0.0";if(l||(c="-1.0 / 0.0"),n)this.userCode="\n const ivec2 strides = ivec2("+i+", "+o+");\n const ivec2 pads = ivec2("+s+", "+u+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n float minMaxValue = 0.0;\n float minMaxValueFound = 0.0;\n int minMaxPosition = 0;\n float avgValue = 0.0;\n\n for (int wR = 0; wR < "+r+"; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+a+"; wC++) {\n int xC = xCCorner + wC;\n\n if (xC < 0 || xC >= "+e.inWidth+") {\n continue;\n }\n\n float value = getX(batch, xR, xC, d);\n\n // If a min / max value has already been found, use it. If not,\n // use the current value.\n float currMinMaxValue = mix(\n value, minMaxValue, minMaxValueFound);\n if (value >= currMinMaxValue) {\n minMaxValue = value;\n minMaxValueFound = 1.0;\n minMaxPosition = wR * "+a+" + wC;\n }\n }\n }\n setOutput(float(minMaxPosition));\n }\n ";else{var f=t+"("+t+"("+t+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"avg"===t&&(f="avgValue / count");var p=4*Math.floor(a/4),h=a%4,d="\n if ("+l+") {\n avgValue += dot(values, ones);\n } else {\n minMaxValue = max(values, minMaxValue);\n }\n ";this.userCode="\n const ivec2 strides = ivec2("+i+", "+o+");\n const ivec2 pads = ivec2("+s+", "+u+");\n const float initializationValue = "+c+";\n const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);\n\n float count = 0.0;\n\n float getValue(int batch, int xR, int xC, int d) {\n if (xC < 0 || xC >= "+e.inWidth+") {\n return initializationValue;\n }\n count += 1.0;\n return getX(batch, xR, xC, d);\n }\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n vec4 minMaxValue = vec4("+c+");\n float avgValue = 0.0;\n count = 0.0;\n\n for (int wR = 0; wR < "+r+"; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+p+"; wC += 4) {\n int xC = xCCorner + wC;\n\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n getValue(batch, xR, xC + 3, d)\n );\n\n "+d+"\n }\n\n int xC = xCCorner + "+p+";\n if ("+(1===h)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+d+"\n } else if ("+(2===h)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n initializationValue,\n initializationValue\n );\n\n "+d+"\n } else if ("+(3===h)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n initializationValue\n );\n\n "+d+"\n }\n }\n setOutput("+f+");\n }\n "}},cr=function(){function e(e){this.variableNames=["source"],this.outputShape=e,this.rank=e.length;var t=qt(this.rank),n=function(e){if(1===e)return"sourceLoc";if(2===e)return"sourceLoc.x, sourceLoc.y";if(3===e)return"sourceLoc.x, sourceLoc.y, sourceLoc.z";if(4===e)return"sourceLoc.x, sourceLoc.y, sourceLoc.z, sourceLoc.w";throw Error("Slicing for rank "+e+" is not yet supported")}(this.rank);this.userCode="\n uniform "+t+" start;\n\n void main() {\n "+t+" sourceLoc = start + getOutputCoords();\n setOutput(getSource("+n+"));\n }\n "}return e.prototype.getCustomSetupFunc=function(e){var t=this;if(e.length!==this.rank)throw Error("The rank ("+this.rank+") of the program must match the length of start ("+e.length+")");return function(n,r){if(null!=t.startLoc||(t.startLoc=n.getUniformLocationNoThrow(r,"start"),null!=t.startLoc))if(1===t.rank)n.gl.uniform1i(t.startLoc,e[0]);else if(2===t.rank)n.gl.uniform2i(t.startLoc,e[0],e[1]);else if(3===t.rank)n.gl.uniform3i(t.startLoc,e[0],e[1],e[2]);else{if(4!==t.rank)throw Error("Slicing for rank "+t.rank+" is not yet supported");n.gl.uniform4i(t.startLoc,e[0],e[1],e[2],e[3])}}},e}(),fr=function(){function e(e){this.gpgpu=e,this.numUsedTextures=0,this.numFreeTextures=0,this.freeTextures={},this.logEnabled=!1,this.usedTextures={}}return e.prototype.acquireTexture=function(e,t){var n,r=pr(t),a=hr(e,r);if(a in this.freeTextures||(this.freeTextures[a]=[]),a in this.usedTextures||(this.usedTextures[a]=[]),this.freeTextures[a].length>0){this.numFreeTextures--,this.numUsedTextures++,this.log();var i=this.freeTextures[a].shift();return this.usedTextures[a].push(i),i}return this.numUsedTextures++,this.log(),r===Xt.PACKED_2X2_FLOAT32?n=this.gpgpu.createPackedMatrixTexture(e[0],e[1]):r===Xt.UNPACKED_FLOAT32?n=this.gpgpu.createFloat32MatrixTexture(e[0],e[1]):r===Xt.UNPACKED_FLOAT16?n=this.gpgpu.createFloat16MatrixTexture(e[0],e[1]):r===Xt.PACKED_4X1_UNSIGNED_BYTE&&(n=this.gpgpu.createUnsignedBytesMatrixTexture(e[0],e[1])),this.usedTextures[a].push(n),n},e.prototype.releaseTexture=function(e,t,n){var r=hr(t,pr(n));r in this.freeTextures||(this.freeTextures[r]=[]),this.freeTextures[r].push(e),this.numFreeTextures++,this.numUsedTextures--;var a=this.usedTextures[r],i=a.indexOf(e);if(i<0)throw new Error("Cannot release a texture that was never provided by this texture manager");a.splice(i,1),this.log()},e.prototype.log=function(){if(this.logEnabled){var e=this.numFreeTextures+this.numUsedTextures;console.log("Free/Used",this.numFreeTextures+" / "+this.numUsedTextures,"("+e+")")}},e.prototype.getNumUsedTextures=function(){return this.numUsedTextures},e.prototype.getNumFreeTextures=function(){return this.numFreeTextures},e.prototype.dispose=function(){var e=this;if(null!=this.freeTextures){for(var t in this.freeTextures)this.freeTextures[t].forEach(function(t){e.gpgpu.deleteMatrixTexture(t)});for(var t in this.usedTextures)this.usedTextures[t].forEach(function(t){e.gpgpu.deleteMatrixTexture(t)});this.freeTextures=null,this.usedTextures=null,this.numUsedTextures=0,this.numFreeTextures=0}},e}();function pr(e){if(e===Kt.DOWNLOAD||e===Kt.PIXELS)return Xt.PACKED_4X1_UNSIGNED_BYTE;if(e===Kt.UPLOAD)return Xt.UNPACKED_FLOAT32;if(e===Kt.RENDER)return me.get("WEBGL_RENDER_FLOAT32_ENABLED")?Xt.UNPACKED_FLOAT32:Xt.UNPACKED_FLOAT16;if(e===Kt.PACK)return Xt.PACKED_2X2_FLOAT32;throw new Error("Unknown logical texture type "+e)}function hr(e,t){return e[0]+"_"+e[1]+"_"+t}var dr=1.7580993408473768,mr=1.0507009873554805,gr=function(){function e(e,t){this.variableNames=["A"],this.outputShape=e,this.userCode="\n uniform float NAN;\n float unaryOperation(float x) {\n "+t+"\n }\n\n void main() {\n float x = getAAtOutCoords();\n float y = unaryOperation(x);\n\n setOutput(y);\n }\n "}return e.prototype.getCustomSetupFunc=function(){var e=this;return function(t,n){null==e.startLoc&&(e.startLoc=t.getUniformLocationNoThrow(n,"NAN"),null==e.startLoc)||t.gl.uniform1f(e.startLoc,NaN)}},e}(),yr="if (isNaN(x)) return x;",vr=Je({concat_:function(e,t){void 0===t&&(t=0),d(e.length>=1,"Pass at least one tensor to concat");var n=Ye(e,"tensors","concat");t=ze(t,n[0].shape)[0];var r=We(n.map(function(e){return e.shape}),t);if(0===b(r))return nt([],r);if(1===(n=n.filter(function(e){return e.size>0})).length)return n[0];var a=n.map(function(e){return e.shape});!function(e,t){var n=e[0].length;e.forEach(function(e,t){d(e.length===n,"Error in concat"+n+"D: rank of tensors["+t+"] must be the same as the rank of the rest ("+n+")")}),d(t>=0&&t>>0,t=(r*=t)>>>0,t+=4294967296*(r-=t)}return 2.3283064365386963e-10*(t>>>0)});n.next=function(){var e=2091639*n.s0+2.3283064365386963e-10*n.c;return n.s0=n.s1,n.s1=n.s2,n.s2=e-(n.c=0|e)},n.c=1,n.s0=r(" "),n.s1=r(" "),n.s2=r(" "),n.s0-=r(e),n.s0<0&&(n.s0+=1),n.s1-=r(e),n.s1<0&&(n.s1+=1),n.s2-=r(e),n.s2<0&&(n.s2+=1),r=null}(e),a=t&&t.state,i=n.next;return i.int32=function(){return 4294967296*n.next()|0},i.double=function(){return i()+1.1102230246251565e-16*(2097152*i()|0)},i.quick=i,a&&("object"==typeof a&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.alea=a}(0,e)}),Or=Er(function(e){!function(e,t,n){function r(e,t){return t.x=e.x,t.y=e.y,t.z=e.z,t.w=e.w,t}function a(e,t){var n=new function(e){var t=this,n="";t.x=0,t.y=0,t.z=0,t.w=0,t.next=function(){var e=t.x^t.x<<11;return t.x=t.y,t.y=t.z,t.z=t.w,t.w^=t.w>>>19^e^e>>>8},e===(0|e)?t.x=e:n+=e;for(var r=0;r>>0)/4294967296};return i.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===e);return e},i.int32=n.next,i.quick=i,a&&("object"==typeof a&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.xor128=a}(0,e)}),kr=Er(function(e){!function(e,t,n){function r(e,t){return t.x=e.x,t.y=e.y,t.z=e.z,t.w=e.w,t.v=e.v,t.d=e.d,t}function a(e,t){var n=new function(e){var t=this,n="";t.next=function(){var e=t.x^t.x>>>2;return t.x=t.y,t.y=t.z,t.z=t.w,t.w=t.v,(t.d=t.d+362437|0)+(t.v=t.v^t.v<<4^e^e<<1)|0},t.x=0,t.y=0,t.z=0,t.w=0,t.v=0,e===(0|e)?t.x=e:n+=e;for(var r=0;r>>4),t.next()}(e),a=t&&t.state,i=function(){return(n.next()>>>0)/4294967296};return i.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===e);return e},i.int32=n.next,i.quick=i,a&&("object"==typeof a&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.xorwow=a}(0,e)}),Ir=Er(function(e){!function(e,t,n){function r(e,t){return t.x=e.x.slice(),t.i=e.i,t}function a(e,t){null==e&&(e=+new Date);var n=new function(e){var t=this;t.next=function(){var e,n,r=t.x,a=t.i;return e=r[a],n=(e^=e>>>7)^e<<24,n^=(e=r[a+1&7])^e>>>10,n^=(e=r[a+3&7])^e>>>3,n^=(e=r[a+4&7])^e<<7,e=r[a+7&7],n^=(e^=e<<13)^e<<9,r[a]=n,t.i=a+1&7,n},function(e,t){var n,r=[];if(t===(0|t))r[0]=t;else for(t=""+t,n=0;n0;--n)e.next()}(t,e)}(e),a=t&&t.state,i=function(){return(n.next()>>>0)/4294967296};return i.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===e);return e},i.int32=n.next,i.quick=i,a&&(a.x&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.xorshift7=a}(0,e)}),Cr=Er(function(e){!function(e,t,n){function r(e,t){return t.i=e.i,t.w=e.w,t.X=e.X.slice(),t}function a(e,t){null==e&&(e=+new Date);var n=new function(e){var t=this;t.next=function(){var e,n,r=t.w,a=t.X,i=t.i;return t.w=r=r+1640531527|0,n=a[i+34&127],e=a[i=i+1&127],n^=n<<13,e^=e<<17,n^=n>>>15,e^=e>>>12,n=a[i]=n^e,t.i=i,n+(r^r>>>16)|0},function(e,t){var n,r,a,i,o,s=[],u=128;for(t===(0|t)?(r=t,t=null):(t+="\0",r=0,u=Math.max(u,t.length)),a=0,i=-32;i>>15,r^=r<<4,r^=r>>>13,i>=0&&(o=o+1640531527|0,a=0==(n=s[127&i]^=r+o)?a+1:0);for(a>=128&&(s[127&(t&&t.length||0)]=-1),a=127,i=512;i>0;--i)r=s[a+34&127],n=s[a=a+1&127],r^=r<<13,n^=n<<17,r^=r>>>15,n^=n>>>12,s[a]=r^n;e.w=o,e.X=s,e.i=a}(t,e)}(e),a=t&&t.state,i=function(){return(n.next()>>>0)/4294967296};return i.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===e);return e},i.int32=n.next,i.quick=i,a&&(a.X&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.xor4096=a}(0,e)}),Ar=Er(function(e){!function(e,t,n){function r(e,t){return t.a=e.a,t.b=e.b,t.c=e.c,t.d=e.d,t}function a(e,t){var n=new function(e){var t=this,n="";t.next=function(){var e=t.b,n=t.c,r=t.d,a=t.a;return e=e<<25^e>>>7^n,n=n-r|0,r=r<<24^r>>>8^a,a=a-e|0,t.b=e=e<<20^e>>>12^n,t.c=n=n-r|0,t.d=r<<16^n>>>16^a,t.a=a-e|0},t.a=0,t.b=0,t.c=-1640531527,t.d=1367130551,e===Math.floor(e)?(t.a=e/4294967296|0,t.b=0|e):n+=e;for(var r=0;r>>0)/4294967296};return i.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===e);return e},i.int32=n.next,i.quick=i,a&&("object"==typeof a&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.tychei=a}(0,e)}),Pr=Er(function(e){!function(t,r){var a,i=this,o=256,s=6,u="random",l=r.pow(o,s),c=r.pow(2,52),f=2*c,p=o-1;function h(e,n,h){var y=[],v=m(function e(t,n){var r,a=[],i=typeof t;if(n&&"object"==i)for(r in t)try{a.push(e(t[r],n-1))}catch(e){}return a.length?a:"string"==i?t:t+"\0"}((n=1==n?{entropy:!0}:n||{}).entropy?[e,g(t)]:null==e?function(){try{var e;return a&&(e=a.randomBytes)?e=e(o):(e=new Uint8Array(o),(i.crypto||i.msCrypto).getRandomValues(e)),g(e)}catch(e){var n=i.navigator,r=n&&n.plugins;return[+new Date,i,r,i.screen,g(t)]}}():e,3),y),b=new function(e){var t,n=e.length,r=this,a=0,i=r.i=r.j=0,s=r.S=[];for(n||(e=[n++]);a=f;)e/=2,t/=2,n>>>=1;return(e+n)/t};return w.int32=function(){return 0|b.g(4)},w.quick=function(){return b.g(4)/4294967296},w.double=w,m(g(b.S),t),(n.pass||h||function(e,t,n,a){return a&&(a.S&&d(a,b),e.state=function(){return d(b,{})}),n?(r[u]=e,t):e})(w,v,"global"in n?n.global:this==r,n.state)}function d(e,t){return t.i=e.i,t.j=e.j,t.S=e.S.slice(),t}function m(e,t){for(var n,r=e+"",a=0;a=1||0===o);var s=Math.sqrt(-2*Math.log(o)/o);t=this.mean+this.stdDev*a*s,n=this.mean+this.stdDev*i*s,this.truncated&&!this.isValidTruncated(t)||(r=!0)}return this.truncated&&!this.isValidTruncated(n)||(this.nextVal=this.convertValue(n)),this.convertValue(t)},e.prototype.convertValue=function(e){return null==this.dtype||"float32"===this.dtype?e:Math.round(e)},e.prototype.isValidTruncated=function(e){return e<=this.upper&&e>=this.lower},e}();function Rr(e,t){return l(this,void 0,void 0,function(){var n,r,a,i,o,s,u,l,f,p,h,d,m,g,y,v,b,w,x,_;return c(this,function(c){switch(c.label){case 0:if(2!==(n=Xe(e,"img","toPixels","int32")).rank&&3!==n.rank)throw new Error("toPixels only supports rank 2 or 3 tensors, got rank "+n.rank+".");if(r=n.shape.slice(0,2),a=r[0],i=r[1],(o=2===n.rank?1:n.shape[2])>4||2===o)throw new Error("toPixels only supports depth of size 1, 3 or 4 but got "+o);return s=n.min(),u=n.max(),[4,s.data()];case 1:return l=c.sent()[0],[4,u.data()];case 2:if(f=c.sent()[0],s.dispose(),u.dispose(),"float32"===n.dtype){if(l<0||f>1)throw new Error("Tensor values for a float32 Tensor must be in the range [0 - 1] but got range ["+l+" - "+f+"].")}else{if("int32"!==n.dtype)throw new Error("Unsupported type for toPixels: "+n.dtype+". Please use float32 or int32 tensors.");if(l<0||f>255)throw new Error("Tensor values for a int32 Tensor must be in the range [0 - 255] but got range ["+l+" - "+f+"].")}return[4,n.data()];case 3:for(p=c.sent(),h="float32"===n.dtype?255:1,d=new Uint8ClampedArray(i*a*4),m=0;m=1+t.length,"input rank should be > than [blockShape] but got "+r.rank),d(n.length===t.length,"crops.shape[0] must be equal to [blockShape] but got "+n.length),d(r.shape[0]%a==0,"input tensor batch must be divisible by prod( blockShape )"),me.engine.runKernel(function(e){return e.batchToSpaceND(r,t,n)},{$x:r},function(e){return{$x:function(){return e.spaceToBatchND(t,n)}}})}}),zr=Je({cast_:function(e,t){var n=Xe(e,"x","cast");return me.engine.runKernel(function(e){return e.cast(n,t)},{$x:n},function(e){return{$x:function(){return e.clone()}}})}}),Br=Je({clone_:function(e){var t=Xe(e,"x","clone");return me.engine.runKernel(function(e){return Q.make(t.shape,{dataId:t.dataId},t.dtype)},{$x:t},function(e){return{$x:function(){return e.toFloat()}}})}}),Fr=Je({cumsum_:function(e,t,n,r){void 0===t&&(t=0),void 0===n&&(n=!1),void 0===r&&(r=!1);var a=Xe(e,"x","cumsum"),i=Fe([t|=0],a.rank),o=a;null!=i&&(o=a.transpose(i));var s=Ue(1,a.rank)[0],u=me.engine.runKernel(function(e){return e.cumsum(o,s,n,r)},{permutedX:o},function(e){return{permutedX:function(){return e.cumsum(t,n,!r)}}});return null!=i&&(u=u.transpose(i)),u}}),Vr=Je({depthToSpace_:function(e,t,n){void 0===n&&(n="NHWC");var r=Xe(e,"x","depthToSpace"),a="NHWC"===n?r.shape[1]:r.shape[2],i="NHWC"===n?r.shape[2]:r.shape[3],o="NHWC"===n?r.shape[3]:r.shape[1];return d(a*t>=0,"Negative dimension size caused by overflow when multiplying\n "+a+" and "+t+" for depthToSpace with input shape\n "+r.shape),d(i*t>=0,"Negative dimension size caused by overflow when multiplying\n "+i+" and "+t+" for depthToSpace with input shape\n "+r.shape),d(o%(t*t)==0,"Dimension size must be evenly divisible by "+t*t+" but is "+o+" for depthToSpace with input shape "+r.shape),me.engine.runKernel(function(e){return e.depthToSpace(r,t,n)},{$x:r})}}),Ur=Je({expandDims_:function(e,t){void 0===t&&(t=0);var n=Xe(e,"x","expandDims");d(t<=n.rank,"Axis must be <= rank of the tensor");var r=n.shape.slice();return t<0&&(d(-(n.rank+1)<=t,"Axis must be in the interval ["+-(n.rank+1)+", "+n.rank+"]"),t=n.rank+t+1),r.splice(t,0,1),ta(n,r)}}),Wr=Je({eye_:function(e,t,n,r){void 0===r&&(r="float32"),null==t&&(t=e);for(var a=Dr([e,t],r),i=e<=t?e:t,o=0;o4)throw new Error("Cannot construct Tensor with more than 4 channels from pixels.");return me.engine.fromPixels(e,t)}}),$r=Je({multinomial_:function(e,t,n,r){void 0===r&&(r=!1);var a=Xe(e,"logits","multinomial"),i=a.size,o=a.rank;if(i<2)throw new Error("Error in multinomial: you need at least 2 outcomes, but got "+i+".");if(o>2)throw new Error("Rank of probabilities must be 1 or 2, but is "+o);n=n||Math.random();var s=1===o?a.as2D(1,-1):a,u=me.engine.runKernel(function(e){return e.multinomial(s,r,t,n)},{logits2D:s});return 1===o?u.as1D():u}}),Gr=Je({oneHot_:function(e,t,n,r){void 0===n&&(n=1),void 0===r&&(r=0);var a=Xe(e,"indices","oneHot","int32");if(d("int32"===a.dtype,"Indices must be of dtype `int32`"),t<2)throw new Error("Error in oneHot: depth must be >=2, but it is "+t);return me.engine.runKernel(function(e){return e.oneHot(a,t,n,r)},{$indices:a},function(e){return{$indices:function(){return _t(a)}}})}}),Hr=Je({pad_:function(e,t,n){void 0===n&&(n=0);var r=Xe(e,"x","pad");if(0===r.rank)throw new Error("pad(scalar) is not defined. Pass non-scalar to pad");var a=t.map(function(e){return e[0]});return me.engine.runKernel(function(e){return e.pad(r,t,n)},{$x:r},function(e){return{$x:function(){return e.slice(a,r.shape)}}})}}),Kr=Je({pad1d_:function(e,t,n){return void 0===n&&(n=0),d(2===t.length,"Invalid number of paddings. Must be length of 2."),Hr(e,[t],n)}}),Xr=Je({pad2d_:function(e,t,n){return void 0===n&&(n=0),d(2===t.length&&2===t[0].length&&2===t[1].length,"Invalid number of paddings. Must be length of 2 each."),Hr(e,t,n)}}),Yr=Je({pad3d_:function(e,t,n){return void 0===n&&(n=0),d(3===t.length&&2===t[0].length&&2===t[1].length&&2===t[2].length,"Invalid number of paddings. Must be length of 2 each."),Hr(e,t,n)}}),Jr=Je({pad4d_:function(e,t,n){return void 0===n&&(n=0),d(4===t.length&&2===t[0].length&&2===t[1].length&&2===t[2].length&&2===t[3].length,"Invalid number of paddings. Must be length of 2 each."),Hr(e,t,n)}}),Zr=Je({rand_:function(e,t,n){var r=b(e),a=null;if(null==n||"float32"===n)a=new Float32Array(r);else if("int32"===n)a=new Int32Array(r);else{if("bool"!==n)throw new Error("Unknown data type "+n);a=new Uint8Array(r)}for(var i=0;i=1+t.length,"input rank "+r.rank+" should be > than [blockShape] "+t.length),d(n.length===t.length,"paddings.shape[0] "+n.length+" must be equal to [blockShape] "+t.length),d(r.shape.reduce(function(e,r,a){return a>0&&a<=t.length?e&&(r+n[a-1][0]+n[a-1][1])%t[a-1]==0:e},!0),"input spatial dimensions "+r.shape.slice(1)+" with paddings "+n.toString()+" must be divisible by blockShapes "+t.toString()),me.engine.runKernel(function(e){return e.spaceToBatchND(r,t,n)},{$x:r},function(e){return{$x:function(){return e.batchToSpaceND(t,n)}}})}}),ra=Je({squeeze_:function(e,t){var n=Xe(e,"x","squeeze");return ta(n,k(n.shape,t).newShape)}}),aa=Je({stack_:function(e,t){void 0===t&&(t=0);var n=Ye(e,"tensors","stack");if(d(n.length>=1,"Pass at least one tensor to tf.stack"),1===n.length)return n[0].expandDims(t);var r=n[0].rank,a=n[0].shape,i=n[0].dtype;d(t<=r,"Axis must be <= rank of the tensor"),n.forEach(function(e){m(a,e.shape,"All tensors passed to stack must have matching shapes")}),n.forEach(function(e){d(i===e.dtype,"All tensors passed to stack must have matching dtypes")});var o=n.map(function(e){return e.expandDims(t)});return vr(o,t)}}),ia=Je({tile_:function(e,t){var n=Xe(e,"x","tile");return d(n.rank===t.length,"Error in transpose: rank of input "+n.rank+" must match length of reps "+t+"."),me.engine.runKernel(function(e){return e.tile(n,t)},{$x:n},function(e){return{$x:function(){var r=_t(n);if(1===n.rank)for(var a=0;a FLOAT_MAX) {\n return vec4(0.0, 0.0, 128.0, 127.0) / 255.0;\n } else if(v < -FLOAT_MAX) {\n return vec4(0.0, 0.0, 128.0, 255.0) / 255.0;\n }\n\n highp vec4 c = vec4(0,0,0,0);\n\n highp float e = floor(log2(av));\n highp float m = exp2(fract(log2(av))) - 1.0;\n\n c[2] = floor(128.0 * m);\n m -= c[2] / 128.0;\n c[1] = floor(32768.0 * m);\n m -= c[1] / 32768.0;\n c[0] = floor(8388608.0 * m);\n\n highp float ebias = e + 127.0;\n c[3] = floor(ebias / 2.0);\n ebias -= c[3] * 2.0;\n c[2] += floor(ebias) * 128.0;\n\n c[3] += 128.0 * step(0.0, -v);\n\n return c / 255.0;\n }\n\n void main() {\n float x = getAAtOutCoords();\n gl_FragColor = encode_float(x);\n }\n "}(a);this.compileAndRun(s,[o],i,null,!1);var u=this.texData.get(i.dataId),l=this.gpgpu.downloadByteEncodedFloatMatrixFromOutputTexture(u.texture,u.texShape[0],u.texShape[1]);return o.dispose(),i.dispose(),l},e.prototype.time=function(e){return l(this,void 0,void 0,function(){var t,n,r,a,i,o;return c(this,function(s){switch(s.label){case 0:return t=this.activeTimers,n=[],r=!1,null==this.programTimersStack?(this.programTimersStack=n,r=!0):this.activeTimers.push(n),this.activeTimers=n,e(),a=y(this.activeTimers),this.activeTimers=t,r&&(this.programTimersStack=null),[4,Promise.all(a).then(function(e){var t=0;return e.forEach(function(e){return t+=e}),t})];case 1:return i=s.sent(),o={uploadWaitMs:this.uploadWaitMs,downloadWaitMs:this.downloadWaitMs,kernelMs:i,wallMs:null},this.uploadWaitMs=0,this.downloadWaitMs=0,[2,o]}})})},e.prototype.memory=function(){return{unreliable:!1,numBytesInGPU:this.numBytesInGPU}},e.prototype.startTimer=function(){return me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?this.gpgpu.beginQuery():{startMs:performance.now(),endMs:null}},e.prototype.endTimer=function(e){return me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?(this.gpgpu.endQuery(),e):(e.endMs=performance.now(),e)},e.prototype.getQueryTime=function(e){return l(this,void 0,void 0,function(){var t;return c(this,function(n){return me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?[2,this.gpgpu.waitForQueryAndGetTime(e)]:[2,(t=e).endMs-t.startMs]})})},e.prototype.disposeData=function(e){if(!this.pendingDisposal.has(e))if(this.pendingRead.has(e))this.pendingDisposal.add(e);else if(this.texData.has(e)){var t=this.texData.get(e),n=t.texture,r=t.texShape,a=t.usage,i=t.complexTensors;null!=n&&this.releaseTexture(e,n,r,a),null!=i&&(i.real.dispose(),i.imag.dispose()),this.texData.delete(e)}},e.prototype.getTexture=function(e){return this.uploadToGPU(e),this.texData.get(e).texture},e.prototype.getGPGPUContext=function(){return this.gpgpu},e.prototype.getCanvas=function(){return this.canvas},e.prototype.complex=function(e,t){var n=Q.make(e.shape,{},"complex64");return this.texData.get(n.dataId).complexTensors={real:me.engine.keep(e.clone()),imag:me.engine.keep(t.clone())},n},e.prototype.real=function(e){return this.texData.get(e.dataId).complexTensors.real.clone()},e.prototype.imag=function(e){return this.texData.get(e.dataId).complexTensors.imag.clone()},e.prototype.slice=function(e,t,n){var r=new cr(n),a=r.getCustomSetupFunc(t);return this.compileAndRun(r,[e],null,a)},e.prototype.stridedSlice=function(e,t,n,r,a,i,o,s,u){var l=Ge(e.shape,t,n,r,a,i,o,s,u),c=l[0],f=l[1],p=l[2],h=f.filter(function(e,t){return-1===p.indexOf(t)});if(h.some(function(e){return 0===e}))return nt([],h);var d=new function(e,t,n,r){this.variableNames=["x"];var a=n.filter(function(e,t){return-1===r.indexOf(t)});this.outputShape=a;var i=n.length,o=qt(n.length),s=qt(a.length),u="";if(1===i)u="coords * strides + begin";else{var l=0;u=n.map(function(e,t){return-1===r.indexOf(t)?(l++,1===a.length?"coords * strides["+t+"] + begin["+t+"]":"coords["+(l-1)+"] * strides["+t+"] + begin["+t+"]"):"begin["+t+"]"}).join(",")}this.userCode="\n "+o+" begin = "+o+"("+e+");\n "+o+" strides = "+o+"("+t+");\n\n void main() {\n "+s+" coords = getOutputCoords();\n setOutput(getX("+u+"));\n }\n "}(c,r,f,p);return this.compileAndRun(d,[e])},e.prototype.reverse=function(e,t){var n=new function(e,t){this.variableNames=["x"];var n=e.length;if(n>4)throw new Error("WebGL backend: Reverse of rank-"+n+" tensor is not yet supported");if(this.outputShape=e,1!==n){var r=e.map(function(n,r){return function(n){return-1!==t.indexOf(n)&&1!==e[n]?e[n]+" - coords["+n+"] - 1":"coords["+n+"]"}(r)}).join(","),a=qt(n);this.userCode="\n void main() {\n "+a+" coords = getOutputCoords();\n setOutput(getX("+r+"));\n }\n "}else this.userCode="\n void main() {\n int coord = getOutputCoords();\n setOutput(getX("+e[0]+" - coord - 1));\n }\n "}(e.shape,t);return this.compileAndRun(n,[e])},e.prototype.concat2Tensors=function(e,t,n){var r=We([e.shape,t.shape],n),a=e.as2D(-1,b(e.shape.slice(n))),i=t.as2D(-1,b(t.shape.slice(n))),o=new function(e,t){this.variableNames=["A","B"],this.outputShape=[],this.outputShape=We([e,t],1),this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int yR = coords.x;\n int yC = coords.y;\n\n float value = 0.0;\n if (yC < "+e[1]+") {\n value = getA(yR, yC);\n } else {\n yC -= "+e[1]+";\n value = getB(yR, yC);\n }\n\n setOutput(value);\n }\n "}(a.shape,i.shape);return this.compileAndRun(o,[a,i]).reshape(r)},e.prototype.concat=function(e,t){if(1===e.length)return e[0];for(var n=e[0],r=1;r= 0 && idx <= "+s+") {\n float z = getX(b, r, c, idx);\n sum += z * z;\n }\n }\n float val = x * "+i+";\n setOutput(val);\n }\n "}(e.shape,t,n,r,a);return this.compileAndRun(i,[e])},e.prototype.LRNGrad=function(e,t,n,r,a,i,o){var s=new function(e,t,n,r,a){this.variableNames=["inputImage","outputImage","dy"],this.outputShape=[],this.outputShape=e,this.depth=e[3],this.depthRadius=t,this.bias=n,this.alpha=r,this.beta=a,this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int r = coords[1];\n int c = coords[2];\n\n float result = 0.0;\n for (int d = 0; d < "+this.depth+"; ++d) {\n int depthBegin = int(max(0.0, float(d - "+t+")));\n int depthEnd = int(min(float("+this.depth+"),\n float(d + "+t+" + 1)));\n\n const int MIN_DEPTH_BEGIN = 0;\n const int MAX_DEPTH_END = "+this.depth+";\n\n float norm = 0.0;\n for (int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k) {\n if (k < depthBegin){\n continue;\n }\n else if (k >= depthBegin && k < depthEnd) {\n norm += getInputImage(b, r, c, k) * getInputImage(b, r, c, k);\n }\n else {\n break;\n }\n }\n\n norm = float("+r+") * norm + float("+n+");\n\n for(int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k){\n if (k < depthBegin){\n continue;\n }\n else if (k >= depthBegin && k < depthEnd){\n float dyi = -2.0 * float("+r+")\n * float("+a+")\n * getInputImage(b ,r ,c, k) * getOutputImage(b, r, c, d)\n / norm;\n if (k == d) {\n dyi += pow(norm, -1.0 * "+a+");\n }\n if (k == coords[3]) {\n dyi *= getDy(b, r, c, d);\n result += dyi;\n }\n }\n else {\n break;\n }\n }\n }\n setOutput(result);\n }\n "}(t.shape,r,a,i,o);return this.compileAndRun(s,[t,n,e])},e.prototype.tile=function(e,t){var n=new function(e,t){this.variableNames=["A"];for(var n=new Array(e.length),r=0;r5)throw Error("Tile for rank "+t+" is not yet supported");if(1===t)return"imod(resRC, "+e[0]+")";for(var n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u"],r=[],a=0;a= end) {\n setOutput(float("+n+"));\n } else {\n setOutput(getX(outC - start));\n }\n }\n "}(e.shape,t,n);return this.compileAndRun(r,[e])},e.prototype.transpose=function(e,t){var n=new function(e,t){this.variableNames=["A"];for(var n=new Array(e.length),r=0;r6)throw Error("Transpose for rank "+t+" is not yet supported");for(var n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u","resRC.v"],r=new Array(t),a=0;a4)throw Error("Gather for rank "+n+" is not yet supported");if(1===n)return"int(getIndices(resRC))";for(var r=["resRC.x","resRC.y","resRC.z","resRC.w"],a=[],i=0;i 4 with a WebGL backend not implemented yet");var r=t.reduce(function(e,t){return e*t}),a=Ae(e.shape,t,r),i=Pe(a.length,t.length),o=Te(e.shape,t,r),s=Me(n,t.length),u=Re(o,n,t.length);return e.reshape(a).transpose(i).reshape(o).slice(s,u)},e.prototype.spaceToBatchND=function(e,t,n){d(e.rank<=4,"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet");var r=t.reduce(function(e,t){return e*t}),a=[[0,0]];a.push.apply(a,n);for(var i=1+t.length;i= 1.0 && floatedReducedAllValue >= 1.0);\n ",p="bvec4"):"any"===t&&(o="0.0",f="\n bool reducedAnyValue = any(values);\n float floatedReducedAnyValue = float(reducedAnyValue);\n anyValue = float(anyValue >= 1.0 || floatedReducedAnyValue >= 1.0);\n ",p="bvec4");var h="";a%n>0&&(h="\n if (inIdx < 0 || inIdx >= "+a+") {\n return initializationValue;\n }\n "),this.userCode="\n const float initializationValue = "+o+";\n const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);\n\n float getValue(int batch, int inIdx) {\n "+h+"\n return getX(batch, inIdx);\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = outIdx * "+n+";\n\n vec4 minMaxValue = vec4("+o+");\n float sumValue = 0.0;\n float allValue = 1.0;\n float anyValue = 0.0;\n\n for (int i = 0; i < "+l+"; i += 4) {\n int inIdx = inOffset + i;\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n "+f+"\n }\n\n int inIdx = inOffset + "+l+";\n if ("+(1===c)+") {\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(2===c)+") {\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(3===c)+") {\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n "+f+"\n }\n setOutput("+u+");\n }\n "}({windowSize:$e(a),inSize:a,batchSize:r},t),o=i.outputShape,s=o[0],u=o[1],l=this.makeOutputArray([s,u],n);return this.compileAndRun(i,[e],l),1===l.shape[1]?l:this.reduce(l,t,n)},e.prototype.argReduce=function(e,t,n){void 0===n&&(n=null);var r=e.shape[0],a=e.shape[1];null!=n&&(r=n.shape[0],a=n.shape[1]);var i=new function(e,t,n){this.variableNames=["A"];var r=e.windowSize,a=e.batchSize,i=e.inSize,o=Math.ceil(i/r);n||this.variableNames.push("bestIndicesA"),this.outputShape=[a,o];var s="max"===t?">":"<",u=n?"inOffset + i;":"round(getBestIndicesA(batch, inOffset + i));";this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = outIdx * "+r+";\n\n int bestIndex = 0;\n float bestValue = getA(batch, inOffset);\n\n for (int i = 0; i < "+r+"; i++) {\n int inIdx = "+u+";\n float candidate = getA(batch, inIdx);\n if (candidate "+s+" bestValue) {\n bestValue = candidate;\n bestIndex = inIdx;\n }\n }\n setOutput(float(bestIndex));\n }\n "}({windowSize:$e(a),inSize:a,batchSize:r},t,null==n),o=i.outputShape,s=o[0],u=o[1],l=this.makeOutputArray([s,u],"int32"),c=[e];return null!=n&&c.push(n),this.compileAndRun(i,c,l),1===l.shape[1]?l:this.argReduce(e,t,l)},e.prototype.sum=function(e,t){Be("sum",t,e.rank);var n=je(e.shape,t),r=n[0],a=b(n[1]),i=e.as2D(-1,a),o=Nt(e.dtype);return this.reduce(i,"sum",o).reshape(r)},e.prototype.unsortedSegmentSum=function(e,t,n){var r=0,a=Fe([r],e.rank),i=e;null!=a&&(i=e.transpose(a),r=Ue(1,e.rank)[0]);var o=function(e,t,n){for(var r=[],a=e.length,i=0;it||n===e){r=!0;break}n=D(e,n+1)}return n}(o,a),u=new function(e,t){this.variableNames=["x","segmentIds"];var n=e.windowSize,r=e.batchSize,a=e.inSize,i=e.numSegments,o=i*Math.ceil(a/n);this.outputShape=[r,o];var s=4*Math.floor(n/4),u=n%4,l="\n sumValue += dot(values, filter);\n ",c="";a%n>0&&(c="\n if (inIdx < 0 || inIdx >= "+a+") {\n return initializationValue;\n }\n ");var f="";a%n>0&&(f="\n if (inIdx < 0 || inIdx >= "+a+") {\n return -1.0;\n }\n "),this.userCode="\n const float initializationValue = 0.0;\n\n float getValue(int batch, int inIdx) {\n "+c+"\n return getX(batch, inIdx);\n }\n\n float getSegmentIdAtIndex(int inIdx) {\n "+f+"\n return getSegmentIds(inIdx);\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = int(floor(float(outIdx) / float(\n "+i+")) * float("+n+"));\n int currentSeg = int(mod(float(outIdx), float("+i+")));\n\n float sumValue = 0.0;\n\n for (int i = 0; i < "+s+"; i += 4) {\n int inIdx = inOffset + i;\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n vec4 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 3)) == currentSeg ? 1 : 0\n );\n\n "+l+"\n }\n\n int inIdx = inOffset + "+s+";\n if ("+(1===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n int inIdxSeg = int(getSegmentIdAtIndex(inIdx));\n\n vec4 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n 0,\n 0,\n 0\n );\n\n "+l+"\n } else if ("+(2===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n vec4 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n 0,\n 0\n );\n\n "+l+"\n } else if ("+(3===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n vec4 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,\n 0\n );\n\n "+l+"\n }\n setOutput(sumValue);\n }\n "}({windowSize:s,inSize:o,batchSize:i,numSegments:a},t),l=u.outputShape,c=l[0],f=l[1],p=this.makeOutputArray([c,f],r);return this.compileAndRun(u,[e,n],p),p.shape[1]===a?p:(n=dt(0,a).tile([o/s]),this.segOpCompute(p,t,n,r,a))},e.prototype.argMin=function(e,t){var n=[t];Be("argMin",n,e.rank);var r=je(e.shape,n),a=r[0],i=b(r[1]),o=e.as2D(-1,i);return this.argReduce(o,"min").reshape(a)},e.prototype.argMax=function(e,t){var n=[t];Be("argMax",n,e.rank);var r=je(e.shape,n),a=r[0],i=b(r[1]),o=e.as2D(-1,i);return this.argReduce(o,"max").reshape(a)},e.prototype.cumsum=function(e,t,n,r){if(t!==e.rank-1)throw new Error("WebGL cumsum shader expects an inner-most axis="+(e.rank-1)+" but got axis="+t);var a=new function(e,t,n){this.variableNames=["x"],this.outputShape=e;var r=e.length,a=e[e.length-1],i=n?"<":">";this.userCode="\n int getIndex(int i) {\n "+(n?"return "+a+" -i - 1;":"return i;")+"\n }\n\n void main() {\n "+qt(r)+" coords = getOutputCoords();\n int end = "+Ht(r,"coords")+";\n float val = 0.0;\n for (int i = "+a+" - 1; i >= 0; i -= 1) {\n int idx = getIndex(i);\n if (idx "+i+" end) {\n continue;\n }\n if (idx == end && "+t+") {\n continue;\n }\n "+Ht(r,"coords")+" = idx;\n val += getX("+function(e,t){if(1===e)return""+t;if(2===e)return t+".x, "+t+".y";if(3===e)return t+".x, "+t+".y, "+t+".z";if(4===e)return t+".x, "+t+".y, "+t+".z, "+t+".w";throw Error("Cumulative sum for rank "+e+" is not yet supported")}(r,"coords")+");\n }\n setOutput(val);\n }\n "}(e.shape,n,r);return this.compileAndRun(a,[e])},e.prototype.equal=function(e,t){var n=new Bt("return float(a == b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.notEqual=function(e,t){var n=new Bt("return float(a != b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.less=function(e,t){var n=new Bt("return float(a < b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.lessEqual=function(e,t){var n=new Bt("return float(a <= b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.greater=function(e,t){var n=new Bt("return float(a > b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.greaterEqual=function(e,t){var n=new Bt("return float(a >= b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.logicalNot=function(e){var t=new gr(e.shape,"return float(!(x >= 1.0));");return this.compileAndRun(t,[e])},e.prototype.logicalAnd=function(e,t){var n=new Bt("return float(a >= 1.0 && b >= 1.0);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.logicalOr=function(e,t){var n=new Bt("return float(a >= 1.0 || b >= 1.0);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.select=function(e,t,n){var r=new function(e,t,n){var r,a;if(this.variableNames=["c","a","b"],this.outputShape=t,n>4)throw Error("Where for rank "+n+" is not yet supported");if(1===n)a="resRC",r="resRC";else{for(var i=["resRC.x","resRC.y","resRC.z","resRC.w"],o=[],s=[],u=0;u= 1.0) {\n setOutput(getA("+a+"));\n } else {\n setOutput(getB("+a+"));\n }\n }\n "}(e.rank,t.shape,t.rank),a=this.makeOutputArray(r.outputShape,Et(t.dtype,n.dtype));return this.compileAndRun(r,[e,t,n],a)},e.prototype.where=function(e){Ce("tf.where() in webgl locks the UI thread. Call tf.whereAsync() instead");var t=e.dataSync();return ua(e.shape,t)},e.prototype.topk=function(e,t,n){return Tt(e.dataSync(),e.shape,e.dtype,t)},e.prototype.min=function(e,t){Be("min",t,e.rank);var n=je(e.shape,t),r=n[0],a=b(n[1]),i=e.as2D(-1,a);return this.reduce(i,"min",i.dtype).reshape(r)},e.prototype.minimum=function(e,t){var n=new Bt("\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n\n return min(a, b);\n",e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.mod=function(e,t){var n=new Bt("if (b == 0.0) return NAN;\n return mod(a, b);",e.shape,t.shape),r=n.getCustomSetupFunc();return this.compileAndRun(n,[e,t],null,r)},e.prototype.max=function(e,t){Be("max",t,e.rank);var n=je(e.shape,t),r=n[0],a=b(n[1]),i=e.as2D(-1,a);return this.reduce(i,"max",i.dtype).reshape(r)},e.prototype.maximum=function(e,t){var n=new Bt("\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n\n return max(a, b);\n",e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.all=function(e,t){Be("all",t,e.rank);var n=je(e.shape,t),r=n[0],a=b(n[1]),i=e.as2D(-1,a);return this.reduce(i,"all",i.dtype).reshape(r)},e.prototype.any=function(e,t){Be("any",t,e.rank);var n=je(e.shape,t),r=n[0],a=b(n[1]),i=e.as2D(-1,a);return this.reduce(i,"any",i.dtype).reshape(r)},e.prototype.squaredDifference=function(e,t){var n=new Bt("return (a - b) * (a - b);",e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.realDivide=function(e,t){var n=new Bt("if (a == b) return 1.0;\n return a / b;",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"float32");return this.compileAndRun(n,[e,t],r)},e.prototype.floorDiv=function(e,t){var n=new Bt("\n float resultSign = sign(a) * sign(b);\n int ia = round(a);\n int ib = round(b);\n int result = ia / ib;\n int amodb = ia - ib * result;\n\n if (resultSign < 0.0 && amodb != 0) {\n result -= 1;\n }\n return float(result);\n",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"int32");return this.compileAndRun(n,[e,t],r)},e.prototype.add=function(e,t){if("complex64"===e.dtype&&"complex64"===t.dtype)return this.complexSeparableBinaryOp(e,t,Lt);var n=new Bt(Lt,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,Et(e.dtype,t.dtype));return this.compileAndRun(n,[e,t],r)},e.prototype.complexSeparableBinaryOp=function(e,t,n){var r=this,a=this.texData.get(e.dataId),i=this.texData.get(t.dataId),o=[[a.complexTensors.real,i.complexTensors.real],[a.complexTensors.imag,i.complexTensors.imag]].map(function(a){var i=a[0],o=a[1],s=new Bt(n,e.shape,t.shape),u=r.makeOutputArray(s.outputShape,Et(i.dtype,o.dtype)),l=r.makeComplexComponentTensorHandle(e,i),c=r.makeComplexComponentTensorHandle(t,o);return r.compileAndRun(s,[l,c],u)}),s=o[0],u=o[1],l=this.complex(s,u);return s.dispose(),u.dispose(),l},e.prototype.makeComplexComponentTensorHandle=function(e,t){return{dataId:t.dataId,dtype:t.dtype,shape:e.shape}},e.prototype.addN=function(e){for(var t=e[0],n=1;n 0.5) {\n return ceil(x);\n } else {\n if (mod(base, 2.0) == 0.0) {\n return base;\n } else {\n return base + 1.0;\n }\n }\n");return this.compileAndRun(t,[e])},e.prototype.exp=function(e){var t=new gr(e.shape,"return exp(x);");return this.compileAndRun(t,[e])},e.prototype.expm1=function(e){var t=new gr(e.shape,"return exp(x) - 1.0;");return this.compileAndRun(t,[e])},e.prototype.log=function(e){var t=new gr(e.shape,"if (x < 0.0) return NAN;\n return log(x);"),n=t.getCustomSetupFunc();return this.compileAndRun(t,[e],null,n)},e.prototype.log1p=function(e){var t=new gr(e.shape,"return log(1.0 + x);");return this.compileAndRun(t,[e])},e.prototype.sqrt=function(e){var t=new gr(e.shape,"return sqrt(x);");return this.compileAndRun(t,[e])},e.prototype.rsqrt=function(e){var t=new gr(e.shape,"return inversesqrt(x);");return this.compileAndRun(t,[e])},e.prototype.square=function(e){var t=new gr(e.shape,"return x * x;");return this.compileAndRun(t,[e])},e.prototype.reciprocal=function(e){var t=new gr(e.shape,"return 1.0 / x;");return this.compileAndRun(t,[e])},e.prototype.relu=function(e){var t=new gr(e.shape,"if (isNaN(x)) return x;\n return (x < 0.0) ? 0.0 : x;\n");return this.compileAndRun(t,[e])},e.prototype.elu=function(e){var t=new gr(e.shape,"return (x >= 0.0) ? x : (exp(x) - 1.0);");return this.compileAndRun(t,[e])},e.prototype.eluDer=function(e,t){var n=new Bt("return (b >= 1.0) ? a : a * (b + 1.0);",e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.selu=function(e){var t=new gr(e.shape,"\n // Stable and Attracting Fixed Point (0, 1) for Normalized Weights.\n // see: https://arxiv.org/abs/1706.02515\n float scaleAlpha = 1.7580993408473768;\n float scale = 1.0507009873554805;\n return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);\n");return this.compileAndRun(t,[e])},e.prototype.int=function(e){var t=new gr(e.shape,"return float(int(x));"),n=this.makeOutputArray(t.outputShape,"int32");return this.compileAndRun(t,[e],n)},e.prototype.clip=function(e,t,n){var r=new function(e,t,n){this.variableNames=["A"],this.outputShape=e,this.userCode="\n void main() {\n float value = getAAtOutCoords();\n if (isNaN(value)) {\n setOutput(value);\n return;\n }\n\n setOutput(clamp(value, float("+t+"), float("+n+")));\n }\n "}(e.shape,t,n);return this.compileAndRun(r,[e])},e.prototype.abs=function(e){var t=new gr(e.shape,"return abs(x);");return this.compileAndRun(t,[e])},e.prototype.sigmoid=function(e){var t=new gr(e.shape,"return 1.0 / (1.0 + exp(-1.0 * x));");return this.compileAndRun(t,[e])},e.prototype.softplus=function(e){var t=new gr(e.shape,"\n float epsilon = 1.1920928955078125e-7;\n float threshold = log(epsilon) + 2.0;\n\n bool too_large = x > -threshold;\n bool too_small = x < threshold;\n\n float result;\n float exp_x = exp(x);\n\n if (too_large){\n result = x;\n }\n else if (too_small){\n result = exp_x;\n }\n else{\n result = log(exp_x + 1.0);\n }\n return result;\n");return this.compileAndRun(t,[e])},e.prototype.sin=function(e){var t=new gr(e.shape,"if (isNaN(x)) return x;\n return sin(x);\n");return this.compileAndRun(t,[e])},e.prototype.cos=function(e){var t=new gr(e.shape,"if (isNaN(x)) return x;\n return cos(x);\n");return this.compileAndRun(t,[e])},e.prototype.tan=function(e){var t=new gr(e.shape,"return tan(x);");return this.compileAndRun(t,[e])},e.prototype.asin=function(e){var t=new gr(e.shape,"return asin(x);");return this.compileAndRun(t,[e])},e.prototype.acos=function(e){var t=new gr(e.shape,"return acos(x);");return this.compileAndRun(t,[e])},e.prototype.atan=function(e){var t=new gr(e.shape,"if (isNaN(x)) return x;\n return atan(x);\n");return this.compileAndRun(t,[e])},e.prototype.atan2=function(e,t){var n=new Bt("\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n\n return atan(a, b);\n",e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.sinh=function(e){var t=new gr(e.shape,"\n float e2x = exp(x);\n return (e2x - 1.0 / e2x) / 2.0;\n");return this.compileAndRun(t,[e])},e.prototype.cosh=function(e){var t=new gr(e.shape,"\n float e2x = exp(-x);\n return (e2x + 1.0 / e2x) / 2.0;\n");return this.compileAndRun(t,[e])},e.prototype.tanh=function(e){var t=new gr(e.shape,"\n float e2x = exp(-2.0 * abs(x));\n return sign(x) * (1.0 - e2x) / (1.0 + e2x);\n");return this.compileAndRun(t,[e])},e.prototype.asinh=function(e){var t=new gr(e.shape,"return log(x + sqrt(x * x + 1.0));");return this.compileAndRun(t,[e])},e.prototype.acosh=function(e){var t=new gr(e.shape,"if (isNaN(x)) return x;\n if (x < 1.0) return NAN;\n return log(x + sqrt(x * x - 1.0));"),n=t.getCustomSetupFunc();return this.compileAndRun(t,[e],null,n)},e.prototype.atanh=function(e){var t=new gr(e.shape,"if (isNaN(x)) return x;\n if ((x < -1.0) || (x > 1.0)) return NAN;\n return (log(1.0 + x) - log(1.0 - x)) / 2.0;"),n=t.getCustomSetupFunc();return this.compileAndRun(t,[e],null,n)},e.prototype.erf=function(e){var t=new gr(e.shape,'\n // Error function is calculated approximately with elementary function.\n // See "Handbook of Mathematical Functions with Formulas,\n // Graphs, and Mathematical Tables", Abramowitz and Stegun.\n float p = 0.3275911;\n float a1 = 0.254829592;\n float a2 = -0.284496736;\n float a3 = 1.421413741;\n float a4 = -1.453152027;\n float a5 = 1.061405429;\n\n float t = 1.0 / (1.0 + p * x);\n return 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);\n');return this.compileAndRun(t,[e])},e.prototype.step=function(e,t){var n=new gr(e.shape,function(e){return void 0===e&&(e=0),yr+"\n return x > 0.0 ? 1.0 : float("+e+");\n "}(t));return this.compileAndRun(n,[e])},e.prototype.conv2d=function(e,t,n){var r=new function(e){this.variableNames=["x","W"],this.outputShape=e.outShape;var t=e.padInfo.top,n=e.padInfo.left,r=e.strideHeight,a=e.strideWidth,i=e.dilationHeight,o=e.dilationWidth,s=e.filterHeight,u=e.filterWidth,l=4*Math.floor(e.inChannels/4),c=e.inChannels%4;this.userCode="\n const ivec2 strides = ivec2("+r+", "+a+");\n const ivec2 pads = ivec2("+t+", "+n+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d2 = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // Convolve x(?, ?, d1) with w(:, :, d1, d2) to get y(yR, yC, d2).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+s+"; wR++) {\n int xR = xRCorner + wR * "+i+";\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+u+"; wC++) {\n int xC = xCCorner + wC * "+o+";\n\n if (xC < 0 || xC >= "+e.inWidth+") {\n continue;\n }\n\n for (int d1 = 0; d1 < "+l+"; d1 += 4) {\n vec4 xValues = vec4(\n getX(batch, xR, xC, d1),\n getX(batch, xR, xC, d1 + 1),\n getX(batch, xR, xC, d1 + 2),\n getX(batch, xR, xC, d1 + 3)\n );\n vec4 wValues = vec4(\n getW(wR, wC, d1, d2),\n getW(wR, wC, d1 + 1, d2),\n getW(wR, wC, d1 + 2, d2),\n getW(wR, wC, d1 + 3, d2)\n );\n\n dotProd += dot(xValues, wValues);\n }\n\n if ("+(1===c)+") {\n dotProd +=\n getX(batch, xR, xC, "+l+") *\n getW(wR, wC, "+l+", d2);\n } else if ("+(2===c)+") {\n vec2 xValues = vec2(\n getX(batch, xR, xC, "+l+"),\n getX(batch, xR, xC, "+l+" + 1)\n );\n vec2 wValues = vec2(\n getW(wR, wC, "+l+", d2),\n getW(wR, wC, "+l+" + 1, d2)\n );\n dotProd += dot(xValues, wValues);\n } else if ("+(3===c)+") {\n vec3 xValues = vec3(\n getX(batch, xR, xC, "+l+"),\n getX(batch, xR, xC, "+l+" + 1),\n getX(batch, xR, xC, "+l+" + 2)\n );\n vec3 wValues = vec3(\n getW(wR, wC, "+l+", d2),\n getW(wR, wC, "+l+" + 1, d2),\n getW(wR, wC, "+l+" + 2, d2)\n );\n dotProd += dot(xValues, wValues);\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.conv2dDerInput=function(e,t,n){var r=new function(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,i=t-1-e.padInfo.top,o=n-1-e.padInfo.left;this.userCode="\n const ivec2 pads = ivec2("+i+", "+o+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords[3];\n\n ivec2 dyCorner = coords.yz - pads;\n int dyRCorner = dyCorner.x;\n int dyCCorner = dyCorner.y;\n\n // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+t+" - 1 - wR;\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = "+n+" - 1 - wC;\n\n for (int d2 = 0; d2 < "+e.outChannels+"; d2++) {\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, d2);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.conv2dDerFilter=function(e,t,n){var r=new function(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;var t=e.strideHeight,n=e.strideWidth,r=e.padInfo.top,a=e.padInfo.left;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int wR = coords.x;\n int wC = coords.y;\n int d1 = coords.z;\n int d2 = coords.w;\n\n // Convolve x(?, ?, d1) with dy(:, :, d2) to get dw(wR, wC, d1, d2).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n\n for (int b = 0; b < "+e.batchSize+"; b++) {\n for (int yR = 0; yR < "+e.outHeight+"; yR++) {\n int xR = wR + yR * "+t+" - "+r+";\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+e.outWidth+"; yC++) {\n int xC = wC + yC * "+n+" - "+a+";\n\n if (xC < 0 || xC >= "+e.inWidth+") {\n continue;\n }\n\n float dyValue = getDy(b, yR, yC, d2);\n float xValue = getX(b, xR, xC, d1);\n dotProd += (xValue * dyValue);\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.depthwiseConv2D=function(e,t,n){var r=new function(e){this.variableNames=["x","W"],this.outputShape=e.outShape;var t=e.inHeight,n=e.inWidth,r=e.padInfo.top,a=e.padInfo.left,i=e.strideHeight,o=e.strideWidth,s=e.dilationHeight,u=e.dilationWidth,l=e.filterHeight,c=e.filterWidth,f=e.outChannels/e.inChannels;this.userCode="\n const ivec2 strides = ivec2("+i+", "+o+");\n const ivec2 pads = ivec2("+r+", "+a+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords.x;\n ivec2 xRCCorner = coords.yz * strides - pads;\n int d2 = coords.w;\n int d1 = d2 / "+f+";\n int q = d2 - d1 * "+f+";\n\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n // TODO(dsmilkov): Flatten the two for loops and vec4 the operations.\n for (int wR = 0; wR < "+l+"; wR++) {\n int xR = xRCorner + wR * "+s+";\n\n if (xR < 0 || xR >= "+t+") {\n continue;\n }\n\n for (int wC = 0; wC < "+c+"; wC++) {\n int xC = xCCorner + wC * "+u+";\n\n if (xC < 0 || xC >= "+n+") {\n continue;\n }\n\n float xVal = getX(batch, xR, xC, d1);\n float wVal = getW(wR, wC, d1, q);\n dotProd += xVal * wVal;\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.depthwiseConv2DDerInput=function(e,t,n){var r=new function(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,i=t-1-e.padInfo.top,o=n-1-e.padInfo.left,s=e.outChannels/e.inChannels;this.userCode="\n const ivec2 pads = ivec2("+i+", "+o+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords[3];\n ivec2 dyCorner = coords.yz - pads;\n int dyRCorner = dyCorner.x;\n int dyCCorner = dyCorner.y;\n\n float dotProd = 0.0;\n\n for (int wR = 0; wR < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+t+" - 1 - wR;\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = "+n+" - 1 - wC;\n\n // TODO: Vec4 over the channelMul\n for (int dm = 0; dm < "+s+"; dm++) {\n int d2 = d1 * "+s+" + dm;\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, dm);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.depthwiseConv2DDerFilter=function(e,t,n){var r=new function(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;var t=e.strideHeight,n=e.strideWidth,r=e.padInfo.top,a=e.padInfo.left,i=e.outChannels/e.inChannels;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int wR = coords.x;\n int wC = coords.y;\n int d1 = coords.z;\n int dm = coords.w;\n int d2 = d1 * "+i+" + dm;\n\n float dotProd = 0.0;\n\n // TODO: Vec4 over the batch size\n for (int b = 0; b < "+e.batchSize+"; b++) {\n for (int yR = 0; yR < "+e.outHeight+"; yR++) {\n int xR = wR + yR * "+t+" - "+r+";\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+e.outWidth+"; yC++) {\n int xC = wC + yC * "+n+" - "+a+";\n\n if (xC < 0 || xC >= "+e.inWidth+") {\n continue;\n }\n\n float dyValue = getDy(b, yR, yC, d2);\n float xValue = getX(b, xR, xC, d1);\n dotProd += (xValue * dyValue);\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.maxPool=function(e,t){var n=new lr(t,"max",!1),r=this.makeOutputArray(n.outputShape,e.dtype);return this.compileAndRun(n,[e],r)},e.prototype.avgPool=function(e,t){var n=new lr(t,"avg",!1),r=this.makeOutputArray(n.outputShape,"float32");return this.compileAndRun(n,[e],r)},e.prototype.maxPoolBackprop=function(e,t,n,r){var a=new lr(r,"max",!0),i=this.compileAndRun(a,[t]),o=new function(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,i=t-1-e.padInfo.top,o=n-1-e.padInfo.left,s=t*n-1;this.userCode="\n const ivec2 pads = ivec2("+i+", "+o+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n\n ivec2 dyRCCorner = coords.yz - pads;\n int dyRCorner = dyRCCorner.x;\n int dyCCorner = dyRCCorner.y;\n\n // Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n float dyValue = getDy(b, idyR, idyC, d);\n int maxPosValue = "+s+" - int(getMaxPos(b, idyR, idyC, d));\n\n // Get the current value, check it against the value from the\n // position matrix.\n int curPosValue = wR * "+n+" + wC;\n float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);\n\n dotProd += dyValue * mask;\n }\n }\n setOutput(dotProd);\n }\n "}(r),s=this.makeOutputArray(o.outputShape,t.dtype),u=this.compileAndRun(o,[e,i],s);return i.dispose(),u},e.prototype.avgPoolBackprop=function(e,t,n){var r=new function(e){this.variableNames=["dy"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,i=t-1-e.padInfo.top,o=n-1-e.padInfo.left,s=1/(t*n);this.userCode="\n const ivec2 pads = ivec2("+i+", "+o+");\n const float avgMultiplier = float("+s+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n\n ivec2 dyRCCorner = coords.yz - pads;\n int dyRCorner = dyRCCorner.x;\n int dyCCorner = dyRCCorner.y;\n\n // Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n float dyValue = getDy(b, idyR, idyC, d);\n\n dotProd += dyValue * avgMultiplier;\n }\n }\n setOutput(dotProd);\n }\n "}(n),a=this.makeOutputArray(r.outputShape,t.dtype);return this.compileAndRun(r,[e],a)},e.prototype.cast=function(e,t){return Ot(e,t,this)},e.prototype.reshape=function(e,t){return kt(e,t)},e.prototype.resizeBilinear=function(e,t,n,r){var a=new function(e,t,n,r){this.variableNames=["A"],this.outputShape=[];var a=e[0],i=e[1],o=e[2],s=e[3];this.outputShape=[a,t,n,s];var u=[r&&t>1?i-1:i,r&&n>1?o-1:o],l=[r&&t>1?t-1:t,r&&n>1?n-1:n];this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+u[0]/l[0]+",\n "+u[1]/l[1]+");\n const vec2 inputShapeRC = vec2("+i+".0, "+o+".0);\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n ivec2 yRC = coords.yz;\n\n // Fractional source index.\n vec2 sourceFracIndexRC = vec2(yRC) * effectiveInputOverOutputRatioRC;\n\n // Compute the four integer indices.\n ivec2 sourceFloorRC = ivec2(sourceFracIndexRC);\n ivec2 sourceCeilRC = ivec2(\n min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));\n\n float topLeft = getA(b, sourceFloorRC.x, sourceFloorRC.y, d);\n float bottomLeft = getA(b, sourceCeilRC.x, sourceFloorRC.y, d);\n float topRight = getA(b, sourceFloorRC.x, sourceCeilRC.y, d);\n float bottomRight = getA(b, sourceCeilRC.x, sourceCeilRC.y, d);\n\n vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);\n\n float top = topLeft + (topRight - topLeft) * fracRC.y;\n float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;\n float newValue = top + (bottom - top) * fracRC.x;\n\n setOutput(newValue);\n }\n "}(e.shape,t,n,r);return this.compileAndRun(a,[e])},e.prototype.resizeBilinearBackprop=function(e,t,n){var r=new function(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t.shape;var r=t.shape,a=r[1],i=r[2],o=e.shape,s=o[1],u=o[2],l=[n&&s>1?a-1:a,n&&u>1?i-1:i],c=[n&&s>1?s-1:s,n&&u>1?u-1:u],f=l[0]/c[0],p=l[1]/c[1],h=1/f,d=1/p,m=2*Math.ceil(h)+2,g=2*Math.ceil(d)+2;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n int r = coords[1];\n int c = coords[2];\n\n float accumulator = 0.0;\n\n const float heightScale = float("+f+");\n const float widthScale = float("+p+");\n\n const float invHeightScale = float("+h+");\n const float invWidthScale = float("+d+");\n\n const int winHeight = int("+m+");\n const int winWidth = int("+g+");\n\n // Compute bounds for where in dy we will look\n float startRLerp = floor(float(r) * invHeightScale);\n int startDyR = int(startRLerp - float(winHeight / 2));\n\n float startCLerp = floor(float(c) * invWidthScale);\n int startDyC = int(startCLerp - float(winWidth / 2));\n\n // Loop over dy\n for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {\n int dyR = dyROffset + startDyR;\n\n // Guard against the window exceeding the bounds of dy\n if (dyR < 0 || dyR >= "+s+") {\n continue;\n }\n\n for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {\n int dyC = dyCOffset + startDyC;\n\n // Guard against the window exceeding the bounds of dy\n if (dyC < 0 || dyC >= "+u+") {\n continue;\n }\n\n float dxR = float(dyR) * heightScale;\n int topDxRIndex = int(floor(dxR));\n int bottomDxRIndex = int(min(ceil(dxR), "+(a-1)+".0));\n float dxRLerp = dxR - float(topDxRIndex);\n float inverseDxRLerp = 1.0 - dxRLerp;\n\n float dxC = float(dyC) * widthScale;\n int leftDxCIndex = int(floor(dxC));\n int rightDxCIndex = int(min(ceil(dxC), "+(i-1)+".0));\n float dxCLerp = dxC - float(leftDxCIndex);\n float inverseDxCLerp = 1.0 - dxCLerp;\n\n if (r == topDxRIndex && c == leftDxCIndex) {\n // topLeft\n accumulator +=\n getDy(b, dyR, dyC, d) * inverseDxRLerp * inverseDxCLerp;\n }\n\n if (r == topDxRIndex && c == rightDxCIndex) {\n // topRight\n accumulator += getDy(b, dyR, dyC, d) * inverseDxRLerp * dxCLerp;\n }\n\n if (r == bottomDxRIndex && c == leftDxCIndex) {\n // bottomLeft\n accumulator += getDy(b, dyR, dyC, d) * dxRLerp * inverseDxCLerp;\n }\n\n if (r == bottomDxRIndex && c == rightDxCIndex) {\n // bottomRight\n accumulator += getDy(b, dyR, dyC, d) * dxRLerp * dxCLerp;\n }\n }\n }\n // End loop over dy\n\n setOutput(accumulator);\n }\n "}(e,t,n);return this.compileAndRun(r,[e])},e.prototype.resizeNearestNeighbor=function(e,t,n,r){var a=new function(e,t,n,r){this.variableNames=["A"],this.outputShape=[];var a=e[0],i=e[1],o=e[2],s=e[3];this.outputShape=[a,t,n,s];var u=[r&&t>1?i-1:i,r&&n>1?o-1:o],l=[r&&t>1?t-1:t,r&&n>1?n-1:n],c=r?"0.5":"0.0";this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+u[0]/l[0]+",\n "+u[1]/l[1]+");\n const vec2 inputShapeRC = vec2("+i+".0, "+o+".0);\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n ivec2 yRC = coords.yz;\n\n // Fractional source index.\n vec2 sourceFracIndexRC = vec2(yRC) * effectiveInputOverOutputRatioRC;\n\n // Compute the coordinators of nearest neighbor point.\n ivec2 sourceNearestRC = ivec2(\n min(inputShapeRC - 1.0, floor(sourceFracIndexRC + "+c+")));\n\n float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);\n\n setOutput(newValue);\n }\n "}(e.shape,t,n,r);return this.compileAndRun(a,[e])},e.prototype.resizeNearestNeighborBackprop=function(e,t,n){var r=new function(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t.shape;var r=t.shape,a=r[1],i=r[2],o=e.shape,s=o[1],u=o[2],l=[n&&s>1?a-1:a,n&&u>1?i-1:i],c=[n&&s>1?s-1:s,n&&u>1?u-1:u],f=l[0]/c[0],p=l[1]/c[1],h=1/f,d=1/p,m=2*Math.ceil(h)+2,g=2*Math.ceil(d)+2;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n int r = coords[1];\n int c = coords[2];\n\n float accumulator = 0.0;\n\n const float heightScale = float("+f+");\n const float widthScale = float("+p+");\n\n const float invHeightScale = float("+h+");\n const float invWidthScale = float("+d+");\n\n const int winHeight = int("+m+");\n const int winWidth = int("+g+");\n\n // Compute bounds for where in dy we will look\n float startRLerp = floor(float(r) * invHeightScale);\n int startDyR = int(floor(startRLerp - float(winHeight / 2)));\n\n float startCLerp = floor(float(c) * invWidthScale);\n int startDyC = int(floor(startCLerp - float(winWidth / 2)));\n\n // Loop over dy\n for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {\n int dyR = dyROffset + startDyR;\n\n // Guard against the window exceeding the bounds of dy\n if (dyR < 0 || dyR >= "+s+") {\n continue;\n }\n\n for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {\n int dyC = dyCOffset + startDyC;\n\n // Guard against the window exceeding the bounds of dy\n if (dyC < 0 || dyC >= "+u+") {\n continue;\n }\n\n float sourceFracRow =\n float("+l[0]+") *\n (float(dyR) / float("+c[0]+"));\n\n float sourceFracCol =\n float("+l[1]+") *\n (float(dyC) / float("+c[1]+"));\n\n int sourceNearestRow = int(min(\n float(int("+a+") - 1),\n "+n+" ? float(round(sourceFracRow)) :\n float(floor(sourceFracRow))));\n\n int sourceNearestCol = int(min(\n float(int("+i+") - 1),\n "+n+" ? float(round(sourceFracCol)) :\n float(floor(sourceFracCol))));\n\n if (r == sourceNearestRow && c == sourceNearestCol) {\n accumulator += getDy(b, dyR, dyC, d);\n }\n }\n }\n // End loop over dy\n\n setOutput(accumulator);\n }\n "}(e,t,n);return this.compileAndRun(r,[e])},e.prototype.multinomial=function(e,t,n,r){var a=t?e:Ze(e),i=a.shape[0],o=a.shape[1],s=new sr(i,o,n),u=this.makeOutputArray(s.outputShape,"int32"),l=s.getCustomSetupFunc(r);return this.compileAndRun(s,[a],u,l)},e.prototype.oneHot=function(e,t,n,r){var a=new function(e,t,n,r){this.variableNames=["indices"],this.outputShape=[e,t],this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int index = round(getIndices(coords.x));\n setOutput(mix(float("+r+"), float("+n+"),\n float(index == coords.y)));\n }\n "}(e.size,t,n,r);return this.compileAndRun(a,[e])},e.prototype.nonMaxSuppression=function(e,t,n,r,a){return Ce("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead"),Ct(e.dataSync(),t.dataSync(),n,r,a)},e.prototype.cropAndResize=function(e,t,n,r,a,i){var o=new function(e,t,n,r,a){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];var i=e[0],o=e[1],s=e[2],u=e[3],l=t[0],c=n[0],f=n[1];this.outputShape=[l,c,f,u];var p="bilinear"===r?1:0,h=[o-1+".0",s-1+".0"],d=h[0],m=h[1],g=c>1?[""+(o-1)/(c-1),"(y2-y1) * height_ratio","y1*"+d+" + float(y)*(height_scale)"]:["0.0","0.0","0.5 * (y1+y2) * "+d],y=g[0],v=g[1],b=g[2],w=f>1?[""+(s-1)/(f-1),"(x2-x1) * width_ratio","x1*"+m+" + float(x)*(width_scale)"]:["0.0","0.0","0.5 * (x1+x2) * "+m],x=w[0],_=w[1],S=w[2];this.userCode="\n const float height_ratio = float("+y+");\n const float width_ratio = float("+x+");\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int y = coords[1];\n int x = coords[2];\n int d = coords[3];\n\n // get box vals\n float y1 = getBoxes(b,0);\n float x1 = getBoxes(b,1);\n float y2 = getBoxes(b,2);\n float x2 = getBoxes(b,3);\n\n // get image in batch index\n int bInd = round(getBoxInd(b));\n if(bInd < 0 || bInd >= "+i+") {\n return;\n }\n\n float height_scale = "+v+";\n float width_scale = "+_+";\n\n float in_y = "+b+";\n if( in_y < 0.0 || in_y > "+d+" ) {\n setOutput(float("+a+"));\n return;\n }\n float in_x = "+S+";\n if( in_x < 0.0 || in_x > "+m+" ) {\n setOutput(float("+a+"));\n return;\n }\n\n vec2 sourceFracIndexRC = vec2(in_y,in_x);\n if("+p+" == 1) {\n // Compute the four integer indices.\n ivec2 sourceFloorRC = ivec2(sourceFracIndexRC);\n ivec2 sourceCeilRC = ivec2(ceil(sourceFracIndexRC));\n\n float topLeft = getImage(b, sourceFloorRC.x, sourceFloorRC.y, d);\n float bottomLeft = getImage(b, sourceCeilRC.x, sourceFloorRC.y, d);\n float topRight = getImage(b, sourceFloorRC.x, sourceCeilRC.y, d);\n float bottomRight = getImage(b, sourceCeilRC.x, sourceCeilRC.y, d);\n\n vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);\n\n float top = topLeft + (topRight - topLeft) * fracRC.y;\n float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;\n float newValue = top + (bottom - top) * fracRC.x;\n setOutput(newValue);\n } else {\n // Compute the coordinators of nearest neighbor point.\n ivec2 sourceNearestRC = ivec2(floor(\n sourceFracIndexRC + vec2(0.5,0.5)));\n float newValue = getImage(b, sourceNearestRC.x, sourceNearestRC.y, d);\n setOutput(newValue);\n }\n }\n "}(e.shape,t.shape,r,a,i);return this.compileAndRun(o,[e,t,n])},e.prototype.depthToSpace=function(e,t,n){d(t>1,"blockSize should be > 1 for depthToSpace, but was: "+t);var r=e.shape[0],a=("NHWC"===n?e.shape[1]:e.shape[2])*t,i=("NHWC"===n?e.shape[2]:e.shape[3])*t,o=("NHWC"===n?e.shape[3]:e.shape[1])/(t*t),s=new Yt("NHWC"===n?[r,a,i,o]:[r,o,a,i],t,n);return this.compileAndRun(s,[e])},e.prototype.split=function(e,t,n){return Pt(e,t,n)},e.prototype.makeOutputArray=function(e,t){return Q.make(e,{},t)},e.prototype.compileAndRun=function(e,t,n,r,a){var i=this;if(void 0===a&&(a=!0),null==n&&(n=this.makeOutputArray(e.outputShape,t[0].dtype)),0===n.size)return this.texData.get(n.dataId).values=I(n.dtype,0),n;var o=t.map(function(e){if("complex64"===e.dtype)throw new Error("GPGPUProgram does not support complex64 input. For complex64 dtypes, please separate the program into real and imaginary parts.");var t=i.texData.get(e.dataId);return null==t.texture&&b(e.shape)<=32?{shape:e.shape,texData:null,isUniform:!0,uniformValues:i.readSync(e.dataId)}:(i.uploadToGPU(e.dataId),{shape:e.shape,texData:t,isUniform:!1})});this.uploadToGPU(n.dataId);var s,u={shape:n.shape,texData:this.texData.get(n.dataId),isUniform:!1},l=function(e,t,n){var r="";o.concat(n).forEach(function(e){r+=e.shape+"_"+(e.isUniform?"uniform":e.texData.texShape)});var a=e.userCode,i=(!0===e.supportsBroadcasting).toString();return e.constructor.name+"_"+i+"_"+r+"_"+a}(e,0,u),c=this.getAndSaveBinary(l,function(){return function(e,t,n,r){for(var a=t.userCode,i=n.map(function(e,n){var r={logicalShape:e.shape,texShape:e.isUniform?null:e.texData.texShape,isUniform:e.isUniform,isPacked:!e.isUniform&&e.texData.usage===Kt.PACK};return{name:t.variableNames[n],shapeInfo:r}}),o=i.map(function(e){return e.shapeInfo}),s={logicalShape:r.shape,texShape:r.texData.texShape,isUniform:!1,isPacked:r.texData.usage===Kt.PACK},u=Ft(i,s,a,!0===t.supportsBroadcasting),l=e.createProgram(u),c={},f=0;fthis.NUM_BYTES_BEFORE_PAGING)for(var p=this.numBytesInGPU-this.NUM_BYTES_BEFORE_PAGING;p>0&&this.lruDataGPU.length>0;){var h=this.lruDataGPU.shift(),d=this.texData.get(h),m=d.shape,g=d.dtype;p-=this.computeBytes(m,g),this.read(h)}return f&&(s=this.endTimer(s),this.activeTimers.push(this.getQueryTime(s))),n},e.prototype.getAndSaveBinary=function(e,t){return e in this.binaryCache||(this.binaryCache[e]=t()),this.binaryCache[e]},e.prototype.getTextureManager=function(){return this.textureManager},e.prototype.dispose=function(){if(!this.disposed){for(var e in this.binaryCache)this.gpgpu.deleteProgram(this.binaryCache[e].webGLProgram);this.textureManager.dispose(),this.canvas.remove(),null!=this.fromPixelsCanvas&&this.fromPixelsCanvas.remove(),this.gpgpuCreatedLocally&&this.gpgpu.dispose(),this.disposed=!0}},e.prototype.floatPrecision=function(){var e=this;return Ee(function(){return e.abs(rt(1e-8)).get()>0?32:16})},e.prototype.throwIfNoData=function(e){if(!this.texData.has(e))throw new Error("WebGL backend: No data found for this tensor. Did you change your backend in the middle of the program? New backends can't use Tensors created with previous backends")},e.prototype.uploadToGPU=function(e){this.throwIfNoData(e);var t=this.texData.get(e),n=t.shape,r=t.values,a=t.texture,i=(t.dtype,t.usage);if(null==a){var o,s=null!=this.activeTimers;s&&(o=performance.now());var u=jn(this.gpgpu.gl,n);t.texShape=u;var l=this.acquireTexture(e,u,i);t.texture=l,null!=r&&(this.gpgpu.uploadMatrixToTexture(l,u[0],u[1],r instanceof Float32Array?r:new Float32Array(r)),t.values=null,s&&(this.uploadWaitMs+=performance.now()-o))}else this.lruDataGPU.indexOf(e)>=0&&(this.lruDataGPU.splice(this.lruDataGPU.indexOf(e),1),this.lruDataGPU.push(e))},e.prototype.cacheOnCPU=function(e,t){var n=this.delayedStorage,r=this.texData.get(e),a=r.texture,i=r.texShape,o=r.dtype,s=r.usage;n&&null!=a&&(this.releaseTexture(e,a,i,s),r.texture=null,r.texShape=null),r.usage=Kt.UPLOAD,null!=t&&(r.values=function(e,t){if("float32"===t||"complex64"===t)return e;if("int32"===t||"bool"===t){for(var n="int32"===t?new Int32Array(e.length):new Uint8Array(e.length),r=0;r=0&&this.lruDataGPU.splice(s,1),this.numBytesInGPU-=this.computeBytes(i,o),this.textureManager.releaseTexture(t,n,r)},e.prototype.acquireTexture=function(e,t,n){var r=this.texData.get(e),a=r.shape,i=r.dtype;return this.lruDataGPU.push(e),this.numBytesInGPU+=this.computeBytes(a,i),this.textureManager.acquireTexture(t,n)},e.prototype.computeBytes=function(e,t){return b(e)*M(t)},e}();me.get("IS_BROWSER")&&me.registerBackend("webgl",function(){return new ca},2,Z);var fa=Je({abs_:function(e){var t=Xe(e,"x","abs");return me.engine.runKernel(function(e){return e.abs(t)},{$x:t},function(e){return{$x:function(){return e.mulStrict(t.toFloat().step(-1))}}})}}),pa=Je({acos_:function(e){var t=Xe(e,"x","acos");return me.engine.runKernel(function(e){return e.acos(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(rt(1).sub(t.toFloat().square()).sqrt()).neg()}}})}}),ha=Je({acosh_:function(e){var t=Xe(e,"x","acosh");return me.engine.runKernel(function(e){return e.acosh(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.toFloat().square().sub(rt(1)).sqrt())}}})}}),da=Je({asin_:function(e){var t=Xe(e,"x","asin");return me.engine.runKernel(function(e){return e.asin(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(rt(1).sub(t.toFloat().square()).sqrt())}}})}}),ma=Je({asinh_:function(e){var t=Xe(e,"x","asinh");return me.engine.runKernel(function(e){return e.asinh(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(rt(1).add(t.toFloat().square()).sqrt())}}})}}),ga=Je({atan_:function(e){var t=Xe(e,"x","atan");return me.engine.runKernel(function(e){return e.atan(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(rt(1).add(t.toFloat().square()))}}})}}),ya=Je({atanh_:function(e){var t=Xe(e,"x","atanh");return me.engine.runKernel(function(e){return e.atanh(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(rt(1).sub(t.toFloat().square()))}}})}}),va=Je({ceil_:function(e){var t=Xe(e,"x","ceil");return me.engine.runKernel(function(e){return e.ceil(t)},{$x:t},function(e){return{$x:function(){return _t(e)}}})}}),ba=Je({clipByValue_:function(e,t,n){var r=Xe(e,"x","clipByValue");return d(t<=n,"Error in clip: min ("+t+") must be less than or equal to max ("+n+")."),me.engine.runKernel(function(e){return e.clip(r,t,n)},{$x:r},function(e){return{$x:function(){return e.where(r.greaterEqual(rt(t)).logicalAnd(r.lessEqual(rt(n))),_t(e))}}})}}),wa=Je({cos_:function(e){var t=Xe(e,"x","cos");return me.engine.runKernel(function(e){return e.cos(t)},{$x:t},function(e){return{$x:function(){return t.toFloat().sin().neg().mulStrict(e)}}})}}),xa=Je({cosh_:function(e){var t=Xe(e,"x","cosh");return me.engine.runKernel(function(e){return e.cosh(t)},{$x:t},function(e){return{$x:function(){return t.toFloat().sinh().mulStrict(e)}}})}}),_a=Je({erf_:function(e){var t=Xe(e,"x","erf");return d("int32"===t.dtype||"float32"===t.dtype,"Input dtype must be `int32` or `float32`."),"int32"===t.dtype&&(t=t.toFloat()),me.engine.runKernel(function(e){return e.erf(t)},{$x:t},function(e){return{$x:function(){return e.mulStrict(rt(2/Math.sqrt(Math.PI)).mul(t.square().neg().exp()))}}})}}),Sa=Je({exp_:function(e){var t=Xe(e,"x","exp");return me.engine.runKernel(function(e,n){return n(e.exp(t))},{$x:t},function(e,t){var n=t[0];return{$x:function(){return e.mulStrict(n)}}})}}),Ea=Je({expm1_:function(e){var t=Xe(e,"x","expm1");return me.engine.runKernel(function(e){return e.expm1(t)},{$x:t},function(e){return{$x:function(){return e.mulStrict(t.exp())}}})}}),Na=Je({floor_:function(e){var t=Xe(e,"x","floor");return me.engine.runKernel(function(e){return e.floor(t)},{$x:t},function(e){return{$x:function(){return _t(e)}}})}}),Oa=Je({log_:function(e){var t=Xe(e,"x","log");return me.engine.runKernel(function(e){return e.log(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.toFloat())}}})}}),ka=Je({log1p_:function(e){var t=Xe(e,"x","log1p");return me.engine.runKernel(function(e){return e.log1p(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.add(rt(1)))}}})}}),Ia=Je({logSigmoid_:function(e){var t=Xe(e,"x","logSigmoid");return me.engine.runKernel(function(e){return e.softplus(t.neg()).neg()},{$x:t},function(e){return{$x:function(){return e.mulStrict(t.neg().sigmoid())}}})}}),Ca=Je({neg_:function(e){var t=Xe(e,"x","neg");return me.engine.runKernel(function(e){return e.neg(t)},{$x:t},function(e){return{$x:function(){return e.neg()}}})}}),Aa=Je({reciprocal_:function(e){var t=Xe(e,"x","reciprocal");return me.engine.runKernel(function(e){return e.reciprocal(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.square().neg())}}})}}),Pa=Je({round_:function(e){var t=Xe(e,"x","round");return me.engine.runKernel(function(e){return e.round(t)},{$x:t},function(e){return{$x:function(){return _t(e)}}})}}),Ta=Je({rsqrt_:function(e){var t=Xe(e,"x","rsqrt");return me.engine.runKernel(function(e){return e.rsqrt(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.pow(rt(1.5)).mul(rt(2))).neg()}}})}}),Ma=Je({sigmoid_:function(e){var t=Xe(e,"x","sigmoid");return me.engine.runKernel(function(e,n){return n(e.sigmoid(t))},{$x:t},function(e,t){var n=t[0];return{$x:function(){return e.mulStrict(n.mul(rt(1).sub(n)))}}})}}),Ra=Je({sign_:function(e){var t=Xe(e,"x","sign");return me.engine.runKernel(function(e){return e.sign(t)},{$x:t},function(e){return{$x:function(){return _t(e)}}})}}),Da=Je({sin_:function(e){var t=Xe(e,"x","sin");return me.engine.runKernel(function(e){return e.sin(t)},{$x:t},function(e){return{$x:function(){return t.toFloat().cos().mulStrict(e)}}})}}),ja=Je({sinh_:function(e){var t=Xe(e,"x","sinh");return me.engine.runKernel(function(e){return e.sinh(t)},{$x:t},function(e){return{$x:function(){return t.toFloat().cosh().mulStrict(e)}}})}}),La=Je({softplus_:function(e){var t=Xe(e,"x","softplus");return me.engine.runKernel(function(e){return e.softplus(t)},{$x:t},function(e){return{$x:function(){return e.mulStrict(t.sigmoid())}}})}}),za=Je({sqrt_:function(e){var t=Xe(e,"x","sqrt");return me.engine.runKernel(function(e){return e.sqrt(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.toFloat().sqrt().mul(rt(2)))}}})}}),Ba=Je({square_:function(e){var t=Xe(e,"x","square");return me.engine.runKernel(function(e){return e.square(t)},{$x:t},function(e){return{$x:function(){return e.mulStrict(t.toFloat().mul(rt(2)))}}})}}),Fa=Je({step_:function(e,t){void 0===t&&(t=0);var n=Xe(e,"x","step");return me.engine.runKernel(function(e){return e.step(n,t)},{$x:n},function(e){return{$x:function(){return _t(e)}}})}}),Va=Je({tan_:function(e){var t=Xe(e,"x","tan");return me.engine.runKernel(function(e){return e.tan(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.cos().square())}}})}}),Ua=Je({tanh_:function(e){var t=Xe(e,"x","tanh");return me.engine.runKernel(function(e,n){return n(e.tanh(t))},{$x:t},function(e,t){var n=t[0];return{$x:function(){return rt(1).sub(n.square()).mulStrict(e)}}})}});function Wa(e){return null==e?null:0===e.rank?e.as1D():1===e.rank?e:2===e.rank?e.as4D(1,1,e.shape[0],e.shape[1]):3===e.rank?e.as4D(1,e.shape[0],e.shape[1],e.shape[2]):e}var qa=Je({batchNormalization2d_:function(e,t,n,r,a,i){void 0===r&&(r=.001);var o,s,u=Xe(e,"x","batchNormalization"),l=Xe(t,"mean","batchNormalization"),c=Xe(n,"variance","batchNormalization");return null!=a&&(o=Xe(a,"scale","batchNormalization")),null!=i&&(s=Xe(i,"offset","batchNormalization")),d(2===u.rank,"Error in batchNormalization3D: x must be rank 3 but got rank "+u.rank+"."),d(2===l.rank||1===l.rank,"Error in batchNormalization2D: mean must be rank 2 or rank 1 but got rank "+l.rank+"."),d(2===c.rank||1===c.rank,"Error in batchNormalization2D: variance must be rank 2 or rank 1 but got rank "+c.rank+"."),null!=o&&d(2===o.rank||1===o.rank,"Error in batchNormalization2D: scale must be rank 2 or rank 1 but got rank "+o.rank+"."),null!=s&&d(2===s.rank||1===s.rank,"Error in batchNormalization2D: offset must be rank 2 or rank 1 but got rank "+s.rank+"."),Ha(u,l,c,r,o,s)}}),$a=Je({batchNormalization3d_:function(e,t,n,r,a,i){void 0===r&&(r=.001);var o,s,u=Xe(e,"x","batchNormalization"),l=Xe(t,"mean","batchNormalization"),c=Xe(n,"variance","batchNormalization");return null!=a&&(o=Xe(a,"scale","batchNormalization")),null!=i&&(s=Xe(i,"offset","batchNormalization")),d(3===u.rank,"Error in batchNormalization3D: x must be rank 3 but got rank "+u.rank+"."),d(3===l.rank||1===l.rank,"Error in batchNormalization3D: mean must be rank 3 or rank 1 but got rank "+l.rank+"."),d(3===c.rank||1===c.rank,"Error in batchNormalization3D: variance must be rank 3 or rank 1 but got rank "+c.rank+"."),null!=o&&d(3===o.rank||1===o.rank,"Error in batchNormalization3D: scale must be rank 3 or rank 1 but got rank "+o.rank+"."),null!=s&&d(3===s.rank||1===s.rank,"Error in batchNormalization3D: offset must be rank 3 or rank 1 but got rank "+s.rank+"."),Ha(u,l,c,r,o,s)}}),Ga=Je({batchNormalization4d_:function(e,t,n,r,a,i){void 0===r&&(r=.001);var o,s,u=Xe(e,"x","batchNormalization"),l=Xe(t,"mean","batchNormalization"),c=Xe(n,"variance","batchNormalization");return null!=a&&(o=Xe(a,"scale","batchNormalization")),null!=i&&(s=Xe(i,"offset","batchNormalization")),d(4===u.rank,"Error in batchNormalization4D: x must be rank 4 but got rank "+u.rank+"."),d(4===l.rank||1===l.rank,"Error in batchNormalization4D: mean must be rank 4 or rank 1 but got rank "+l.rank+"."),d(4===c.rank||1===c.rank,"Error in batchNormalization4D: variance must be rank 4 or rank 1 but got rank "+c.rank+"."),null!=o&&d(4===o.rank||1===o.rank,"Error in batchNormalization4D: scale must be rank 4 or rank 1 but got rank "+o.rank+"."),null!=s&&d(4===s.rank||1===s.rank,"Error in batchNormalization4D: offset must be rank 4 or rank 1 but got rank "+s.rank+"."),Ha(u,l,c,r,o,s)}}),Ha=Je({batchNormalization_:function(e,t,n,r,a,i){void 0===r&&(r=.001);var o,s,u,l=Xe(e,"x","batchNormalization"),c=Xe(t,"mean","batchNormalization"),f=Xe(n,"variance","batchNormalization");return null!=a&&(o=Xe(a,"scale","batchNormalization")),null!=i&&(s=Xe(i,"offset","batchNormalization")),d(c.rank===f.rank,"Batch normalization gradient requires mean and variance to have equal ranks."),d(null==s||c.rank===s.rank,"Batch normalization gradient requires mean and offset to have equal ranks."),d(null==o||c.rank===o.rank,"Batch normalization gradient requires mean and scale to have equal ranks."),u=0===l.rank||1===l.rank?l.as4D(1,1,1,l.size):2===l.rank?l.as4D(1,1,l.shape[0],l.shape[1]):3===l.rank?l.as4D(1,l.shape[0],l.shape[1],l.shape[2]):l,me.engine.runKernel(function(e){return e.batchNormalization(u,Wa(c),Wa(f),r,Wa(o),Wa(s))},{$x:l,$mean:c,$variance:f,$scale:o,$offset:s},function(e){var t=null==o?rt(1):o,n=Rt(c.shape,u.shape),a=[];if(1===c.rank){for(var i=0;i=2&&i.rank>=2&&a.rank===i.rank,"Error in matMul: inputs must have the same rank of at least 2, got ranks "+a.rank+" and "+i.rank+"."),d(w(c,f),"Error in matMul: outer dimensions ("+c+") and ("+f+") of Tensors with shapes "+a.shape+" and "+i.shape+" must match."),d(o===s,"Error in matMul: inner shapes ("+o+") and ("+s+") of Tensors with shapes "+a.shape+" and "+i.shape+" and transposeA="+n+" and transposeB="+r+" must match.");var m=a.shape.slice(0,-2).concat([u,l]),g=n?a.as3D(p,o,u):a.as3D(p,u,o),y=r?i.as3D(h,l,s):i.as3D(h,s,l);return me.engine.runKernel(function(e){return e.batchMatMul(g,y,n,r)},{$a:g,$b:y},function(e){return n||r?!n&&r?{$a:function(){return e.matMul(y.toFloat(),!1,!1)},$b:function(){return e.matMul(g.toFloat(),!0,!1)}}:n&&!r?{$a:function(){return y.toFloat().matMul(e,!1,!0)},$b:function(){return g.toFloat().matMul(e,!1,!1)}}:{$a:function(){return y.toFloat().matMul(e,!0,!0)},$b:function(){return e.matMul(g.toFloat(),!0,!0)}}:{$a:function(){return e.matMul(y.toFloat(),!1,!0)},$b:function(){return g.toFloat().matMul(e,!0,!1)}}}).reshape(m)}}),ei=Je({dot_:function(e,t){var n=Xe(e,"t1","dot"),r=Xe(t,"t2","dot");d(!(1!==n.rank&&2!==n.rank||1!==r.rank&&2!==r.rank),"Error in dot: inputs must all be rank 1 or 2, but got ranks "+n.rank+" and "+r.rank+".");var a=1===n.rank?n.size:n.shape[1],i=1===r.rank?r.size:r.shape[0];return d(a===i,"Error in dot: inner dimensions of inputs must match, but got "+a+" and "+i+"."),1===n.rank&&1===r.rank?n.as2D(1,-1).matMul(r.as2D(-1,1)).asScalar():1===n.rank&&2===r.rank?n.as2D(1,-1).matMul(r.as2D(r.shape[0],r.shape[1])).as1D():2===n.rank&&1===r.rank?n.matMul(r.as2D(-1,1)).as1D():n.matMul(r.as2D(r.shape[0],r.shape[1]))}}),ti=Je({outerProduct_:function(e,t){var n=Xe(e,"v1","outerProduct"),r=Xe(t,"v2","outerProduct");return d(1===n.rank&&1===r.rank,"Error in outerProduct: inputs must be rank 1, but got ranks "+n.rank+" and "+r.rank+"."),n.as2D(-1,1).matMul(r.as2D(1,-1))}});function ni(e,t,n,r,a,i){d(e.length===t.rank,"Length of inShape ("+e.length+") and rank of dy ("+t.rank+") must match");var o=e,s=t,u=!1;3===t.rank&&(u=!0,s=t.as4D(1,t.shape[0],t.shape[1],t.shape[2]),o=[1,e[0],e[1],e[2]]);var l=o[3],c=s.shape[3];d(4===o.length,"Error in conv2dDerInput: inShape must be length 4, but got length "+o.length+"."),d(4===s.rank,"Error in conv2dDerInput: dy must be rank 4, but got rank "+s.rank),d(4===n.rank,"Error in conv2dDerInput: filter must be rank 4, but got rank "+n.rank),d(l===n.shape[2],"Error in conv2dDerInput: depth of input ("+l+") must match input depth for filter "+n.shape[2]+"."),d(c===n.shape[3],"Error in conv2dDerInput: depth of output ("+c+") must match output depth for filter "+n.shape[3]+"."),null!=i&&d(x(a),"Error in conv2dDerInput: pad must be an integer when using, dimRoundingMode "+i+" but got pad "+a+".");var f=Xa(o,n.shape,r,1,a,i),p=me.engine.runKernel(function(e){return e.conv2dDerInput(s,n,f)},{dy4D:s});return u?p.as3D(p.shape[1],p.shape[2],p.shape[3]):p}function ri(e){var t=function(e){return"number"==typeof e?[e,e]:e}(e),n=t[0],r=t[1];return 1===n&&1===r}function ai(e,t){return ri(e)||ri(t)}var ii=Je({conv1d_:function(e,t,n,r,a,i,o){void 0===a&&(a="NWC"),void 0===i&&(i=1);var s=Xe(e,"x","conv1d"),u=Xe(t,"filter","conv1d"),l=s,c=!1;2===s.rank&&(c=!0,l=s.as3D(1,s.shape[0],s.shape[1])),d(3===l.rank,"Error in conv1d: input must be rank 3, but got rank "+l.rank+"."),d(3===u.rank,"Error in conv1d: filter must be rank 3, but got rank "+u.rank+"."),null!=o&&d(x(r),"Error in conv1d: pad must be an integer when using, dimRoundingMode "+o+" but got pad "+r+"."),d(l.shape[2]===u.shape[1],"Error in conv1d: depth of input ("+l.shape[2]+") must match input depth for filter "+u.shape[1]+"."),d(ai(n,i),"Error in conv1D: Either stride or dilation must be 1. Got stride "+n+" and dilation '"+i+"'"),d("NWC"===a,"Error in conv1d: got dataFormat of "+a+" but only NWC is currently supported.");var f=u.as4D(1,u.shape[0],u.shape[1],u.shape[2]),p=l.as4D(l.shape[0],1,l.shape[1],l.shape[2]),h=oi(p,f,[1,n],r,"NHWC",[1,i],o);return c?h.as2D(h.shape[2],h.shape[3]):h.as3D(h.shape[0],h.shape[2],h.shape[3])}}),oi=Je({conv2d_:function(e,t,n,r,a,i,o){void 0===a&&(a="NHWC"),void 0===i&&(i=[1,1]);var s=Xe(e,"x","conv2d"),u=Xe(t,"filter","conv2d"),l=s,c=!1;3===s.rank&&(c=!0,l=s.as4D(1,s.shape[0],s.shape[1],s.shape[2])),d(4===l.rank,"Error in conv2d: input must be rank 4, but got rank "+l.rank+"."),d(4===u.rank,"Error in conv2d: filter must be rank 4, but got rank "+u.rank+"."),null!=o&&d(x(r),"Error in conv2d: pad must be an integer when using, dimRoundingMode "+o+" but got pad "+r+"."),d(l.shape[3]===u.shape[2],"Error in conv2d: depth of input ("+l.shape[3]+") must match input depth for filter "+u.shape[2]+"."),d(ai(n,i),"Error in conv2D: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+i+"'"),d("NHWC"===a,"Error in conv2d: got dataFormat of "+a+" but only NHWC is currently supported.");var f,p=Xa(l.shape,u.shape,n,i,r,o);if(1===p.filterHeight&&1===p.filterWidth&&1===p.dilationHeight&&1===p.dilationWidth&&1===p.strideHeight&&1===p.strideWidth&&"SAME"===p.padInfo.type){var h=l.reshape([-1,p.inChannels]),m=u.reshape([p.inChannels,p.outChannels]);f=Qa(h,m).reshape(p.outShape)}else f=me.engine.runKernel(function(e){return e.conv2d(l,u,p)},{x:l,$filter:u},function(e){return d(ri(i),"Error in gradient of conv2D: dilation rates greater than 1 are notyet supported in gradients. Got dilations '"+i+"'"),{x:function(){return ni(l.shape,e,u,n,r)},$filter:function(){return function(e,t,n,r,a,i){var o=e;3===e.rank&&(o=e.as4D(1,e.shape[0],e.shape[1],e.shape[2]));var s=t;3===s.rank&&(s=t.as4D(1,t.shape[0],t.shape[1],t.shape[2])),d(4===o.rank,"Error in conv2dDerFilter: input must be rank 4, but got shape "+o.shape+"."),d(4===s.rank,"Error in conv2dDerFilter: dy must be rank 4, but got shape "+s.shape+"."),d(4===n.length,"Error in conv2dDerFilter: filterShape must be length 4, but got "+n+"."),d(o.shape[3]===n[2],"Error in conv2dDerFilter: depth of input "+o.shape[3]+") must match input depth in filter ("+n[2]+"."),d(s.shape[3]===n[3],"Error in conv2dDerFilter: depth of dy ("+s.shape[3]+") must match output depth for filter ("+n[3]+").");var u=Xa(o.shape,n,r,1,a,i);return me.engine.runKernel(function(e){return e.conv2dDerFilter(o,s,u)},{x4D:o,dy4D:s})}(l,e,u.shape,n,r)}}});return c?f.as3D(f.shape[1],f.shape[2],f.shape[3]):f}}),si=Je({depthwiseConv2d_:function(e,t,n,r,a,i,o){void 0===a&&(a="NHWC"),void 0===i&&(i=[1,1]);var s=Xe(e,"x","depthwiseConv2d"),u=Xe(t,"filter","depthwiseConv2d"),l=s,c=!1;3===s.rank&&(c=!0,l=s.as4D(1,s.shape[0],s.shape[1],s.shape[2])),d(4===l.rank,"Error in depthwiseConv2d: input must be rank 4, but got rank "+l.rank+"."),d(4===u.rank,"Error in depthwiseConv2d: filter must be rank 4, but got rank "+u.rank+"."),d(l.shape[3]===u.shape[2],"Error in depthwiseConv2d: number of input channels ("+l.shape[3]+") must match the inChannels dimension in filter "+u.shape[2]+"."),null==i&&(i=[1,1]),d(ai(n,i),"Error in depthwiseConv2d: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+i+"'"),null!=o&&d(x(r),"Error in depthwiseConv2d: pad must be an integer when using, dimRoundingMode "+o+" but got pad "+r+".");var f=Xa(l.shape,u.shape,n,i,r,o,!0),p=me.engine.runKernel(function(e){return e.depthwiseConv2D(l,u,f)},{x:l,$filter:u},function(e){return d(ri(i),"Error in gradient of depthwiseConv2d: dilation rates greater than 1 are not yet supported. Got dilations '"+i+"'"),{x:function(){return function(e,t,n,r){var a=t,i=!1;3===t.rank&&(i=!0,a=t.as4D(1,t.shape[0],t.shape[1],t.shape[2]));var o=me.engine.runKernel(function(e){return e.depthwiseConv2DDerInput(a,n,r)},{dy4D:a});return i?o.as3D(o.shape[1],o.shape[2],o.shape[3]):o}(l.shape,e,u,f)},$filter:function(){return function(e,t,n,r){var a=e;3===e.rank&&(a=e.as4D(1,e.shape[0],e.shape[1],e.shape[2]));var i=t;return 3===i.rank&&(i=t.as4D(1,t.shape[0],t.shape[1],t.shape[2])),me.engine.runKernel(function(e){return e.depthwiseConv2DDerFilter(a,i,r)},{x4D:a,dy4D:i})}(l,e,u.shape,f)}}});return c?p.as3D(p.shape[1],p.shape[2],p.shape[3]):p}}),ui=Je({separableConv2d_:function(e,t,n,r,a,i,o){void 0===i&&(i=[1,1]),void 0===o&&(o="NHWC");var s=Xe(e,"x","separableConv2d"),u=Xe(t,"depthwiseFilter","separableConv2d"),l=Xe(n,"pointwiseFilter","separableConv2d"),c=s,f=!1;if(3===s.rank&&(f=!0,c=s.as4D(1,s.shape[0],s.shape[1],s.shape[2])),"NCHW"===o)throw new Error("separableConv2d currently does not support dataFormat NCHW; only NHWC is supported");d(4===c.rank,"Error in separableConv2d: input must be rank 4, but got rank "+c.rank+"."),d(4===u.rank,"Error in separableConv2d: depthwise filter must be rank 4, but got rank "+u.rank+"."),d(4===l.rank,"Error in separableConv2d: pointwise filter must be rank 4, but got rank "+u.rank+"."),d(1===l.shape[0],"Error in separableConv2d: the first dimension of pointwise filter must be 1, but got "+l.shape[0]+"."),d(1===l.shape[1],"Error in separableConv2d: the second dimension of pointwise filter must be 1, but got "+l.shape[1]+".");var p=u.shape[2],h=u.shape[3];d(l.shape[2]===p*h,"Error in separableConv2d: the third dimension of pointwise filter must be "+p*h+", but got "+l.shape[2]+".");var m=si(c,u,r,a,o,i),g=oi(m,l,1,"valid",o);return f?g.as3D(g.shape[1],g.shape[2],g.shape[3]):g}}),li=Je({conv2dTranspose_:function(e,t,n,r,a,i){return ni(n,Xe(e,"x","conv2dTranspose"),Xe(t,"filter","conv2dTranspose"),r,a,i)}}),ci=Je({reverse_:function(e,t){var n=Xe(e,"x","reverse");if(0===n.rank)return n.clone();var r=ze(t,n.shape);return me.engine.runKernel(function(e){return e.reverse(n,r)},{$x:n},function(e){return{$x:function(){return e.reverse(r)}}}).reshapeAs(n)}}),fi=Je({reverse1d_:function(e){var t=Xe(e,"x","reverse");return d(1===t.rank,"Error in reverse1D: x must be rank 1 but got\n rank "+t.rank+"."),ci(t,0)}}),pi=Je({reverse2d_:function(e,t){var n=Xe(e,"x","reverse");return d(2===n.rank,"Error in reverse2D: x must be rank 2 but got\n rank "+n.rank+"."),ci(n,t)}}),hi=Je({reverse3d_:function(e,t){var n=Xe(e,"x","reverse");return d(3===n.rank,"Error in reverse3D: x must be rank 3 but got\n rank "+n.rank+"."),ci(n,t)}}),di=Je({reverse4d_:function(e,t){var n=Xe(e,"x","reverse");return d(4===n.rank,"Error in reverse4D: x must be rank 4 but got\n rank "+n.rank+"."),ci(n,t)}}),mi=Je({maxPool_:function(e,t,n,r,a){var i=Xe(e,"x","maxPool"),o=i,s=!1;3===i.rank&&(s=!0,o=i.as4D(1,i.shape[0],i.shape[1],i.shape[2])),d(4===o.rank,"Error in maxPool: input must be rank 4 but got rank "+o.rank+"."),null!=a&&d(x(r),"Error in maxPool: pad must be an integer when using, dimRoundingMode "+a+" but got pad "+r+".");var u=Ka(o.shape,t,n,r,a),l=me.engine.runKernel(function(e,t){return t(e.maxPool(o,u))},{x:o},function(e,a){var i=a[0];return{x:function(){return function(e,t,n,r,a,i,o){var s=Xe(e,"dy","maxPoolBackprop"),u=Xe(t,"input","maxPoolBackprop"),l=Xe(n,"output","maxPoolBackprop");d(u.rank===s.rank,"Rank of input ("+u.rank+") does not match rank of dy ("+s.rank+")"),d(4===s.rank,"Error in maxPoolBackprop: dy must be rank 4 but got rank "+s.rank+"."),d(4===u.rank,"Error in maxPoolBackprop: input must be rank 4 but got rank "+u.rank+".");var c=Ka(u.shape,r,a,i,o);return me.engine.runKernel(function(e){return e.maxPoolBackprop(s,u,l,c)},{$dy:s,$input:u})}(e,o,i,t,n,r)}}});return s?l.as3D(l.shape[1],l.shape[2],l.shape[3]):l}}),gi=Je({avgPool_:function(e,t,n,r,a){var i=Xe(e,"x","avgPool");d("float32"===i.dtype,"The input dtype to avgPool must be float32");var o=i,s=!1;3===i.rank&&(s=!0,o=i.as4D(1,i.shape[0],i.shape[1],i.shape[2])),d(4===o.rank,"Error in avgPool: x must be rank 4 but got rank "+o.rank+"."),null!=a&&d(x(r),"Error in avgPool: pad must be an integer when using, dimRoundingMode "+a+" but got pad "+r+".");var u=Ka(o.shape,t,n,r),l=me.engine.runKernel(function(e){return e.avgPool(o,u)},{x:o},function(e){return{x:function(){return function(e,t,n,r,a){var i=Xe(e,"dy","avgPoolBackprop"),o=Xe(t,"input","avgPoolBackprop");d(o.rank===i.rank,"Rank of input ("+o.rank+") does not match rank of dy ("+i.rank+")");var s=o,u=i,l=!1;3===o.rank&&(l=!0,s=o.as4D(1,o.shape[0],o.shape[1],o.shape[2]),u=i.as4D(1,i.shape[0],i.shape[1],i.shape[2])),d(4===u.rank,"Error in avgPoolBackprop: dy must be rank 4 but got rank "+u.rank+"."),d(4===s.rank,"Error in avgPoolBackprop: input must be rank 4 but got rank "+s.rank+".");var c=Ka(s.shape,n,r,a),f=me.engine.runKernel(function(e){return e.avgPoolBackprop(u,s,c)},{dy4D:u,input4D:s});return l?f.as3D(f.shape[1],f.shape[2],f.shape[3]):f}(e,o,t,n,r)}}});return l=l.cast(i.dtype),s?l.as3D(l.shape[1],l.shape[2],l.shape[3]):l}}),yi=Je({slice_:function(e,t,n){var r,a,i=Xe(e,"x","slice");if(0===i.rank)throw new Error("Slicing scalar is not possible");r="number"==typeof t?[t].concat(new Array(i.rank-1).fill(0)):t.length=0?e:(d(-1===e,"Bad value in size"),i.shape[t]-r[t])}),function(e,t,n){d(e.rank===t.length,"Error in slice"+e.rank+"D: Length of begin "+t+" must match the rank of the array ("+e.rank+")."),d(e.rank===n.length,"Error in slice"+e.rank+"D: Length of size "+n+" must match the rank of the array ("+e.rank+").");for(var r=0;r0&&(t=t.sum(r)),t.reshape(n.shape)},$b:function(){var t=e,n=Rt(r.shape,a);return n.length>0&&(t=t.sum(n)),t.reshape(r.shape)}}})}}),Gi=Je({addN_:function(e){d(Array.isArray(e),function(){return"The argument passed to tf.addN() must be a list of tensors"}),d(e.length>=1,function(){return"Must pass at least one tensor to tf.addN(), but got "+e.length});var t=e.map(function(e,t){return Xe(e,"tensors"+t,"addN")}),n=t[0];t.forEach(function(e){if(e.dtype!==n.dtype)throw new Error("All tensors passed to tf.addN() must have the same dtype")}),t.forEach(function(e){if(!w(e.shape,n.shape))throw new Error("All tensors passed to tf.addN() must have the same shape")});var r=t;return me.engine.runKernel(function(e){return e.addN(t)},r,function(e){var n={};return t.forEach(function(t,r){n[r]=function(){return e.clone()}}),n})}}),Hi=Je({addStrict_:function(e,t){return m(e.shape,t.shape,"Error in addStrict: "),e.add(t)}}),Ki=Je({atan2_:function(e,t){var n=Xe(e,"a","atan2"),r=Xe(t,"b","atan2");ne(n,r);var a=Dt(n.shape,r.shape);return me.engine.runKernel(function(e){return e.atan2(n,r)},{$a:n,$b:r},function(e){return{$a:function(){var t=$i(n.square(),r.square()),i=e.mul(r.div(t)),o=Rt(n.shape,a);return o.length>0&&(i=i.sum(o)),i.reshape(n.shape)},$b:function(){var t=$i(n.square(),r.square()),i=Ca(e.mul(n.div(t))),o=Rt(r.shape,a);return o.length>0&&(i=i.sum(o)),i.reshape(r.shape)}}})}}),Xi=Je({div_:function(e,t){var n,r=Xe(e,"a","div"),a=Xe(t,"b","div");if(ne(r,a),"int32"===r.dtype&&"int32"===a.dtype)return Ji(r,a);n=function(e){return e.realDivide(r,a)};var i=Dt(r.shape,a.shape);return me.engine.runKernel(n,{$a:r,$b:a},function(e){return{$a:function(){var t=e.div(a.toFloat()),n=Rt(r.shape,i);return n.length>0?t.sum(n).reshape(r.shape):t},$b:function(){var t=e.mul(r.toFloat()),n=Rt(a.shape,i);n.length>0&&(t=t.sum(n).reshape(a.shape));var o=a.square();return t.div(o.toFloat()).neg()}}})}}),Yi=Je({divStrict_:function(e,t){return m(e.shape,t.shape,"Error in divideStrict: "),e.div(t)}}),Ji=Je({floorDiv_:function(e,t){var n=Xe(e,"a","floorDiv"),r=Xe(t,"b","floorDiv");ne(n,r);var a=Dt(n.shape,r.shape);return me.engine.runKernel(function(e){return e.floorDiv(n,r)},{$a:n,$b:r},function(e){return{$a:function(){var t=e.div(r.toFloat()),i=Rt(n.shape,a);return i.length>0?t.sum(i).reshape(n.shape):t},$b:function(){var t=e.mul(n.toFloat()),i=Rt(r.shape,a);i.length>0&&(t=t.sum(i).reshape(r.shape));var o=r.square();return t.div(o.toFloat()).neg()}}})}}),Zi=Je({maximum_:function(e,t){var n=Xe(e,"a","maximum"),r=Xe(t,"b","maximum");return ne(n,r),"bool"===n.dtype&&(n=n.toInt()),"bool"===r.dtype&&(r=r.toInt()),Dt(n.shape,r.shape),me.engine.runKernel(function(e){return e.maximum(n,r)},{$a:n,$b:r},function(e){return{$a:function(){return e.mul(n.greaterEqual(r).toFloat())},$b:function(){return e.mul(n.less(r).toFloat())}}})}}),Qi=Je({maximumStrict_:function(e,t){return m(e.shape,t.shape,"Error in maximumStrict: "),e.maximum(t)}}),eo=Je({minimum_:function(e,t){var n=Xe(e,"a","minimum"),r=Xe(t,"b","minimum");return ne(n,r),"bool"===n.dtype&&(n=n.toInt()),"bool"===r.dtype&&(r=r.toInt()),Dt(n.shape,r.shape),me.engine.runKernel(function(e){return e.minimum(n,r)},{$a:n,$b:r},function(e){return{$a:function(){return e.mul(n.lessEqual(r).toFloat())},$b:function(){return e.mul(n.greater(r).toFloat())}}})}}),to=Je({minimumStrict_:function(e,t){return m(e.shape,t.shape,"Error in minimumStrict: "),e.minimum(t)}}),no=Je({mod_:function(e,t){var n=Xe(e,"a","mod"),r=Xe(t,"b","mod");ne(n,r);var a=Dt(n.shape,r.shape);return me.engine.runKernel(function(e){return e.mod(n,r)},{$a:n,$b:r},function(e){return{$a:function(){var t=Rt(n.shape,a);return t.length>0?e.sum(t).reshape(n.shape):e},$b:function(){var t=e.mul(n.div(r).floor().neg()),i=Rt(r.shape,a);return i.length>0?t.sum(i).reshape(r.shape):t}}})}}),ro=Je({modStrict_:function(e,t){return m(e.shape,t.shape,"Error in modStrict: "),e.mod(t)}}),ao=Je({mul_:function(e,t){var n=Xe(e,"a","mul"),r=Xe(t,"b","mul");ne(n,r);var a=Dt(n.shape,r.shape);return me.engine.runKernel(function(e){return e.multiply(n,r)},{$a:n,$b:r},function(e){return{$a:function(){var t=e.mul(r.toFloat()),i=Rt(n.shape,a);return i.length>0?t.sum(i).reshape(n.shape):t},$b:function(){var t=e.mul(n.toFloat()),i=Rt(r.shape,a);return i.length>0?t.sum(i).reshape(r.shape):t}}})}}),io=Je({mulStrict_:function(e,t){return m(e.shape,t.shape,"Error in multiplyStrict: "),e.mul(t)}}),oo=Je({pow_:function(e,t){var n=Xe(e,"base","pow"),r=Xe(t,"exp","pow"),a=Dt(n.shape,r.shape);return e=n.cast(Et(n.dtype,r.dtype)),t=r.cast(Et(n.dtype,r.dtype)),me.engine.runKernel(function(e,t){return t(e.pow(n,r))},{$base:n,$exp:r},function(e,t){var i=t[0];return{$base:function(){var t=e.mul(r.toFloat().mul(i.div(n))),o=Rt(n.shape,a);return o.length>0&&(t=t.sum(o)),t.reshape(n.shape)},$exp:function(){var t=e.mul(i.mul(n.log()).toFloat()),o=Rt(r.shape,a);return o.length>0&&(t=t.sum(o)),t.reshape(r.shape)}}})}}),so=Je({powStrict_:function(e,t){return m(e.shape,t.shape,"Error in powStrict: "),e.pow(t)}}),uo=Je({squaredDifference_:function(e,t){var n=Xe(e,"a","squaredDifference"),r=Xe(t,"b","squaredDifference");return ne(n,r),Dt(n.shape,r.shape),me.engine.runKernel(function(e){return e.squaredDifference(n,r)},{$a:n,$b:r},function(e){var t=rt(2);return{$a:function(){return e.mul(n.sub(r).mul(t))},$b:function(){return e.mul(r.sub(n).mul(t))}}})}}),lo=Je({squaredDifferenceStrict_:function(e,t){return m(e.shape,t.shape,"Error in squaredDifferenceStrict: "),e.squaredDifference(t)}}),co=Je({sub_:function(e,t){var n=Xe(e,"a","sub"),r=Xe(t,"b","sub");ne(n,r);var a=Dt(n.shape,r.shape);return me.engine.runKernel(function(e){return e.subtract(n,r)},{$a:n,$b:r},function(e){return{$a:function(){var t=e,r=Rt(n.shape,a);return r.length>0&&(t=t.sum(r)),t.reshape(n.shape)},$b:function(){var t=e,n=Rt(r.shape,a);return n.length>0&&(t=t.sum(n)),t.neg().reshape(r.shape)}}})}}),fo=Je({subStrict_:function(e,t){return m(e.shape,t.shape,"Error in subStrict: "),e.sub(t)}}),po=Je({logicalAnd_:function(e,t){var n=Xe(e,"a","logicalAnd","bool"),r=Xe(t,"b","logicalAnd","bool");return d("bool"===n.dtype&&"bool"===r.dtype,"Error Array must be of type bool."),Dt(n.shape,r.shape),me.engine.runKernel(function(e){return e.logicalAnd(n,r)},{$a:n,$b:r})}}),ho=Je({logicalNot_:function(e){var t=Xe(e,"x","logicalNot","bool");return d("bool"===t.dtype,"Error Array must be of type bool."),me.engine.runKernel(function(e){return e.logicalNot(t)},{$x:t})}}),mo=Je({logicalOr_:function(e,t){var n=Xe(e,"a","logicalOr","bool"),r=Xe(t,"b","logicalOr","bool");return d("bool"===n.dtype&&"bool"===r.dtype,"Error Array must be of type bool."),Dt(n.shape,r.shape),me.engine.runKernel(function(e){return e.logicalOr(n,r)},{$a:n,$b:r})}}),go=Je({logicalXor_:function(e,t){var n=Xe(e,"a","logicalXor","bool"),r=Xe(t,"b","logicalXor","bool");return d("bool"===n.dtype&&"bool"===r.dtype,"Error Array must be of type bool."),Dt(n.shape,r.shape),mo(e,t).logicalAnd(po(e,t).logicalNot())}}),yo=Je({where_:function(e,t,n){var r=Xe(t,"a","where"),a=Xe(n,"b","where"),i=Xe(e,"condition","where","bool");return d("bool"===i.dtype,"Error Condition must be of type bool."),m(r.shape,a.shape,"Error in where: "),1===i.rank?d(i.shape[0]===r.shape[0],"The first dimension of `a` must match the size of `condition`."):m(i.shape,a.shape,"Error in where: "),me.engine.runKernel(function(e){return e.select(i,r,a)},{$condition:i,$a:r,$b:a},function(e){return{$condition:function(){return _t(i)},$a:function(){return e.mul(i.cast(r.dtype))},$b:function(){return e.mul(i.logicalNot().cast(a.dtype))}}})}}),vo=function(e){return l(this,void 0,void 0,function(){var t,n,r;return c(this,function(a){switch(a.label){case 0:return d("bool"===(t=Xe(e,"condition","where","bool")).dtype,"Condition must be of type bool."),[4,t.data()];case 1:return n=a.sent(),r=ua(t.shape,n),e!==t&&t.dispose(),[2,r]}})})},bo=Je({elu_:function(e){var t=Xe(e,"x","elu");return me.engine.runKernel(function(e,n){return n(e.elu(t))},{$x:t},function(e,t){var n=t[0];return{$x:function(){return me.engine.runKernel(function(t){return t.eluDer(e,n)},{dy:e,y:n})}}})}}),wo=Je({leakyRelu_:function(e,t){void 0===t&&(t=.2);var n=Xe(e,"x","leakyRelu");return Zi(rt(t).mul(n),n)}}),xo=Je({prelu_:function(e,t){var n=Xe(e,"x","prelu"),r=Xe(t,"alpha","prelu"),a=rt(0);return Zi(a,n).add(r.mul(eo(a,n)))}}),_o=Je({relu_:function(e){var t=Xe(e,"x","relu");return"bool"===t.dtype?t.toInt():me.engine.runKernel(function(e){return e.relu(t)},{$x:t},function(e){var n=t.step();return{$x:function(){return e.mulStrict(n.toFloat())}}})}}),So=Je({selu_:function(e){var t=Xe(e,"x","selu");return me.engine.runKernel(function(e){return e.selu(t)},{$x:t},function(e){return{$x:function(){var n=t.greater(rt(0)),r=rt(dr),a=rt(mr),i=e.mul(a),o=e.mul(r).mul(t.toFloat().exp());return yo(n,i,o)}}})}}),Eo=Je({transpose_:function(e,t){var n=Xe(e,"x","transpose");return null==t&&(t=n.shape.map(function(e,t){return t}).reverse()),d(n.rank===t.length,"Error in transpose: rank of input "+n.rank+" must match length of perm "+t+"."),t.forEach(function(e){d(e>=0&&ea)throw new Error("'k' passed to topk() must be <= the last dimension ("+a+") but got "+t);var i=me.engine.runKernel(function(e){return e.topk(r,t,n)},{$x:r});return{values:i[0],indices:i[1]}}});!function(e){e[e.NONE=0]="NONE",e[e.MEAN=1]="MEAN",e[e.SUM=2]="SUM",e[e.SUM_BY_NONZERO_WEIGHTS=3]="SUM_BY_NONZERO_WEIGHTS"}(Co||(Co={}));var Lo=Je({absoluteDifference_:function(e,t,n,r){void 0===r&&(r=Co.SUM_BY_NONZERO_WEIGHTS);var a=Xe(e,"labels","absoluteDifference"),i=Xe(t,"predictions","absoluteDifference"),o=null;null!=n&&(o=Xe(n,"weights","absoluteDifference")),m(a.shape,i.shape,"Error in absoluteDifference: ");var s=a.sub(i).abs();return zo(s,o,r)}}),zo=Je({computeWeightedLoss_:function(e,t,n){void 0===n&&(n=Co.SUM_BY_NONZERO_WEIGHTS);var r=Xe(e,"losses","computeWeightedLoss"),a=null;null!=t&&(a=Xe(t,"weights","computeWeightedLoss"));var i=null==a?r:r.mul(a);if(n===Co.NONE)return i;if(n===Co.SUM)return i.sum();if(n===Co.MEAN){if(null==a)return i.mean();var o=b(r.shape)/b(a.shape),s=i.sum().div(a.sum());return o>1?s.div(rt(o)):s}if(n===Co.SUM_BY_NONZERO_WEIGHTS){if(null==a)return i.sum().div(rt(r.size));var u=a.mul(ct(r.shape)).notEqual(rt(0)).sum().toFloat();return i.sum().div(u)}throw Error("Unknown reduction: "+n)}}),Bo=Je({cosineDistance_:function(e,t,n,r,a){void 0===a&&(a=Co.SUM_BY_NONZERO_WEIGHTS);var i=Xe(e,"labels","cosineDistance"),o=Xe(t,"predictions","cosineDistance"),s=null;null!=r&&(s=Xe(r,"weights","cosineDistance")),m(i.shape,o.shape,"Error in cosineDistance: ");var u=rt(1).sub(i.mul(o).sum(n,!0));return zo(u,s,a)}}),Fo=Je({hingeLoss_:function(e,t,n,r){void 0===r&&(r=Co.SUM_BY_NONZERO_WEIGHTS);var a=Xe(e,"labels","hingeLoss"),i=Xe(t,"predictions","hingeLoss"),o=null;null!=n&&(o=Xe(n,"weights","hingeLoss")),m(a.shape,i.shape,"Error in hingeLoss: ");var s=rt(1);a=rt(2).mul(a).sub(s);var u=s.sub(a.mul(i)).relu();return zo(u,o,r)}}),Vo=Je({huberLoss_:function(e,t,n,r,a){void 0===r&&(r=1),void 0===a&&(a=Co.SUM_BY_NONZERO_WEIGHTS);var i=Xe(e,"labels","huberLoss"),o=Xe(t,"predictions","huberLoss"),s=null;null!=n&&(s=Xe(n,"weights","huberLoss")),m(i.shape,o.shape,"Error in huberLoss: ");var u=rt(r),l=o.sub(i).abs(),c=eo(l,u),f=l.sub(c),p=rt(.5).mul(c.square()).add(u.mul(f));return zo(p,s,a)}}),Uo=Je({logLoss_:function(e,t,n,r,a){void 0===r&&(r=1e-7),void 0===a&&(a=Co.SUM_BY_NONZERO_WEIGHTS);var i=Xe(e,"labels","logLoss"),o=Xe(t,"predictions","logLoss"),s=null;null!=n&&(s=Xe(n,"weights","logLoss")),m(i.shape,o.shape,"Error in logLoss: ");var u=rt(1),l=rt(r),c=i.mul(o.add(l).log()).neg().sub(u.sub(i).mul(u.sub(o).add(l).log()));return zo(c,s,a)}}),Wo=Je({meanSquaredError_:function(e,t,n,r){void 0===r&&(r=Co.SUM_BY_NONZERO_WEIGHTS);var a=Xe(e,"labels","meanSquaredError"),i=Xe(t,"predictions","meanSquaredError"),o=null;null!=n&&(o=Xe(n,"weights","meanSquaredError")),m(a.shape,i.shape,"Error in meanSquaredError: ");var s=a.squaredDifference(i);return zo(s,o,r)}}),qo=Je({sigmoidCrossEntropy_:function(e,t,n,r,a){void 0===r&&(r=0),void 0===a&&(a=Co.SUM_BY_NONZERO_WEIGHTS);var i=Xe(e,"multiClassLabels","sigmoidCrossEntropy"),o=Xe(t,"logits","sigmoidCrossEntropy"),s=null;if(null!=n&&(s=Xe(n,"weights","sigmoidCrossEntropy")),m(i.shape,o.shape,"Error in sigmoidCrossEntropy: "),r>0){var u=rt(r),l=rt(1),c=rt(.5);i=i.mul(l.sub(u)).add(c.mul(u))}var f=function(e,t){var n=Xe(e,"labels","sigmoidCrossEntropyWithLogits"),r=Xe(t,"logits","sigmoidCrossEntropyWithLogits");m(n.shape,r.shape,"Error in sigmoidCrossEntropyWithLogits: ");var a=r.relu(),i=r.mul(n),o=r.abs().neg().exp().log1p();return a.sub(i).add(o)}(i,o);return zo(f,s,a)}}),$o=Je({softmaxCrossEntropy_:function(e,t,n,r,a){void 0===r&&(r=0),void 0===a&&(a=Co.SUM_BY_NONZERO_WEIGHTS);var i=Xe(e,"onehotLabels","softmaxCrossEntropy"),o=Xe(t,"logits","softmaxCrossEntropy"),s=null;if(null!=n&&(s=Xe(n,"weights","softmaxCrossEntropy")),m(i.shape,o.shape,"Error in softmaxCrossEntropy: "),r>0){var u=rt(r),l=rt(1),c=rt(i.shape[1]);i=i.mul(l.sub(u)).add(u.div(c))}var f=function(e,t,n){if(void 0===n&&(n=-1),-1===n&&(n=t.rank-1),n!==t.rank-1)throw Error("Softmax cross entropy along a non-last dimension is not yet supported. Labels / logits was rank "+t.rank+" and dim was "+n);return _e(function(e,t){var r=t.logSumExp([n],!0),a=t.toFloat().sub(r);return{value:a.mul(e).neg().sum([n]),gradFunc:function(t){var r=Le(t.shape,[n]);return[t.reshape(r).mul(e.toFloat().sub(a.exp())),t.reshape(r).mul(a.exp().sub(e.toFloat()))]}}})(e,t)}(i,o);return zo(f,s,a)}}),Go=Object.freeze({get Reduction(){return Co},absoluteDifference:Lo,computeWeightedLoss:zo,cosineDistance:Bo,hingeLoss:Fo,huberLoss:Vo,logLoss:Uo,meanSquaredError:Wo,sigmoidCrossEntropy:qo,softmaxCrossEntropy:$o});function Ho(e,t){return void 0===t&&(t=!1),me.engine.tidy(function(){if(2!==e.shape.length)throw new Error("qr2d() requires a 2D Tensor, but got a "+e.shape.length+"D Tensor.");for(var n=e.shape[0],r=e.shape[1],a=Wr(n),i=e.clone(),o=it([[1]],[1,1]),s=o.clone(),u=n>=r?r:n,l=function(e){var t,u=i,l=s,c=a;t=me.engine.tidy(function(){var t=i.slice([e,e],[n-e,1]),u=t.norm(),l=i.slice([e,e],[1,1]),c=l.sign().neg(),f=l.sub(c.mul(u)),p=t.div(f);s=1===p.shape[0]?o.clone():o.concat(p.slice([1,0],[p.shape[0]-1,p.shape[1]]),0);var h=c.matMul(f).div(u).neg(),d=i.slice([e,0],[n-e,r]),m=h.mul(s);i=0===e?d.sub(m.matMul(s.transpose().matMul(d))):i.slice([0,0],[e,r]).concat(d.sub(m.matMul(s.transpose().matMul(d))),0);var g=a.slice([0,e],[n,a.shape[1]-e]);return a=0===e?g.sub(g.matMul(s).matMul(m.transpose())):a.slice([0,0],[n,e]).concat(g.sub(g.matMul(s).matMul(m.transpose())),1),[s,i,a]}),s=t[0],i=t[1],a=t[2],Oe([u,l,c])},c=0;cr&&(a=a.slice([0,0],[n,r]),i=i.slice([0,0],[r,r])),[a,i]})}var Ko=Je({gramSchmidt_:function(e){var t;if(Array.isArray(e)){t=!1,d(null!=e&&e.length>0,"Gram-Schmidt process: input must not be null, undefined, or empty");for(var n=e[0].shape[0],r=1;r0)for(var n=0;n= 2, but got rank "+e.rank);if(2===e.rank)return Ho(e,t);var n=e.shape.slice(0,e.shape.length-2).reduce(function(e,t){return e*t}),r=[],a=[];return sa(e.reshape([n,e.shape[e.shape.length-2],e.shape[e.shape.length-1]]),0).forEach(function(e){var n=Ho(e,t),i=n[0],o=n[1];r.push(i),a.push(o)}),[aa(r,0).reshape(e.shape),aa(a,0).reshape(e.shape)]}}),Yo=Object.freeze({gramSchmidt:Ko,qr:Xo});function Jo(e,t,n,r,a){null==r&&(r=.5),null==a&&(a=Number.NEGATIVE_INFINITY);var i=e.shape[0];return n=Math.min(n,i),d(0<=r&&r<=1,"iouThreshold must be in [0, 1], but was '"+r+"'"),d(2===e.rank,"boxes must be a 2D tensor, but was of rank '"+e.rank+"'"),d(4===e.shape[1],"boxes must have 4 columns, but 2nd dimension was "+e.shape[1]),d(1===t.rank,"scores must be a 1D tensor"),d(t.shape[0]===i,"scores has incompatible shape with boxes. Expected "+i+", but was "+t.shape[0]),{maxOutputSize:n,iouThreshold:r,scoreThreshold:a}}var Zo=Je({resizeBilinear_:function(e,t,n){void 0===n&&(n=!1);var r=Xe(e,"images","resizeBilinear");d(3===r.rank||4===r.rank,"Error in resizeBilinear: x must be rank 3 or 4, but got rank "+r.rank+"."),d(2===t.length,"Error in resizeBilinear: new shape must 2D, but got shape "+t+".");var a=r,i=!1;3===r.rank&&(i=!0,a=r.as4D(1,r.shape[0],r.shape[1],r.shape[2]));var o=t[0],s=t[1],u=me.engine.runKernel(function(e,t){return e.resizeBilinear(a,o,s,n)},{batchImages:a},function(e,t){return{batchImages:function(){return me.engine.runKernel(function(t){return t.resizeBilinearBackprop(e,a,n)},{})}}});return i?u.as3D(u.shape[1],u.shape[2],u.shape[3]):u}}),Qo=Je({resizeNearestNeighbor_:function(e,t,n){void 0===n&&(n=!1);var r=Xe(e,"images","resizeNearestNeighbor");d(3===r.rank||4===r.rank,"Error in resizeNearestNeighbor: x must be rank 3 or 4, but got rank "+r.rank+"."),d(2===t.length,"Error in resizeNearestNeighbor: new shape must 2D, but got shape "+t+"."),d("float32"===r.dtype||"int32"===r.dtype,"`images` must have `int32` or `float32` as dtype");var a=r,i=!1;3===r.rank&&(i=!0,a=r.as4D(1,r.shape[0],r.shape[1],r.shape[2]));var o=t[0],s=t[1],u=me.engine.runKernel(function(e,t){return e.resizeNearestNeighbor(a,o,s,n)},{batchImages:a},function(e,t){return{batchImages:function(){return me.engine.runKernel(function(t){return t.resizeNearestNeighborBackprop(e,a,n)},{})}}});return i?u.as3D(u.shape[1],u.shape[2],u.shape[3]):u}}),es=Je({nonMaxSuppression_:function(e,t,n,r,a){void 0===r&&(r=.5),void 0===a&&(a=Number.NEGATIVE_INFINITY);var i=Xe(e,"boxes","nonMaxSuppression"),o=Xe(t,"scores","nonMaxSuppression"),s=Jo(i,o,n,r,a);return n=s.maxOutputSize,r=s.iouThreshold,a=s.scoreThreshold,me.engine.runKernel(function(e){return e.nonMaxSuppression(i,o,n,r,a)},{$boxes:i})}}),ts=Object.freeze({resizeBilinear:Zo,resizeNearestNeighbor:Qo,nonMaxSuppression:es,nonMaxSuppressionAsync:function(e,t,n,r,a){return void 0===r&&(r=.5),void 0===a&&(a=Number.NEGATIVE_INFINITY),l(this,void 0,void 0,function(){var i,o,s,u,l,f;return c(this,function(c){switch(c.label){case 0:return i=Xe(e,"boxes","nonMaxSuppressionAsync"),o=Xe(t,"scores","nonMaxSuppressionAsync"),s=Jo(i,o,n,r,a),n=s.maxOutputSize,r=s.iouThreshold,a=s.scoreThreshold,[4,i.data()];case 1:return u=c.sent(),[4,o.data()];case 2:return l=c.sent(),f=Ct(u,l,n,r,a),i!==e&&i.dispose(),o!==t&&o.dispose(),[2,f]}})})},cropAndResize:function(e,t,n,r,a,i){var o=Xe(e,"image","cropAndResize","float32"),s=Xe(t,"boxes","cropAndResize","float32"),u=Xe(n,"boxInd","cropAndResize","int32");a=a||"bilinear",i=i||0;var l=s.shape[0];return d(4===o.rank,"Error in cropAndResize: image must be rank 4,but got rank "+o.rank+"."),d(2===s.rank&&4===s.shape[1],"Error in cropAndResize: boxes must be have size ["+l+",4] but had shape "+s.shape+"."),d(1===u.rank&&u.shape[0]===l,"Error in cropAndResize: boxInd must be have size ["+l+"] but had shape "+s.shape+"."),d("int32"===u.dtype,"Error in cropAndResize: boxInd must be of dtype int32, but got dtype "+u.dtype+"."),d(2===r.length,"Error in cropAndResize: cropSize must be of length 2, but got length "+r.length+"."),d(r[0]>=1&&r[1]>=1,"cropSize must be atleast [1,1], but was "+r),d("bilinear"===a||"nearest"===a,"method must be bilinear or nearest, but was "+a),me.engine.runKernel(function(e,t){return e.cropAndResize(o,s,u,r,a,i)},{$image:o,$boxes:s})}}),ns=Object.freeze({image:ts,linalg:Yo,losses:Go,op:Je,batchNormalization2d:qa,batchNormalization3d:$a,batchNormalization4d:Ga,batchNormalization:Ha,complex:Qe,real:et,imag:tt,concat:vr,concat1d:br,concat2d:wr,concat3d:xr,concat4d:_r,split:Sr,conv1d:ii,conv2d:oi,depthwiseConv2d:si,separableConv2d:ui,conv2dTranspose:li,matMul:Qa,dot:ei,outerProduct:ti,reverse:ci,reverse1d:fi,reverse2d:pi,reverse3d:hi,reverse4d:di,maxPool:mi,avgPool:gi,slice:yi,slice1d:vi,slice2d:bi,slice3d:wi,slice4d:xi,abs:fa,acos:pa,acosh:ha,asin:da,asinh:ma,atan:ga,atanh:ya,ceil:va,clipByValue:ba,cos:wa,cosh:xa,erf:_a,exp:Sa,expm1:Ea,floor:Na,log:Oa,log1p:ka,logSigmoid:Ia,neg:Ca,reciprocal:Aa,round:Pa,rsqrt:Ta,sigmoid:Ma,sign:Ra,sin:Da,sinh:ja,softplus:La,sqrt:za,square:Ba,step:Fa,tan:Va,tanh:Ua,all:Si,any:Ei,argMax:Ni,argMin:Oi,logSumExp:ki,max:Ii,mean:Ci,min:Ai,moments:Pi,sum:Ti,equal:Mi,equalStrict:Ri,greater:Di,greaterEqual:ji,greaterEqualStrict:Li,greaterStrict:zi,less:Bi,lessEqual:Fi,lessEqualStrict:Vi,lessStrict:Ui,notEqual:Wi,notEqualStrict:qi,add:$i,addN:Gi,addStrict:Hi,atan2:Ki,div:Xi,divStrict:Yi,floorDiv:Ji,maximum:Zi,maximumStrict:Qi,minimum:eo,minimumStrict:to,mod:no,modStrict:ro,mul:ao,mulStrict:io,pow:oo,powStrict:so,squaredDifference:uo,squaredDifferenceStrict:lo,sub:co,subStrict:fo,elu:bo,leakyRelu:wo,prelu:xo,relu:_o,selu:So,logicalAnd:po,logicalNot:ho,logicalOr:mo,logicalXor:go,where:yo,whereAsync:vo,buffer:Dr,toPixels:Rr,print:jr,batchToSpaceND:Lr,cast:zr,clone:Br,cumsum:Fr,depthToSpace:Vr,expandDims:Ur,eye:Wr,fromPixels:qr,multinomial:$r,oneHot:Gr,pad:Hr,pad1d:Kr,pad2d:Xr,pad3d:Yr,pad4d:Jr,rand:Zr,randomNormal:Qr,randomUniform:ea,reshape:ta,spaceToBatchND:na,squeeze:ra,stack:aa,tile:ia,truncatedNormal:oa,unstack:sa,fill:pt,linspace:ht,ones:ct,range:dt,scalar:rt,tensor:nt,tensor1d:at,tensor2d:it,tensor3d:ot,tensor4d:st,tensor5d:ut,tensor6d:lt,zeros:ft,onesLike:xt,zerosLike:_t,transpose:Eo,softmax:Ze,localResponseNormalization:No,norm:Oo,gather:Ao,unsortedSegmentSum:Po,basicLSTMCell:To,multiRNNCell:Mo,movingAverage:Ro,stridedSlice:Do,topk:jo}),rs=function(){function e(){this.blockSize=48,this.data=new WeakMap,this.firstUse=!0,me.get("IS_BROWSER")&&(this.canvas=document.createElement("canvas"))}return e.prototype.register=function(e,t,n){if(this.firstUse&&(this.firstUse=!1,me.get("IS_NODE")&&Ce("\n============================\nHi there 👋. Looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, which binds to TensorFlow C++, by running npm i @tensorflow/tfjs-node, or npm i @tensorflow/tfjs-node-gpu if you have CUDA. Then call require('@tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of your program. Visit https://github.com/tensorflow/tfjs-node for more details.\n============================\n")),this.data.has(e))throw new Error("Data buffer is already registered");this.data.set(e,{dtype:n})},e.prototype.write=function(e,t){if(null==t)throw new Error("MathBackendCPU.write(): values can not be null");this.throwIfNoData(e),this.data.get(e).values=t},e.prototype.fromPixels=function(e,t){if(null==e)throw new Error("pixels passed to tf.fromPixels() can not be null");var n,r;if(me.get("IS_NODE")&&null==e.getContext)throw new Error("When running in node, pixels must be an HTMLCanvasElement like the one returned by the `canvas` npm package");if(null!=e.getContext)n=e.getContext("2d").getImageData(0,0,e.width,e.height).data;else if(e instanceof ImageData)n=e.data;else{if(!(e instanceof HTMLImageElement||e instanceof HTMLVideoElement))throw new Error("pixels passed to tf.fromPixels() must be either an HTMLVideoElement, HTMLImageElement, HTMLCanvasElement or ImageData, but was "+e.constructor.name);if(null==this.canvas)throw new Error("Can't read pixels from HTMLImageElement outside the browser.");this.canvas.width=e.width,this.canvas.height=e.height,this.canvas.getContext("2d").drawImage(e,0,0,e.width,e.height),n=this.canvas.getContext("2d").getImageData(0,0,e.width,e.height).data}if(4===t)r=new Int32Array(n);else{var a=e.width*e.height;r=new Int32Array(a*t);for(var i=0;ip&&(p=m,h=d)}u[c]=h}return o},e.prototype.cumsum=function(e,t,n,r){if(this.assertNotComplex(e,"cumsum"),t!==e.rank-1)throw new Error("backend.cumsum in CPU expects an inner-most axis="+(e.rank-1)+" but got axis="+t);for(var a=Et(e.dtype,"int32"),i=ft(e.shape,a),o=i.dataSync(),s=e.dataSync(),u=e.shape[e.rank-1],l=r?function(e,t){return e+u-t-1}:function(e,t){return e+t},c=0;ct?1:0})},e.prototype.greaterEqual=function(e,t){return this.assertNotComplex([e,t],"greaterEqual"),this.broadcastedBinaryOp(e,t,"bool",function(e,t){return e>=t?1:0})},e.prototype.logicalNot=function(e){this.assertNotComplex(e,"logicalNot");for(var t=e.dataSync(),n=new Int32Array(t.length),r=0;r1||1===t.rank?1:t.shape[1],c=0;c=0&&t>=0?n:(n+t)%t})},e.prototype.max=function(e,t){this.assertNotComplex(e,"max"),Be("max",t,e.rank);for(var n=je(e.shape,t),r=n[0],a=n[1],i=ft(r,e.dtype),o=b(a),s=i.dataSync(),u=e.dataSync(),l=0;lf&&(f=h)}s[l]=f}return i},e.prototype.maximum=function(e,t){return this.assertNotComplex([e,t],"maximum"),this.broadcastedBinaryOp(e,t,e.dtype,function(e,t){return Math.max(e,t)})},e.prototype.all=function(e,t){this.assertNotComplex(e,"all"),Be("all",t,e.rank);for(var n=je(e.shape,t),r=n[0],a=n[1],i=ft(r,e.dtype),o=b(a),s=i.dataSync(),u=e.dataSync(),l=0;l0?n[r]=1:n[r]=0;return Q.make(e.shape,{values:n})},e.prototype.round=function(e){this.assertNotComplex(e,"round");for(var t=e.dataSync(),n=new Float32Array(t.length),r=0;r.5?n[r]=Math.ceil(t[r]):n[r]=a%2==0?a:a+1}return Q.make(e.shape,{values:n})},e.prototype.exp=function(e){this.assertNotComplex(e,"exp");for(var t=e.dataSync(),n=new Float32Array(t.length),r=0;r=0?a:Math.exp(a)-1}return Q.make(e.shape,{values:t})},e.prototype.eluDer=function(e,t){this.assertNotComplex([e,t],"eluDer");for(var n=new Float32Array(t.size),r=t.dataSync(),a=e.dataSync(),i=0;i=1?a[i]:a[i]*(o+1)}return Q.make(t.shape,{values:n})},e.prototype.selu=function(e){this.assertNotComplex(e,"selu");for(var t=new Float32Array(e.size),n=e.dataSync(),r=0;r=0?1.0507009873554805*a:1.7580993408473768*(Math.exp(a)-1)}return Q.make(e.shape,{values:t})},e.prototype.clip=function(e,t,n){this.assertNotComplex(e,"clip");for(var r=new Float32Array(e.size),a=e.dataSync(),i=0;in?n:o-t,s=r[a]0?1:t}return Q.make(e.shape,{values:n})},e.prototype.conv2d=function(e,t,n){this.assertNotComplex([e,t],"conv2d");for(var r=n.filterHeight,a=n.filterWidth,i=n.dilationHeight,o=n.dilationWidth,s=n.padInfo.left,u=n.padInfo.top,l=Dr(n.outShape,e.dtype),c=e.dataSync(),f=t.dataSync(),p=l.values,h=0;h=n.inHeight))for(var x=b*t.strides[0],_=d+w*e.strides[1],S=0;S=n.inWidth))for(var I=x+O*t.strides[1],C=_+k*n.inChannels,A=I,P=0;P=n.inHeight))for(var _=w*t.strides[0],S=m+x*e.strides[1],E=0;E=n.inWidth))for(var C=_+k*t.strides[1],A=S+I*n.inChannels,P=N,T=C,M=0;Mx?x=O:"avg"===n&&(_+=O,S++)}if(isNaN(x))break}s.set("avg"===n?_/S:x,f,h,y,p)}return s.toTensor()},e.prototype.maxPool=function(e,t){return this.pool(e,t,"max")},e.prototype.maxPoolPositions=function(e,t){for(var n=Dr(t.outShape,"int32"),r=t.strideHeight,a=t.strideWidth,i=t.filterHeight,o=t.filterWidth,s=t.padInfo.top,u=t.padInfo.left,l=0;lb&&(b=N,w=_*o+E)}n.set(w,l,f,m,c)}return n.toTensor()},e.prototype.maxPoolBackprop=function(e,t,n,r){this.assertNotComplex([t,n],"maxPoolBackprop");for(var a=this.maxPoolPositions(t,r),i=r.strideHeight,o=r.strideWidth,s=r.filterHeight,u=r.filterWidth,l=u-1-r.padInfo.left,c=s-1-r.padInfo.top,f=Dr(t.shape,"float32"),p=0;p=r.outHeight||Math.floor(w)!==w))for(var x=0;x=r.outWidth||Math.floor(_)!==_)){var S=s*u-1-a.get(p,w,_,h)===b*u+x?1:0;0!==S&&(v+=e.get(p,w,_,h)*S)}}}f.set(v,p,d,m,h)}return f.toTensor()},e.prototype.avgPoolBackprop=function(e,t,n){this.assertNotComplex([e,t],"avgPoolBackprop");for(var r=n.strideHeight,a=n.strideWidth,i=n.filterHeight,o=n.filterWidth,s=o-1-n.padInfo.left,u=i-1-n.padInfo.top,l=Dr(t.shape,"float32"),c=1/(i*o),f=0;f=n.outHeight||Math.floor(b)!==b))for(var w=0;w=n.outWidth||Math.floor(x)!==x||(y+=e.get(f,b,x,p))}}l.set(y*c,f,h,d,p)}return l.toTensor()},e.prototype.cast=function(e,t){return Ot(e,t,this)},e.prototype.reshape=function(e,t){return kt(e,t)},e.prototype.avgPool=function(e,t){return this.assertNotComplex(e,"avgPool"),this.pool(e,t,"avg").toFloat()},e.prototype.resizeBilinear=function(e,t,n,r){this.assertNotComplex(e,"resizeBilinear");for(var a=e.shape,i=a[0],o=a[1],s=a[2],u=a[3],l=Dr([i,t,n,u],e.dtype),c=[r&&t>1?o-1:o,r&&n>1?s-1:s],f=[r&&t>1?t-1:t,r&&n>1?n-1:n],p=0;p1?i-1:i,n&&c>1?o-1:o],h=[n&&l>1?l-1:l,n&&c>1?c-1:c],d=p[0]/h[0],m=p[1]/h[1],g=0;g1?o-1:o,r&&n>1?s-1:s],f=[r&&t>1?t-1:t,r&&n>1?n-1:n],p=0;p1?i-1:i,n&&c>1?o-1:o],h=[n&&l>1?l-1:l,n&&c>1?c-1:c],d=1/(p[0]/h[0]),m=1/(p[1]/h[1]),g=2*Math.ceil(d)+2,y=2*Math.ceil(m)+2,v=0;v=l))for(var C=0;C=c)){var P=p[0]*(I/h[0]),T=p[1]*(A/h[1]),M=Math.min(i-1,n?Math.round(P):Math.floor(P)),R=Math.min(o-1,n?Math.round(T):Math.floor(T));b===M&&w===R&&(O+=e.get(v,I,A,N))}}}f.set(O,v,b,w,N)}return f.toTensor()},e.prototype.batchNormalization=function(e,t,n,r,a,i){this.assertNotComplex([e,t,n,a,i],"batchNormalization");for(var o=e.dataSync(),s=t.dataSync(),u=n.dataSync(),l=a?a.dataSync():new Float32Array([1]),c=i?i.dataSync():new Float32Array([0]),f=new Float32Array(o.length),p=c.length,h=l.length,d=u.length,m=s.length,g=0,y=0,v=0,b=0,w=0;w=p&&(g=0),y>=m&&(y=0),v>=h&&(v=0),b>=d&&(b=0);return st(f,e.shape)},e.prototype.localResponseNormalization4D=function(e,t,n,r,a){this.assertNotComplex(e,"localResponseNormalization4D");var i=e.shape[3],o=i-1,s=e.dataSync(),u=b(e.shape),l=new Float32Array(u);function c(e){for(var n=e%i,r=e-n+Math.max(0,n-t),a=e-n+Math.min(n+t,o),u=0;r<=a;r++){var l=s[r];u+=l*l}return u}for(var f=0;f=0&&e.get(i)1,"blockSize should be > 1 for depthToSpace, but was: "+t);for(var r=e.shape[0],a=e.shape[1],i=e.shape[2],o=e.shape[3],s=a*t,u=i*t,l=o/(t*t),c=e.dataSync(),f=new Float32Array(r*s*u*l),p=0,h=0;h=s))for(var k=p>1?(E-_)*(u-1)/(p-1):0,I=h>1?(N-S)*(l-1)/(h-1):0,C=0;C1?_*(u-1)+C*k:.5*(_+E)*(u-1);if(A<0||A>u-1)for(var P=0;P1?S*(l-1)+P*I:.5*(S+N)*(l-1))<0||$>l-1)for(T=0;T1?S*(l-1)+P*I:.5*(S+N)*(l-1))<0||$>l-1)for(T=0;T0,"scheme must not be an empty string.");var r=e.getInstance();d(null==r.managers[t],"A model store manager is already registered for scheme '"+t+"'."),r.managers[t]=n},e.getManager=function(e){var t=this.getInstance().managers[e];if(null==t)throw new Error("Cannot find model manager for scheme '"+e+"'");return t},e.getSchemes=function(){return Object.keys(this.getInstance().managers)},e}();function gs(e){if(-1===e.indexOf(ds))throw new Error("The url string provided does not contain a scheme. Supported schemes are: "+ms.getSchemes().join(","));return{scheme:e.split(ds)[0],path:e.split(ds)[1]}}function ys(e,t,n){return void 0===n&&(n=!1),l(this,void 0,void 0,function(){var r,a,i,o,s,u,l,f,p;return c(this,function(c){switch(c.label){case 0:return d(e!==t,"Old path and new path are the same: '"+e+"'"),d((r=hs.getLoadHandlers(e)).length>0,"Copying failed because no load handler is found for source URL "+e+"."),d(r.length<2,"Copying failed because more than one ("+r.length+") load handlers for source URL "+e+"."),a=r[0],d((i=hs.getSaveHandlers(t)).length>0,"Copying failed because no save handler is found for destination URL "+t+"."),d(i.length<2,"Copying failed because more than one ("+r.length+") save handlers for destination URL "+t+"."),o=i[0],s=gs(e).scheme,u=gs(e).path,l=s===gs(e).scheme,[4,a.load()];case 1:return f=c.sent(),n&&l?[4,ms.getManager(s).removeModel(u)]:[3,3];case 2:c.sent(),c.label=3;case 3:return[4,o.save(f)];case 4:return p=c.sent(),!n||l?[3,6]:[4,ms.getManager(s).removeModel(u)];case 5:c.sent(),c.label=6;case 6:return[2,p.modelArtifactsInfo]}})})}var vs="models_store",bs="model_info_store";function ws(){if(!me.get("IS_BROWSER"))throw new Error("Failed to obtain IndexedDB factory because the current environmentis not a web browser.");var e=window,t=e.indexedDB||e.mozIndexedDB||e.webkitIndexedDB||e.msIndexedDB||e.shimIndexedDB;if(null==t)throw new Error("The current browser does not appear to support IndexedDB.");return t}function xs(e){var t=e.result;t.createObjectStore(vs,{keyPath:"modelPath"}),t.createObjectStore(bs,{keyPath:"modelPath"})}var _s=function(){function e(e){if(this.indexedDB=ws(),null==e||!e)throw new Error("For IndexedDB, modelPath must not be null, undefined or empty.");this.modelPath=e}return e.prototype.save=function(e){return l(this,void 0,void 0,function(){return c(this,function(t){if(e.modelTopology instanceof ArrayBuffer)throw new Error("BrowserLocalStorage.save() does not support saving model topology in binary formats yet.");return[2,this.databaseAction(this.modelPath,e)]})})},e.prototype.load=function(){return l(this,void 0,void 0,function(){return c(this,function(e){return[2,this.databaseAction(this.modelPath)]})})},e.prototype.databaseAction=function(e,t){var n=this;return new Promise(function(e,r){var a=n.indexedDB.open("tensorflowjs",1);a.onupgradeneeded=function(){return xs(a)},a.onsuccess=function(){var i=a.result;if(null==t){var o=i.transaction(vs,"readonly"),s=o.objectStore(vs).get(n.modelPath);s.onsuccess=function(){if(null==s.result)return i.close(),r(new Error("Cannot find model with path '"+n.modelPath+"' in IndexedDB."));e(s.result.modelArtifacts)},s.onerror=function(e){return i.close(),r(s.error)},o.oncomplete=function(){return i.close()}}else{var u,l=ps(t),c=i.transaction(bs,"readwrite"),f=c.objectStore(bs),p=f.put({modelPath:n.modelPath,modelArtifactsInfo:l});p.onsuccess=function(){var a=(u=i.transaction(vs,"readwrite")).objectStore(vs).put({modelPath:n.modelPath,modelArtifacts:t,modelArtifactsInfo:l});a.onsuccess=function(){return e({modelArtifactsInfo:l})},a.onerror=function(e){var t=(f=c.objectStore(bs)).delete(n.modelPath);t.onsuccess=function(){return i.close(),r(a.error)},t.onerror=function(e){return i.close(),r(a.error)}}},p.onerror=function(e){return i.close(),r(p.error)},c.oncomplete=function(){null==u?i.close():u.oncomplete=function(){return i.close()}}}},a.onerror=function(e){return r(a.error)}})},e.URL_SCHEME="indexeddb://",e}(),Ss=function(e){return me.get("IS_BROWSER")&&!Array.isArray(e)&&e.startsWith(_s.URL_SCHEME)?function(e){return new _s(e)}(e.slice(_s.URL_SCHEME.length)):null};hs.registerSaveRouter(Ss),hs.registerLoadRouter(Ss);var Es=function(){function e(){this.indexedDB=ws()}return e.prototype.listModels=function(){return l(this,void 0,void 0,function(){var e=this;return c(this,function(t){return[2,new Promise(function(t,n){var r=e.indexedDB.open("tensorflowjs",1);r.onupgradeneeded=function(){return xs(r)},r.onsuccess=function(){var e=r.result,a=e.transaction(bs,"readonly"),i=a.objectStore(bs).getAll();i.onsuccess=function(){for(var e={},n=0,r=i.result;n0,"URL path for browserHTTPRequest must not be null, undefined or empty."),Array.isArray(e)&&d(2===e.length,"URL paths for browserHTTPRequest must have a length of 2, (actual length is "+e.length+")."),this.path=e,null!=t&&null!=t.body)throw new Error("requestInit is expected to have no pre-existing body, but has one.");this.requestInit=t||{}}return e.prototype.save=function(e){return l(this,void 0,void 0,function(){var t,n,r,a;return c(this,function(i){switch(i.label){case 0:if(e.modelTopology instanceof ArrayBuffer)throw new Error("BrowserHTTPRequest.save() does not support saving model topology in binary formats yet.");return(t=Object.assign({method:this.DEFAULT_METHOD},this.requestInit)).body=new FormData,n=[{paths:["./model.weights.bin"],weights:e.weightSpecs}],r={modelTopology:e.modelTopology,weightsManifest:n},t.body.append("model.json",new Blob([JSON.stringify(r)],{type:"application/json"}),"model.json"),null!=e.weightData&&t.body.append("model.weights.bin",new Blob([e.weightData],{type:"application/octet-stream"}),"model.weights.bin"),[4,fetch(this.path,t)];case 1:if(200===(a=i.sent()).status)return[2,{modelArtifactsInfo:ps(e),responses:[a]}];throw new Error("BrowserHTTPRequest.save() failed due to HTTP response status "+a.status+".")}})})},e.prototype.load=function(){return l(this,void 0,void 0,function(){return c(this,function(e){return[2,Array.isArray(this.path)?this.loadBinaryModel():this.loadJSONModel()]})})},e.prototype.loadBinaryTopology=function(){return l(this,void 0,void 0,function(){var e;return c(this,function(t){switch(t.label){case 0:return t.trys.push([0,3,,4]),[4,fetch(this.path[0],this.requestInit)];case 1:return[4,t.sent().arrayBuffer()];case 2:return[2,t.sent()];case 3:throw e=t.sent(),new Error(this.path[0]+" not found. "+e);case 4:return[2]}})})},e.prototype.loadBinaryModel=function(){return l(this,void 0,void 0,function(){var e,t,n,r,a,i,o,s;return c(this,function(u){switch(u.label){case 0:return e=this.loadBinaryTopology(),[4,fetch(this.path[1],this.requestInit)];case 1:return t=u.sent(),[4,Promise.all([e,t])];case 2:return n=u.sent(),r=n[0],[4,n[1].json()];case 3:return null==(a=u.sent())?[3,5]:[4,this.loadWeights(a)];case 4:s=u.sent(),i=s[0],o=s[1],u.label=5;case 5:return[2,{modelTopology:r,weightSpecs:i,weightData:o}]}})})},e.prototype.loadJSONModel=function(){return l(this,void 0,void 0,function(){var e,t,n,r,a,i,o;return c(this,function(s){switch(s.label){case 0:return[4,fetch(this.path,this.requestInit)];case 1:return[4,s.sent().json()];case 2:if(e=s.sent(),t=e.modelTopology,n=e.weightsManifest,null==t&&null==n)throw new Error("The JSON from HTTP path "+this.path+" contains neither model topology or manifest for weights.");return null==n?[3,4]:(i=e.weightsManifest,[4,this.loadWeights(i)]);case 3:o=s.sent(),r=o[0],a=o[1],s.label=4;case 4:return[2,{modelTopology:t,weightSpecs:r,weightData:a}]}})})},e.prototype.loadWeights=function(e){return l(this,void 0,void 0,function(){var t,n,r,a,i,o,s,u,l;return c(this,function(c){switch(c.label){case 0:for(t=Array.isArray(this.path)?this.path[1]:this.path,n=[],r=0,a=e;r0&&(e=u({weightSpecs:this.weightSpecs},e)),null!=this.weightData&&this.weightData.byteLength>0&&(e=u({weightData:this.weightData},e)),[2,e]})})},e}(),Hs=function(){function e(e){this.saveHandler=e}return e.prototype.save=function(e){return l(this,void 0,void 0,function(){return c(this,function(t){return[2,this.saveHandler(e)]})})},e}(),Ks=hs.registerSaveRouter,Xs=hs.registerLoadRouter,Ys=hs.getSaveHandlers,Js=hs.getLoadHandlers,Zs=Object.freeze({browserFiles:function(e){return new Fs(e)},browserHTTPRequest:$s,concatenateArrayBuffers:cs,decodeWeights:ss,encodeWeights:function(e){return l(this,void 0,void 0,function(){var t,n,r,a;return c(this,function(i){switch(i.label){case 0:for(r in t=[],n=[],e){if("float32"!==(a=e[r]).dtype&&"int32"!==a.dtype&&"bool"!==a.dtype)throw new Error("Unsupported dtype in weight '"+r+"': "+a.dtype);t.push({name:r,shape:a.shape,dtype:a.dtype}),n.push(a.data())}return[4,Promise.all(n)];case 1:return[2,{data:function(e){if(null===e)throw new Error("Invalid input value: "+JSON.stringify(e));var t=0,n=[];e.forEach(function(e){if(t+=e.byteLength,n.push(e.byteLength===e.buffer.byteLength?e:new e.constructor(e)),!(e instanceof Float32Array||e instanceof Int32Array||e instanceof Uint8Array))throw new Error("Unsupported TypedArray subtype: "+e.constructor.name)});var r=new Uint8Array(t),a=0;return n.forEach(function(e){r.set(new Uint8Array(e.buffer),a),a+=e.byteLength}),r.buffer}(i.sent()),specs:t}]}})})},fromMemory:function(e,t,n){return new Gs(e,t,n)},getLoadHandlers:Js,getModelArtifactsInfoForJSON:ps,getSaveHandlers:Ys,loadWeights:function(e,t,n,r){return void 0===t&&(t=""),l(this,void 0,void 0,function(){var a,i,o,s,u,l,f,p,h,d;return c(this,function(c){switch(c.label){case 0:if(a=e.map(function(){return!1}),i={},o=null!=n?n.map(function(){return!1}):[],s=[],e.forEach(function(e,t){var r=0;e.weights.forEach(function(e){var u="quantization"in e?e.quantization.dtype:e.dtype,l=os[u]*b(e.shape),c=function(){a[t]=!0,null==i[t]&&(i[t]=[]),i[t].push({manifestEntry:e,groupOffset:r,sizeBytes:l})};null!=n?n.forEach(function(t,n){t===e.name&&(c(),o[n]=!0)}):c(),s.push(e.name),r+=l})}),!o.every(function(e){return e}))throw u=n.filter(function(e,t){return!o[t]}),new Error("Could not find weights in manifest with names: "+u.join(", ")+". \nManifest JSON has weights with names: "+s.join(", ")+".");return l=a.reduce(function(e,t,n){return t&&e.push(n),e},[]),f=[],l.forEach(function(n){e[n].paths.forEach(function(e){var n=t+(t.endsWith("/")?"":"/")+e;f.push(n)})}),[4,Vs(f,r)];case 1:return p=c.sent(),h={},d=0,l.forEach(function(t){for(var n=e[t].paths.length,r=0,a=0;a0,"Class being registered has an empty-string as its className, which is disallowed."),eu.register(e)}var nu=Object.freeze({Serializable:Qs,SerializationMap:eu,registerClass:tu});function ru(e,t,n){if(null==n&&(n=me.get("TEST_EPSILON")),e instanceof Q||t instanceof Q){if(e instanceof Q&&t instanceof Q){if(e.dtype!==t.dtype)throw new Error("Arrays are of different type actual: "+e.dtype+" vs expected: "+t.dtype+".");if(!w(e.shape,t.shape))throw new Error("Arrays are of different shape actual: "+e.shape+" vs expected: "+t.shape+".")}}else{var r=e.constructor.name,a=t.constructor.name;if(r!==a)throw new Error("Arrays are of different type actual: "+r+" vs expected: "+a)}var i,o;if(i=e instanceof Q?e.dataSync():e,o=t instanceof Q?t.dataSync():t,i.length!==o.length)throw new Error("Arrays have different lengths actual: "+i.length+" vs expected: "+o.length+".\nActual: "+i+".\nExpected: "+o+".");for(var s=0;sn)}var iu=Object.freeze({WEBGL_ENVS:{HAS_WEBGL:!0},NODE_ENVS:{IS_NODE:!0},CHROME_ENVS:{IS_CHROME:!0},BROWSER_ENVS:{IS_BROWSER:!0},CPU_ENVS:{HAS_WEBGL:!1},ALL_ENVS:{},expectArraysClose:ru,expectPromiseToFail:function(e,t){e().then(function(){return t.fail()},function(){return t()})},expectArraysEqual:function(e,t){return ru(e,t,0)},expectNumbersClose:function(e,t,n){if(null==n&&(n=me.get("TEST_EPSILON")),!au(e,t,n))throw new Error("Numbers differ: actual === "+e+", expected === "+t)},expectValuesInRange:function(e,t,n){var r;r=e instanceof Q?e.dataSync():e;for(var a=0;an)throw new Error("Value out of range:"+r[a]+" low: "+t+", high: "+n)},expectArrayBuffersEqual:function(e,t){expect(new Float32Array(e)).toEqual(new Float32Array(t))}}),ou="0.13.0",su=Object.freeze({gpgpu_util:ar,webgl_util:Ln,MathBackendWebGL:ca,GPGPUContext:ir}),uu=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return s(t,e),t.prototype.minimize=function(e,t,n){void 0===t&&(t=!1);var r=this.computeGradients(e,n),a=r.value,i=r.grads;return this.applyGradients(i),Object.keys(i).forEach(function(e){return i[e].dispose()}),t?a:(a.dispose(),null)},t.prototype.computeGradients=function(e,t){return xe(e,t)},t}(Qs),lu=function(e){function t(t,n,r){void 0===r&&(r=null);var a=e.call(this)||this;return a.learningRate=t,a.rho=n,a.epsilon=r,a.accumulatedGrads={},a.accumulatedUpdates={},a.c=Ne(rt(-t)),a.rhoScalar=Ne(rt(n)),a.oneMinusRho=Ne(rt(1-n)),null===r&&(r=me.get("EPSILON")),a.epsilonScalar=Ne(rt(r)),a}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=me.engine.registeredVariables[n];null==r.accumulatedGrads[n]&&Ee(function(){t.accumulatedGrads[n]=_t(a).variable(!1)}),null==r.accumulatedUpdates[n]&&Ee(function(){t.accumulatedUpdates[n]=_t(a).variable(!1)});var i=e[n],o=r.accumulatedGrads[n],s=r.accumulatedUpdates[n];Ee(function(){var e=t.rhoScalar.mul(o).add(t.oneMinusRho.mul(i.square())),r=s.add(t.epsilonScalar).sqrt().div(o.add(t.epsilonScalar).sqrt()).mul(i),u=t.rhoScalar.mul(s).add(t.oneMinusRho.mul(r.square()));t.accumulatedGrads[n].assign(e),t.accumulatedUpdates[n].assign(u);var l=t.c.mul(r).add(a);a.assign(l)})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsilonScalar.dispose(),this.rhoScalar.dispose(),this.oneMinusRho.dispose(),null!=this.accumulatedUpdates&&(Object.keys(this.accumulatedUpdates).forEach(function(t){return e.accumulatedUpdates[t].dispose()}),Object.keys(this.accumulatedGrads).forEach(function(t){return e.accumulatedGrads[t].dispose()}))},t.prototype.getConfig=function(){return{learningRate:this.learningRate,rho:this.rho,epsilon:this.epsilon}},t.fromConfig=function(e,t){return new e(t.learningRate,t.rho,t.epsilon)},t.className="AdadeltaOptimizer",t}(uu);tu(lu);var cu=function(e){function t(t,n){void 0===n&&(n=.1);var r=e.call(this)||this;return r.learningRate=t,r.initialAccumulatorValue=n,r.accumulatedGrads={},r.c=Ne(rt(-t)),r.epsilon=Ne(rt(me.get("EPSILON"))),r}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=me.engine.registeredVariables[n];null==r.accumulatedGrads[n]&&Ee(function(){t.accumulatedGrads[n]=pt(a.shape,t.initialAccumulatorValue).variable(!1)});var i=e[n],o=r.accumulatedGrads[n];Ee(function(){var e=o.add(i.square());t.accumulatedGrads[n].assign(e);var r=t.c.mul(i.div(e.add(t.epsilon).sqrt())).add(a);a.assign(r)})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){var e=this;this.epsilon.dispose(),this.c.dispose(),null!=this.accumulatedGrads&&Object.keys(this.accumulatedGrads).forEach(function(t){return e.accumulatedGrads[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,initialAccumulatorValue:this.initialAccumulatorValue}},t.fromConfig=function(e,t){return new e(t.learningRate,t.initialAccumulatorValue)},t.className="AdagradOptimizer",t}(uu);tu(cu);var fu=function(e){function t(t,n,r,a){void 0===a&&(a=null);var i=e.call(this)||this;return i.learningRate=t,i.beta1=n,i.beta2=r,i.epsilon=a,i.accumulatedFirstMoment={},i.accumulatedSecondMoment={},i.c=Ne(rt(-t)),i.beta1Scalar=Ne(rt(n)),i.beta2Scalar=Ne(rt(r)),Ee(function(){i.accBeta1=rt(n).variable(),i.accBeta2=rt(r).variable()}),i.oneMinusBeta1=Ne(rt(1-n)),i.oneMinusBeta2=Ne(rt(1-r)),i.one=Ne(rt(1)),null===a&&(a=me.get("EPSILON")),i.epsScalar=Ne(rt(a)),i}return s(t,e),t.prototype.applyGradients=function(e){var t=this;Ee(function(){var n=t.one.sub(t.accBeta1),r=t.one.sub(t.accBeta2);for(var a in e){var i=me.engine.registeredVariables[a];if(null==t.accumulatedFirstMoment[a]){var o=!1;t.accumulatedFirstMoment[a]=_t(i).variable(o)}null==t.accumulatedSecondMoment[a]&&(o=!1,t.accumulatedSecondMoment[a]=_t(i).variable(o));var s=e[a],u=t.accumulatedFirstMoment[a],l=t.accumulatedSecondMoment[a],c=t.beta1Scalar.mul(u).add(t.oneMinusBeta1.mul(s)),f=t.beta2Scalar.mul(l).add(t.oneMinusBeta2.mul(s.square())),p=c.div(n),h=f.div(r);t.accumulatedFirstMoment[a].assign(c),t.accumulatedSecondMoment[a].assign(f);var d=t.c.mul(p.div(t.epsScalar.add(h.sqrt()))).add(i);i.assign(d)}t.accBeta1.assign(t.accBeta1.mul(t.beta1Scalar)),t.accBeta2.assign(t.accBeta2.mul(t.beta2Scalar))})},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsScalar.dispose(),this.beta1Scalar.dispose(),this.beta2Scalar.dispose(),this.accBeta1.dispose(),this.accBeta2.dispose(),this.oneMinusBeta1.dispose(),this.oneMinusBeta2.dispose(),this.one.dispose(),null!=this.accumulatedFirstMoment&&Object.keys(this.accumulatedFirstMoment).forEach(function(t){return e.accumulatedFirstMoment[t].dispose()}),null!=this.accumulatedSecondMoment&&Object.keys(this.accumulatedSecondMoment).forEach(function(t){return e.accumulatedSecondMoment[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon}},t.fromConfig=function(e,t){return new e(t.learningRate,t.beta1,t.beta2,t.epsilon)},t.className="AdamOptimizer",t}(uu);tu(fu);var pu=function(e){function t(t,n,r,a,i){void 0===a&&(a=null),void 0===i&&(i=0);var o=e.call(this)||this;return o.learningRate=t,o.beta1=n,o.beta2=r,o.epsilon=a,o.decay=i,o.accumulatedFirstMoment={},o.accumulatedWeightedInfNorm={},o.c=Ne(rt(-t)),o.beta1Scalar=Ne(rt(n)),o.beta2Scalar=Ne(rt(r)),o.decayScalar=Ne(rt(i)),Ee(function(){o.iteration=rt(0).variable(),o.accBeta1=rt(n).variable()}),o.oneMinusBeta1=Ne(rt(1-n)),o.one=Ne(rt(1)),null===a&&(a=me.get("EPSILON")),o.epsScalar=Ne(rt(a)),o}return s(t,e),t.prototype.applyGradients=function(e){var t=this;Ee(function(){var n=t.one.sub(t.accBeta1),r=t.c.div(t.one.add(t.decayScalar.mul(t.iteration)));for(var a in e){var i=me.engine.registeredVariables[a];if(null==t.accumulatedFirstMoment[a]){var o=!1;t.accumulatedFirstMoment[a]=_t(i).variable(o)}null==t.accumulatedWeightedInfNorm[a]&&(o=!1,t.accumulatedWeightedInfNorm[a]=_t(i).variable(o));var s=e[a],u=t.accumulatedFirstMoment[a],l=t.accumulatedWeightedInfNorm[a],c=t.beta1Scalar.mul(u).add(t.oneMinusBeta1.mul(s)),f=t.beta2Scalar.mul(l),p=s.abs(),h=f.maximum(p);t.accumulatedFirstMoment[a].assign(c),t.accumulatedWeightedInfNorm[a].assign(h);var d=r.div(n).mul(c.div(t.epsScalar.add(h))).add(i);i.assign(d)}t.iteration.assign(t.iteration.add(t.one)),t.accBeta1.assign(t.accBeta1.mul(t.beta1Scalar))})},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsScalar.dispose(),this.accBeta1.dispose(),this.beta1Scalar.dispose(),this.beta2Scalar.dispose(),this.oneMinusBeta1.dispose(),this.decayScalar.dispose(),this.iteration.dispose(),this.one.dispose(),null!=this.accumulatedFirstMoment&&Object.keys(this.accumulatedFirstMoment).forEach(function(t){return e.accumulatedFirstMoment[t].dispose()}),null!=this.accumulatedWeightedInfNorm&&Object.keys(this.accumulatedWeightedInfNorm).forEach(function(t){return e.accumulatedWeightedInfNorm[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon,decay:this.decay}},t.fromConfig=function(e,t){return new e(t.learningRate,t.beta1,t.beta2,t.epsilon,t.decay)},t.className="AdamaxOptimizer",t}(uu);tu(pu);var hu=function(e){function t(t){var n=e.call(this)||this;return n.learningRate=t,n.setLearningRate(t),n}return s(t,e),t.prototype.applyGradients=function(e){var t=this;Object.keys(e).forEach(function(n){var r=e[n],a=me.engine.registeredVariables[n];Ee(function(){var e=t.c.mul(r).add(a);a.assign(e)})})},t.prototype.setLearningRate=function(e){this.learningRate=e,null!=this.c&&this.c.dispose(),this.c=Ne(rt(-e))},t.prototype.dispose=function(){this.c.dispose()},t.prototype.getConfig=function(){return{learningRate:this.learningRate}},t.fromConfig=function(e,t){return new e(t.learningRate)},t.className="SGDOptimizer",t}(uu);tu(hu);var du=function(e){function t(t,n,r){void 0===r&&(r=!1);var a=e.call(this,t)||this;return a.learningRate=t,a.momentum=n,a.useNesterov=r,a.m=rt(a.momentum),a.accumulations={},a}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=me.engine.registeredVariables[n];null==r.accumulations[n]&&Ee(function(){t.accumulations[n]=_t(a).variable(!1)});var i=r.accumulations[n],o=e[n];Ee(function(){var e,r=t.m.mul(i).add(o);e=t.useNesterov?t.c.mul(o.add(r.mul(t.m))).add(a):t.c.mul(r).add(a),t.accumulations[n].assign(r),a.assign(e)})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){if(e.prototype.dispose.call(this),this.m.dispose(),null!=this.accumulations)for(var t in this.accumulations)this.accumulations[t].dispose()},t.prototype.setMomentum=function(e){this.momentum=e},t.prototype.getConfig=function(){return{learningRate:this.learningRate,momentum:this.momentum,useNesterov:this.useNesterov}},t.fromConfig=function(e,t){return new e(t.learningRate,t.momentum,t.useNesterov)},t.className="MomentumOptimizer",t}(hu);tu(du);var mu=function(e){function t(t,n,r,a,i){void 0===n&&(n=.9),void 0===r&&(r=0),void 0===a&&(a=null),void 0===i&&(i=!1);var o=e.call(this)||this;return o.learningRate=t,o.decay=n,o.momentum=r,o.epsilon=a,o.accumulatedMeanSquares={},o.accumulatedMeanGrads={},o.accumulatedMoments={},o.c=Ne(rt(t)),o.decayScalar=Ne(rt(n)),o.momentumScalar=Ne(rt(r)),o.oneMinusDecay=Ne(rt(1-n)),o.centered=i,null===a&&(a=me.get("EPSILON")),o.epsilonScalar=Ne(rt(a)),o}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=me.engine.registeredVariables[n];null==r.accumulatedMeanSquares[n]&&Ee(function(){t.accumulatedMeanSquares[n]=_t(a).variable(!1)}),null==r.accumulatedMeanGrads[n]&&r.centered&&Ee(function(){t.accumulatedMeanGrads[n]=_t(a).variable(!1)}),null==r.accumulatedMoments[n]&&Ee(function(){t.accumulatedMoments[n]=_t(a).variable(!1)});var i=r.accumulatedMeanSquares[n],o=r.accumulatedMeanGrads[n],s=r.accumulatedMoments[n],u=e[n];Ee(function(){var e=t.decayScalar.mul(i).add(t.oneMinusDecay.mul(u.square()));if(t.centered){var r=t.decayScalar.mul(o).add(t.oneMinusDecay.mul(u)),l=t.momentumScalar.mul(s).add(t.c.mul(u).div(e.sub(r.square().add(t.epsilonScalar)).sqrt()));t.accumulatedMeanSquares[n].assign(e),t.accumulatedMeanGrads[n].assign(r),t.accumulatedMoments[n].assign(l);var c=a.sub(l);a.assign(c)}else{var f=t.decayScalar.mul(i).add(t.oneMinusDecay.mul(u.square()));l=t.momentumScalar.mul(s).add(t.c.mul(u).div(f.add(t.epsilonScalar).sqrt())),t.accumulatedMeanSquares[n].assign(f),t.accumulatedMoments[n].assign(l),c=a.sub(l),a.assign(c)}})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsilonScalar.dispose(),this.decayScalar.dispose(),this.momentumScalar.dispose(),this.oneMinusDecay.dispose(),null!=this.accumulatedMeanSquares&&Object.keys(this.accumulatedMeanSquares).forEach(function(t){return e.accumulatedMeanSquares[t].dispose()}),null!=this.accumulatedMeanGrads&&this.centered&&Object.keys(this.accumulatedMeanGrads).forEach(function(t){return e.accumulatedMeanGrads[t].dispose()}),null!=this.accumulatedMoments&&Object.keys(this.accumulatedMoments).forEach(function(t){return e.accumulatedMoments[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,decay:this.decay,momentum:this.momentum,epsilon:this.epsilon,centered:this.centered}},t.fromConfig=function(e,t){return new e(t.learningRate,t.decay,t.momentum,t.epsilon,t.centered)},t.className="RMSPropOptimizer",t}(uu);tu(mu);var gu=function(){function e(){}return e.sgd=function(e){return new hu(e)},e.momentum=function(e,t,n){return void 0===n&&(n=!1),new du(e,t,n)},e.rmsprop=function(e,t,n,r,a){return void 0===t&&(t=.9),void 0===n&&(n=0),void 0===r&&(r=null),void 0===a&&(a=!1),new mu(e,t,n,r,a)},e.adam=function(e,t,n,r){return void 0===e&&(e=.001),void 0===t&&(t=.9),void 0===n&&(n=.999),void 0===r&&(r=null),new fu(e,t,n,r)},e.adadelta=function(e,t,n){return void 0===e&&(e=.001),void 0===t&&(t=.95),void 0===n&&(n=null),new lu(e,t,n)},e.adamax=function(e,t,n,r,a){return void 0===e&&(e=.002),void 0===t&&(t=.9),void 0===n&&(n=.999),void 0===r&&(r=null),void 0===a&&(a=0),new pu(e,t,n,r,a)},e.adagrad=function(e,t){return void 0===t&&(t=.1),new cu(e,t)},e}(),yu={sgd:gu.sgd,momentum:gu.momentum,adadelta:gu.adadelta,adagrad:gu.adagrad,rmsprop:gu.rmsprop,adamax:gu.adamax,adam:gu.adam},vu=de.setBackend,bu=de.getBackend,wu=de.disposeVariables,xu=de.memory;J=ns}).call(this,n(46),n(26),n(162).setImmediate,n(16).Buffer)},function(e,t,n){"use strict";n.r(t),function(e,r,a,i){n.d(t,"setBackend",function(){return mu}),n.d(t,"getBackend",function(){return gu}),n.d(t,"disposeVariables",function(){return yu}),n.d(t,"memory",function(){return vu}),n.d(t,"version_core",function(){return ru}),n.d(t,"nextFrame",function(){return ns}),n.d(t,"environment",function(){return ge}),n.d(t,"io",function(){return Xs}),n.d(t,"serialization",function(){return Qs}),n.d(t,"test_util",function(){return nu}),n.d(t,"util",function(){return V}),n.d(t,"webgl",function(){return au}),n.d(t,"AdadeltaOptimizer",function(){return ou}),n.d(t,"AdagradOptimizer",function(){return su}),n.d(t,"AdamOptimizer",function(){return uu}),n.d(t,"AdamaxOptimizer",function(){return lu}),n.d(t,"MomentumOptimizer",function(){return fu}),n.d(t,"Optimizer",function(){return iu}),n.d(t,"RMSPropOptimizer",function(){return pu}),n.d(t,"SGDOptimizer",function(){return cu}),n.d(t,"Tensor",function(){return Q}),n.d(t,"TensorBuffer",function(){return X}),n.d(t,"variable",function(){return te}),n.d(t,"Variable",function(){return ee}),n.d(t,"Rank",function(){return mt}),n.d(t,"Reduction",function(){return Oo}),n.d(t,"ENV",function(){return me}),n.d(t,"Environment",function(){return de}),n.d(t,"image",function(){return Zo}),n.d(t,"linalg",function(){return Ho}),n.d(t,"losses",function(){return Wo}),n.d(t,"op",function(){return Ye}),n.d(t,"batchNormalization2d",function(){return Ua}),n.d(t,"batchNormalization3d",function(){return Wa}),n.d(t,"batchNormalization4d",function(){return qa}),n.d(t,"batchNormalization",function(){return $a}),n.d(t,"complex",function(){return Ze}),n.d(t,"real",function(){return Qe}),n.d(t,"imag",function(){return et}),n.d(t,"concat",function(){return gr}),n.d(t,"concat1d",function(){return yr}),n.d(t,"concat2d",function(){return vr}),n.d(t,"concat3d",function(){return br}),n.d(t,"concat4d",function(){return wr}),n.d(t,"split",function(){return xr}),n.d(t,"conv1d",function(){return ei}),n.d(t,"conv2d",function(){return ti}),n.d(t,"depthwiseConv2d",function(){return ni}),n.d(t,"separableConv2d",function(){return ri}),n.d(t,"conv2dTranspose",function(){return ai}),n.d(t,"matMul",function(){return ii}),n.d(t,"dot",function(){return oi}),n.d(t,"outerProduct",function(){return si}),n.d(t,"reverse",function(){return ui}),n.d(t,"reverse1d",function(){return li}),n.d(t,"reverse2d",function(){return ci}),n.d(t,"reverse3d",function(){return fi}),n.d(t,"reverse4d",function(){return pi}),n.d(t,"maxPool",function(){return hi}),n.d(t,"avgPool",function(){return di}),n.d(t,"slice",function(){return mi}),n.d(t,"slice1d",function(){return gi}),n.d(t,"slice2d",function(){return yi}),n.d(t,"slice3d",function(){return vi}),n.d(t,"slice4d",function(){return bi}),n.d(t,"abs",function(){return la}),n.d(t,"acos",function(){return ca}),n.d(t,"acosh",function(){return fa}),n.d(t,"asin",function(){return pa}),n.d(t,"asinh",function(){return ha}),n.d(t,"atan",function(){return da}),n.d(t,"atanh",function(){return ma}),n.d(t,"ceil",function(){return ga}),n.d(t,"clipByValue",function(){return ya}),n.d(t,"cos",function(){return va}),n.d(t,"cosh",function(){return ba}),n.d(t,"erf",function(){return wa}),n.d(t,"exp",function(){return xa}),n.d(t,"expm1",function(){return _a}),n.d(t,"floor",function(){return Sa}),n.d(t,"log",function(){return Ea}),n.d(t,"log1p",function(){return Na}),n.d(t,"logSigmoid",function(){return Oa}),n.d(t,"neg",function(){return ka}),n.d(t,"reciprocal",function(){return Ia}),n.d(t,"round",function(){return Ca}),n.d(t,"rsqrt",function(){return Aa}),n.d(t,"sigmoid",function(){return Pa}),n.d(t,"sign",function(){return Ta}),n.d(t,"sin",function(){return Ma}),n.d(t,"sinh",function(){return Ra}),n.d(t,"softplus",function(){return Da}),n.d(t,"sqrt",function(){return ja}),n.d(t,"square",function(){return La}),n.d(t,"step",function(){return za}),n.d(t,"tan",function(){return Ba}),n.d(t,"tanh",function(){return Fa}),n.d(t,"all",function(){return wi}),n.d(t,"any",function(){return xi}),n.d(t,"argMax",function(){return _i}),n.d(t,"argMin",function(){return Si}),n.d(t,"logSumExp",function(){return Ei}),n.d(t,"max",function(){return Ni}),n.d(t,"mean",function(){return Oi}),n.d(t,"min",function(){return ki}),n.d(t,"moments",function(){return Ii}),n.d(t,"sum",function(){return Ci}),n.d(t,"equal",function(){return Ai}),n.d(t,"equalStrict",function(){return Pi}),n.d(t,"greater",function(){return Ti}),n.d(t,"greaterEqual",function(){return Mi}),n.d(t,"greaterEqualStrict",function(){return Ri}),n.d(t,"greaterStrict",function(){return Di}),n.d(t,"less",function(){return ji}),n.d(t,"lessEqual",function(){return Li}),n.d(t,"lessEqualStrict",function(){return zi}),n.d(t,"lessStrict",function(){return Bi}),n.d(t,"notEqual",function(){return Fi}),n.d(t,"notEqualStrict",function(){return Vi}),n.d(t,"add",function(){return Ui}),n.d(t,"addN",function(){return Wi}),n.d(t,"addStrict",function(){return qi}),n.d(t,"atan2",function(){return $i}),n.d(t,"div",function(){return Gi}),n.d(t,"divStrict",function(){return Hi}),n.d(t,"floorDiv",function(){return Ki}),n.d(t,"maximum",function(){return Xi}),n.d(t,"maximumStrict",function(){return Yi}),n.d(t,"minimum",function(){return Ji}),n.d(t,"minimumStrict",function(){return Zi}),n.d(t,"mod",function(){return Qi}),n.d(t,"modStrict",function(){return eo}),n.d(t,"mul",function(){return to}),n.d(t,"mulStrict",function(){return no}),n.d(t,"pow",function(){return ro}),n.d(t,"powStrict",function(){return ao}),n.d(t,"squaredDifference",function(){return io}),n.d(t,"squaredDifferenceStrict",function(){return oo}),n.d(t,"sub",function(){return so}),n.d(t,"subStrict",function(){return uo}),n.d(t,"elu",function(){return go}),n.d(t,"leakyRelu",function(){return yo}),n.d(t,"prelu",function(){return vo}),n.d(t,"relu",function(){return bo}),n.d(t,"selu",function(){return wo}),n.d(t,"logicalAnd",function(){return lo}),n.d(t,"logicalNot",function(){return co}),n.d(t,"logicalOr",function(){return fo}),n.d(t,"logicalXor",function(){return po}),n.d(t,"where",function(){return ho}),n.d(t,"whereAsync",function(){return mo}),n.d(t,"buffer",function(){return Mr}),n.d(t,"toPixels",function(){return Tr}),n.d(t,"print",function(){return Rr}),n.d(t,"batchToSpaceND",function(){return Dr}),n.d(t,"cast",function(){return jr}),n.d(t,"clone",function(){return Lr}),n.d(t,"cumsum",function(){return zr}),n.d(t,"depthToSpace",function(){return Br}),n.d(t,"expandDims",function(){return Fr}),n.d(t,"eye",function(){return Vr}),n.d(t,"fromPixels",function(){return Ur}),n.d(t,"multinomial",function(){return Wr}),n.d(t,"oneHot",function(){return qr}),n.d(t,"pad",function(){return $r}),n.d(t,"pad1d",function(){return Gr}),n.d(t,"pad2d",function(){return Hr}),n.d(t,"pad3d",function(){return Kr}),n.d(t,"pad4d",function(){return Xr}),n.d(t,"rand",function(){return Yr}),n.d(t,"randomNormal",function(){return Jr}),n.d(t,"randomUniform",function(){return Zr}),n.d(t,"reshape",function(){return Qr}),n.d(t,"spaceToBatchND",function(){return ea}),n.d(t,"squeeze",function(){return ta}),n.d(t,"stack",function(){return na}),n.d(t,"tile",function(){return ra}),n.d(t,"truncatedNormal",function(){return aa}),n.d(t,"unstack",function(){return ia}),n.d(t,"fill",function(){return ft}),n.d(t,"linspace",function(){return pt}),n.d(t,"ones",function(){return lt}),n.d(t,"range",function(){return ht}),n.d(t,"scalar",function(){return nt}),n.d(t,"tensor",function(){return tt}),n.d(t,"tensor1d",function(){return rt}),n.d(t,"tensor2d",function(){return at}),n.d(t,"tensor3d",function(){return it}),n.d(t,"tensor4d",function(){return ot}),n.d(t,"tensor5d",function(){return st}),n.d(t,"tensor6d",function(){return ut}),n.d(t,"zeros",function(){return ct}),n.d(t,"onesLike",function(){return wt}),n.d(t,"zerosLike",function(){return xt}),n.d(t,"transpose",function(){return xo}),n.d(t,"softmax",function(){return Je}),n.d(t,"localResponseNormalization",function(){return _o}),n.d(t,"norm",function(){return So}),n.d(t,"gather",function(){return ko}),n.d(t,"unsortedSegmentSum",function(){return Io}),n.d(t,"basicLSTMCell",function(){return Co}),n.d(t,"multiRNNCell",function(){return Ao}),n.d(t,"movingAverage",function(){return Po}),n.d(t,"stridedSlice",function(){return To}),n.d(t,"topk",function(){return Mo}),n.d(t,"train",function(){return du}),n.d(t,"tidy",function(){return Ee}),n.d(t,"keep",function(){return Ne}),n.d(t,"dispose",function(){return Oe}),n.d(t,"time",function(){return ke}),n.d(t,"customGrad",function(){return _e}),n.d(t,"grad",function(){return ye}),n.d(t,"grads",function(){return ve}),n.d(t,"valueAndGrad",function(){return be}),n.d(t,"valueAndGrads",function(){return we}),n.d(t,"variableGrads",function(){return xe}); +var a=function(t,e){return(a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)};function s(t,e){function n(){this.constructor=t}a(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var u=function(){return(u=Object.assign||function(t){for(var e,n=1,r=arguments.length;n0&&i[i.length-1])&&(6===o[0]||2===o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]0;)r=Math.random()*e|0,n=t[--e],t[e]=t[r],t[r]=n}function g(t,e,n){return Math.max(t,Math.min(e,n))}function y(t){return t%2==0?t:t+1}function b(t){for(var e=0,n=0;n=n?i():setTimeout(a,s)}};a()})}function T(t,e){for(var n=1,r=-1,i=0;i=0)n*=t[i];else if(-1===t[i]){if(-1!==r)throw Error("Shapes can only have 1 implicit size. Found -1 at dim "+r+" and dim "+i);r=i}else if(t[i]<0)throw Error("Shapes can not be < 0. Found "+t[i]+" at dim "+i);if(-1===r){if(e>0&&e!==n)throw Error("Size("+e+") must match the product of shape "+t);return t}if(0===n)throw Error("Cannot infer the missing size in ["+t+"] when there are 0 elements");if(e%n!=0)throw Error("The implicit shape can't be a fractional number. Got "+e+" / "+n);var o=t.slice();return o[r]=e/n,o}function M(t,e){var n=e.length;return w((t=null==t?e.map(function(t,e){return e}):[].concat(t)).every(function(t){return t>=-n&&ta)&&1===t[a]&&(n.push(t[a]),r.push(a)),i[o]<=a&&o++}1!==t[a]&&(n.push(t[a]),r.push(a))}return{newShape:n,keptDims:r}}function D(t,e){var n=null;if(null==t||"float32"===t)n=new Float32Array(e);else if("int32"===t)n=new Int32Array(e);else{if("bool"!==t)throw new Error("Unknown data type "+t);n=new Uint8Array(e)}return n}function j(t,e){var n=null;if(null==t||"float32"===t)n=new Float32Array(e);else if("int32"===t)n=new Int32Array(e);else if("bool"===t)n=new Uint8Array(e);else{if("string"!==t)throw new Error("Unknown data type "+t);n=new Array(e)}return n}function P(t,e,n){if("float32"===e)for(var r=0;r=0;--r)n[r]=n[r+1]*t[r+1];return n}function $(t,e,n){if("string"===e)throw new Error("Cannot convert a string[] to a TypedArray");if(Array.isArray(t)&&(t=S(t)),n&&z(t,e),function(t,e){return t instanceof Float32Array&&"float32"===e||t instanceof Int32Array&&"int32"===e||t instanceof Uint8Array&&"bool"===e}(t,e))return t;if(null==e||"float32"===e||"complex64"===e)return new Float32Array(t);if("int32"===e)return new Int32Array(t);if("bool"===e){for(var r=new Uint8Array(t.length),i=0;i=0,function(){return"Tensor must have a shape comprised of positive integers but got shape ["+t+"]."})})}var et,nt=function(){if(null!=f.global.fetch)return f.global.fetch;if(f.get("IS_NODE"))return rt.fetchImport();throw new Error("Unable to find the fetch() method. Please add your own fetch() function to the global namespace.")},rt={fetchImport:function(){return n(477)}};function it(t,e){return null==et&&(et=nt()),et(t,e)}var ot=Object.freeze({shuffle:v,clamp:g,nearestLargerEven:y,sum:b,randUniform:function(t,e){var n=Math.random();return e*n+(1-n)*t},distSquared:function(t,e){for(var n=0,r=0;r=this.shape[n]){var a="Requested out of range element at "+t+". Buffer shape="+this.shape;throw new Error(a)}n++}for(var s=t[t.length-1],u=0;u1)for(var c=0;c20){var l=3*s,f=Array.from(e.slice(0,l)),h=Array.from(e.slice(u-3*s,u));return"complex64"===r&&(f=ft(f),h=ft(h)),["["+f.map(function(t,e){return ct(t,o[e],r)}).join(", ")+", ..., "+h.map(function(t,e){return ct(t,o[u-3+e],r)}).join(", ")+"]"]}return["["+("complex64"===r?ft(e):Array.from(e)).map(function(t,e){return ct(t,o[e],r)}).join(", ")+"]"]}var d=n.slice(1),p=i.slice(1),m=i[0]*s,v=[];if(u>20){for(var g=0;g<3;g++){var y=(b=g*m)+m;v.push.apply(v,t(e.slice(b,y),d,r,p,o,!1))}for(v.push("..."),g=u-3;g0&&(t.unreliable=!0,null==t.reasons&&(t.reasons=[]),t.reasons.push("Memory usage by string tensors is approximate (2 bytes per character)")),t},t.prototype.profile=function(t){return c(this,void 0,void 0,function(){var e,n;return l(this,function(r){return this.state.profiling=!0,e=this.state.numBytes,n=this.state.numTensors,this.state.activeProfile.kernels=[],this.state.activeProfile.result=t(),this.state.profiling=!1,this.state.activeProfile.peakBytes=Math.max.apply(Math,this.state.activeProfile.kernels.map(function(t){return t.totalBytesSnapshot})),this.state.activeProfile.newBytes=this.state.numBytes-e,this.state.activeProfile.newTensors=this.state.numTensors-n,[2,this.state.activeProfile]})})},t.prototype.isTapeOn=function(){return this.state.gradientDepth>0&&0===this.state.kernelDepth},t.prototype.addTapeNode=function(t,e,n){var r={};t.forEach(function(t,e){r[e]=t});var i={id:this.state.nextTapeNodeId++,name:this.state.activeScope.name,inputs:r,outputs:[e],gradient:function(t){var e={};return n(t).forEach(function(t,n){e[n]=function(){return t}}),e}};this.state.activeTape.push(i)},t.prototype.keep=function(t){return t.kept=!0,t},t.prototype.startTape=function(){0===this.state.gradientDepth&&(this.state.activeTape=[]),this.state.gradientDepth++},t.prototype.endTape=function(){this.state.gradientDepth--},t.prototype.startScope=function(t){var e={track:[],name:"unnamed scope",id:this.state.nextScopeId++};t&&(e.name=t),this.state.scopeStack.push(e),this.state.activeScope=e},t.prototype.endScope=function(t){for(var e=this,n=Ot(t),r=new Set(n.map(function(t){return t.id})),i=0;i0,function(){return"gradients() received an empty list of xs."}),null!=n&&"float32"!==n.dtype)throw new Error("dy must have 'float32' dtype, but has '"+n.dtype+"'");var o=this.scopedRun(function(){return i.startTape()},function(){return i.endTape()},function(){return i.tidy("forward",t)});w(o instanceof vt,function(){return"The result y returned by f() must be a tensor."});var a=function(t,e,n){for(var r={},i={},o=0;o=0;o--)for(a=(p=t[o]).inputs,l=0;l0)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that the f you passed encloses all operations that lead from x to y.");return this.tidy("backward",function(){var t,r,s={};s[o.id]=null==n?(r=J(_(t=o.shape),"float32"),vt.make(t,{values:r})):n,function(t,e,n){for(var r=function(r){var i=e[r],o=[];if(i.outputs.forEach(function(e){var n=t[e.id];if(null!=n)o.push(n);else{var r=vt.make(e.shape,{values:Z(e.size,e.dtype)},e.dtype);o.push(r)}}),null==i.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+i.name+".");var a=i.gradient(1===i.outputs.length?o[0]:o),s=function(e){if(!(e in a))throw new Error("Cannot backprop through input "+e+". Available gradients found: "+Object.keys(a)+".");var r=n(function(){return a[e]()});if("float32"!==r.dtype)throw new Error("Error in gradient for op "+i.name+". The gradient of input "+e+" must have 'float32' dtype, but has '"+r.dtype+"'");var o=i.inputs[e];if(!E(r.shape,o.shape))throw new Error("Error in gradient for op "+i.name+". The gradient of input '"+e+"' has shape '"+r.shape+"', which does not match the shape of the input '"+o.shape+"'");if(null==t[o.id])t[o.id]=r;else{var s=t[o.id];t[o.id]=s.add(r),s.dispose()}};for(var u in i.inputs)s(u)},i=e.length-1;i>=0;i--)r(i)}(s,a,function(t){return i.tidy(t)});var u=e.map(function(t){return s[t.id]});return 0===i.state.gradientDepth&&(i.state.activeTape.forEach(function(t){for(var e in t.saved)t.saved[e].dispose()}),i.state.activeTape=null),{value:o,grads:u}})},t.prototype.customGrad=function(t){var e=this;return w(G(t),function(){return"The f passed in customGrad(f) must be a function."}),function(){for(var n,r=[],i=0;in||e>n)throw r="["+t+"x"+e+"]",new Error("Requested texture size "+r+" greater than WebGL maximum on this browser / GPU ["+n+"x"+n+"].")}function ie(t,e){return pe(t,e,function(){return t.createFramebuffer()},"Unable to create WebGLFramebuffer.")}function oe(t,e,n,r,i,o,a,s){var u=t.getAttribLocation(n,r);return-1!==u&&(Ft(t,e,function(){return t.bindBuffer(t.ARRAY_BUFFER,i)}),Ft(t,e,function(){return t.vertexAttribPointer(u,o,t.FLOAT,!1,a,s)}),Ft(t,e,function(){return t.enableVertexAttribArray(u)}),!0)}function ae(t,e,n,r){me(t,r),Ft(t,e,function(){return t.activeTexture(t.TEXTURE0+r)}),Ft(t,e,function(){return t.bindTexture(t.TEXTURE_2D,n)})}function se(t,e,n,r){return pe(t,e,function(){return t.getUniformLocation(n,r)},'uniform "'+r+'" not present in program.')}function ue(t,e,n){return t.getUniformLocation(e,n)}function ce(t,e,n,r,i,o){Ft(t,e,function(){return ae(t,e,r,o)}),Ft(t,e,function(){return t.uniform1i(i,o)})}function le(t,e,n,r){Ft(t,e,function(){return t.bindFramebuffer(t.FRAMEBUFFER,r)}),Ft(t,e,function(){return t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,n,0)})}function fe(t,e,n){Ft(t,e,function(){return t.bindFramebuffer(t.FRAMEBUFFER,n)}),Ft(t,e,function(){return t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,null,0)})}function he(t){var e=t.checkFramebufferStatus(t.FRAMEBUFFER);if(e!==t.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+de(t,e))}function de(t,e){switch(e){case t.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_ATTACHMENT";case t.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";case t.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:return"FRAMEBUFFER_INCOMPLETE_DIMENSIONS";case t.FRAMEBUFFER_UNSUPPORTED:return"FRAMEBUFFER_UNSUPPORTED";default:return"unknown error "+e}}function pe(t,e,n,r){var i=Ft(t,e,function(){return n()});if(null==i)throw new Error(r);return i}function me(t,e){var n=t.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,r=e+t.TEXTURE0;if(rn)throw new Error("textureUnit must be in [gl.TEXTURE0, gl.TEXTURE"+n+"].")}function ve(t,e){return void 0===e&&(e=2),_(t.slice(0,t.length-e))}function ge(t){if(0===t.length)throw Error("Cannot get rows and columns of an empty shape array.");return[t.length>1?t[t.length-2]:1,t[t.length-1]]}function ye(t,e){var n;void 0===e&&(e=!1);var r=f.getNumber("WEBGL_MAX_TEXTURE_SIZE");if(e&&(r*=2,1===(t=t.map(function(e,n){return n>=t.length-2?y(t[n]):t[n]})).length&&(t=[2,t[0]])),2!==t.length){var i=R(t);t=i.newShape}var o=_(t);if(t.length<=1&&o<=r)return[1,o];if(2===t.length&&t[0]<=r&&t[1]<=r)return t;if(3===t.length&&t[0]*t[1]<=r&&t[2]<=r)return[t[0]*t[1],t[2]];if(3===t.length&&t[0]<=r&&t[1]*t[2]<=r)return[t[0],t[1]*t[2]];if(4===t.length&&t[0]*t[1]*t[2]<=r&&t[3]<=r)return[t[0]*t[1]*t[2],t[3]];if(4===t.length&&t[0]<=r&&t[1]*t[2]*t[3]<=r)return[t[0],t[1]*t[2]*t[3]];if(e){var a=ve(t),s=2,u=2;return t.length&&(s=(n=ge(t))[0],u=n[1]),A(o=a*(s/2)*(u/2)).map(function(t){return 2*t})}return A(o)}function be(t){return t%2==0}function we(t,e){if(E(t=t.slice(-2),e=e.slice(-2)))return!0;if(!t.length||!e.length)return!0;if(0===t[0]||0===t[1]||0===e[0]||0===e[1])return!0;if(t.length!==e.length){var n=t.slice(-1)[0],r=e.slice(-1)[0];if(n===r)return!0;if(be(n)&&be(r)&&(1===t[0]||1===e[0]))return!0}return t[1]===e[1]&&be(t[0])&&be(e[0])}function xe(t){if(null==Kt){var e=Lt(t);Kt=e.getParameter(e.MAX_TEXTURE_SIZE)}return Kt}function ke(t){if(null==Xt){var e=Lt(t);Xt=e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS)}return Math.min(16,Xt)}function Se(t){if(0===t)return 0;var e=Lt(t);return _e(e,"EXT_disjoint_timer_query_webgl2")&&2===t?2:_e(e,"EXT_disjoint_timer_query")?1:0}function _e(t,e){return null!=t.getExtension(e)}function Ee(t){try{if(null!=Lt(t))return!0}catch(t){return!1}return!1}function Ce(t){if(0===t)return!1;var e=Lt(t);if(1===t){if(!_e(e,"OES_texture_float"))return!1}else if(!_e(e,"EXT_color_buffer_float"))return!1;return Ae(e,t)}function Ne(t){if(0===t)return!1;var e=Lt(t);if(1===t){if(!_e(e,"OES_texture_float"))return!1;if(!_e(e,"WEBGL_color_buffer_float"))return!1}else if(!_e(e,"EXT_color_buffer_float"))return!1;return Ae(e,t)}function Ae(t,e){var n=t.createFramebuffer(),r=t.createTexture();t.bindTexture(t.TEXTURE_2D,r);var i=2===e?t.RGBA32F:t.RGBA;t.texImage2D(t.TEXTURE_2D,0,i,1,1,0,t.RGBA,t.FLOAT,null),t.bindFramebuffer(t.FRAMEBUFFER,n),t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,r,0);var o=t.checkFramebufferStatus(t.FRAMEBUFFER)===t.FRAMEBUFFER_COMPLETE;return t.bindTexture(t.TEXTURE_2D,null),t.bindFramebuffer(t.FRAMEBUFFER,null),t.deleteTexture(r),t.deleteFramebuffer(n),o}function Oe(t){return 2===t&&null!=Lt(t).fenceSync}var Ie=Object.freeze({callAndCheck:Ft,canBeRepresented:Vt,getWebGLErrorMessage:Wt,getExtensionOrThrow:qt,createVertexShader:Ht,createFragmentShader:Gt,createProgram:Yt,linkProgram:Jt,validateProgram:Zt,createStaticVertexBuffer:Qt,createStaticIndexBuffer:te,getNumChannels:ee,createTexture:ne,validateTextureSize:re,createFramebuffer:ie,bindVertexBufferToProgramAttribute:oe,bindTextureUnit:ae,unbindTextureUnit:function(t,e,n){me(t,n),Ft(t,e,function(){return t.activeTexture(t.TEXTURE0+n)}),Ft(t,e,function(){return t.bindTexture(t.TEXTURE_2D,null)})},getProgramUniformLocationOrThrow:se,getProgramUniformLocation:ue,bindTextureToProgramUniformSampler:ce,bindCanvasToFramebuffer:function(t,e){Ft(t,e,function(){return t.bindFramebuffer(t.FRAMEBUFFER,null)}),Ft(t,e,function(){return t.viewport(0,0,t.canvas.width,t.canvas.height)}),Ft(t,e,function(){return t.scissor(0,0,t.canvas.width,t.canvas.height)})},bindColorTextureToFramebuffer:le,unbindColorTextureFromFramebuffer:fe,validateFramebuffer:he,getFramebufferErrorMessage:de,getBatchDim:ve,getRowsCols:ge,getTextureShapeFromLogicalShape:ye,isReshapeFree:we,get MAX_TEXTURE_SIZE(){return Kt},get MAX_TEXTURES_IN_SHADER(){return Xt},getWebGLMaxTextureSize:xe,getMaxTexturesInShader:ke,getWebGLDisjointQueryTimerVersion:Se,isWebGLVersionEnabled:Ee,isRenderToFloatTextureEnabled:Ce,isDownloadFloatTextureEnabled:Ne,isWebGLFenceEnabled:Oe});function Te(){f.set("PROD",!0)}function Me(){f.set("DEBUG",!0)}function Re(){f.set("DEPRECATION_WARNINGS_ENABLED",!1),console.warn("TensorFlow.js deprecation warnings have been disabled.")}function De(t){f.getBool("DEPRECATION_WARNINGS_ENABLED")&&console.warn(t+" You can disable deprecation warnings with tf.disableDeprecationWarnings().")}function je(){Dt.disposeVariables()}function Pe(){return Dt.memory()}function ze(t){return Dt.profile(t)}function Le(t,e){return Dt.tidy(t,e)}function Fe(t){Ot(t).forEach(function(t){return t.dispose()})}function Be(t){return Dt.keep(t)}function Ue(t){return Dt.time(t)}function Ve(t){return Dt.setBackend(t)}function We(){return Dt.ready()}function qe(){return Dt.backendName}function He(t){Dt.removeBackend(t)}function Ge(t){return Dt.findBackend(t)}function Ke(t){return Dt.findBackendFactory(t)}function Xe(t,e,n){return void 0===n&&(n=1),Dt.registerBackend(t,e,n)}function $e(){return Dt.backend}function Ye(){for(var t=[],e=0;e=2*e+1||i%2==1?a.push(i):o.push(i);r.push.apply(r,o),r.push(0),r.push.apply(r,a)}return r}function Qe(t,e,n,r){void 0===r&&(r=!0);var i=[];r?i.push(t[0]/n):i.push(t[0]*n);for(var o=1;ot.rank)throw new Error("index innermost dimension length must be <= tensor rank; saw: "+e.shape[e.rank-1]+" vs. "+t.rank);if(0===t.size)throw new Error("Requested more than 0 entries, but input is empty. Input shape: "+t.shape+".");for(var n=e.shape,r=n[n.length-1],i=1,o=0;o0}),f.registerFlag("WEBGL_VERSION",function(){return Ee(2)?2:Ee(1)?1:0}),f.registerFlag("WEBGL_BUFFER_SUPPORTED",function(){return 2===f.get("WEBGL_VERSION")}),f.registerFlag("WEBGL_CPU_FORWARD",function(){return!1}),f.registerFlag("WEBGL_PACK",function(){return f.getBool("HAS_WEBGL")}),f.registerFlag("WEBGL_PACK_NORMALIZATION",function(){return f.getBool("WEBGL_PACK")}),f.registerFlag("WEBGL_PACK_CLIP",function(){return f.getBool("WEBGL_PACK")}),f.registerFlag("WEBGL_PACK_DEPTHWISECONV",function(){return f.getBool("WEBGL_PACK")}),f.registerFlag("WEBGL_PACK_BINARY_OPERATIONS",function(){return f.getBool("WEBGL_PACK")}),f.registerFlag("WEBGL_PACK_ARRAY_OPERATIONS",function(){return f.getBool("WEBGL_PACK")}),f.registerFlag("WEBGL_PACK_IMAGE_OPERATIONS",function(){return f.getBool("WEBGL_PACK")}),f.registerFlag("WEBGL_PACK_REDUCE",function(){return f.getBool("WEBGL_PACK")}),f.registerFlag("WEBGL_LAZILY_UNPACK",function(){return f.getBool("WEBGL_PACK")}),f.registerFlag("WEBGL_CONV_IM2COL",function(){return f.getBool("WEBGL_PACK")}),f.registerFlag("WEBGL_MAX_TEXTURE_SIZE",function(){return xe(f.getNumber("WEBGL_VERSION"))}),f.registerFlag("WEBGL_MAX_TEXTURES_IN_SHADER",function(){return ke(f.getNumber("WEBGL_VERSION"))}),f.registerFlag("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION",function(){var t=f.getNumber("WEBGL_VERSION");return 0===t?0:Se(t)}),f.registerFlag("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE",function(){return f.getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0&&(t=navigator.userAgent||navigator.vendor||window.opera,!(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(t)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(t.substr(0,4))));var t}),f.registerFlag("WEBGL_RENDER_FLOAT32_ENABLED",function(){return Ce(f.getNumber("WEBGL_VERSION"))}),f.registerFlag("WEBGL_DOWNLOAD_FLOAT_ENABLED",function(){return Ne(f.getNumber("WEBGL_VERSION"))}),f.registerFlag("WEBGL_FENCE_API_ENABLED",function(){return Oe(f.getNumber("WEBGL_VERSION"))}),f.registerFlag("WEBGL_SIZE_UPLOAD_UNIFORM",function(){return f.getBool("WEBGL_RENDER_FLOAT32_ENABLED")?4:0}),mt=De;var hn=30;function dn(t){return t<=hn?t:K(t,Math.floor(Math.sqrt(t)))}function pn(t,e,n){for(var r=e.rank>1?e.shape[e.rank-1]:1,i=n.length,o=1,a=r;a0?o>=l[e]:o<=l[e]);o+=i)n+=1;return n}),[c,d,f]}function vn(t,e,n,r,i){var o=e[i],a=n[i]||1;(t&1<0?Number.MIN_SAFE_INTEGER:Number.MAX_SAFE_INTEGER);var s=r[i];return o<0&&(o+=s),g(0,o,s-1)}function gn(t,e,n,r,i){var o=e[i],a=n[i]||1;(t&1<0?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER);var s=r[i];return o<0&&(o+=s),a>0?g(0,o,s):g(-1,o,s-1)}function yn(t,e,n){for(var r=n.length,i=0;i1){r=i;break}for(i=r+1;i0||n[i]!==t[i])return!1;return!0}function bn(t,e){for(var n=t.length>0?t[t.length-1]:1,r=0;r0,function(){return"Element arr["+r.join("][")+"] should be a primitive, but is an array of "+e.length+" elements"}),w(e.length===n[0],function(){return"Element arr["+r.join("][")+"] should have "+n[0]+" elements, but has "+e.length+" elements"});for(var i=n.slice(1),o=0;o=0&&(i=r),xn(r,i,e,n),null==t||!F(t)&&!Array.isArray(t)&&"number"!=typeof t&&"boolean"!=typeof t&&"string"!=typeof t){var o=null==t?"null":t.constructor.name;throw new Error("Argument '"+e+"' passed to '"+n+"' must be a Tensor or TensorLike, but got '"+o+"'")}var a=wn(t);F(t)||Array.isArray(t)||(t=[t]);var s="string"!==i?$(t,i,f.getBool("DEBUG")):S(t);return vt.make(a,{values:s},i)}function Sn(t,e,n,r){if(void 0===r&&(r="numeric"),!Array.isArray(t))throw new Error("Argument "+e+" passed to "+n+" must be a `Tensor[]` or `TensorLike[]`");return t.map(function(t,r){return kn(t,e+"["+r+"]",n)},r)}function _n(t){return w(G(t),function(){return"The f passed in grad(f) must be a function"}),function(e,n){var r=kn(e,"x","tf.grad",null),i=null!=n?kn(n,"dy","tf.grad"):null;return Dt.tidy(function(){var e=Dt.gradients(function(){return t(r)},[r],i),n=e.value,o=e.grads;return null!=i&&x(n.shape,i.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),In(o),o[0]})}}function En(t){return w(G(t),function(){return"The f passed in grads(f) must be a function"}),function(e,n){w(Array.isArray(e),function(){return"The args passed in grads(f)(args) must be an array of `Tensor`s or `TensorLike`s"});var r=Sn(e,"args","tf.grads",null),i=null!=n?kn(n,"dy","tf.grads"):null;return Dt.tidy(function(){var e=Dt.gradients(function(){return t.apply(void 0,r)},r,i),n=e.value,o=e.grads;return null!=i&&x(n.shape,i.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),In(o),o})}}function Cn(t){return w(G(t),function(){return"The f passed in valueAndGrad(f) must be a function"}),function(e,n){w(e instanceof vt,function(){return"The x passed in valueAndGrad(f)(x) must be a tensor"}),w(null==n||n instanceof vt,function(){return"The dy passed in valueAndGrad(f)(x, dy) must be a tensor"});var r=Dt.gradients(function(){return t(e)},[e],n),i=r.grads,o=r.value;return In(i),{grad:i[0],value:o}}}function Nn(t){return w(G(t),function(){return"The f passed in valueAndGrads(f) must be a function"}),function(e,n){w(Array.isArray(e)&&e.every(function(t){return t instanceof vt}),function(){return"The args passed in valueAndGrads(f)(args) must be array of tensors"}),w(null==n||n instanceof vt,function(){return"The dy passed in valueAndGrads(f)(args, dy) must be a tensor"});var r=Dt.gradients(function(){return t.apply(void 0,e)},e,n);return null!=n&&x(r.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),In(r.grads),r}}function An(t,e){if(w(G(t),function(){return"The f passed in variableGrads(f) must be a function"}),w(null==e||Array.isArray(e)&&e.every(function(t){return t instanceof gt}),function(){return"The varList passed in variableGrads(f, varList) must be an array of variables"}),null==e)for(var n in e=[],Dt.registeredVariables)e.push(Dt.registeredVariables[n]);var r=e.length;w((e=e.filter(function(t){return t.trainable})).length>0,function(){return"variableGrads() expects at least one of the input variables to be trainable, but none of the "+r+" variables is trainable."});var i=Dt.gradients(t,e,null,!0),o=i.value,a=i.grads;w(a.some(function(t){return null!=t}),function(){return"Cannot find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize()."}),w(0===o.rank,function(){return"The f passed in variableGrads(f) must return a scalar, but it returned a rank-"+o.rank+" tensor"});var s={};return e.forEach(function(t,e){null!=a[e]&&(s[t.name]=a[e])}),{value:o,grads:s}}function On(t){return Dt.customGrad(t)}function In(t){if(t.filter(function(t){return null==t}).length>0)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that\n the f you passed encloses all operations that lead from x to y.")}function Tn(t){var e=Object.keys(t);if(1!==e.length)throw new Error("Please provide an object with a single key (operation name) mapping to a function. Got an object with "+e.length+" keys.");var n=e[0],r=t[n];n.endsWith("_")&&(n=n.substring(0,n.length-1));var i=function(){for(var t=[],e=0;e1)return Gn([0],r);var i=Z(Math.abs(Math.ceil((e-t)/n)),r);ei}).sort(function(t,e){return e.score-t.score}),a=[],s=0;s=0;--h)if(ur(t,l,a[h])>=r){f=!0;break}if(!f&&(a.push(l),a.length>=n))break}return Fn(a,"int32")}function ur(t,e,n){var r=t.subarray(4*e,4*e+4),i=t.subarray(4*n,4*n+4),o=Math.min(r[0],r[2]),a=Math.min(r[1],r[3]),s=Math.max(r[0],r[2]),u=Math.max(r[1],r[3]),c=Math.min(i[0],i[2]),l=Math.min(i[1],i[3]),f=Math.max(i[0],i[2]),h=Math.max(i[1],i[3]),d=(s-o)*(u-a),p=(f-c)*(h-l);if(d<=0||p<=0)return 0;var m=Math.max(o,c),v=Math.max(a,l),g=Math.min(s,f),y=Math.min(u,h),b=Math.max(g-m,0)*Math.max(y-v,0);return b/(d+p-b)}function cr(t,e,n){var r=new Array(t.rank).fill(0),i=t.shape.slice();return e.map(function(e){i[n]=e;var o=t.slice(r,i);return r[n]+=e,o})}function lr(t,e,n,r,i){for(var o=e[e.length-1],a=[t.length/o,o],s=a[0],u=a[1],c=D(n,s*r),l=D("int32",s*r),f=0;f=1,function(){return"Pass at least one tensor to concat"});var n=Sn(t,"tensors","concat");e=M(e,n[0].shape)[0];var r=ln(n.map(function(t){return t.shape}),e);if(0===_(r))return zn([],r);if(1===(n=n.filter(function(t){return t.size>0})).length)return n[0];var i=n.map(function(t){return t.shape});!function(t,e){var n=t[0].length;t.forEach(function(t,e){w(t.length===n,function(){return"Error in concat"+n+"D: rank of tensors["+e+"] must be the same as the rank of the rest ("+n+")"})}),w(e>=0&&e>>0,e=(r*=e)>>>0,e+=4294967296*(r-=e)}return 2.3283064365386963e-10*(e>>>0)});n.next=function(){var t=2091639*n.s0+2.3283064365386963e-10*n.c;return n.s0=n.s1,n.s1=n.s2,n.s2=t-(n.c=0|t)},n.c=1,n.s0=r(" "),n.s1=r(" "),n.s2=r(" "),n.s0-=r(t),n.s0<0&&(n.s0+=1),n.s1-=r(t),n.s1<0&&(n.s1+=1),n.s2-=r(t),n.s2<0&&(n.s2+=1),r=null}(t),i=e&&e.state,o=n.next;return o.int32=function(){return 4294967296*n.next()|0},o.double=function(){return o()+1.1102230246251565e-16*(2097152*o()|0)},o.quick=o,i&&("object"==typeof i&&r(i,n),o.state=function(){return r(n,{})}),o}e&&e.exports?e.exports=i:this.alea=i}(0,t)}),br=gr(function(t){!function(t,e,n){function r(t,e){return e.x=t.x,e.y=t.y,e.z=t.z,e.w=t.w,e}function i(t,e){var n=new function(t){var e=this,n="";e.x=0,e.y=0,e.z=0,e.w=0,e.next=function(){var t=e.x^e.x<<11;return e.x=e.y,e.y=e.z,e.z=e.w,e.w^=e.w>>>19^t^t>>>8},t===(0|t)?e.x=t:n+=t;for(var r=0;r>>0)/4294967296};return o.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},o.int32=n.next,o.quick=o,i&&("object"==typeof i&&r(i,n),o.state=function(){return r(n,{})}),o}e&&e.exports?e.exports=i:this.xor128=i}(0,t)}),wr=gr(function(t){!function(t,e,n){function r(t,e){return e.x=t.x,e.y=t.y,e.z=t.z,e.w=t.w,e.v=t.v,e.d=t.d,e}function i(t,e){var n=new function(t){var e=this,n="";e.next=function(){var t=e.x^e.x>>>2;return e.x=e.y,e.y=e.z,e.z=e.w,e.w=e.v,(e.d=e.d+362437|0)+(e.v=e.v^e.v<<4^t^t<<1)|0},e.x=0,e.y=0,e.z=0,e.w=0,e.v=0,t===(0|t)?e.x=t:n+=t;for(var r=0;r>>4),e.next()}(t),i=e&&e.state,o=function(){return(n.next()>>>0)/4294967296};return o.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},o.int32=n.next,o.quick=o,i&&("object"==typeof i&&r(i,n),o.state=function(){return r(n,{})}),o}e&&e.exports?e.exports=i:this.xorwow=i}(0,t)}),xr=gr(function(t){!function(t,e,n){function r(t,e){return e.x=t.x.slice(),e.i=t.i,e}function i(t,e){null==t&&(t=+new Date);var n=new function(t){var e=this;e.next=function(){var t,n,r=e.x,i=e.i;return t=r[i],n=(t^=t>>>7)^t<<24,n^=(t=r[i+1&7])^t>>>10,n^=(t=r[i+3&7])^t>>>3,n^=(t=r[i+4&7])^t<<7,t=r[i+7&7],n^=(t^=t<<13)^t<<9,r[i]=n,e.i=i+1&7,n},function(t,e){var n,r=[];if(e===(0|e))r[0]=e;else for(e=""+e,n=0;n0;--n)t.next()}(e,t)}(t),i=e&&e.state,o=function(){return(n.next()>>>0)/4294967296};return o.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},o.int32=n.next,o.quick=o,i&&(i.x&&r(i,n),o.state=function(){return r(n,{})}),o}e&&e.exports?e.exports=i:this.xorshift7=i}(0,t)}),kr=gr(function(t){!function(t,e,n){function r(t,e){return e.i=t.i,e.w=t.w,e.X=t.X.slice(),e}function i(t,e){null==t&&(t=+new Date);var n=new function(t){var e=this;e.next=function(){var t,n,r=e.w,i=e.X,o=e.i;return e.w=r=r+1640531527|0,n=i[o+34&127],t=i[o=o+1&127],n^=n<<13,t^=t<<17,n^=n>>>15,t^=t>>>12,n=i[o]=n^t,e.i=o,n+(r^r>>>16)|0},function(t,e){var n,r,i,o,a,s=[],u=128;for(e===(0|e)?(r=e,e=null):(e+="\0",r=0,u=Math.max(u,e.length)),i=0,o=-32;o>>15,r^=r<<4,r^=r>>>13,o>=0&&(a=a+1640531527|0,i=0==(n=s[127&o]^=r+a)?i+1:0);for(i>=128&&(s[127&(e&&e.length||0)]=-1),i=127,o=512;o>0;--o)r=s[i+34&127],n=s[i=i+1&127],r^=r<<13,n^=n<<17,r^=r>>>15,n^=n>>>12,s[i]=r^n;t.w=a,t.X=s,t.i=i}(e,t)}(t),i=e&&e.state,o=function(){return(n.next()>>>0)/4294967296};return o.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},o.int32=n.next,o.quick=o,i&&(i.X&&r(i,n),o.state=function(){return r(n,{})}),o}e&&e.exports?e.exports=i:this.xor4096=i}(0,t)}),Sr=gr(function(t){!function(t,e,n){function r(t,e){return e.a=t.a,e.b=t.b,e.c=t.c,e.d=t.d,e}function i(t,e){var n=new function(t){var e=this,n="";e.next=function(){var t=e.b,n=e.c,r=e.d,i=e.a;return t=t<<25^t>>>7^n,n=n-r|0,r=r<<24^r>>>8^i,i=i-t|0,e.b=t=t<<20^t>>>12^n,e.c=n=n-r|0,e.d=r<<16^n>>>16^i,e.a=i-t|0},e.a=0,e.b=0,e.c=-1640531527,e.d=1367130551,t===Math.floor(t)?(e.a=t/4294967296|0,e.b=0|t):n+=t;for(var r=0;r>>0)/4294967296};return o.double=function(){do{var t=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===t);return t},o.int32=n.next,o.quick=o,i&&("object"==typeof i&&r(i,n),o.state=function(){return r(n,{})}),o}e&&e.exports?e.exports=i:this.tychei=i}(0,t)}),_r=gr(function(t){!function(e,r){var i,o=this,a=256,s=6,u="random",c=r.pow(a,s),l=r.pow(2,52),f=2*l,h=a-1;function d(t,n,h){var d=[],y=v(function t(e,n){var r,i=[],o=typeof e;if(n&&"object"==o)for(r in e)try{i.push(t(e[r],n-1))}catch(t){}return i.length?i:"string"==o?e:e+"\0"}((n=1==n?{entropy:!0}:n||{}).entropy?[t,g(e)]:null==t?function(){try{var t;return i&&(t=i.randomBytes)?t=t(a):(t=new Uint8Array(a),(o.crypto||o.msCrypto).getRandomValues(t)),g(t)}catch(t){var n=o.navigator,r=n&&n.plugins;return[+new Date,o,r,o.screen,g(e)]}}():t,3),d),b=new p(d),w=function(){for(var t=b.g(s),e=c,n=0;t=f;)t/=2,e/=2,n>>>=1;return(t+n)/e};return w.int32=function(){return 0|b.g(4)},w.quick=function(){return b.g(4)/4294967296},w.double=w,v(g(b.S),e),(n.pass||h||function(t,e,n,i){return i&&(i.S&&m(i,b),t.state=function(){return m(b,{})}),n?(r[u]=t,e):t})(w,y,"global"in n?n.global:this==r,n.state)}function p(t){var e,n=t.length,r=this,i=0,o=r.i=r.j=0,s=r.S=[];for(n||(t=[n++]);i=1||0===a);var s=Math.sqrt(-2*Math.log(a)/a);e=this.mean+this.stdDev*i*s,n=this.mean+this.stdDev*o*s,this.truncated&&!this.isValidTruncated(e)||(r=!0)}return this.truncated&&!this.isValidTruncated(n)||(this.nextVal=this.convertValue(n)),this.convertValue(e)},t.prototype.convertValue=function(t){return null==this.dtype||"float32"===this.dtype?t:Math.round(t)},t.prototype.isValidTruncated=function(t){return t<=this.upper&&t>=this.lower},t}(),Nr=function(){function t(t,e,n,r){void 0===t&&(t=0),void 0===e&&(e=1),void 0===r&&(r=Math.random());var i=this;if(this.canReturnFloat=function(){return null==i.dtype||"float32"===i.dtype},this.min=t,this.range=e-t,this.dtype=n,!this.canReturnFloat()&&this.range<=1)throw new Error("The difference between "+t+" - "+e+" <= 1 and dtype is not float");this.random=Er(r.toString())}return t.prototype.convertValue=function(t){return this.canReturnFloat()?t:Math.round(t)},t.prototype.nextValue=function(){return this.convertValue(this.min+this.range*this.random())},t}();function Ar(t,e,n){return void 0===e&&(e="float32"),e=e||"float32",tt(t),new ht(t,e,n)}function Or(t,e){void 0===e&&(e=!1),console.log(t.toString(e))}var Ir=Tn({batchToSpaceND_:function(t,e,n){var r=kn(t,"x","batchToSpaceND"),i=e.reduce(function(t,e){return t*e});return w(r.rank>=1+e.length,function(){return"input rank is "+r.rank+" but should be > than blockShape.length "+e.length}),w(n.length===e.length,function(){return"crops.length is "+n.length+" but should be equal to blockShape.length "+e.length}),w(r.shape[0]%i==0,function(){return"input tensor batch is "+r.shape[0]+" but is not divisible by the product of the elements of blockShape "+e.join(" * ")+" === "+i}),Dt.runKernel(function(t){return t.batchToSpaceND(r,e,n)},{$x:r},function(t){return{$x:function(){return t.spaceToBatchND(e,n)}}})}}),Tr=Tn({cast_:function(t,e){var n=kn(t,"x","cast");return Dt.runKernel(function(t){return t.cast(n,e)},{$x:n},function(t){return{$x:function(){return t.clone()}}})}}),Mr=Tn({clone_:function(t){var e=kn(t,"x","clone",null);return Dt.runKernel(function(t){return vt.make(e.shape,{dataId:e.dataId},e.dtype)},{$x:e},function(t){return{$x:function(){return t.toFloat()}}})}}),Rr=Tn({cumsum_:function(t,e,n,r){void 0===e&&(e=0),void 0===n&&(n=!1),void 0===r&&(r=!1);var i=kn(t,"x","cumsum"),o=sn([e|=0],i.rank),a=i;null!=o&&(a=i.transpose(o));var s=cn(1,i.rank)[0],u=Dt.runKernel(function(t){return t.cumsum(a,s,n,r)},{permutedX:a},function(t){return{permutedX:function(){return t.cumsum(e,n,!r)}}});return null!=o&&(u=u.transpose(o)),u}}),Dr=Tn({depthToSpace_:function(t,e,n){void 0===n&&(n="NHWC");var r=kn(t,"x","depthToSpace"),i="NHWC"===n?r.shape[1]:r.shape[2],o="NHWC"===n?r.shape[2]:r.shape[3],a="NHWC"===n?r.shape[3]:r.shape[1];return w(i*e>=0,function(){return"Negative dimension size caused by overflow when multiplying\n "+i+" and "+e+" for depthToSpace with input shape\n "+r.shape}),w(o*e>=0,function(){return"Negative dimension size caused by overflow when multiplying\n "+o+" and "+e+" for depthToSpace with input shape\n "+r.shape}),w(a%(e*e)==0,function(){return"Dimension size must be evenly divisible by "+e*e+" but is "+a+" for depthToSpace with input shape "+r.shape}),Dt.runKernel(function(t){return t.depthToSpace(r,e,n)},{$x:r})}}),jr=Tn({expandDims_:function(t,e){void 0===e&&(e=0);var n=kn(t,"x","expandDims");w(e<=n.rank,function(){return"Axis must be <= rank of the tensor"});var r=n.shape.slice();return e<0&&(w(-(n.rank+1)<=e,function(){return"Axis must be in the interval ["+-(n.rank+1)+", "+n.rank+"]"}),e=n.rank+e+1),r.splice(e,0,1),Kr(n,r)}}),Pr=Tn({eye_:function(t,e,n,r){void 0===r&&(r="float32"),null==e&&(e=t);for(var i=Ar([t,e],r),o=t<=e?t:e,a=0;a2)throw new Error("Rank of probabilities must be 1 or 2, but is "+a);n=n||Math.random();var s=1===a?i.as2D(1,-1):i,u=Dt.runKernel(function(t){return t.multinomial(s,r,e,n)},{logits2D:s});return 1===a?u.as1D():u}}),Lr=Tn({oneHot_:function(t,e,n,r){if(void 0===n&&(n=1),void 0===r&&(r=0),e<2)throw new Error("Error in oneHot: depth must be >=2, but it is "+e);var i=kn(t,"indices","oneHot","int32"),o=i.shape.concat([e]);return i=i.flatten(),Dt.runKernel(function(t){return t.oneHot(i,e,n,r)},{$indices:i},function(t){return{$indices:function(){return Gn(i.shape,"float32")}}}).reshape(o)}}),Fr=Tn({pad_:function(t,e,n){void 0===n&&(n=0);var r=kn(t,"x","pad");if(0===r.rank)throw new Error("pad(scalar) is not defined. Pass non-scalar to pad");var i=e.map(function(t){return t[0]});return Dt.runKernel(function(t){return t.pad(r,e,n)},{$x:r},function(t){return{$x:function(){return t.slice(i,r.shape)}}})}}),Br=Tn({pad1d_:function(t,e,n){return void 0===n&&(n=0),w(2===e.length,function(){return"Invalid number of paddings. Must be length of 2."}),Fr(t,[e],n)}}),Ur=Tn({pad2d_:function(t,e,n){return void 0===n&&(n=0),w(2===e.length&&2===e[0].length&&2===e[1].length,function(){return"Invalid number of paddings. Must be length of 2 each."}),Fr(t,e,n)}}),Vr=Tn({pad3d_:function(t,e,n){return void 0===n&&(n=0),w(3===e.length&&2===e[0].length&&2===e[1].length&&2===e[2].length,function(){return"Invalid number of paddings. Must be length of 2 each."}),Fr(t,e,n)}}),Wr=Tn({pad4d_:function(t,e,n){return void 0===n&&(n=0),w(4===e.length&&2===e[0].length&&2===e[1].length&&2===e[2].length&&2===e[3].length,function(){return"Invalid number of paddings. Must be length of 2 each."}),Fr(t,e,n)}}),qr=Tn({rand_:function(t,e,n){var r=_(t),i=null;if(null==n||"float32"===n)i=new Float32Array(r);else if("int32"===n)i=new Int32Array(r);else{if("bool"!==n)throw new Error("Unknown data type "+n);i=new Uint8Array(r)}for(var o=0;o=1+e.length,function(){return"input rank "+r.rank+" should be > than [blockShape] "+e.length}),w(n.length===e.length,function(){return"paddings.shape[0] "+n.length+" must be equal to [blockShape] "+e.length}),w(r.shape.reduce(function(t,r,i){return i>0&&i<=e.length?t&&(r+n[i-1][0]+n[i-1][1])%e[i-1]==0:t},!0),function(){return"input spatial dimensions "+r.shape.slice(1)+" with paddings "+n.toString()+" must be divisible by blockShapes "+e.toString()}),Dt.runKernel(function(t){return t.spaceToBatchND(r,e,n)},{$x:r},function(t){return{$x:function(){return t.batchToSpaceND(e,n)}}})}}),$r=Tn({squeeze_:function(t,e){var n=kn(t,"x","squeeze");return Kr(n,R(n.shape,e).newShape)}}),Yr=Tn({stack_:function(t,e){void 0===e&&(e=0);var n=Sn(t,"tensors","stack");if(w(n.length>=1,function(){return"Pass at least one tensor to tf.stack"}),1===n.length)return n[0].expandDims(e);var r=n[0].rank,i=n[0].shape,o=n[0].dtype;w(e<=r,function(){return"Axis must be <= rank of the tensor"}),n.forEach(function(t){x(i,t.shape,"All tensors passed to stack must have matching shapes")}),n.forEach(function(t){w(o===t.dtype,function(){return"All tensors passed to stack must have matching dtypes"})});var a=n.map(function(t){return t.expandDims(e)});return fr(a,e)}}),Jr=Tn({tile_:function(t,e){var n=kn(t,"x","tile");return w(n.rank===e.length,function(){return"Error in transpose: rank of input "+n.rank+" must match length of reps "+e+"."}),Dt.runKernel(function(t,r){var i=t.tile(n,e);return r([n]),i},{$x:n},function(t,n){var r=n[0];return{$x:function(){var n=Jn(r);if(1===r.rank)for(var i=0;i=-n.shape.length&&e1&&1===a&&r.unshift(o)}return r}function oi(t,e){for(var n=[],r=0;r1)&&n.unshift(o)}return n}function ai(t,e){for(var n=[],r=Math.max(t.length,e.length),i=0;i= "+this.outputShape[0]+" ? 0. : result.y;\n result.z = 0.;\n result.w = 0.;\n ";else{var a=ri("coords",i);o+="\n bool nextRowOutOfBounds =\n ("+a[i-2]+" + 1) >= "+this.outputShape[i-2]+";\n bool nextColOutOfBounds =\n ("+a[i-1]+" + 1) >= "+this.outputShape[i-1]+";\n result.y = nextColOutOfBounds ? 0. : result.y;\n result.z = nextRowOutOfBounds ? 0. : result.z;\n result.w = nextColOutOfBounds || nextRowOutOfBounds ? 0. : result.w;\n "}this.userCode="\n vec4 binaryOperation(vec4 a, vec4 b) {\n "+t+"\n }\n\n void main() {\n vec4 a = getAAtOutCoords();\n vec4 b = getBAtOutCoords();\n\n vec4 result = binaryOperation(a, b);\n "+o+"\n\n setOutput(result);\n }\n "},Ei=function(){function t(t){this.variableNames=["A"],this.outputShape=t,this.userCode="\n uniform float min;\n uniform float max;\n\n void main() {\n float value = getAAtOutCoords();\n if (isnan(value)) {\n setOutput(value);\n return;\n }\n\n setOutput(clamp(value, min, max));\n }\n "}return t.prototype.getCustomSetupFunc=function(t,e){var n=this;return function(r,i){null==n.minLoc&&(n.minLoc=r.getUniformLocationNoThrow(i,"min"),n.maxLoc=r.getUniformLocationNoThrow(i,"max")),r.gl.uniform1f(n.minLoc,t),r.gl.uniform1f(n.maxLoc,e)}},t}(),Ci=function(){function t(t){this.variableNames=["A"],this.usesPackedTextures=!0,this.outputShape=t,this.userCode="\n uniform float min;\n uniform float max;\n\n void main() {\n vec4 value = getAAtOutCoords();\n\n if (any(isnan(value))) {\n setOutput(value);\n return;\n }\n\n setOutput(clamp(value, vec4(min), vec4(max)));\n }\n "}return t.prototype.getCustomSetupFunc=function(t,e){var n=this;return function(r,i){null==n.minLoc&&(n.minLoc=r.getUniformLocationNoThrow(i,"min"),n.maxLoc=r.getUniformLocationNoThrow(i,"max")),r.gl.uniform1f(n.minLoc,t),r.gl.uniform1f(n.maxLoc,e)}},t}();function Ni(t,e){if(1===t)return""+e;if(2===t)return e+".y";if(3===t)return e+".z";if(4===t)return e+".w";throw Error("Cumulative sum for rank "+t+" is not yet supported")}var Ai,Oi,Ii=function(){function t(t,e,n){this.variableNames=["x"],this.outputShape=[],this.outputShape=t,this.blockSize=e,this.dataFormat=n,this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int h = "+this.getHeightCoordString()+";\n int w = "+this.getWidthCoordString()+";\n int d = "+this.getDepthCoordString()+";\n\n int in_h = h / "+e+";\n int offset_h = imod(h, "+e+");\n int in_w = w / "+e+";\n int offset_w = imod(w, "+e+");\n int offset_d = (offset_h * "+e+" + offset_w) *\n "+this.getOutputDepthSize()+";\n int in_d = d + offset_d;\n\n float result = "+this.getInputSamplingString()+";\n setOutput(result);\n }\n "}return t.prototype.getHeightCoordString=function(){return"NHWC"===this.dataFormat?"coords[1]":"coords[2]"},t.prototype.getWidthCoordString=function(){return"NHWC"===this.dataFormat?"coords[2]":"coords[3]"},t.prototype.getDepthCoordString=function(){return"NHWC"===this.dataFormat?"coords[3]":"coords[1]"},t.prototype.getOutputDepthSize=function(){return"NHWC"===this.dataFormat?this.outputShape[3]:this.outputShape[1]},t.prototype.getInputSamplingString=function(){return"NHWC"===this.dataFormat?"getX(b, in_h, in_w, in_d)":"getX(b, in_d, in_h, in_w)"},t}(),Ti=function(t,e,n){this.variableNames=["real","imag"];var r=e[1];this.outputShape=e;var i=n?"2.0 * "+Math.PI:"-2.0 * "+Math.PI,o=n?r+".0":"1.0";this.userCode="\n const float exponentMultiplier = "+i+";\n\n float unaryOpComplex(float real, float expR, float imag, float expI) {\n "+t+"\n }\n\n float mulMatDFT(int batch, int index) {\n float indexRatio = float(index) / float("+r+");\n float exponentMultiplierTimesIndexRatio =\n exponentMultiplier * indexRatio;\n\n float result = 0.0;\n\n for (int i = 0; i < "+r+"; i++) {\n // x = (-2|2 * PI / N) * index * i;\n float x = exponentMultiplierTimesIndexRatio * float(i);\n float expR = cos(x);\n float expI = sin(x);\n float real = getReal(batch, i);\n float imag = getImag(batch, i);\n\n result +=\n unaryOpComplex(real, expR, imag, expI) / "+o+";\n }\n\n return result;\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n setOutput(mulMatDFT(coords[0], coords[1]));\n }\n "},Mi=function(){function t(t,e){this.outputShape=[],this.variableNames=["x"],this.outputShape=t,this.userCode="\n uniform float value;\n void main() {\n // Input can be obtained from uniform value.\n setOutput(value);\n }\n "}return t.prototype.getCustomSetupFunc=function(t){var e=this;return function(n,r){null==e.valueLoc&&(e.valueLoc=n.getUniformLocationNoThrow(r,"value")),n.gl.uniform1f(e.valueLoc,t)}},t}();function Ri(t,e){return[e,t]}function Di(t,e){return t*e}function ji(t,e,n){var r=function(t,e){if(t%e!=0)throw new Error("unpackedSize ("+t+") must be a multiple of "+e);return t/e}(t.length,n);if(e.length= "+r);for(var i=0,o=0;o= "+o);for(var a=r%2==1,s=n%2==1,u=Math.floor(r/2),c=Math.floor(n/2),l=Math.ceil(r/2),f=l*Math.ceil(n/2),h=y(n)*y(r),d=0;d= "+r);for(var i=0,o=0;o0?(e=this.beginQuery(),this.endQuery(),n=function(){return r.isQueryAvailable(e,f.getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))}):n=function(){return!0};return{query:e,isFencePassed:n}},t.prototype.downloadMatrixFromPackedTexture=function(t,e,n,r,i,o){var a=this;return this.downloadMatrixDriver(t,function(){return oo(a.gl,a.debug,e,n,r,i,o,a.textureConfig)})},t.prototype.createProgram=function(t){this.throwIfDisposed();var e=this.gl,n=Gt(e,this.debug,t),r=Fi(e,this.debug),i=Yt(e,this.debug);return Ft(e,this.debug,function(){return e.attachShader(i,r)}),Ft(e,this.debug,function(){return e.attachShader(i,n)}),Jt(e,this.debug,i),this.debug&&Zt(e,this.debug,i),this.vertexAttrsAreBound||(this.setProgram(i),this.vertexAttrsAreBound=$i(e,this.debug,this.program,this.vertexBuffer)),i},t.prototype.deleteProgram=function(t){var e=this;this.throwIfDisposed(),t===this.program&&(this.program=null),null!=t&&Ft(this.gl,this.debug,function(){return e.gl.deleteProgram(t)})},t.prototype.setProgram=function(t){var e=this;this.throwIfDisposed(),this.program=t,null!=this.program&&this.debug&&Zt(this.gl,this.debug,this.program),Ft(this.gl,this.debug,function(){return e.gl.useProgram(t)})},t.prototype.getUniformLocation=function(t,e,n){return void 0===n&&(n=!0),this.throwIfDisposed(),n?se(this.gl,this.debug,t,e):ue(this.gl,t,e)},t.prototype.getAttributeLocation=function(t,e){var n=this;return this.throwIfDisposed(),Ft(this.gl,this.debug,function(){return n.gl.getAttribLocation(t,e)})},t.prototype.getUniformLocationNoThrow=function(t,e){return this.throwIfDisposed(),this.gl.getUniformLocation(t,e)},t.prototype.setInputMatrixTexture=function(t,e,n){this.throwIfDisposed(),this.throwIfNoProgram(),ce(this.gl,this.debug,this.program,t,e,n)},t.prototype.setOutputMatrixTexture=function(t,e,n){this.setOutputMatrixTextureDriver(t,n,e)},t.prototype.setOutputPackedMatrixTexture=function(t,e,n){this.throwIfDisposed();var r=Pi(e,n),i=r[0],o=r[1];this.setOutputMatrixTextureDriver(t,i,o)},t.prototype.setOutputMatrixWriteRegion=function(t,e,n,r){this.setOutputMatrixWriteRegionDriver(n,t,r,e)},t.prototype.setOutputPackedMatrixWriteRegion=function(t,e,n,r){throw new Error("setOutputPackedMatrixWriteRegion not implemented.")},t.prototype.debugValidate=function(){null!=this.program&&Zt(this.gl,this.debug,this.program),he(this.gl)},t.prototype.executeProgram=function(){this.throwIfDisposed(),this.throwIfNoProgram();var t=this.gl;this.debug&&this.debugValidate(),Ft(t,this.debug,function(){return t.drawElements(t.TRIANGLES,6,t.UNSIGNED_SHORT,0)})},t.prototype.blockUntilAllProgramsCompleted=function(){var t=this;this.throwIfDisposed(),Ft(this.gl,this.debug,function(){return t.gl.finish()})},t.prototype.getQueryTimerExtension=function(){return null==this.disjointQueryTimerExtension&&(this.disjointQueryTimerExtension=qt(this.gl,this.debug,2===f.getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")?"EXT_disjoint_timer_query_webgl2":"EXT_disjoint_timer_query")),this.disjointQueryTimerExtension},t.prototype.getQueryTimerExtensionWebGL2=function(){return this.getQueryTimerExtension()},t.prototype.getQueryTimerExtensionWebGL1=function(){return this.getQueryTimerExtension()},t.prototype.beginQuery=function(){if(2===f.getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")){var t=this.gl,e=this.getQueryTimerExtensionWebGL2(),n=t.createQuery();return t.beginQuery(e.TIME_ELAPSED_EXT,n),n}var r=this.getQueryTimerExtensionWebGL1(),i=r.createQueryEXT();return r.beginQueryEXT(r.TIME_ELAPSED_EXT,i),i},t.prototype.endQuery=function(){if(2!==f.getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")){var t=this.getQueryTimerExtensionWebGL1();t.endQueryEXT(t.TIME_ELAPSED_EXT)}else{var e=this.gl,n=this.getQueryTimerExtensionWebGL2();e.endQuery(n.TIME_ELAPSED_EXT)}},t.prototype.waitForQueryAndGetTime=function(t){return c(this,void 0,void 0,function(){var e=this;return l(this,function(n){switch(n.label){case 0:return[4,I(function(){return e.disposed||e.isQueryAvailable(t,f.getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))})];case 1:return n.sent(),[2,this.getQueryTime(t,f.getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))]}})})},t.prototype.getQueryTime=function(t,e){if(0===e)return null;if(2===e){var n=this.gl;return n.getQueryParameter(t,n.QUERY_RESULT)/1e6}var r=this.getQueryTimerExtensionWebGL1();return r.getQueryObjectEXT(t,r.QUERY_RESULT_EXT)/1e6},t.prototype.isQueryAvailable=function(t,e){if(0===e)return!0;if(2===e){var n=this.gl,r=this.getQueryTimerExtensionWebGL2(),i=n.getQueryParameter(t,n.QUERY_RESULT_AVAILABLE);return null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),i&&!this.disjoint}return i=(r=this.getQueryTimerExtensionWebGL1()).getQueryObjectEXT(t,r.QUERY_RESULT_AVAILABLE_EXT),null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),i&&!this.disjoint},t.prototype.pollFence=function(t){var e=this;return new Promise(function(n){e.addItemToPoll(function(){return t.isFencePassed()},function(){return n()})})},t.prototype.pollItems=function(){for(var t=function(t){for(var e=0;e1||I(function(){return n.pollItems(),0===n.itemsToPoll.length})},t.prototype.bindTextureToFrameBuffer=function(t){this.throwIfDisposed(),le(this.gl,this.debug,t,this.framebuffer),this.debug&&he(this.gl)},t.prototype.unbindTextureToFrameBuffer=function(){null!=this.outputTexture?(le(this.gl,this.debug,this.outputTexture,this.framebuffer),this.debug&&he(this.gl)):fe(this.gl,this.debug,this.framebuffer)},t.prototype.downloadMatrixDriver=function(t,e){this.bindTextureToFrameBuffer(t);var n=e();return this.unbindTextureToFrameBuffer(),n},t.prototype.setOutputMatrixTextureDriver=function(t,e,n){this.throwIfDisposed();var r=this.gl;le(r,this.debug,t,this.framebuffer),this.debug&&he(r),this.outputTexture=t,Ft(r,this.debug,function(){return r.viewport(0,0,e,n)}),Ft(r,this.debug,function(){return r.scissor(0,0,e,n)})},t.prototype.setOutputMatrixWriteRegionDriver=function(t,e,n,r){var i=this;this.throwIfDisposed(),Ft(this.gl,this.debug,function(){return i.gl.scissor(t,e,n,r)})},t.prototype.throwIfDisposed=function(){if(this.disposed)throw new Error("Attempted to use disposed GPGPUContext.")},t.prototype.throwIfNoProgram=function(){if(null==this.program)throw new Error("No GPU program is currently set.")},t}();function uo(t,e){if(t.length!==e.length)throw Error("Binary was compiled with "+t.length+" inputs, but was executed with "+e.length+" inputs");t.forEach(function(t,n){var r=t.logicalShape,i=e[n],o=i.shape;if(!E(r,o))throw Error("Binary was compiled with different shapes than the current args. Shapes "+r+" and "+o+" must match");if(!t.isUniform||!i.isUniform){var a=t.texShape,s=i.isUniform?null:i.texData.texShape;if(!E(a,s))throw Error("Binary was compiled with different texture shapes than the current args. Shape "+a+" and "+s+" must match")}})}var co=function(t,e,n,r,i,o){void 0===n&&(n=!1),void 0===r&&(r=!1),void 0===i&&(i=!1),void 0===o&&(o=null),this.variableNames=["matrixA","matrixB"],this.usesPackedTextures=!0,this.outputShape=e;var a=n?t[1]:t[2],s=Math.ceil(a/2),u=n?"i * 2, rc.y":"rc.y, i * 2",c=r?"rc.z, i * 2":"i * 2, rc.z",l=n?["a.xxyy","a.zzww"]:["a.xxzz","a.yyww"],f=r?["b.xzxz","b.ywyw"]:["b.xyxy","b.zwzw"],h="",d="";o&&(h="vec4 activation(vec4 x) {\n "+o+"\n }",d="result = activation(result);");var p=i?"result += getBiasAtOutCoords();":"";i&&this.variableNames.push("bias"),this.userCode="\n "+h+"\n\n const float sharedDimension = "+s+".0;\n\n vec4 dot2x2ARowBCol(ivec3 rc) {\n vec4 result = vec4(0);\n for (int i = 0; i < "+s+"; i++) {\n vec4 a = getMatrixA(rc.x, "+u+");\n vec4 b = getMatrixB(rc.x, "+c+");\n\n result += ("+l[0]+" * "+f[0]+") + ("+l[1]+" * "+f[1]+");\n }\n return result;\n }\n\n void main() {\n ivec3 rc = getOutputCoords();\n vec4 result = dot2x2ARowBCol(rc);\n\n "+p+"\n\n "+d+"\n\n setOutput(result);\n }\n "},lo=function(){function t(t,e,n){this.variableNames=["probs"],this.outputShape=[t,n],this.userCode="\n uniform float seed;\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n\n float r = random(seed);\n float cdf = 0.0;\n\n for (int i = 0; i < "+(e-1)+"; i++) {\n cdf += getProbs(batch, i);\n\n if (r < cdf) {\n setOutput(float(i));\n return;\n }\n }\n\n // If no other event happened, last event happened.\n setOutput(float("+(e-1)+"));\n }\n "}return t.prototype.getCustomSetupFunc=function(t){var e=this;return function(n,r){null==e.seedLoc&&(e.seedLoc=n.getUniformLocation(r,"seed")),n.gl.uniform1f(e.seedLoc,t)}},t}(),fo=function(t,e,n){if(this.variableNames=["x"],"avg"===e&&n)throw new Error("Cannot compute positions for average pool.");var r=t.filterWidth,i=t.strideHeight,o=t.strideWidth,a=t.dilationHeight,s=t.dilationWidth,u=t.effectiveFilterHeight,c=t.effectiveFilterWidth,l=t.padInfo.top,f=t.padInfo.left;this.outputShape=t.outShape;var h="avg"===e,d="0.0";if(h||(d="-1.0 / 1e-20"),n)this.userCode="\n const ivec2 strides = ivec2("+i+", "+o+");\n const ivec2 pads = ivec2("+l+", "+f+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n float minMaxValue = 0.0;\n float minMaxValueFound = 0.0;\n int minMaxPosition = 0;\n float avgValue = 0.0;\n\n for (int wR = 0; wR < "+u+";\n wR += "+a+") {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+c+";\n wC += "+s+") {\n int xC = xCCorner + wC;\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n float value = getX(batch, xR, xC, d);\n\n // If a min / max value has already been found, use it. If not,\n // use the current value.\n float currMinMaxValue = mix(\n value, minMaxValue, minMaxValueFound);\n if (value >= currMinMaxValue) {\n minMaxValue = value;\n minMaxValueFound = 1.0;\n minMaxPosition = wR * "+c+" + wC;\n }\n }\n }\n setOutput(float(minMaxPosition));\n }\n ";else{var p=e+"("+e+"("+e+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"avg"===e&&(p="avgValue / count");var m=4*Math.floor(r/4),v=r%4,g="\n if ("+h+") {\n avgValue += dot(values, ones);\n } else {\n minMaxValue = max(values, minMaxValue);\n }\n ";this.userCode="\n const ivec2 strides = ivec2("+i+", "+o+");\n const ivec2 pads = ivec2("+l+", "+f+");\n const float initializationValue = "+d+";\n const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);\n\n float count = 0.0;\n\n float getValue(int batch, int xR, int xC, int d) {\n if (xC < 0 || xC >= "+t.inWidth+") {\n return initializationValue;\n }\n count += 1.0;\n return getX(batch, xR, xC, d);\n }\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n vec4 minMaxValue = vec4("+d+");\n float avgValue = 0.0;\n count = 0.0;\n\n for (int wR = 0; wR < "+u+";\n wR += "+a+") {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+m+"; wC += 4) {\n int xC = xCCorner + wC * "+s+";\n\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + "+s+", d),\n getValue(batch, xR, xC + 2 * "+s+", d),\n getValue(batch, xR, xC + 3 * "+s+", d)\n );\n\n "+g+"\n }\n\n int xC = xCCorner + "+m+";\n if ("+(1===v)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+g+"\n } else if ("+(2===v)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + "+s+", d),\n initializationValue,\n initializationValue\n );\n\n "+g+"\n } else if ("+(3===v)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + "+s+", d),\n getValue(batch, xR, xC + 2 * "+s+", d),\n initializationValue\n );\n\n "+g+"\n }\n }\n setOutput("+p+");\n }\n "}},ho=function(t,e,n,r,i,o,a){void 0===a&&(a=!0),this.variableNames=["updates","indices","defaultValue"],this.outputShape=o;var s=vi(i.length),u=vi(o.length),c="";1===n?c="i":2===n&&(c="i, j");var l="getIndices("+c+")",f="";1===r?f="i":2===r&&(f="i, coords[1]");var h="getUpdates("+f+")",d=e>1?"strides[j]":"strides";this.userCode="\n "+s+" strides = "+s+"("+i+");\n\n void main() {\n "+u+" coords = getOutputCoords();\n float sum = 0.0;\n bool found = false;\n for (int i = 0; i < "+t+"; i++) {\n int flattenedIndex = 0;\n for (int j = 0; j < "+e+"; j++) {\n int index = round("+l+");\n flattenedIndex += index * "+d+";\n }\n if (flattenedIndex == coords[0]) {\n sum += "+h+";\n found = true;\n }\n }\n setOutput(mix(getDefaultValue(), sum, float(found)));\n }\n "},po=function(){function t(t){this.variableNames=["source"],this.outputShape=t,this.rank=t.length;var e,n=vi(this.rank),r="uniform int start["+this.rank+"];",i=function(t){if(1===t)return"sourceLoc";if(t<=6)return mo.slice(0,t).map(function(t){return"sourceLoc."+t}).join(",");throw Error("Slicing for rank "+t+" is not yet supported")}(this.rank);e="\n "+n+" sourceLoc;\n "+n+" coords = getOutputCoords();\n "+t.map(function(t,e){return"sourceLoc."+mo[e]+" = start["+e+"] + coords."+mo[e]+";"}).join("\n")+"\n ",this.userCode="\n "+r+"\n void main() {\n "+e+"\n setOutput(getSource("+i+"));\n }\n "}return t.prototype.getCustomSetupFunc=function(t){var e=this;if(t.length!==this.rank)throw Error("The rank ("+this.rank+") of the program must match the length of start ("+t.length+")");return function(n,r){null==e.startLoc&&(e.startLoc=n.getUniformLocationNoThrow(r,"start"),null==e.startLoc)||n.gl.uniform1iv(e.startLoc,t)}},t}(),mo=["x","y","z","w","u","v"],vo=function(){function t(t){this.variableNames=["source"],this.usesPackedTextures=!0,this.outputShape=t,this.rank=t.length;var e=vi(this.rank),n=ri("coords",this.rank),r=ri("sourceLoc",this.rank),i=1===this.rank?"sourceLoc":"vec2("+r.slice(-2).join()+")",o="getChannel(getSource("+r.join()+"), "+i+")",a="\n result.x = "+o+";\n if (++"+n[this.rank-1]+" < "+t[this.rank-1]+") {\n ++"+r[this.rank-1]+";\n result.y = "+o+";\n --"+r[this.rank-1]+";\n }\n ",s=1===this.rank?"":"\n --"+n[this.rank-1]+";\n if (++"+n[this.rank-2]+" < "+t[this.rank-2]+") {\n ++"+r[this.rank-2]+";\n result.z = "+o+";\n if (++"+n[this.rank-1]+" < "+t[this.rank-1]+") {\n ++"+r[this.rank-1]+";\n result.w = "+o+";\n }\n }\n ",u=this.rank<=4?"sourceLoc = coords +\n "+e+"("+t.map(function(t,e){return"start["+e+"]"}).join()+");":t.map(function(t,e){return r[e]+" = "+n[e]+" + start["+e+"];"}).join("\n");this.userCode="\n uniform int start["+this.rank+"];\n void main() {\n "+e+" coords = getOutputCoords();\n "+e+" sourceLoc;\n "+u+" \n vec4 result = vec4(0.);\n "+a+"\n "+s+"\n setOutput(result);\n }\n "}return t.prototype.getCustomSetupFunc=function(t){var e=this;if(t.length!==this.rank)throw Error("The rank ("+this.rank+") of the program must match the length of start ("+t.length+")");return function(n,r){null==e.startLoc&&(e.startLoc=n.getUniformLocationNoThrow(r,"start"),null==e.startLoc)||n.gl.uniform1iv(e.startLoc,t)}},t}(),go=function(){function t(t){this.gpgpu=t,this.numUsedTextures=0,this.numFreeTextures=0,this.freeTextures={},this.logEnabled=!1,this.usedTextures={}}return t.prototype.acquireTexture=function(t,e,n){var r,i=yo(e,n),o=bo(t,i,n);if(o in this.freeTextures||(this.freeTextures[o]=[]),o in this.usedTextures||(this.usedTextures[o]=[]),this.freeTextures[o].length>0){this.numFreeTextures--,this.numUsedTextures++,this.log();var a=this.freeTextures[o].shift();return this.usedTextures[o].push(a),a}return this.numUsedTextures++,this.log(),i===Oi.PACKED_2X2_FLOAT32?r=this.gpgpu.createPackedMatrixTexture(t[0],t[1]):i===Oi.PACKED_2X2_FLOAT16?r=this.gpgpu.createFloat16PackedMatrixTexture(t[0],t[1]):i===Oi.UNPACKED_FLOAT32?r=this.gpgpu.createFloat32MatrixTexture(t[0],t[1]):i===Oi.UNPACKED_FLOAT16?r=this.gpgpu.createFloat16MatrixTexture(t[0],t[1]):i===Oi.PACKED_4X1_UNSIGNED_BYTE&&(r=this.gpgpu.createUnsignedBytesMatrixTexture(t[0],t[1])),this.usedTextures[o].push(r),r},t.prototype.releaseTexture=function(t,e,n,r){if(null!=this.freeTextures){var i=bo(e,yo(n,r),r);i in this.freeTextures||(this.freeTextures[i]=[]),this.freeTextures[i].push(t),this.numFreeTextures++,this.numUsedTextures--;var o=this.usedTextures[i],a=o.indexOf(t);if(a<0)throw new Error("Cannot release a texture that was never provided by this texture manager");o.splice(a,1),this.log()}},t.prototype.log=function(){if(this.logEnabled){var t=this.numFreeTextures+this.numUsedTextures;console.log("Free/Used",this.numFreeTextures+" / "+this.numUsedTextures,"("+t+")")}},t.prototype.getNumUsedTextures=function(){return this.numUsedTextures},t.prototype.getNumFreeTextures=function(){return this.numFreeTextures},t.prototype.dispose=function(){var t=this;if(null!=this.freeTextures){for(var e in this.freeTextures)this.freeTextures[e].forEach(function(e){t.gpgpu.deleteMatrixTexture(e)});for(var e in this.usedTextures)this.usedTextures[e].forEach(function(e){t.gpgpu.deleteMatrixTexture(e)});this.freeTextures=null,this.usedTextures=null,this.numUsedTextures=0,this.numFreeTextures=0}},t}();function yo(t,e){if(t===Ai.UPLOAD)return e?Oi.PACKED_2X2_FLOAT32:Oi.UNPACKED_FLOAT32;if(t===Ai.RENDER||null==t)return e?f.getBool("WEBGL_RENDER_FLOAT32_ENABLED")?Oi.PACKED_2X2_FLOAT32:Oi.PACKED_2X2_FLOAT16:f.getBool("WEBGL_RENDER_FLOAT32_ENABLED")?Oi.UNPACKED_FLOAT32:Oi.UNPACKED_FLOAT16;if(t===Ai.DOWNLOAD||t===Ai.PIXELS)return Oi.PACKED_4X1_UNSIGNED_BYTE;throw new Error("Unknown logical texture type "+t)}function bo(t,e,n){return t[0]+"_"+t[1]+"_"+e+"_"+n}var wo=function(t,e){this.variableNames=["A"],this.outputShape=t,this.userCode="\n float unaryOperation(float x) {\n "+e+"\n }\n\n void main() {\n float x = getAAtOutCoords();\n float y = unaryOperation(x);\n\n setOutput(y);\n }\n "},xo="if (isnan(x)) return x;",ko=xo+"\n return (x < 0.0) ? 0.0 : x;\n",So="return exp(x);",_o="\n vec4 result = x * vec4(greaterThanEqual(x, vec4(0.0)));\n bvec4 isNaN = isnan(x);\n\n result.r = isNaN.r ? x.r : result.r;\n result.g = isNaN.g ? x.g : result.g;\n result.b = isNaN.b ? x.b : result.b;\n result.a = isNaN.a ? x.a : result.a;\n\n return result;\n",Eo=function(t,e){this.variableNames=["A"],this.usesPackedTextures=!0,this.outputShape=t,this.userCode="\n vec4 unaryOperation(vec4 x) {\n "+e+"\n }\n\n void main() {\n vec4 x = getAAtOutCoords();\n vec4 y = unaryOperation(x);\n\n setOutput(y);\n }\n "},Co={},No=600,Ao=function(){function t(t){if(this.gpgpu=t,this.pendingRead=new WeakMap,this.pendingDisposal=new WeakSet,this.dataRefCount=new WeakMap,this.numBytesInGPU=0,this.uploadWaitMs=0,this.downloadWaitMs=0,this.warnedAboutMemory=!1,this.disposed=!1,!f.getBool("HAS_WEBGL"))throw new Error("WebGL is not supported on this device");if(null==t){var e=Lt(f.getNumber("WEBGL_VERSION"));this.binaryCache=(n=f.getNumber("WEBGL_VERSION"))in Co?Co[n]:(Co[n]={},Co[n]),this.gpgpu=new so(e),this.canvas=e.canvas,this.gpgpuCreatedLocally=!0}else this.binaryCache={},this.gpgpuCreatedLocally=!1,this.canvas=t.gl.canvas;var n;this.textureManager=new go(this.gpgpu),this.numMBBeforeWarning=null==f.global.screen?1024:f.global.screen.height*f.global.screen.width*window.devicePixelRatio*No/1024/1024,this.texData=new Zn(Dt)}return t.prototype.register=function(t,e,n){if(this.texData.has(t))throw new Error("Data buffer is already registered");this.texData.set(t,{shape:e,dtype:n})},t.prototype.fromPixels=function(t,e){if(null==t)throw new Error("pixels passed to tf.browser.fromPixels() can not be null");var n=[t.height,t.width],r=[t.height,t.width,e];if(f.getBool("IS_BROWSER")){if(!(t instanceof HTMLVideoElement||t instanceof HTMLImageElement||t instanceof HTMLCanvasElement||t instanceof ImageData))throw new Error("pixels passed to tf.browser.fromPixels() must be either an HTMLVideoElement, HTMLImageElement, HTMLCanvasElement or ImageData, but was "+t.constructor.name);if(t instanceof HTMLVideoElement){if(null==this.fromPixels2DContext){if("complete"!==document.readyState)throw new Error("The DOM is not ready yet. Please call tf.browser.fromPixels() once the DOM is ready. One way to do that is to add an event listener for `DOMContentLoaded` on the document object");this.fromPixels2DContext=document.createElement("canvas").getContext("2d")}this.fromPixels2DContext.canvas.width=t.width,this.fromPixels2DContext.canvas.height=t.height,this.fromPixels2DContext.drawImage(t,0,0,t.width,t.height),t=this.fromPixels2DContext.canvas}}var i=this.makeTensorHandle(n,"int32");this.texData.get(i.dataId).usage=Ai.PIXELS,this.gpgpu.uploadPixelDataToTexture(this.getTexture(i.dataId),t);var o=new function(t){this.variableNames=["A"];var e=si(),n=t[0],r=t[1];this.outputShape=t,this.userCode="\n void main() {\n ivec3 coords = getOutputCoords();\n int texR = coords[0];\n int texC = coords[1];\n int depth = coords[2];\n vec2 uv = (vec2(texC, texR) + halfCR) / vec2("+r+".0, "+n+".0);\n\n vec4 values = "+e.texture2D+"(A, uv);\n float value;\n if (depth == 0) {\n value = values.r;\n } else if (depth == 1) {\n value = values.g;\n } else if (depth == 2) {\n value = values.b;\n } else if (depth == 3) {\n value = values.a;\n }\n\n setOutput(floor(value * 255.0 + 0.5));\n }\n "}(r),a=this.compileAndRun(o,[i]);return this.disposeData(i.dataId),a},t.prototype.makeTensorHandle=function(t,e){var n={};return this.register(n,t,e),{dataId:n,shape:t,dtype:e}},t.prototype.write=function(t,e){if(null==e)throw new Error("MathBackendWebGL.write(): values can not be null");if(f.getBool("DEBUG"))for(var n=0;n FLOAT_MAX) {\n return vec4(0.0, 0.0, 128.0, 127.0) / 255.0;\n } else if(v < -FLOAT_MAX) {\n return vec4(0.0, 0.0, 128.0, 255.0) / 255.0;\n }\n\n highp vec4 c = vec4(0,0,0,0);\n\n highp float e = floor(log2(av));\n highp float m = exp2(fract(log2(av))) - 1.0;\n\n c[2] = floor(128.0 * m);\n m -= c[2] / 128.0;\n c[1] = floor(32768.0 * m);\n m -= c[1] / 32768.0;\n c[0] = floor(8388608.0 * m);\n\n highp float ebias = e + 127.0;\n c[3] = floor(ebias / 2.0);\n ebias -= c[3] * 2.0;\n c[2] += floor(ebias) * 128.0;\n\n c[3] += 128.0 * step(0.0, -v);\n\n return c / 255.0;\n }\n\n void main() {\n float x = getAAtOutCoords();\n "+e.output+" = encode_float(x);\n }\n "}(i);return n.compileAndRun(e,[{shape:i,dtype:o,dataId:t}],d,null)}),m=this.texData.get(p.dataId),v=this.gpgpu.downloadByteEncodedFloatMatrixFromOutputTexture(m.texture,m.texShape[0],m.texShape[1]).subarray(0,u);return this.disposeData(d.dataId),v},t.prototype.time=function(t){return c(this,void 0,void 0,function(){var e,n,r,i,o,a,s;return l(this,function(u){switch(u.label){case 0:return e=this.activeTimers,n=[],r=!1,null==this.programTimersStack?(this.programTimersStack=n,r=!0):this.activeTimers.push(n),this.activeTimers=n,t(),i=S(this.activeTimers.map(function(t){return t.query})).filter(function(t){return null!=t}),o=S(this.activeTimers.map(function(t){return t.name})).filter(function(t){return null!=t}),this.activeTimers=e,r&&(this.programTimersStack=null),[4,Promise.all(i)];case 1:return a=u.sent(),s={uploadWaitMs:this.uploadWaitMs,downloadWaitMs:this.downloadWaitMs,kernelMs:b(a),getExtraProfileInfo:function(){return a.map(function(t,e){return{name:o[e],ms:t}}).map(function(t){return t.name+": "+t.ms}).join(", ")},wallMs:null},this.uploadWaitMs=0,this.downloadWaitMs=0,[2,s]}})})},t.prototype.memory=function(){return{unreliable:!1,numBytesInGPU:this.numBytesInGPU}},t.prototype.startTimer=function(){return f.getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?this.gpgpu.beginQuery():{startMs:performance.now(),endMs:null}},t.prototype.endTimer=function(t){return f.getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?(this.gpgpu.endQuery(),t):(t.endMs=performance.now(),t)},t.prototype.getQueryTime=function(t){return c(this,void 0,void 0,function(){var e;return l(this,function(n){return f.getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?[2,this.gpgpu.waitForQueryAndGetTime(t)]:[2,(e=t).endMs-e.startMs]})})},t.prototype.disposeData=function(t){if(!this.pendingDisposal.has(t))if(this.pendingRead.has(t))this.pendingDisposal.add(t);else if(this.texData.has(t)){this.releaseGPUData(t);var e=this.texData.get(t).complexTensors;null!=e&&(e.real.dispose(),e.imag.dispose()),this.texData.delete(t)}},t.prototype.releaseGPUData=function(t){var e=this.texData.get(t),n=e.texture,r=e.dtype,i=e.texShape,o=e.usage,a=e.isPacked,s=e.slice,u=s&&s.origDataId||t,c=this.dataRefCount.get(u);c>1?this.dataRefCount.set(u,c-1):(this.dataRefCount.delete(u),null!=n&&(this.numBytesInGPU-=this.computeBytes(i,r),this.textureManager.releaseTexture(n,i,o,a)));var l=this.texData.get(t);l.texture=null,l.texShape=null,l.isPacked=!1,l.slice=null},t.prototype.getTexture=function(t){return this.uploadToGPU(t),this.texData.get(t).texture},t.prototype.getCPUBackend=function(){return f.getBool("WEBGL_CPU_FORWARD")?(null==this.cpuBackend&&(this.cpuBackend=Dt.findBackend("cpu")),this.cpuBackend):null},t.prototype.shouldExecuteOnCPU=function(t,e){var n=this;return void 0===e&&(e=128),null!=this.getCPUBackend()&&t.every(function(t){return null==n.texData.get(t.dataId).texture&&t.size4)throw new Error("WebGL backend: Reverse of rank-"+n+" tensor is not yet supported");this.outputShape=t;var r=ri("rc",n),i=r[n-1]+" + 1 < "+this.outputShape[n-1],o=r[n-2]+" + 1 < "+this.outputShape[n-2],a=vi(n);function s(n){var r=t.map(function(r,i){return function(n,r){return-1!==e.indexOf(n)&&1!==t[n]?t[n]+" - "+r[n]+" - 1":""+r[n]}(i,n)});return"getChannel(getX("+r.join(",")+"), vec2("+r.slice(-2).join(",")+"))"}this.userCode=1===n?"\n void main(){\n int rc = getOutputCoords();\n vec4 result = vec4(0.);\n result.r = getChannel(getX("+t[0]+" - rc - 1),\n "+t[0]+" - rc - 1);\n if("+i+"){\n result.g = getChannel(getX("+t[0]+" - (rc + 1) - 1),\n "+t[0]+" - (rc + 1) - 1);\n }\n setOutput(result);\n }\n ":"\n void main() {\n "+a+" rc = getOutputCoords();\n vec4 result = vec4(0.);\n result.r = "+s(r.slice())+";\n if("+i+"){\n result.g = "+function(t){return t[n-1]="("+t[n-1]+" + 1)",s(t)}(r.slice())+";\n }\n if("+o+") {\n result.b = "+function(t){return t[n-2]="("+t[n-2]+" + 1)",s(t)}(r.slice())+";\n if("+i+") {\n result.a = "+function(t){return t[n-1]="("+t[n-1]+" + 1)",t[n-2]="("+t[n-2]+" + 1)",s(t)}(r.slice())+";\n }\n }\n setOutput(result);\n }\n "}(t.shape,e):new function(t,e){this.variableNames=["x"];var n=t.length;if(n>4)throw new Error("WebGL backend: Reverse of rank-"+n+" tensor is not yet supported");if(this.outputShape=t,1!==n){var r=t.map(function(n,r){return function(n){return-1!==e.indexOf(n)&&1!==t[n]?t[n]+" - coords["+n+"] - 1":"coords["+n+"]"}(r)}).join(","),i=vi(n);this.userCode="\n void main() {\n "+i+" coords = getOutputCoords();\n setOutput(getX("+r+"));\n }\n "}else this.userCode="\n void main() {\n int coord = getOutputCoords();\n setOutput(getX("+t[0]+" - coord - 1));\n }\n "}(t.shape,e);return this.compileAndRun(n,[t])},t.prototype.concat=function(t,e){if(this.shouldExecuteOnCPU(t))return this.cpuBackend.concat(t,e);if(1===t.length)return t[0];if(t.length>f.getNumber("WEBGL_MAX_TEXTURES_IN_SHADER")){var n=Math.floor(t.length/2),r=this.concat(t.slice(0,n),e),i=this.concat(t.slice(n),e);return this.concat([r,i],e)}if(f.getBool("WEBGL_PACK_ARRAY_OPERATIONS")&&t[0].rank>1){var o=new function(t,e){this.usesPackedTextures=!0,this.outputShape=[],this.outputShape=ln(t,e);var n=this.outputShape,r=n.length,i=vi(r),o=ri("coords",r),a=["x","y","z","w","u","v"].slice(0,r);this.variableNames=t.map(function(t,e){return"T"+e});var s=new Array(t.length-1);s[0]=t[0][e];for(var u=1;u1e3){n&&(t=t.transpose([0,2,1])),r&&(e=e.transpose([0,2,1]));var u=1===o?t:t.as3D(s,a,1),c=1===o?2:1,l=1===o?e.as3D(s,1,a):e;return this.multiply(u,l).sum(c,!0)}var f=Et(t.dtype,e.dtype),h=new co(t.shape,[s,i,o],n,r),d=this.makePackedTensor(h.outputShape,f);return this.compileAndRun(h,[t,e],d)},t.prototype.fusedBatchMatMul=function(t,e,n,r,i,o){var a=n?t.shape[2]:t.shape[1],s=r?e.shape[1]:e.shape[2],u=t.shape[0],c=Et(t.dtype,e.dtype),l=new co(t.shape,[u,a,s],n,r,!!i,o?function(t,e){if(void 0===e&&(e=!1),"linear"===t)return"return x;";if("relu"===t)return e?_o:ko;throw new Error("Activation "+t+" has not been implemented for the WebGL backend.")}(o,!0):null),f=this.makePackedTensor(l.outputShape,c),h=[t,e];return i&&h.push(i),this.compileAndRun(l,h,f)},t.prototype.multiply=function(t,e){if("complex64"===t.dtype){var n=this.texData.get(t.dataId),r=this.texData.get(e.dataId),i=new bi("return areal * breal - aimag * bimag;",t.shape,e.shape),o=new bi("return areal * bimag + aimag * breal;",t.shape,e.shape),a=[this.makeComplexComponentTensorHandle(t,n.complexTensors.real),this.makeComplexComponentTensorHandle(t,n.complexTensors.imag),this.makeComplexComponentTensorHandle(e,r.complexTensors.real),this.makeComplexComponentTensorHandle(e,r.complexTensors.imag)],s=this.compileAndRun(i,a),u=this.compileAndRun(o,a),c=this.complex(s,u);return s.dispose(),u.dispose(),c}if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.multiply(t,e);if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,ki,t.dtype);var l=new Si(ki,t.shape,e.shape),h=this.makeOutputArray(l.outputShape,t.dtype);return this.compileAndRun(l,[t,e],h)},t.prototype.batchNormalization=function(t,e,n,r,i,o){var a=[t,e,n],s=null;null!=o&&(s=o.shape,a.push(o));var u=null;if(null!=i&&(u=i.shape,a.push(i)),f.getBool("WEBGL_PACK_NORMALIZATION")){var c=new function(t,e,n,r,i,o){this.usesPackedTextures=!0,this.variableNames=["x","mean","variance"],ai(t,e),ai(t,n);var a="vec4(0.0)";null!=r&&(ai(t,r),this.variableNames.push("offset"),a="getOffsetAtOutCoords()");var s="vec4(1.0)";null!=i&&(ai(t,i),this.variableNames.push("scale"),s="getScaleAtOutCoords()"),this.outputShape=t,this.userCode="\n void main() {\n vec4 offset = "+a+";\n vec4 scale = "+s+";\n\n vec4 x = getXAtOutCoords();\n vec4 mean = getMeanAtOutCoords();\n vec4 variance = getVarianceAtOutCoords();\n\n vec4 inv = scale * inversesqrt(variance + vec4("+o+"));\n\n setOutput((x - mean) * inv + offset);\n }\n "}(t.shape,e.shape,n.shape,s,u,r);return this.compileAndRun(c,a)}var l=new function(t,e,n,r,i,o){this.outputShape=[],this.variableNames=["x","mean","variance"],ai(t,e),ai(t,n);var a="0.0";null!=r&&(ai(t,r),this.variableNames.push("offset"),a="getOffsetAtOutCoords()");var s="1.0";null!=i&&(ai(t,i),this.variableNames.push("scale"),s="getScaleAtOutCoords()"),this.outputShape=t,this.userCode="\n void main() {\n float x = getXAtOutCoords();\n float mean = getMeanAtOutCoords();\n float variance = getVarianceAtOutCoords();\n float offset = "+a+";\n float scale = "+s+";\n float inv = scale * inversesqrt(variance + float("+o+"));\n setOutput(dot(vec3(x, -mean, offset), vec3(inv, inv, 1)));\n }\n "}(t.shape,e.shape,n.shape,s,u,r);return this.compileAndRun(l,a)},t.prototype.localResponseNormalization4D=function(t,e,n,r,i){var o=f.getBool("WEBGL_PACK_NORMALIZATION")?new function(t,e,n,r,i){this.variableNames=["x"],this.outputShape=[],this.usesPackedTextures=!0;var o,a=e,s=t[3]-1;this.outputShape=t;var u="float("+n+") + float("+r+") * sum";o=.5===i?"inversesqrt("+u+")":1===i?"1.0/("+u+")":"exp(log("+u+") * float(-"+i+"));",this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords.x;\n int r = coords.y;\n int c = coords.z;\n int d = coords.w;\n\n bool hasNextCol = d < "+this.outputShape[3]+";\n bool hasNextRow = c < "+this.outputShape[2]+";\n\n vec4 sum = vec4(0.);\n vec4 xFragAtOutputCoords = getX(b, r, c, d);\n\n vec4 xAtOutputCoords = vec4(\n getChannel(xFragAtOutputCoords, vec2(c, d)),\n hasNextCol ?\n getChannel(xFragAtOutputCoords, vec2(c, d + 1)) : 0.0,\n hasNextRow ?\n getChannel(xFragAtOutputCoords , vec2(c + 1, d)) : 0.0,\n (hasNextRow && hasNextCol) ?\n getChannel(xFragAtOutputCoords, vec2(c + 1, d + 1)) : 0.0\n );\n\n int firstChannel = d - "+a+";\n vec2 cache = vec2(0.);\n if(firstChannel >= 0){\n vec4 firstChannelFrag = getX(b, r, c, firstChannel);\n cache.x = getChannel(firstChannelFrag, vec2(c, firstChannel));\n if(hasNextRow){\n cache.y = getChannel(firstChannelFrag, vec2(c + 1, firstChannel));\n }\n }\n\n ivec2 depth = ivec2(d, d + 1);\n for (int j = - "+a+"; j <= "+a+"; j++) {\n ivec2 idx = depth + j;\n bvec2 aboveLowerBound = greaterThanEqual(idx, ivec2(0));\n bvec2 belowUpperBound = lessThanEqual(idx, ivec2("+s+"));\n\n bool depthInRange = aboveLowerBound.x && belowUpperBound.x;\n bool depthPlusOneInRange = aboveLowerBound.y && belowUpperBound.y;\n\n if(depthInRange || depthPlusOneInRange){\n vec4 z = vec4(0.);\n vec4 xFragAtCurrentDepth;\n z.xz = cache.xy;\n if(depthPlusOneInRange && hasNextCol){\n xFragAtCurrentDepth = idx.y != d ?\n getX(b, r, c, idx.y) : xFragAtOutputCoords;\n z.y = getChannel(xFragAtCurrentDepth, vec2(c, idx.y));\n if(hasNextRow){\n z.w = getChannel(xFragAtCurrentDepth, vec2(c + 1, idx.y));\n }\n }\n cache.xy = z.yw;\n sum += z * z;\n }\n }\n vec4 result = xAtOutputCoords * "+o+";\n setOutput(result);\n }\n "}(t.shape,e,n,r,i):new function(t,e,n,r,i){this.variableNames=["x"],this.outputShape=[];var o,a=e,s=t[3]-1;this.outputShape=t;var u="float("+n+") + float("+r+") * sum";o=.5===i?"inversesqrt("+u+")":1===i?"1.0/("+u+")":"exp(log("+u+") * float(-"+i+"));",this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int r = coords[1];\n int c = coords[2];\n int d = coords[3];\n float x = getX(b, r, c, d);\n float sum = 0.0;\n for (int j = -"+a+"; j <= "+a+"; j++) {\n int idx = d + j;\n if (idx >= 0 && idx <= "+s+") {\n float z = getX(b, r, c, idx);\n sum += z * z;\n }\n }\n float val = x * "+o+";\n setOutput(val);\n }\n "}(t.shape,e,n,r,i);return this.compileAndRun(o,[t])},t.prototype.LRNGrad=function(t,e,n,r,i,o,a){var s=new function(t,e,n,r,i){this.variableNames=["inputImage","outputImage","dy"],this.outputShape=[],this.outputShape=t,this.depth=t[3],this.depthRadius=e,this.bias=n,this.alpha=r,this.beta=i,this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int r = coords[1];\n int c = coords[2];\n\n float result = 0.0;\n for (int d = 0; d < "+this.depth+"; ++d) {\n int depthBegin = int(max(0.0, float(d - "+e+")));\n int depthEnd = int(min(float("+this.depth+"),\n float(d + "+e+" + 1)));\n\n const int MIN_DEPTH_BEGIN = 0;\n const int MAX_DEPTH_END = "+this.depth+";\n\n float norm = 0.0;\n for (int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k) {\n if (k < depthBegin){\n continue;\n }\n else if (k >= depthBegin && k < depthEnd) {\n norm += getInputImage(b, r, c, k) * getInputImage(b, r, c, k);\n }\n else {\n break;\n }\n }\n\n norm = float("+r+") * norm + float("+n+");\n\n for(int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k){\n if (k < depthBegin){\n continue;\n }\n else if (k >= depthBegin && k < depthEnd){\n float dyi = -2.0 * float("+r+")\n * float("+i+")\n * getInputImage(b ,r ,c, k) * getOutputImage(b, r, c, d)\n / norm;\n if (k == d) {\n dyi += pow(norm, -1.0 * "+i+");\n }\n if (k == coords[3]) {\n dyi *= getDy(b, r, c, d);\n result += dyi;\n }\n }\n else {\n break;\n }\n }\n }\n setOutput(result);\n }\n "}(e.shape,r,i,o,a);return this.compileAndRun(s,[e,n,t])},t.prototype.tile=function(t,e){var n=new function(t,e){this.variableNames=["A"];for(var n=new Array(t.length),r=0;r5)throw Error("Tile for rank "+e+" is not yet supported");if(1===e)return"imod(resRC, "+t[0]+")";for(var n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u"],r=[],i=0;i= end":"any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))",d="",p=0,m=1===r?2:4;p= end) {\n setOutput(float("+n+"));\n } else {\n setOutput(getX(outC - start));\n }\n }\n "}(t.shape,e,n);return this.compileAndRun(r,[t])},t.prototype.transpose=function(t,e){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.transpose(t,e);var n=f.getBool("WEBGL_PACK_ARRAY_OPERATIONS")?new function(t,e){this.variableNames=["A"],this.usesPackedTextures=!0;for(var n=new Array(t.length),r=0;r6)throw Error("Packed transpose for rank "+this.rank+" is not yet supported.");var i=vi(this.rank),o=ni("rc",this.rank),a=new Array(this.rank);for(r=0;r6)throw Error("Transpose for rank "+e+" is not yet supported");for(var n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u","resRC.v"],r=new Array(e),i=0;i4)throw Error("Gather for rank "+n+" is not yet supported");if(1===n)return"int(getIndices(resRC))";for(var r=["resRC.x","resRC.y","resRC.z","resRC.w"],i=[],o=0;o 4 with a WebGL backend not implemented yet"});var r=e.reduce(function(t,e){return t*e}),i=Je(t.shape,e,r),o=Ze(i.length,e.length),a=Qe(t.shape,e,r),s=tn(n,e.length),u=en(a,n,e.length);return t.reshape(i).transpose(o).reshape(a).slice(s,u)},t.prototype.spaceToBatchND=function(t,e,n){w(t.rank<=4,function(){return"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet"});var r=e.reduce(function(t,e){return t*e}),i=[[0,0]];i.push.apply(i,n);for(var o=1+e.length;o= 1.0 && floatedReducedAllValue >= 1.0);\n ",h="bvec4"):"any"===e&&(a="0.0",f="\n bool reducedAnyValue = any(values);\n float floatedReducedAnyValue = float(reducedAnyValue);\n anyValue = float(anyValue >= 1.0 || floatedReducedAnyValue >= 1.0);\n ",h="bvec4");var d="";i%n>0&&(d="\n if (inIdx < 0 || inIdx >= "+i+") {\n return initializationValue;\n }\n "),this.userCode="\n const float initializationValue = "+a+";\n const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);\n\n float getValue(int batch, int inIdx) {\n "+d+"\n return getX(batch, inIdx);\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = outIdx * "+n+";\n\n vec4 minMaxValue = vec4("+a+");\n float prodValue = 1.0;\n float sumValue = 0.0;\n float allValue = 1.0;\n float anyValue = 0.0;\n\n for (int i = 0; i < "+c+"; i += 4) {\n int inIdx = inOffset + i;\n "+h+" values = "+h+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n "+f+"\n }\n\n int inIdx = inOffset + "+c+";\n if ("+(1===l)+") {\n "+h+" values = "+h+"(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(2===l)+") {\n "+h+" values = "+h+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(3===l)+") {\n "+h+" values = "+h+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n "+f+"\n }\n setOutput("+u+");\n }\n "}({windowSize:dn(i),inSize:i,batchSize:r},e),a=o.outputShape,s=a[0],u=a[1],c=this.makeOutputArray([s,u],n);return this.compileAndRun(o,[t],c),1===c.shape[1]?c:this.reduce(c,e,n)},t.prototype.argReduce=function(t,e,n){void 0===n&&(n=null);var r=t.shape[0],i=t.shape[1];null!=n&&(r=n.shape[0],i=n.shape[1]);var o=new function(t,e,n){this.variableNames=["A"];var r=t.windowSize,i=t.batchSize,o=t.inSize,a=Math.ceil(o/r);n||this.variableNames.push("bestIndicesA"),this.outputShape=[i,a];var s="max"===e?">":"<",u=n?"inOffset + i;":"round(getBestIndicesA(batch, inOffset + i));";this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = outIdx * "+r+";\n\n int bestIndex = inOffset;\n float bestValue = getA(batch, bestIndex);\n\n for (int i = 0; i < "+r+"; i++) {\n int inIdx = "+u+";\n float candidate = getA(batch, inIdx);\n if (candidate "+s+" bestValue) {\n bestValue = candidate;\n bestIndex = inIdx;\n }\n }\n setOutput(float(bestIndex));\n }\n "}({windowSize:dn(i),inSize:i,batchSize:r},e,null==n),a=o.outputShape,s=a[0],u=a[1],c=this.makeOutputArray([s,u],"int32"),l=[t];return null!=n&&l.push(n),this.compileAndRun(o,l,c),1===c.shape[1]?c:this.argReduce(t,e,c)},t.prototype.argReducePacked=function(t,e,n){void 0===n&&(n=null);var r=null!=n?n.shape:t.shape,i=new function(t,e,n,r){this.variableNames=["A"],this.usesPackedTextures=!0,w(t.length>2,function(){return"Packed arg"+(n.charAt(0).toUpperCase()+n.slice(1))+" supports only inputs with rank above 2."});var i=t[t.length-1],o=Math.ceil(i/e);this.outputShape=t.slice(0,-1),o>1&&this.outputShape.push(o),r||this.variableNames.push("bestIndicesA");var a,s,u=this.outputShape,c=u.length,l=vi(c),f=ri("coords",c);if(1===o){var h=vi(s=c+1);a="\n "+h+" sourceLocR = "+h+"("+f.join()+", 0);\n ++"+f[c-1]+";\n "+h+" sourceLocG = "+h+"("+f.join()+", 0);\n ++"+f[c-2]+";\n "+h+" sourceLocA = "+h+"("+f.join()+", 0);\n --"+f[c-1]+";\n "+h+" sourceLocB = "+h+"("+f.join()+", 0);\n --"+f[c-2]+";"}else s=c,a="\n "+l+" sourceLocR = coords;\n ++"+f[c-1]+";\n "+l+" sourceLocG = coords;\n ++"+f[c-2]+";\n "+l+" sourceLocA = coords;\n --"+f[c-1]+";\n "+l+" sourceLocB = coords;\n --"+f[c-2]+";";var d=["x","y","z","w","u","v"].slice(0,s),p="."+d[s-1],m=d.map(function(t){return"int "+t}),v=ri("sourceLocR",s-1).concat("inIdx.r"),g=ri("sourceLocG",s-1).concat("inIdx.g"),y=ri("sourceLocB",s-1).concat("inIdx.b"),b=ri("sourceLocA",s-1).concat("inIdx.a"),x="max"===n?"greaterThan":"lessThan",k=r?"":"\n inIdx = round(vec4(getBestIndicesAChannel("+v.join()+"),\n getBestIndicesAChannel("+g.join()+"),\n getBestIndicesAChannel("+y.join()+"),\n getBestIndicesAChannel("+b.join()+")));",S="vec4(\n getAChannel("+v.join()+"),\n hasNextCol ? getAChannel("+g.join()+") : 0.,\n hasNextRow ? getAChannel("+y.join()+") : 0.,\n hasNextRow && hasNextCol ? getAChannel("+b.join()+") : 0.)",_=r?"":"\n float getBestIndicesAChannel("+m.join()+") {\n return getChannel(getBestIndicesA("+d.join()+"),\n vec2("+d.slice(-2).join()+"));\n }";this.userCode="\n float getAChannel("+m.join()+") {\n return getChannel(getA("+d.join()+"),\n vec2("+d.slice(-2).join()+"));\n }\n "+_+"\n void main() {\n "+l+" coords = getOutputCoords();\n bool hasNextCol = "+f[c-1]+" < "+(u[c-1]-1)+";\n bool hasNextRow = "+f[c-2]+" < "+(u[c-2]-1)+";\n "+a+"\n ivec4 srcIdx = ivec4(sourceLocR"+p+", sourceLocG"+p+",\n sourceLocB"+p+", sourceLocA"+p+") * "+e+";\n ivec4 inIdx = srcIdx;\n vec4 bestIndex = vec4(inIdx);\n vec4 bestValue = "+S+";\n\n for (int i = 0; i < "+e+"; i++) {\n inIdx = srcIdx;\n "+k+"\n vec4 candidate = "+S+";\n bvec4 nan = isnan(candidate);\n bvec4 replace = bvec4(\n vec4("+x+"(candidate, bestValue)) * (vec4(1.0) - vec4(nan)));\n\n bestValue = vec4(replace.x ? candidate.x : bestValue.x,\n replace.y ? candidate.y : bestValue.y,\n replace.z ? candidate.z : bestValue.z,\n replace.w ? candidate.w : bestValue.w);\n bestIndex = mix(bestIndex, vec4(inIdx), vec4(replace));\n srcIdx++;\n }\n setOutput(bestIndex);\n }\n "}(r,dn(r[r.length-1]),e,null==n),o=this.makePackedTensor(i.outputShape,"int32"),a=null==n?[t]:[t,n];return this.compileAndRun(i,a,o),o.rank===t.rank?this.argReducePacked(t,e,o):o},t.prototype.sum=function(t,e){an("sum",e,t.rank);var n=rn(t.shape,e),r=n[0],i=_(n[1]),o=t.as2D(-1,i),a=Ct(t.dtype);return this.reduce(o,"sum",a).reshape(r)},t.prototype.prod=function(t,e){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.prod(t,e);var n=rn(t.shape,e),r=n[0],i=_(n[1]),o=t.as2D(-1,i),a=Ct(t.dtype);return this.reduce(o,"prod",a).reshape(r)},t.prototype.unsortedSegmentSum=function(t,e,n){var r=0,i=sn([r],t.rank),o=t;null!=i&&(o=t.transpose(i),r=cn(1,t.rank)[0]);var a=function(t,e,n){for(var r=[],i=t.length,o=0;oe||n===t?r=!0:n=K(t,n+1);return n}(a,i),u=new function(t,e){this.variableNames=["x","segmentIds"];var n=t.windowSize,r=t.batchSize,i=t.inSize,o=t.numSegments,a=o*Math.ceil(i/n);this.outputShape=[r,a];var s=4*Math.floor(n/4),u=n%4,c="\n sumValue += dot(values, segFilter);\n ",l="";i%n>0&&(l="\n if (inIdx < 0 || inIdx >= "+i+") {\n return initializationValue;\n }\n ");var f="";i%n>0&&(f="\n if (inIdx < 0 || inIdx >= "+i+") {\n return -1.0;\n }\n "),this.userCode="\n const float initializationValue = 0.0;\n\n float getValue(int batch, int inIdx) {\n "+l+"\n return getX(batch, inIdx);\n }\n\n float getSegmentIdAtIndex(int inIdx) {\n "+f+"\n return getSegmentIds(inIdx);\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = int(floor(float(outIdx) / float(\n "+o+")) * float("+n+"));\n int currentSeg = int(mod(float(outIdx), float("+o+")));\n\n float sumValue = 0.0;\n\n for (int i = 0; i < "+s+"; i += 4) {\n int inIdx = inOffset + i;\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n vec4 segFilter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 3)) == currentSeg ? 1 : 0\n );\n\n "+c+"\n }\n\n int inIdx = inOffset + "+s+";\n if ("+(1===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n int inIdxSeg = int(getSegmentIdAtIndex(inIdx));\n\n vec4 segFilter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n 0,\n 0,\n 0\n );\n\n "+c+"\n } else if ("+(2===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n vec4 segFilter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n 0,\n 0\n );\n\n "+c+"\n } else if ("+(3===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n vec4 segFilter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,\n 0\n );\n\n "+c+"\n }\n setOutput(sumValue);\n }\n "}({windowSize:s,inSize:a,batchSize:o,numSegments:i},e),c=u.outputShape,l=c[0],f=c[1],h=this.makeOutputArray([l,f],r);return this.compileAndRun(u,[t,n],h),h.shape[1]===i?h:(n=$n(0,i).tile([a/s]),this.segOpCompute(h,e,n,r,i))},t.prototype.argMinMaxReduce=function(t,e,n){var r=[e];if(an("arg"+n.charAt(0).toUpperCase()+n.slice(1),r,t.rank),!f.getBool("WEBGL_PACK_REDUCE")||t.rank<=2){var i=rn(t.shape,r),o=i[0],a=_(i[1]),s=t.as2D(-1,a);return this.argReduce(s,n).reshape(o)}return this.argReducePacked(t,n)},t.prototype.argMin=function(t,e){return this.argMinMaxReduce(t,e,"min")},t.prototype.argMax=function(t,e){return this.argMinMaxReduce(t,e,"max")},t.prototype.cumsum=function(t,e,n,r){if(e!==t.rank-1)throw new Error("WebGL cumsum shader expects an inner-most axis="+(t.rank-1)+" but got axis="+e);var i=new function(t,e,n){this.variableNames=["x"],this.outputShape=t;var r=t.length,i=t[t.length-1],o=n?"<":">";this.userCode="\n int getIndex(int i) {\n "+(n?"return "+i+" -i - 1;":"return i;")+"\n }\n\n void main() {\n "+vi(r)+" coords = getOutputCoords();\n int end = "+Ni(r,"coords")+";\n float val = 0.0;\n for (int i = "+i+" - 1; i >= 0; i -= 1) {\n int idx = getIndex(i);\n if (idx "+o+" end) {\n continue;\n }\n if (idx == end && "+e+") {\n continue;\n }\n "+Ni(r,"coords")+" = idx;\n val += getX("+function(t,e){if(1===t)return""+e;if(2===t)return e+".x, "+e+".y";if(3===t)return e+".x, "+e+".y, "+e+".z";if(4===t)return e+".x, "+e+".y, "+e+".z, "+e+".w";throw Error("Cumulative sum for rank "+t+" is not yet supported")}(r,"coords")+");\n }\n setOutput(val);\n }\n "}(t.shape,n,r);return this.compileAndRun(i,[t])},t.prototype.equal=function(t,e){if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(equal(a, b));\n","bool");var n=new Si("return float(a == b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.notEqual=function(t,e){if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(notEqual(a, b));\n","bool");var n=new Si("return float(a != b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.less=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.less(t,e);if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(lessThan(a, b));\n","bool");var n=new Si("return float(a < b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.lessEqual=function(t,e){if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(lessThanEqual(a, b));\n","bool");var n=new Si("return float(a <= b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.greater=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.greater(t,e);if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(greaterThan(a, b));\n","bool");var n=new Si("return float(a > b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.greaterEqual=function(t,e){if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(greaterThanEqual(a, b));\n","bool");var n=new Si("return float(a >= b);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.logicalNot=function(t){var e=new wo(t.shape,"return float(!(x >= 1.0));");return this.compileAndRun(e,[t])},t.prototype.logicalAnd=function(t,e){if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return vec4(\n vec4(greaterThanEqual(a, vec4(1.0))) *\n vec4(greaterThanEqual(b, vec4(1.0))));\n","bool");var n=new Si("return float(a >= 1.0 && b >= 1.0);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.logicalOr=function(t,e){if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n return min(\n vec4(greaterThanEqual(a, vec4(1.0))) +\n vec4(greaterThanEqual(b, vec4(1.0))),\n vec4(1.0));\n","bool");var n=new Si("return float(a >= 1.0 || b >= 1.0);",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[t,e],r)},t.prototype.select=function(t,e,n){var r=new function(t,e,n){var r,i;if(this.variableNames=["c","a","b"],this.outputShape=e,n>4)throw Error("Where for rank "+n+" is not yet supported");if(1===n)i="resRC",r="resRC";else{for(var o=["resRC.x","resRC.y","resRC.z","resRC.w"],a=[],s=[],u=0;u= 1.0) {\n setOutput(getA("+i+"));\n } else {\n setOutput(getB("+i+"));\n }\n }\n "}(t.rank,e.shape,e.rank),i=this.makeOutputArray(r.outputShape,Et(e.dtype,n.dtype));return this.compileAndRun(r,[t,e,n],i)},t.prototype.where=function(t){Ye("tf.where() in webgl locks the UI thread. Call tf.whereAsync() instead");var e=t.dataSync();return ei(t.shape,e)},t.prototype.topk=function(t,e,n){return lr(t.dataSync(),t.shape,t.dtype,e)},t.prototype.min=function(t,e){an("min",e,t.rank);var n=rn(t.shape,e),r=n[0],i=_(n[1]),o=t.as2D(-1,i);return this.reduce(o,"min",o.dtype).reshape(r)},t.prototype.minimum=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.minimum(t,e);var n=f.getBool("WEBGL_PACK_BINARY_OPERATIONS")?new _i("\n vec4 result = vec4(min(a, b));\n vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));\n \n result.r = isNaN.r > 0. ? NAN : result.r;\n result.g = isNaN.g > 0. ? NAN : result.g;\n result.b = isNaN.b > 0. ? NAN : result.b;\n result.a = isNaN.a > 0. ? NAN : result.a;\n\n return result;\n",t.shape,e.shape):new Si("\n if (isnan(a)) return a;\n if (isnan(b)) return b;\n\n return min(a, b);\n",t.shape,e.shape);return this.compileAndRun(n,[t,e])},t.prototype.mod=function(t,e){var n=f.getBool("WEBGL_PACK_BINARY_OPERATIONS")?new _i("\n vec4 result = mod(a, b);\n vec4 isNaN = vec4(equal(b, vec4(0.0)));\n \n result.r = isNaN.r > 0. ? NAN : result.r;\n result.g = isNaN.g > 0. ? NAN : result.g;\n result.b = isNaN.b > 0. ? NAN : result.b;\n result.a = isNaN.a > 0. ? NAN : result.a;\n\n return result;\n",t.shape,e.shape):new Si("if (b == 0.0) return NAN;\n return mod(a, b);",t.shape,e.shape);return this.compileAndRun(n,[t,e])},t.prototype.max=function(t,e){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.max(t,e);an("max",e,t.rank);var n=rn(t.shape,e),r=n[0],i=_(n[1]),o=t.as2D(-1,i);return this.reduce(o,"max",o.dtype).reshape(r)},t.prototype.maximum=function(t,e){if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.maximum(t,e);var n=f.getBool("WEBGL_PACK_BINARY_OPERATIONS")?new _i("\n vec4 result = vec4(max(a, b));\n vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));\n \n result.r = isNaN.r > 0. ? NAN : result.r;\n result.g = isNaN.g > 0. ? NAN : result.g;\n result.b = isNaN.b > 0. ? NAN : result.b;\n result.a = isNaN.a > 0. ? NAN : result.a;\n\n return result;\n",t.shape,e.shape):new Si("\n if (isnan(a)) return a;\n if (isnan(b)) return b;\n\n return max(a, b);\n",t.shape,e.shape);return this.compileAndRun(n,[t,e])},t.prototype.all=function(t,e){an("all",e,t.rank);var n=rn(t.shape,e),r=n[0],i=_(n[1]),o=t.as2D(-1,i);return this.reduce(o,"all",o.dtype).reshape(r)},t.prototype.any=function(t,e){an("any",e,t.rank);var n=rn(t.shape,e),r=n[0],i=_(n[1]),o=t.as2D(-1,i);return this.reduce(o,"any",o.dtype).reshape(r)},t.prototype.squaredDifference=function(t,e){var n=f.getBool("WEBGL_PACK_BINARY_OPERATIONS")?new _i("return (a - b) * (a - b);",t.shape,e.shape):new Si("return (a - b) * (a - b);",t.shape,e.shape);return this.compileAndRun(n,[t,e])},t.prototype.realDivide=function(t,e){if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n // vec4 one = vec4(equal(a, b));\n // return one + (vec4(1.0) - one) * a / b;\n vec4 result = a / b;\n if(b.x == 0.0) {\n result.x = NAN;\n } else if(a.x == b.x) {\n result.x = 1.;\n }\n if(b.y == 0.0) {\n result.y = NAN;\n } else if(a.y == b.y) {\n result.y = 1.;\n }\n if(b.z == 0.0) {\n result.z = NAN;\n } else if(a.z == b.z) {\n result.z = 1.;\n }\n if(b.w == 0.0) {\n result.w = NAN;\n } else if(a.w == b.w) {\n result.w = 1.;\n }\n \n return result;\n","float32",!0);var n=new Si("\nif (b == 0.0) {\n return NAN;\n} \nif (a == b) {\n return 1.0;\n};\nreturn a / b;",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"float32");return this.compileAndRun(n,[t,e],r)},t.prototype.floorDiv=function(t,e){if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,"\n ivec4 ia = round(a);\n ivec4 ib = round(b);\n bvec4 cond = notEqual(ib, ivec4(0));\n ivec4 result = ivec4(0);\n vec4 s = sign(a) * sign(b);\n\n // Windows (D3D) wants guaranteed non-zero int division at compile-time.\n if (cond[0]) {\n result[0] = idiv(ia[0], ib[0], s[0]);\n }\n if (cond[1]) {\n result[1] = idiv(ia[1], ib[1], s[1]);\n }\n if (cond[2]) {\n result[2] = idiv(ia[2], ib[2], s[2]);\n }\n if (cond[3]) {\n result[3] = idiv(ia[3], ib[3], s[3]);\n }\n return vec4(result);\n","int32");var n=new Si("\n float s = sign(a) * sign(b);\n int ia = round(a);\n int ib = round(b);\n if (ib != 0) {\n // Windows (D3D) wants guaranteed non-zero int division at compile-time.\n return float(idiv(ia, ib, s));\n } else {\n return NAN;\n }\n",t.shape,e.shape),r=this.makeOutputArray(n.outputShape,"int32");return this.compileAndRun(n,[t,e],r)},t.prototype.add=function(t,e){if("complex64"===t.dtype&&"complex64"===e.dtype)return this.complexSeparableBinaryOp(t,e,wi);if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.add(t,e);var n=Et(t.dtype,e.dtype);if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,wi,n);var r=new Si(wi,t.shape,e.shape),i=this.makeOutputArray(r.outputShape,n);return this.compileAndRun(r,[t,e],i)},t.prototype.packedBinaryOp=function(t,e,n,r,i){void 0===i&&(i=!1);var o=new _i(n,t.shape,e.shape,i),a=this.makePackedTensor(o.outputShape,r);return this.compileAndRun(o,[t,e],a)},t.prototype.complexSeparableBinaryOp=function(t,e,n){var r=this,i=this.texData.get(t.dataId),o=this.texData.get(e.dataId),a=[[i.complexTensors.real,o.complexTensors.real],[i.complexTensors.imag,o.complexTensors.imag]].map(function(i){var o=i[0],a=i[1],s=r.makeComplexComponentTensorHandle(t,o),u=r.makeComplexComponentTensorHandle(e,a),c=new Si(n,t.shape,e.shape),l=r.makeOutputArray(c.outputShape,Et(o.dtype,a.dtype));return r.compileAndRun(c,[s,u],l)}),s=a[0],u=a[1],c=this.complex(s,u);return s.dispose(),u.dispose(),c},t.prototype.makeComplexComponentTensorHandle=function(t,e){return{dataId:e.dataId,dtype:e.dtype,shape:t.shape}},t.prototype.addN=function(t){if(1===t.length)return t[0];if(t.length>f.get("WEBGL_MAX_TEXTURES_IN_SHADER")){var e=Math.floor(t.length/2),n=this.addN(t.slice(0,e)),r=this.addN(t.slice(e));return this.addN([n,r])}var i=t.map(function(t){return t.dtype}).reduce(function(t,e){return Et(t,e)}),o=t.map(function(t){return t.shape}),a=f.getBool("WEBGL_PACK"),s=a?new function(t,e){this.outputShape=[],this.usesPackedTextures=!0,this.outputShape=t,this.variableNames=e.map(function(t,e){return"T"+e});var n=[];this.variableNames.forEach(function(t){n.push("vec4 v"+t+" = get"+t+"AtOutCoords();")});var r=this.variableNames.map(function(t){return"v"+t}).join(" + ");this.userCode="\n void main() {\n "+n.join("\n ")+"\n\n vec4 result = "+r+";\n setOutput(result);\n }\n "}(t[0].shape,o):new function(t,e){this.outputShape=[],this.outputShape=t,this.variableNames=e.map(function(t,e){return"T"+e});var n=[];this.variableNames.forEach(function(t){n.push("float v"+t+" = get"+t+"AtOutCoords();")});var r=this.variableNames.map(function(t){return"v"+t}).join(" + ");this.userCode="\n void main() {\n "+n.join("\n ")+"\n\n float result = "+r+";\n setOutput(result);\n }\n "}(t[0].shape,o),u=a?this.makePackedTensor(s.outputShape,i):this.makeOutputArray(s.outputShape,i);return this.compileAndRun(s,t,u)},t.prototype.subtract=function(t,e){if("complex64"===t.dtype&&"complex64"===e.dtype)return this.complexSeparableBinaryOp(t,e,xi);if(this.shouldExecuteOnCPU([t,e]))return this.cpuBackend.subtract(t,e);var n=Et(t.dtype,e.dtype);if(f.getBool("WEBGL_PACK_BINARY_OPERATIONS"))return this.packedBinaryOp(t,e,xi,t.dtype);var r=new Si(xi,t.shape,e.shape),i=this.makeOutputArray(r.outputShape,n);return this.compileAndRun(r,[t,e],i)},t.prototype.pow=function(t,e){var n=f.getBool("WEBGL_PACK_BINARY_OPERATIONS"),r=n?new _i("\n // isModRound1 has 1 for components with round(mod(b, 2.0)) == 1, 0 otherwise.\n vec4 isModRound1 = vec4(equal(round(mod(b, 2.0)), ivec4(1)));\n vec4 multiplier = sign(a) * isModRound1 + (vec4(1.0) - isModRound1);\n vec4 result = multiplier * pow(abs(a), b);\n\n vec4 isNaN = vec4(lessThan(a, vec4(0.0))) * vec4(lessThan(floor(b), b));\n \n result.r = isNaN.r > 0. ? NAN : result.r;\n result.g = isNaN.g > 0. ? NAN : result.g;\n result.b = isNaN.b > 0. ? NAN : result.b;\n result.a = isNaN.a > 0. ? NAN : result.a;\n\n return result;\n",t.shape,e.shape):new Si("\nif(a < 0.0 && floor(b) < b){\n return NAN;\n}\nreturn (round(mod(b, 2.0)) != 1) ?\n pow(abs(a), b) : sign(a) * pow(abs(a), b);\n",t.shape,e.shape),i=Et(t.dtype,e.dtype),o=n?this.makePackedTensor(r.outputShape,i):this.makeOutputArray(r.outputShape,i);return this.compileAndRun(r,[t,e],o)},t.prototype.ceil=function(t){var e=new wo(t.shape,"return ceil(x);");return this.compileAndRun(e,[t])},t.prototype.floor=function(t){var e=new wo(t.shape,"return floor(x);");return this.compileAndRun(e,[t])},t.prototype.sign=function(t){var e=new wo(t.shape,"\n if (isnan(x)) { return 0.0; }\n return sign(x);\n");return this.compileAndRun(e,[t])},t.prototype.isNaN=function(t){var e=new wo(t.shape,"return float(isnan(x));"),n=this.makeOutputArray(e.outputShape,"bool");return this.compileAndRun(e,[t],n)},t.prototype.isInf=function(t){var e=new wo(t.shape,"return float(isinf(x));"),n=this.makeOutputArray(e.outputShape,"bool");return this.compileAndRun(e,[t],n)},t.prototype.isFinite=function(t){var e=new wo(t.shape,"return float(!isnan(x) && !isinf(x));"),n=this.makeOutputArray(e.outputShape,"bool");return this.compileAndRun(e,[t],n)},t.prototype.round=function(t){var e=new wo(t.shape,"\n // OpenGL ES does not support round function.\n // The algorithm is based on banker's rounding.\n float base = floor(x);\n if ((x - base) < 0.5) {\n return floor(x);\n } else if ((x - base) > 0.5) {\n return ceil(x);\n } else {\n if (mod(base, 2.0) == 0.0) {\n return base;\n } else {\n return base + 1.0;\n }\n }\n");return this.compileAndRun(e,[t])},t.prototype.exp=function(t){var e;return e=f.getBool("WEBGL_PACK")?new Eo(t.shape,So):new wo(t.shape,So),this.compileAndRun(e,[t])},t.prototype.expm1=function(t){var e=new wo(t.shape,"return exp(x) - 1.0;");return this.compileAndRun(e,[t])},t.prototype.log=function(t){var e;return e=f.getBool("WEBGL_PACK")?new Eo(t.shape,"\n vec4 result = log(x);\n vec4 isNaN = vec4(lessThan(x, vec4(0.0)));\n result.r = isNaN.r == 1.0 ? NAN : result.r;\n result.g = isNaN.g == 1.0 ? NAN : result.g;\n result.b = isNaN.b == 1.0 ? NAN : result.b;\n result.a = isNaN.a == 1.0 ? NAN : result.a;\n\n return result;\n"):new wo(t.shape,"if (x < 0.0) return NAN;\n return log(x);"),this.compileAndRun(e,[t])},t.prototype.log1p=function(t){var e=new wo(t.shape,"return log(1.0 + x);");return this.compileAndRun(e,[t])},t.prototype.sqrt=function(t){var e=new wo(t.shape,"return sqrt(x);");return this.compileAndRun(e,[t])},t.prototype.rsqrt=function(t){if(this.shouldExecuteOnCPU([t]))return this.cpuBackend.rsqrt(t);var e=new wo(t.shape,"return inversesqrt(x);");return this.compileAndRun(e,[t])},t.prototype.square=function(t){var e=new wo(t.shape,"return x * x;");return this.compileAndRun(e,[t])},t.prototype.reciprocal=function(t){var e=new wo(t.shape,"return 1.0 / x;");return this.compileAndRun(e,[t])},t.prototype.relu=function(t){var e;return e=f.getBool("WEBGL_PACK")?new Eo(t.shape,_o):new wo(t.shape,ko),this.compileAndRun(e,[t])},t.prototype.prelu=function(t,e){var n=f.getBool("WEBGL_PACK_BINARY_OPERATIONS")?new _i("\n vec4 aLessThanZero = vec4(lessThan(a, vec4(0.)));\n return (aLessThanZero * (b * a)) + ((vec4(1.0) - aLessThanZero) * a);\n",t.shape,e.shape):new Si("return (a < 0.) ? b * a : a;",t.shape,e.shape);return this.compileAndRun(n,[t,e])},t.prototype.elu=function(t){var e=new wo(t.shape,"return (x >= 0.0) ? x : (exp(x) - 1.0);");return this.compileAndRun(e,[t])},t.prototype.eluDer=function(t,e){var n=f.getBool("WEBGL_PACK_BINARY_OPERATIONS")?new _i("\n vec4 bGTEZero = vec4(greaterThanEqual(b, vec4(0.)));\n return (bGTEZero * a) + ((vec4(1.0) - bGTEZero) * (a * (b + vec4(1.0))));\n",t.shape,e.shape):new Si("return (b >= 1.0) ? a : a * (b + 1.0);",t.shape,e.shape);return this.compileAndRun(n,[t,e])},t.prototype.selu=function(t){var e=new wo(t.shape,"\n // Stable and Attracting Fixed Point (0, 1) for Normalized Weights.\n // see: https://arxiv.org/abs/1706.02515\n float scaleAlpha = 1.7580993408473768;\n float scale = 1.0507009873554805;\n return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);\n");return this.compileAndRun(e,[t])},t.prototype.int=function(t){var e=new wo(t.shape,"return float(int(x));"),n=this.makeOutputArray(e.outputShape,"int32");return this.compileAndRun(e,[t],n)},t.prototype.clip=function(t,e,n){var r,i=(r=f.getBool("WEBGL_PACK_CLIP")?new Ci(t.shape):new Ei(t.shape)).getCustomSetupFunc(e,n);return this.compileAndRun(r,[t],null,i)},t.prototype.abs=function(t){var e=new wo(t.shape,"return abs(x);");return this.compileAndRun(e,[t])},t.prototype.complexAbs=function(t){var e=this.texData.get(t.dataId),n=new function(t){this.variableNames=["real","imag"],this.outputShape=t,this.userCode="\n void main() {\n float re = abs(getRealAtOutCoords());\n float im = abs(getImagAtOutCoords());\n float mx = max(re, im);\n\n // sadly the length function in glsl is not underflow-safe\n // (at least not on Intel GPUs). So the safe solution is\n // to ensure underflow-safety in all cases.\n setOutput(\n mx == 0.0 ? 0.0 : mx * length(vec2(1, min(re, im)/mx))\n );\n }\n "}(t.shape),r=[this.makeComplexComponentTensorHandle(t,e.complexTensors.real),this.makeComplexComponentTensorHandle(t,e.complexTensors.imag)];return this.compileAndRun(n,r)},t.prototype.sigmoid=function(t){var e=new wo(t.shape,"return 1.0 / (1.0 + exp(-1.0 * x));");return this.compileAndRun(e,[t])},t.prototype.softplus=function(t){var e=new wo(t.shape,"\n float epsilon = 1.1920928955078125e-7;\n float threshold = log(epsilon) + 2.0;\n\n bool too_large = x > -threshold;\n bool too_small = x < threshold;\n\n float result;\n float exp_x = exp(x);\n\n if (too_large){\n result = x;\n }\n else if (too_small){\n result = exp_x;\n }\n else{\n result = log(exp_x + 1.0);\n }\n return result;\n");return this.compileAndRun(e,[t])},t.prototype.sin=function(t){var e=new wo(t.shape,"if (isnan(x)) return x;\n return sin(x);\n");return this.compileAndRun(e,[t])},t.prototype.cos=function(t){var e=new wo(t.shape,"if (isnan(x)) return x;\n return cos(x);\n");return this.compileAndRun(e,[t])},t.prototype.tan=function(t){var e=new wo(t.shape,"return tan(x);");return this.compileAndRun(e,[t])},t.prototype.asin=function(t){var e=new wo(t.shape,"return asin(x);");return this.compileAndRun(e,[t])},t.prototype.acos=function(t){var e=new wo(t.shape,"return acos(x);");return this.compileAndRun(e,[t])},t.prototype.atan=function(t){var e=new wo(t.shape,"if (isnan(x)) return x;\n return atan(x);\n");return this.compileAndRun(e,[t])},t.prototype.atan2=function(t,e){var n=f.getBool("WEBGL_PACK_BINARY_OPERATIONS")?new _i("\n vec4 result = atan(a, b);\n vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0));\n \n result.r = isNaN.r > 0. ? NAN : result.r;\n result.g = isNaN.g > 0. ? NAN : result.g;\n result.b = isNaN.b > 0. ? NAN : result.b;\n result.a = isNaN.a > 0. ? NAN : result.a;\n\n return result;\n",t.shape,e.shape):new Si("\n if (isnan(a)) return a;\n if (isnan(b)) return b;\n\n return atan(a, b);\n",t.shape,e.shape);return this.compileAndRun(n,[t,e])},t.prototype.sinh=function(t){var e=new wo(t.shape,"\n float e2x = exp(x);\n return (e2x - 1.0 / e2x) / 2.0;\n");return this.compileAndRun(e,[t])},t.prototype.cosh=function(t){var e=new wo(t.shape,"\n float e2x = exp(-x);\n return (e2x + 1.0 / e2x) / 2.0;\n");return this.compileAndRun(e,[t])},t.prototype.tanh=function(t){var e=new wo(t.shape,"\n float e2x = exp(-2.0 * abs(x));\n return sign(x) * (1.0 - e2x) / (1.0 + e2x);\n");return this.compileAndRun(e,[t])},t.prototype.asinh=function(t){var e=new wo(t.shape,"return log(x + sqrt(x * x + 1.0));");return this.compileAndRun(e,[t])},t.prototype.acosh=function(t){var e=new wo(t.shape,"if (isnan(x)) return x;\n if (x < 1.0) return NAN;\n return log(x + sqrt(x * x - 1.0));");return this.compileAndRun(e,[t])},t.prototype.atanh=function(t){var e=new wo(t.shape,"if (isnan(x)) return x;\n if ((x < -1.0) || (x > 1.0)) return NAN;\n return (log(1.0 + x) - log(1.0 - x)) / 2.0;");return this.compileAndRun(e,[t])},t.prototype.erf=function(t){var e=new wo(t.shape,'\n // Error function is calculated approximately with elementary function.\n // See "Handbook of Mathematical Functions with Formulas,\n // Graphs, and Mathematical Tables", Abramowitz and Stegun.\n float p = 0.3275911;\n float a1 = 0.254829592;\n float a2 = -0.284496736;\n float a3 = 1.421413741;\n float a4 = -1.453152027;\n float a5 = 1.061405429;\n\n float t = 1.0 / (1.0 + p * x);\n return 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);\n');return this.compileAndRun(e,[t])},t.prototype.step=function(t,e){var n=new wo(t.shape,function(t){return void 0===t&&(t=0),xo+"\n return x > 0.0 ? 1.0 : float("+t+");\n "}(e));return this.compileAndRun(n,[t])},t.prototype.conv2dByMatMul=function(t,e,n){var r=t.shape,i=this.texData.get(t.dataId),o=n.inChannels,a=r[0]*r[1]*r[2],s=n.outChannels,u=(1===a||1===s)&&o>1e3,c=r[2]%2!=0&&!!i.isPacked;if(u||!f.getBool("WEBGL_LAZILY_UNPACK")||!f.getBool("WEBGL_PACK_BINARY_OPERATIONS")||!c){var l=this.reshape(t,[1,r[0]*r[1]*r[2],n.inChannels]),h=this.reshape(e,[1,n.inChannels,n.outChannels]);return this.reshape(this.batchMatMul(l,h,!1,!1),n.outShape)}var d=vt.make([1,r[0]*r[1]*(r[2]+1),n.inChannels],{dataId:t.dataId},t.dtype,this),p=i.shape;i.shape=i.shape.slice(),i.shape[i.shape.length-2]++,w(we(i.shape,d.shape),function(){return"packed reshape "+i.shape+" to "+d.shape+" isn't free"});var m=this.reshape(e,[1,n.inChannels,n.outChannels]),v=this.batchMatMul(d,m,!1,!1),g=this.texData.get(v.dataId);return w(g.isPacked,function(){return"batchMatMul result is expected to be packed"}),i.shape=p,g.shape=n.outShape,vt.make(n.outShape,{dataId:v.dataId},v.dtype,this)},t.prototype.conv2dWithIm2Row=function(t,e,n){var r=n.filterWidth,i=n.filterHeight,o=n.inChannels,a=n.outWidth,s=n.outHeight,u=r*i*o,c=s*a,l=[u,c],f=t.squeeze([0]),h=e.reshape([1,u,-1]),d=new function(t,e,n){this.variableNames=["A"],this.usesPackedTextures=!0,this.outputShape=t;var r=n.filterWidth,i=n.inChannels,o=n.strideWidth,a=n.strideHeight,s=n.padInfo,u=n.outWidth,c=n.dilationWidth,l=n.dilationHeight,f=s.left,h=s.top,d=i*r,p=si();this.userCode="\n void main() {\n ivec2 rc = getOutputCoords();\n\n vec4 result = vec4(0);\n\n for(int row=0; row<=1; row++) {\n for(int col=0; col<=1; col++) {\n int blockIndex = rc.y + col;\n int pos = rc.x + row;\n\n if(blockIndex >= "+t[1]+" || pos >= "+t[0]+") continue;\n\n int offsetY = int(blockIndex / ("+u+")) * "+a+" - "+h+";\n int d0 = offsetY + "+l+" * (pos / "+d+");\n\n if(d0 >= "+e[0]+" || d0 < 0) continue;\n\n int offsetX = int(mod(float(blockIndex), "+u+".) * "+o+". - "+f+".);\n int d1 = offsetX + "+c+" * (int(mod(float(pos), "+d+".) / "+i+".));\n\n if(d1 >= "+e[1]+" || d1 < 0) continue;\n\n vec2 innerDims = vec2(d1, int(mod(float(pos), "+i+".)));\n result[row * 2 + col] = getChannel(getA(d0, int(innerDims.x),\n int(innerDims.y)), innerDims);\n }\n }\n\n "+p.output+" = result;\n }\n "}(l,f.shape,n),p=this.compileAndRun(d,[f]).reshape([1,l[0],l[1]]),m=new co(p.shape,[1,c,n.outChannels],!0,!1);return this.compileAndRun(m,[p,h]).reshape([1,s,a,n.outChannels])},t.prototype.conv2d=function(t,e,n){if(1===n.filterHeight&&1===n.filterWidth&&1===n.dilationHeight&&1===n.dilationWidth&&1===n.strideHeight&&1===n.strideWidth&&("SAME"===n.padInfo.type||"VALID"===n.padInfo.type))return this.conv2dByMatMul(t,e,n);if(f.getBool("WEBGL_CONV_IM2COL")&&1===t.shape[0])return this.conv2dWithIm2Row(t,e,n);var r=new function(t){this.variableNames=["x","W"],this.outputShape=t.outShape;var e=t.padInfo.top,n=t.padInfo.left,r=t.strideHeight,i=t.strideWidth,o=t.dilationHeight,a=t.dilationWidth,s=t.filterHeight,u=t.filterWidth,c=4*Math.floor(t.inChannels/4),l=t.inChannels%4;this.userCode="\n const ivec2 strides = ivec2("+r+", "+i+");\n const ivec2 pads = ivec2("+e+", "+n+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d2 = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // Convolve x(?, ?, d1) with w(:, :, d1, d2) to get y(yR, yC, d2).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+s+"; wR++) {\n int xR = xRCorner + wR * "+o+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+u+"; wC++) {\n int xC = xCCorner + wC * "+a+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n for (int d1 = 0; d1 < "+c+"; d1 += 4) {\n vec4 xValues = vec4(\n getX(batch, xR, xC, d1),\n getX(batch, xR, xC, d1 + 1),\n getX(batch, xR, xC, d1 + 2),\n getX(batch, xR, xC, d1 + 3)\n );\n vec4 wValues = vec4(\n getW(wR, wC, d1, d2),\n getW(wR, wC, d1 + 1, d2),\n getW(wR, wC, d1 + 2, d2),\n getW(wR, wC, d1 + 3, d2)\n );\n\n dotProd += dot(xValues, wValues);\n }\n\n if ("+(1===l)+") {\n dotProd +=\n getX(batch, xR, xC, "+c+") *\n getW(wR, wC, "+c+", d2);\n } else if ("+(2===l)+") {\n vec2 xValues = vec2(\n getX(batch, xR, xC, "+c+"),\n getX(batch, xR, xC, "+c+" + 1)\n );\n vec2 wValues = vec2(\n getW(wR, wC, "+c+", d2),\n getW(wR, wC, "+c+" + 1, d2)\n );\n dotProd += dot(xValues, wValues);\n } else if ("+(3===l)+") {\n vec3 xValues = vec3(\n getX(batch, xR, xC, "+c+"),\n getX(batch, xR, xC, "+c+" + 1),\n getX(batch, xR, xC, "+c+" + 2)\n );\n vec3 wValues = vec3(\n getW(wR, wC, "+c+", d2),\n getW(wR, wC, "+c+" + 1, d2),\n getW(wR, wC, "+c+" + 2, d2)\n );\n dotProd += dot(xValues, wValues);\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},t.prototype.conv2dDerInput=function(t,e,n){var r=new function(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterHeight,n=t.filterWidth,r=t.strideHeight,i=t.strideWidth,o=e-1-t.padInfo.top,a=n-1-t.padInfo.left;this.userCode="\n const ivec2 pads = ivec2("+o+", "+a+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords[3];\n\n ivec2 dyCorner = coords.yz - pads;\n int dyRCorner = dyCorner.x;\n int dyCCorner = dyCorner.y;\n\n // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+e+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+e+" - 1 - wR;\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+i+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = "+n+" - 1 - wC;\n\n for (int d2 = 0; d2 < "+t.outChannels+"; d2++) {\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, d2);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},t.prototype.conv2dDerFilter=function(t,e,n){var r=new function(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideHeight,n=t.strideWidth,r=t.padInfo.top,i=t.padInfo.left;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int wR = coords.x;\n int wC = coords.y;\n int d1 = coords.z;\n int d2 = coords.w;\n\n // Convolve x(?, ?, d1) with dy(:, :, d2) to get dw(wR, wC, d1, d2).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n\n for (int b = 0; b < "+t.batchSize+"; b++) {\n for (int yR = 0; yR < "+t.outHeight+"; yR++) {\n int xR = wR + yR * "+e+" - "+r+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+t.outWidth+"; yC++) {\n int xC = wC + yC * "+n+" - "+i+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n float dyValue = getDy(b, yR, yC, d2);\n float xValue = getX(b, xR, xC, d1);\n dotProd += (xValue * dyValue);\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},t.prototype.depthwiseConv2D=function(t,e,n){var r;return f.getBool("WEBGL_PACK_DEPTHWISECONV")&&n.strideWidth<=2&&n.outChannels/n.inChannels==1?(r=new function(t){this.variableNames=["x","W"],this.usesPackedTextures=!0,this.outputShape=t.outShape;for(var e=t.inHeight,n=t.inWidth,r=t.padInfo.top,i=t.padInfo.left,o=t.strideHeight,a=t.strideWidth,s=t.dilationHeight,u=t.dilationWidth,c=t.filterHeight,l=t.filterWidth,f=l,h="int xR; int xC; int xCOffset;",d=0;d= 0 && xR < "+e+" && xCOffset >= 0 && xCOffset < "+n+") {\n xTexelR"+d+"C"+p+" = getX(batch, xR, xCOffset, d1);\n } else {\n xTexelR"+d+"C"+p+" = vec4(0.);\n }\n\n xCOffset = xC + 1 - 2;\n if(xR >= 0 && xR < "+e+" && xCOffset >= 0 && xCOffset < "+n+") {\n vec4 previous = getX(batch, xR, xCOffset, d1);\n xR"+d+"C"+p+" = vec4(previous.zw, xTexelR"+d+"C"+p+".xy);\n } else {\n xR"+d+"C"+p+" = vec4(0, 0, xTexelR"+d+"C"+p+".xy);\n }\n ":"\n if(xR >= 0 && xR < "+e+" && xC >= 0 && xC < "+n+") {\n xTexelR"+d+"C"+p+" = getX(batch, xR, xC, d1);\n } else {\n xTexelR"+d+"C"+p+" = vec4(0.);\n }\n\n xR"+d+"C"+p+" = xTexelR"+d+"C"+p+";\n ",p+1= 0 && xR < "+e+" &&\n xCOffset >= 0 && xCOffset < "+n+") {\n xTexelR"+d+"C"+(p+2)+" = getX(batch, xR, xCOffset, d1);\n }\n ",u>1&&(h+="\n xCOffset -= 2;\n if(xR >= 0 && xR < "+e+" &&\n xCOffset >= 0 && xCOffset < "+n+") {\n xTexelR"+d+"C"+p+" = getX(batch, xR, xCOffset, d1);\n } else {\n xTexelR"+d+"C"+p+" = vec4(0.);\n }\n "),h+="\n xR"+d+"C"+(p+1)+" = vec4(\n xTexelR"+d+"C"+p+".zw, xTexelR"+d+"C"+(p+2)+".xy);\n "):h+="\n xCOffset = xC + "+v+";\n\n if(xR >= 0 && xR < "+e+" &&\n xCOffset >= 0 && xCOffset < "+n+") {\n xTexelR"+d+"C"+(p+2)+" = getX(batch, xR, xCOffset, d1);\n }\n\n xR"+d+"C"+(p+1)+" = xTexelR"+d+"C"+(p+2)+";\n "}}else p= 0 && xR < "+e+") {\n ",i%2==1?(h+="\n xCOffset = xC + 1 - "+a+";\n if(xCOffset >= 0 && xCOffset < "+n+") {\n xTexelR"+d+"C"+p+" = getX(batch, xR, xCOffset, d1);\n } else {\n xTexelR"+d+"C"+p+" = vec4(0.);\n }\n\n if(xC + 1 >= 0 && xC + 1 < "+n+") {\n xTexelR"+d+"C"+(p+2)+" = getX(batch, xR, xC + 1, d1);\n } else {\n xTexelR"+d+"C"+(p+2)+" = vec4(0.);\n }\n\n xR"+d+"C"+p+" = vec4(\n xTexelR"+d+"C"+p+".zw, xTexelR"+d+"C"+(p+2)+".zw);\n ",p+1= 0 && xCOffset < "+n+") {\n final = getX(batch, xR, xCOffset, d1);\n }\n xR"+d+"C"+(p+1)+" = vec4(xTexelR"+d+"C"+(p+2)+".xy, final.xy);\n ")):(h+="\n if(xC >= 0 && xC < "+n+") {\n xTexelR"+d+"C"+p+" = getX(batch, xR, xC, d1);\n } else {\n xTexelR"+d+"C"+p+" = vec4(0.);\n }\n\n xCOffset = xC + "+a+";\n if(xCOffset >= 0 && xCOffset < "+n+") {\n xTexelR"+d+"C"+(p+2)+" = getX(batch, xR, xCOffset, d1);\n } else {\n xTexelR"+d+"C"+(p+2)+" = vec4(0.);\n }\n\n xR"+d+"C"+p+" = vec4(\n xTexelR"+d+"C"+p+".xy, xTexelR"+d+"C"+(p+2)+".xy);\n ",p+1= "+e+") {\n continue;\n }\n\n for (int wC = 0; wC < "+l+"; wC++) {\n int xC = xCCorner + wC * "+u+";\n\n if (xC < 0 || xC >= "+n+") {\n continue;\n }\n\n float xVal = getX(batch, xR, xC, d1);\n float wVal = getW(wR, wC, d1, q);\n dotProd += xVal * wVal;\n }\n }\n setOutput(dotProd);\n }\n "}(n),this.compileAndRun(r,[t,e]))},t.prototype.depthwiseConv2DDerInput=function(t,e,n){var r=new function(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterHeight,n=t.filterWidth,r=t.strideHeight,i=t.strideWidth,o=e-1-t.padInfo.top,a=n-1-t.padInfo.left,s=t.outChannels/t.inChannels;this.userCode="\n const ivec2 pads = ivec2("+o+", "+a+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords[3];\n ivec2 dyCorner = coords.yz - pads;\n int dyRCorner = dyCorner.x;\n int dyCCorner = dyCorner.y;\n\n float dotProd = 0.0;\n\n for (int wR = 0; wR < "+e+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+e+" - 1 - wR;\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+i+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = "+n+" - 1 - wC;\n\n // TODO: Vec4 over the channelMul\n for (int dm = 0; dm < "+s+"; dm++) {\n int d2 = d1 * "+s+" + dm;\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, dm);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},t.prototype.depthwiseConv2DDerFilter=function(t,e,n){var r=new function(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideHeight,n=t.strideWidth,r=t.padInfo.top,i=t.padInfo.left,o=t.outChannels/t.inChannels;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int wR = coords.x;\n int wC = coords.y;\n int d1 = coords.z;\n int dm = coords.w;\n int d2 = d1 * "+o+" + dm;\n\n float dotProd = 0.0;\n\n // TODO: Vec4 over the batch size\n for (int b = 0; b < "+t.batchSize+"; b++) {\n for (int yR = 0; yR < "+t.outHeight+"; yR++) {\n int xR = wR + yR * "+e+" - "+r+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+t.outWidth+"; yC++) {\n int xC = wC + yC * "+n+" - "+i+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n float dyValue = getDy(b, yR, yC, d2);\n float xValue = getX(b, xR, xC, d1);\n dotProd += (xValue * dyValue);\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},t.prototype.conv3d=function(t,e,n){var r=new function(t){this.variableNames=["x","W"],this.outputShape=t.outShape;var e=t.padInfo.front,n=t.padInfo.top,r=t.padInfo.left,i=t.strideDepth,o=t.strideHeight,a=t.strideWidth,s=t.dilationDepth,u=t.dilationHeight,c=t.dilationWidth,l=t.filterDepth,f=t.filterHeight,h=t.filterWidth,d=4*Math.floor(t.inChannels/4),p=t.inChannels%4;this.userCode="\n const ivec3 strides = ivec3("+i+", "+o+", "+a+");\n const ivec3 pads = ivec3("+e+", "+n+", "+r+");\n\n void main() {\n ivec5 coords = getOutputCoords();\n int batch = coords.x;\n int d2 = coords.u;\n\n ivec3 xFRCCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;\n int xFCorner = xFRCCorner.x;\n int xRCorner = xFRCCorner.y;\n int xCCorner = xFRCCorner.z;\n\n // Convolve x(?, ?, ?, d1) with w(:, :, :, d1, d2) to get\n // y(yF, yR, yC, d2). ? = to be determined. : = across all\n // values in that axis.\n float dotProd = 0.0;\n for (int wF = 0; wF < "+l+"; wF++) {\n int xF = xFCorner + wF * "+s+";\n\n if (xF < 0 || xF >= "+t.inDepth+") {\n continue;\n }\n\n for (int wR = 0; wR < "+f+"; wR++) {\n int xR = xRCorner + wR * "+u+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+h+"; wC++) {\n int xC = xCCorner + wC * "+c+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n for (int d1 = 0; d1 < "+d+"; d1 += 4) {\n vec4 xValues = vec4(\n getX(batch, xF, xR, xC, d1),\n getX(batch, xF, xR, xC, d1 + 1),\n getX(batch, xF, xR, xC, d1 + 2),\n getX(batch, xF, xR, xC, d1 + 3)\n );\n vec4 wValues = vec4(\n getW(wF, wR, wC, d1, d2),\n getW(wF, wR, wC, d1 + 1, d2),\n getW(wF, wR, wC, d1 + 2, d2),\n getW(wF, wR, wC, d1 + 3, d2)\n );\n\n dotProd += dot(xValues, wValues);\n }\n\n if ("+(1===p)+") {\n dotProd +=\n getX(batch, xF, xR, xC, "+d+") *\n getW(wF, wR, wC, "+d+", d2);\n } else if ("+(2===p)+") {\n vec2 xValues = vec2(\n getX(batch, xF, xR, xC, "+d+"),\n getX(batch, xF, xR, xC, "+d+" + 1)\n );\n vec2 wValues = vec2(\n getW(wF, wR, wC, "+d+", d2),\n getW(wF, wR, wC, "+d+" + 1, d2)\n );\n dotProd += dot(xValues, wValues);\n } else if ("+(3===p)+") {\n vec3 xValues = vec3(\n getX(batch, xF, xR, xC, "+d+"),\n getX(batch, xF, xR, xC, "+d+" + 1),\n getX(batch, xF, xR, xC, "+d+" + 2)\n );\n vec3 wValues = vec3(\n getW(wF, wR, wC, "+d+", d2),\n getW(wF, wR, wC, "+d+" + 1, d2),\n getW(wF, wR, wC, "+d+" + 2, d2)\n );\n dotProd += dot(xValues, wValues);\n }\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},t.prototype.conv3dDerInput=function(t,e,n){var r=new function(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterDepth,n=t.filterHeight,r=t.filterWidth,i=t.strideDepth,o=t.strideHeight,a=t.strideWidth,s=e-1-t.padInfo.front,u=n-1-t.padInfo.top,c=r-1-t.padInfo.left;this.userCode="\n const ivec3 pads = ivec3("+s+", "+u+", "+c+");\n\n void main() {\n ivec5 coords = getOutputCoords();\n int batch = coords.x;\n int d1 = coords.u;\n\n\n ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;\n int dyFCorner = dyCorner.x;\n int dyRCorner = dyCorner.y;\n int dyCCorner = dyCorner.z;\n\n float dotProd = 0.0;\n for (int wF = 0; wF < "+e+"; wF++) {\n float dyF = float(dyFCorner + wF) / "+i+".0;\n\n if (dyF < 0.0 || dyF >= "+t.outDepth+".0 || fract(dyF) > 0.0) {\n continue;\n }\n int idyF = int(dyF);\n\n int wFPerm = "+e+" - 1 - wF;\n\n for (int wR = 0; wR < "+n+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+o+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 ||\n fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+n+" - 1 - wR;\n\n for (int wC = 0; wC < "+r+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = "+r+" - 1 - wC;\n\n for (int d2 = 0; d2 < "+t.outChannels+"; d2++) {\n float xValue = getDy(batch, idyF, idyR, idyC, d2);\n float wValue = getW(wFPerm, wRPerm, wCPerm, d1, d2);\n dotProd += xValue * wValue;\n }\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},t.prototype.conv3dDerFilter=function(t,e,n){var r=new function(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideDepth,n=t.strideHeight,r=t.strideWidth,i=t.padInfo.front,o=t.padInfo.top,a=t.padInfo.left;this.userCode="\n void main() {\n ivec5 coords = getOutputCoords();\n int wF = coords.x;\n int wR = coords.y;\n int wC = coords.z;\n int d1 = coords.w;\n int d2 = coords.u;\n\n float dotProd = 0.0;\n\n for (int b = 0; b < "+t.batchSize+"; b++) {\n for (int yF = 0; yF < "+t.outDepth+"; yF++) {\n int xF = wF + yF * "+e+" - "+i+";\n\n if (xF < 0 || xF >= "+t.inDepth+") {\n continue;\n }\n\n for (int yR = 0; yR < "+t.outHeight+"; yR++) {\n int xR = wR + yR * "+n+" - "+o+";\n\n if (xR < 0 || xR >= "+t.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+t.outWidth+"; yC++) {\n int xC = wC + yC * "+r+" - "+a+";\n\n if (xC < 0 || xC >= "+t.inWidth+") {\n continue;\n }\n\n float dyValue = getDy(b, yF, yR, yC, d2);\n float xValue = getX(b, xF, xR, xC, d1);\n dotProd += (xValue * dyValue);\n }\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[t,e])},t.prototype.maxPool=function(t,e){var n=new fo(e,"max",!1),r=this.makeOutputArray(n.outputShape,t.dtype);return this.compileAndRun(n,[t],r)},t.prototype.avgPool=function(t,e){var n=new fo(e,"avg",!1),r=this.makeOutputArray(n.outputShape,"float32");return this.compileAndRun(n,[t],r)},t.prototype.maxPoolBackprop=function(t,e,n,r){var i=new fo(r,"max",!0),o=this.compileAndRun(i,[e]),a=new function(t){this.variableNames=["dy","maxPos"],this.outputShape=t.inShape;var e=t.strideHeight,n=t.strideWidth,r=t.dilationHeight,i=t.effectiveFilterHeight,o=t.effectiveFilterWidth,a=i-1-t.padInfo.top,s=o-1-t.padInfo.left,u=i*o-1;this.userCode="\n const ivec2 pads = ivec2("+a+", "+s+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n\n ivec2 dyRCCorner = coords.yz - pads;\n int dyRCorner = dyRCCorner.x;\n int dyCCorner = dyRCCorner.y;\n\n // Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+i+";\n wR += "+r+") {\n float dyR = float(dyRCorner + wR) / "+e+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+o+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+n+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n float dyValue = getDy(b, idyR, idyC, d);\n int maxPosValue = "+u+" - int(getMaxPos(b, idyR, idyC, d));\n\n // Get the current value, check it against the value from the\n // position matrix.\n int curPosValue = wR * "+o+" + wC;\n float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);\n\n dotProd += dyValue * mask;\n }\n }\n setOutput(dotProd);\n }\n "}(r),s=this.makeOutputArray(a.outputShape,e.dtype),u=this.compileAndRun(a,[t,o],s);return o.dispose(),u},t.prototype.avgPoolBackprop=function(t,e,n){var r=new function(t){this.variableNames=["dy"],this.outputShape=t.inShape;var e=t.filterHeight,n=t.filterWidth,r=t.strideHeight,i=t.strideWidth,o=t.dilationHeight,a=t.dilationWidth,s=t.effectiveFilterHeight,u=t.effectiveFilterWidth,c=s-1-t.padInfo.top,l=u-1-t.padInfo.left,f=1/(e*n);this.userCode="\n const ivec2 pads = ivec2("+c+", "+l+");\n const float avgMultiplier = float("+f+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n\n ivec2 dyRCCorner = coords.yz - pads;\n int dyRCorner = dyRCCorner.x;\n int dyCCorner = dyRCCorner.y;\n\n // Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+s+";\n wR += "+o+") {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+t.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+u+";\n wC+= "+a+") {\n float dyC = float(dyCCorner + wC) / "+i+".0;\n\n if (dyC < 0.0 || dyC >= "+t.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n float dyValue = getDy(b, idyR, idyC, d);\n\n dotProd += dyValue * avgMultiplier;\n }\n }\n setOutput(dotProd);\n }\n "}(n),i=this.makeOutputArray(r.outputShape,e.dtype);return this.compileAndRun(r,[t],i)},t.prototype.cast=function(t,e){return tr(t,e,this)},t.prototype.unstack=function(t,e){for(var n=t.shape[e],r=new Array(t.rank-1),i=0,o=0;o1?o-1:o,r&&n>1?a-1:a],c=[r&&e>1?e-1:e,r&&n>1?n-1:n];this.userCode="\n const vec3 effectiveInputOverOutputRatioRC = vec3(\n "+u[0]/c[0]+",\n "+u[1]/c[1]+",\n "+u[1]/c[1]+");\n const vec3 inputShapeRC = vec3("+o+".0, "+a+".0,\n "+a+".0);\n\n float getAValue(int b, int r, int c, int d) {\n return getChannel(getA(b, r, c, d), vec2(c, d));\n }\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n // Calculate values for next column in yRC.z.\n ivec3 yRC = coords.yzz + ivec3(0, 0, 1);\n\n // Fractional source index.\n vec3 sourceFracIndexRC = vec3(yRC) * effectiveInputOverOutputRatioRC;\n\n // Compute the four integer indices.\n ivec3 sourceFloorRC = ivec3(sourceFracIndexRC);\n ivec3 sourceCeilRC = ivec3(\n min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));\n \n // Should we calculate next column and row elements in 2x2 packed cell.\n bool hasNextCol = d < "+(s-1)+"; \n bool hasNextRow = coords.z < "+(n-1)+";\n\n // In parallel, construct four corners for all four components in\n // packed 2x2 cell.\n vec4 topLeft = vec4(\n getAValue(b, sourceFloorRC.x, sourceFloorRC.y, d),\n hasNextCol ? getAValue(b, sourceFloorRC.x, sourceFloorRC.y, d + 1)\n : 0.0,\n hasNextRow ? getAValue(b, sourceFloorRC.x, sourceFloorRC.z, d)\n : 0.0,\n (hasNextRow && hasNextCol) ?\n getAValue(b, sourceFloorRC.x, sourceFloorRC.z, d + 1) : 0.0);\n\n vec4 bottomLeft = vec4(\n getAValue(b, sourceCeilRC.x, sourceFloorRC.y, d),\n hasNextCol ? getAValue(b, sourceCeilRC.x, sourceFloorRC.y, d + 1)\n : 0.0,\n hasNextRow ? getAValue(b, sourceCeilRC.x, sourceFloorRC.z, d)\n : 0.0,\n (hasNextRow && hasNextCol) ?\n getAValue(b, sourceCeilRC.x, sourceFloorRC.z, d + 1) : 0.0);\n\n vec4 topRight = vec4(\n getAValue(b, sourceFloorRC.x, sourceCeilRC.y, d),\n hasNextCol ? getAValue(b, sourceFloorRC.x, sourceCeilRC.y, d + 1)\n : 0.0,\n hasNextRow ? getAValue(b, sourceFloorRC.x, sourceCeilRC.z, d)\n : 0.0,\n (hasNextRow && hasNextCol) ?\n getAValue(b, sourceFloorRC.x, sourceCeilRC.z, d + 1) : 0.0);\n\n vec4 bottomRight = vec4(\n getAValue(b, sourceCeilRC.x, sourceCeilRC.y, d),\n hasNextCol ? getAValue(b, sourceCeilRC.x, sourceCeilRC.y, d + 1)\n : 0.0,\n hasNextRow ? getAValue(b, sourceCeilRC.x, sourceCeilRC.z, d)\n : 0.0,\n (hasNextRow && hasNextCol) ?\n getAValue(b, sourceCeilRC.x, sourceCeilRC.z, d + 1) : 0.0);\n\n vec3 fracRC = sourceFracIndexRC - vec3(sourceFloorRC);\n\n vec4 top = mix(topLeft, topRight, fracRC.yyzz);\n vec4 bottom = mix(bottomLeft, bottomRight, fracRC.yyzz);\n vec4 newValue = mix(top, bottom, fracRC.x);\n\n setOutput(newValue);\n }\n "}(t.shape,e,n,r):new function(t,e,n,r){this.variableNames=["A"],this.outputShape=[];var i=t[0],o=t[1],a=t[2],s=t[3];this.outputShape=[i,e,n,s];var u=[r&&e>1?o-1:o,r&&n>1?a-1:a],c=[r&&e>1?e-1:e,r&&n>1?n-1:n];this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+u[0]/c[0]+",\n "+u[1]/c[1]+");\n const vec2 inputShapeRC = vec2("+o+".0, "+a+".0);\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n ivec2 yRC = coords.yz;\n\n // Fractional source index.\n vec2 sourceFracIndexRC = vec2(yRC) * effectiveInputOverOutputRatioRC;\n\n // Compute the four integer indices.\n ivec2 sourceFloorRC = ivec2(sourceFracIndexRC);\n ivec2 sourceCeilRC = ivec2(\n min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));\n\n float topLeft = getA(b, sourceFloorRC.x, sourceFloorRC.y, d);\n float bottomLeft = getA(b, sourceCeilRC.x, sourceFloorRC.y, d);\n float topRight = getA(b, sourceFloorRC.x, sourceCeilRC.y, d);\n float bottomRight = getA(b, sourceCeilRC.x, sourceCeilRC.y, d);\n\n vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);\n\n float top = topLeft + (topRight - topLeft) * fracRC.y;\n float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;\n float newValue = top + (bottom - top) * fracRC.x;\n\n setOutput(newValue);\n }\n "}(t.shape,e,n,r);return this.compileAndRun(i,[t])},t.prototype.resizeBilinearBackprop=function(t,e,n){var r=new function(t,e,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=e.shape;var r=e.shape,i=r[1],o=r[2],a=t.shape,s=a[1],u=a[2],c=[n&&s>1?i-1:i,n&&u>1?o-1:o],l=[n&&s>1?s-1:s,n&&u>1?u-1:u],f=c[0]/l[0],h=c[1]/l[1],d=1/f,p=1/h,m=2*Math.ceil(d)+2,v=2*Math.ceil(p)+2;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n int r = coords[1];\n int c = coords[2];\n\n float accumulator = 0.0;\n\n const float heightScale = float("+f+");\n const float widthScale = float("+h+");\n\n const float invHeightScale = float("+d+");\n const float invWidthScale = float("+p+");\n\n const int winHeight = int("+m+");\n const int winWidth = int("+v+");\n\n // Compute bounds for where in dy we will look\n float startRLerp = floor(float(r) * invHeightScale);\n int startDyR = int(startRLerp - float(winHeight / 2));\n\n float startCLerp = floor(float(c) * invWidthScale);\n int startDyC = int(startCLerp - float(winWidth / 2));\n\n // Loop over dy\n for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {\n int dyR = dyROffset + startDyR;\n\n // Guard against the window exceeding the bounds of dy\n if (dyR < 0 || dyR >= "+s+") {\n continue;\n }\n\n for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {\n int dyC = dyCOffset + startDyC;\n\n // Guard against the window exceeding the bounds of dy\n if (dyC < 0 || dyC >= "+u+") {\n continue;\n }\n\n float dxR = float(dyR) * heightScale;\n int topDxRIndex = int(floor(dxR));\n int bottomDxRIndex = int(min(ceil(dxR), "+(i-1)+".0));\n float dxRLerp = dxR - float(topDxRIndex);\n float inverseDxRLerp = 1.0 - dxRLerp;\n\n float dxC = float(dyC) * widthScale;\n int leftDxCIndex = int(floor(dxC));\n int rightDxCIndex = int(min(ceil(dxC), "+(o-1)+".0));\n float dxCLerp = dxC - float(leftDxCIndex);\n float inverseDxCLerp = 1.0 - dxCLerp;\n\n if (r == topDxRIndex && c == leftDxCIndex) {\n // topLeft\n accumulator +=\n getDy(b, dyR, dyC, d) * inverseDxRLerp * inverseDxCLerp;\n }\n\n if (r == topDxRIndex && c == rightDxCIndex) {\n // topRight\n accumulator += getDy(b, dyR, dyC, d) * inverseDxRLerp * dxCLerp;\n }\n\n if (r == bottomDxRIndex && c == leftDxCIndex) {\n // bottomLeft\n accumulator += getDy(b, dyR, dyC, d) * dxRLerp * inverseDxCLerp;\n }\n\n if (r == bottomDxRIndex && c == rightDxCIndex) {\n // bottomRight\n accumulator += getDy(b, dyR, dyC, d) * dxRLerp * dxCLerp;\n }\n }\n }\n // End loop over dy\n\n setOutput(accumulator);\n }\n "}(t,e,n);return this.compileAndRun(r,[t])},t.prototype.resizeNearestNeighbor=function(t,e,n,r){var i=new function(t,e,n,r){this.variableNames=["A"],this.outputShape=[];var i=t[0],o=t[1],a=t[2],s=t[3];this.outputShape=[i,e,n,s];var u=[r&&e>1?o-1:o,r&&n>1?a-1:a],c=[r&&e>1?e-1:e,r&&n>1?n-1:n],l=r?"0.5":"0.0";this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+u[0]/c[0]+",\n "+u[1]/c[1]+");\n const vec2 inputShapeRC = vec2("+o+".0, "+a+".0);\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n ivec2 yRC = coords.yz;\n\n // Fractional source index.\n vec2 sourceFracIndexRC = vec2(yRC) * effectiveInputOverOutputRatioRC;\n\n // Compute the coordinators of nearest neighbor point.\n ivec2 sourceNearestRC = ivec2(\n min(inputShapeRC - 1.0, floor(sourceFracIndexRC + "+l+")));\n\n float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);\n\n setOutput(newValue);\n }\n "}(t.shape,e,n,r);return this.compileAndRun(i,[t])},t.prototype.resizeNearestNeighborBackprop=function(t,e,n){var r=new function(t,e,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=e.shape;var r=e.shape,i=r[1],o=r[2],a=t.shape,s=a[1],u=a[2],c=[n&&s>1?i-1:i,n&&u>1?o-1:o],l=[n&&s>1?s-1:s,n&&u>1?u-1:u],f=c[0]/l[0],h=c[1]/l[1],d=1/f,p=1/h,m=2*Math.ceil(d)+2,v=2*Math.ceil(p)+2;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n int r = coords[1];\n int c = coords[2];\n\n float accumulator = 0.0;\n\n const float heightScale = float("+f+");\n const float widthScale = float("+h+");\n\n const float invHeightScale = float("+d+");\n const float invWidthScale = float("+p+");\n\n const int winHeight = int("+m+");\n const int winWidth = int("+v+");\n\n // Compute bounds for where in dy we will look\n float startRLerp = floor(float(r) * invHeightScale);\n int startDyR = int(floor(startRLerp - float(winHeight / 2)));\n\n float startCLerp = floor(float(c) * invWidthScale);\n int startDyC = int(floor(startCLerp - float(winWidth / 2)));\n\n // Loop over dy\n for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {\n int dyR = dyROffset + startDyR;\n\n // Guard against the window exceeding the bounds of dy\n if (dyR < 0 || dyR >= "+s+") {\n continue;\n }\n\n for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {\n int dyC = dyCOffset + startDyC;\n\n // Guard against the window exceeding the bounds of dy\n if (dyC < 0 || dyC >= "+u+") {\n continue;\n }\n\n float sourceFracRow =\n float("+c[0]+") *\n (float(dyR) / float("+l[0]+"));\n\n float sourceFracCol =\n float("+c[1]+") *\n (float(dyC) / float("+l[1]+"));\n\n int sourceNearestRow = int(min(\n float(int("+i+") - 1),\n "+n+" ? float(round(sourceFracRow)) :\n float(floor(sourceFracRow))));\n\n int sourceNearestCol = int(min(\n float(int("+o+") - 1),\n "+n+" ? float(round(sourceFracCol)) :\n float(floor(sourceFracCol))));\n\n if (r == sourceNearestRow && c == sourceNearestCol) {\n accumulator += getDy(b, dyR, dyC, d);\n }\n }\n }\n // End loop over dy\n\n setOutput(accumulator);\n }\n "}(t,e,n);return this.compileAndRun(r,[t])},t.prototype.multinomial=function(t,e,n,r){var i=e?t:Mn(t),o=i.shape[0],a=i.shape[1],s=new lo(o,a,n),u=this.makeOutputArray(s.outputShape,"int32"),c=s.getCustomSetupFunc(r);return this.compileAndRun(s,[i],u,c)},t.prototype.oneHot=function(t,e,n,r){var i=new function(t,e,n,r){this.variableNames=["indices"],this.outputShape=[t,e],this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int index = round(getIndices(coords.x));\n setOutput(mix(float("+r+"), float("+n+"),\n float(index == coords.y)));\n }\n "}(t.size,e,n,r);return this.compileAndRun(i,[t])},t.prototype.nonMaxSuppression=function(t,e,n,r,i){return Ye("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead"),sr(t.dataSync(),e.dataSync(),n,r,i)},t.prototype.cropAndResize=function(t,e,n,r,i,o){var a=new function(t,e,n,r,i){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];var o=t[0],a=t[1],s=t[2],u=t[3],c=e[0],l=n[0],f=n[1];this.outputShape=[c,l,f,u];var h="bilinear"===r?1:0,d=[a-1+".0",s-1+".0"],p=d[0],m=d[1],v=l>1?[""+(a-1)/(l-1),"(y2-y1) * height_ratio","y1*"+p+" + float(y)*(height_scale)"]:["0.0","0.0","0.5 * (y1+y2) * "+p],g=v[0],y=v[1],b=v[2],w=f>1?[""+(s-1)/(f-1),"(x2-x1) * width_ratio","x1*"+m+" + float(x)*(width_scale)"]:["0.0","0.0","0.5 * (x1+x2) * "+m],x=w[0],k=w[1],S=w[2];this.userCode="\n const float height_ratio = float("+g+");\n const float width_ratio = float("+x+");\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int y = coords[1];\n int x = coords[2];\n int d = coords[3];\n\n // get box vals\n float y1 = getBoxes(b,0);\n float x1 = getBoxes(b,1);\n float y2 = getBoxes(b,2);\n float x2 = getBoxes(b,3);\n\n // get image in batch index\n int bInd = round(getBoxInd(b));\n if(bInd < 0 || bInd >= "+o+") {\n return;\n }\n\n float height_scale = "+y+";\n float width_scale = "+k+";\n\n float in_y = "+b+";\n if( in_y < 0.0 || in_y > "+p+" ) {\n setOutput(float("+i+"));\n return;\n }\n float in_x = "+S+";\n if( in_x < 0.0 || in_x > "+m+" ) {\n setOutput(float("+i+"));\n return;\n }\n\n vec2 sourceFracIndexCR = vec2(in_x,in_y);\n if("+h+" == 1) {\n // Compute the four integer indices.\n ivec2 sourceFloorCR = ivec2(sourceFracIndexCR);\n ivec2 sourceCeilCR = ivec2(ceil(sourceFracIndexCR));\n\n float topLeft = getImage(b, sourceFloorCR.y, sourceFloorCR.x, d);\n float bottomLeft = getImage(b, sourceCeilCR.y, sourceFloorCR.x, d);\n float topRight = getImage(b, sourceFloorCR.y, sourceCeilCR.x, d);\n float bottomRight = getImage(b, sourceCeilCR.y, sourceCeilCR.x, d);\n\n vec2 fracCR = sourceFracIndexCR - vec2(sourceFloorCR);\n\n float top = topLeft + (topRight - topLeft) * fracCR.x;\n float bottom = bottomLeft + (bottomRight - bottomLeft) * fracCR.x;\n float newValue = top + (bottom - top) * fracCR.y;\n setOutput(newValue);\n } else {\n // Compute the coordinators of nearest neighbor point.\n ivec2 sourceNearestCR = ivec2(floor(\n sourceFracIndexCR + vec2(0.5,0.5)));\n float newValue = getImage(b, sourceNearestCR.y, sourceNearestCR.x, d);\n setOutput(newValue);\n }\n }\n "}(t.shape,e.shape,r,i,o);return this.compileAndRun(a,[t,e,n])},t.prototype.depthToSpace=function(t,e,n){w(e>1,function(){return"blockSize should be > 1 for depthToSpace, but was: "+e});var r=t.shape[0],i="NHWC"===n?t.shape[1]:t.shape[2],o="NHWC"===n?t.shape[2]:t.shape[3],a="NHWC"===n?t.shape[3]:t.shape[1],s=i*e,u=o*e,c=a/(e*e),l=new Ii("NHWC"===n?[r,s,u,c]:[r,c,s,u],e,n);return this.compileAndRun(l,[t])},t.prototype.split=function(t,e,n){return cr(t,e,n)},t.prototype.scatterND=function(t,e,n){var r=pn(0,t,n),i=r.sliceRank,o=r.numUpdates,a=r.sliceSize,s=r.strides,u=r.outputSize,c=[u/a,a],l=t.reshape([o,i]),f=e.reshape([o,a]);if(0===u)return er(zn([]),n);var h=Ln(0),d=new ho(o,i,l.rank,f.rank,s,c);return this.compileAndRun(d,[f,l,h]).reshape(n)},t.prototype.sparseToDense=function(t,e,n,r){var i=pn(0,t,n),o=i.sliceRank,a=i.numUpdates,s=i.strides,u=i.outputSize,c=new ho(a,o,t.rank,e.rank,s,[u,1],!1);return this.compileAndRun(c,[e,t,r]).reshape(n)},t.prototype.fft=function(t){return this.fftImpl(t,!1)},t.prototype.ifft=function(t){return this.fftImpl(t,!0)},t.prototype.fftImpl=function(t,e){var n=this.texData.get(t.dataId),r=new Ti("return real * expR - imag * expI;",t.shape,e),i=new Ti("return real * expI + imag * expR;",t.shape,e),o=[this.makeComplexComponentTensorHandle(t,n.complexTensors.real),this.makeComplexComponentTensorHandle(t,n.complexTensors.imag)],a=this.compileAndRun(r,o),s=this.compileAndRun(i,o),u=this.complex(a,s).as2D(t.shape[0],t.shape[1]);return a.dispose(),s.dispose(),u},t.prototype.gatherND=function(t,e){var n=e.shape,r=n[n.length-1],i=fn(t,e),o=i[0],a=i[1],s=i[2],u=i[3],c=e.reshape([a,r]),l=t.reshape([t.size/s,s]),f=new function(t,e,n){this.sliceDim=t,this.strides=e,this.variableNames=["x","indices"],this.outputShape=n;var r=vi(e.length),i=vi(n.length),o=this.sliceDim>1?"strides[j]":"strides";this.userCode="\n "+r+" strides = "+r+"("+this.strides+");\n void main() {\n "+i+" coords = getOutputCoords();\n int flattenIndex = 0;\n for (int j = 0; j < "+this.sliceDim+"; j++) {\n int index = round(getIndices(coords[0], j));\n flattenIndex += index * "+o+";\n }\n setOutput(getX(flattenIndex, coords[1]));\n }\n "}(r,u,[a,s]);return this.compileAndRun(f,[l,c]).reshape(o)},t.prototype.fill=function(t,e,n){if("string"===(n=n||H(e))){var r=j(n,_(t));return r.fill(e),vt.make(t,{values:r},n)}var i=new Mi(t,e),o=i.getCustomSetupFunc(e),a=this.makeOutputArray(t,n);return this.compileAndRun(i,[],a,o)},t.prototype.onesLike=function(t){if("string"===t.dtype)throw new Error("onesLike is not supported under string dtype");return this.fill(t.shape,1,t.dtype)},t.prototype.zerosLike=function(t){return this.fill(t.shape,"string"===t.dtype?"":0,t.dtype)},t.prototype.linspace=function(t,e,n){return nr(t,e,n)},t.prototype.makeOutputArray=function(t,e){return vt.make(t,{},e,this)},t.prototype.makePackedTensor=function(t,e){var n=vt.make(t,{},e,this);return this.texData.get(n.dataId).isPacked=!0,n},t.prototype.unpackTensor=function(t){var e=new function(t){this.variableNames=["A"],this.usesPackedTextures=!0,this.outputShape=t;var e=t.length,n=ri("rc",e),r=vi(e),i=function(t,e){if(1===t)return"rc";for(var n="",r=0;r "+e[0];for(var r="",i=t-2;i= "+e[i],i= "+e+";\n bool rEdge = rp1 >= "+n+";\n "}(e,t[t.length-1],t[t.length-2],n),a=function(t,e){var n=t.length,r=function(t,e){for(var n=[],r=0;r<=1;r++)for(var i=0;i<=1;i++){for(var o=(0===r?"r":"rp1")+", "+(0===i?"c":"cp1"),a=2;a= "+t[0]+" ? 0. : getA(rc + 1),\n 0, 0":"getA("+r[0]+"),\n cEdge ? 0. : getA("+r[1]+"),\n rEdge ? 0. : getA("+r[2]+"),\n rEdge || cEdge ? 0. : getA("+r[3]+")"}(t,n);this.userCode="\n void main() {\n "+r+" rc = getOutputCoords();\n\n if("+i+") {\n setOutput(vec4(0));\n } else {\n "+o+"\n\n setOutput(vec4("+a+"));\n }\n }\n "}}(t.shape);return this.compileAndRun(e,[t],this.makePackedTensor(t.shape,t.dtype))},t.prototype.packedReshape=function(t,e){var n=t.reshape([ve(t.shape)].concat(ge(t.shape))),r=new function(t,e){this.variableNames=["A"],this.usesPackedTextures=!0,this.outputShape=t;for(var n="",r=0;r<4;r++){var i="thisRC = rc;";r%2==1&&(i+="thisRC.z += 1;"),r>1&&(i+="thisRC.y += 1;"),n+="\n "+i+"\n "+(r>0?"if(thisRC.y < rows && thisRC.z < cols){":"")+"\n int flatIndex = getFlatIndex(thisRC);\n\n ivec3 inputRC = inputCoordsFromReshapedOutCoords(flatIndex);\n vec2 inputRCInnerDims = vec2(float(inputRC.y),float(inputRC.z));\n\n result["+r+"] =\n getChannel(getA(inputRC.x, inputRC.y, inputRC.z), inputRCInnerDims);\n "+(r>0?"}":"")+"\n "}this.userCode="\n \n ivec3 inputCoordsFromReshapedOutCoords(int index) {\n "+ui(["r","c","d"],e)+"\n return ivec3(r, c, d);\n }\n \n "+function(t){return"\n int getFlatIndex(ivec3 coords) {\n return round("+function(t,e){if(t.length!==e.length)throw new Error("Vectors to be dotted must be of the same length -got "+t.length+" and "+e.length);for(var n=[],r=Math.floor(t.length/4),i=t.length%4,o=0;o0,n=t.isUniform?"uniform":t.texData.texShape;r+=t.shape+"_"+n+"_"+e});var i=t.userCode;return t.constructor.name+"_"+r+"_"+i}(t,0,s),c=this.getAndSaveBinary(u,function(){return function(t,e,n,r){var i=e.userCode,o=n.map(function(t,n){var r={logicalShape:t.shape,texShape:t.isUniform?null:t.texData.texShape,isUniform:t.isUniform,isPacked:!t.isUniform&&t.texData.isPacked,flatOffset:null};return null!=t.texData&&null!=t.texData.slice&&t.texData.slice.flatOffset>0&&(r.flatOffset=t.texData.slice.flatOffset),{name:e.variableNames[n],shapeInfo:r}}),a=o.map(function(t){return t.shapeInfo}),s={logicalShape:r.shape,texShape:r.texData.texShape,isUniform:!1,isPacked:r.texData.isPacked,flatOffset:null},u=function(t,e,n,r){var i=[];t.forEach(function(t){var e=_(t.shapeInfo.logicalShape);t.shapeInfo.isUniform?i.push("uniform float "+t.name+(e>1?"["+e+"]":"")+";"):(i.push("uniform sampler2D "+t.name+";"),i.push("uniform int offset"+t.name+";"))});var o,a,s=i.join("\n"),u=t.map(function(t){return function(t,e,n){void 0===n&&(n=!1);var r="";r+=n?function t(e){var n;switch(e.shapeInfo.logicalShape.length){case 0:return"\n vec4 get"+(n=e.name).charAt(0).toUpperCase()+n.slice(1)+"() {\n return "+si().texture2D+"("+n+", halfCR);\n }\n ";case 1:return function(t){var e=t.name,n="get"+e.charAt(0).toUpperCase()+e.slice(1),r=t.shapeInfo.texShape,i=[Math.ceil(r[0]/2),Math.ceil(r[1]/2)],o=si();return"\n vec4 "+n+"(int index) {\n vec2 uv = packedUVfrom1D(\n "+i[0]+", "+i[1]+", index);\n return "+o.texture2D+"("+e+", uv);\n }\n "}(e);case 2:return function(t){var e=t.shapeInfo.logicalShape,n=t.name,r="get"+n.charAt(0).toUpperCase()+n.slice(1),i=t.shapeInfo.texShape,o=i[0],a=i[1],s=si();if(null!=i&&E(e,i))return"\n vec4 "+r+"(int row, int col) {\n vec2 uv = (vec2(col, row) + halfCR) / vec2("+a+".0, "+o+".0);\n\n return "+s.texture2D+"("+n+", uv);\n }\n ";var u=[Math.ceil(i[0]/2),Math.ceil(i[1]/2)];return"\n vec4 "+r+"(int row, int col) {\n vec2 uv = packedUVfrom2D("+Math.ceil(e[1]/2)+", "+u[0]+", "+u[1]+", row, col);\n return "+s.texture2D+"("+n+", uv);\n }\n "}(e);case 3:return function(e){var n=e.shapeInfo.logicalShape,r=e.name,i="get"+r.charAt(0).toUpperCase()+r.slice(1),o=e.shapeInfo.texShape,a=[Math.ceil(o[0]/2),Math.ceil(o[1]/2)];if(1===n[0]){var s=gi(e,n.slice(1));return"\n "+t(s)+"\n vec4 "+i+"(int b, int row, int col) {\n return "+i+"("+yi(["b","row","col"],[1,2])+");\n }\n "}var u=a[0],c=a[1],l=Math.ceil(n[2]/2);return"\n vec4 "+i+"(int b, int row, int col) {\n vec2 uv = packedUVfrom3D(\n "+u+", "+c+", "+l*Math.ceil(n[1]/2)+", "+l+", b, row, col);\n return "+si().texture2D+"("+r+", uv);\n }\n "}(e);default:return function(t){for(var e=t.shapeInfo.logicalShape,n=e.length,r=t.name,i="get"+r.charAt(0).toUpperCase()+r.slice(1),o=t.shapeInfo.texShape,a=[Math.ceil(o[0]/2),Math.ceil(o[1]/2)],s=a[0],u=a[1],c=Math.ceil(e[n-1]/2),l=c*Math.ceil(e[n-2]/2),f="int b, int row, int col",h="b * "+l+" + (row / 2) * "+c+" + (col / 2)",d=2;d=1?"coords = 0;":c.map(function(t){return"coords."+h[t+f]+" = 0;"}).join("\n"),r=u<2&&s>0?"coords":t.shapeInfo.logicalShape.map(function(t,e){return"coords."+h[e+f]}).join(", ");var d="return outputValue;",p=1===_(t.shapeInfo.logicalShape),m=1===_(e.logicalShape);if(1!==s||p||m){if(p&&!m)d=1===u?"\n return vec4(outputValue.x, outputValue.x, 0., 0.);\n ":"\n return vec4(outputValue.x);\n ";else if(c.length){var v=s-2,g=s-1;c.indexOf(v)>-1&&c.indexOf(g)>-1?d="return vec4(outputValue.x);":c.indexOf(v)>-1?d="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":c.indexOf(g)>-1&&(d="return vec4(outputValue.xx, outputValue.zz);")}}else d="\n return vec4(outputValue.xy, outputValue.xy);\n ";return"\n vec4 "+a+"() {\n "+l+" coords = getOutputCoords();\n "+n+"\n vec4 outputValue = get"+o+"("+r+");\n "+d+"\n }\n "}(t,e):function(t,e){var n=t.name,r=n.charAt(0).toUpperCase()+n.slice(1),i="get"+r+"AtOutCoords",o=e.texShape,a=t.shapeInfo.texShape,s=t.shapeInfo.logicalShape.length,u=e.logicalShape.length;if(!t.shapeInfo.isUniform&&s===u&&null==t.shapeInfo.flatOffset&&E(a,o))return"\n float "+i+"() {\n return sampleTexture("+n+", resultUV);\n }\n ";var c=vi(u),l=ii(t.shapeInfo.logicalShape,e.logicalShape),f=u-s,h=["x","y","z","w","u","v"];return"\n float "+i+"() {\n "+c+" coords = getOutputCoords();\n "+(0===s?"":u<2&&l.length>=1?"coords = 0;":l.map(function(t){return"coords."+h[t+f]+" = 0;"}).join("\n"))+"\n return get"+r+"("+(u<2&&s>0?"coords":t.shapeInfo.logicalShape.map(function(t,e){return"coords."+h[e+f]}).join(", "))+");\n }\n "}(t,e)),r}(t,e,r)}).join("\n"),c=e.texShape,l=si(),f="\n float sampleTexture(sampler2D textureSampler, vec2 uv) {\n return "+l.texture2D+"(textureSampler, uv).r;\n }\n ",h=function(t){return t.version+"\n precision highp float;\n precision highp int;\n precision highp sampler2D;\n "+t.varyingFs+" vec2 resultUV;\n "+t.defineOutput+"\n const vec2 halfCR = vec2(0.5, 0.5);\n\n struct ivec5\n {\n int x;\n int y;\n int z;\n int w;\n int u;\n };\n\n struct ivec6\n {\n int x;\n int y;\n int z;\n int w;\n int u;\n int v;\n };\n\n uniform float NAN;\n #define isnan(value) isnan_custom(value)\n "+t.defineSpecialNaN+"\n bvec4 isnan_custom(vec4 val) {\n return bvec4(isnan(val.x), isnan(val.y), isnan(val.z), isnan(val.w));\n }\n\n "+t.defineSpecialInf+"\n "+t.defineRound+"\n\n int imod(int x, int y) {\n return x - y * (x / y);\n }\n\n int idiv(int a, int b, float sign) {\n int res = a / b;\n int mod = imod(a, b);\n if (sign < 0. && mod != 0) {\n res -= 1;\n }\n return res;\n }\n\n //Based on the work of Dave Hoskins\n //https://www.shadertoy.com/view/4djSRW\n #define HASHSCALE1 443.8975\n float random(float seed){\n vec2 p = resultUV * seed;\n vec3 p3 = fract(vec3(p.xyx) * HASHSCALE1);\n p3 += dot(p3, p3.yzx + 19.19);\n return fract((p3.x + p3.y) * p3.z);\n }\n\n "+li+"\n "+fi+"\n "+hi+"\n "}(l);return e.isPacked?(o=function(t,e){switch(t.length){case 0:return"\n int getOutputCoords() {\n return 0;\n }\n ";case 1:return function(t,e){var n=[Math.ceil(e[0]/2),Math.ceil(e[1]/2)];return 1===n[0]?"\n int getOutputCoords() {\n return 2 * int(resultUV.x * "+n[1]+".0);\n }\n ":1===n[1]?"\n int getOutputCoords() {\n return 2 * int(resultUV.y * "+n[0]+".0);\n }\n ":"\n int getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+n[0]+", "+n[1]+"));\n return resTexRC.x * "+n[1]+" + resTexRC.y;\n }\n "}(0,e);case 2:return function(t,e){var n=[Math.ceil(e[0]/2),Math.ceil(e[1]/2)];if(E(t,e))return"\n ivec2 getOutputCoords() {\n return 2 * ivec2(resultUV.yx * vec2("+n[0]+", "+n[1]+"));\n }\n ";var r=Math.ceil(t[1]/2);return"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+n[0]+", "+n[1]+"));\n\n int index = resTexRC.x * "+n[1]+" + resTexRC.y;\n int r = 2 * (index / "+r+");\n int c = imod(index, "+r+") * 2;\n\n return ivec2(r, c);\n }\n "}(t,e);case 3:return n=t,r=e,i=[Math.ceil(r[0]/2),Math.ceil(r[1]/2)],a=(o=Math.ceil(n[2]/2))*Math.ceil(n[1]/2),"\n ivec3 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+i[0]+", "+i[1]+"));\n int index = resTexRC.x * "+i[1]+" + resTexRC.y;\n\n int b = index / "+a+";\n index -= b * "+a+";\n\n int r = 2 * (index / "+o+");\n int c = imod(index, "+o+") * 2;\n\n return ivec3(b, r, c);\n }\n ";default:return function(t,e){for(var n=[Math.ceil(e[0]/2),Math.ceil(e[1]/2)],r=Math.ceil(t[t.length-1]/2),i=r*Math.ceil(t[t.length-2]/2),o=i,a="",s="b, r, c",u=2;u0?32:16})),this.floatPrecisionValue},t.prototype.epsilon=function(){return 32===this.floatPrecision()?1e-7:1e-4},t.prototype.uploadToGPU=function(t){var e,n=this.texData.get(t),r=n.shape,i=n.dtype,o=n.values,a=n.texture,s=n.usage,u=n.isPacked;if(null==a){var c,l=null!=this.activeTimers;l&&(c=performance.now());var f=ye(r,u);n.texShape=f;var h=this.acquireTexture(f,s,i,u);if(n.texture=h,null!=o){if(u){var d=ve(r),p=1,m=1;r.length&&(p=(e=ge(r))[0],m=e[1]),this.gpgpu.uploadMatrixToPackedTexture(h,d,p,m,f[0],f[1],Oo(o))}else this.gpgpu.uploadMatrixToTexture(h,f[0],f[1],Oo(o));n.values=null,l&&(this.uploadWaitMs+=performance.now()-c)}}},t.prototype.convertAndCacheOnCPU=function(t,e){var n=this.texData.get(t),r=n.dtype;return this.releaseGPUData(t),n.usage=Ai.UPLOAD,null!=e&&(n.values=function(t,e){if("float32"===e||"complex64"===e)return t;if("int32"===e||"bool"===e){for(var n="int32"===e?new Int32Array(t.length):new Uint8Array(t.length),r=0;r1024*this.numMBBeforeWarning*1024){var i=(this.numBytesInGPU/1024/1024).toFixed(2);this.warnedAboutMemory=!0,console.warn("High memory usage in GPU: "+i+" MB, most likely due to a memory leak")}return this.textureManager.acquireTexture(t,e,r)},t.prototype.computeBytes=function(t,e){return t[0]*t[1]*B(e)},t}();function Oo(t){return t instanceof Float32Array?t:new Float32Array(t)}jt()&&Dt.registerBackend("webgl",function(){return new Ao},2);var Io=Tn({abs_:function(t){var e=kn(t,"x","abs");return"complex64"===e.dtype?Dt.runKernel(function(t){return t.complexAbs(e)},{$x:e}):Dt.runKernel(function(t,n){var r=t.abs(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.mul(n.toFloat().step(-1))}}})}}),To=Tn({acos_:function(t){var e=kn(t,"x","acos");return Dt.runKernel(function(t,n){var r=t.acos(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.divStrict(Ln(1).sub(n.toFloat().square()).sqrt()).neg()}}})}}),Mo=Tn({acosh_:function(t){var e=kn(t,"x","acosh");return Dt.runKernel(function(t,n){var r=t.acosh(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.divStrict(n.toFloat().square().sub(1).sqrt())}}})}}),Ro=Tn({asin_:function(t){var e=kn(t,"x","asin");return Dt.runKernel(function(t,n){var r=t.asin(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.divStrict(Ln(1).sub(n.toFloat().square()).sqrt())}}})}}),Do=Tn({asinh_:function(t){var e=kn(t,"x","asinh");return Dt.runKernel(function(t,n){var r=t.asinh(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.divStrict(Ln(1).add(n.toFloat().square()).sqrt())}}})}}),jo=Tn({atan_:function(t){var e=kn(t,"x","atan");return Dt.runKernel(function(t,n){var r=t.atan(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.div(n.toFloat().square().add(1))}}})}}),Po=Tn({atanh_:function(t){var e=kn(t,"x","atanh");return Dt.runKernel(function(t,n){var r=t.atanh(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.div(Ln(1).sub(n.toFloat().square()))}}})}}),zo=Tn({ceil_:function(t){var e=kn(t,"x","ceil");return Dt.runKernel(function(t){return t.ceil(e)},{$x:e},function(t){return{$x:function(){return Jn(t)}}})}}),Lo=Tn({clipByValue_:function(t,e,n){var r=kn(t,"x","clipByValue");return w(e<=n,function(){return"Error in clip: min ("+e+") must be less than or equal to max ("+n+")."}),Dt.runKernel(function(t,i){var o=t.clip(r,e,n);return i([r]),o},{$x:r},function(t,r){var i=r[0];return{$x:function(){return t.where(i.greaterEqual(e).logicalAnd(i.lessEqual(n)),Jn(t))}}})}}),Fo=Tn({cos_:function(t){var e=kn(t,"x","cos");return Dt.runKernel(function(t,n){var r=t.cos(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return n.toFloat().sin().neg().mul(t)}}})}}),Bo=Tn({cosh_:function(t){var e=kn(t,"x","cosh");return Dt.runKernel(function(t,n){var r=t.cosh(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return n.toFloat().sinh().mulStrict(t)}}})}}),Uo=Tn({erf_:function(t){var e=kn(t,"x","erf");return w("int32"===e.dtype||"float32"===e.dtype,function(){return"Input dtype must be `int32` or `float32`."}),"int32"===e.dtype&&(e=e.toFloat()),Dt.runKernel(function(t,n){var r=t.erf(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.mul(n.square().neg().exp().mul(2/Math.sqrt(Math.PI)))}}})}}),Vo=Tn({exp_:function(t){var e=kn(t,"x","exp");return Dt.runKernel(function(t,n){var r=t.exp(e);return n([r]),r},{$x:e},function(t,e){return{$x:function(){return t.mulStrict(e[0])}}})}}),Wo=Tn({expm1_:function(t){var e=kn(t,"x","expm1");return Dt.runKernel(function(t,n){var r=t.expm1(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.mul(n.exp())}}})}}),qo=Tn({floor_:function(t){var e=kn(t,"x","floor");return Dt.runKernel(function(t){return t.floor(e)},{$x:e},function(t){return{$x:function(){return Jn(t)}}})}}),Ho=Tn({log_:function(t){var e=kn(t,"x","log");return Dt.runKernel(function(t,n){var r=t.log(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.div(n.toFloat())}}})}}),Go=Tn({log1p_:function(t){var e=kn(t,"x","log1p");return Dt.runKernel(function(t,n){var r=t.log1p(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.div(n.add(1))}}})}}),Ko=Tn({logSigmoid_:function(t){var e=kn(t,"x","logSigmoid");return Dt.runKernel(function(t,n){var r=t.softplus(e.neg()).neg();return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.mul(n.neg().sigmoid())}}})}}),Xo=Tn({neg_:function(t){var e=kn(t,"x","neg");return Dt.runKernel(function(t){return t.neg(e)},{$x:e},function(t){return{$x:function(){return t.neg()}}})}}),$o=Tn({reciprocal_:function(t){var e=kn(t,"x","reciprocal");return Dt.runKernel(function(t,n){var r=t.reciprocal(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.div(n.square().neg())}}})}}),Yo=Tn({round_:function(t){var e=kn(t,"x","round");return Dt.runKernel(function(t){return t.round(e)},{$x:e},function(t){return{$x:function(){return Jn(t)}}})}}),Jo=Tn({rsqrt_:function(t){var e=kn(t,"x","rsqrt");return Dt.runKernel(function(t,n){var r=t.rsqrt(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.div(n.pow(1.5).mul(2)).neg()}}})}}),Zo=Tn({sigmoid_:function(t){var e=kn(t,"x","sigmoid");return Dt.runKernel(function(t,n){var r=t.sigmoid(e);return n([r]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.mul(n.mul(Ln(1).sub(n)))}}})}}),Qo=Tn({sign_:function(t){var e=kn(t,"x","sign");return Dt.runKernel(function(t){return t.sign(e)},{$x:e},function(t){return{$x:function(){return Jn(t)}}})}}),ta=Tn({isNaN_:function(t){var e=kn(t,"x","isNaN");return Dt.runKernel(function(t){return t.isNaN(e)},{$x:e},function(t){return{$x:function(){return Jn(t)}}})}}),ea=Tn({isInf_:function(t){var e=kn(t,"x","isInf");return Dt.runKernel(function(t){return t.isInf(e)},{$x:e},function(t){return{$x:function(){return Jn(t)}}})}}),na=Tn({isFinite_:function(t){var e=kn(t,"x","isFinite");return Dt.runKernel(function(t){return t.isFinite(e)},{$x:e},function(t){return{$x:function(){return Jn(t)}}})}}),ra=Tn({sin_:function(t){var e=kn(t,"x","sin");return Dt.runKernel(function(t,n){var r=t.sin(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return n.toFloat().cos().mul(t)}}})}}),ia=Tn({sinh_:function(t){var e=kn(t,"x","sinh");return Dt.runKernel(function(t,n){var r=t.sinh(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return n.toFloat().cosh().mulStrict(t)}}})}}),oa=Tn({softplus_:function(t){var e=kn(t,"x","softplus");return Dt.runKernel(function(t,n){var r=t.softplus(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.mul(n.sigmoid())}}})}}),aa=Tn({sqrt_:function(t){var e=kn(t,"x","sqrt");return Dt.runKernel(function(t,n){var r=t.sqrt(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.div(n.toFloat().sqrt().mul(2))}}})}}),sa=Tn({square_:function(t){var e=kn(t,"x","square");return Dt.runKernel(function(t,n){return n([e]),t.square(e)},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.mul(n.toFloat().mul(2))}}})}}),ua=Tn({step_:function(t,e){void 0===e&&(e=0);var n=kn(t,"x","step");return Dt.runKernel(function(t){return t.step(n,e)},{$x:n},function(t){return{$x:function(){return Jn(t)}}})}}),ca=Tn({tan_:function(t){var e=kn(t,"x","tan");return Dt.runKernel(function(t,n){var r=t.tan(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.div(n.cos().square())}}})}}),la=Tn({tanh_:function(t){var e=kn(t,"x","tanh");return Dt.runKernel(function(t,n){var r=t.tanh(e);return n([r]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return Ln(1).sub(n.square()).mulStrict(t)}}})}});function fa(t,e,n,r,i,o){var a,s,u=kn(t,"x","batchNorm"),c=kn(e,"mean","batchNorm"),l=kn(n,"variance","batchNorm");return null!=i&&(a=kn(i,"scale","batchNorm")),null!=r&&(s=kn(r,"offset","batchNorm")),w(2===u.rank,function(){return"Error in batchNorm3D: x must be rank 3 but got rank "+u.rank+"."}),w(2===c.rank||1===c.rank,function(){return"Error in batchNorm2D: mean must be rank 2 or rank 1 but got rank "+c.rank+"."}),w(2===l.rank||1===l.rank,function(){return"Error in batchNorm2D: variance must be rank 2 or rank 1 but got rank "+l.rank+"."}),null!=a&&w(2===a.rank||1===a.rank,function(){return"Error in batchNorm2D: scale must be rank 2 or rank 1 but got rank "+a.rank+"."}),null!=s&&w(2===s.rank||1===s.rank,function(){return"Error in batchNorm2D: offset must be rank 2 or rank 1 but got rank "+s.rank+"."}),pa(u,c,l,s,a,o)}function ha(t,e,n,r,i,o){var a,s,u=kn(t,"x","batchNorm"),c=kn(e,"mean","batchNorm"),l=kn(n,"variance","batchNorm");return null!=i&&(a=kn(i,"scale","batchNorm")),null!=r&&(s=kn(r,"offset","batchNorm")),w(3===u.rank,function(){return"Error in batchNorm3D: x must be rank 3 but got rank "+u.rank+"."}),w(3===c.rank||1===c.rank,function(){return"Error in batchNorm3D: mean must be rank 3 or rank 1 but got rank "+c.rank+"."}),w(3===l.rank||1===l.rank,function(){return"Error in batchNorm3D: variance must be rank 3 or rank 1 but got rank "+l.rank+"."}),null!=a&&w(3===a.rank||1===a.rank,function(){return"Error in batchNorm3D: scale must be rank 3 or rank 1 but got rank "+a.rank+"."}),null!=s&&w(3===s.rank||1===s.rank,function(){return"Error in batchNorm3D: offset must be rank 3 or rank 1 but got rank "+s.rank+"."}),pa(u,c,l,s,a,o)}function da(t,e,n,r,i,o){var a,s,u=kn(t,"x","batchNorm"),c=kn(e,"mean","batchNorm"),l=kn(n,"variance","batchNorm");return null!=i&&(a=kn(i,"scale","batchNorm")),null!=r&&(s=kn(r,"offset","batchNorm")),w(4===u.rank,function(){return"Error in batchNorm4D: x must be rank 4 but got rank "+u.rank+"."}),w(4===c.rank||1===c.rank,function(){return"Error in batchNorm4D: mean must be rank 4 or rank 1 but got rank "+c.rank+"."}),w(4===l.rank||1===l.rank,function(){return"Error in batchNorm4D: variance must be rank 4 or rank 1 but got rank "+l.rank+"."}),null!=a&&w(4===a.rank||1===a.rank,function(){return"Error in batchNorm4D: scale must be rank 4 or rank 1 but got rank "+a.rank+"."}),null!=s&&w(4===s.rank||1===s.rank,function(){return"Error in batchNorm4D: offset must be rank 4 or rank 1 but got rank "+s.rank+"."}),pa(u,c,l,s,a,o)}function pa(t,e,n,r,i,o){null==o&&(o=.001);var a,s,u,c=kn(t,"x","batchNorm"),l=kn(e,"mean","batchNorm"),f=kn(n,"variance","batchNorm");return null!=i&&(a=kn(i,"scale","batchNorm")),null!=r&&(s=kn(r,"offset","batchNorm")),w(l.rank===f.rank,function(){return"Batch normalization gradient requires mean and variance to have equal ranks."}),w(null==s||l.rank===s.rank,function(){return"Batch normalization gradient requires mean and offset to have equal ranks."}),w(null==a||l.rank===a.rank,function(){return"Batch normalization gradient requires mean and scale to have equal ranks."}),u=0===c.rank||1===c.rank?c.as4D(1,1,1,c.size):2===c.rank?c.as4D(1,1,c.shape[0],c.shape[1]):3===c.rank?c.as4D(1,c.shape[0],c.shape[1],c.shape[2]):c,Dt.runKernel(function(t,e){var n=t.batchNormalization(u,ma(l),ma(f),o,ma(a),ma(s));return e([c,l,f,a]),n},{$x:c,$mean:l,$variance:f,$scale:a,$offset:s},function(t,e){var n=e[0],r=e[1],i=e[2],a=e[3],s=null==a?Ln(1):a,c=oi(r.shape,u.shape),l=[];if(1===r.rank){for(var f=0;f=2&&a.rank>=2&&o.rank===a.rank,function(){return"Error in matMul: inputs must have the same rank of at least 2, got ranks "+o.rank+" and "+a.rank+"."}),w(E(f,h),function(){return"Error in matMul: outer dimensions ("+f+") and ("+h+") of Tensors with shapes "+o.shape+" and "+a.shape+" must match."}),w(s===u,function(){return"Error in matMul: inner shapes ("+s+") and ("+u+") of Tensors with shapes "+o.shape+" and "+a.shape+" and transposeA="+n+" and transposeB="+r+" must match."});var m=o.shape.slice(0,-2).concat([c,l]),v=n?o.as3D(d,s,c):o.as3D(d,c,s),g=r?a.as3D(p,l,u):a.as3D(p,u,l);return Dt.runKernel(function(t,e){var i=t.batchMatMul(v,g,n,r);return e([v,g]),i},{$a:v,$b:g},function(t,e){var i=e,o=i[0],a=i[1];return n||r?!n&&r?{$a:function(){return t.matMul(a,!1,!1)},$b:function(){return t.matMul(o,!0,!1)}}:n&&!r?{$a:function(){return a.matMul(t,!1,!0)},$b:function(){return o.matMul(t,!1,!1)}}:{$a:function(){return a.matMul(t,!0,!0)},$b:function(){return t.matMul(o,!0,!0)}}:{$a:function(){return t.matMul(a,!1,!0)},$b:function(){return o.matMul(t,!0,!1)}}}).reshape(m)}}),Ha=Tn({dot_:function(t,e){var n=kn(t,"t1","dot"),r=kn(e,"t2","dot");w(!(1!==n.rank&&2!==n.rank||1!==r.rank&&2!==r.rank),function(){return"Error in dot: inputs must all be rank 1 or 2, but got ranks "+n.rank+" and "+r.rank+"."});var i=1===n.rank?n.size:n.shape[1],o=1===r.rank?r.size:r.shape[0];return w(i===o,function(){return"Error in dot: inner dimensions of inputs must match, but got "+i+" and "+o+"."}),1===n.rank&&1===r.rank?n.as2D(1,-1).matMul(r.as2D(-1,1)).asScalar():1===n.rank&&2===r.rank?n.as2D(1,-1).matMul(r.as2D(r.shape[0],r.shape[1])).as1D():2===n.rank&&1===r.rank?n.matMul(r.as2D(-1,1)).as1D():n.matMul(r.as2D(r.shape[0],r.shape[1]))}}),Ga=Tn({outerProduct_:function(t,e){var n=kn(t,"v1","outerProduct"),r=kn(e,"v2","outerProduct");return w(1===n.rank&&1===r.rank,function(){return"Error in outerProduct: inputs must be rank 1, but got ranks "+n.rank+" and "+r.rank+"."}),n.as2D(-1,1).matMul(r.as2D(1,-1))}}),Ka=Tn({reverse_:function(t,e){var n=kn(t,"x","reverse");if(0===n.rank)return n.clone();var r=M(e,n.shape);return Dt.runKernel(function(t){return t.reverse(n,r)},{$x:n},function(t){return{$x:function(){return t.reverse(r)}}}).reshapeAs(n)}}),Xa=Tn({reverse1d_:function(t){var e=kn(t,"x","reverse");return w(1===e.rank,function(){return"Error in reverse1D: x must be rank 1 but got rank "+e.rank+"."}),Ka(e,0)}}),$a=Tn({reverse2d_:function(t,e){var n=kn(t,"x","reverse");return w(2===n.rank,function(){return"Error in reverse2D: x must be rank 2 but got rank "+n.rank+"."}),Ka(n,e)}}),Ya=Tn({reverse3d_:function(t,e){var n=kn(t,"x","reverse");return w(3===n.rank,function(){return"Error in reverse3D: x must be rank 3 but got rank "+n.rank+"."}),Ka(n,e)}}),Ja=Tn({reverse4d_:function(t,e){var n=kn(t,"x","reverse");return w(4===n.rank,function(){return"Error in reverse4D: x must be rank 4 but got rank "+n.rank+"."}),Ka(n,e)}});function Za(t,e,n,r,i,o){var a=kn(t,"x","maxPool"),s=a,u=!1;3===a.rank&&(u=!0,s=a.as4D(1,a.shape[0],a.shape[1],a.shape[2])),null==r&&(r=[1,1]),w(4===s.rank,function(){return"Error in maxPool: input must be rank 4 but got rank "+s.rank+"."}),w(Ra(n,r),function(){return"Error in maxPool: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+r+"'"}),null!=o&&w(C(i),function(){return"Error in maxPool: pad must be an integer when using, dimRoundingMode "+o+" but got pad "+i+"."});var c=Ea(s.shape,e,n,r,i,o),l=Dt.runKernel(function(t,e){var n=t.maxPool(s,c);return e([s,n]),n},{x:s},function(t,o){var a=o[0],s=o[1];return{x:function(){return function(t,e,n,r,i,o,a,s){var u=kn(t,"dy","maxPoolBackprop"),c=kn(e,"input","maxPoolBackprop"),l=kn(n,"output","maxPoolBackprop");w(c.rank===u.rank,function(){return"Rank of input ("+c.rank+") does not match rank of dy ("+u.rank+")"}),null==o&&(o=[1,1]),w(Ra(i,o),function(){return"Error in maxPoolBackProp: Either strides or dilations must be 1. Got strides "+i+" and dilations '"+o+"'"}),w(4===u.rank,function(){return"Error in maxPoolBackprop: dy must be rank 4 but got rank "+u.rank+"."}),w(4===c.rank,function(){return"Error in maxPoolBackprop: input must be rank 4 but got rank "+c.rank+"."});var f=Ea(c.shape,r,i,o,a,void 0);return Dt.runKernel(function(t){return t.maxPoolBackprop(u,c,l,f)},{$dy:u,$input:c})}(t,a,s,e,n,r,i)}}});return u?l.as3D(l.shape[1],l.shape[2],l.shape[3]):l}function Qa(t,e,n,r,i,o){var a=kn(t,"x","avgPool","float32");null==r&&(r=[1,1]),w(Ra(n,r),function(){return"Error in avgPool: Either strides or dilations must be 1. Got strides "+n+" and dilations '"+r+"'"});var s=a,u=!1;3===a.rank&&(u=!0,s=a.as4D(1,a.shape[0],a.shape[1],a.shape[2])),w(4===s.rank,function(){return"Error in avgPool: x must be rank 4 but got rank "+s.rank+"."}),null!=o&&w(C(i),function(){return"Error in avgPool: pad must be an integer when using, dimRoundingMode "+o+" but got pad "+i+"."});var c=Ea(s.shape,e,n,r,i,o),l=Dt.runKernel(function(t){return t.avgPool(s,c)},{x:s},function(t){return{x:function(){return function(t,e,n,r,i,o){var a=kn(t,"dy","avgPoolBackprop"),s=kn(e,"input","avgPoolBackprop");w(s.rank===a.rank,function(){return"Rank of input ("+s.rank+") does not match rank of dy ("+a.rank+")"}),null==i&&(i=[1,1]),w(Ra(r,i),function(){return"Error in avgPoolBackprop: Either strides or dilations must be 1. Got strides "+r+" and dilations '"+i+"'"});var u=s,c=a,l=!1;3===s.rank&&(l=!0,u=s.as4D(1,s.shape[0],s.shape[1],s.shape[2]),c=a.as4D(1,a.shape[0],a.shape[1],a.shape[2])),w(4===c.rank,function(){return"Error in avgPoolBackprop: dy must be rank 4 but got rank "+c.rank+"."}),w(4===u.rank,function(){return"Error in avgPoolBackprop: input must be rank 4 but got rank "+u.rank+"."});var f=Ea(u.shape,n,r,i,o),h=Dt.runKernel(function(t){return t.avgPoolBackprop(c,u,f)},{dy4D:c,input4D:u});return l?h.as3D(h.shape[1],h.shape[2],h.shape[3]):h}(t,s,e,n,r,i)}}});return l=l.cast(a.dtype),u?l.as3D(l.shape[1],l.shape[2],l.shape[3]):l}var ts=Tn({maxPool_:function(t,e,n,r,i){return Za(t,e,n,1,r,i)}}),es=Tn({avgPool_:function(t,e,n,r,i){return Qa(t,e,n,1,r,i)}}),ns=Tn({pool_:function(t,e,n,r,i,o){null==i&&(i=[1,1]),null==o&&(o=1),0===r&&(r="valid");var a=kn(t,"x","maxPool"),s=a,u=!1;3===a.rank&&(u=!0,s=a.as4D(1,a.shape[0],a.shape[1],a.shape[2])),w(Ra(o,i),function(){return"Error in pool: Either strides or dilations must be 1. Got strides "+o+" and dilations '"+i+"'"});var c,l=Ea(s.shape,e,o,i,r),f=[l.dilationHeight,l.dilationWidth];c="same"===r?function(t,e){var n=t.map(function(t,n){return t+(t-1)*(e[n]-1)}).map(function(t){return t-1}),r=n.map(function(t){return Math.floor(t/2)}),i=n.map(function(t,e){return t-r[e]});return n.map(function(t,e){return[r[e],i[e]]})}([l.filterHeight,l.filterWidth],f):[[0,0],[0,0]];var h=1===f[0]&&1===f[1],d=function(t,e,n){var r=n.map(function(t){return t[0]}),i=n.map(function(t){return t[1]}),o=t.concat(r,i),a=e.map(function(t,e){return(t-o[e]%t)%t}),s=i.map(function(t,e){return t+a[e]});return[e.map(function(t,e){return[r[e],s[e]]}),e.map(function(t,e){return[0,a[e]]})]}([l.inHeight,l.inWidth],f,c),p=d[0],m=d[1],v=h?r:"valid",g=h?s:Xr(s,f,p),y=("avg"===n?function(){return Qa(g,e,o,1,v)}:function(){return Za(g,e,o,1,v)})(),b=h?y:Ir(y,f,m);return u?b.as3D(b.shape[1],b.shape[2],b.shape[3]):b}}),rs=Tn({slice_:function(t,e,n){var r,i,o=kn(t,"x","slice");if(0===o.rank)throw new Error("Slicing scalar is not possible");r="number"==typeof e?[e].concat(new Array(o.rank-1).fill(0)):e.length=0?t:(w(-1===t,function(){return"Bad value in size"}),o.shape[e]-r[e])}),function(t,e,n){w(t.rank===e.length,function(){return"Error in slice"+t.rank+"D: Length of begin "+e+" must match the rank of the array ("+t.rank+")."}),w(t.rank===n.length,function(){return"Error in slice"+t.rank+"D: Length of size "+n+" must match the rank of the array ("+t.rank+")."});for(var r=function(r){w(e[r]+n[r]<=t.shape[r],function(){return"Error in slice"+t.rank+"D: begin["+r+"] + size["+r+"] ("+(e[r]+n[r])+") would overflow input.shape["+r+"] ("+t.shape[r]+")"})},i=0;i0&&(e=e.sum(n)),e.reshape(r.shape)},$b:function(){var e=t,n=oi(i.shape,o);return n.length>0&&(e=e.sum(n)),e.reshape(i.shape)}}})}}),Rs=Tn({addN_:function(t){w(Array.isArray(t),function(){return"The argument passed to tf.addN() must be a list of tensors"}),w(t.length>=1,function(){return"Must pass at least one tensor to tf.addN(), but got "+t.length});var e=t.map(function(t,e){return kn(t,"tensors"+e,"addN")}),n=e[0];e.forEach(function(t){if(t.dtype!==n.dtype)throw new Error("All tensors passed to tf.addN() must have the same dtype")}),e.forEach(function(t){if(!E(t.shape,n.shape))throw new Error("All tensors passed to tf.addN() must have the same shape")});var r=e;return Dt.runKernel(function(t){return t.addN(e)},r,function(t){var n={};return e.forEach(function(e,r){n[r]=function(){return t.clone()}}),n})}}),Ds=Tn({addStrict_:function(t,e){var n=kn(t,"a","addStrict"),r=kn(e,"b","addStrict");return x(n.shape,r.shape,"Error in addStrict: "),n.add(r)}}),js=Tn({atan2_:function(t,e){var n,r=kn(t,"a","atan2"),i=kn(e,"b","atan2");n=Nt(r,i),r=n[0],i=n[1];var o=ai(r.shape,i.shape);return Dt.runKernel(function(t,e){var n=t.atan2(r,i);return e([r,i]),n},{$a:r,$b:i},function(t,e){var n=e[0],r=e[1];return{$a:function(){var e=Ms(n.square(),r.square()),i=t.mul(r.div(e)),a=oi(n.shape,o);return a.length>0&&(i=i.sum(a)),i.reshape(n.shape)},$b:function(){var e=Ms(n.square(),r.square()),i=Xo(t.mul(n.div(e))),a=oi(r.shape,o);return a.length>0&&(i=i.sum(a)),i.reshape(r.shape)}}})}}),Ps=Tn({div_:function(t,e){var n,r=kn(t,"a","div"),i=kn(e,"b","div");if(n=Nt(r,i),r=n[0],i=n[1],"int32"===r.dtype&&"int32"===i.dtype)return Ls(r,i);var o=ai(r.shape,i.shape);return Dt.runKernel(function(t,e){var n=t.realDivide(r,i);return e([r,i]),n},{$a:r,$b:i},function(t,e){var n=e[0],r=e[1];return{$a:function(){var e=t.div(r.toFloat()),i=oi(n.shape,o);return i.length>0?e.sum(i).reshape(n.shape):e},$b:function(){var e=t.mul(n.toFloat()),i=oi(r.shape,o);i.length>0&&(e=e.sum(i).reshape(r.shape));var a=r.square();return e.div(a.toFloat()).neg()}}})}}),zs=Tn({divStrict_:function(t,e){var n=kn(t,"a","div"),r=kn(e,"b","div");return x(n.shape,r.shape,"Error in divideStrict: "),n.div(r)}}),Ls=Tn({floorDiv_:function(t,e){var n,r=kn(t,"a","floorDiv"),i=kn(e,"b","floorDiv");n=Nt(r,i),r=n[0],i=n[1];var o=ai(r.shape,i.shape);return Dt.runKernel(function(t,e){var n=t.floorDiv(r,i);return e([r,i]),n},{$a:r,$b:i},function(t,e){var n=e[0],r=e[1];return{$a:function(){var e=t.div(r.toFloat()),i=oi(n.shape,o);return i.length>0?e.sum(i).reshape(n.shape):e},$b:function(){var e=t.mul(n.toFloat()),i=oi(r.shape,o);i.length>0&&(e=e.sum(i).reshape(r.shape));var a=r.square();return e.div(a.toFloat()).neg()}}})}}),Fs=Tn({maximum_:function(t,e){var n,r=kn(t,"a","maximum"),i=kn(e,"b","maximum");return n=Nt(r,i),r=n[0],i=n[1],"bool"===r.dtype&&(r=r.toInt(),i=i.toInt()),ai(r.shape,i.shape),Dt.runKernel(function(t,e){var n=t.maximum(r,i);return e([r,i]),n},{$a:r,$b:i},function(t,e){var n=e[0],r=e[1];return{$a:function(){return t.mul(n.greaterEqual(r).toFloat())},$b:function(){return t.mul(n.less(r).toFloat())}}})}}),Bs=Tn({maximumStrict_:function(t,e){var n=kn(t,"a","maximumStrict"),r=kn(e,"b","maximumStrict");return x(n.shape,r.shape,"Error in maximumStrict: "),n.maximum(r)}}),Us=Tn({minimum_:function(t,e){var n,r=kn(t,"a","minimum"),i=kn(e,"b","minimum");return n=Nt(r,i),r=n[0],i=n[1],"bool"===r.dtype&&(r=r.toInt(),i=i.toInt()),ai(r.shape,i.shape),Dt.runKernel(function(t,e){var n=t.minimum(r,i);return e([r,i]),n},{$a:r,$b:i},function(t,e){var n=e[0],r=e[1];return{$a:function(){return t.mul(n.lessEqual(r).toFloat())},$b:function(){return t.mul(n.greater(r).toFloat())}}})}}),Vs=Tn({minimumStrict_:function(t,e){var n=kn(t,"a","minimumStrict"),r=kn(e,"b","minimumStrict");return x(n.shape,r.shape,"Error in minimumStrict: "),n.minimum(r)}}),Ws=Tn({mod_:function(t,e){var n,r=kn(t,"a","mod"),i=kn(e,"b","mod");n=Nt(r,i),r=n[0],i=n[1];var o=ai(r.shape,i.shape);return Dt.runKernel(function(t,e){var n=t.mod(r,i);return e([r,i]),n},{$a:r,$b:i},function(t,e){var n=e[0],r=e[1];return{$a:function(){var e=oi(n.shape,o);return e.length>0?t.sum(e).reshape(n.shape):t},$b:function(){var e=t.mul(n.div(r).floor().neg()),i=oi(r.shape,o);return i.length>0?e.sum(i).reshape(r.shape):e}}})}}),qs=Tn({modStrict_:function(t,e){var n=kn(t,"a","modStrict"),r=kn(e,"b","modStrict");return x(n.shape,r.shape,"Error in modStrict: "),n.mod(r)}}),Hs=Tn({mul_:function(t,e){var n,r=kn(t,"a","mul"),i=kn(e,"b","mul");n=Nt(r,i),r=n[0],i=n[1];var o=ai(r.shape,i.shape);return Dt.runKernel(function(t,e){var n=t.multiply(r,i);return e([r,i]),n},{$a:r,$b:i},function(t,e){var n=e[0],r=e[1];return{$a:function(){var e=t.mul(r.toFloat()),i=oi(n.shape,o);return i.length>0?e.sum(i).reshape(n.shape):e},$b:function(){var e=t.mul(n.toFloat()),i=oi(r.shape,o);return i.length>0?e.sum(i).reshape(r.shape):e}}})}}),Gs=Tn({mulStrict_:function(t,e){var n=kn(t,"a","mul"),r=kn(e,"b","mul");return x(n.shape,r.shape,"Error in multiplyStrict: "),n.mul(r)}}),Ks=Tn({pow_:function(t,e){var n=kn(t,"base","pow"),r=kn(e,"exp","pow"),i=ai(n.shape,r.shape);return t=n.cast(Et(n.dtype,r.dtype)),e=r.cast(Et(n.dtype,r.dtype)),Dt.runKernel(function(t,e){var i=t.pow(n,r);return e([n,r,i]),i},{$base:n,$exp:r},function(t,e){var n=e[0],r=e[1],o=e[2];return{$base:function(){var e=r.toFloat(),o=t.mul(e.mul(n.pow(e.sub(Ln(1))))),a=oi(n.shape,i);return a.length>0&&(o=o.sum(a)),o.reshape(n.shape)},$exp:function(){var e=n.greater(0),a=n.log().where(e,Jn(n)),s=t.mul(o.mul(a)),u=oi(r.shape,i);return u.length>0&&(s=s.sum(u)),s.reshape(r.shape)}}})}}),Xs=Tn({powStrict_:function(t,e){return x(t.shape,e.shape,"Error in powStrict: "),t.pow(e)}}),$s=Tn({squaredDifference_:function(t,e){var n,r=kn(t,"a","squaredDifference"),i=kn(e,"b","squaredDifference");return n=Nt(r,i),r=n[0],i=n[1],ai(r.shape,i.shape),Dt.runKernel(function(t,e){var n=t.squaredDifference(r,i);return e([r,i]),n},{$a:r,$b:i},function(t,e){var n=e[0],r=e[1],i=Ln(2);return{$a:function(){return t.mul(n.sub(r).mul(i))},$b:function(){return t.mul(r.sub(n).mul(i))}}})}}),Ys=Tn({squaredDifferenceStrict_:function(t,e){var n=kn(t,"a","squaredDifferenceStrict"),r=kn(e,"b","squaredDifferenceStrict");return x(n.shape,r.shape,"Error in squaredDifferenceStrict: "),n.squaredDifference(r)}}),Js=Tn({sub_:function(t,e){var n,r=kn(t,"a","sub"),i=kn(e,"b","sub");n=Nt(r,i),r=n[0],i=n[1];var o=ai(r.shape,i.shape);return Dt.runKernel(function(t){return t.subtract(r,i)},{$a:r,$b:i},function(t){return{$a:function(){var e=t,n=oi(r.shape,o);return n.length>0&&(e=e.sum(n)),e.reshape(r.shape)},$b:function(){var e=t,n=oi(i.shape,o);return n.length>0&&(e=e.sum(n)),e.neg().reshape(i.shape)}}})}}),Zs=Tn({subStrict_:function(t,e){var n=kn(t,"a","subStrict"),r=kn(e,"b","subStrict");return x(n.shape,r.shape,"Error in subStrict: "),n.sub(r)}}),Qs=Tn({logicalAnd_:function(t,e){var n=kn(t,"a","logicalAnd","bool"),r=kn(e,"b","logicalAnd","bool");return ai(n.shape,r.shape),Dt.runKernel(function(t){return t.logicalAnd(n,r)},{$a:n,$b:r})}}),tu=Tn({logicalNot_:function(t){var e=kn(t,"x","logicalNot","bool");return Dt.runKernel(function(t){return t.logicalNot(e)},{$x:e})}}),eu=Tn({logicalOr_:function(t,e){var n=kn(t,"a","logicalOr","bool"),r=kn(e,"b","logicalOr","bool");return ai(n.shape,r.shape),Dt.runKernel(function(t){return t.logicalOr(n,r)},{$a:n,$b:r})}}),nu=Tn({logicalXor_:function(t,e){var n=kn(t,"a","logicalXor","bool"),r=kn(e,"b","logicalXor","bool");return ai(n.shape,r.shape),eu(t,e).logicalAnd(Qs(t,e).logicalNot())}}),ru=Tn({where_:function(t,e,n){var r=kn(e,"a","where"),i=kn(n,"b","where"),o=kn(t,"condition","where","bool");return x(r.shape,i.shape,"Error in where: "),1===o.rank?w(o.shape[0]===r.shape[0],function(){return"The first dimension of `a` must match the size of `condition`."}):x(o.shape,i.shape,"Error in where: "),Dt.runKernel(function(t,e){var n=t.select(o,r,i);return e([o]),n},{$condition:o,$a:r,$b:i},function(t,e){var n=e[0];return{$condition:function(){return Jn(n).toFloat()},$a:function(){return t.mul(n.cast(t.dtype))},$b:function(){return t.mul(n.logicalNot().cast(t.dtype))}}})}}),iu=function(t){return c(this,void 0,void 0,function(){var e,n,r;return l(this,function(i){switch(i.label){case 0:return[4,(e=kn(t,"condition","whereAsync","bool")).data()];case 1:return n=i.sent(),r=ei(e.shape,n),t!==e&&e.dispose(),[2,r]}})})},ou=Tn({elu_:function(t){var e=kn(t,"x","elu");return Dt.runKernel(function(t,n){var r=t.elu(e);return n([r]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return Dt.runKernel(function(e){return e.eluDer(t,n)},{dy:t,y:n})}}})}}),au=Tn({leakyRelu_:function(t,e){void 0===e&&(e=.2);var n=kn(t,"x","leakyRelu");return Fs(Ln(e).mul(n),n)}}),su=Tn({prelu_:function(t,e){var n=kn(t,"x","prelu"),r=kn(e,"alpha","prelu");return Dt.runKernel(function(t,e){var i=t.prelu(n,r);return e([n,r]),i},{$x:n,$alpha:r},function(t,e){var n=e[0],r=e[1],i=n.greater(0);return{$x:function(){return ru(i,t,t.mul(r))},$alpha:function(){var e=ru(i,Jn(t),t.mul(n)),o=oi(r.shape,t.shape);return o.length>0&&(e=e.sum(o)),e.reshape(r.shape)}}})}}),uu=Tn({relu_:function(t){var e=kn(t,"x","relu");return"bool"===e.dtype?e.toInt():Dt.runKernel(function(t,n){var r=t.relu(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){return t.mulStrict(n.step().toFloat())}}})}}),cu=Tn({selu_:function(t){var e=kn(t,"x","selu");return Dt.runKernel(function(t,n){var r=t.selu(e);return n([e]),r},{$x:e},function(t,e){var n=e[0];return{$x:function(){var e=n.greater(Ln(0)),r=Ln(1.7580993408473768),i=Ln(1.0507009873554805),o=t.mul(i),a=t.mul(r).mul(n.toFloat().exp());return ru(e,o,a)}}})}}),lu=Tn({transpose_:function(t,e){var n=kn(t,"x","transpose");return null==e&&(e=n.shape.map(function(t,e){return e}).reverse()),w(n.rank===e.length,function(){return"Error in transpose: rank of input "+n.rank+" must match length of perm "+e+"."}),e.forEach(function(t){w(t>=0&&ti)throw new Error("'k' passed to topk() must be <= the last dimension ("+i+") but got "+e);var o=Dt.runKernel(function(t){return t.topk(r,e,n)},{$x:r});return{values:o[0],indices:o[1]}}}),ku=Tn({scatterND_:function(t,e,n){var r=kn(t,"indices","scatterND","int32"),i=kn(e,"updates","scatterND");return function(t,e,n){if(e.rank<1)throw new Error("tf.scatterND() expects the indices to be rank 1 or higher, but the rank was "+e.rank+".");if(t.rank<1)throw new Error("tf.scatterND() expects the updates to be rank 1 or higher, but the rank was "+t.rank+".");if("int32"!==e.dtype)throw new Error("The dtype of 'indices' should be int32, but got dtype: "+e.dtype);if(n.length<1)throw new Error("Output rank must be greater or equal to 1, but got shape: "+n);if(0===n.length){if(0===e.size)throw new Error("Indices specified for empty output. indices shape: "+e.shape);if(0===t.size)throw new Error("Updates specified for empty output. updates shape: "+t.shape)}!function(t,e,n){var r=e.rank>1?e.shape[e.rank-1]:1,i=e.rank>1?e.rank-1:1,o="Must have updates.shape = indices.shape[:batchDim] + shape[sliceDim:], got updates.shape: "+n.shape+", indices.shape: "+e.shape+", shape: "+t+", sliceDim: "+r+", and batchDim: "+i+".";if(n.rank2)throw new Error("sparseIndices should be a scalar, vector, or matrix, but got shape "+t.shape+".");var i=t.rank>0?t.shape[0]:1,o=t.rank>1?t.shape[1]:1;if(n.length!==o)throw new Error("outputShape has incorrect number of elements:, "+n.length+", should be: "+o+".");var a=e.size;if(0!==e.rank&&(1!==e.rank||a!==i))throw new Error("sparseValues has incorrect shape "+e.shape+", should be [] or ["+i+"]");if(e.dtype!==r.dtype)throw new Error("sparseValues.dtype must match defaultValues.dtype")}(i,o,n,a),Dt.runKernel(function(t){return t.sparseToDense(i,o,n,a)},{$sparseIndices:i,$sparseValues:o,$defaultValue:a})}}),Ou=Tn({gatherND_:function(t,e){var n=kn(e,"indices","gatherND","int32"),r=kn(t,"x","gatherND");return Dt.runKernel(function(t){return t.gatherND(r,n)},{$x:r,$indices:n})}}),Iu=Tn({dropout_:function(t,e,n,r){if(null!=n&&!E(t.shape,n))throw new Error("Non-default noise shape is not implemented yet: "+JSON.stringify(n));var i=Gr(t.shape,0,1,"float32",r).greater(e);return i=i.div(Js(1,e)),t.mul(i)}});function Tu(t,e,n){for(var r=1-t%2,i=new Float32Array(t),o=0;o1?s.div(Ln(a)):s}if(n===Mu.SUM_BY_NONZERO_WEIGHTS){if(null==i)return o.sum().div(Ln(r.size));var u=i.mul(Hn(r.shape)).notEqual(Ln(0)).sum().toFloat();return o.sum().div(u)}throw Error("Unknown reduction: "+n)}}),zu=Tn({cosineDistance_:function(t,e,n,r,i){void 0===i&&(i=Mu.SUM_BY_NONZERO_WEIGHTS);var o=kn(t,"labels","cosineDistance"),a=kn(e,"predictions","cosineDistance"),s=null;null!=r&&(s=kn(r,"weights","cosineDistance")),x(o.shape,a.shape,"Error in cosineDistance: ");var u=Ln(1).sub(o.mul(a).sum(n,!0));return Pu(u,s,i)}}),Lu=Tn({hingeLoss_:function(t,e,n,r){void 0===r&&(r=Mu.SUM_BY_NONZERO_WEIGHTS);var i=kn(t,"labels","hingeLoss"),o=kn(e,"predictions","hingeLoss"),a=null;null!=n&&(a=kn(n,"weights","hingeLoss")),x(i.shape,o.shape,"Error in hingeLoss: ");var s=Ln(1);i=Ln(2).mul(i).sub(s);var u=s.sub(i.mul(o)).relu();return Pu(u,a,r)}}),Fu=Tn({huberLoss_:function(t,e,n,r,i){void 0===r&&(r=1),void 0===i&&(i=Mu.SUM_BY_NONZERO_WEIGHTS);var o=kn(t,"labels","huberLoss"),a=kn(e,"predictions","huberLoss"),s=null;null!=n&&(s=kn(n,"weights","huberLoss")),x(o.shape,a.shape,"Error in huberLoss: ");var u=Ln(r),c=a.sub(o).abs(),l=Us(c,u),f=c.sub(l),h=Ln(.5).mul(l.square()).add(u.mul(f));return Pu(h,s,i)}}),Bu=Tn({logLoss_:function(t,e,n,r,i){void 0===r&&(r=1e-7),void 0===i&&(i=Mu.SUM_BY_NONZERO_WEIGHTS);var o=kn(t,"labels","logLoss"),a=kn(e,"predictions","logLoss"),s=null;null!=n&&(s=kn(n,"weights","logLoss")),x(o.shape,a.shape,"Error in logLoss: ");var u=Ln(1),c=Ln(r),l=o.mul(a.add(c).log()).neg().sub(u.sub(o).mul(u.sub(a).add(c).log()));return Pu(l,s,i)}}),Uu=Tn({meanSquaredError_:function(t,e,n,r){void 0===r&&(r=Mu.SUM_BY_NONZERO_WEIGHTS);var i=kn(t,"labels","meanSquaredError"),o=kn(e,"predictions","meanSquaredError"),a=null;null!=n&&(a=kn(n,"weights","meanSquaredError")),x(i.shape,o.shape,"Error in meanSquaredError: ");var s=i.squaredDifference(o);return Pu(s,a,r)}}),Vu=Tn({sigmoidCrossEntropy_:function(t,e,n,r,i){void 0===r&&(r=0),void 0===i&&(i=Mu.SUM_BY_NONZERO_WEIGHTS);var o=kn(t,"multiClassLabels","sigmoidCrossEntropy"),a=kn(e,"logits","sigmoidCrossEntropy"),s=null;if(null!=n&&(s=kn(n,"weights","sigmoidCrossEntropy")),x(o.shape,a.shape,"Error in sigmoidCrossEntropy: "),r>0){var u=Ln(r),c=Ln(1),l=Ln(.5);o=o.mul(c.sub(u)).add(l.mul(u))}var f=function(t,e){var n=kn(t,"labels","sigmoidCrossEntropyWithLogits"),r=kn(e,"logits","sigmoidCrossEntropyWithLogits");x(n.shape,r.shape,"Error in sigmoidCrossEntropyWithLogits: ");var i=r.relu(),o=r.mul(n),a=r.abs().neg().exp().log1p();return i.sub(o).add(a)}(o,a);return Pu(f,s,i)}}),Wu=Tn({softmaxCrossEntropy_:function(t,e,n,r,i){void 0===r&&(r=0),void 0===i&&(i=Mu.SUM_BY_NONZERO_WEIGHTS);var o=kn(t,"onehotLabels","softmaxCrossEntropy"),a=kn(e,"logits","softmaxCrossEntropy"),s=null;if(null!=n&&(s=kn(n,"weights","softmaxCrossEntropy")),x(o.shape,a.shape,"Error in softmaxCrossEntropy: "),r>0){var u=Ln(r),c=Ln(1),l=Ln(o.shape[1]);o=o.mul(c.sub(u)).add(u.div(l))}var f=function(t,e,n){if(void 0===n&&(n=-1),-1===n&&(n=e.rank-1),n!==e.rank-1)throw Error("Softmax cross entropy along a non-last dimension is not yet supported. Labels / logits was rank "+e.rank+" and dim was "+n);return On(function(t,e,r){var i=e.logSumExp([n],!0),o=e.toFloat().sub(i);return r([t,o]),{value:o.mul(t).neg().sum([n]),gradFunc:function(t,e){var r=e[0],i=e[1],o=on(t.shape,[n]);return[t.reshape(o).mul(r.toFloat().sub(i.exp())),t.reshape(o).mul(i.exp().sub(r.toFloat()))]}}})(t,e)}(o,a);return Pu(f,s,i)}}),qu=Object.freeze({get Reduction(){return Mu},absoluteDifference:ju,computeWeightedLoss:Pu,cosineDistance:zu,hingeLoss:Lu,huberLoss:Fu,logLoss:Bu,meanSquaredError:Uu,sigmoidCrossEntropy:Vu,softmaxCrossEntropy:Wu});function Hu(t,e){return void 0===e&&(e=!1),Dt.tidy(function(){if(2!==t.shape.length)throw new Error("qr2d() requires a 2D Tensor, but got a "+t.shape.length+"D Tensor.");for(var n=t.shape[0],r=t.shape[1],i=Pr(n),o=t.clone(),a=Bn([[1]],[1,1]),s=a.clone(),u=n>=r?r:n,c=function(t){var e,u=o,c=s,l=i;e=Dt.tidy(function(){var e=o.slice([t,t],[n-t,1]),u=e.norm(),c=o.slice([t,t],[1,1]),l=c.sign().neg(),f=c.sub(l.mul(u)),h=e.div(f);s=1===h.shape[0]?a.clone():a.concat(h.slice([1,0],[h.shape[0]-1,h.shape[1]]),0);var d=l.matMul(f).div(u).neg(),p=o.slice([t,0],[n-t,r]),m=d.mul(s);o=0===t?p.sub(m.matMul(s.transpose().matMul(p))):o.slice([0,0],[t,r]).concat(p.sub(m.matMul(s.transpose().matMul(p))),0);var v=i.slice([0,t],[n,i.shape[1]-t]);return i=0===t?v.sub(v.matMul(s).matMul(m.transpose())):i.slice([0,0],[n,t]).concat(v.sub(v.matMul(s).matMul(m.transpose())),1),[s,o,i]}),s=e[0],o=e[1],i=e[2],Fe([u,c,l])},l=0;lr&&(i=i.slice([0,0],[n,r]),o=o.slice([0,0],[r,r])),[i,o]})}var Gu=Tn({gramSchmidt_:function(t){var e;if(Array.isArray(t)){e=!1,w(null!=t&&t.length>0,function(){return"Gram-Schmidt process: input must not be null, undefined, or empty"});for(var n=t[0].shape[0],r=function(e){w(t[e].shape[0]===n,function(){return"Gram-Schmidt: Non-unique lengths found in the input vectors: ("+t[e].shape[0]+" vs. "+n+")"})},i=1;i0)for(var n=0;n= 2, but got rank "+t.rank);if(2===t.rank)return Hu(t,e);var n=t.shape.slice(0,t.shape.length-2).reduce(function(t,e){return t*e}),r=[],i=[];return Qr(t.reshape([n,t.shape[t.shape.length-2],t.shape[t.shape.length-1]]),0).forEach(function(t){var n=Hu(t,e),o=n[0],a=n[1];r.push(o),i.push(a)}),[Yr(r,0).reshape(t.shape),Yr(i,0).reshape(t.shape)]}}),Xu=Object.freeze({gramSchmidt:Gu,qr:Ku});function $u(t,e,n,r,i){null==r&&(r=.5),null==i&&(i=Number.NEGATIVE_INFINITY);var o=t.shape[0];return n=Math.min(n,o),w(0<=r&&r<=1,function(){return"iouThreshold must be in [0, 1], but was '"+r+"'"}),w(2===t.rank,function(){return"boxes must be a 2D tensor, but was of rank '"+t.rank+"'"}),w(4===t.shape[1],function(){return"boxes must have 4 columns, but 2nd dimension was "+t.shape[1]}),w(1===e.rank,function(){return"scores must be a 1D tensor"}),w(e.shape[0]===o,function(){return"scores has incompatible shape with boxes. Expected "+o+", but was "+e.shape[0]}),{maxOutputSize:n,iouThreshold:r,scoreThreshold:i}}var Yu=Tn({resizeBilinear_:function(t,e,n){void 0===n&&(n=!1);var r=kn(t,"images","resizeBilinear");w(3===r.rank||4===r.rank,function(){return"Error in resizeBilinear: x must be rank 3 or 4, but got rank "+r.rank+"."}),w(2===e.length,function(){return"Error in resizeBilinear: new shape must 2D, but got shape "+e+"."});var i=r,o=!1;3===r.rank&&(o=!0,i=r.as4D(1,r.shape[0],r.shape[1],r.shape[2]));var a=e[0],s=e[1],u=Dt.runKernel(function(t,e){return e([i]),t.resizeBilinear(i,a,s,n)},{batchImages:i},function(t,e){return{batchImages:function(){return Dt.runKernel(function(r){return r.resizeBilinearBackprop(t,e[0],n)},{})}}});return o?u.as3D(u.shape[1],u.shape[2],u.shape[3]):u}}),Ju=Tn({resizeNearestNeighbor_:function(t,e,n){void 0===n&&(n=!1);var r=kn(t,"images","resizeNearestNeighbor");w(3===r.rank||4===r.rank,function(){return"Error in resizeNearestNeighbor: x must be rank 3 or 4, but got rank "+r.rank+"."}),w(2===e.length,function(){return"Error in resizeNearestNeighbor: new shape must 2D, but got shape "+e+"."}),w("float32"===r.dtype||"int32"===r.dtype,function(){return"`images` must have `int32` or `float32` as dtype"});var i=r,o=!1;3===r.rank&&(o=!0,i=r.as4D(1,r.shape[0],r.shape[1],r.shape[2]));var a=e[0],s=e[1],u=Dt.runKernel(function(t,e){return e([i]),t.resizeNearestNeighbor(i,a,s,n)},{batchImages:i},function(t,e){return{batchImages:function(){return Dt.runKernel(function(r){return r.resizeNearestNeighborBackprop(t,e[0],n)},{})}}});return o?u.as3D(u.shape[1],u.shape[2],u.shape[3]):u}}),Zu=Tn({nonMaxSuppression_:function(t,e,n,r,i){void 0===r&&(r=.5),void 0===i&&(i=Number.NEGATIVE_INFINITY);var o=kn(t,"boxes","nonMaxSuppression"),a=kn(e,"scores","nonMaxSuppression"),s=$u(o,a,n,r,i);return n=s.maxOutputSize,r=s.iouThreshold,i=s.scoreThreshold,Dt.runKernel(function(t){return t.nonMaxSuppression(o,a,n,r,i)},{$boxes:o})}}),Qu=Tn({cropAndResize_:function(t,e,n,r,i,o){var a=kn(t,"image","cropAndResize","float32"),s=kn(e,"boxes","cropAndResize","float32"),u=kn(n,"boxInd","cropAndResize","int32");i=i||"bilinear",o=o||0;var c=s.shape[0];return w(4===a.rank,function(){return"Error in cropAndResize: image must be rank 4,but got rank "+a.rank+"."}),w(2===s.rank&&4===s.shape[1],function(){return"Error in cropAndResize: boxes must be have size ["+c+",4] but had shape "+s.shape+"."}),w(1===u.rank&&u.shape[0]===c,function(){return"Error in cropAndResize: boxInd must be have size ["+c+"] but had shape "+s.shape+"."}),w(2===r.length,function(){return"Error in cropAndResize: cropSize must be of length 2, but got length "+r.length+"."}),w(r[0]>=1&&r[1]>=1,function(){return"cropSize must be atleast [1,1], but was "+r}),w("bilinear"===i||"nearest"===i,function(){return"method must be bilinear or nearest, but was "+i}),Dt.runKernel(function(t,e){return t.cropAndResize(a,s,u,r,i,o)},{$image:a,$boxes:s})}}),tc=Object.freeze({resizeBilinear:Yu,resizeNearestNeighbor:Ju,nonMaxSuppression:Zu,nonMaxSuppressionAsync:function(t,e,n,r,i){return void 0===r&&(r=.5),void 0===i&&(i=Number.NEGATIVE_INFINITY),c(this,void 0,void 0,function(){var o,a,s,u,c,f;return l(this,function(l){switch(l.label){case 0:return o=kn(t,"boxes","nonMaxSuppressionAsync"),a=kn(e,"scores","nonMaxSuppressionAsync"),s=$u(o,a,n,r,i),n=s.maxOutputSize,r=s.iouThreshold,i=s.scoreThreshold,[4,o.data()];case 1:return u=l.sent(),[4,a.data()];case 2:return c=l.sent(),f=sr(u,c,n,r,i),o!==t&&o.dispose(),a!==e&&a.dispose(),[2,f]}})})},cropAndResize:Qu}),ec=Tn({matMul_:function(t,e,n,r,i,o){var a;void 0===n&&(n=!1),void 0===r&&(r=!1),void 0===o&&(o="linear");var s=kn(t,"a","fused matMul"),u=kn(e,"b","fused matMul");a=Nt(s,u),s=a[0],u=a[1];var c=n?s.shape[s.rank-2]:s.shape[s.rank-1],l=r?u.shape[u.rank-1]:u.shape[u.rank-2],f=n?s.shape[s.rank-1]:s.shape[s.rank-2],h=r?u.shape[u.rank-2]:u.shape[u.rank-1],d=s.shape.slice(0,-2),p=u.shape.slice(0,-2),m=_(d),v=_(p);w(s.rank>=2&&u.rank>=2&&s.rank===u.rank,function(){return"Error in fused matMul: inputs must have the same rank of at least 2, got ranks "+s.rank+" and "+u.rank+"."}),w(E(d,p),function(){return"Error in fused matMul: outer dimensions ("+d+") and ("+p+") of Tensors with shapes "+s.shape+" and "+u.shape+" must match."}),w(c===l,function(){return"Error in fused matMul: inner shapes ("+c+") and ("+l+") of Tensors with shapes "+s.shape+" and "+u.shape+" and transposeA="+n+" and transposeB="+r+" must match."});var g,y=s.shape.slice(0,-2).concat([f,h]),b=n?s.as3D(m,c,f):s.as3D(m,f,c),x=r?u.as3D(v,h,l):u.as3D(v,l,h);null!=i&&ai(y,(g=Nt(g=kn(i,"bias","fused matMul"),s)[0]).shape);var k={$a:b,$b:x};return null!=i&&(k.$bias=g),Dt.runKernel(function(t,e){var i=t.fusedBatchMatMul(b,x,n,r,g,o);return e([b,x,i]),i},k,function(t,e){var a,s=e[0],u=e[1],c=e[2];if(null==o||"linear"===o)a=t;else{if("relu"!==o)throw new Error("Gradient for activation "+o+" has not been implemented yet.");a=t.mul(c.step())}var l={};return null!=i&&(l={$bias:function(){var t=a,e=oi(g.shape,a.shape);return e.length>0&&(t=t.sum(e)),t.reshape(g.shape)}}),n||r?!n&&r?Object.assign({$a:function(){return a.matMul(u,!1,!1)},$b:function(){return a.matMul(s,!0,!1)}},l):n&&!r?Object.assign({$a:function(){return u.matMul(a,!1,!0)},$b:function(){return s.matMul(a,!1,!1)}},l):Object.assign({$a:function(){return u.matMul(a,!0,!0)},$b:function(){return a.matMul(s,!0,!0)}},l):Object.assign({$a:function(){return a.matMul(u,!1,!0)},$b:function(){return s.matMul(a,!0,!1)}},l)}).reshape(y)}}),nc=Object.freeze({matMul:ec}),rc=Object.freeze({image:tc,linalg:Xu,losses:qu,spectral:Nu,fused:nc,op:Tn,batchNormalization2d:ga,batchNormalization3d:ya,batchNormalization4d:ba,batchNormalization:wa,batchNorm:xa,batchNorm2d:ka,batchNorm3d:Sa,batchNorm4d:_a,complex:Dn,real:jn,imag:Pn,concat:fr,concat1d:hr,concat2d:dr,concat3d:pr,concat4d:mr,split:vr,conv1d:za,conv2d:La,conv3d:Fa,conv2dDerFilter:Ba,depthwiseConv2d:Ua,separableConv2d:Va,conv2dTranspose:Wa,matMul:qa,dot:Ha,outerProduct:Ga,reverse:Ka,reverse1d:Xa,reverse2d:$a,reverse3d:Ya,reverse4d:Ja,maxPool:ts,avgPool:es,pool:ns,slice:rs,slice1d:is,slice2d:os,slice3d:as,slice4d:ss,abs:Io,acos:To,acosh:Mo,asin:Ro,asinh:Do,atan:jo,atanh:Po,ceil:zo,clipByValue:Lo,cos:Fo,cosh:Bo,erf:Uo,exp:Vo,expm1:Wo,floor:qo,log:Ho,log1p:Go,logSigmoid:Ko,neg:Xo,reciprocal:$o,round:Yo,rsqrt:Jo,sigmoid:Zo,sign:Qo,isNaN:ta,isInf:ea,isFinite:na,sin:ra,sinh:ia,softplus:oa,sqrt:aa,square:sa,step:ua,tan:ca,tanh:la,all:cs,any:ls,argMax:fs,argMin:hs,logSumExp:ds,max:ps,mean:ms,min:vs,moments:gs,sum:ys,prod:bs,equal:ws,equalStrict:xs,greater:ks,greaterEqual:Ss,greaterEqualStrict:_s,greaterStrict:Es,less:Cs,lessEqual:Ns,lessEqualStrict:As,lessStrict:Os,notEqual:Is,notEqualStrict:Ts,add:Ms,addN:Rs,addStrict:Ds,atan2:js,div:Ps,divStrict:zs,floorDiv:Ls,maximum:Fs,maximumStrict:Bs,minimum:Us,minimumStrict:Vs,mod:Ws,modStrict:qs,mul:Hs,mulStrict:Gs,pow:Ks,powStrict:Xs,squaredDifference:$s,squaredDifferenceStrict:Ys,sub:Js,subStrict:Zs,elu:ou,leakyRelu:au,prelu:su,relu:uu,selu:cu,logicalAnd:Qs,logicalNot:tu,logicalOr:eu,logicalXor:nu,where:ru,whereAsync:iu,buffer:Ar,print:Or,batchToSpaceND:Ir,cast:Tr,clone:Mr,cumsum:Rr,depthToSpace:Dr,expandDims:jr,eye:Pr,multinomial:zr,oneHot:Lr,pad:Fr,pad1d:Br,pad2d:Ur,pad3d:Vr,pad4d:Wr,rand:qr,randomNormal:Hr,randomUniform:Gr,reshape:Kr,spaceToBatchND:Xr,squeeze:$r,stack:Yr,tile:Jr,truncatedNormal:Zr,unstack:Qr,setdiff1dAsync:ti,fill:Kn,linspace:Xn,ones:Hn,range:$n,scalar:Ln,tensor:zn,tensor1d:Fn,tensor2d:Bn,tensor3d:Un,tensor4d:Vn,tensor5d:Wn,tensor6d:qn,zeros:Gn,onesLike:Yn,zerosLike:Jn,transpose:lu,softmax:Mn,logSoftmax:Rn,localResponseNormalization:fu,norm:hu,gather:mu,unsortedSegmentSum:vu,basicLSTMCell:gu,multiRNNCell:yu,movingAverage:bu,stridedSlice:wu,topk:xu,scatterND:ku,fft:Su,ifft:_u,rfft:Eu,irfft:Cu,sparseToDense:Au,gatherND:Ou,dropout:Iu,hannWindow:Ru,hammingWindow:Du}),ic=function(){function t(){this.blockSize=48,this.firstUse=!0,f.get("IS_BROWSER")&&(this.fromPixels2DContext=document.createElement("canvas").getContext("2d")),this.data=new Zn(Dt)}return t.prototype.register=function(t,e,n){if(this.firstUse&&(this.firstUse=!1,f.get("IS_NODE")&&Ye("\n============================\nHi there 👋. Looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, which binds to TensorFlow C++, by running npm i @tensorflow/tfjs-node, or npm i @tensorflow/tfjs-node-gpu if you have CUDA. Then call require('@tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of your program. Visit https://github.com/tensorflow/tfjs-node for more details.\n============================\n")),this.data.has(t))throw new Error("Data buffer is already registered");this.data.set(t,{dtype:n})},t.prototype.write=function(t,e){if(null==e)throw new Error("MathBackendCPU.write(): values can not be null");this.data.get(t).values=e},t.prototype.fromPixels=function(t,e){if(null==t)throw new Error("pixels passed to tf.browser.fromPixels() can not be null");var n,r;if(f.get("IS_NODE")&&null==t.getContext)throw new Error("When running in node, pixels must be an HTMLCanvasElement like the one returned by the `canvas` npm package");if(null!=t.getContext)n=t.getContext("2d").getImageData(0,0,t.width,t.height).data;else if(t instanceof ImageData)n=t.data;else{if(!(t instanceof HTMLImageElement||t instanceof HTMLVideoElement))throw new Error("pixels passed to tf.browser.fromPixels() must be either an HTMLVideoElement, HTMLImageElement, HTMLCanvasElement or ImageData, but was "+t.constructor.name);if(null==this.fromPixels2DContext)throw new Error("Can't read pixels from HTMLImageElement outside the browser.");this.fromPixels2DContext.canvas.width=t.width,this.fromPixels2DContext.canvas.height=t.height,this.fromPixels2DContext.drawImage(t,0,0,t.width,t.height),n=this.fromPixels2DContext.getImageData(0,0,t.width,t.height).data}if(4===e)r=new Int32Array(n);else{var i=t.width*t.height;r=new Int32Array(i*e);for(var o=0;oh&&(h=m,d=p)}u[l]=d}return a},t.prototype.cumsum=function(t,e,n,r){if(this.assertNotComplex(t,"cumsum"),e!==t.rank-1)throw new Error("backend.cumsum in CPU expects an inner-most axis="+(t.rank-1)+" but got axis="+e);for(var i=Et(t.dtype,"int32"),o=Gn(t.shape,i),a=o.dataSync(),s=t.dataSync(),u=t.shape[t.rank-1],c=r?function(t,e){return t+u-e-1}:function(t,e){return t+e},l=0;le?1:0})},t.prototype.greaterEqual=function(t,e){return this.assertNotComplex([t,e],"greaterEqual"),this.broadcastedBinaryOp(t,e,"bool",function(t,e){return t>=e?1:0})},t.prototype.logicalNot=function(t){this.assertNotComplex(t,"logicalNot");for(var e=t.dataSync(),n=new Uint8Array(e.length),r=0;r1||1===e.rank?1:e.shape[1],l=0;l=0&&e>=0?n:(n+e)%e})},t.prototype.max=function(t,e){this.assertNotComplex(t,"max"),an("max",e,t.rank);for(var n=rn(t.shape,e),r=n[0],i=n[1],o=Gn(r,t.dtype),a=_(i),s=o.dataSync(),u=t.dataSync(),c=0;cf&&(f=d)}s[c]=f}return o},t.prototype.maximum=function(t,e){return this.assertNotComplex([t,e],"maximum"),this.broadcastedBinaryOp(t,e,t.dtype,function(t,e){return Math.max(t,e)})},t.prototype.all=function(t,e){this.assertNotComplex(t,"all"),an("all",e,t.rank);for(var n=rn(t.shape,e),r=n[0],i=n[1],o=Gn(r,t.dtype),a=_(i),s=o.dataSync(),u=t.dataSync(),c=0;c0?n[r]=1:n[r]=0;return vt.make(t.shape,{values:n})},t.prototype.isNaN=function(t){this.assertNotComplex(t,"x");for(var e=t.dataSync(),n=new Uint8Array(e.length),r=0;r.5?n[r]=Math.ceil(e[r]):n[r]=i%2==0?i:i+1}return vt.make(t.shape,{values:n})},t.prototype.exp=function(t){this.assertNotComplex(t,"exp");for(var e=t.dataSync(),n=new Float32Array(e.length),r=0;r=0?i:Math.exp(i)-1}return vt.make(t.shape,{values:e})},t.prototype.eluDer=function(t,e){this.assertNotComplex([t,e],"eluDer");for(var n=new Float32Array(e.size),r=e.dataSync(),i=t.dataSync(),o=0;o=1?i[o]:i[o]*(a+1)}return vt.make(e.shape,{values:n})},t.prototype.selu=function(t){this.assertNotComplex(t,"selu");for(var e=new Float32Array(t.size),n=t.dataSync(),r=0;r=0?1.0507009873554805*i:1.7580993408473768*(Math.exp(i)-1)}return vt.make(t.shape,{values:e})},t.prototype.clip=function(t,e,n){this.assertNotComplex(t,"clip");for(var r=new Float32Array(t.size),i=t.dataSync(),o=0;on?n:a-e,s=r[i]0?1:e}return vt.make(t.shape,{values:n})},t.prototype.conv2d=function(t,e,n){this.assertNotComplex([t,e],"conv2d");for(var r=n.filterHeight,i=n.filterWidth,o=n.dilationHeight,a=n.dilationWidth,s=n.padInfo.left,u=n.padInfo.top,c=Ar(n.outShape,t.dtype),l=t.dataSync(),f=e.dataSync(),h=c.values,d=0;d=n.inHeight))for(var x=b*e.strides[0],k=p+w*t.strides[1],S=0;S=n.inWidth))for(var A=x+C*e.strides[1],O=k+N*n.inChannels,I=A,T=0;T=n.inDepth))for(var _=k*e.strides[0],E=g+S*t.strides[1],C=0;C=n.inHeight))for(var T=_+O*e.strides[1],M=E+I*t.strides[2],R=0;R=n.inWidth))for(var L=T+P*e.strides[2],F=M+z*n.inChannels,B=L,U=0;U=n.inHeight))for(var k=w*e.strides[0],S=m+x*t.strides[1],_=0;_=n.inWidth))for(var O=k+N*e.strides[1],I=S+A*n.inChannels,T=E,M=O,R=0;RT?T=z:"avg"===n&&(M+=z,R++)}if(isNaN(T))break}p[C+N*g+x]="avg"===n?M/R:T}return d.toTensor()},t.prototype.maxPool=function(t,e){return this.pool(t,e,"max")},t.prototype.maxPoolPositions=function(t,e){for(var n=Ar(e.outShape,"int32"),r=e.strideHeight,i=e.strideWidth,o=e.dilationHeight,a=e.dilationWidth,s=e.effectiveFilterHeight,u=e.effectiveFilterWidth,c=e.padInfo.top,l=e.padInfo.left,f=t.bufferSync(),h=0;hk&&(k=A,S=E*u+N)}n.set(S,h,p,y,d)}}return n.toTensor()},t.prototype.maxPoolBackprop=function(t,e,n,r){this.assertNotComplex([e,n],"maxPoolBackprop");for(var i=this.maxPoolPositions(e,r),o=r.strideHeight,a=r.strideWidth,s=r.dilationHeight,u=r.dilationWidth,c=r.effectiveFilterHeight,l=r.effectiveFilterWidth,f=l-1-r.padInfo.left,h=c-1-r.padInfo.top,d=Ar(e.shape,"float32"),p=i.bufferSync(),m=t.bufferSync(),v=0;v=r.outHeight||Math.floor(_)!==_))for(var E=0;E=r.outWidth||Math.floor(C)!==C)){var N=c*l-1-p.get(v,_,C,g)===S*l+E?1:0;0!==N&&(k+=m.get(v,_,C,g)*N)}}}d.set(k,v,y,b,g)}return d.toTensor()},t.prototype.avgPoolBackprop=function(t,e,n){this.assertNotComplex([t,e],"avgPoolBackprop");for(var r=n.strideHeight,i=n.strideWidth,o=n.filterHeight,a=n.filterWidth,s=n.dilationHeight,u=n.dilationWidth,c=n.effectiveFilterHeight,l=n.effectiveFilterWidth,f=l-1-n.padInfo.left,h=c-1-n.padInfo.top,d=Ar(e.shape,"float32"),p=1/(o*a),m=t.bufferSync(),v=0;v=n.outHeight||Math.floor(_)!==_))for(var E=0;E=n.outWidth||Math.floor(C)!==C||(k+=m.get(v,_,C,g))}}d.set(k*p,v,y,b,g)}return d.toTensor()},t.prototype.cast=function(t,e){return tr(t,e,this)},t.prototype.reshape=function(t,e){return er(t,e)},t.prototype.avgPool=function(t,e){return this.assertNotComplex(t,"avgPool"),this.pool(t,e,"avg").toFloat()},t.prototype.resizeBilinear=function(t,e,n,r){this.assertNotComplex(t,"resizeBilinear");for(var i=t.shape,o=i[0],a=i[1],s=i[2],u=i[3],c=t.dataSync(),l=new Float32Array(_([o,e,n,u])),f=[r&&e>1?a-1:a,r&&n>1?s-1:s],h=[r&&e>1?e-1:e,r&&n>1?n-1:n],d=0,p=f[0]/h[0],m=f[1]/h[1],v=0;v1?o-1:o,n&&l>1?a-1:a],d=[n&&c>1?c-1:c,n&&l>1?l-1:l],p=h[0]/d[0],m=h[1]/d[1],v=t.dataSync(),g=0,y=0;y1?a-1:a,r&&n>1?s-1:s],h=[r&&e>1?e-1:e,r&&n>1?n-1:n],d=f[0]/h[0],p=f[1]/h[1],m=0,v=0;v1?o-1:o,n&&l>1?a-1:a],p=[n&&c>1?c-1:c,n&&l>1?l-1:l],m=d[0]/p[0],v=d[1]/p[1],g=1/m,y=1/v,b=2*Math.ceil(g)+2,w=2*Math.ceil(y)+2,x=0;x=c)){var j=k+D*t.strides[1],P=D*m;if(S===Math.min(o-1,n?Math.round(P):Math.floor(P)))for(var z=0;z=l)){var F=j+L*t.strides[2],B=L*v;N===Math.min(a-1,n?Math.round(B):Math.floor(B))&&(M+=h[F+T])}}}}f[A+T]=M}return Vn(f,e.shape,e.dtype)},t.prototype.batchNormalization=function(t,e,n,r,i,o){this.assertNotComplex([t,e,n,i,o],"batchNorm");for(var a=t.dataSync(),s=e.dataSync(),u=n.dataSync(),c=i?i.dataSync():new Float32Array([1]),l=o?o.dataSync():new Float32Array([0]),f=new Float32Array(a.length),h=l.length,d=c.length,p=u.length,m=s.length,v=0,g=0,y=0,b=0,w=0;w=h&&(v=0),g>=m&&(g=0),y>=d&&(y=0),b>=p&&(b=0);return Vn(f,t.shape)},t.prototype.localResponseNormalization4D=function(t,e,n,r,i){this.assertNotComplex(t,"localResponseNormalization4D");var o=t.shape[3],a=o-1,s=t.dataSync(),u=t.size,c=new Float32Array(u);function l(t){for(var n=t%o,r=t-n+Math.max(0,n-e),i=t-n+Math.min(n+e,a),u=0;r<=i;r++){var c=s[r];u+=c*c}return u}for(var f=0;f=0&&o[a]1,function(){return"blockSize should be > 1 for depthToSpace, but was: "+e});for(var r=t.shape[0],i=t.shape[1],o=t.shape[2],a=t.shape[3],s=i*e,u=o*e,c=a/(e*e),l=t.dataSync(),f=new Float32Array(r*s*u*c),h=0,d=0;d=s))for(var N=h>1?(_-k)*(u-1)/(h-1):0,A=d>1?(E-S)*(c-1)/(d-1):0,O=0;O1?k*(u-1)+O*N:.5*(k+_)*(u-1);if(I<0||I>u-1)for(var T=0;T1?S*(c-1)+T*A:.5*(S+E)*(c-1))<0||H>c-1)for(M=0;M1?S*(c-1)+T*A:.5*(S+E)*(c-1))<0||H>c-1)for(M=0;M=t.size/s)throw new Error("Invalid indices: "+d+" does not index into "+t.shape);for(var g=0;g=r/i)throw new Error("Invalid indices: "+m+" does not index into "+n);for(var b=0;b0,function(){return"scheme must not be an empty string."});var r=t.getInstance();w(null==r.managers[e],function(){return"A model store manager is already registered for scheme '"+e+"'."}),r.managers[e]=n},t.getManager=function(t){var e=this.getInstance().managers[t];if(null==e)throw new Error("Cannot find model manager for scheme '"+t+"'");return e},t.getSchemes=function(){return Object.keys(this.getInstance().managers)},t}();function mc(t){if(-1===t.indexOf(dc))throw new Error("The url string provided does not contain a scheme. Supported schemes are: "+pc.getSchemes().join(","));return{scheme:t.split(dc)[0],path:t.split(dc)[1]}}function vc(t,e,n){return void 0===n&&(n=!1),c(this,void 0,void 0,function(){var r,i,o,a,s,u,c,f,h;return l(this,function(l){switch(l.label){case 0:return w(t!==e,function(){return"Old path and new path are the same: '"+t+"'"}),w((r=hc.getLoadHandlers(t)).length>0,function(){return"Copying failed because no load handler is found for source URL "+t+"."}),w(r.length<2,function(){return"Copying failed because more than one ("+r.length+") load handlers for source URL "+t+"."}),i=r[0],w((o=hc.getSaveHandlers(e)).length>0,function(){return"Copying failed because no save handler is found for destination URL "+e+"."}),w(o.length<2,function(){return"Copying failed because more than one ("+r.length+") save handlers for destination URL "+e+"."}),a=o[0],s=mc(t).scheme,u=mc(t).path,c=s===mc(t).scheme,[4,i.load()];case 1:return f=l.sent(),n&&c?[4,pc.getManager(s).removeModel(u)]:[3,3];case 2:l.sent(),l.label=3;case 3:return[4,a.save(f)];case 4:return h=l.sent(),!n||c?[3,6]:[4,pc.getManager(s).removeModel(u)];case 5:l.sent(),l.label=6;case 6:return[2,h.modelArtifactsInfo]}})})}var gc="models_store",yc="model_info_store";function bc(){if(!f.getBool("IS_BROWSER"))throw new Error("Failed to obtain IndexedDB factory because the current environmentis not a web browser.");var t=window,e=t.indexedDB||t.mozIndexedDB||t.webkitIndexedDB||t.msIndexedDB||t.shimIndexedDB;if(null==e)throw new Error("The current browser does not appear to support IndexedDB.");return e}function wc(t){var e=t.result;e.createObjectStore(gc,{keyPath:"modelPath"}),e.createObjectStore(yc,{keyPath:"modelPath"})}var xc=function(){function t(t){if(this.indexedDB=bc(),null==t||!t)throw new Error("For IndexedDB, modelPath must not be null, undefined or empty.");this.modelPath=t}return t.prototype.save=function(t){return c(this,void 0,void 0,function(){return l(this,function(e){if(t.modelTopology instanceof ArrayBuffer)throw new Error("BrowserLocalStorage.save() does not support saving model topology in binary formats yet.");return[2,this.databaseAction(this.modelPath,t)]})})},t.prototype.load=function(){return c(this,void 0,void 0,function(){return l(this,function(t){return[2,this.databaseAction(this.modelPath)]})})},t.prototype.databaseAction=function(t,e){var n=this;return new Promise(function(t,r){var i=n.indexedDB.open("tensorflowjs",1);i.onupgradeneeded=function(){return wc(i)},i.onsuccess=function(){var o=i.result;if(null==e){var a=o.transaction(gc,"readonly"),s=a.objectStore(gc).get(n.modelPath);s.onsuccess=function(){if(null==s.result)return o.close(),r(new Error("Cannot find model with path '"+n.modelPath+"' in IndexedDB."));t(s.result.modelArtifacts)},s.onerror=function(t){return o.close(),r(s.error)},a.oncomplete=function(){return o.close()}}else{var u,c=fc(e),l=o.transaction(yc,"readwrite"),f=l.objectStore(yc),h=f.put({modelPath:n.modelPath,modelArtifactsInfo:c});h.onsuccess=function(){var i=(u=o.transaction(gc,"readwrite")).objectStore(gc).put({modelPath:n.modelPath,modelArtifacts:e,modelArtifactsInfo:c});i.onsuccess=function(){return t({modelArtifactsInfo:c})},i.onerror=function(t){var e=(f=l.objectStore(yc)).delete(n.modelPath);e.onsuccess=function(){return o.close(),r(i.error)},e.onerror=function(t){return o.close(),r(i.error)}}},h.onerror=function(t){return o.close(),r(h.error)},l.oncomplete=function(){null==u?o.close():u.oncomplete=function(){return o.close()}}}},i.onerror=function(t){return r(i.error)}})},t.URL_SCHEME="indexeddb://",t}(),kc=function(t){return f.getBool("IS_BROWSER")&&!Array.isArray(t)&&t.startsWith(xc.URL_SCHEME)?(e=t.slice(xc.URL_SCHEME.length),new xc(e)):null;var e};hc.registerSaveRouter(kc),hc.registerLoadRouter(kc);var Sc=function(){function t(){this.indexedDB=bc()}return t.prototype.listModels=function(){return c(this,void 0,void 0,function(){var t=this;return l(this,function(e){return[2,new Promise(function(e,n){var r=t.indexedDB.open("tensorflowjs",1);r.onupgradeneeded=function(){return wc(r)},r.onsuccess=function(){var t=r.result,i=t.transaction(yc,"readonly"),o=i.objectStore(yc).getAll();o.onsuccess=function(){for(var t={},n=0,r=o.result;n0,function(){return"promises must be a none empty array"})}(t),function(t,e){w(t>=0&&t<=1,function(){return"Progress fraction must be in range [0, 1], but got startFraction "+t}),w(e>=0&&e<=1,function(){return"Progress fraction must be in range [0, 1], but got endFraction "+e}),w(e>=t,function(){return"startFraction must be no more than endFraction, but got startFraction "+t+" and endFraction "+e})}(n=null==n?0:n,r=null==r?1:r);var i=0;return Promise.all(t.map(function(o){return o.then(function(o){var a=n+ ++i/t.length*(r-n);return e(a),o}),o}))}function Wc(t,e){return c(this,void 0,void 0,function(){var n,r,i,o,a,s,u,c,f;return l(this,function(l){switch(l.label){case 0:return null==e&&(e={}),n=null==e.fetchFunc?it:e.fetchFunc,r=t.map(function(t){return n(t,e.requestInit)}),i=0,o=.5,null!=e.onProgress?[3,2]:[4,Promise.all(r)];case 1:return a=l.sent(),[3,4];case 2:return[4,Vc(r,e.onProgress,i,o)];case 3:a=l.sent(),l.label=4;case 4:return s=a.map(function(t){return t.arrayBuffer()}),u=.5,c=1,null!=e.onProgress?[3,6]:[4,Promise.all(s)];case 5:return f=l.sent(),[3,8];case 6:return[4,Vc(s,e.onProgress,u,c)];case 7:f=l.sent(),l.label=8;case 8:return[2,f]}})})}function qc(t){var e=this;return function(n,r,i){return void 0===r&&(r=""),c(e,void 0,void 0,function(){var e,o,a,s,u,c,f,h,d,p;return l(this,function(l){switch(l.label){case 0:if(e=n.map(function(){return!1}),o={},a=null!=i?i.map(function(){return!1}):[],s=[],n.forEach(function(t,n){var r=0;t.weights.forEach(function(t){var u="quantization"in t?t.quantization.dtype:t.dtype,c=oc[u]*_(t.shape),l=function(){e[n]=!0,null==o[n]&&(o[n]=[]),o[n].push({manifestEntry:t,groupOffset:r,sizeBytes:c})};null!=i?i.forEach(function(e,n){e===t.name&&(l(),a[n]=!0)}):l(),s.push(t.name),r+=c})}),!a.every(function(t){return t}))throw u=i.filter(function(t,e){return!a[e]}),new Error("Could not find weights in manifest with names: "+u.join(", ")+". \nManifest JSON has weights with names: "+s.join(", ")+".");return c=e.reduce(function(t,e,n){return e&&t.push(n),t},[]),f=[],c.forEach(function(t){n[t].paths.forEach(function(t){var e=r+(r.endsWith("/")?"":"/")+t;f.push(e)})}),[4,t(f)];case 1:return h=l.sent(),d={},p=0,c.forEach(function(t){for(var e=n[t].paths.length,r=0,i=0;i0,function(){return"URL path for http must not be null, undefined or empty."}),Array.isArray(t)&&w(2===t.length,function(){return"URL paths for http must have a length of 2, (actual length is "+t.length+")."}),this.path=t,null!=e.requestInit&&null!=e.requestInit.body)throw new Error("requestInit is expected to have no pre-existing body, but has one.");this.requestInit=e.requestInit||{}}return t.prototype.save=function(t){return c(this,void 0,void 0,function(){var e,n,r,i;return l(this,function(o){switch(o.label){case 0:if(t.modelTopology instanceof ArrayBuffer)throw new Error("BrowserHTTPRequest.save() does not support saving model topology in binary formats yet.");return(e=Object.assign({method:this.DEFAULT_METHOD},this.requestInit)).body=new FormData,n=[{paths:["./model.weights.bin"],weights:t.weightSpecs}],r={modelTopology:t.modelTopology,format:t.format,generatedBy:t.generatedBy,convertedBy:t.convertedBy,weightsManifest:n},e.body.append("model.json",new Blob([JSON.stringify(r)],{type:"application/json"}),"model.json"),null!=t.weightData&&e.body.append("model.weights.bin",new Blob([t.weightData],{type:"application/octet-stream"}),"model.weights.bin"),[4,this.fetch(this.path,e)];case 1:if((i=o.sent()).ok)return[2,{modelArtifactsInfo:fc(t),responses:[i]}];throw new Error("BrowserHTTPRequest.save() failed due to HTTP response status "+i.status+".")}})})},t.prototype.load=function(){return c(this,void 0,void 0,function(){var t,e,n,r,i,o,a,s;return l(this,function(u){switch(u.label){case 0:return[4,this.fetch(this.path,this.requestInit)];case 1:if(!(t=u.sent()).ok)throw new Error("Request to "+this.path+" failed with status code "+t.status+". Please verify this URL points to the model JSON of the model to load.");u.label=2;case 2:return u.trys.push([2,4,,5]),[4,t.json()];case 3:return e=u.sent(),[3,5];case 4:throw u.sent(),n="Failed to parse model JSON of response from "+this.path+".",this.path.endsWith(".pb")?n+=" Your path contains a .pb file extension. Support for .pb models have been removed in TensorFlow.js 1.0 in favor of .json models. You can re-convert your Python TensorFlow model using the TensorFlow.js 1.0 conversion scripts or you can convert your.pb models with the 'pb2json'NPM script in the tensorflow/tfjs-converter repository.":n+=" Please make sure the server is serving valid JSON for this request.",new Error(n);case 5:if(r=e.modelTopology,i=e.weightsManifest,null==r&&null==i)throw new Error("The JSON from HTTP path "+this.path+" contains neither model topology or manifest for weights.");return null==i?[3,7]:[4,this.loadWeights(i)];case 6:s=u.sent(),o=s[0],a=s[1],u.label=7;case 7:return[2,{modelTopology:r,weightSpecs:o,weightData:a}]}})})},t.prototype.loadWeights=function(t){return c(this,void 0,void 0,function(){var e,n,r,i,o,a,s,u,c,f,h;return l(this,function(l){switch(l.label){case 0:for(e=Array.isArray(this.path)?this.path[1]:this.path,n=function(t){var e=t.lastIndexOf("/"),n=t.lastIndexOf("?");return[t.substring(0,e)+"/",n>e?t.substring(n):""]}(e),r=n[0],i=n[1],o=this.weightPathPrefix||r,a=[],s=0,u=t;s0&&(t=u({weightSpecs:this.weightSpecs},t)),null!=this.weightData&&this.weightData.byteLength>0&&(t=u({weightData:this.weightData},t)),[2,t]})})},t}(),Yc=function(){function t(t){this.saveHandler=t}return t.prototype.save=function(t){return c(this,void 0,void 0,function(){return l(this,function(e){return[2,this.saveHandler(t)]})})},t}(),Jc=Object.freeze({browserFiles:function(t){return new Uc(t)},browserHTTPRequest:function(t,e){return Xc(t,e)},concatenateArrayBuffers:cc,decodeWeights:ac,encodeWeights:function(t){return c(this,void 0,void 0,function(){var e,n,r,i;return l(this,function(o){switch(o.label){case 0:for(r in e=[],n=[],t){if("float32"!==(i=t[r]).dtype&&"int32"!==i.dtype&&"bool"!==i.dtype)throw new Error("Unsupported dtype in weight '"+r+"': "+i.dtype);e.push({name:r,shape:i.shape,dtype:i.dtype}),n.push(i.data())}return[4,Promise.all(n)];case 1:return[2,{data:function(t){if(null===t)throw new Error("Invalid input value: "+JSON.stringify(t));var e=0,n=[];t.forEach(function(t){if(e+=t.byteLength,n.push(t.byteLength===t.buffer.byteLength?t:new t.constructor(t)),!(t instanceof Float32Array||t instanceof Int32Array||t instanceof Uint8Array))throw new Error("Unsupported TypedArray subtype: "+t.constructor.name)});var r=new Uint8Array(e),i=0;return n.forEach(function(t){r.set(new Uint8Array(t.buffer),i),i+=t.byteLength}),r.buffer}(o.sent()),specs:e}]}})})},fromMemory:function(t,e,n){return new $c(t,e,n)},getLoadHandlers:function(t,e){return hc.getLoadHandlers(t)},getModelArtifactsInfoForJSON:fc,getSaveHandlers:function(t){return hc.getSaveHandlers(t)},http:Xc,isHTTPScheme:Gc,loadWeights:function(t,e,n,r){return void 0===e&&(e=""),c(this,void 0,void 0,function(){return l(this,function(i){return[2,qc(function(t){return Wc(t,{requestInit:r})})(t,e,n)]})})},registerLoadRouter:function(t){return hc.registerLoadRouter(t)},registerSaveRouter:function(t){return hc.registerSaveRouter(t)},weightsLoaderFactory:qc,withSaveHandler:function(t){return new Yc(t)},copyModel:function(t,e){return c(this,void 0,void 0,function(){return l(this,function(n){return[2,vc(t,e,!1)]})})},listModels:function(){return c(this,void 0,void 0,function(){var t,e,n,r,i,o,a;return l(this,function(s){switch(s.label){case 0:t=pc.getSchemes(),e={},n=0,r=t,s.label=1;case 1:return n0&&Number.isInteger(n),function(){return"If provided, numClasses must be a positive integer, but got "+n}),w(1===r.rank,function(){return"Expected the rank of labels to be 1, but got "+r.rank}),w(1===i.rank,function(){return"Expected the rank of predictions to be 1, but got "+i.rank}),w(r.shape[0]===i.shape[0],function(){return"Mismatch in the number of examples: "+r.shape[0]+" vs. "+i.shape[0]+". Labels and predictions should have the same number of elements."}),w(n>0&&Number.isInteger(n),function(){return"numClasses is required to be a positive integer, but got "+n});var o=Lr(r.asType("int32"),n),a=Lr(i.asType("int32"),n);return o.transpose().matMul(a).asType("int32")}}),Qc=Object.freeze({confusionMatrix:Zc}),tl=Tn({fromPixels_:function(t,e){if(void 0===e&&(e=3),e>4)throw new Error("Cannot construct Tensor with more than 4 channels from pixels.");return Dt.fromPixels(t,e)}}),el=Object.freeze({toPixels:function(t,e){return c(this,void 0,void 0,function(){var n,r,i,o,a,s,u,c,f,h,d,p,m,v,g,y,b,w,x,k,S,_,E;return l(this,function(l){switch(l.label){case 0:if(n=kn(t,"img","toPixels"),t instanceof vt||(n=n.toInt()),2!==n.rank&&3!==n.rank)throw new Error("toPixels only supports rank 2 or 3 tensors, got rank "+n.rank+".");if(r=n.shape.slice(0,2),i=r[0],o=r[1],(a=2===n.rank?1:n.shape[2])>4||2===a)throw new Error("toPixels only supports depth of size 1, 3 or 4 but got "+a);return[4,n.data()];case 1:return s=l.sent(),u=n.min(),c=n.max(),[4,Promise.all([u.data(),c.data()])];case 2:if(f=l.sent(),h=f[0],d=f[1],p=h[0],m=d[0],u.dispose(),c.dispose(),"float32"===n.dtype){if(p<0||m>1)throw new Error("Tensor values for a float32 Tensor must be in the range [0 - 1] but got range ["+p+" - "+m+"].")}else{if("int32"!==n.dtype)throw new Error("Unsupported type for toPixels: "+n.dtype+". Please use float32 or int32 tensors.");if(p<0||m>255)throw new Error("Tensor values for a int32 Tensor must be in the range [0 - 255] but got range ["+p+" - "+m+"].")}for(v="float32"===n.dtype?255:1,g=new Uint8ClampedArray(o*i*4),y=0;y0,function(){return"Class being registered has an empty-string as its className, which is disallowed."}),rl.register(t)}var ol=Object.freeze({Serializable:nl,SerializationMap:rl,registerClass:il}),al=.001,sl=.1;function ul(){return 32===Dt.backend.floatPrecision()?al:sl}function cl(t,e,n){var r=!0;if((F(t)||F(e))&&(r=!1),F(t)&&F(e)&&(r=!0),r){var i=t.constructor.name,o=e.constructor.name;if(i!==o)throw new Error("Arrays are of different type. Actual: "+i+". Expected: "+o)}if(Array.isArray(t)&&Array.isArray(e)){var a=wn(t),s=wn(e);if(!E(a,s))throw new Error("Arrays have different shapes. Actual: ["+a+"]. Expected: ["+s+"]")}var u=F(t)?t:S(t),c=F(e)?e:S(e);if(u.length!==c.length)throw new Error("Arrays have different lengths actual: "+u.length+" vs expected: "+c.length+".\nActual: "+u+".\nExpected: "+c+".");for(var l=0;ln)}var fl=Object.freeze({TEST_EPSILON_FLOAT16:sl,expectArraysClose:function(t,e,n){return null==n&&(n=ul()),cl(t,e,function(t,e){return ll(t,e,n)})},testEpsilon:ul,expectPromiseToFail:function(t,e){t().then(function(){return e.fail()},function(){return e()})},expectArraysEqual:function(t,e){var n="string"==typeof e||"number"==typeof e||"boolean"==typeof e?[e]:e;return V(t)||V(t[0])||V(e)||V(e[0])?cl(t,n,function(t,e){return t==e}):cl(t,e,function(t,e){return ll(t,e,0)})},expectNumbersClose:function(t,e,n){if(null==n&&(n=ul()),!ll(t,e,n))throw new Error("Numbers differ: actual === "+t+", expected === "+e)},expectValuesInRange:function(t,e,n){for(var r=0;rn)throw new Error("Value out of range:"+t[r]+" low: "+e+", high: "+n)},expectArrayBuffersEqual:function(t,e){expect(new Float32Array(t)).toEqual(new Float32Array(e))}}),hl="1.1.2",dl=Object.freeze({gpgpu_util:ao,webgl_util:Ie,MathBackendWebGL:Ao,GPGPUContext:so}),pl=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return s(e,t),e.prototype.minimize=function(t,e,n){void 0===e&&(e=!1);var r=this.computeGradients(t,n),i=r.value,o=r.grads;return this.applyGradients(o),Object.keys(o).forEach(function(t){return o[t].dispose()}),e?i:(i.dispose(),null)},e.prototype.computeGradients=function(t,e){return An(t,e)},e.prototype.dispose=function(){},e}(nl);Object.defineProperty(pl,Symbol.hasInstance,{value:function(t){return null!=t.minimize&&null!=t.computeGradients&&null!=t.applyGradients}});var ml=function(t){function e(e,n,r){void 0===r&&(r=null);var i=t.call(this)||this;return i.learningRate=e,i.rho=n,i.epsilon=r,i.accumulatedGrads={},i.accumulatedUpdates={},null==r&&(i.epsilon=Dt.backend.epsilon()),i}return s(e,t),e.prototype.applyGradients=function(t){var e=this,n=function(n){var i=Dt.registeredVariables[n];null==r.accumulatedGrads[n]&&Le(function(){e.accumulatedGrads[n]=Jn(i).variable(!1)}),null==r.accumulatedUpdates[n]&&Le(function(){e.accumulatedUpdates[n]=Jn(i).variable(!1)});var o=t[n],a=r.accumulatedGrads[n],s=r.accumulatedUpdates[n];Le(function(){var t=a.mul(e.rho).add(o.square().mul(1-e.rho)),r=s.add(e.epsilon).sqrt().div(a.add(e.epsilon).sqrt()).mul(o),u=s.mul(e.rho).add(r.square().mul(1-e.rho));e.accumulatedGrads[n].assign(t),e.accumulatedUpdates[n].assign(u);var c=r.mul(-e.learningRate).add(i);i.assign(c)})},r=this;for(var i in t)n(i)},e.prototype.dispose=function(){var t=this;null!=this.accumulatedUpdates&&(Object.keys(this.accumulatedUpdates).forEach(function(e){return t.accumulatedUpdates[e].dispose()}),Object.keys(this.accumulatedGrads).forEach(function(e){return t.accumulatedGrads[e].dispose()}))},e.prototype.getConfig=function(){return{learningRate:this.learningRate,rho:this.rho,epsilon:this.epsilon}},e.fromConfig=function(t,e){return new t(e.learningRate,e.rho,e.epsilon)},e.className="AdadeltaOptimizer",e}(pl);il(ml);var vl=function(t){function e(e,n){void 0===n&&(n=.1);var r=t.call(this)||this;return r.learningRate=e,r.initialAccumulatorValue=n,r.accumulatedGrads={},r}return s(e,t),e.prototype.applyGradients=function(t){var e=this,n=function(n){var i=Dt.registeredVariables[n];null==r.accumulatedGrads[n]&&Le(function(){e.accumulatedGrads[n]=Kn(i.shape,e.initialAccumulatorValue).variable(!1)});var o=t[n],a=r.accumulatedGrads[n];Le(function(){var t=a.add(o.square());e.accumulatedGrads[n].assign(t);var r=o.div(t.add(Dt.backend.epsilon()).sqrt()).mul(-e.learningRate).add(i);i.assign(r)})},r=this;for(var i in t)n(i)},e.prototype.dispose=function(){var t=this;null!=this.accumulatedGrads&&Object.keys(this.accumulatedGrads).forEach(function(e){return t.accumulatedGrads[e].dispose()})},e.prototype.getConfig=function(){return{learningRate:this.learningRate,initialAccumulatorValue:this.initialAccumulatorValue}},e.fromConfig=function(t,e){return new t(e.learningRate,e.initialAccumulatorValue)},e.className="AdagradOptimizer",e}(pl);il(vl);var gl=function(t){function e(e,n,r,i){void 0===i&&(i=null);var o=t.call(this)||this;return o.learningRate=e,o.beta1=n,o.beta2=r,o.epsilon=i,o.accumulatedFirstMoment={},o.accumulatedSecondMoment={},Le(function(){o.accBeta1=Ln(n).variable(),o.accBeta2=Ln(r).variable()}),null==i&&(o.epsilon=Dt.backend.epsilon()),o}return s(e,t),e.prototype.applyGradients=function(t){var e=this;Le(function(){var n=Js(1,e.accBeta1),r=Js(1,e.accBeta2);for(var i in t){var o=Dt.registeredVariables[i];if(null==e.accumulatedFirstMoment[i]){var a=!1;e.accumulatedFirstMoment[i]=Jn(o).variable(a)}null==e.accumulatedSecondMoment[i]&&(a=!1,e.accumulatedSecondMoment[i]=Jn(o).variable(a));var s=t[i],u=e.accumulatedFirstMoment[i],c=e.accumulatedSecondMoment[i],l=u.mul(e.beta1).add(s.mul(1-e.beta1)),f=c.mul(e.beta2).add(s.square().mul(1-e.beta2)),h=l.div(n),d=f.div(r);e.accumulatedFirstMoment[i].assign(l),e.accumulatedSecondMoment[i].assign(f);var p=h.div(d.sqrt().add(e.epsilon)).mul(-e.learningRate).add(o);o.assign(p)}e.accBeta1.assign(e.accBeta1.mul(e.beta1)),e.accBeta2.assign(e.accBeta2.mul(e.beta2))})},e.prototype.dispose=function(){var t=this;this.accBeta1.dispose(),this.accBeta2.dispose(),null!=this.accumulatedFirstMoment&&Object.keys(this.accumulatedFirstMoment).forEach(function(e){return t.accumulatedFirstMoment[e].dispose()}),null!=this.accumulatedSecondMoment&&Object.keys(this.accumulatedSecondMoment).forEach(function(e){return t.accumulatedSecondMoment[e].dispose()})},e.prototype.getConfig=function(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon}},e.fromConfig=function(t,e){return new t(e.learningRate,e.beta1,e.beta2,e.epsilon)},e.className="AdamOptimizer",e}(pl);il(gl);var yl=function(t){function e(e,n,r,i,o){void 0===i&&(i=null),void 0===o&&(o=0);var a=t.call(this)||this;return a.learningRate=e,a.beta1=n,a.beta2=r,a.epsilon=i,a.decay=o,a.accumulatedFirstMoment={},a.accumulatedWeightedInfNorm={},Le(function(){a.iteration=Ln(0).variable(),a.accBeta1=Ln(n).variable()}),null==i&&(a.epsilon=Dt.backend.epsilon()),a}return s(e,t),e.prototype.applyGradients=function(t){var e=this;Le(function(){var n=Js(1,e.accBeta1),r=Ps(-e.learningRate,e.iteration.mul(e.decay).add(1));for(var i in t){var o=Dt.registeredVariables[i];if(null==e.accumulatedFirstMoment[i]){var a=!1;e.accumulatedFirstMoment[i]=Jn(o).variable(a)}null==e.accumulatedWeightedInfNorm[i]&&(a=!1,e.accumulatedWeightedInfNorm[i]=Jn(o).variable(a));var s=t[i],u=e.accumulatedFirstMoment[i],c=e.accumulatedWeightedInfNorm[i],l=u.mul(e.beta1).add(s.mul(1-e.beta1)),f=c.mul(e.beta2),h=s.abs(),d=f.maximum(h);e.accumulatedFirstMoment[i].assign(l),e.accumulatedWeightedInfNorm[i].assign(d);var p=r.div(n).mul(l.div(d.add(e.epsilon))).add(o);o.assign(p)}e.iteration.assign(e.iteration.add(1)),e.accBeta1.assign(e.accBeta1.mul(e.beta1))})},e.prototype.dispose=function(){var t=this;this.accBeta1.dispose(),this.iteration.dispose(),null!=this.accumulatedFirstMoment&&Object.keys(this.accumulatedFirstMoment).forEach(function(e){return t.accumulatedFirstMoment[e].dispose()}),null!=this.accumulatedWeightedInfNorm&&Object.keys(this.accumulatedWeightedInfNorm).forEach(function(e){return t.accumulatedWeightedInfNorm[e].dispose()})},e.prototype.getConfig=function(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon,decay:this.decay}},e.fromConfig=function(t,e){return new t(e.learningRate,e.beta1,e.beta2,e.epsilon,e.decay)},e.className="AdamaxOptimizer",e}(pl);il(yl);var bl=function(t){function e(e){var n=t.call(this)||this;return n.learningRate=e,n.setLearningRate(e),n}return s(e,t),e.prototype.applyGradients=function(t){var e=this;Object.keys(t).forEach(function(n){var r=t[n],i=Dt.registeredVariables[n];Le(function(){var t=e.c.mul(r).add(i);i.assign(t)})})},e.prototype.setLearningRate=function(t){this.learningRate=t,null!=this.c&&this.c.dispose(),this.c=Be(Ln(-t))},e.prototype.dispose=function(){this.c.dispose()},e.prototype.getConfig=function(){return{learningRate:this.learningRate}},e.fromConfig=function(t,e){return new t(e.learningRate)},e.className="SGDOptimizer",e}(pl);il(bl);var wl=function(t){function e(e,n,r){void 0===r&&(r=!1);var i=t.call(this,e)||this;return i.learningRate=e,i.momentum=n,i.useNesterov=r,i.m=Ln(i.momentum),i.accumulations={},i}return s(e,t),e.prototype.applyGradients=function(t){var e=this,n=function(n){var i=Dt.registeredVariables[n];null==r.accumulations[n]&&Le(function(){e.accumulations[n]=Jn(i).variable(!1)});var o=r.accumulations[n],a=t[n];Le(function(){var t,r=e.m.mul(o).add(a);t=e.useNesterov?e.c.mul(a.add(r.mul(e.m))).add(i):e.c.mul(r).add(i),e.accumulations[n].assign(r),i.assign(t)})},r=this;for(var i in t)n(i)},e.prototype.dispose=function(){if(t.prototype.dispose.call(this),this.m.dispose(),null!=this.accumulations)for(var e in this.accumulations)this.accumulations[e].dispose()},e.prototype.setMomentum=function(t){this.momentum=t},e.prototype.getConfig=function(){return{learningRate:this.learningRate,momentum:this.momentum,useNesterov:this.useNesterov}},e.fromConfig=function(t,e){return new t(e.learningRate,e.momentum,e.useNesterov)},e.className="MomentumOptimizer",e}(bl);il(wl);var xl=function(t){function e(e,n,r,i,o){void 0===n&&(n=.9),void 0===r&&(r=0),void 0===i&&(i=null),void 0===o&&(o=!1);var a=t.call(this)||this;return a.learningRate=e,a.decay=n,a.momentum=r,a.epsilon=i,a.accumulatedMeanSquares={},a.accumulatedMeanGrads={},a.accumulatedMoments={},a.centered=o,null==i&&(a.epsilon=Dt.backend.epsilon()),a}return s(e,t),e.prototype.applyGradients=function(t){var e=this,n=function(n){var i=Dt.registeredVariables[n];null==r.accumulatedMeanSquares[n]&&Le(function(){e.accumulatedMeanSquares[n]=Jn(i).variable(!1)}),null==r.accumulatedMeanGrads[n]&&r.centered&&Le(function(){e.accumulatedMeanGrads[n]=Jn(i).variable(!1)}),null==r.accumulatedMoments[n]&&Le(function(){e.accumulatedMoments[n]=Jn(i).variable(!1)});var o=r.accumulatedMeanSquares[n],a=r.accumulatedMeanGrads[n],s=r.accumulatedMoments[n],u=t[n];Le(function(){var t=o.mul(e.decay).add(u.square().mul(1-e.decay));if(e.centered){var r=a.mul(e.decay).add(u.mul(1-e.decay)),c=s.mul(e.momentum).add(u.mul(e.learningRate).div(t.sub(r.square().add(e.epsilon)).sqrt()));e.accumulatedMeanSquares[n].assign(t),e.accumulatedMeanGrads[n].assign(r),e.accumulatedMoments[n].assign(c);var l=i.sub(c);i.assign(l)}else{var f=o.mul(e.decay).add(u.square().mul(1-e.decay));c=s.mul(e.momentum).add(u.mul(e.learningRate).div(f.add(e.epsilon).sqrt())),e.accumulatedMeanSquares[n].assign(f),e.accumulatedMoments[n].assign(c),l=i.sub(c),i.assign(l)}})},r=this;for(var i in t)n(i)},e.prototype.dispose=function(){var t=this;null!=this.accumulatedMeanSquares&&Object.keys(this.accumulatedMeanSquares).forEach(function(e){return t.accumulatedMeanSquares[e].dispose()}),null!=this.accumulatedMeanGrads&&this.centered&&Object.keys(this.accumulatedMeanGrads).forEach(function(e){return t.accumulatedMeanGrads[e].dispose()}),null!=this.accumulatedMoments&&Object.keys(this.accumulatedMoments).forEach(function(e){return t.accumulatedMoments[e].dispose()})},e.prototype.getConfig=function(){return{learningRate:this.learningRate,decay:this.decay,momentum:this.momentum,epsilon:this.epsilon,centered:this.centered}},e.fromConfig=function(t,e){return new t(e.learningRate,e.decay,e.momentum,e.epsilon,e.centered)},e.className="RMSPropOptimizer",e}(pl);il(xl);var kl=function(){function t(){}return t.sgd=function(t){return new bl(t)},t.momentum=function(t,e,n){return void 0===n&&(n=!1),new wl(t,e,n)},t.rmsprop=function(t,e,n,r,i){return void 0===e&&(e=.9),void 0===n&&(n=0),void 0===r&&(r=null),void 0===i&&(i=!1),new xl(t,e,n,r,i)},t.adam=function(t,e,n,r){return void 0===t&&(t=.001),void 0===e&&(e=.9),void 0===n&&(n=.999),void 0===r&&(r=null),new gl(t,e,n,r)},t.adadelta=function(t,e,n){return void 0===t&&(t=.001),void 0===e&&(e=.95),void 0===n&&(n=null),new ml(t,e,n)},t.adamax=function(t,e,n,r,i){return void 0===t&&(t=.002),void 0===e&&(e=.9),void 0===n&&(n=.999),void 0===r&&(r=null),void 0===i&&(i=0),new yl(t,e,n,r,i)},t.adagrad=function(t,e){return void 0===e&&(e=.1),new vl(t,e)},t}(),Sl={sgd:kl.sgd,momentum:kl.momentum,adadelta:kl.adadelta,adagrad:kl.adagrad,rmsprop:kl.rmsprop,adamax:kl.adamax,adam:kl.adam},_l="undefined"!=typeof requestAnimationFrame?requestAnimationFrame:void 0!==o?o:function(t){return t()};function El(){return new Promise(function(t){return _l(function(){return t()})})}pt=rc}.call(this,n(45),n(30),n(14).Buffer,n(253).setImmediate)},function(t,e,n){var r=n(6),i=n(43),o=n(29),a=n(28),s=n(42),u=function(t,e,n){var c,l,f,h,d=t&u.F,p=t&u.G,m=t&u.S,v=t&u.P,g=t&u.B,y=p?r:m?r[e]||(r[e]={}):(r[e]||{}).prototype,b=p?i:i[e]||(i[e]={}),w=b.prototype||(b.prototype={});for(c in p&&(n=e),n)f=((l=!d&&y&&void 0!==y[c])?y:n)[c],h=g&&l?s(f,r):v&&"function"==typeof f?s(Function.call,f):f,y&&a(y,c,f,t&u.U),b[c]!=f&&o(b,c,h),v&&w[c]!=f&&(w[c]=f)};r.core=i,u.F=1,u.G=2,u.S=4,u.P=8,u.B=16,u.W=32,u.U=64,u.R=128,t.exports=u},function(t,e,n){"use strict";n.r(e);var r=n(0),i=function(t,e){return(i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)}; /** * @license - * Copyright 2018 Google LLC. All Rights Reserved. + * Copyright 2019 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 @@ -31,50 +31,17 @@ var o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__p * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= - */ -var o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])};function s(e,t){function n(){this.constructor=e}o(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var u=Object.assign||function(e){for(var t,n=1,r=arguments.length;n0&&a[a.length-1])&&(6===i[0]||2===i[0])){o=0;continue}if(3===i[0]&&(!a||i[1]>a[0]&&i[1]0;)r=Math.random()*t|0,n=e[--t],e[t]=e[r],e[r]=n}function p(e,t,n){return Math.max(e,Math.min(t,n))}function h(e,t){var n=Math.random();return t*n+(1-n)*e}function d(e,t){if(!e)throw new Error("string"==typeof t?t:t())}function m(e,t,n){void 0===n&&(n=""),d(w(e,t),n+" Shapes "+e+" and "+t+" must match")}function g(e){d(null!=e,"The input to the tensor constructor must be a non-null value.")}function y(e,t){if(void 0===t&&(t=[]),Array.isArray(e))for(var n=0;n0,function(){return"Element arr["+r.join("][")+"] should be a primitive, but is an array of "+t.length+" elements"}),d(t.length===n[0],function(){return"Element arr["+r.join("][")+"] should have "+n[0]+" elements, but has "+t.length+" elements"});for(var a=n.slice(1),i=0;i1;--t)if(e%t==0)return[t,e/t];return[1,e]}function E(e,t){return t<=e.length?e:e+" ".repeat(t-e.length)}function N(e,t,n){return void 0===t&&(t=function(e){return 0}),new Promise(function(r,a){var i=0,o=function(){if(e())r();else{var s=t(++i);null!=n&&i>=n?a():setTimeout(o,s)}};o()})}function O(e,t){for(var n=1,r=-1,a=0;a=0)n*=e[a];else if(-1===e[a]){if(-1!==r)throw Error("Shapes can only have 1 implicit size. Found -1 at dim "+r+" and dim "+a);r=a}else if(e[a]<0)throw Error("Shapes can not be < 0. Found "+e[a]+" at dim "+a);if(-1===r){if(t>0&&t!==n)throw Error("Size("+t+") must match the product of shape "+e);return e}if(0===n)throw Error("Cannot infer the missing size in ["+e+"] when there are 0 elements");if(t%n!=0)throw Error("The implicit shape can't be a fractional number. Got "+t+" / "+n);var i=e.slice();return i[r]=t/n,i}function k(e,t){for(var n=[],r=[],a=0,i=0;ii)&&1===e[i]&&(n.push(e[i]),r.push(i)),t[a]<=i&&a++}1!==e[i]&&(n.push(e[i]),r.push(i))}return{newShape:n,keptDims:r}}function I(e,t){var n=null;if(null==e||"float32"===e)n=new Float32Array(t);else if("int32"===e)n=new Int32Array(t);else{if("bool"!==e)throw new Error("Unknown data type "+e);n=new Uint8Array(t)}return n}function C(e,t,n){if("float32"===t)for(var r=0;r=0;--r)n[r]=n[r+1]*e[r+1];return n}function L(e,t,n){return function(e,t){return e instanceof Float32Array&&"float32"===t||e instanceof Int32Array&&"int32"===t||e instanceof Uint8Array&&"bool"===t}(e,t)?e:(Array.isArray(e)&&(e=y(e)),function(e,t,n){if(null==t||"float32"===t||"complex64"===t)return new Float32Array(e);if("int32"===t)return n&&A(e,t),new Int32Array(e);if("bool"===t){for(var r=new Uint8Array(e.length),a=0;a1)for(var l=0;lq){var c=$*s,f=Array.from(t.subarray(0,c)),p=Array.from(t.subarray(u-$*s,u));return"complex64"===r&&(f=K(f),p=K(p)),["["+f.map(function(e,t){return H(e,i[t])}).join(", ")+", ..., "+p.map(function(e,t){return H(e,i[u-$+t])}).join(", ")+"]"]}return["["+("complex64"===r?K(t):Array.from(t)).map(function(e,t){return H(e,i[t])}).join(", ")+"]"]}var h=n.slice(1),d=a.slice(1),m=a[0]*s,g=[];if(u>q){for(var y=0;y<$;y++){var v=(b=y*m)+m;g.push.apply(g,e(t.subarray(b,v),h,r,d,i,!1))}for(g.push("..."),y=u-$;y {...}) to avoid memory leaks.");return this.keepTensors.add(e.id),e},e.prototype.startScope=function(e,t){void 0===t&&(t=!1),t&&0===this.gradientScopeCount&&(this.activeTape=[]),t&&this.gradientScopeCount++;var n={track:[],name:"unnamed scope"};e&&(n.name=e),this.scopeStack.push(n),this.activeScope=n},e.prototype.endScope=function(e,t){var n=this;void 0===t&&(t=!1),t&&(this.gradientScopeCount--,0===this.gradientScopeCount&&(this.activeTape=null));var r=new Set(this.keepTensors),a=re(e);a.forEach(function(e){return r.add(e.id)});for(var i=0;i0,"gradients() received an empty list of xs."),this.tidy("gradients",function(){var i=e();d(i instanceof Q,"The result y returned by f() must be a tensor.");var o=function(e,t,n){for(var r={},a={},i=0;i=0;i--)for(o=(d=e[i]).inputs,c=0;c0)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that the f you passed encloses all operations that lead from x to y.");var s={};return s[i.id]=null==n?function(e){var t=z(b(e),"float32");return Q.make(e,{values:t})}(i.shape):n,function(e,t){for(var n=function(n){var r=t[n],a=[];if(r.outputs.forEach(function(t){var n=e[t.id];if(null!=n)a.push(n);else{var r=Q.make(t.shape,{values:B(t.size,t.dtype)},t.dtype);a.push(r)}}),null==r.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+r.name+".");var i=r.gradient(1===r.outputs.length?a[0]:a);for(var o in r.inputs){if(!(o in i))throw new Error("Cannot backprop through input "+o+". Available gradients found: "+Object.keys(i)+".");var s=i[o](),u=r.inputs[o];if(!w(s.shape,u.shape))throw new Error("Error in gradient for op "+r.name+". The gradient of input '"+o+"' has shape '"+s.shape+"', which does not match the shape of the input '"+u.shape+"'");if(null==e[u.id])e[u.id]=s;else{var l=e[u.id];e[u.id]=l.add(s),l.dispose()}}},r=t.length-1;r>=0;r--)n(r)}(s,o),{value:i,grads:t.map(function(e){return s[e.id]})}},!0)},e.prototype.customGrad=function(e){var t=this;return d(R(e),"The f passed in customGrad(f) must be a function."),function(){for(var n,r,a=[],i=0;i {op();...}); to avoid memory leaks.");return this.activeScope.track.push(e),e},e}();!function(e){e[e.NUMBER=0]="NUMBER",e[e.BOOLEAN=1]="BOOLEAN",e[e.STRING=2]="STRING"}(ae||(ae={}));var oe=[{name:"DEBUG",type:ae.BOOLEAN},{name:"IS_BROWSER",type:ae.BOOLEAN},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION",type:ae.NUMBER},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE",type:ae.BOOLEAN},{name:"WEBGL_VERSION",type:ae.NUMBER},{name:"WEBGL_RENDER_FLOAT32_ENABLED",type:ae.BOOLEAN},{name:"WEBGL_DOWNLOAD_FLOAT_ENABLED",type:ae.BOOLEAN},{name:"WEBGL_FENCE_API_ENABLED",type:ae.BOOLEAN},{name:"BACKEND",type:ae.STRING},{name:"EPSILON",type:ae.NUMBER}];function se(e,t){var n;try{n=fe(e,t)}catch(e){return!1}return null!=n&&(pe(n),!0)}var ue="tfjsflags";function le(){var e={};if("undefined"==typeof window||void 0===window.location)return e;var t=function(e){var t={};return window.location.search.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,function(e){for(var n=[],r=1;r0?0:function(e,t){if(0===e)return 0;var n,r=fe(e,t);return n=ce(r,"EXT_disjoint_timer_query_webgl2")&&2===e?2:ce(r,"EXT_disjoint_timer_query")?1:0,null!=r&&pe(r),n}(n,this.get("IS_BROWSER"))}if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE"===t)return this.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0&&!function(){var e=navigator.userAgent||navigator.vendor||window.opera;return/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(e)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(e.substr(0,4))}();if("HAS_WEBGL"===t)return this.get("WEBGL_VERSION")>0;if("WEBGL_VERSION"===t)return se(2,this.get("IS_BROWSER"))?2:se(1,this.get("IS_BROWSER"))?1:0;if("WEBGL_RENDER_FLOAT32_ENABLED"===t)return function(e,t){if(0===e)return!1;var n=fe(e,t);if(1===e){if(!ce(n,"OES_texture_float"))return!1}else if(!ce(n,"EXT_color_buffer_float"))return!1;var r=he(n,e);return pe(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("WEBGL_DOWNLOAD_FLOAT_ENABLED"===t)return function(e,t){if(0===e)return!1;var n=fe(e,t);if(1===e){if(!ce(n,"OES_texture_float"))return!1;if(!ce(n,"WEBGL_color_buffer_float"))return!1}else if(!ce(n,"EXT_color_buffer_float"))return!1;var r=he(n,e);return pe(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("WEBGL_FENCE_API_ENABLED"===t)return function(e,t){if(2!==e)return!1;var n=fe(e,t),r=null!=n.fenceSync;return pe(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("TEST_EPSILON"===t)return 32===this.backend.floatPrecision()?.001:.1;if("EPSILON"===t)return 32===this.backend.floatPrecision()?1e-7:.001;throw new Error("Unknown feature "+t+".")},t.prototype.setFeatures=function(e){this.features=Object.assign({},e)},t.prototype.reset=function(){this.features=le(),null!=this.globalEngine&&(this.globalEngine=null)},t.prototype.initBackend=function(e,t){var n=this;if(void 0===t&&(t=!1),this.backendName=e,this.engines[e])this.globalEngine=this.engines[e];else{var r=this.findBackend(e);this.globalEngine=new ie(r,t,function(){return n.get("DEBUG")}),this.engines[e]=this.globalEngine}},Object.defineProperty(t.prototype,"backend",{get:function(){return this.engine.backend},enumerable:!0,configurable:!0}),t.prototype.findBackend=function(e){return e in this.registry?this.registry[e].backend:null},t.prototype.registerBackend=function(e,t,n,r){var a=this;if(void 0===n&&(n=1),e in this.registry)return console.warn(e+" backend was already registered. Reusing existing backend"),null!=r&&r(function(){return a.engine}),!1;try{var i=t();return this.registry[e]={backend:i,priority:n},!0}catch(t){return console.warn("Registration of backend "+e+" failed"),console.warn(t.stack||t.message),!1}},t.prototype.removeBackend=function(e){if(!(e in this.registry))throw new Error(e+" backend not found in registry");this.registry[e].backend.dispose(),delete this.registry[e],e in this.engines&&delete this.engines[e]},Object.defineProperty(t.prototype,"engine",{get:function(){return this.initDefaultBackend(),this.globalEngine},enumerable:!0,configurable:!0}),t.prototype.initDefaultBackend=function(){null==this.globalEngine&&this.initBackend(this.get("BACKEND"),!1)},t}(),me=function(){var t=function(){var t;if("undefined"!=typeof window)t=window;else{if(void 0===e)throw new Error("Could not find a global object");t=e}return t}();return null==t.ENV&&(t.ENV=new de(le()),Z(function(){return t.ENV.engine})),t.ENV}(),ge=Object.freeze({Environment:de,ENV:me});function ye(e){return d(R(e),"The f passed in grad(f) must be a function"),function(t,n){return d(t instanceof Q,"The x passed in grad(f)(x) must be a tensor"),d(null==n||n instanceof Q,"The dy passed in grad(f)(x, dy) must be a tensor"),me.engine.tidy(function(){var r=me.engine.gradients(function(){return e(t)},[t],n),a=r.value,i=r.grads;return null!=n&&m(a.shape,n.shape,"The shape of dy passed in grad(f)(x, dy) must match the shape returned by f(x)"),Se(i),i[0]})}}function ve(e){return d(R(e),"The f passed in grads(f) must be a function"),function(t,n){return d(Array.isArray(t)&&t.every(function(e){return e instanceof Q}),"The args passed in grads(f)(args) must be an array of tensors"),d(null==n||n instanceof Q,"The dy passed in grads(f)(args, dy) must be a tensor"),me.engine.tidy(function(){var r=me.engine.gradients(function(){return e.apply(void 0,t)},t,n),a=r.value,i=r.grads;return null!=n&&m(a.shape,n.shape,"The shape of dy passed in grads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Se(i),i})}}function be(e){return d(R(e),"The f passed in valueAndGrad(f) must be a function"),function(t,n){d(t instanceof Q,"The x passed in valueAndGrad(f)(x) must be a tensor"),d(null==n||n instanceof Q,"The dy passed in valueAndGrad(f)(x, dy) must be a tensor");var r=me.engine.gradients(function(){return e(t)},[t],n),a=r.grads,i=r.value;return Se(a),{grad:a[0],value:i}}}function we(e){return d(R(e),"The f passed in valueAndGrads(f) must be a function"),function(t,n){d(Array.isArray(t)&&t.every(function(e){return e instanceof Q}),"The args passed in valueAndGrads(f)(args) must be array of tensors"),d(null==n||n instanceof Q,"The dy passed in valueAndGrads(f)(args, dy) must be a tensor");var r=me.engine.gradients(function(){return e.apply(void 0,t)},t,n);return null!=n&&m(r.value.shape,n.shape,"The shape of dy passed in valueAndGrads(f)([x1,...], dy) must match the shape returned by f([x1,...])"),Se(r.grads),r}}function xe(e,t){if(d(R(e),"The f passed in variableGrads(f) must be a function"),d(null==t||Array.isArray(t)&&t.every(function(e){return e instanceof ee}),"The varList passed in variableGrads(f, varList) must be an array of variables"),null==t)for(var n in t=[],me.engine.registeredVariables)t.push(me.engine.registeredVariables[n]);var r=t.length;d((t=t.filter(function(e){return e.trainable})).length>0,"variableGrads() expects at least one of the input variables to be trainable, but none of the "+r+" variables is trainable.");var a=me.engine.gradients(e,t,null,!0),i=a.value,o=a.grads;d(o.some(function(e){return null!=e}),"Cannot find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize()."),d(0===i.rank,"The f passed in variableGrads(f) must return a scalar, but it returned a rank-"+i.rank+" tensor");var s={};return t.forEach(function(e,t){null!=o[t]&&(s[e.name]=o[t])}),{value:i,grads:s}}function _e(e){return me.engine.customGrad(e)}function Se(e){if(e.filter(function(e){return null==e}).length>0)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that\n the f you passed encloses all operations that lead from x to y.")}var Ee=de.tidy,Ne=de.keep,Oe=de.dispose,ke=de.time;function Ie(){for(var e=[],t=0;t=2*t+1||a%2==1?o.push(a):i.push(a);r.push.apply(r,i),r.push(0),r.push.apply(r,o)}return r}function Pe(e,t,n,r){void 0===r&&(r=!0);var a=[];r?a.push(e[0]/n):a.push(e[0]*n);for(var i=1;i=-n&&e0?a>=c[t]:a<=c[t]);a+=r[t])n+=1;return n}),[l,h,f]}function Ge(e,t,n,r,a){var i=t[a];e&1<0?Number.MIN_SAFE_INTEGER:Number.MAX_SAFE_INTEGER);var o=r[a];return i<0&&(i+=o),p(0,i,o-1)}function He(e,t,n,r,a){var i=t[a];e&1<0?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER);var o=r[a];return i<0&&(i+=o),n[a]>0?p(0,i,o):p(-1,i,o-1)}function Ke(e,t,n,r){if(void 0===r&&(r="float32"),r=r||"float32",e instanceof Q)return e;if(!T(e)&&!Array.isArray(e)&&"number"!=typeof e&&"boolean"!=typeof e)throw new Error("Argument '"+t+"' passed to '"+n+"' must be a Tensor or TensorLike, but got "+e.constructor.name);var a=v(e);return T(e)||Array.isArray(e)||(e=[e]),Q.make(a,{values:L(e,r,me.get("DEBUG"))},r)}function Xe(e,t,n){if(!Array.isArray(e))throw new Error("Argument "+t+" passed to "+n+" must be a `Tensor[]` or `TensorLike[]`");return e.map(function(e,r){return Ke(e,t+"["+r+"]",n)})}function Ye(e){var t=Object.keys(e);if(1!==t.length)throw new Error("Please provide an object with a single key (operation name) mapping to a function. Got an object with "+t.length+" keys.");var n=t[0],r=e[n];n.endsWith("_")&&(n=n.substring(0,n.length-1));var a=function(){for(var e=[],t=0;t1)return ct([0],r);var a=B(Math.abs(Math.ceil((t-e)/n)),r);ta}).sort(function(e,t){return t.score-e.score}),o=[],s=0;s=0;--p)if(Ct(e,c,o[p])>=r){f=!0;break}if(!f&&(o.push(c),o.length>=n))break}return rt(o,"int32")}function Ct(e,t,n){var r=e.subarray(4*t,4*t+4),a=e.subarray(4*n,4*n+4),i=Math.min(r[0],r[2]),o=Math.min(r[1],r[3]),s=Math.max(r[0],r[2]),u=Math.max(r[1],r[3]),l=Math.min(a[0],a[2]),c=Math.min(a[1],a[3]),f=Math.max(a[0],a[2]),p=Math.max(a[1],a[3]),h=(s-i)*(u-o),d=(f-l)*(p-c);if(h<=0||d<=0)return 0;var m=Math.max(i,l),g=Math.max(o,c),y=Math.min(s,f),v=Math.min(u,p),b=Math.max(y-m,0)*Math.max(v-g,0);return b/(h+d-b)}function At(e,t,n){var r=Array(e.rank).fill(0),a=e.shape.slice();return t.map(function(t){a[n]=t;var i=e.slice(r,a);return r[n]+=t,i})}function Pt(e,t,n,r,a){for(var i=t[t.length-1],o=[e.length/i,i],s=o[0],u=o[1],l=I(n,s*r),c=I("int32",s*r),f=0;f1&&1===o&&r.unshift(i)}return r}function Mt(e,t){for(var n=[],r=0;r1)&&n.unshift(i)}return n}function Rt(e,t){for(var n=[],r=Math.max(e.length,t.length),a=0;a1?"["+t+"]":"")+";":"uniform sampler2D "+e.name+";"});a=a.join("\n");var i=e.map(function(e){return function(e,t,n){var r=function(e){var t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1)+"Flat",r=b(e.shapeInfo.logicalShape);if(e.shapeInfo.isUniform)return 1===r?"float "+n+"(int index) {return "+t+";}":"\n float "+n+"(int index) {\n for (int i = 0; i < "+r+"; i++) {\n if (i == index) {\n return "+t+"[i];\n }\n }\n }\n ";var a=e.shapeInfo.texShape,i=a[0],o=a[1];return 1===o&&1===i?"\n float "+n+"(int index) {\n return sampleTexture("+t+", halfCR);\n }\n ":1===o?"\n float "+n+"(int index) {\n vec2 uv = vec2(0.5, (float(index) + 0.5) / "+i+".0);\n return sampleTexture("+t+", uv);\n }\n ":1===i?"\n float "+n+"(int index) {\n vec2 uv = vec2((float(index) + 0.5) / "+o+".0, 0.5);\n return sampleTexture("+t+", uv);\n }\n ":"\n float "+n+"(int index) {\n vec2 uv = UVfrom1D("+i+", "+o+", index);\n return sampleTexture("+t+", uv);\n }\n "}(e);return r+=function e(t){var n=t.shapeInfo.logicalShape;switch(n.length){case 0:return function(e){var t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1);return e.shapeInfo.isUniform?"float "+n+"() {return "+t+";}":"\n float "+n+"() {\n return sampleTexture("+t+", halfCR);\n }\n "}(t);case 1:return function(e){var t=e.name,n="get"+t.charAt(0).toUpperCase()+t.slice(1);return"\n float "+n+"(int index) {\n return "+n+"Flat(index);\n }\n "}(t);case 2:return function(t){var n=t.shapeInfo.logicalShape,r=t.name,a="get"+r.charAt(0).toUpperCase()+r.slice(1),i=t.shapeInfo.texShape;if(null!=i&&w(n,i)){var o=i[0];return"\n float "+a+"(int row, int col) {\n vec2 uv = (vec2(col, row) + halfCR) / vec2("+i[1]+".0, "+o+".0);\n return sampleTexture("+r+", uv);\n }\n "}var s=k(n),u=s.newShape,l=s.keptDims,c=u;if(c.lengths||o.length>0),c=function(e){for(var t=0;t=1?"coords = 0;":s.map(function(e){return"coords["+(e+u)+"] = 0;"}).join("\n"))+"\n return get"+n+"("+(i<2&&a>0?"coords":e.shapeInfo.logicalShape.map(function(e,t){return"coords["+(t+u)+"]"}).join(", "))+");\n }\n "}(e,t,a,i);var p=b(e.shapeInfo.logicalShape),h="";l&&c&&(h="\n int mainPart = index / "+p+";\n index -= mainPart * "+p+";\n ");var d=t.texShape;if(f)return 1===p?"float "+i+"() {return "+r+";}":"\n float "+i+"() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+d[0]+", "+d[1]+"));\n int index = resTexRC.x * "+d[1]+" + resTexRC.y;\n "+h+"\n return get"+a+"Flat(index);\n }\n ";var m=e.shapeInfo.texShape;return w(m,d)?"\n float "+i+"() {\n return sampleTexture("+r+", resultUV);\n }\n ":"\n float "+i+"() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+d[0]+", "+d[1]+"));\n int index = resTexRC.x * "+d[1]+" + resTexRC.y;\n "+h+"\n int texR = index / "+m[1]+";\n int texC = index - texR * "+m[1]+";\n vec2 uv = (vec2(texC, texR) + halfCR) /\n vec2("+m[1]+".0, "+m[0]+".0);\n\n return sampleTexture("+r+", uv);\n }\n "}(e,t,n)),r}(e,t,r)}).join("\n"),o=t.texShape,s=function(e,t){switch(e.length){case 0:return"\n int getOutputCoords() {\n return 0;\n }\n ";case 1:return function(e,t){return 1===t[0]?"\n int getOutputCoords() {\n return int(resultUV.x * "+t[1]+".0);\n }\n ":1===t[1]?"\n int getOutputCoords() {\n return int(resultUV.y * "+t[0]+".0);\n }\n ":"\n int getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n return resTexRC.x * "+t[1]+" + resTexRC.y;\n }\n "}(0,t);case 2:return function(e,t){return w(e,t)?"\n ivec2 getOutputCoords() {\n return ivec2(resultUV.yx * vec2("+t[0]+", "+t[1]+"));\n }\n ":1===e[1]?"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n return ivec2(index, 0);\n }\n ":1===e[0]?"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n return ivec2(0, index);\n }\n ":"\n ivec2 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n int r = index / "+e[1]+";\n int c = index - r * "+e[1]+";\n return ivec2(r, c);\n }\n "}(e,t);case 3:return function(e,t){var n=e[1]*e[2],r=e[2];return"\n ivec3 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n int r = index / "+n+";\n index -= r * "+n+";\n int c = index / "+r+";\n int d = index - c * "+r+";\n return ivec3(r, c, d);\n }\n "}(e,t);case 4:return function(e,t){var n=e[3],r=e[2]*n,a=e[1]*r;return"\n ivec4 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n\n int r = index / "+a+";\n index -= r * "+a+";\n\n int c = index / "+r+";\n index -= c * "+r+";\n\n int d = index / "+n+";\n int d2 = index - d * "+n+";\n\n return ivec4(r, c, d, d2);\n }\n "}(e,t);case 5:return function(e,t){var n=e[4],r=e[3]*n,a=e[2]*r,i=e[1]*a;return"\n ivec5 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx * vec2("+t[0]+",\n "+t[1]+"));\n\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n\n int r = index / "+i+";\n index -= r * "+i+";\n\n int c = index / "+a+";\n index -= c * "+a+";\n\n int d = index / "+r+";\n index -= d * "+r+";\n\n int d2 = index / "+n+";\n int d3 = index - d2 * "+n+";\n\n ivec5 outShape = ivec5(r, c, d, d2, d3);\n return outShape;\n }\n "}(e,t);case 6:return function(e,t){var n=e[5],r=e[4]*n,a=e[3]*r,i=e[2]*a,o=e[1]*i;return"\n ivec6 getOutputCoords() {\n ivec2 resTexRC = ivec2(resultUV.yx *\n vec2("+t[0]+", "+t[1]+"));\n int index = resTexRC.x * "+t[1]+" + resTexRC.y;\n\n int r = index / "+o+";\n index -= r * "+o+";\n\n int c = index / "+i+";\n index -= c * "+i+";\n\n int d = index / "+a+";\n index -= d * "+a+";\n\n int d2 = index / "+r+";\n index -= d2 * "+r+";\n\n int d3 = index / "+n+";\n int d4 = index - d3 * "+n+";\n\n ivec6 result = ivec6(r, c, d, d2, d3, d4);\n return result;\n }\n "}(e,t);default:throw new Error(e.length+"-D output sampling is not yet supported")}}(t.logicalShape,o);return[Ut,Ft,Vt,a,s,i,n].join("\n")}var Ft="\n float sampleTexture(sampler2D textureSampler, vec2 uv) {\n return texture2D(textureSampler, uv).r;\n }\n",Vt="\n void setOutput(float val) {\n gl_FragColor = vec4(val, 0, 0, 0);\n }\n",Ut="\n precision highp float;\n precision highp int;\n varying vec2 resultUV;\n const vec2 halfCR = vec2(0.5, 0.5);\n\n struct ivec5\n {\n int x;\n int y;\n int z;\n int w;\n int u;\n };\n\n struct ivec6\n {\n int x;\n int y;\n int z;\n int w;\n int u;\n int v;\n };\n\n bool isNaN(float val) {\n return (val < 0.0 || 0.0 < val || val == 0.0) ? false : true;\n }\n\n bool hasNaN(vec4 values) {\n vec4 v1 = values * values;\n vec4 v2 = values * values;\n return any(notEqual(v1, v2));\n }\n\n float getNaN(vec4 values) {\n return dot(vec4(1), values);\n }\n\n int round(float value) {\n return int(floor(value + 0.5));\n }\n\n int imod(int x, int y) {\n return x - y * (x / y);\n }\n\n //Based on the work of Dave Hoskins\n //https://www.shadertoy.com/view/4djSRW\n #define HASHSCALE1 443.8975\n float random(float seed){\n vec2 p = resultUV * seed;\n vec3 p3 = fract(vec3(p.xyx) * HASHSCALE1);\n p3 += dot(p3, p3.yzx + 19.19);\n return fract((p3.x + p3.y) * p3.z);\n }\n\n \nvec2 UVfrom1D(int texNumR, int texNumC, int index) {\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n \nvec2 UVfrom2D(int texNumR, int texNumC, int numC, int row, int col) {\n int index = row * numC + col;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n \nvec2 UVfrom3D(int texNumR, int texNumC, int stride0,\n int stride1, int row, int col, int depth) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 + depth;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n \nvec2 UVfrom4D(int texNumR, int texNumC, int stride0,\n int stride1, int stride2, int row, int col, int depth,\n int depth2) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 + depth * stride2 + depth2;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n \nvec2 UVfrom5D(int texNumR, int texNumC, int stride0,\n int stride1, int stride2, int stride3, int row, int col, int depth,\n int depth2, int depth3) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 +\n depth * stride2 + depth2 * stride3 + depth3;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n \nvec2 UVfrom6D(int texNumR, int texNumC, int stride0,\n int stride1, int stride2, int stride3, int stride4,\n int row, int col, int depth, int depth2, int depth3, int depth4) {\n // Explicitly use integer operations as dot() only works on floats.\n int index = row * stride0 + col * stride1 + depth * stride2 + depth2 *\n stride3 + depth3 * stride4 + depth4;\n int texR = index / texNumC;\n int texC = index - texR * texNumC;\n return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);\n}\n\n";function Wt(e){if(e<=1)return"int";if(2===e)return"ivec2";if(3===e)return"ivec3";if(4===e)return"ivec4";if(5===e)return"ivec5";if(6===e)return"ivec6";throw Error("GPU for rank "+e+" is not yet supported")}function qt(e,t){var n=JSON.parse(JSON.stringify(e));return n.shapeInfo.logicalShape=t,n}function $t(e,t){return t.map(function(t){return e[t]}).join(", ")}function Gt(e,t){if(1===e)return""+t;if(2===e)return t+".y";if(3===e)return t+".z";if(4===e)return t+".w";throw Error("Cumulative sum for rank "+e+" is not yet supported")}var Ht,Kt,Xt=function(){function e(e,t,n){this.variableNames=["x"],this.outputShape=[],this.outputShape=e,this.blockSize=t,this.dataFormat=n,this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int h = "+this.getHeightCoordString()+";\n int w = "+this.getWidthCoordString()+";\n int d = "+this.getDepthCoordString()+";\n\n int in_h = h / "+t+";\n int offset_h = imod(h, "+t+");\n int in_w = w / "+t+";\n int offset_w = imod(w, "+t+");\n int offset_d = (offset_h * "+t+" + offset_w) *\n "+this.getOutputDepthSize()+";\n int in_d = d + offset_d;\n\n float result = "+this.getInputSamplingString()+";\n setOutput(result);\n }\n "}return e.prototype.getHeightCoordString=function(){return"NHWC"===this.dataFormat?"coords[1]":"coords[2]"},e.prototype.getWidthCoordString=function(){return"NHWC"===this.dataFormat?"coords[2]":"coords[3]"},e.prototype.getDepthCoordString=function(){return"NHWC"===this.dataFormat?"coords[3]":"coords[1]"},e.prototype.getOutputDepthSize=function(){return"NHWC"===this.dataFormat?this.outputShape[3]:this.outputShape[1]},e.prototype.getInputSamplingString=function(){return"NHWC"===this.dataFormat?"getX(b, in_h, in_w, in_d)":"getX(b, in_d, in_h, in_w)"},e}();function Yt(e,t){return[t,e]}function Jt(e,t){return e*t}function Zt(e,t,n){var r=function(e,t){if(e%t!=0)throw new Error("unpackedSize ("+e+") must be a multiple of "+t);return e/t}(e.length,n);if(t.length= "+r);for(var a=0,i=0;ir||n>r)throw a="["+t+"x"+n+"]",new Error("Requested texture size "+a+" greater than WebGL maximum on this browser / GPU ["+r+"x"+r+"].")}function Sn(e){return Mn(e,function(){return e.createFramebuffer()},"Unable to create WebGLFramebuffer.")}function En(e,t,n,r,a,i,o){var s=e.getAttribLocation(t,n);return-1!==s&&(an(e,function(){return e.bindBuffer(e.ARRAY_BUFFER,r)}),an(e,function(){return e.vertexAttribPointer(s,a,e.FLOAT,!1,i,o)}),an(e,function(){return e.enableVertexAttribArray(s)}),!0)}function Nn(e,t,n){Rn(e,n),an(e,function(){return e.activeTexture(e.TEXTURE0+n)}),an(e,function(){return e.bindTexture(e.TEXTURE_2D,t)})}function On(e,t,n){return Mn(e,function(){return e.getUniformLocation(t,n)},'uniform "'+n+'" not present in program.')}function kn(e,t,n){return e.getUniformLocation(t,n)}function In(e,t,n,r,a){an(e,function(){return Nn(e,n,a)}),an(e,function(){return e.uniform1i(r,a)})}function Cn(e,t,n){an(e,function(){return e.bindFramebuffer(e.FRAMEBUFFER,n)}),an(e,function(){return e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,t,0)})}function An(e,t){an(e,function(){return e.bindFramebuffer(e.FRAMEBUFFER,t)}),an(e,function(){return e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,null,0)})}function Pn(e){var t=e.checkFramebufferStatus(e.FRAMEBUFFER);if(t!==e.FRAMEBUFFER_COMPLETE)throw new Error("Error binding framebuffer: "+Tn(e,t))}function Tn(e,t){switch(t){case e.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_ATTACHMENT";case e.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:return"FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";case e.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:return"FRAMEBUFFER_INCOMPLETE_DIMENSIONS";case e.FRAMEBUFFER_UNSUPPORTED:return"FRAMEBUFFER_UNSUPPORTED";default:return"unknown error "+t}}function Mn(e,t,n){var r=an(e,function(){return t()});if(null==r)throw new Error(n);return r}function Rn(e,t){var n=e.MAX_COMBINED_TEXTURE_IMAGE_UNITS-1,r=t+e.TEXTURE0;if(rn)throw new Error("textureUnit must be in [gl.TEXTURE0, gl.TEXTURE"+n+"].")}function Dn(e,t){2!==t.length&&(t=k(t).newShape);var n=bn(e),r=b(t);return t.length<=1&&r<=n?[r,1]:2===t.length&&t[0]<=n&&t[1]<=n?t:3===t.length&&t[0]<=n&&t[1]*t[2]<=n?[t[0],t[1]*t[2]]:4===t.length&&t[0]<=n&&t[1]*t[2]*t[3]<=n?[t[0],t[1]*t[2]*t[3]]:S(r)}var jn=Object.freeze({createWebGLRenderingContext:nn,createWebGLRenderingContextFromCanvas:rn,callAndCheck:an,enableDebugWebGLErrorChecking:sn,checkWebGLError:un,getWebGLErrorMessage:ln,getExtensionOrThrow:cn,createVertexShader:fn,createFragmentShader:pn,createProgram:dn,linkProgram:mn,validateProgram:gn,createStaticVertexBuffer:yn,createStaticIndexBuffer:vn,queryMaxTextureSize:bn,getNumChannels:wn,createTexture:xn,validateTextureSize:_n,createFramebuffer:Sn,bindVertexBufferToProgramAttribute:En,bindTextureUnit:Nn,unbindTextureUnit:function(e,t){Rn(e,t),an(e,function(){return e.activeTexture(e.TEXTURE0+t)}),an(e,function(){return e.bindTexture(e.TEXTURE_2D,null)})},getProgramUniformLocationOrThrow:On,getProgramUniformLocation:kn,bindTextureToProgramUniformSampler:In,bindCanvasToFramebuffer:function(e){an(e,function(){return e.bindFramebuffer(e.FRAMEBUFFER,null)}),an(e,function(){return e.viewport(0,0,e.canvas.width,e.canvas.height)}),an(e,function(){return e.scissor(0,0,e.canvas.width,e.canvas.height)})},bindColorTextureToFramebuffer:Cn,unbindColorTextureFromFramebuffer:An,validateFramebuffer:Pn,getFramebufferErrorMessage:Tn,getTextureShapeFromLogicalShape:Dn});function Ln(e){var t,n={alpha:!1,antialias:!1,premultipliedAlpha:!1,preserveDrawingBuffer:!1,depth:!1,stencil:!1,failIfMajorPerformanceCaveat:!0};return an(t=null!=e?rn(e,n):nn(n),function(){return t.disable(t.DEPTH_TEST)}),an(t,function(){return t.disable(t.STENCIL_TEST)}),an(t,function(){return t.disable(t.BLEND)}),an(t,function(){return t.disable(t.DITHER)}),an(t,function(){return t.disable(t.POLYGON_OFFSET_FILL)}),an(t,function(){return t.disable(t.SAMPLE_COVERAGE)}),an(t,function(){return t.enable(t.SCISSOR_TEST)}),an(t,function(){return t.enable(t.CULL_FACE)}),an(t,function(){return t.cullFace(t.BACK)}),t}function zn(e){return fn(e,"\n precision highp float;\n attribute vec3 clipSpacePos;\n attribute vec2 uv;\n varying vec2 resultUV;\n\n void main() {\n gl_Position = vec4(clipSpacePos, 1);\n resultUV = uv;\n }")}function Bn(e){return yn(e,new Float32Array([-1,1,0,0,1,-1,-1,0,0,0,1,1,0,1,1,1,-1,0,1,0]))}function Fn(e){return vn(e,new Uint16Array([0,1,2,2,1,3]))}function Vn(e,t){var n,r,a,i,o,s,u,l=e;return 2===me.get("WEBGL_VERSION")?(n=l.R32F,r=l.R16F,a=l.RGBA32F,i=l.RED,o=4,s=1,u=l.HALF_FLOAT):(n=e.RGBA,r=e.RGBA,a=l.RGBA,i=e.RGBA,o=4,s=4,u=null!=t?t.HALF_FLOAT_OES:null),{internalFormatFloat:n,internalFormatHalfFloat:r,internalFormatPackedFloat:a,textureFormatFloat:i,downloadTextureFormat:e.RGBA,downloadUnpackNumChannels:o,defaultNumChannels:s,textureTypeHalfFloat:u}}function Un(e,t,n,r,a,i){_n(e,t,n);var o=xn(e),s=e.TEXTURE_2D;return an(e,function(){return e.bindTexture(s,o)}),an(e,function(){return e.texParameteri(s,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE)}),an(e,function(){return e.texParameteri(s,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE)}),an(e,function(){return e.texParameteri(s,e.TEXTURE_MIN_FILTER,e.NEAREST)}),an(e,function(){return e.texParameteri(s,e.TEXTURE_MAG_FILTER,e.NEAREST)}),an(e,function(){return e.texImage2D(s,0,r,t,n,0,a,i,null)}),an(e,function(){return e.bindTexture(e.TEXTURE_2D,null)}),o}function Wn(e,t,n,r){var a=Yt(t,n);return Un(e,a[0],a[1],r.internalFormatFloat,r.textureFormatFloat,e.FLOAT)}function qn(e,t,n,r){var a=Yt(t,n);return Un(e,a[0],a[1],r.internalFormatFloat,r.textureFormatFloat,r.textureTypeHalfFloat)}function $n(e,t,n,r){var a=Yt(t,n);return Un(e,a[0],a[1],e.RGBA,e.RGBA,e.UNSIGNED_BYTE)}function Gn(e,t,n,r){var a=Qt(t,n);return Un(e,a[0],a[1],r.internalFormatPackedFloat,e.RGBA,e.FLOAT)}function Hn(e,t,n){return an(e,function(){return e.bindBuffer(e.ARRAY_BUFFER,n)}),En(e,t,"clipSpacePos",n,3,20,0)&&En(e,t,"uv",n,2,20,12)}function Kn(e,t,n){an(e,function(){return e.bindTexture(e.TEXTURE_2D,t)}),an(e,function(){return e.texImage2D(e.TEXTURE_2D,0,e.RGBA,e.RGBA,e.UNSIGNED_BYTE,n)}),an(e,function(){return e.bindTexture(e.TEXTURE_2D,null)})}function Xn(e,t,n,r,a,i){_n(e,n,r),an(e,function(){return e.bindTexture(e.TEXTURE_2D,t)}),an(e,function(){return e.texSubImage2D(e.TEXTURE_2D,0,0,0,n,r,i,e.FLOAT,a)}),an(e,function(){return e.bindTexture(e.TEXTURE_2D,null)})}function Yn(e,t,n,r,a,i,o){var s,u=Yt(n,r),l=u[0],c=u[1];1===o.defaultNumChannels?s=a:function(e,t,n){var r=Jt(e.length,n);if(t.length= "+r);for(var a=0,i=0;i= "+a);for(var i=Qt(t,n),o=i[0],s=i[1],u=n%2==1,l=t%2==1,c=Math.floor(n/2),f=Math.floor(t/2),p=u?4:0,h=n,d=0,m=0;m= "+a);for(var i=n%2==1,o=t%2==1,s=Math.floor(n/2),u=Math.floor(t/2),l=Qt(t,n),c=l[0],f=l[1],p=i?4:0,h=n+(i?1:0),d=0,m=0,g=n,y=0;y0?(t=this.beginQuery(),this.endQuery(),n=function(){return r.isQueryAvailable(t,me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))}):n=function(){return!0};return{query:t,isFencePassed:n}},e.prototype.downloadMatrixFromPackedTexture=function(e,t,n){var r=this;return this.downloadMatrixDriver(e,function(){return nr(r.gl,t,n,r.textureConfig)})},e.prototype.createProgram=function(e){this.throwIfDisposed();var t=this.gl,n=pn(t,e),r=zn(t),a=dn(t);return an(t,function(){return t.attachShader(a,r)}),an(t,function(){return t.attachShader(a,n)}),mn(t,a),this.autoDebugValidate&&gn(t,a),this.vertexAttrsAreBound||(this.setProgram(a),this.vertexAttrsAreBound=Hn(t,this.program,this.vertexBuffer)),a},e.prototype.deleteProgram=function(e){var t=this;this.throwIfDisposed(),e===this.program&&(this.program=null),null!=e&&an(this.gl,function(){return t.gl.deleteProgram(e)})},e.prototype.setProgram=function(e){var t=this;this.throwIfDisposed(),this.program=e,null!=this.program&&this.autoDebugValidate&&gn(this.gl,this.program),an(this.gl,function(){return t.gl.useProgram(e)})},e.prototype.getUniformLocation=function(e,t,n){return void 0===n&&(n=!0),this.throwIfDisposed(),n?On(this.gl,e,t):kn(this.gl,e,t)},e.prototype.getAttributeLocation=function(e,t){var n=this;return this.throwIfDisposed(),an(this.gl,function(){return n.gl.getAttribLocation(e,t)})},e.prototype.getUniformLocationNoThrow=function(e,t){return this.throwIfDisposed(),this.gl.getUniformLocation(e,t)},e.prototype.setInputMatrixTexture=function(e,t,n){this.throwIfDisposed(),this.throwIfNoProgram(),In(this.gl,this.program,e,t,n)},e.prototype.setOutputMatrixTexture=function(e,t,n){this.setOutputMatrixTextureDriver(e,n,t)},e.prototype.setOutputPackedMatrixTexture=function(e,t,n){this.throwIfDisposed();var r=Qt(t,n),a=r[0],i=r[1];this.setOutputMatrixTextureDriver(e,a,i)},e.prototype.setOutputMatrixWriteRegion=function(e,t,n,r){this.setOutputMatrixWriteRegionDriver(n,e,r,t)},e.prototype.setOutputPackedMatrixWriteRegion=function(e,t,n,r){throw new Error("setOutputPackedMatrixWriteRegion not implemented.")},e.prototype.debugValidate=function(){null!=this.program&&gn(this.gl,this.program),Pn(this.gl)},e.prototype.executeProgram=function(){this.throwIfDisposed(),this.throwIfNoProgram();var e=this.gl;this.autoDebugValidate&&this.debugValidate(),an(e,function(){return e.drawElements(e.TRIANGLES,6,e.UNSIGNED_SHORT,0)})},e.prototype.blockUntilAllProgramsCompleted=function(){var e=this;this.throwIfDisposed(),an(this.gl,function(){return e.gl.finish()})},e.prototype.getQueryTimerExtension=function(){return null==this.disjointQueryTimerExtension&&(this.disjointQueryTimerExtension=cn(this.gl,2===me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")?"EXT_disjoint_timer_query_webgl2":"EXT_disjoint_timer_query")),this.disjointQueryTimerExtension},e.prototype.getQueryTimerExtensionWebGL2=function(){return this.getQueryTimerExtension()},e.prototype.getQueryTimerExtensionWebGL1=function(){return this.getQueryTimerExtension()},e.prototype.beginQuery=function(){if(2===me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")){var e=this.gl,t=this.getQueryTimerExtensionWebGL2(),n=e.createQuery();return e.beginQuery(t.TIME_ELAPSED_EXT,n),n}var r=this.getQueryTimerExtensionWebGL1(),a=r.createQueryEXT();return r.beginQueryEXT(r.TIME_ELAPSED_EXT,a),a},e.prototype.endQuery=function(){if(2!==me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")){var e=this.getQueryTimerExtensionWebGL1();e.endQueryEXT(e.TIME_ELAPSED_EXT)}else{var t=this.gl,n=this.getQueryTimerExtensionWebGL2();t.endQuery(n.TIME_ELAPSED_EXT)}},e.prototype.waitForQueryAndGetTime=function(e){return l(this,void 0,void 0,function(){var t=this;return c(this,function(n){switch(n.label){case 0:return[4,N(function(){return t.isQueryAvailable(e,me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))})];case 1:return n.sent(),[2,this.getQueryTime(e,me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))]}})})},e.prototype.getQueryTime=function(e,t){if(0===t)return null;if(2===t){var n=this.gl;return n.getQueryParameter(e,n.QUERY_RESULT)/1e6}var r=this.getQueryTimerExtensionWebGL1();return r.getQueryObjectEXT(e,r.QUERY_RESULT_EXT)/1e6},e.prototype.isQueryAvailable=function(e,t){if(0===t)return!0;if(2===t){var n=this.gl,r=this.getQueryTimerExtensionWebGL2(),a=n.getQueryParameter(e,n.QUERY_RESULT_AVAILABLE);return null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),a&&!this.disjoint}return a=(r=this.getQueryTimerExtensionWebGL1()).getQueryObjectEXT(e,r.QUERY_RESULT_AVAILABLE_EXT),null==this.disjoint&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),a&&!this.disjoint},e.prototype.pollFence=function(e){var t=this;return new Promise(function(n){t.addItemToPoll(function(){return e.isFencePassed()},function(){return n()})})},e.prototype.pollItems=function(){for(var e=function(e){for(var t=0,n=e.length-1,r=-1;t<=n;){var a=t+n>>1;e[a]()?(r=a,t=a+1):n=a-1}return r}(this.itemsToPoll.map(function(e){return e.isDoneFn})),t=0;t<=e;++t)(0,this.itemsToPoll[t].resolveFn)();this.itemsToPoll=this.itemsToPoll.slice(e+1)},e.prototype.addItemToPoll=function(e,t){var n=this;this.itemsToPoll.push({isDoneFn:e,resolveFn:t}),this.itemsToPoll.length>1||N(function(){return n.pollItems(),0===n.itemsToPoll.length})},e.prototype.bindTextureToFrameBuffer=function(e){this.throwIfDisposed(),Cn(this.gl,e,this.framebuffer),this.autoDebugValidate&&Pn(this.gl)},e.prototype.unbindTextureToFrameBuffer=function(){null!=this.outputTexture?(Cn(this.gl,this.outputTexture,this.framebuffer),this.autoDebugValidate&&Pn(this.gl)):An(this.gl,this.framebuffer)},e.prototype.downloadMatrixDriver=function(e,t){this.bindTextureToFrameBuffer(e);var n=t();return this.unbindTextureToFrameBuffer(),n},e.prototype.setOutputMatrixTextureDriver=function(e,t,n){this.throwIfDisposed();var r=this.gl;Cn(r,e,this.framebuffer),this.autoDebugValidate&&Pn(r),this.outputTexture=e,an(r,function(){return r.viewport(0,0,t,n)}),an(r,function(){return r.scissor(0,0,t,n)})},e.prototype.setOutputMatrixWriteRegionDriver=function(e,t,n,r){var a=this;this.throwIfDisposed(),an(this.gl,function(){return a.gl.scissor(e,t,n,r)})},e.prototype.throwIfDisposed=function(){if(this.disposed)throw new Error("Attempted to use disposed GPGPUContext.")},e.prototype.throwIfNoProgram=function(){if(null==this.program)throw new Error("No GPU program is currently set.")},e}();function ir(e,t){if(e.length!==t.length)throw Error("Binary was compiled with "+e.length+" inputs, but was executed with "+t.length+" inputs");e.forEach(function(e,n){var r=e.logicalShape,a=t[n],i=a.shape;if(!w(r,i))throw Error("Binary was compiled with different shapes than the current args. Shapes "+r+" and "+i+" must match");if(!e.isUniform||!a.isUniform){var o=e.texShape,s=a.isUniform?null:a.texData.texShape;if(!w(o,s))throw Error("Binary was compiled with different texture shapes than the current args. Shape "+o+" and "+s+" must match")}})}var or=function(){function e(e,t,n){this.variableNames=["probs"],this.outputShape=[e,n],this.userCode="\n uniform float seed;\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n\n float r = random(seed);\n float cdf = 0.0;\n\n for (int i = 0; i < "+(t-1)+"; i++) {\n cdf += getProbs(batch, i);\n\n if (r < cdf) {\n setOutput(float(i));\n return;\n }\n }\n\n // If no other event happened, last event happened.\n setOutput(float("+(t-1)+"));\n }\n "}return e.prototype.getCustomSetupFunc=function(e){var t=this;return function(n,r){null==t.seedLoc&&(t.seedLoc=n.getUniformLocation(r,"seed")),n.gl.uniform1f(t.seedLoc,e)}},e}(),sr=function(e,t,n){if(this.variableNames=["x"],"avg"===t&&n)throw new Error("Cannot compute positions for average pool.");var r=e.filterHeight,a=e.filterWidth,i=e.strideHeight,o=e.strideWidth,s=e.padInfo.top,u=e.padInfo.left;this.outputShape=e.outShape;var l="avg"===t,c="0.0";if(l||(c="-1.0 / 0.0"),n)this.userCode="\n const ivec2 strides = ivec2("+i+", "+o+");\n const ivec2 pads = ivec2("+s+", "+u+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n float minMaxValue = 0.0;\n float minMaxValueFound = 0.0;\n int minMaxPosition = 0;\n float avgValue = 0.0;\n\n for (int wR = 0; wR < "+r+"; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+a+"; wC++) {\n int xC = xCCorner + wC;\n\n if (xC < 0 || xC >= "+e.inWidth+") {\n continue;\n }\n\n float value = getX(batch, xR, xC, d);\n\n // If a min / max value has already been found, use it. If not,\n // use the current value.\n float currMinMaxValue = mix(\n value, minMaxValue, minMaxValueFound);\n if (value >= currMinMaxValue) {\n minMaxValue = value;\n minMaxValueFound = 1.0;\n minMaxPosition = wR * "+a+" + wC;\n }\n }\n }\n setOutput(float(minMaxPosition));\n }\n ";else{var f=t+"("+t+"("+t+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";"avg"===t&&(f="avgValue / count");var p=4*Math.floor(a/4),h=a%4,d="\n if ("+l+") {\n avgValue += dot(values, ones);\n } else {\n minMaxValue = max(values, minMaxValue);\n }\n ";this.userCode="\n const ivec2 strides = ivec2("+i+", "+o+");\n const ivec2 pads = ivec2("+s+", "+u+");\n const float initializationValue = "+c+";\n const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);\n\n float count = 0.0;\n\n float getValue(int batch, int xR, int xC, int d) {\n if (xC < 0 || xC >= "+e.inWidth+") {\n return initializationValue;\n }\n count += 1.0;\n return getX(batch, xR, xC, d);\n }\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // max/min x(?, ?, d) to get y(yR, yC, d).\n // ? = to be determined\n vec4 minMaxValue = vec4("+c+");\n float avgValue = 0.0;\n count = 0.0;\n\n for (int wR = 0; wR < "+r+"; wR++) {\n int xR = xRCorner + wR;\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+p+"; wC += 4) {\n int xC = xCCorner + wC;\n\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n getValue(batch, xR, xC + 3, d)\n );\n\n "+d+"\n }\n\n int xC = xCCorner + "+p+";\n if ("+(1===h)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+d+"\n } else if ("+(2===h)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n initializationValue,\n initializationValue\n );\n\n "+d+"\n } else if ("+(3===h)+") {\n vec4 values = vec4(\n getValue(batch, xR, xC, d),\n getValue(batch, xR, xC + 1, d),\n getValue(batch, xR, xC + 2, d),\n initializationValue\n );\n\n "+d+"\n }\n }\n setOutput("+f+");\n }\n "}},ur=function(){function e(e){this.variableNames=["source"],this.outputShape=e,this.rank=e.length;var t=Wt(this.rank),n=function(e){if(1===e)return"sourceLoc";if(2===e)return"sourceLoc.x, sourceLoc.y";if(3===e)return"sourceLoc.x, sourceLoc.y, sourceLoc.z";if(4===e)return"sourceLoc.x, sourceLoc.y, sourceLoc.z, sourceLoc.w";throw Error("Slicing for rank "+e+" is not yet supported")}(this.rank);this.userCode="\n uniform "+t+" start;\n\n void main() {\n "+t+" sourceLoc = start + getOutputCoords();\n setOutput(getSource("+n+"));\n }\n "}return e.prototype.getCustomSetupFunc=function(e){var t=this;if(e.length!==this.rank)throw Error("The rank ("+this.rank+") of the program must match the length of start ("+e.length+")");return function(n,r){if(null!=t.startLoc||(t.startLoc=n.getUniformLocationNoThrow(r,"start"),null!=t.startLoc))if(1===t.rank)n.gl.uniform1i(t.startLoc,e[0]);else if(2===t.rank)n.gl.uniform2i(t.startLoc,e[0],e[1]);else if(3===t.rank)n.gl.uniform3i(t.startLoc,e[0],e[1],e[2]);else{if(4!==t.rank)throw Error("Slicing for rank "+t.rank+" is not yet supported");n.gl.uniform4i(t.startLoc,e[0],e[1],e[2],e[3])}}},e}(),lr=function(){function e(e){this.gpgpu=e,this.numUsedTextures=0,this.numFreeTextures=0,this.freeTextures={},this.logEnabled=!1,this.usedTextures={}}return e.prototype.acquireTexture=function(e,t){var n,r=cr(t),a=fr(e,r);if(a in this.freeTextures||(this.freeTextures[a]=[]),a in this.usedTextures||(this.usedTextures[a]=[]),this.freeTextures[a].length>0){this.numFreeTextures--,this.numUsedTextures++,this.log();var i=this.freeTextures[a].shift();return this.usedTextures[a].push(i),i}return this.numUsedTextures++,this.log(),r===Kt.FLOAT32?n=this.gpgpu.createFloat32MatrixTexture(e[0],e[1]):r===Kt.FLOAT16?n=this.gpgpu.createFloat16MatrixTexture(e[0],e[1]):r===Kt.UNSIGNED_BYTE&&(n=this.gpgpu.createUnsignedBytesMatrixTexture(e[0],e[1])),this.usedTextures[a].push(n),n},e.prototype.releaseTexture=function(e,t,n){var r=fr(t,cr(n));r in this.freeTextures||(this.freeTextures[r]=[]),this.freeTextures[r].push(e),this.numFreeTextures++,this.numUsedTextures--;var a=this.usedTextures[r],i=a.indexOf(e);if(i<0)throw new Error("Cannot release a texture that was never provided by this texture manager");a.splice(i,1),this.log()},e.prototype.log=function(){if(this.logEnabled){var e=this.numFreeTextures+this.numUsedTextures;console.log("Free/Used",this.numFreeTextures+" / "+this.numUsedTextures,"("+e+")")}},e.prototype.getNumUsedTextures=function(){return this.numUsedTextures},e.prototype.getNumFreeTextures=function(){return this.numFreeTextures},e.prototype.dispose=function(){var e=this;if(null!=this.freeTextures){for(var t in this.freeTextures)this.freeTextures[t].forEach(function(t){e.gpgpu.deleteMatrixTexture(t)});for(var t in this.usedTextures)this.usedTextures[t].forEach(function(t){e.gpgpu.deleteMatrixTexture(t)});this.freeTextures=null,this.usedTextures=null,this.numUsedTextures=0,this.numFreeTextures=0}},e}();function cr(e){if(e===Ht.DOWNLOAD||e===Ht.PIXELS)return Kt.UNSIGNED_BYTE;if(e===Ht.UPLOAD)return Kt.FLOAT32;if(e===Ht.RENDER)return me.get("WEBGL_RENDER_FLOAT32_ENABLED")?Kt.FLOAT32:Kt.FLOAT16;throw new Error("Unknown logical texture type "+e)}function fr(e,t){return e[0]+"_"+e[1]+"_"+t}var pr=1.7580993408473768,hr=1.0507009873554805,dr=function(){function e(e,t){this.variableNames=["A"],this.outputShape=e,this.userCode="\n uniform float NAN;\n float unaryOperation(float x) {\n "+t+"\n }\n\n void main() {\n float x = getAAtOutCoords();\n float y = unaryOperation(x);\n\n setOutput(y);\n }\n "}return e.prototype.getCustomSetupFunc=function(){var e=this;return function(t,n){null==e.startLoc&&(e.startLoc=t.getUniformLocationNoThrow(n,"NAN"),null==e.startLoc)||t.gl.uniform1f(e.startLoc,NaN)}},e}(),mr="if (isNaN(x)) return x;",gr=Ye({concat_:function(e,t){void 0===t&&(t=0),d(e.length>=1,"Pass at least one tensor to concat");var n=Xe(e,"tensors","concat"),r=Ue(n.map(function(e){return e.shape}),t);if(0===b(r))return tt([],r);if(1===(n=n.filter(function(e){return e.size>0})).length)return n[0];var a=Le(t,n[0].shape)[0],i=n.map(function(e){return e.shape});!function(e,t){var n=e[0].length;e.forEach(function(e,t){d(e.length===n,"Error in concat"+n+"D: rank of tensors["+t+"] must be the same as the rank of the rest ("+n+")")}),d(t>=0&&t>>0,t=(r*=t)>>>0,t+=4294967296*(r-=t)}return 2.3283064365386963e-10*(t>>>0)});n.next=function(){var e=2091639*n.s0+2.3283064365386963e-10*n.c;return n.s0=n.s1,n.s1=n.s2,n.s2=e-(n.c=0|e)},n.c=1,n.s0=r(" "),n.s1=r(" "),n.s2=r(" "),n.s0-=r(e),n.s0<0&&(n.s0+=1),n.s1-=r(e),n.s1<0&&(n.s1+=1),n.s2-=r(e),n.s2<0&&(n.s2+=1),r=null}(e),a=t&&t.state,i=n.next;return i.int32=function(){return 4294967296*n.next()|0},i.double=function(){return i()+1.1102230246251565e-16*(2097152*i()|0)},i.quick=i,a&&("object"==typeof a&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.alea=a}(0,e)}),Er=_r(function(e){!function(e,t,n){function r(e,t){return t.x=e.x,t.y=e.y,t.z=e.z,t.w=e.w,t}function a(e,t){var n=new function(e){var t=this,n="";t.x=0,t.y=0,t.z=0,t.w=0,t.next=function(){var e=t.x^t.x<<11;return t.x=t.y,t.y=t.z,t.z=t.w,t.w^=t.w>>>19^e^e>>>8},e===(0|e)?t.x=e:n+=e;for(var r=0;r>>0)/4294967296};return i.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===e);return e},i.int32=n.next,i.quick=i,a&&("object"==typeof a&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.xor128=a}(0,e)}),Nr=_r(function(e){!function(e,t,n){function r(e,t){return t.x=e.x,t.y=e.y,t.z=e.z,t.w=e.w,t.v=e.v,t.d=e.d,t}function a(e,t){var n=new function(e){var t=this,n="";t.next=function(){var e=t.x^t.x>>>2;return t.x=t.y,t.y=t.z,t.z=t.w,t.w=t.v,(t.d=t.d+362437|0)+(t.v=t.v^t.v<<4^e^e<<1)|0},t.x=0,t.y=0,t.z=0,t.w=0,t.v=0,e===(0|e)?t.x=e:n+=e;for(var r=0;r>>4),t.next()}(e),a=t&&t.state,i=function(){return(n.next()>>>0)/4294967296};return i.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===e);return e},i.int32=n.next,i.quick=i,a&&("object"==typeof a&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.xorwow=a}(0,e)}),Or=_r(function(e){!function(e,t,n){function r(e,t){return t.x=e.x.slice(),t.i=e.i,t}function a(e,t){null==e&&(e=+new Date);var n=new function(e){var t=this;t.next=function(){var e,n,r=t.x,a=t.i;return e=r[a],n=(e^=e>>>7)^e<<24,n^=(e=r[a+1&7])^e>>>10,n^=(e=r[a+3&7])^e>>>3,n^=(e=r[a+4&7])^e<<7,e=r[a+7&7],n^=(e^=e<<13)^e<<9,r[a]=n,t.i=a+1&7,n},function(e,t){var n,r=[];if(t===(0|t))r[0]=t;else for(t=""+t,n=0;n0;--n)e.next()}(t,e)}(e),a=t&&t.state,i=function(){return(n.next()>>>0)/4294967296};return i.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===e);return e},i.int32=n.next,i.quick=i,a&&(a.x&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.xorshift7=a}(0,e)}),kr=_r(function(e){!function(e,t,n){function r(e,t){return t.i=e.i,t.w=e.w,t.X=e.X.slice(),t}function a(e,t){null==e&&(e=+new Date);var n=new function(e){var t=this;t.next=function(){var e,n,r=t.w,a=t.X,i=t.i;return t.w=r=r+1640531527|0,n=a[i+34&127],e=a[i=i+1&127],n^=n<<13,e^=e<<17,n^=n>>>15,e^=e>>>12,n=a[i]=n^e,t.i=i,n+(r^r>>>16)|0},function(e,t){var n,r,a,i,o,s=[],u=128;for(t===(0|t)?(r=t,t=null):(t+="\0",r=0,u=Math.max(u,t.length)),a=0,i=-32;i>>15,r^=r<<4,r^=r>>>13,i>=0&&(o=o+1640531527|0,a=0==(n=s[127&i]^=r+o)?a+1:0);for(a>=128&&(s[127&(t&&t.length||0)]=-1),a=127,i=512;i>0;--i)r=s[a+34&127],n=s[a=a+1&127],r^=r<<13,n^=n<<17,r^=r>>>15,n^=n>>>12,s[a]=r^n;e.w=o,e.X=s,e.i=a}(t,e)}(e),a=t&&t.state,i=function(){return(n.next()>>>0)/4294967296};return i.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===e);return e},i.int32=n.next,i.quick=i,a&&(a.X&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.xor4096=a}(0,e)}),Ir=_r(function(e){!function(e,t,n){function r(e,t){return t.a=e.a,t.b=e.b,t.c=e.c,t.d=e.d,t}function a(e,t){var n=new function(e){var t=this,n="";t.next=function(){var e=t.b,n=t.c,r=t.d,a=t.a;return e=e<<25^e>>>7^n,n=n-r|0,r=r<<24^r>>>8^a,a=a-e|0,t.b=e=e<<20^e>>>12^n,t.c=n=n-r|0,t.d=r<<16^n>>>16^a,t.a=a-e|0},t.a=0,t.b=0,t.c=-1640531527,t.d=1367130551,e===Math.floor(e)?(t.a=e/4294967296|0,t.b=0|e):n+=e;for(var r=0;r>>0)/4294967296};return i.double=function(){do{var e=((n.next()>>>11)+(n.next()>>>0)/4294967296)/(1<<21)}while(0===e);return e},i.int32=n.next,i.quick=i,a&&("object"==typeof a&&r(a,n),i.state=function(){return r(n,{})}),i}t&&t.exports?t.exports=a:this.tychei=a}(0,e)}),Cr=_r(function(e){!function(t,r){var a,i=this,o=256,s=6,u="random",l=r.pow(o,s),c=r.pow(2,52),f=2*c,p=o-1;function h(e,n,h){var y=[],v=m(function e(t,n){var r,a=[],i=typeof t;if(n&&"object"==i)for(r in t)try{a.push(e(t[r],n-1))}catch(e){}return a.length?a:"string"==i?t:t+"\0"}((n=1==n?{entropy:!0}:n||{}).entropy?[e,g(t)]:null==e?function(){try{var e;return a&&(e=a.randomBytes)?e=e(o):(e=new Uint8Array(o),(i.crypto||i.msCrypto).getRandomValues(e)),g(e)}catch(e){var n=i.navigator,r=n&&n.plugins;return[+new Date,i,r,i.screen,g(t)]}}():e,3),y),b=new function(e){var t,n=e.length,r=this,a=0,i=r.i=r.j=0,s=r.S=[];for(n||(e=[n++]);a=f;)e/=2,t/=2,n>>>=1;return(e+n)/t};return w.int32=function(){return 0|b.g(4)},w.quick=function(){return b.g(4)/4294967296},w.double=w,m(g(b.S),t),(n.pass||h||function(e,t,n,a){return a&&(a.S&&d(a,b),e.state=function(){return d(b,{})}),n?(r[u]=e,t):e})(w,v,"global"in n?n.global:this==r,n.state)}function d(e,t){return t.i=e.i,t.j=e.j,t.S=e.S.slice(),t}function m(e,t){for(var n,r=e+"",a=0;a=1||0===o);var s=Math.sqrt(-2*Math.log(o)/o);t=this.mean+this.stdDev*a*s,n=this.mean+this.stdDev*i*s,this.truncated&&!this.isValidTruncated(t)||(r=!0)}return this.truncated&&!this.isValidTruncated(n)||(this.nextVal=this.convertValue(n)),this.convertValue(t)},e.prototype.convertValue=function(e){return null==this.dtype||"float32"===this.dtype?e:Math.round(e)},e.prototype.isValidTruncated=function(e){return e<=this.upper&&e>=this.lower},e}();function Tr(e,t){return l(this,void 0,void 0,function(){var n,r,a,i,o,s,u,l,f,p,h,d,m,g,y,v,b,w,x,_;return c(this,function(c){switch(c.label){case 0:if(2!==(n=Ke(e,"img","toPixels","int32")).rank&&3!==n.rank)throw new Error("toPixels only supports rank 2 or 3 tensors, got rank "+n.rank+".");if(r=n.shape.slice(0,2),a=r[0],i=r[1],(o=2===n.rank?1:n.shape[2])>4||2===o)throw new Error("toPixels only supports depth of size 1, 3 or 4 but got "+o);return s=n.min(),u=n.max(),[4,s.data()];case 1:return l=c.sent()[0],[4,u.data()];case 2:if(f=c.sent()[0],s.dispose(),u.dispose(),"float32"===n.dtype){if(l<0||f>1)throw new Error("Tensor values for a float32 Tensor must be in the range [0 - 1] but got range ["+l+" - "+f+"].")}else{if("int32"!==n.dtype)throw new Error("Unsupported type for toPixels: "+n.dtype+". Please use float32 or int32 tensors.");if(l<0||f>255)throw new Error("Tensor values for a int32 Tensor must be in the range [0 - 255] but got range ["+l+" - "+f+"].")}return[4,n.data()];case 3:for(p=c.sent(),h="float32"===n.dtype?255:1,d=new Uint8ClampedArray(i*a*4),m=0;m=1+t.length,"input rank should be > than [blockShape] but got "+r.rank),d(n.length===t.length,"crops.shape[0] must be equal to [blockShape] but got "+n.length),d(r.shape[0]%a==0,"input tensor batch must be divisible by prod( blockShape )"),me.engine.runKernel(function(e){return e.batchToSpaceND(r,t,n)},{$x:r},function(e){return{$x:function(){return e.spaceToBatchND(t,n)}}})}}),jr=Ye({cast_:function(e,t){var n=Ke(e,"x","cast");return me.engine.runKernel(function(e){return e.cast(n,t)},{$x:n},function(e){return{$x:function(){return e.clone()}}})}}),Lr=Ye({clone_:function(e){var t=Ke(e,"x","clone");return me.engine.runKernel(function(e){return Q.make(t.shape,{dataId:t.dataId},t.dtype)},{$x:t},function(e){return{$x:function(){return e.toFloat()}}})}}),zr=Ye({cumsum_:function(e,t,n,r){void 0===t&&(t=0),void 0===n&&(n=!1),void 0===r&&(r=!1);var a=Ke(e,"x","cumsum"),i=Be([t|=0],a.rank),o=a;null!=i&&(o=a.transpose(i));var s=Ve(1,a.rank)[0],u=me.engine.runKernel(function(e){return e.cumsum(o,s,n,r)},{permutedX:o},function(e){return{permutedX:function(){return e.cumsum(t,n,!r)}}});return null!=i&&(u=u.transpose(i)),u}}),Br=Ye({depthToSpace_:function(e,t,n){void 0===n&&(n="NHWC");var r=Ke(e,"x","depthToSpace"),a="NHWC"===n?r.shape[1]:r.shape[2],i="NHWC"===n?r.shape[2]:r.shape[3],o="NHWC"===n?r.shape[3]:r.shape[1];return d(a*t>=0,"Negative dimension size caused by overflow when multiplying\n "+a+" and "+t+" for depthToSpace with input shape\n "+r.shape),d(i*t>=0,"Negative dimension size caused by overflow when multiplying\n "+i+" and "+t+" for depthToSpace with input shape\n "+r.shape),d(o%(t*t)==0,"Dimension size must be evenly divisible by "+t*t+" but is "+o+" for depthToSpace with input shape "+r.shape),me.engine.runKernel(function(e){return e.depthToSpace(r,t,n)},{$x:r})}}),Fr=Ye({expandDims_:function(e,t){void 0===t&&(t=0);var n=Ke(e,"x","expandDims");d(t<=n.rank,"Axis must be <= rank of the tensor");var r=n.shape.slice();return t<0&&(d(-(n.rank+1)<=t,"Axis must be in the interval ["+-(n.rank+1)+", "+n.rank+"]"),t=n.rank+t+1),r.splice(t,0,1),Qr(n,r)}}),Vr=Ye({eye_:function(e,t,n,r){void 0===r&&(r="float32"),null==t&&(t=e);for(var a=Mr([e,t],r),i=e<=t?e:t,o=0;o4)throw new Error("Cannot construct Tensor with more than 4 channels from pixels.");return me.engine.fromPixels(e,t)}}),Wr=Ye({multinomial_:function(e,t,n,r){void 0===r&&(r=!1);var a=Ke(e,"logits","multinomial"),i=a.size,o=a.rank;if(i<2)throw new Error("Error in multinomial: you need at least 2 outcomes, but got "+i+".");if(o>2)throw new Error("Rank of probabilities must be 1 or 2, but is "+o);n=n||Math.random();var s=1===o?a.as2D(1,-1):a,u=me.engine.runKernel(function(e){return e.multinomial(s,r,t,n)},{logits2D:s});return 1===o?u.as1D():u}}),qr=Ye({oneHot_:function(e,t,n,r){void 0===n&&(n=1),void 0===r&&(r=0);var a=Ke(e,"indices","oneHot","int32");if(d("int32"===a.dtype,"Indices must be of dtype `int32`"),t<2)throw new Error("Error in oneHot: depth must be >=2, but it is "+t);return me.engine.runKernel(function(e){return e.oneHot(a,t,n,r)},{$indices:a},function(e){return{$indices:function(){return xt(a)}}})}}),$r=Ye({pad_:function(e,t,n){void 0===n&&(n=0);var r=Ke(e,"x","pad");if(0===r.rank)throw new Error("pad(scalar) is not defined. Pass non-scalar to pad");var a=t.map(function(e){return e[0]});return me.engine.runKernel(function(e){return e.pad(r,t,n)},{$x:r},function(e){return{$x:function(){return e.slice(a,r.shape)}}})}}),Gr=Ye({pad1d_:function(e,t,n){return void 0===n&&(n=0),d(2===t.length,"Invalid number of paddings. Must be length of 2."),$r(e,[t],n)}}),Hr=Ye({pad2d_:function(e,t,n){return void 0===n&&(n=0),d(2===t.length&&2===t[0].length&&2===t[1].length,"Invalid number of paddings. Must be length of 2 each."),$r(e,t,n)}}),Kr=Ye({pad3d_:function(e,t,n){return void 0===n&&(n=0),d(3===t.length&&2===t[0].length&&2===t[1].length&&2===t[2].length,"Invalid number of paddings. Must be length of 2 each."),$r(e,t,n)}}),Xr=Ye({pad4d_:function(e,t,n){return void 0===n&&(n=0),d(4===t.length&&2===t[0].length&&2===t[1].length&&2===t[2].length&&2===t[3].length,"Invalid number of paddings. Must be length of 2 each."),$r(e,t,n)}}),Yr=Ye({rand_:function(e,t,n){var r=b(e),a=null;if(null==n||"float32"===n)a=new Float32Array(r);else if("int32"===n)a=new Int32Array(r);else{if("bool"!==n)throw new Error("Unknown data type "+n);a=new Uint8Array(r)}for(var i=0;i=1+t.length,"input rank "+r.rank+" should be > than [blockShape] "+t.length),d(n.length===t.length,"paddings.shape[0] "+n.length+" must be equal to [blockShape] "+t.length),d(r.shape.reduce(function(e,r,a){return a>0&&a<=t.length?e&&(r+n[a-1][0]+n[a-1][1])%t[a-1]==0:e},!0),"input spatial dimensions "+r.shape.slice(1)+" with paddings "+n.toString()+" must be divisible by blockShapes "+t.toString()),me.engine.runKernel(function(e){return e.spaceToBatchND(r,t,n)},{$x:r},function(e){return{$x:function(){return e.batchToSpaceND(t,n)}}})}}),ta=Ye({squeeze_:function(e,t){var n=Ke(e,"x","squeeze");return Qr(n,k(n.shape,t).newShape)}}),na=Ye({stack_:function(e,t){void 0===t&&(t=0);var n=Xe(e,"tensors","stack");if(d(n.length>=1,"Pass at least one tensor to tf.stack"),1===n.length)return n[0].expandDims(t);var r=n[0].rank,a=n[0].shape,i=n[0].dtype;d(t<=r,"Axis must be <= rank of the tensor"),n.forEach(function(e){m(a,e.shape,"All tensors passed to stack must have matching shapes")}),n.forEach(function(e){d(i===e.dtype,"All tensors passed to stack must have matching dtypes")});var o=n.map(function(e){return e.expandDims(t)});return gr(o,t)}}),ra=Ye({tile_:function(e,t){var n=Ke(e,"x","tile");return d(n.rank===t.length,"Error in transpose: rank of input "+n.rank+" must match length of reps "+t+"."),me.engine.runKernel(function(e){return e.tile(n,t)},{$x:n},function(e){return{$x:function(){var r=xt(n);if(1===n.rank)for(var a=0;a FLOAT_MAX) {\n return vec4(0.0, 0.0, 128.0, 127.0) / 255.0;\n } else if(v < -FLOAT_MAX) {\n return vec4(0.0, 0.0, 128.0, 255.0) / 255.0;\n }\n\n highp vec4 c = vec4(0,0,0,0);\n\n highp float e = floor(log2(av));\n highp float m = exp2(fract(log2(av))) - 1.0;\n\n c[2] = floor(128.0 * m);\n m -= c[2] / 128.0;\n c[1] = floor(32768.0 * m);\n m -= c[1] / 32768.0;\n c[0] = floor(8388608.0 * m);\n\n highp float ebias = e + 127.0;\n c[3] = floor(ebias / 2.0);\n ebias -= c[3] * 2.0;\n c[2] += floor(ebias) * 128.0;\n\n c[3] += 128.0 * step(0.0, -v);\n\n return c / 255.0;\n }\n\n void main() {\n float x = getAAtOutCoords();\n gl_FragColor = encode_float(x);\n }\n "}(a);this.compileAndRun(s,[o],i,null,!1);var u=this.texData.get(i.dataId),l=this.gpgpu.downloadByteEncodedFloatMatrixFromOutputTexture(u.texture,u.texShape[0],u.texShape[1]);return o.dispose(),i.dispose(),l},e.prototype.time=function(e){return l(this,void 0,void 0,function(){var t,n,r,a,i,o;return c(this,function(s){switch(s.label){case 0:return t=this.activeTimers,n=[],r=!1,null==this.programTimersStack?(this.programTimersStack=n,r=!0):this.activeTimers.push(n),this.activeTimers=n,e(),a=y(this.activeTimers),this.activeTimers=t,r&&(this.programTimersStack=null),[4,Promise.all(a).then(function(e){var t=0;return e.forEach(function(e){return t+=e}),t})];case 1:return i=s.sent(),o={uploadWaitMs:this.uploadWaitMs,downloadWaitMs:this.downloadWaitMs,kernelMs:i,wallMs:null},this.uploadWaitMs=0,this.downloadWaitMs=0,[2,o]}})})},e.prototype.memory=function(){return{unreliable:!1,numBytesInGPU:this.numBytesInGPU}},e.prototype.startTimer=function(){return me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?this.gpgpu.beginQuery():{startMs:performance.now(),endMs:null}},e.prototype.endTimer=function(e){return me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?(this.gpgpu.endQuery(),e):(e.endMs=performance.now(),e)},e.prototype.getQueryTime=function(e){return l(this,void 0,void 0,function(){var t;return c(this,function(n){return me.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?[2,this.gpgpu.waitForQueryAndGetTime(e)]:[2,(t=e).endMs-t.startMs]})})},e.prototype.disposeData=function(e){if(!this.pendingDisposal.has(e))if(this.pendingRead.has(e))this.pendingDisposal.add(e);else if(this.texData.has(e)){var t=this.texData.get(e),n=t.texture,r=t.texShape,a=t.usage,i=t.complexTensors;null!=n&&this.releaseTexture(e,n,r,a),null!=i&&(i.real.dispose(),i.imag.dispose()),this.texData.delete(e)}},e.prototype.getTexture=function(e){return this.uploadToGPU(e),this.texData.get(e).texture},e.prototype.getGPGPUContext=function(){return this.gpgpu},e.prototype.getCanvas=function(){return this.canvas},e.prototype.complex=function(e,t){var n=Q.make(e.shape,{},"complex64");return this.texData.get(n.dataId).complexTensors={real:me.engine.keep(e.clone()),imag:me.engine.keep(t.clone())},n},e.prototype.real=function(e){return this.texData.get(e.dataId).complexTensors.real.clone()},e.prototype.imag=function(e){return this.texData.get(e.dataId).complexTensors.imag.clone()},e.prototype.slice=function(e,t,n){var r=new ur(n),a=r.getCustomSetupFunc(t);return this.compileAndRun(r,[e],null,a)},e.prototype.stridedSlice=function(e,t,n,r,a,i,o,s,u){var l=$e(e.shape,t,n,r,a,i,o,s,u),c=l[0],f=l[1],p=l[2],h=f.filter(function(e,t){return-1===p.indexOf(t)});if(h.some(function(e){return 0===e}))return tt([],h);var d=new function(e,t,n,r){this.variableNames=["x"];var a=n.filter(function(e,t){return-1===r.indexOf(t)});this.outputShape=a;var i=n.length,o=Wt(n.length),s=Wt(a.length),u="";if(1===i)u="coords * strides + begin";else{var l=0;u=n.map(function(e,t){return-1===r.indexOf(t)?(l++,1===a.length?"coords * strides["+t+"] + begin["+t+"]":"coords["+(l-1)+"] * strides["+t+"] + begin["+t+"]"):"begin["+t+"]"}).join(",")}this.userCode="\n "+o+" begin = "+o+"("+e+");\n "+o+" strides = "+o+"("+t+");\n\n void main() {\n "+s+" coords = getOutputCoords();\n setOutput(getX("+u+"));\n }\n "}(c,r,f,p);return this.compileAndRun(d,[e])},e.prototype.reverse=function(e,t){var n=new function(e,t){this.variableNames=["x"];var n=e.length;if(n>4)throw new Error("WebGL backend: Reverse of rank-"+n+" tensor is not yet supported");if(this.outputShape=e,1!==n){var r=e.map(function(n,r){return function(n){return-1!==t.indexOf(n)&&1!==e[n]?e[n]+" - coords["+n+"] - 1":"coords["+n+"]"}(r)}).join(","),a=Wt(n);this.userCode="\n void main() {\n "+a+" coords = getOutputCoords();\n setOutput(getX("+r+"));\n }\n "}else this.userCode="\n void main() {\n int coord = getOutputCoords();\n setOutput(getX("+e[0]+" - coord - 1));\n }\n "}(e.shape,t);return this.compileAndRun(n,[e])},e.prototype.concat2Tensors=function(e,t,n){var r=Ue([e.shape,t.shape],n),a=e.as2D(-1,b(e.shape.slice(n))),i=t.as2D(-1,b(t.shape.slice(n))),o=new function(e,t){this.variableNames=["A","B"],this.outputShape=[],this.outputShape=Ue([e,t],1),this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int yR = coords.x;\n int yC = coords.y;\n\n float value = 0.0;\n if (yC < "+e[1]+") {\n value = getA(yR, yC);\n } else {\n yC -= "+e[1]+";\n value = getB(yR, yC);\n }\n\n setOutput(value);\n }\n "}(a.shape,i.shape);return this.compileAndRun(o,[a,i]).reshape(r)},e.prototype.concat=function(e,t){if(1===e.length)return e[0];for(var n=e[0],r=1;r= 0 && idx <= "+s+") {\n float z = getX(b, r, c, idx);\n sum += z * z;\n }\n }\n float val = x * "+i+";\n setOutput(val);\n }\n "}(e.shape,t,n,r,a);return this.compileAndRun(i,[e])},e.prototype.LRNGrad=function(e,t,n,r,a,i,o){var s=new function(e,t,n,r,a){this.variableNames=["inputImage","outputImage","dy"],this.outputShape=[],this.outputShape=e,this.depth=e[3],this.depthRadius=t,this.bias=n,this.alpha=r,this.beta=a,this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int r = coords[1];\n int c = coords[2];\n\n float result = 0.0;\n for (int d = 0; d < "+this.depth+"; ++d) {\n int depthBegin = int(max(0.0, float(d - "+t+")));\n int depthEnd = int(min(float("+this.depth+"),\n float(d + "+t+" + 1)));\n\n const int MIN_DEPTH_BEGIN = 0;\n const int MAX_DEPTH_END = "+this.depth+";\n\n float norm = 0.0;\n for (int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k) {\n if (k < depthBegin){\n continue;\n }\n else if (k >= depthBegin && k < depthEnd) {\n norm += getInputImage(b, r, c, k) * getInputImage(b, r, c, k);\n }\n else {\n break;\n }\n }\n\n norm = float("+r+") * norm + float("+n+");\n\n for(int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k){\n if (k < depthBegin){\n continue;\n }\n else if (k >= depthBegin && k < depthEnd){\n float dyi = -2.0 * float("+r+")\n * float("+a+")\n * getInputImage(b ,r ,c, k) * getOutputImage(b, r, c, d)\n / norm;\n if (k == d) {\n dyi += pow(norm, -1.0 * "+a+");\n }\n if (k == coords[3]) {\n dyi *= getDy(b, r, c, d);\n result += dyi;\n }\n }\n else {\n break;\n }\n }\n }\n setOutput(result);\n }\n "}(t.shape,r,a,i,o);return this.compileAndRun(s,[t,n,e])},e.prototype.tile=function(e,t){var n=new function(e,t){this.variableNames=["A"];for(var n=new Array(e.length),r=0;r5)throw Error("Tile for rank "+t+" is not yet supported");if(1===t)return"imod(resRC, "+e[0]+")";for(var n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u"],r=[],a=0;a= end) {\n setOutput(float("+n+"));\n } else {\n setOutput(getX(outC - start));\n }\n }\n "}(e.shape,t,n);return this.compileAndRun(r,[e])},e.prototype.transpose=function(e,t){var n=new function(e,t){this.variableNames=["A"];for(var n=new Array(e.length),r=0;r6)throw Error("Transpose for rank "+t+" is not yet supported");for(var n=["resRC.x","resRC.y","resRC.z","resRC.w","resRC.u","resRC.v"],r=new Array(t),a=0;a4)throw Error("Gather for rank "+n+" is not yet supported");if(1===n)return"int(getIndices(resRC))";for(var r=["resRC.x","resRC.y","resRC.z","resRC.w"],a=[],i=0;i 4 with a WebGL backend not implemented yet");var r=t.reduce(function(e,t){return e*t}),a=Ce(e.shape,t,r),i=Ae(a.length,t.length),o=Pe(e.shape,t,r),s=Te(n,t.length),u=Me(o,n,t.length);return e.reshape(a).transpose(i).reshape(o).slice(s,u)},e.prototype.spaceToBatchND=function(e,t,n){d(e.rank<=4,"spaceToBatchND for rank > 4 with a WebGL backend not implemented yet");var r=t.reduce(function(e,t){return e*t}),a=[[0,0]];a.push.apply(a,n);for(var i=1+t.length;i= 1.0 && floatedReducedAllValue >= 1.0);\n ",p="bvec4"):"any"===t&&(o="0.0",f="\n bool reducedAnyValue = any(values);\n float floatedReducedAnyValue = float(reducedAnyValue);\n anyValue = float(anyValue >= 1.0 || floatedReducedAnyValue >= 1.0);\n ",p="bvec4");var h="";a%n>0&&(h="\n if (inIdx < 0 || inIdx >= "+a+") {\n return initializationValue;\n }\n "),this.userCode="\n const float initializationValue = "+o+";\n const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);\n\n float getValue(int batch, int inIdx) {\n "+h+"\n return getX(batch, inIdx);\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = outIdx * "+n+";\n\n vec4 minMaxValue = vec4("+o+");\n float sumValue = 0.0;\n float allValue = 1.0;\n float anyValue = 0.0;\n\n for (int i = 0; i < "+l+"; i += 4) {\n int inIdx = inOffset + i;\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n "+f+"\n }\n\n int inIdx = inOffset + "+l+";\n if ("+(1===c)+") {\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(2===c)+") {\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n "+f+"\n } else if ("+(3===c)+") {\n "+p+" values = "+p+"(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n "+f+"\n }\n setOutput("+u+");\n }\n "}({windowSize:qe(a),inSize:a,batchSize:r},t),o=i.outputShape,s=o[0],u=o[1],l=this.makeOutputArray([s,u],n);return this.compileAndRun(i,[e],l),1===l.shape[1]?l:this.reduce(l,t,n)},e.prototype.argReduce=function(e,t,n){void 0===n&&(n=null);var r=e.shape[0],a=e.shape[1];null!=n&&(r=n.shape[0],a=n.shape[1]);var i=new function(e,t,n){this.variableNames=["A"];var r=e.windowSize,a=e.batchSize,i=e.inSize,o=Math.ceil(i/r);n||this.variableNames.push("bestIndicesA"),this.outputShape=[a,o];var s="max"===t?">":"<",u=n?"inOffset + i;":"round(getBestIndicesA(batch, inOffset + i));";this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = outIdx * "+r+";\n\n int bestIndex = 0;\n float bestValue = getA(batch, inOffset);\n\n for (int i = 0; i < "+r+"; i++) {\n int inIdx = "+u+";\n float candidate = getA(batch, inIdx);\n if (candidate "+s+" bestValue) {\n bestValue = candidate;\n bestIndex = inIdx;\n }\n }\n setOutput(float(bestIndex));\n }\n "}({windowSize:qe(a),inSize:a,batchSize:r},t,null==n),o=i.outputShape,s=o[0],u=o[1],l=this.makeOutputArray([s,u],"int32"),c=[e];return null!=n&&c.push(n),this.compileAndRun(i,c,l),1===l.shape[1]?l:this.argReduce(e,t,l)},e.prototype.sum=function(e,t){ze("sum",t,e.rank);var n=De(e.shape,t),r=n[0],a=b(n[1]),i=e.as2D(-1,a),o=Et(e.dtype);return this.reduce(i,"sum",o).reshape(r)},e.prototype.unsortedSegmentSum=function(e,t,n){var r=0,a=Be([r],e.rank),i=e;null!=a&&(i=e.transpose(a),r=Ve(1,e.rank)[0]);var o=function(e,t,n){for(var r=[],a=e.length,i=0;it||n===e){r=!0;break}n=D(e,n+1)}return n}(o,a),u=new function(e,t){this.variableNames=["x","segmentIds"];var n=e.windowSize,r=e.batchSize,a=e.inSize,i=e.numSegments,o=i*Math.ceil(a/n);this.outputShape=[r,o];var s=4*Math.floor(n/4),u=n%4,l="\n sumValue += dot(values, filter);\n ",c="";a%n>0&&(c="\n if (inIdx < 0 || inIdx >= "+a+") {\n return initializationValue;\n }\n ");var f="";a%n>0&&(f="\n if (inIdx < 0 || inIdx >= "+a+") {\n return -1.0;\n }\n "),this.userCode="\n const float initializationValue = 0.0;\n\n float getValue(int batch, int inIdx) {\n "+c+"\n return getX(batch, inIdx);\n }\n\n float getSegmentIdAtIndex(int inIdx) {\n "+f+"\n return getSegmentIds(inIdx);\n }\n\n void main() {\n ivec2 coords = getOutputCoords();\n int batch = coords[0];\n int outIdx = coords[1];\n int inOffset = int(floor(float(outIdx) / float(\n "+i+")) * float("+n+"));\n int currentSeg = int(mod(float(outIdx), float("+i+")));\n\n float sumValue = 0.0;\n\n for (int i = 0; i < "+s+"; i += 4) {\n int inIdx = inOffset + i;\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n getValue(batch, inIdx + 3)\n );\n\n vec4 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 3)) == currentSeg ? 1 : 0\n );\n\n "+l+"\n }\n\n int inIdx = inOffset + "+s+";\n if ("+(1===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n initializationValue,\n initializationValue,\n initializationValue\n );\n\n int inIdxSeg = int(getSegmentIdAtIndex(inIdx));\n\n vec4 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n 0,\n 0,\n 0\n );\n\n "+l+"\n } else if ("+(2===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n initializationValue,\n initializationValue\n );\n\n vec4 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n 0,\n 0\n );\n\n "+l+"\n } else if ("+(3===u)+") {\n vec4 values = vec4(\n getValue(batch, inIdx),\n getValue(batch, inIdx + 1),\n getValue(batch, inIdx + 2),\n initializationValue\n );\n\n vec4 filter = vec4(\n int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,\n int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,\n 0\n );\n\n "+l+"\n }\n setOutput(sumValue);\n }\n "}({windowSize:s,inSize:o,batchSize:i,numSegments:a},t),l=u.outputShape,c=l[0],f=l[1],p=this.makeOutputArray([c,f],r);return this.compileAndRun(u,[e,n],p),p.shape[1]===a?p:(n=ht(0,a).tile([o/s]),this.segOpCompute(p,t,n,r,a))},e.prototype.argMin=function(e,t){var n=[t];ze("argMin",n,e.rank);var r=De(e.shape,n),a=r[0],i=b(r[1]),o=e.as2D(-1,i);return this.argReduce(o,"min").reshape(a)},e.prototype.argMax=function(e,t){var n=[t];ze("argMax",n,e.rank);var r=De(e.shape,n),a=r[0],i=b(r[1]),o=e.as2D(-1,i);return this.argReduce(o,"max").reshape(a)},e.prototype.cumsum=function(e,t,n,r){if(t!==e.rank-1)throw new Error("WebGL cumsum shader expects an inner-most axis="+(e.rank-1)+" but got axis="+t);var a=new function(e,t,n){this.variableNames=["x"],this.outputShape=e;var r=e.length,a=e[e.length-1],i=n?"<":">";this.userCode="\n int getIndex(int i) {\n "+(n?"return "+a+" -i - 1;":"return i;")+"\n }\n\n void main() {\n "+Wt(r)+" coords = getOutputCoords();\n int end = "+Gt(r,"coords")+";\n float val = 0.0;\n for (int i = "+a+" - 1; i >= 0; i -= 1) {\n int idx = getIndex(i);\n if (idx "+i+" end) {\n continue;\n }\n if (idx == end && "+t+") {\n continue;\n }\n "+Gt(r,"coords")+" = idx;\n val += getX("+function(e,t){if(1===e)return""+t;if(2===e)return t+".x, "+t+".y";if(3===e)return t+".x, "+t+".y, "+t+".z";if(4===e)return t+".x, "+t+".y, "+t+".z, "+t+".w";throw Error("Cumulative sum for rank "+e+" is not yet supported")}(r,"coords")+");\n }\n setOutput(val);\n }\n "}(e.shape,n,r);return this.compileAndRun(a,[e])},e.prototype.equal=function(e,t){var n=new zt("return float(a == b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.notEqual=function(e,t){var n=new zt("return float(a != b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.less=function(e,t){var n=new zt("return float(a < b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.lessEqual=function(e,t){var n=new zt("return float(a <= b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.greater=function(e,t){var n=new zt("return float(a > b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.greaterEqual=function(e,t){var n=new zt("return float(a >= b);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.logicalNot=function(e){var t=new dr(e.shape,"return float(!(x >= 1.0));");return this.compileAndRun(t,[e])},e.prototype.logicalAnd=function(e,t){var n=new zt("return float(a >= 1.0 && b >= 1.0);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.logicalOr=function(e,t){var n=new zt("return float(a >= 1.0 || b >= 1.0);",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"bool");return this.compileAndRun(n,[e,t],r)},e.prototype.select=function(e,t,n){var r=new function(e,t,n){var r,a;if(this.variableNames=["c","a","b"],this.outputShape=t,n>4)throw Error("Where for rank "+n+" is not yet supported");if(1===n)a="resRC",r="resRC";else{for(var i=["resRC.x","resRC.y","resRC.z","resRC.w"],o=[],s=[],u=0;u= 1.0) {\n setOutput(getA("+a+"));\n } else {\n setOutput(getB("+a+"));\n }\n }\n "}(e.rank,t.shape,t.rank),a=this.makeOutputArray(r.outputShape,St(t.dtype,n.dtype));return this.compileAndRun(r,[e,t,n],a)},e.prototype.where=function(e){Ie("tf.where() in webgl locks the UI thread. Call tf.whereAsync() instead");var t=e.dataSync();return oa(e.shape,t)},e.prototype.topk=function(e,t,n){return Pt(e.dataSync(),e.shape,e.dtype,t)},e.prototype.min=function(e,t){ze("min",t,e.rank);var n=De(e.shape,t),r=n[0],a=b(n[1]),i=e.as2D(-1,a);return this.reduce(i,"min",i.dtype).reshape(r)},e.prototype.minimum=function(e,t){var n=new zt("\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n\n return min(a, b);\n",e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.mod=function(e,t){var n=new zt("if (b == 0.0) return NAN;\n return mod(a, b);",e.shape,t.shape),r=n.getCustomSetupFunc();return this.compileAndRun(n,[e,t],null,r)},e.prototype.max=function(e,t){ze("max",t,e.rank);var n=De(e.shape,t),r=n[0],a=b(n[1]),i=e.as2D(-1,a);return this.reduce(i,"max",i.dtype).reshape(r)},e.prototype.maximum=function(e,t){var n=new zt("\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n\n return max(a, b);\n",e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.all=function(e,t){ze("all",t,e.rank);var n=De(e.shape,t),r=n[0],a=b(n[1]),i=e.as2D(-1,a);return this.reduce(i,"all",i.dtype).reshape(r)},e.prototype.any=function(e,t){ze("any",t,e.rank);var n=De(e.shape,t),r=n[0],a=b(n[1]),i=e.as2D(-1,a);return this.reduce(i,"any",i.dtype).reshape(r)},e.prototype.squaredDifference=function(e,t){var n=new zt("return (a - b) * (a - b);",e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.realDivide=function(e,t){var n=new zt("if (a == b) return 1.0;\n return a / b;",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"float32");return this.compileAndRun(n,[e,t],r)},e.prototype.floorDiv=function(e,t){var n=new zt("\n float resultSign = sign(a) * sign(b);\n int ia = round(a);\n int ib = round(b);\n int result = ia / ib;\n int amodb = ia - ib * result;\n\n if (resultSign < 0.0 && amodb != 0) {\n result -= 1;\n }\n return float(result);\n",e.shape,t.shape),r=this.makeOutputArray(n.outputShape,"int32");return this.compileAndRun(n,[e,t],r)},e.prototype.add=function(e,t){if("complex64"===e.dtype&&"complex64"===t.dtype)return this.complexSeparableBinaryOp(e,t,jt);var n=new zt(jt,e.shape,t.shape),r=this.makeOutputArray(n.outputShape,St(e.dtype,t.dtype));return this.compileAndRun(n,[e,t],r)},e.prototype.complexSeparableBinaryOp=function(e,t,n){var r=this,a=this.texData.get(e.dataId),i=this.texData.get(t.dataId),o=[[a.complexTensors.real,i.complexTensors.real],[a.complexTensors.imag,i.complexTensors.imag]].map(function(a){var i=a[0],o=a[1],s=new zt(n,e.shape,t.shape),u=r.makeOutputArray(s.outputShape,St(i.dtype,o.dtype)),l=r.makeComplexComponentTensorHandle(e,i),c=r.makeComplexComponentTensorHandle(t,o);return r.compileAndRun(s,[l,c],u)}),s=o[0],u=o[1],l=this.complex(s,u);return s.dispose(),u.dispose(),l},e.prototype.makeComplexComponentTensorHandle=function(e,t){return{dataId:t.dataId,dtype:t.dtype,shape:e.shape}},e.prototype.addN=function(e){for(var t=e[0],n=1;n 0.5) {\n return ceil(x);\n } else {\n if (mod(base, 2.0) == 0.0) {\n return base;\n } else {\n return base + 1.0;\n }\n }\n");return this.compileAndRun(t,[e])},e.prototype.exp=function(e){var t=new dr(e.shape,"return exp(x);");return this.compileAndRun(t,[e])},e.prototype.expm1=function(e){var t=new dr(e.shape,"return exp(x) - 1.0;");return this.compileAndRun(t,[e])},e.prototype.log=function(e){var t=new dr(e.shape,"if (x < 0.0) return NAN;\n return log(x);"),n=t.getCustomSetupFunc();return this.compileAndRun(t,[e],null,n)},e.prototype.log1p=function(e){var t=new dr(e.shape,"return log(1.0 + x);");return this.compileAndRun(t,[e])},e.prototype.sqrt=function(e){var t=new dr(e.shape,"return sqrt(x);");return this.compileAndRun(t,[e])},e.prototype.rsqrt=function(e){var t=new dr(e.shape,"return inversesqrt(x);");return this.compileAndRun(t,[e])},e.prototype.square=function(e){var t=new dr(e.shape,"return x * x;");return this.compileAndRun(t,[e])},e.prototype.reciprocal=function(e){var t=new dr(e.shape,"return 1.0 / x;");return this.compileAndRun(t,[e])},e.prototype.relu=function(e){var t=new dr(e.shape,"if (isNaN(x)) return x;\n return (x < 0.0) ? 0.0 : x;\n");return this.compileAndRun(t,[e])},e.prototype.elu=function(e){var t=new dr(e.shape,"return (x >= 0.0) ? x : (exp(x) - 1.0);");return this.compileAndRun(t,[e])},e.prototype.eluDer=function(e,t){var n=new zt("return (b >= 1.0) ? a : a * (b + 1.0);",e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.selu=function(e){var t=new dr(e.shape,"\n // Stable and Attracting Fixed Point (0, 1) for Normalized Weights.\n // see: https://arxiv.org/abs/1706.02515\n float scaleAlpha = 1.7580993408473768;\n float scale = 1.0507009873554805;\n return (x >= 0.0) ? scale * x : scaleAlpha * (exp(x) - 1.0);\n");return this.compileAndRun(t,[e])},e.prototype.int=function(e){var t=new dr(e.shape,"return float(int(x));"),n=this.makeOutputArray(t.outputShape,"int32");return this.compileAndRun(t,[e],n)},e.prototype.clip=function(e,t,n){var r=new function(e,t,n){this.variableNames=["A"],this.outputShape=e,this.userCode="\n void main() {\n float value = getAAtOutCoords();\n if (isNaN(value)) {\n setOutput(value);\n return;\n }\n\n setOutput(clamp(value, float("+t+"), float("+n+")));\n }\n "}(e.shape,t,n);return this.compileAndRun(r,[e])},e.prototype.abs=function(e){var t=new dr(e.shape,"return abs(x);");return this.compileAndRun(t,[e])},e.prototype.sigmoid=function(e){var t=new dr(e.shape,"return 1.0 / (1.0 + exp(-1.0 * x));");return this.compileAndRun(t,[e])},e.prototype.softplus=function(e){var t=new dr(e.shape,"\n float epsilon = 1.1920928955078125e-7;\n float threshold = log(epsilon) + 2.0;\n\n bool too_large = x > -threshold;\n bool too_small = x < threshold;\n\n float result;\n float exp_x = exp(x);\n\n if (too_large){\n result = x;\n }\n else if (too_small){\n result = exp_x;\n }\n else{\n result = log(exp_x + 1.0);\n }\n return result;\n");return this.compileAndRun(t,[e])},e.prototype.sin=function(e){var t=new dr(e.shape,"if (isNaN(x)) return x;\n return sin(x);\n");return this.compileAndRun(t,[e])},e.prototype.cos=function(e){var t=new dr(e.shape,"if (isNaN(x)) return x;\n return cos(x);\n");return this.compileAndRun(t,[e])},e.prototype.tan=function(e){var t=new dr(e.shape,"return tan(x);");return this.compileAndRun(t,[e])},e.prototype.asin=function(e){var t=new dr(e.shape,"return asin(x);");return this.compileAndRun(t,[e])},e.prototype.acos=function(e){var t=new dr(e.shape,"return acos(x);");return this.compileAndRun(t,[e])},e.prototype.atan=function(e){var t=new dr(e.shape,"if (isNaN(x)) return x;\n return atan(x);\n");return this.compileAndRun(t,[e])},e.prototype.atan2=function(e,t){var n=new zt("\n if (isNaN(a)) return a;\n if (isNaN(b)) return b;\n\n return atan(a, b);\n",e.shape,t.shape);return this.compileAndRun(n,[e,t])},e.prototype.sinh=function(e){var t=new dr(e.shape,"\n float e2x = exp(x);\n return (e2x - 1.0 / e2x) / 2.0;\n");return this.compileAndRun(t,[e])},e.prototype.cosh=function(e){var t=new dr(e.shape,"\n float e2x = exp(-x);\n return (e2x + 1.0 / e2x) / 2.0;\n");return this.compileAndRun(t,[e])},e.prototype.tanh=function(e){var t=new dr(e.shape,"\n float e2x = exp(-2.0 * abs(x));\n return sign(x) * (1.0 - e2x) / (1.0 + e2x);\n");return this.compileAndRun(t,[e])},e.prototype.asinh=function(e){var t=new dr(e.shape,"return log(x + sqrt(x * x + 1.0));");return this.compileAndRun(t,[e])},e.prototype.acosh=function(e){var t=new dr(e.shape,"if (isNaN(x)) return x;\n if (x < 1.0) return NAN;\n return log(x + sqrt(x * x - 1.0));"),n=t.getCustomSetupFunc();return this.compileAndRun(t,[e],null,n)},e.prototype.atanh=function(e){var t=new dr(e.shape,"if (isNaN(x)) return x;\n if ((x < -1.0) || (x > 1.0)) return NAN;\n return (log(1.0 + x) - log(1.0 - x)) / 2.0;"),n=t.getCustomSetupFunc();return this.compileAndRun(t,[e],null,n)},e.prototype.erf=function(e){var t=new dr(e.shape,'\n // Error function is calculated approximately with elementary function.\n // See "Handbook of Mathematical Functions with Formulas,\n // Graphs, and Mathematical Tables", Abramowitz and Stegun.\n float p = 0.3275911;\n float a1 = 0.254829592;\n float a2 = -0.284496736;\n float a3 = 1.421413741;\n float a4 = -1.453152027;\n float a5 = 1.061405429;\n\n float t = 1.0 / (1.0 + p * x);\n return 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);\n');return this.compileAndRun(t,[e])},e.prototype.step=function(e,t){var n=new dr(e.shape,function(e){return void 0===e&&(e=0),mr+"\n return x > 0.0 ? 1.0 : float("+e+");\n "}(t));return this.compileAndRun(n,[e])},e.prototype.conv2d=function(e,t,n){var r=new function(e){this.variableNames=["x","W"],this.outputShape=e.outShape;var t=e.padInfo.top,n=e.padInfo.left,r=e.strideHeight,a=e.strideWidth,i=e.dilationHeight,o=e.dilationWidth,s=e.filterHeight,u=e.filterWidth,l=4*Math.floor(e.inChannels/4),c=e.inChannels%4;this.userCode="\n const ivec2 strides = ivec2("+r+", "+a+");\n const ivec2 pads = ivec2("+t+", "+n+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d2 = coords[3];\n\n ivec2 xRCCorner = coords.yz * strides - pads;\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // Convolve x(?, ?, d1) with w(:, :, d1, d2) to get y(yR, yC, d2).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+s+"; wR++) {\n int xR = xRCorner + wR * "+i+";\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int wC = 0; wC < "+u+"; wC++) {\n int xC = xCCorner + wC * "+o+";\n\n if (xC < 0 || xC >= "+e.inWidth+") {\n continue;\n }\n\n for (int d1 = 0; d1 < "+l+"; d1 += 4) {\n vec4 xValues = vec4(\n getX(batch, xR, xC, d1),\n getX(batch, xR, xC, d1 + 1),\n getX(batch, xR, xC, d1 + 2),\n getX(batch, xR, xC, d1 + 3)\n );\n vec4 wValues = vec4(\n getW(wR, wC, d1, d2),\n getW(wR, wC, d1 + 1, d2),\n getW(wR, wC, d1 + 2, d2),\n getW(wR, wC, d1 + 3, d2)\n );\n\n dotProd += dot(xValues, wValues);\n }\n\n if ("+(1===c)+") {\n dotProd +=\n getX(batch, xR, xC, "+l+") *\n getW(wR, wC, "+l+", d2);\n } else if ("+(2===c)+") {\n vec2 xValues = vec2(\n getX(batch, xR, xC, "+l+"),\n getX(batch, xR, xC, "+l+" + 1)\n );\n vec2 wValues = vec2(\n getW(wR, wC, "+l+", d2),\n getW(wR, wC, "+l+" + 1, d2)\n );\n dotProd += dot(xValues, wValues);\n } else if ("+(3===c)+") {\n vec3 xValues = vec3(\n getX(batch, xR, xC, "+l+"),\n getX(batch, xR, xC, "+l+" + 1),\n getX(batch, xR, xC, "+l+" + 2)\n );\n vec3 wValues = vec3(\n getW(wR, wC, "+l+", d2),\n getW(wR, wC, "+l+" + 1, d2),\n getW(wR, wC, "+l+" + 2, d2)\n );\n dotProd += dot(xValues, wValues);\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.conv2dDerInput=function(e,t,n){var r=new function(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,i=t-1-e.padInfo.top,o=n-1-e.padInfo.left;this.userCode="\n const ivec2 pads = ivec2("+i+", "+o+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords[3];\n\n ivec2 dyCorner = coords.yz - pads;\n int dyRCorner = dyCorner.x;\n int dyCCorner = dyCorner.y;\n\n // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+t+" - 1 - wR;\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = "+n+" - 1 - wC;\n\n for (int d2 = 0; d2 < "+e.outChannels+"; d2++) {\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, d2);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.conv2dDerFilter=function(e,t,n){var r=new function(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;var t=e.strideHeight,n=e.strideWidth,r=e.padInfo.top,a=e.padInfo.left;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int wR = coords.x;\n int wC = coords.y;\n int d1 = coords.z;\n int d2 = coords.w;\n\n // Convolve x(?, ?, d1) with dy(:, :, d2) to get dw(wR, wC, d1, d2).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n\n for (int b = 0; b < "+e.batchSize+"; b++) {\n for (int yR = 0; yR < "+e.outHeight+"; yR++) {\n int xR = wR + yR * "+t+" - "+r+";\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+e.outWidth+"; yC++) {\n int xC = wC + yC * "+n+" - "+a+";\n\n if (xC < 0 || xC >= "+e.inWidth+") {\n continue;\n }\n\n float dyValue = getDy(b, yR, yC, d2);\n float xValue = getX(b, xR, xC, d1);\n dotProd += (xValue * dyValue);\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.depthwiseConv2D=function(e,t,n){var r=new function(e){this.variableNames=["x","W"],this.outputShape=e.outShape;var t=e.inHeight,n=e.inWidth,r=e.padInfo.top,a=e.padInfo.left,i=e.strideHeight,o=e.strideWidth,s=e.dilationHeight,u=e.dilationWidth,l=e.filterHeight,c=e.filterWidth,f=e.outChannels/e.inChannels;this.userCode="\n const ivec2 strides = ivec2("+i+", "+o+");\n const ivec2 pads = ivec2("+r+", "+a+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords.x;\n ivec2 xRCCorner = coords.yz * strides - pads;\n int d2 = coords.w;\n int d1 = d2 / "+f+";\n int q = d2 - d1 * "+f+";\n\n int xRCorner = xRCCorner.x;\n int xCCorner = xRCCorner.y;\n\n // Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n // TODO(dsmilkov): Flatten the two for loops and vec4 the operations.\n for (int wR = 0; wR < "+l+"; wR++) {\n int xR = xRCorner + wR * "+s+";\n\n if (xR < 0 || xR >= "+t+") {\n continue;\n }\n\n for (int wC = 0; wC < "+c+"; wC++) {\n int xC = xCCorner + wC * "+u+";\n\n if (xC < 0 || xC >= "+n+") {\n continue;\n }\n\n float xVal = getX(batch, xR, xC, d1);\n float wVal = getW(wR, wC, d1, q);\n dotProd += xVal * wVal;\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.depthwiseConv2DDerInput=function(e,t,n){var r=new function(e){this.variableNames=["dy","W"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,i=t-1-e.padInfo.top,o=n-1-e.padInfo.left,s=e.outChannels/e.inChannels;this.userCode="\n const ivec2 pads = ivec2("+i+", "+o+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int batch = coords[0];\n int d1 = coords[3];\n ivec2 dyCorner = coords.yz - pads;\n int dyRCorner = dyCorner.x;\n int dyCCorner = dyCorner.y;\n\n float dotProd = 0.0;\n\n for (int wR = 0; wR < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n int wRPerm = "+t+" - 1 - wR;\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n int wCPerm = "+n+" - 1 - wC;\n\n // TODO: Vec4 over the channelMul\n for (int dm = 0; dm < "+s+"; dm++) {\n int d2 = d1 * "+s+" + dm;\n float xValue = getDy(batch, idyR, idyC, d2);\n float wValue = getW(wRPerm, wCPerm, d1, dm);\n dotProd += xValue * wValue;\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.depthwiseConv2DDerFilter=function(e,t,n){var r=new function(e){this.variableNames=["x","dy"],this.outputShape=e.filterShape;var t=e.strideHeight,n=e.strideWidth,r=e.padInfo.top,a=e.padInfo.left,i=e.outChannels/e.inChannels;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int wR = coords.x;\n int wC = coords.y;\n int d1 = coords.z;\n int dm = coords.w;\n int d2 = d1 * "+i+" + dm;\n\n float dotProd = 0.0;\n\n // TODO: Vec4 over the batch size\n for (int b = 0; b < "+e.batchSize+"; b++) {\n for (int yR = 0; yR < "+e.outHeight+"; yR++) {\n int xR = wR + yR * "+t+" - "+r+";\n\n if (xR < 0 || xR >= "+e.inHeight+") {\n continue;\n }\n\n for (int yC = 0; yC < "+e.outWidth+"; yC++) {\n int xC = wC + yC * "+n+" - "+a+";\n\n if (xC < 0 || xC >= "+e.inWidth+") {\n continue;\n }\n\n float dyValue = getDy(b, yR, yC, d2);\n float xValue = getX(b, xR, xC, d1);\n dotProd += (xValue * dyValue);\n }\n }\n }\n setOutput(dotProd);\n }\n "}(n);return this.compileAndRun(r,[e,t])},e.prototype.maxPool=function(e,t){var n=new sr(t,"max",!1),r=this.makeOutputArray(n.outputShape,e.dtype);return this.compileAndRun(n,[e],r)},e.prototype.avgPool=function(e,t){var n=new sr(t,"avg",!1),r=this.makeOutputArray(n.outputShape,"float32");return this.compileAndRun(n,[e],r)},e.prototype.maxPoolBackprop=function(e,t,n,r){var a=new sr(r,"max",!0),i=this.compileAndRun(a,[t]),o=new function(e){this.variableNames=["dy","maxPos"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,i=t-1-e.padInfo.top,o=n-1-e.padInfo.left,s=t*n-1;this.userCode="\n const ivec2 pads = ivec2("+i+", "+o+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n\n ivec2 dyRCCorner = coords.yz - pads;\n int dyRCorner = dyRCCorner.x;\n int dyCCorner = dyRCCorner.y;\n\n // Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n float dyValue = getDy(b, idyR, idyC, d);\n int maxPosValue = "+s+" - int(getMaxPos(b, idyR, idyC, d));\n\n // Get the current value, check it against the value from the\n // position matrix.\n int curPosValue = wR * "+n+" + wC;\n float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);\n\n dotProd += dyValue * mask;\n }\n }\n setOutput(dotProd);\n }\n "}(r),s=this.makeOutputArray(o.outputShape,t.dtype),u=this.compileAndRun(o,[e,i],s);return i.dispose(),u},e.prototype.avgPoolBackprop=function(e,t,n){var r=new function(e){this.variableNames=["dy"],this.outputShape=e.inShape;var t=e.filterHeight,n=e.filterWidth,r=e.strideHeight,a=e.strideWidth,i=t-1-e.padInfo.top,o=n-1-e.padInfo.left,s=1/(t*n);this.userCode="\n const ivec2 pads = ivec2("+i+", "+o+");\n const float avgMultiplier = float("+s+");\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n\n ivec2 dyRCCorner = coords.yz - pads;\n int dyRCorner = dyRCCorner.x;\n int dyCCorner = dyRCCorner.y;\n\n // Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).\n // ? = to be determined. : = across all values in that axis.\n float dotProd = 0.0;\n for (int wR = 0; wR < "+t+"; wR++) {\n float dyR = float(dyRCorner + wR) / "+r+".0;\n\n if (dyR < 0.0 || dyR >= "+e.outHeight+".0 || fract(dyR) > 0.0) {\n continue;\n }\n int idyR = int(dyR);\n\n for (int wC = 0; wC < "+n+"; wC++) {\n float dyC = float(dyCCorner + wC) / "+a+".0;\n\n if (dyC < 0.0 || dyC >= "+e.outWidth+".0 ||\n fract(dyC) > 0.0) {\n continue;\n }\n int idyC = int(dyC);\n\n float dyValue = getDy(b, idyR, idyC, d);\n\n dotProd += dyValue * avgMultiplier;\n }\n }\n setOutput(dotProd);\n }\n "}(n),a=this.makeOutputArray(r.outputShape,t.dtype);return this.compileAndRun(r,[e],a)},e.prototype.cast=function(e,t){return Nt(e,t,this)},e.prototype.reshape=function(e,t){return Ot(e,t)},e.prototype.resizeBilinear=function(e,t,n,r){var a=new function(e,t,n,r){this.variableNames=["A"],this.outputShape=[];var a=e[0],i=e[1],o=e[2],s=e[3];this.outputShape=[a,t,n,s];var u=[r&&t>1?i-1:i,r&&n>1?o-1:o],l=[r&&t>1?t-1:t,r&&n>1?n-1:n];this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+u[0]/l[0]+",\n "+u[1]/l[1]+");\n const vec2 inputShapeRC = vec2("+i+".0, "+o+".0);\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n ivec2 yRC = coords.yz;\n\n // Fractional source index.\n vec2 sourceFracIndexRC = vec2(yRC) * effectiveInputOverOutputRatioRC;\n\n // Compute the four integer indices.\n ivec2 sourceFloorRC = ivec2(sourceFracIndexRC);\n ivec2 sourceCeilRC = ivec2(\n min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));\n\n float topLeft = getA(b, sourceFloorRC.x, sourceFloorRC.y, d);\n float bottomLeft = getA(b, sourceCeilRC.x, sourceFloorRC.y, d);\n float topRight = getA(b, sourceFloorRC.x, sourceCeilRC.y, d);\n float bottomRight = getA(b, sourceCeilRC.x, sourceCeilRC.y, d);\n\n vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);\n\n float top = topLeft + (topRight - topLeft) * fracRC.y;\n float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;\n float newValue = top + (bottom - top) * fracRC.x;\n\n setOutput(newValue);\n }\n "}(e.shape,t,n,r);return this.compileAndRun(a,[e])},e.prototype.resizeBilinearBackprop=function(e,t,n){var r=new function(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t.shape;var r=t.shape,a=r[1],i=r[2],o=e.shape,s=o[1],u=o[2],l=[n&&s>1?a-1:a,n&&u>1?i-1:i],c=[n&&s>1?s-1:s,n&&u>1?u-1:u],f=l[0]/c[0],p=l[1]/c[1],h=1/f,d=1/p,m=2*Math.ceil(h)+2,g=2*Math.ceil(d)+2;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n int r = coords[1];\n int c = coords[2];\n\n float accumulator = 0.0;\n\n const float heightScale = float("+f+");\n const float widthScale = float("+p+");\n\n const float invHeightScale = float("+h+");\n const float invWidthScale = float("+d+");\n\n const int winHeight = int("+m+");\n const int winWidth = int("+g+");\n\n // Compute bounds for where in dy we will look\n float startRLerp = floor(float(r) * invHeightScale);\n int startDyR = int(startRLerp - float(winHeight / 2));\n\n float startCLerp = floor(float(c) * invWidthScale);\n int startDyC = int(startCLerp - float(winWidth / 2));\n\n // Loop over dy\n for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {\n int dyR = dyROffset + startDyR;\n\n // Guard against the window exceeding the bounds of dy\n if (dyR < 0 || dyR >= "+s+") {\n continue;\n }\n\n for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {\n int dyC = dyCOffset + startDyC;\n\n // Guard against the window exceeding the bounds of dy\n if (dyC < 0 || dyC >= "+u+") {\n continue;\n }\n\n float dxR = float(dyR) * heightScale;\n int topDxRIndex = int(floor(dxR));\n int bottomDxRIndex = int(min(ceil(dxR), "+(a-1)+".0));\n float dxRLerp = dxR - float(topDxRIndex);\n float inverseDxRLerp = 1.0 - dxRLerp;\n\n float dxC = float(dyC) * widthScale;\n int leftDxCIndex = int(floor(dxC));\n int rightDxCIndex = int(min(ceil(dxC), "+(i-1)+".0));\n float dxCLerp = dxC - float(leftDxCIndex);\n float inverseDxCLerp = 1.0 - dxCLerp;\n\n if (r == topDxRIndex && c == leftDxCIndex) {\n // topLeft\n accumulator +=\n getDy(b, dyR, dyC, d) * inverseDxRLerp * inverseDxCLerp;\n }\n\n if (r == topDxRIndex && c == rightDxCIndex) {\n // topRight\n accumulator += getDy(b, dyR, dyC, d) * inverseDxRLerp * dxCLerp;\n }\n\n if (r == bottomDxRIndex && c == leftDxCIndex) {\n // bottomLeft\n accumulator += getDy(b, dyR, dyC, d) * dxRLerp * inverseDxCLerp;\n }\n\n if (r == bottomDxRIndex && c == rightDxCIndex) {\n // bottomRight\n accumulator += getDy(b, dyR, dyC, d) * dxRLerp * dxCLerp;\n }\n }\n }\n // End loop over dy\n\n setOutput(accumulator);\n }\n "}(e,t,n);return this.compileAndRun(r,[e])},e.prototype.resizeNearestNeighbor=function(e,t,n,r){var a=new function(e,t,n,r){this.variableNames=["A"],this.outputShape=[];var a=e[0],i=e[1],o=e[2],s=e[3];this.outputShape=[a,t,n,s];var u=[r&&t>1?i-1:i,r&&n>1?o-1:o],l=[r&&t>1?t-1:t,r&&n>1?n-1:n],c=r?"0.5":"0.0";this.userCode="\n const vec2 effectiveInputOverOutputRatioRC = vec2(\n "+u[0]/l[0]+",\n "+u[1]/l[1]+");\n const vec2 inputShapeRC = vec2("+i+".0, "+o+".0);\n\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n ivec2 yRC = coords.yz;\n\n // Fractional source index.\n vec2 sourceFracIndexRC = vec2(yRC) * effectiveInputOverOutputRatioRC;\n\n // Compute the coordinators of nearest neighbor point.\n ivec2 sourceNearestRC = ivec2(\n min(inputShapeRC - 1.0, floor(sourceFracIndexRC + "+c+")));\n\n float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);\n\n setOutput(newValue);\n }\n "}(e.shape,t,n,r);return this.compileAndRun(a,[e])},e.prototype.resizeNearestNeighborBackprop=function(e,t,n){var r=new function(e,t,n){this.variableNames=["dy"],this.outputShape=[],this.outputShape=t.shape;var r=t.shape,a=r[1],i=r[2],o=e.shape,s=o[1],u=o[2],l=[n&&s>1?a-1:a,n&&u>1?i-1:i],c=[n&&s>1?s-1:s,n&&u>1?u-1:u],f=l[0]/c[0],p=l[1]/c[1],h=1/f,d=1/p,m=2*Math.ceil(h)+2,g=2*Math.ceil(d)+2;this.userCode="\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int d = coords[3];\n int r = coords[1];\n int c = coords[2];\n\n float accumulator = 0.0;\n\n const float heightScale = float("+f+");\n const float widthScale = float("+p+");\n\n const float invHeightScale = float("+h+");\n const float invWidthScale = float("+d+");\n\n const int winHeight = int("+m+");\n const int winWidth = int("+g+");\n\n // Compute bounds for where in dy we will look\n float startRLerp = floor(float(r) * invHeightScale);\n int startDyR = int(floor(startRLerp - float(winHeight / 2)));\n\n float startCLerp = floor(float(c) * invWidthScale);\n int startDyC = int(floor(startCLerp - float(winWidth / 2)));\n\n // Loop over dy\n for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {\n int dyR = dyROffset + startDyR;\n\n // Guard against the window exceeding the bounds of dy\n if (dyR < 0 || dyR >= "+s+") {\n continue;\n }\n\n for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {\n int dyC = dyCOffset + startDyC;\n\n // Guard against the window exceeding the bounds of dy\n if (dyC < 0 || dyC >= "+u+") {\n continue;\n }\n\n float sourceFracRow =\n float("+l[0]+") *\n (float(dyR) / float("+c[0]+"));\n\n float sourceFracCol =\n float("+l[1]+") *\n (float(dyC) / float("+c[1]+"));\n\n int sourceNearestRow = int(min(\n float(int("+a+") - 1),\n "+n+" ? float(round(sourceFracRow)) :\n float(floor(sourceFracRow))));\n\n int sourceNearestCol = int(min(\n float(int("+i+") - 1),\n "+n+" ? float(round(sourceFracCol)) :\n float(floor(sourceFracCol))));\n\n if (r == sourceNearestRow && c == sourceNearestCol) {\n accumulator += getDy(b, dyR, dyC, d);\n }\n }\n }\n // End loop over dy\n\n setOutput(accumulator);\n }\n "}(e,t,n);return this.compileAndRun(r,[e])},e.prototype.multinomial=function(e,t,n,r){var a=t?e:Je(e),i=a.shape[0],o=a.shape[1],s=new or(i,o,n),u=this.makeOutputArray(s.outputShape,"int32"),l=s.getCustomSetupFunc(r);return this.compileAndRun(s,[a],u,l)},e.prototype.oneHot=function(e,t,n,r){var a=new function(e,t,n,r){this.variableNames=["indices"],this.outputShape=[e,t],this.userCode="\n void main() {\n ivec2 coords = getOutputCoords();\n int index = round(getIndices(coords.x));\n setOutput(mix(float("+r+"), float("+n+"),\n float(index == coords.y)));\n }\n "}(e.size,t,n,r);return this.compileAndRun(a,[e])},e.prototype.nonMaxSuppression=function(e,t,n,r,a){return Ie("tf.nonMaxSuppression() in webgl locks the UI thread. Call tf.nonMaxSuppressionAsync() instead"),It(e.dataSync(),t.dataSync(),n,r,a)},e.prototype.cropAndResize=function(e,t,n,r,a,i){var o=new function(e,t,n,r,a){this.variableNames=["Image","Boxes","BoxInd"],this.outputShape=[];var i=e[0],o=e[1],s=e[2],u=e[3],l=t[0],c=n[0],f=n[1];this.outputShape=[l,c,f,u];var p="bilinear"===r?1:0,h=[o-1+".0",s-1+".0"],d=h[0],m=h[1],g=c>1?[""+(o-1)/(c-1),"(y2-y1) * height_ratio","y1*"+d+" + float(y)*(height_scale)"]:["0.0","0.0","0.5 * (y1+y2) * "+d],y=g[0],v=g[1],b=g[2],w=f>1?[""+(s-1)/(f-1),"(x2-x1) * width_ratio","x1*"+m+" + float(x)*(width_scale)"]:["0.0","0.0","0.5 * (x1+x2) * "+m],x=w[0],_=w[1],S=w[2];this.userCode="\n const float height_ratio = float("+y+");\n const float width_ratio = float("+x+");\n void main() {\n ivec4 coords = getOutputCoords();\n int b = coords[0];\n int y = coords[1];\n int x = coords[2];\n int d = coords[3];\n\n // get box vals\n float y1 = getBoxes(b,0);\n float x1 = getBoxes(b,1);\n float y2 = getBoxes(b,2);\n float x2 = getBoxes(b,3);\n\n // get image in batch index\n int bInd = round(getBoxInd(b));\n if(bInd < 0 || bInd >= "+i+") {\n return;\n }\n\n float height_scale = "+v+";\n float width_scale = "+_+";\n\n float in_y = "+b+";\n if( in_y < 0.0 || in_y > "+d+" ) {\n setOutput(float("+a+"));\n return;\n }\n float in_x = "+S+";\n if( in_x < 0.0 || in_x > "+m+" ) {\n setOutput(float("+a+"));\n return;\n }\n\n vec2 sourceFracIndexRC = vec2(in_y,in_x);\n if("+p+" == 1) {\n // Compute the four integer indices.\n ivec2 sourceFloorRC = ivec2(sourceFracIndexRC);\n ivec2 sourceCeilRC = ivec2(ceil(sourceFracIndexRC));\n\n float topLeft = getImage(b, sourceFloorRC.x, sourceFloorRC.y, d);\n float bottomLeft = getImage(b, sourceCeilRC.x, sourceFloorRC.y, d);\n float topRight = getImage(b, sourceFloorRC.x, sourceCeilRC.y, d);\n float bottomRight = getImage(b, sourceCeilRC.x, sourceCeilRC.y, d);\n\n vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);\n\n float top = topLeft + (topRight - topLeft) * fracRC.y;\n float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;\n float newValue = top + (bottom - top) * fracRC.x;\n setOutput(newValue);\n } else {\n // Compute the coordinators of nearest neighbor point.\n ivec2 sourceNearestRC = ivec2(floor(\n sourceFracIndexRC + vec2(0.5,0.5)));\n float newValue = getImage(b, sourceNearestRC.x, sourceNearestRC.y, d);\n setOutput(newValue);\n }\n }\n "}(e.shape,t.shape,r,a,i);return this.compileAndRun(o,[e,t,n])},e.prototype.depthToSpace=function(e,t,n){d(t>1,"blockSize should be > 1 for depthToSpace, but was: "+t);var r=e.shape[0],a=("NHWC"===n?e.shape[1]:e.shape[2])*t,i=("NHWC"===n?e.shape[2]:e.shape[3])*t,o=("NHWC"===n?e.shape[3]:e.shape[1])/(t*t),s=new Xt("NHWC"===n?[r,a,i,o]:[r,o,a,i],t,n);return this.compileAndRun(s,[e])},e.prototype.split=function(e,t,n){return At(e,t,n)},e.prototype.makeOutputArray=function(e,t){return Q.make(e,{},t)},e.prototype.compileAndRun=function(e,t,n,r,a){var i=this;if(void 0===a&&(a=!0),null==n&&(n=this.makeOutputArray(e.outputShape,t[0].dtype)),0===n.size)return this.texData.get(n.dataId).values=I(n.dtype,0),n;var o=t.map(function(e){if("complex64"===e.dtype)throw new Error("GPGPUProgram does not support complex64 input. For complex64 dtypes, please separate the program into real and imaginary parts.");var t=i.texData.get(e.dataId);return null==t.texture&&b(e.shape)<=32?{shape:e.shape,texData:null,isUniform:!0,uniformValues:i.readSync(e.dataId)}:(i.uploadToGPU(e.dataId),{shape:e.shape,texData:t,isUniform:!1})});this.uploadToGPU(n.dataId);var s,u={shape:n.shape,texData:this.texData.get(n.dataId),isUniform:!1},l=function(e,t,n){var r="";o.concat(n).forEach(function(e){r+=e.shape+"_"+(e.isUniform?"uniform":e.texData.texShape)});var a=e.userCode,i=(!0===e.supportsBroadcasting).toString();return e.constructor.name+"_"+i+"_"+r+"_"+a}(e,0,u),c=this.getAndSaveBinary(l,function(){return function(e,t,n,r){for(var a=t.userCode,i=n.map(function(e,n){var r={logicalShape:e.shape,texShape:e.isUniform?null:e.texData.texShape,isUniform:e.isUniform};return{name:t.variableNames[n],shapeInfo:r}}),o=i.map(function(e){return e.shapeInfo}),s={logicalShape:r.shape,texShape:r.texData.texShape,isUniform:!1},u=Bt(i,s,a,!0===t.supportsBroadcasting),l=e.createProgram(u),c={},f=0;fthis.NUM_BYTES_BEFORE_PAGING)for(var p=this.numBytesInGPU-this.NUM_BYTES_BEFORE_PAGING;p>0&&this.lruDataGPU.length>0;){var h=this.lruDataGPU.shift(),d=this.texData.get(h),m=d.shape,g=d.dtype;p-=this.computeBytes(m,g),this.read(h)}return f&&(s=this.endTimer(s),this.activeTimers.push(this.getQueryTime(s))),n},e.prototype.getAndSaveBinary=function(e,t){return e in this.binaryCache||(this.binaryCache[e]=t()),this.binaryCache[e]},e.prototype.getTextureManager=function(){return this.textureManager},e.prototype.dispose=function(){if(!this.disposed){for(var e in this.binaryCache)this.gpgpu.deleteProgram(this.binaryCache[e].webGLProgram);this.textureManager.dispose(),this.canvas.remove(),null!=this.fromPixelsCanvas&&this.fromPixelsCanvas.remove(),this.gpgpuCreatedLocally&&this.gpgpu.dispose(),this.disposed=!0}},e.prototype.floatPrecision=function(){var e=this;return Ee(function(){return e.abs(nt(1e-8)).get()>0?32:16})},e.prototype.throwIfNoData=function(e){if(!this.texData.has(e))throw new Error("WebGL backend: No data found for this tensor. Did you change your backend in the middle of the program? New backends can't use Tensors created with previous backends")},e.prototype.uploadToGPU=function(e){this.throwIfNoData(e);var t=this.texData.get(e),n=t.shape,r=t.values,a=t.texture,i=(t.dtype,t.usage);if(null==a){var o,s=null!=this.activeTimers;s&&(o=performance.now());var u=Dn(this.gpgpu.gl,n);t.texShape=u;var l=this.acquireTexture(e,u,i);t.texture=l,null!=r&&(this.gpgpu.uploadMatrixToTexture(l,u[0],u[1],r instanceof Float32Array?r:new Float32Array(r)),t.values=null,s&&(this.uploadWaitMs+=performance.now()-o))}else this.lruDataGPU.indexOf(e)>=0&&(this.lruDataGPU.splice(this.lruDataGPU.indexOf(e),1),this.lruDataGPU.push(e))},e.prototype.cacheOnCPU=function(e,t){var n=this.delayedStorage,r=this.texData.get(e),a=r.texture,i=r.texShape,o=r.dtype,s=r.usage;n&&null!=a&&(this.releaseTexture(e,a,i,s),r.texture=null,r.texShape=null),r.usage=Ht.UPLOAD,null!=t&&(r.values=function(e,t){if("float32"===t||"complex64"===t)return e;if("int32"===t||"bool"===t){for(var n="int32"===t?new Int32Array(e.length):new Uint8Array(e.length),r=0;r=0&&this.lruDataGPU.splice(s,1),this.numBytesInGPU-=this.computeBytes(i,o),this.textureManager.releaseTexture(t,n,r)},e.prototype.acquireTexture=function(e,t,n){var r=this.texData.get(e),a=r.shape,i=r.dtype;return this.lruDataGPU.push(e),this.numBytesInGPU+=this.computeBytes(a,i),this.textureManager.acquireTexture(t,n)},e.prototype.computeBytes=function(e,t){return b(e)*M(t)},e}();me.get("IS_BROWSER")&&me.registerBackend("webgl",function(){return new ua},2,Z);var la=Ye({abs_:function(e){var t=Ke(e,"x","abs");return me.engine.runKernel(function(e){return e.abs(t)},{$x:t},function(e){return{$x:function(){return e.mulStrict(t.toFloat().step(-1))}}})}}),ca=Ye({acos_:function(e){var t=Ke(e,"x","acos");return me.engine.runKernel(function(e){return e.acos(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(nt(1).sub(t.toFloat().square()).sqrt()).neg()}}})}}),fa=Ye({acosh_:function(e){var t=Ke(e,"x","acosh");return me.engine.runKernel(function(e){return e.acosh(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.toFloat().square().sub(nt(1)).sqrt())}}})}}),pa=Ye({asin_:function(e){var t=Ke(e,"x","asin");return me.engine.runKernel(function(e){return e.asin(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(nt(1).sub(t.toFloat().square()).sqrt())}}})}}),ha=Ye({asinh_:function(e){var t=Ke(e,"x","asinh");return me.engine.runKernel(function(e){return e.asinh(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(nt(1).add(t.toFloat().square()).sqrt())}}})}}),da=Ye({atan_:function(e){var t=Ke(e,"x","atan");return me.engine.runKernel(function(e){return e.atan(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(nt(1).add(t.toFloat().square()))}}})}}),ma=Ye({atanh_:function(e){var t=Ke(e,"x","atanh");return me.engine.runKernel(function(e){return e.atanh(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(nt(1).sub(t.toFloat().square()))}}})}}),ga=Ye({ceil_:function(e){var t=Ke(e,"x","ceil");return me.engine.runKernel(function(e){return e.ceil(t)},{$x:t},function(e){return{$x:function(){return xt(e)}}})}}),ya=Ye({clipByValue_:function(e,t,n){var r=Ke(e,"x","clipByValue");return d(t<=n,"Error in clip: min ("+t+") must be less than or equal to max ("+n+")."),me.engine.runKernel(function(e){return e.clip(r,t,n)},{$x:r},function(e){return{$x:function(){return e.where(r.greaterEqual(nt(t)).logicalAnd(r.lessEqual(nt(n))),xt(e))}}})}}),va=Ye({cos_:function(e){var t=Ke(e,"x","cos");return me.engine.runKernel(function(e){return e.cos(t)},{$x:t},function(e){return{$x:function(){return t.toFloat().sin().neg().mulStrict(e)}}})}}),ba=Ye({cosh_:function(e){var t=Ke(e,"x","cosh");return me.engine.runKernel(function(e){return e.cosh(t)},{$x:t},function(e){return{$x:function(){return t.toFloat().sinh().mulStrict(e)}}})}}),wa=Ye({erf_:function(e){var t=Ke(e,"x","erf");return d("int32"===t.dtype||"float32"===t.dtype,"Input dtype must be `int32` or `float32`."),"int32"===t.dtype&&(t=t.toFloat()),me.engine.runKernel(function(e){return e.erf(t)},{$x:t},function(e){return{$x:function(){return e.mulStrict(nt(2/Math.sqrt(Math.PI)).mul(t.square().neg().exp()))}}})}}),xa=Ye({exp_:function(e){var t=Ke(e,"x","exp");return me.engine.runKernel(function(e,n){return n(e.exp(t))},{$x:t},function(e,t){var n=t[0];return{$x:function(){return e.mulStrict(n)}}})}}),_a=Ye({expm1_:function(e){var t=Ke(e,"x","expm1");return me.engine.runKernel(function(e){return e.expm1(t)},{$x:t},function(e){return{$x:function(){return e.mulStrict(t.exp())}}})}}),Sa=Ye({floor_:function(e){var t=Ke(e,"x","floor");return me.engine.runKernel(function(e){return e.floor(t)},{$x:t},function(e){return{$x:function(){return xt(e)}}})}}),Ea=Ye({log_:function(e){var t=Ke(e,"x","log");return me.engine.runKernel(function(e){return e.log(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.toFloat())}}})}}),Na=Ye({log1p_:function(e){var t=Ke(e,"x","log1p");return me.engine.runKernel(function(e){return e.log1p(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.add(nt(1)))}}})}}),Oa=Ye({logSigmoid_:function(e){var t=Ke(e,"x","logSigmoid");return me.engine.runKernel(function(e){return e.softplus(t.neg()).neg()},{$x:t},function(e){return{$x:function(){return e.mulStrict(t.neg().sigmoid())}}})}}),ka=Ye({neg_:function(e){var t=Ke(e,"x","neg");return me.engine.runKernel(function(e){return e.neg(t)},{$x:t},function(e){return{$x:function(){return e.neg()}}})}}),Ia=Ye({reciprocal_:function(e){var t=Ke(e,"x","reciprocal");return me.engine.runKernel(function(e){return e.reciprocal(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.square().neg())}}})}}),Ca=Ye({round_:function(e){var t=Ke(e,"x","round");return me.engine.runKernel(function(e){return e.round(t)},{$x:t},function(e){return{$x:function(){return xt(e)}}})}}),Aa=Ye({rsqrt_:function(e){var t=Ke(e,"x","rsqrt");return me.engine.runKernel(function(e){return e.rsqrt(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.pow(nt(1.5)).mul(nt(2))).neg()}}})}}),Pa=Ye({sigmoid_:function(e){var t=Ke(e,"x","sigmoid");return me.engine.runKernel(function(e,n){return n(e.sigmoid(t))},{$x:t},function(e,t){var n=t[0];return{$x:function(){return e.mulStrict(n.mul(nt(1).sub(n)))}}})}}),Ta=Ye({sign_:function(e){var t=Ke(e,"x","sign");return me.engine.runKernel(function(e){return e.sign(t)},{$x:t},function(e){return{$x:function(){return xt(e)}}})}}),Ma=Ye({sin_:function(e){var t=Ke(e,"x","sin");return me.engine.runKernel(function(e){return e.sin(t)},{$x:t},function(e){return{$x:function(){return t.toFloat().cos().mulStrict(e)}}})}}),Ra=Ye({sinh_:function(e){var t=Ke(e,"x","sinh");return me.engine.runKernel(function(e){return e.sinh(t)},{$x:t},function(e){return{$x:function(){return t.toFloat().cosh().mulStrict(e)}}})}}),Da=Ye({softplus_:function(e){var t=Ke(e,"x","softplus");return me.engine.runKernel(function(e){return e.softplus(t)},{$x:t},function(e){return{$x:function(){return e.mulStrict(t.sigmoid())}}})}}),ja=Ye({sqrt_:function(e){var t=Ke(e,"x","sqrt");return me.engine.runKernel(function(e){return e.sqrt(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.toFloat().sqrt().mul(nt(2)))}}})}}),La=Ye({square_:function(e){var t=Ke(e,"x","square");return me.engine.runKernel(function(e){return e.square(t)},{$x:t},function(e){return{$x:function(){return e.mulStrict(t.toFloat().mul(nt(2)))}}})}}),za=Ye({step_:function(e,t){void 0===t&&(t=0);var n=Ke(e,"x","step");return me.engine.runKernel(function(e){return e.step(n,t)},{$x:n},function(e){return{$x:function(){return xt(e)}}})}}),Ba=Ye({tan_:function(e){var t=Ke(e,"x","tan");return me.engine.runKernel(function(e){return e.tan(t)},{$x:t},function(e){return{$x:function(){return e.divStrict(t.cos().square())}}})}}),Fa=Ye({tanh_:function(e){var t=Ke(e,"x","tanh");return me.engine.runKernel(function(e,n){return n(e.tanh(t))},{$x:t},function(e,t){var n=t[0];return{$x:function(){return nt(1).sub(n.square()).mulStrict(e)}}})}});function Va(e){return null==e?null:0===e.rank?e.as1D():1===e.rank?e:2===e.rank?e.as4D(1,1,e.shape[0],e.shape[1]):3===e.rank?e.as4D(1,e.shape[0],e.shape[1],e.shape[2]):e}var Ua=Ye({batchNormalization2d_:function(e,t,n,r,a,i){void 0===r&&(r=.001);var o,s,u=Ke(e,"x","batchNormalization"),l=Ke(t,"mean","batchNormalization"),c=Ke(n,"variance","batchNormalization");return null!=a&&(o=Ke(a,"scale","batchNormalization")),null!=i&&(s=Ke(i,"offset","batchNormalization")),d(2===u.rank,"Error in batchNormalization3D: x must be rank 3 but got rank "+u.rank+"."),d(2===l.rank||1===l.rank,"Error in batchNormalization2D: mean must be rank 2 or rank 1 but got rank "+l.rank+"."),d(2===c.rank||1===c.rank,"Error in batchNormalization2D: variance must be rank 2 or rank 1 but got rank "+c.rank+"."),null!=o&&d(2===o.rank||1===o.rank,"Error in batchNormalization2D: scale must be rank 2 or rank 1 but got rank "+o.rank+"."),null!=s&&d(2===s.rank||1===s.rank,"Error in batchNormalization2D: offset must be rank 2 or rank 1 but got rank "+s.rank+"."),$a(u,l,c,r,o,s)}}),Wa=Ye({batchNormalization3d_:function(e,t,n,r,a,i){void 0===r&&(r=.001);var o,s,u=Ke(e,"x","batchNormalization"),l=Ke(t,"mean","batchNormalization"),c=Ke(n,"variance","batchNormalization");return null!=a&&(o=Ke(a,"scale","batchNormalization")),null!=i&&(s=Ke(i,"offset","batchNormalization")),d(3===u.rank,"Error in batchNormalization3D: x must be rank 3 but got rank "+u.rank+"."),d(3===l.rank||1===l.rank,"Error in batchNormalization3D: mean must be rank 3 or rank 1 but got rank "+l.rank+"."),d(3===c.rank||1===c.rank,"Error in batchNormalization3D: variance must be rank 3 or rank 1 but got rank "+c.rank+"."),null!=o&&d(3===o.rank||1===o.rank,"Error in batchNormalization3D: scale must be rank 3 or rank 1 but got rank "+o.rank+"."),null!=s&&d(3===s.rank||1===s.rank,"Error in batchNormalization3D: offset must be rank 3 or rank 1 but got rank "+s.rank+"."),$a(u,l,c,r,o,s)}}),qa=Ye({batchNormalization4d_:function(e,t,n,r,a,i){void 0===r&&(r=.001);var o,s,u=Ke(e,"x","batchNormalization"),l=Ke(t,"mean","batchNormalization"),c=Ke(n,"variance","batchNormalization");return null!=a&&(o=Ke(a,"scale","batchNormalization")),null!=i&&(s=Ke(i,"offset","batchNormalization")),d(4===u.rank,"Error in batchNormalization4D: x must be rank 4 but got rank "+u.rank+"."),d(4===l.rank||1===l.rank,"Error in batchNormalization4D: mean must be rank 4 or rank 1 but got rank "+l.rank+"."),d(4===c.rank||1===c.rank,"Error in batchNormalization4D: variance must be rank 4 or rank 1 but got rank "+c.rank+"."),null!=o&&d(4===o.rank||1===o.rank,"Error in batchNormalization4D: scale must be rank 4 or rank 1 but got rank "+o.rank+"."),null!=s&&d(4===s.rank||1===s.rank,"Error in batchNormalization4D: offset must be rank 4 or rank 1 but got rank "+s.rank+"."),$a(u,l,c,r,o,s)}}),$a=Ye({batchNormalization_:function(e,t,n,r,a,i){void 0===r&&(r=.001);var o,s,u,l=Ke(e,"x","batchNormalization"),c=Ke(t,"mean","batchNormalization"),f=Ke(n,"variance","batchNormalization");return null!=a&&(o=Ke(a,"scale","batchNormalization")),null!=i&&(s=Ke(i,"offset","batchNormalization")),d(c.rank===f.rank,"Batch normalization gradient requires mean and variance to have equal ranks."),d(null==s||c.rank===s.rank,"Batch normalization gradient requires mean and offset to have equal ranks."),d(null==o||c.rank===o.rank,"Batch normalization gradient requires mean and scale to have equal ranks."),u=0===l.rank||1===l.rank?l.as4D(1,1,1,l.size):2===l.rank?l.as4D(1,1,l.shape[0],l.shape[1]):3===l.rank?l.as4D(1,l.shape[0],l.shape[1],l.shape[2]):l,me.engine.runKernel(function(e){return e.batchNormalization(u,Va(c),Va(f),r,Va(o),Va(s))},{$x:l,$mean:c,$variance:f,$scale:o,$offset:s},function(e){var t=null==o?nt(1):o,n=Mt(c.shape,u.shape),a=[];if(1===c.rank){for(var i=0;i=2&&i.rank>=2&&a.rank===i.rank,"Error in matMul: inputs must have the same rank of at least 2, got ranks "+a.rank+" and "+i.rank+"."),d(w(c,f),"Error in matMul: outer dimensions ("+c+") and ("+f+") of Tensors with shapes "+a.shape+" and "+i.shape+" must match."),d(o===s,"Error in matMul: inner shapes ("+o+") and ("+s+") of Tensors with shapes "+a.shape+" and "+i.shape+" and transposeA="+n+" and transposeB="+r+" must match.");var m=a.shape.slice(0,-2).concat([u,l]),g=n?a.as3D(p,o,u):a.as3D(p,u,o),y=r?i.as3D(h,l,s):i.as3D(h,s,l);return me.engine.runKernel(function(e){return e.batchMatMul(g,y,n,r)},{$a:g,$b:y},function(e){return n||r?!n&&r?{$a:function(){return e.matMul(y.toFloat(),!1,!1)},$b:function(){return e.matMul(g.toFloat(),!0,!1)}}:n&&!r?{$a:function(){return y.toFloat().matMul(e,!1,!0)},$b:function(){return g.toFloat().matMul(e,!1,!1)}}:{$a:function(){return y.toFloat().matMul(e,!0,!0)},$b:function(){return e.matMul(g.toFloat(),!0,!0)}}:{$a:function(){return e.matMul(y.toFloat(),!1,!0)},$b:function(){return g.toFloat().matMul(e,!0,!1)}}}).reshape(m)}}),oi=Ye({dot_:function(e,t){var n=Ke(e,"t1","dot"),r=Ke(t,"t2","dot");d(!(1!==n.rank&&2!==n.rank||1!==r.rank&&2!==r.rank),"Error in dot: inputs must all be rank 1 or 2, but got ranks "+n.rank+" and "+r.rank+".");var a=1===n.rank?n.size:n.shape[1],i=1===r.rank?r.size:r.shape[0];return d(a===i,"Error in dot: inner dimensions of inputs must match, but got "+a+" and "+i+"."),1===n.rank&&1===r.rank?n.as2D(1,-1).matMul(r.as2D(-1,1)).asScalar():1===n.rank&&2===r.rank?n.as2D(1,-1).matMul(r.as2D(r.shape[0],r.shape[1])).as1D():2===n.rank&&1===r.rank?n.matMul(r.as2D(-1,1)).as1D():n.matMul(r.as2D(r.shape[0],r.shape[1]))}}),si=Ye({outerProduct_:function(e,t){var n=Ke(e,"v1","outerProduct"),r=Ke(t,"v2","outerProduct");return d(1===n.rank&&1===r.rank,"Error in outerProduct: inputs must be rank 1, but got ranks "+n.rank+" and "+r.rank+"."),n.as2D(-1,1).matMul(r.as2D(1,-1))}}),ui=Ye({reverse_:function(e,t){var n=Ke(e,"x","reverse");if(0===n.rank)return n.clone();var r=Le(t,n.shape);return me.engine.runKernel(function(e){return e.reverse(n,r)},{$x:n},function(e){return{$x:function(){return e.reverse(r)}}}).reshapeAs(n)}}),li=Ye({reverse1d_:function(e){var t=Ke(e,"x","reverse");return d(1===t.rank,"Error in reverse1D: x must be rank 1 but got\n rank "+t.rank+"."),ui(t,0)}}),ci=Ye({reverse2d_:function(e,t){var n=Ke(e,"x","reverse");return d(2===n.rank,"Error in reverse2D: x must be rank 2 but got\n rank "+n.rank+"."),ui(n,t)}}),fi=Ye({reverse3d_:function(e,t){var n=Ke(e,"x","reverse");return d(3===n.rank,"Error in reverse3D: x must be rank 3 but got\n rank "+n.rank+"."),ui(n,t)}}),pi=Ye({reverse4d_:function(e,t){var n=Ke(e,"x","reverse");return d(4===n.rank,"Error in reverse4D: x must be rank 4 but got\n rank "+n.rank+"."),ui(n,t)}}),hi=Ye({maxPool_:function(e,t,n,r,a){var i=Ke(e,"x","maxPool"),o=i,s=!1;3===i.rank&&(s=!0,o=i.as4D(1,i.shape[0],i.shape[1],i.shape[2])),d(4===o.rank,"Error in maxPool: input must be rank 4 but got rank "+o.rank+"."),null!=a&&d(x(r),"Error in maxPool: pad must be an integer when using, dimRoundingMode "+a+" but got pad "+r+".");var u=Ga(o.shape,t,n,r,a),l=me.engine.runKernel(function(e,t){return t(e.maxPool(o,u))},{x:o},function(e,a){var i=a[0];return{x:function(){return function(e,t,n,r,a,i,o){var s=Ke(e,"dy","maxPoolBackprop"),u=Ke(t,"input","maxPoolBackprop"),l=Ke(n,"output","maxPoolBackprop");d(u.rank===s.rank,"Rank of input ("+u.rank+") does not match rank of dy ("+s.rank+")"),d(4===s.rank,"Error in maxPoolBackprop: dy must be rank 4 but got rank "+s.rank+"."),d(4===u.rank,"Error in maxPoolBackprop: input must be rank 4 but got rank "+u.rank+".");var c=Ga(u.shape,r,a,i,o);return me.engine.runKernel(function(e){return e.maxPoolBackprop(s,u,l,c)},{$dy:s,$input:u})}(e,o,i,t,n,r)}}});return s?l.as3D(l.shape[1],l.shape[2],l.shape[3]):l}}),di=Ye({avgPool_:function(e,t,n,r,a){var i=Ke(e,"x","avgPool");d("float32"===i.dtype,"The input dtype to avgPool must be float32");var o=i,s=!1;3===i.rank&&(s=!0,o=i.as4D(1,i.shape[0],i.shape[1],i.shape[2])),d(4===o.rank,"Error in avgPool: x must be rank 4 but got rank "+o.rank+"."),null!=a&&d(x(r),"Error in avgPool: pad must be an integer when using, dimRoundingMode "+a+" but got pad "+r+".");var u=Ga(o.shape,t,n,r),l=me.engine.runKernel(function(e){return e.avgPool(o,u)},{x:o},function(e){return{x:function(){return function(e,t,n,r,a){var i=Ke(e,"dy","avgPoolBackprop"),o=Ke(t,"input","avgPoolBackprop");d(o.rank===i.rank,"Rank of input ("+o.rank+") does not match rank of dy ("+i.rank+")");var s=o,u=i,l=!1;3===o.rank&&(l=!0,s=o.as4D(1,o.shape[0],o.shape[1],o.shape[2]),u=i.as4D(1,i.shape[0],i.shape[1],i.shape[2])),d(4===u.rank,"Error in avgPoolBackprop: dy must be rank 4 but got rank "+u.rank+"."),d(4===s.rank,"Error in avgPoolBackprop: input must be rank 4 but got rank "+s.rank+".");var c=Ga(s.shape,n,r,a),f=me.engine.runKernel(function(e){return e.avgPoolBackprop(u,s,c)},{dy4D:u,input4D:s});return l?f.as3D(f.shape[1],f.shape[2],f.shape[3]):f}(e,o,t,n,r)}}});return l=l.cast(i.dtype),s?l.as3D(l.shape[1],l.shape[2],l.shape[3]):l}}),mi=Ye({slice_:function(e,t,n){var r,a,i=Ke(e,"x","slice");if(0===i.rank)throw new Error("Slicing scalar is not possible");r="number"==typeof t?[t].concat(new Array(i.rank-1).fill(0)):t.length=0?e:(d(-1===e,"Bad value in size"),i.shape[t]-r[t])}),function(e,t,n){d(e.rank===t.length,"Error in slice"+e.rank+"D: Length of begin "+t+" must match the rank of the array ("+e.rank+")."),d(e.rank===n.length,"Error in slice"+e.rank+"D: Length of size "+n+" must match the rank of the array ("+e.rank+").");for(var r=0;r0&&(t=t.sum(r)),t.reshape(n.shape)},$b:function(){var t=e,n=Mt(r.shape,a);return n.length>0&&(t=t.sum(n)),t.reshape(r.shape)}}})}}),Wi=Ye({addN_:function(e){d(Array.isArray(e),function(){return"The argument passed to tf.addN() must be a list of tensors"}),d(e.length>=1,function(){return"Must pass at least one tensor to tf.addN(), but got "+e.length});var t=e.map(function(e,t){return Ke(e,"tensors"+t,"addN")}),n=t[0];t.forEach(function(e){if(e.dtype!==n.dtype)throw new Error("All tensors passed to tf.addN() must have the same dtype")}),t.forEach(function(e){if(!w(e.shape,n.shape))throw new Error("All tensors passed to tf.addN() must have the same shape")});var r=t;return me.engine.runKernel(function(e){return e.addN(t)},r,function(e){var n={};return t.forEach(function(t,r){n[r]=function(){return e.clone()}}),n})}}),qi=Ye({addStrict_:function(e,t){return m(e.shape,t.shape,"Error in addStrict: "),e.add(t)}}),$i=Ye({atan2_:function(e,t){var n=Ke(e,"a","atan2"),r=Ke(t,"b","atan2");ne(n,r);var a=Rt(n.shape,r.shape);return me.engine.runKernel(function(e){return e.atan2(n,r)},{$a:n,$b:r},function(e){return{$a:function(){var t=Ui(n.square(),r.square()),i=e.mul(r.div(t)),o=Mt(n.shape,a);return o.length>0&&(i=i.sum(o)),i.reshape(n.shape)},$b:function(){var t=Ui(n.square(),r.square()),i=ka(e.mul(n.div(t))),o=Mt(r.shape,a);return o.length>0&&(i=i.sum(o)),i.reshape(r.shape)}}})}}),Gi=Ye({div_:function(e,t){var n,r=Ke(e,"a","div"),a=Ke(t,"b","div");if(ne(r,a),"int32"===r.dtype&&"int32"===a.dtype)return Ki(r,a);n=function(e){return e.realDivide(r,a)};var i=Rt(r.shape,a.shape);return me.engine.runKernel(n,{$a:r,$b:a},function(e){return{$a:function(){var t=e.div(a.toFloat()),n=Mt(r.shape,i);return n.length>0?t.sum(n).reshape(r.shape):t},$b:function(){var t=e.mul(r.toFloat()),n=Mt(a.shape,i);n.length>0&&(t=t.sum(n).reshape(a.shape));var o=a.square();return t.div(o.toFloat()).neg()}}})}}),Hi=Ye({divStrict_:function(e,t){return m(e.shape,t.shape,"Error in divideStrict: "),e.div(t)}}),Ki=Ye({floorDiv_:function(e,t){var n=Ke(e,"a","floorDiv"),r=Ke(t,"b","floorDiv");ne(n,r);var a=Rt(n.shape,r.shape);return me.engine.runKernel(function(e){return e.floorDiv(n,r)},{$a:n,$b:r},function(e){return{$a:function(){var t=e.div(r.toFloat()),i=Mt(n.shape,a);return i.length>0?t.sum(i).reshape(n.shape):t},$b:function(){var t=e.mul(n.toFloat()),i=Mt(r.shape,a);i.length>0&&(t=t.sum(i).reshape(r.shape));var o=r.square();return t.div(o.toFloat()).neg()}}})}}),Xi=Ye({maximum_:function(e,t){var n=Ke(e,"a","maximum"),r=Ke(t,"b","maximum");return ne(n,r),"bool"===n.dtype&&(n=n.toInt()),"bool"===r.dtype&&(r=r.toInt()),Rt(n.shape,r.shape),me.engine.runKernel(function(e){return e.maximum(n,r)},{$a:n,$b:r},function(e){return{$a:function(){return e.mul(n.greaterEqual(r).toFloat())},$b:function(){return e.mul(n.less(r).toFloat())}}})}}),Yi=Ye({maximumStrict_:function(e,t){return m(e.shape,t.shape,"Error in maximumStrict: "),e.maximum(t)}}),Ji=Ye({minimum_:function(e,t){var n=Ke(e,"a","minimum"),r=Ke(t,"b","minimum");return ne(n,r),"bool"===n.dtype&&(n=n.toInt()),"bool"===r.dtype&&(r=r.toInt()),Rt(n.shape,r.shape),me.engine.runKernel(function(e){return e.minimum(n,r)},{$a:n,$b:r},function(e){return{$a:function(){return e.mul(n.lessEqual(r).toFloat())},$b:function(){return e.mul(n.greater(r).toFloat())}}})}}),Zi=Ye({minimumStrict_:function(e,t){return m(e.shape,t.shape,"Error in minimumStrict: "),e.minimum(t)}}),Qi=Ye({mod_:function(e,t){var n=Ke(e,"a","mod"),r=Ke(t,"b","mod");ne(n,r);var a=Rt(n.shape,r.shape);return me.engine.runKernel(function(e){return e.mod(n,r)},{$a:n,$b:r},function(e){return{$a:function(){var t=Mt(n.shape,a);return t.length>0?e.sum(t).reshape(n.shape):e},$b:function(){var t=e.mul(n.div(r).floor().neg()),i=Mt(r.shape,a);return i.length>0?t.sum(i).reshape(r.shape):t}}})}}),eo=Ye({modStrict_:function(e,t){return m(e.shape,t.shape,"Error in modStrict: "),e.mod(t)}}),to=Ye({mul_:function(e,t){var n=Ke(e,"a","mul"),r=Ke(t,"b","mul");ne(n,r);var a=Rt(n.shape,r.shape);return me.engine.runKernel(function(e){return e.multiply(n,r)},{$a:n,$b:r},function(e){return{$a:function(){var t=e.mul(r.toFloat()),i=Mt(n.shape,a);return i.length>0?t.sum(i).reshape(n.shape):t},$b:function(){var t=e.mul(n.toFloat()),i=Mt(r.shape,a);return i.length>0?t.sum(i).reshape(r.shape):t}}})}}),no=Ye({mulStrict_:function(e,t){return m(e.shape,t.shape,"Error in multiplyStrict: "),e.mul(t)}}),ro=Ye({pow_:function(e,t){var n=Ke(e,"base","pow"),r=Ke(t,"exp","pow"),a=Rt(n.shape,r.shape);return e=n.cast(St(n.dtype,r.dtype)),t=r.cast(St(n.dtype,r.dtype)),me.engine.runKernel(function(e,t){return t(e.pow(n,r))},{$base:n,$exp:r},function(e,t){var i=t[0];return{$base:function(){var t=e.mul(r.toFloat().mul(i.div(n))),o=Mt(n.shape,a);return o.length>0&&(t=t.sum(o)),t.reshape(n.shape)},$exp:function(){var t=e.mul(i.mul(n.log()).toFloat()),o=Mt(r.shape,a);return o.length>0&&(t=t.sum(o)),t.reshape(r.shape)}}})}}),ao=Ye({powStrict_:function(e,t){return m(e.shape,t.shape,"Error in powStrict: "),e.pow(t)}}),io=Ye({squaredDifference_:function(e,t){var n=Ke(e,"a","squaredDifference"),r=Ke(t,"b","squaredDifference");return ne(n,r),Rt(n.shape,r.shape),me.engine.runKernel(function(e){return e.squaredDifference(n,r)},{$a:n,$b:r},function(e){var t=nt(2);return{$a:function(){return e.mul(n.sub(r).mul(t))},$b:function(){return e.mul(r.sub(n).mul(t))}}})}}),oo=Ye({squaredDifferenceStrict_:function(e,t){return m(e.shape,t.shape,"Error in squaredDifferenceStrict: "),e.squaredDifference(t)}}),so=Ye({sub_:function(e,t){var n=Ke(e,"a","sub"),r=Ke(t,"b","sub");ne(n,r);var a=Rt(n.shape,r.shape);return me.engine.runKernel(function(e){return e.subtract(n,r)},{$a:n,$b:r},function(e){return{$a:function(){var t=e,r=Mt(n.shape,a);return r.length>0&&(t=t.sum(r)),t.reshape(n.shape)},$b:function(){var t=e,n=Mt(r.shape,a);return n.length>0&&(t=t.sum(n)),t.neg().reshape(r.shape)}}})}}),uo=Ye({subStrict_:function(e,t){return m(e.shape,t.shape,"Error in subStrict: "),e.sub(t)}}),lo=Ye({logicalAnd_:function(e,t){var n=Ke(e,"a","logicalAnd","bool"),r=Ke(t,"b","logicalAnd","bool");return d("bool"===n.dtype&&"bool"===r.dtype,"Error Array must be of type bool."),Rt(n.shape,r.shape),me.engine.runKernel(function(e){return e.logicalAnd(n,r)},{$a:n,$b:r})}}),co=Ye({logicalNot_:function(e){var t=Ke(e,"x","logicalNot","bool");return d("bool"===t.dtype,"Error Array must be of type bool."),me.engine.runKernel(function(e){return e.logicalNot(t)},{$x:t})}}),fo=Ye({logicalOr_:function(e,t){var n=Ke(e,"a","logicalOr","bool"),r=Ke(t,"b","logicalOr","bool");return d("bool"===n.dtype&&"bool"===r.dtype,"Error Array must be of type bool."),Rt(n.shape,r.shape),me.engine.runKernel(function(e){return e.logicalOr(n,r)},{$a:n,$b:r})}}),po=Ye({logicalXor_:function(e,t){var n=Ke(e,"a","logicalXor","bool"),r=Ke(t,"b","logicalXor","bool");return d("bool"===n.dtype&&"bool"===r.dtype,"Error Array must be of type bool."),Rt(n.shape,r.shape),fo(e,t).logicalAnd(lo(e,t).logicalNot())}}),ho=Ye({where_:function(e,t,n){var r=Ke(t,"a","where"),a=Ke(n,"b","where"),i=Ke(e,"condition","where","bool");return d("bool"===i.dtype,"Error Condition must be of type bool."),m(r.shape,a.shape,"Error in where: "),1===i.rank?d(i.shape[0]===r.shape[0],"The first dimension of `a` must match the size of `condition`."):m(i.shape,a.shape,"Error in where: "),me.engine.runKernel(function(e){return e.select(i,r,a)},{$condition:i,$a:r,$b:a},function(e){return{$condition:function(){return xt(i)},$a:function(){return e.mul(i.cast(r.dtype))},$b:function(){return e.mul(i.logicalNot().cast(a.dtype))}}})}}),mo=function(e){return l(this,void 0,void 0,function(){var t,n,r;return c(this,function(a){switch(a.label){case 0:return d("bool"===(t=Ke(e,"condition","where","bool")).dtype,"Condition must be of type bool."),[4,t.data()];case 1:return n=a.sent(),r=oa(t.shape,n),e!==t&&t.dispose(),[2,r]}})})},go=Ye({elu_:function(e){var t=Ke(e,"x","elu");return me.engine.runKernel(function(e,n){return n(e.elu(t))},{$x:t},function(e,t){var n=t[0];return{$x:function(){return me.engine.runKernel(function(t){return t.eluDer(e,n)},{dy:e,y:n})}}})}}),yo=Ye({leakyRelu_:function(e,t){void 0===t&&(t=.2);var n=Ke(e,"x","leakyRelu");return Xi(nt(t).mul(n),n)}}),vo=Ye({prelu_:function(e,t){var n=Ke(e,"x","prelu"),r=Ke(t,"alpha","prelu"),a=nt(0);return Xi(a,n).add(r.mul(Ji(a,n)))}}),bo=Ye({relu_:function(e){var t=Ke(e,"x","relu");return"bool"===t.dtype?t.toInt():me.engine.runKernel(function(e){return e.relu(t)},{$x:t},function(e){var n=t.step();return{$x:function(){return e.mulStrict(n.toFloat())}}})}}),wo=Ye({selu_:function(e){var t=Ke(e,"x","selu");return me.engine.runKernel(function(e){return e.selu(t)},{$x:t},function(e){return{$x:function(){var n=t.greater(nt(0)),r=nt(pr),a=nt(hr),i=e.mul(a),o=e.mul(r).mul(t.toFloat().exp());return ho(n,i,o)}}})}}),xo=Ye({transpose_:function(e,t){var n=Ke(e,"x","transpose");return null==t&&(t=n.shape.map(function(e,t){return t}).reverse()),d(n.rank===t.length,"Error in transpose: rank of input "+n.rank+" must match length of perm "+t+"."),t.forEach(function(e){d(e>=0&&ea)throw new Error("'k' passed to topk() must be <= the last dimension ("+a+") but got "+t);var i=me.engine.runKernel(function(e){return e.topk(r,t,n)},{$x:r});return{values:i[0],indices:i[1]}}});!function(e){e[e.NONE=0]="NONE",e[e.MEAN=1]="MEAN",e[e.SUM=2]="SUM",e[e.SUM_BY_NONZERO_WEIGHTS=3]="SUM_BY_NONZERO_WEIGHTS"}(Oo||(Oo={}));var Ro=Ye({absoluteDifference_:function(e,t,n,r){void 0===r&&(r=Oo.SUM_BY_NONZERO_WEIGHTS);var a=Ke(e,"labels","absoluteDifference"),i=Ke(t,"predictions","absoluteDifference"),o=null;null!=n&&(o=Ke(n,"weights","absoluteDifference")),m(a.shape,i.shape,"Error in absoluteDifference: ");var s=a.sub(i).abs();return Do(s,o,r)}}),Do=Ye({computeWeightedLoss_:function(e,t,n){void 0===n&&(n=Oo.SUM_BY_NONZERO_WEIGHTS);var r=Ke(e,"losses","computeWeightedLoss"),a=null;null!=t&&(a=Ke(t,"weights","computeWeightedLoss"));var i=null==a?r:r.mul(a);if(n===Oo.NONE)return i;if(n===Oo.SUM)return i.sum();if(n===Oo.MEAN){if(null==a)return i.mean();var o=b(r.shape)/b(a.shape),s=i.sum().div(a.sum());return o>1?s.div(nt(o)):s}if(n===Oo.SUM_BY_NONZERO_WEIGHTS){if(null==a)return i.sum().div(nt(r.size));var u=a.mul(lt(r.shape)).notEqual(nt(0)).sum().toFloat();return i.sum().div(u)}throw Error("Unknown reduction: "+n)}}),jo=Ye({cosineDistance_:function(e,t,n,r,a){void 0===a&&(a=Oo.SUM_BY_NONZERO_WEIGHTS);var i=Ke(e,"labels","cosineDistance"),o=Ke(t,"predictions","cosineDistance"),s=null;null!=r&&(s=Ke(r,"weights","cosineDistance")),m(i.shape,o.shape,"Error in cosineDistance: ");var u=nt(1).sub(i.mul(o).sum(n,!0));return Do(u,s,a)}}),Lo=Ye({hingeLoss_:function(e,t,n,r){void 0===r&&(r=Oo.SUM_BY_NONZERO_WEIGHTS);var a=Ke(e,"labels","hingeLoss"),i=Ke(t,"predictions","hingeLoss"),o=null;null!=n&&(o=Ke(n,"weights","hingeLoss")),m(a.shape,i.shape,"Error in hingeLoss: ");var s=nt(1);a=nt(2).mul(a).sub(s);var u=s.sub(a.mul(i)).relu();return Do(u,o,r)}}),zo=Ye({huberLoss_:function(e,t,n,r,a){void 0===r&&(r=1),void 0===a&&(a=Oo.SUM_BY_NONZERO_WEIGHTS);var i=Ke(e,"labels","huberLoss"),o=Ke(t,"predictions","huberLoss"),s=null;null!=n&&(s=Ke(n,"weights","huberLoss")),m(i.shape,o.shape,"Error in huberLoss: ");var u=nt(r),l=o.sub(i).abs(),c=Ji(l,u),f=l.sub(c),p=nt(.5).mul(c.square()).add(u.mul(f));return Do(p,s,a)}}),Bo=Ye({logLoss_:function(e,t,n,r,a){void 0===r&&(r=1e-7),void 0===a&&(a=Oo.SUM_BY_NONZERO_WEIGHTS);var i=Ke(e,"labels","logLoss"),o=Ke(t,"predictions","logLoss"),s=null;null!=n&&(s=Ke(n,"weights","logLoss")),m(i.shape,o.shape,"Error in logLoss: ");var u=nt(1),l=nt(r),c=i.mul(o.add(l).log()).neg().sub(u.sub(i).mul(u.sub(o).add(l).log()));return Do(c,s,a)}}),Fo=Ye({meanSquaredError_:function(e,t,n,r){void 0===r&&(r=Oo.SUM_BY_NONZERO_WEIGHTS);var a=Ke(e,"labels","meanSquaredError"),i=Ke(t,"predictions","meanSquaredError"),o=null;null!=n&&(o=Ke(n,"weights","meanSquaredError")),m(a.shape,i.shape,"Error in meanSquaredError: ");var s=a.squaredDifference(i);return Do(s,o,r)}}),Vo=Ye({sigmoidCrossEntropy_:function(e,t,n,r,a){void 0===r&&(r=0),void 0===a&&(a=Oo.SUM_BY_NONZERO_WEIGHTS);var i=Ke(e,"multiClassLabels","sigmoidCrossEntropy"),o=Ke(t,"logits","sigmoidCrossEntropy"),s=null;if(null!=n&&(s=Ke(n,"weights","sigmoidCrossEntropy")),m(i.shape,o.shape,"Error in sigmoidCrossEntropy: "),r>0){var u=nt(r),l=nt(1),c=nt(.5);i=i.mul(l.sub(u)).add(c.mul(u))}var f=function(e,t){var n=Ke(e,"labels","sigmoidCrossEntropyWithLogits"),r=Ke(t,"logits","sigmoidCrossEntropyWithLogits");m(n.shape,r.shape,"Error in sigmoidCrossEntropyWithLogits: ");var a=r.relu(),i=r.mul(n),o=r.abs().neg().exp().log1p();return a.sub(i).add(o)}(i,o);return Do(f,s,a)}}),Uo=Ye({softmaxCrossEntropy_:function(e,t,n,r,a){void 0===r&&(r=0),void 0===a&&(a=Oo.SUM_BY_NONZERO_WEIGHTS);var i=Ke(e,"onehotLabels","softmaxCrossEntropy"),o=Ke(t,"logits","softmaxCrossEntropy"),s=null;if(null!=n&&(s=Ke(n,"weights","softmaxCrossEntropy")),m(i.shape,o.shape,"Error in softmaxCrossEntropy: "),r>0){var u=nt(r),l=nt(1),c=nt(i.shape[1]);i=i.mul(l.sub(u)).add(u.div(c))}var f=function(e,t,n){if(void 0===n&&(n=-1),-1===n&&(n=t.rank-1),n!==t.rank-1)throw Error("Softmax cross entropy along a non-last dimension is not yet supported. Labels / logits was rank "+t.rank+" and dim was "+n);return _e(function(e,t){var r=t.logSumExp([n],!0),a=t.toFloat().sub(r);return{value:a.mul(e).neg().sum([n]),gradFunc:function(t){var r=je(t.shape,[n]);return[t.reshape(r).mul(e.toFloat().sub(a.exp())),t.reshape(r).mul(a.exp().sub(e.toFloat()))]}}})(e,t)}(i,o);return Do(f,s,a)}}),Wo=Object.freeze({get Reduction(){return Oo},absoluteDifference:Ro,computeWeightedLoss:Do,cosineDistance:jo,hingeLoss:Lo,huberLoss:zo,logLoss:Bo,meanSquaredError:Fo,sigmoidCrossEntropy:Vo,softmaxCrossEntropy:Uo});function qo(e,t){return void 0===t&&(t=!1),me.engine.tidy(function(){if(2!==e.shape.length)throw new Error("qr2d() requires a 2D Tensor, but got a "+e.shape.length+"D Tensor.");for(var n=e.shape[0],r=e.shape[1],a=Vr(n),i=e.clone(),o=at([[1]],[1,1]),s=o.clone(),u=n>=r?r:n,l=function(e){var t,u=i,l=s,c=a;t=me.engine.tidy(function(){var t=i.slice([e,e],[n-e,1]),u=t.norm(),l=i.slice([e,e],[1,1]),c=l.sign().neg(),f=l.sub(c.mul(u)),p=t.div(f);s=1===p.shape[0]?o.clone():o.concat(p.slice([1,0],[p.shape[0]-1,p.shape[1]]),0);var h=c.matMul(f).div(u).neg(),d=i.slice([e,0],[n-e,r]),m=h.mul(s);i=0===e?d.sub(m.matMul(s.transpose().matMul(d))):i.slice([0,0],[e,r]).concat(d.sub(m.matMul(s.transpose().matMul(d))),0);var g=a.slice([0,e],[n,a.shape[1]-e]);return a=0===e?g.sub(g.matMul(s).matMul(m.transpose())):a.slice([0,0],[n,e]).concat(g.sub(g.matMul(s).matMul(m.transpose())),1),[s,i,a]}),s=t[0],i=t[1],a=t[2],Oe([u,l,c])},c=0;cr&&(a=a.slice([0,0],[n,r]),i=i.slice([0,0],[r,r])),[a,i]})}var $o=Ye({gramSchmidt_:function(e){var t;if(Array.isArray(e)){t=!1,d(null!=e&&e.length>0,"Gram-Schmidt process: input must not be null, undefined, or empty");for(var n=e[0].shape[0],r=1;r0)for(var n=0;n= 2, but got rank "+e.rank);if(2===e.rank)return qo(e,t);var n=e.shape.slice(0,e.shape.length-2).reduce(function(e,t){return e*t}),r=[],a=[];return ia(e.reshape([n,e.shape[e.shape.length-2],e.shape[e.shape.length-1]]),0).forEach(function(e){var n=qo(e,t),i=n[0],o=n[1];r.push(i),a.push(o)}),[na(r,0).reshape(e.shape),na(a,0).reshape(e.shape)]}}),Ho=Object.freeze({gramSchmidt:$o,qr:Go});function Ko(e,t,n,r,a){null==r&&(r=.5),null==a&&(a=Number.NEGATIVE_INFINITY);var i=e.shape[0];return n=Math.min(n,i),d(0<=r&&r<=1,"iouThreshold must be in [0, 1], but was '"+r+"'"),d(2===e.rank,"boxes must be a 2D tensor, but was of rank '"+e.rank+"'"),d(4===e.shape[1],"boxes must have 4 columns, but 2nd dimension was "+e.shape[1]),d(1===t.rank,"scores must be a 1D tensor"),d(t.shape[0]===i,"scores has incompatible shape with boxes. Expected "+i+", but was "+t.shape[0]),{maxOutputSize:n,iouThreshold:r,scoreThreshold:a}}var Xo=Ye({resizeBilinear_:function(e,t,n){void 0===n&&(n=!1);var r=Ke(e,"images","resizeBilinear");d(3===r.rank||4===r.rank,"Error in resizeBilinear: x must be rank 3 or 4, but got rank "+r.rank+"."),d(2===t.length,"Error in resizeBilinear: new shape must 2D, but got shape "+t+".");var a=r,i=!1;3===r.rank&&(i=!0,a=r.as4D(1,r.shape[0],r.shape[1],r.shape[2]));var o=t[0],s=t[1],u=me.engine.runKernel(function(e,t){return e.resizeBilinear(a,o,s,n)},{batchImages:a},function(e,t){return{batchImages:function(){return me.engine.runKernel(function(t){return t.resizeBilinearBackprop(e,a,n)},{})}}});return i?u.as3D(u.shape[1],u.shape[2],u.shape[3]):u}}),Yo=Ye({resizeNearestNeighbor_:function(e,t,n){void 0===n&&(n=!1);var r=Ke(e,"images","resizeNearestNeighbor");d(3===r.rank||4===r.rank,"Error in resizeNearestNeighbor: x must be rank 3 or 4, but got rank "+r.rank+"."),d(2===t.length,"Error in resizeNearestNeighbor: new shape must 2D, but got shape "+t+"."),d("float32"===r.dtype||"int32"===r.dtype,"`images` must have `int32` or `float32` as dtype");var a=r,i=!1;3===r.rank&&(i=!0,a=r.as4D(1,r.shape[0],r.shape[1],r.shape[2]));var o=t[0],s=t[1],u=me.engine.runKernel(function(e,t){return e.resizeNearestNeighbor(a,o,s,n)},{batchImages:a},function(e,t){return{batchImages:function(){return me.engine.runKernel(function(t){return t.resizeNearestNeighborBackprop(e,a,n)},{})}}});return i?u.as3D(u.shape[1],u.shape[2],u.shape[3]):u}}),Jo=Ye({nonMaxSuppression_:function(e,t,n,r,a){void 0===r&&(r=.5),void 0===a&&(a=Number.NEGATIVE_INFINITY);var i=Ke(e,"boxes","nonMaxSuppression"),o=Ke(t,"scores","nonMaxSuppression"),s=Ko(i,o,n,r,a);return n=s.maxOutputSize,r=s.iouThreshold,a=s.scoreThreshold,me.engine.runKernel(function(e){return e.nonMaxSuppression(i,o,n,r,a)},{$boxes:i})}}),Zo=Object.freeze({resizeBilinear:Xo,resizeNearestNeighbor:Yo,nonMaxSuppression:Jo,nonMaxSuppressionAsync:function(e,t,n,r,a){return void 0===r&&(r=.5),void 0===a&&(a=Number.NEGATIVE_INFINITY),l(this,void 0,void 0,function(){var i,o,s,u,l,f;return c(this,function(c){switch(c.label){case 0:return i=Ke(e,"boxes","nonMaxSuppressionAsync"),o=Ke(t,"scores","nonMaxSuppressionAsync"),s=Ko(i,o,n,r,a),n=s.maxOutputSize,r=s.iouThreshold,a=s.scoreThreshold,[4,i.data()];case 1:return u=c.sent(),[4,o.data()];case 2:return l=c.sent(),f=It(u,l,n,r,a),i!==e&&i.dispose(),o!==t&&o.dispose(),[2,f]}})})},cropAndResize:function(e,t,n,r,a,i){var o=Ke(e,"image","cropAndResize","float32"),s=Ke(t,"boxes","cropAndResize","float32"),u=Ke(n,"boxInd","cropAndResize","int32");a=a||"bilinear",i=i||0;var l=s.shape[0];return d(4===o.rank,"Error in cropAndResize: image must be rank 4,but got rank "+o.rank+"."),d(2===s.rank&&4===s.shape[1],"Error in cropAndResize: boxes must be have size ["+l+",4] but had shape "+s.shape+"."),d(1===u.rank&&u.shape[0]===l,"Error in cropAndResize: boxInd must be have size ["+l+"] but had shape "+s.shape+"."),d("int32"===u.dtype,"Error in cropAndResize: boxInd must be of dtype int32, but got dtype "+u.dtype+"."),d(2===r.length,"Error in cropAndResize: cropSize must be of length 2, but got length "+r.length+"."),d(r[0]>=1&&r[1]>=1,"cropSize must be atleast [1,1], but was "+r),d("bilinear"===a||"nearest"===a,"method must be bilinear or nearest, but was "+a),me.engine.runKernel(function(e,t){return e.cropAndResize(o,s,u,r,a,i)},{$image:o,$boxes:s})}}),Qo=Object.freeze({image:Zo,linalg:Ho,losses:Wo,op:Ye,batchNormalization2d:Ua,batchNormalization3d:Wa,batchNormalization4d:qa,batchNormalization:$a,complex:Ze,real:Qe,imag:et,concat:gr,concat1d:yr,concat2d:vr,concat3d:br,concat4d:wr,split:xr,conv1d:ei,conv2d:ti,depthwiseConv2d:ni,separableConv2d:ri,conv2dTranspose:ai,matMul:ii,dot:oi,outerProduct:si,reverse:ui,reverse1d:li,reverse2d:ci,reverse3d:fi,reverse4d:pi,maxPool:hi,avgPool:di,slice:mi,slice1d:gi,slice2d:yi,slice3d:vi,slice4d:bi,abs:la,acos:ca,acosh:fa,asin:pa,asinh:ha,atan:da,atanh:ma,ceil:ga,clipByValue:ya,cos:va,cosh:ba,erf:wa,exp:xa,expm1:_a,floor:Sa,log:Ea,log1p:Na,logSigmoid:Oa,neg:ka,reciprocal:Ia,round:Ca,rsqrt:Aa,sigmoid:Pa,sign:Ta,sin:Ma,sinh:Ra,softplus:Da,sqrt:ja,square:La,step:za,tan:Ba,tanh:Fa,all:wi,any:xi,argMax:_i,argMin:Si,logSumExp:Ei,max:Ni,mean:Oi,min:ki,moments:Ii,sum:Ci,equal:Ai,equalStrict:Pi,greater:Ti,greaterEqual:Mi,greaterEqualStrict:Ri,greaterStrict:Di,less:ji,lessEqual:Li,lessEqualStrict:zi,lessStrict:Bi,notEqual:Fi,notEqualStrict:Vi,add:Ui,addN:Wi,addStrict:qi,atan2:$i,div:Gi,divStrict:Hi,floorDiv:Ki,maximum:Xi,maximumStrict:Yi,minimum:Ji,minimumStrict:Zi,mod:Qi,modStrict:eo,mul:to,mulStrict:no,pow:ro,powStrict:ao,squaredDifference:io,squaredDifferenceStrict:oo,sub:so,subStrict:uo,elu:go,leakyRelu:yo,prelu:vo,relu:bo,selu:wo,logicalAnd:lo,logicalNot:co,logicalOr:fo,logicalXor:po,where:ho,whereAsync:mo,buffer:Mr,toPixels:Tr,print:Rr,batchToSpaceND:Dr,cast:jr,clone:Lr,cumsum:zr,depthToSpace:Br,expandDims:Fr,eye:Vr,fromPixels:Ur,multinomial:Wr,oneHot:qr,pad:$r,pad1d:Gr,pad2d:Hr,pad3d:Kr,pad4d:Xr,rand:Yr,randomNormal:Jr,randomUniform:Zr,reshape:Qr,spaceToBatchND:ea,squeeze:ta,stack:na,tile:ra,truncatedNormal:aa,unstack:ia,fill:ft,linspace:pt,ones:lt,range:ht,scalar:nt,tensor:tt,tensor1d:rt,tensor2d:at,tensor3d:it,tensor4d:ot,tensor5d:st,tensor6d:ut,zeros:ct,onesLike:wt,zerosLike:xt,transpose:xo,softmax:Je,localResponseNormalization:_o,norm:So,gather:ko,unsortedSegmentSum:Io,basicLSTMCell:Co,multiRNNCell:Ao,movingAverage:Po,stridedSlice:To,topk:Mo}),es=function(){function e(){this.blockSize=48,this.data=new WeakMap,this.firstUse=!0,me.get("IS_BROWSER")&&(this.canvas=document.createElement("canvas"))}return e.prototype.register=function(e,t,n){if(this.firstUse&&(this.firstUse=!1,me.get("IS_NODE")&&Ie("\n============================\nHi there 👋. Looks like you are running TensorFlow.js in Node.js. To speed things up dramatically, install our node backend, which binds to TensorFlow C++, by running npm i @tensorflow/tfjs-node, or npm i @tensorflow/tfjs-node-gpu if you have CUDA. Then call require('@tensorflow/tfjs-node'); (-gpu suffix for CUDA) at the start of your program. Visit https://github.com/tensorflow/tfjs-node for more details.\n============================\n")),this.data.has(e))throw new Error("Data buffer is already registered");this.data.set(e,{dtype:n})},e.prototype.write=function(e,t){if(null==t)throw new Error("MathBackendCPU.write(): values can not be null");this.throwIfNoData(e),this.data.get(e).values=t},e.prototype.fromPixels=function(e,t){if(null==e)throw new Error("pixels passed to tf.fromPixels() can not be null");var n,r;if(me.get("IS_NODE")&&null==e.getContext)throw new Error("When running in node, pixels must be an HTMLCanvasElement like the one returned by the `canvas` npm package");if(null!=e.getContext)n=e.getContext("2d").getImageData(0,0,e.width,e.height).data;else if(e instanceof ImageData)n=e.data;else{if(!(e instanceof HTMLImageElement||e instanceof HTMLVideoElement))throw new Error("pixels passed to tf.fromPixels() must be either an HTMLVideoElement, HTMLImageElement, HTMLCanvasElement or ImageData, but was "+e.constructor.name);if(null==this.canvas)throw new Error("Can't read pixels from HTMLImageElement outside the browser.");this.canvas.width=e.width,this.canvas.height=e.height,this.canvas.getContext("2d").drawImage(e,0,0,e.width,e.height),n=this.canvas.getContext("2d").getImageData(0,0,e.width,e.height).data}if(4===t)r=new Int32Array(n);else{var a=e.width*e.height;r=new Int32Array(a*t);for(var i=0;ip&&(p=m,h=d)}u[c]=h}return o},e.prototype.cumsum=function(e,t,n,r){if(this.assertNotComplex(e,"cumsum"),t!==e.rank-1)throw new Error("backend.cumsum in CPU expects an inner-most axis="+(e.rank-1)+" but got axis="+t);for(var a=St(e.dtype,"int32"),i=ct(e.shape,a),o=i.dataSync(),s=e.dataSync(),u=e.shape[e.rank-1],l=r?function(e,t){return e+u-t-1}:function(e,t){return e+t},c=0;ct?1:0})},e.prototype.greaterEqual=function(e,t){return this.assertNotComplex([e,t],"greaterEqual"),this.broadcastedBinaryOp(e,t,"bool",function(e,t){return e>=t?1:0})},e.prototype.logicalNot=function(e){this.assertNotComplex(e,"logicalNot");for(var t=e.dataSync(),n=new Int32Array(t.length),r=0;r1||1===t.rank?1:t.shape[1],c=0;c=0&&t>=0?n:(n+t)%t})},e.prototype.max=function(e,t){this.assertNotComplex(e,"max"),ze("max",t,e.rank);for(var n=De(e.shape,t),r=n[0],a=n[1],i=ct(r,e.dtype),o=b(a),s=i.dataSync(),u=e.dataSync(),l=0;lf&&(f=h)}s[l]=f}return i},e.prototype.maximum=function(e,t){return this.assertNotComplex([e,t],"maximum"),this.broadcastedBinaryOp(e,t,e.dtype,function(e,t){return Math.max(e,t)})},e.prototype.all=function(e,t){this.assertNotComplex(e,"all"),ze("all",t,e.rank);for(var n=De(e.shape,t),r=n[0],a=n[1],i=ct(r,e.dtype),o=b(a),s=i.dataSync(),u=e.dataSync(),l=0;l0?n[r]=1:n[r]=0;return Q.make(e.shape,{values:n})},e.prototype.round=function(e){this.assertNotComplex(e,"round");for(var t=e.dataSync(),n=new Float32Array(t.length),r=0;r.5?n[r]=Math.ceil(t[r]):n[r]=a%2==0?a:a+1}return Q.make(e.shape,{values:n})},e.prototype.exp=function(e){this.assertNotComplex(e,"exp");for(var t=e.dataSync(),n=new Float32Array(t.length),r=0;r=0?a:Math.exp(a)-1}return Q.make(e.shape,{values:t})},e.prototype.eluDer=function(e,t){this.assertNotComplex([e,t],"eluDer");for(var n=new Float32Array(t.size),r=t.dataSync(),a=e.dataSync(),i=0;i=1?a[i]:a[i]*(o+1)}return Q.make(t.shape,{values:n})},e.prototype.selu=function(e){this.assertNotComplex(e,"selu");for(var t=new Float32Array(e.size),n=e.dataSync(),r=0;r=0?1.0507009873554805*a:1.7580993408473768*(Math.exp(a)-1)}return Q.make(e.shape,{values:t})},e.prototype.clip=function(e,t,n){this.assertNotComplex(e,"clip");for(var r=new Float32Array(e.size),a=e.dataSync(),i=0;in?n:o-t,s=r[a]0?1:t}return Q.make(e.shape,{values:n})},e.prototype.conv2d=function(e,t,n){this.assertNotComplex([e,t],"conv2d");for(var r=n.filterHeight,a=n.filterWidth,i=n.dilationHeight,o=n.dilationWidth,s=n.padInfo.left,u=n.padInfo.top,l=Mr(n.outShape,e.dtype),c=e.dataSync(),f=t.dataSync(),p=l.values,h=0;h=n.inHeight))for(var x=b*t.strides[0],_=d+w*e.strides[1],S=0;S=n.inWidth))for(var I=x+O*t.strides[1],C=_+k*n.inChannels,A=I,P=0;P=n.inHeight))for(var _=w*t.strides[0],S=m+x*e.strides[1],E=0;E=n.inWidth))for(var C=_+k*t.strides[1],A=S+I*n.inChannels,P=N,T=C,M=0;Mx?x=O:"avg"===n&&(_+=O,S++)}if(isNaN(x))break}s.set("avg"===n?_/S:x,f,h,y,p)}return s.toTensor()},e.prototype.maxPool=function(e,t){return this.pool(e,t,"max")},e.prototype.maxPoolPositions=function(e,t){for(var n=Mr(t.outShape,"int32"),r=t.strideHeight,a=t.strideWidth,i=t.filterHeight,o=t.filterWidth,s=t.padInfo.top,u=t.padInfo.left,l=0;lb&&(b=N,w=_*o+E)}n.set(w,l,f,m,c)}return n.toTensor()},e.prototype.maxPoolBackprop=function(e,t,n,r){this.assertNotComplex([t,n],"maxPoolBackprop");for(var a=this.maxPoolPositions(t,r),i=r.strideHeight,o=r.strideWidth,s=r.filterHeight,u=r.filterWidth,l=u-1-r.padInfo.left,c=s-1-r.padInfo.top,f=Mr(t.shape,"float32"),p=0;p=r.outHeight||Math.floor(w)!==w))for(var x=0;x=r.outWidth||Math.floor(_)!==_)){var S=s*u-1-a.get(p,w,_,h)===b*u+x?1:0;0!==S&&(v+=e.get(p,w,_,h)*S)}}}f.set(v,p,d,m,h)}return f.toTensor()},e.prototype.avgPoolBackprop=function(e,t,n){this.assertNotComplex([e,t],"avgPoolBackprop");for(var r=n.strideHeight,a=n.strideWidth,i=n.filterHeight,o=n.filterWidth,s=o-1-n.padInfo.left,u=i-1-n.padInfo.top,l=Mr(t.shape,"float32"),c=1/(i*o),f=0;f=n.outHeight||Math.floor(b)!==b))for(var w=0;w=n.outWidth||Math.floor(x)!==x||(y+=e.get(f,b,x,p))}}l.set(y*c,f,h,d,p)}return l.toTensor()},e.prototype.cast=function(e,t){return Nt(e,t,this)},e.prototype.reshape=function(e,t){return Ot(e,t)},e.prototype.avgPool=function(e,t){return this.assertNotComplex(e,"avgPool"),this.pool(e,t,"avg").toFloat()},e.prototype.resizeBilinear=function(e,t,n,r){this.assertNotComplex(e,"resizeBilinear");for(var a=e.shape,i=a[0],o=a[1],s=a[2],u=a[3],l=Mr([i,t,n,u],e.dtype),c=[r&&t>1?o-1:o,r&&n>1?s-1:s],f=[r&&t>1?t-1:t,r&&n>1?n-1:n],p=0;p1?i-1:i,n&&c>1?o-1:o],h=[n&&l>1?l-1:l,n&&c>1?c-1:c],d=p[0]/h[0],m=p[1]/h[1],g=0;g1?o-1:o,r&&n>1?s-1:s],f=[r&&t>1?t-1:t,r&&n>1?n-1:n],p=0;p1?i-1:i,n&&c>1?o-1:o],h=[n&&l>1?l-1:l,n&&c>1?c-1:c],d=1/(p[0]/h[0]),m=1/(p[1]/h[1]),g=2*Math.ceil(d)+2,y=2*Math.ceil(m)+2,v=0;v=l))for(var C=0;C=c)){var P=p[0]*(I/h[0]),T=p[1]*(A/h[1]),M=Math.min(i-1,n?Math.round(P):Math.floor(P)),R=Math.min(o-1,n?Math.round(T):Math.floor(T));b===M&&w===R&&(O+=e.get(v,I,A,N))}}}f.set(O,v,b,w,N)}return f.toTensor()},e.prototype.batchNormalization=function(e,t,n,r,a,i){this.assertNotComplex([e,t,n,a,i],"batchNormalization");for(var o=e.dataSync(),s=t.dataSync(),u=n.dataSync(),l=a?a.dataSync():new Float32Array([1]),c=i?i.dataSync():new Float32Array([0]),f=new Float32Array(o.length),p=c.length,h=l.length,d=u.length,m=s.length,g=0,y=0,v=0,b=0,w=0;w=p&&(g=0),y>=m&&(y=0),v>=h&&(v=0),b>=d&&(b=0);return ot(f,e.shape)},e.prototype.localResponseNormalization4D=function(e,t,n,r,a){this.assertNotComplex(e,"localResponseNormalization4D");var i=e.shape[3],o=i-1,s=e.dataSync(),u=b(e.shape),l=new Float32Array(u);function c(e){for(var n=e%i,r=e-n+Math.max(0,n-t),a=e-n+Math.min(n+t,o),u=0;r<=a;r++){var l=s[r];u+=l*l}return u}for(var f=0;f=0&&e.get(i)1,"blockSize should be > 1 for depthToSpace, but was: "+t);for(var r=e.shape[0],a=e.shape[1],i=e.shape[2],o=e.shape[3],s=a*t,u=i*t,l=o/(t*t),c=e.dataSync(),f=new Float32Array(r*s*u*l),p=0,h=0;h=s))for(var k=p>1?(E-_)*(u-1)/(p-1):0,I=h>1?(N-S)*(l-1)/(h-1):0,C=0;C1?_*(u-1)+C*k:.5*(_+E)*(u-1);if(A<0||A>u-1)for(var P=0;P1?S*(l-1)+P*I:.5*(S+N)*(l-1))<0||$>l-1)for(T=0;T1?S*(l-1)+P*I:.5*(S+N)*(l-1))<0||$>l-1)for(T=0;T0,"scheme must not be an empty string.");var r=e.getInstance();d(null==r.managers[t],"A model store manager is already registered for scheme '"+t+"'."),r.managers[t]=n},e.getManager=function(e){var t=this.getInstance().managers[e];if(null==t)throw new Error("Cannot find model manager for scheme '"+e+"'");return t},e.getSchemes=function(){return Object.keys(this.getInstance().managers)},e}();function hs(e){if(-1===e.indexOf(fs))throw new Error("The url string provided does not contain a scheme. Supported schemes are: "+ps.getSchemes().join(","));return{scheme:e.split(fs)[0],path:e.split(fs)[1]}}function ds(e,t,n){return void 0===n&&(n=!1),l(this,void 0,void 0,function(){var r,a,i,o,s,u,l,f,p;return c(this,function(c){switch(c.label){case 0:return d(e!==t,"Old path and new path are the same: '"+e+"'"),d((r=cs.getLoadHandlers(e)).length>0,"Copying failed because no load handler is found for source URL "+e+"."),d(r.length<2,"Copying failed because more than one ("+r.length+") load handlers for source URL "+e+"."),a=r[0],d((i=cs.getSaveHandlers(t)).length>0,"Copying failed because no save handler is found for destination URL "+t+"."),d(i.length<2,"Copying failed because more than one ("+r.length+") save handlers for destination URL "+t+"."),o=i[0],s=hs(e).scheme,u=hs(e).path,l=s===hs(e).scheme,[4,a.load()];case 1:return f=c.sent(),n&&l?[4,ps.getManager(s).removeModel(u)]:[3,3];case 2:c.sent(),c.label=3;case 3:return[4,o.save(f)];case 4:return p=c.sent(),!n||l?[3,6]:[4,ps.getManager(s).removeModel(u)];case 5:c.sent(),c.label=6;case 6:return[2,p.modelArtifactsInfo]}})})}var ms="models_store",gs="model_info_store";function ys(){if(!me.get("IS_BROWSER"))throw new Error("Failed to obtain IndexedDB factory because the current environmentis not a web browser.");var e=window,t=e.indexedDB||e.mozIndexedDB||e.webkitIndexedDB||e.msIndexedDB||e.shimIndexedDB;if(null==t)throw new Error("The current browser does not appear to support IndexedDB.");return t}function vs(e){var t=e.result;t.createObjectStore(ms,{keyPath:"modelPath"}),t.createObjectStore(gs,{keyPath:"modelPath"})}var bs=function(){function e(e){if(this.indexedDB=ys(),null==e||!e)throw new Error("For IndexedDB, modelPath must not be null, undefined or empty.");this.modelPath=e}return e.prototype.save=function(e){return l(this,void 0,void 0,function(){return c(this,function(t){if(e.modelTopology instanceof ArrayBuffer)throw new Error("BrowserLocalStorage.save() does not support saving model topology in binary formats yet.");return[2,this.databaseAction(this.modelPath,e)]})})},e.prototype.load=function(){return l(this,void 0,void 0,function(){return c(this,function(e){return[2,this.databaseAction(this.modelPath)]})})},e.prototype.databaseAction=function(e,t){var n=this;return new Promise(function(e,r){var a=n.indexedDB.open("tensorflowjs",1);a.onupgradeneeded=function(){return vs(a)},a.onsuccess=function(){var i=a.result;if(null==t){var o=i.transaction(ms,"readonly"),s=o.objectStore(ms).get(n.modelPath);s.onsuccess=function(){if(null==s.result)return i.close(),r(new Error("Cannot find model with path '"+n.modelPath+"' in IndexedDB."));e(s.result.modelArtifacts)},s.onerror=function(e){return i.close(),r(s.error)},o.oncomplete=function(){return i.close()}}else{var u,l=ls(t),c=i.transaction(gs,"readwrite"),f=c.objectStore(gs),p=f.put({modelPath:n.modelPath,modelArtifactsInfo:l});p.onsuccess=function(){var a=(u=i.transaction(ms,"readwrite")).objectStore(ms).put({modelPath:n.modelPath,modelArtifacts:t,modelArtifactsInfo:l});a.onsuccess=function(){return e({modelArtifactsInfo:l})},a.onerror=function(e){var t=(f=c.objectStore(gs)).delete(n.modelPath);t.onsuccess=function(){return i.close(),r(a.error)},t.onerror=function(e){return i.close(),r(a.error)}}},p.onerror=function(e){return i.close(),r(p.error)},c.oncomplete=function(){null==u?i.close():u.oncomplete=function(){return i.close()}}}},a.onerror=function(e){return r(a.error)}})},e.URL_SCHEME="indexeddb://",e}(),ws=function(e){return me.get("IS_BROWSER")&&!Array.isArray(e)&&e.startsWith(bs.URL_SCHEME)?function(e){return new bs(e)}(e.slice(bs.URL_SCHEME.length)):null};cs.registerSaveRouter(ws),cs.registerLoadRouter(ws);var xs=function(){function e(){this.indexedDB=ys()}return e.prototype.listModels=function(){return l(this,void 0,void 0,function(){var e=this;return c(this,function(t){return[2,new Promise(function(t,n){var r=e.indexedDB.open("tensorflowjs",1);r.onupgradeneeded=function(){return vs(r)},r.onsuccess=function(){var e=r.result,a=e.transaction(gs,"readonly"),i=a.objectStore(gs).getAll();i.onsuccess=function(){for(var e={},n=0,r=i.result;n0,"URL path for browserHTTPRequest must not be null, undefined or empty."),Array.isArray(e)&&d(2===e.length,"URL paths for browserHTTPRequest must have a length of 2, (actual length is "+e.length+")."),this.path=e,null!=t&&null!=t.body)throw new Error("requestInit is expected to have no pre-existing body, but has one.");this.requestInit=t||{}}return e.prototype.save=function(e){return l(this,void 0,void 0,function(){var t,n,r,a;return c(this,function(i){switch(i.label){case 0:if(e.modelTopology instanceof ArrayBuffer)throw new Error("BrowserHTTPRequest.save() does not support saving model topology in binary formats yet.");return(t=Object.assign({method:this.DEFAULT_METHOD},this.requestInit)).body=new FormData,n=[{paths:["./model.weights.bin"],weights:e.weightSpecs}],r={modelTopology:e.modelTopology,weightsManifest:n},t.body.append("model.json",new Blob([JSON.stringify(r)],{type:"application/json"}),"model.json"),null!=e.weightData&&t.body.append("model.weights.bin",new Blob([e.weightData],{type:"application/octet-stream"}),"model.weights.bin"),[4,fetch(this.path,t)];case 1:if(200===(a=i.sent()).status)return[2,{modelArtifactsInfo:ls(e),responses:[a]}];throw new Error("BrowserHTTPRequest.save() failed due to HTTP response status "+a.status+".")}})})},e.prototype.load=function(){return l(this,void 0,void 0,function(){return c(this,function(e){return[2,Array.isArray(this.path)?this.loadBinaryModel():this.loadJSONModel()]})})},e.prototype.loadBinaryTopology=function(){return l(this,void 0,void 0,function(){var e;return c(this,function(t){switch(t.label){case 0:return t.trys.push([0,3,,4]),[4,fetch(this.path[0],this.requestInit)];case 1:return[4,t.sent().arrayBuffer()];case 2:return[2,t.sent()];case 3:throw e=t.sent(),new Error(this.path[0]+" not found. "+e);case 4:return[2]}})})},e.prototype.loadBinaryModel=function(){return l(this,void 0,void 0,function(){var e,t,n,r,a,i,o,s;return c(this,function(u){switch(u.label){case 0:return e=this.loadBinaryTopology(),[4,fetch(this.path[1],this.requestInit)];case 1:return t=u.sent(),[4,Promise.all([e,t])];case 2:return n=u.sent(),r=n[0],[4,n[1].json()];case 3:return null==(a=u.sent())?[3,5]:[4,this.loadWeights(a)];case 4:s=u.sent(),i=s[0],o=s[1],u.label=5;case 5:return[2,{modelTopology:r,weightSpecs:i,weightData:o}]}})})},e.prototype.loadJSONModel=function(){return l(this,void 0,void 0,function(){var e,t,n,r,a,i,o;return c(this,function(s){switch(s.label){case 0:return[4,fetch(this.path,this.requestInit)];case 1:return[4,s.sent().json()];case 2:if(e=s.sent(),t=e.modelTopology,n=e.weightsManifest,null==t&&null==n)throw new Error("The JSON from HTTP path "+this.path+" contains neither model topology or manifest for weights.");return null==n?[3,4]:(i=e.weightsManifest,[4,this.loadWeights(i)]);case 3:o=s.sent(),r=o[0],a=o[1],s.label=4;case 4:return[2,{modelTopology:t,weightSpecs:r,weightData:a}]}})})},e.prototype.loadWeights=function(e){return l(this,void 0,void 0,function(){var t,n,r,a,i,o,s,u,l;return c(this,function(c){switch(c.label){case 0:for(t=Array.isArray(this.path)?this.path[1]:this.path,n=[],r=0,a=e;r0&&(e=u({weightSpecs:this.weightSpecs},e)),null!=this.weightData&&this.weightData.byteLength>0&&(e=u({weightData:this.weightData},e)),[2,e]})})},e}(),qs=function(){function e(e){this.saveHandler=e}return e.prototype.save=function(e){return l(this,void 0,void 0,function(){return c(this,function(t){return[2,this.saveHandler(e)]})})},e}(),$s=cs.registerSaveRouter,Gs=cs.registerLoadRouter,Hs=cs.getSaveHandlers,Ks=cs.getLoadHandlers,Xs=Object.freeze({browserFiles:function(e){return new Ls(e)},browserHTTPRequest:Us,concatenateArrayBuffers:ss,decodeWeights:as,encodeWeights:function(e){return l(this,void 0,void 0,function(){var t,n,r,a;return c(this,function(i){switch(i.label){case 0:for(r in t=[],n=[],e){if("float32"!==(a=e[r]).dtype&&"int32"!==a.dtype&&"bool"!==a.dtype)throw new Error("Unsupported dtype in weight '"+r+"': "+a.dtype);t.push({name:r,shape:a.shape,dtype:a.dtype}),n.push(a.data())}return[4,Promise.all(n)];case 1:return[2,{data:function(e){if(null===e)throw new Error("Invalid input value: "+JSON.stringify(e));var t=0,n=[];e.forEach(function(e){if(t+=e.byteLength,n.push(e.byteLength===e.buffer.byteLength?e:new e.constructor(e)),!(e instanceof Float32Array||e instanceof Int32Array||e instanceof Uint8Array))throw new Error("Unsupported TypedArray subtype: "+e.constructor.name)});var r=new Uint8Array(t),a=0;return n.forEach(function(e){r.set(new Uint8Array(e.buffer),a),a+=e.byteLength}),r.buffer}(i.sent()),specs:t}]}})})},fromMemory:function(e,t,n){return new Ws(e,t,n)},getLoadHandlers:Ks,getModelArtifactsInfoForJSON:ls,getSaveHandlers:Hs,loadWeights:function(e,t,n,r){return void 0===t&&(t=""),l(this,void 0,void 0,function(){var a,i,o,s,u,l,f,p,h,d;return c(this,function(c){switch(c.label){case 0:if(a=e.map(function(){return!1}),i={},o=null!=n?n.map(function(){return!1}):[],s=[],e.forEach(function(e,t){var r=0;e.weights.forEach(function(e){var u="quantization"in e?e.quantization.dtype:e.dtype,l=rs[u]*b(e.shape),c=function(){a[t]=!0,null==i[t]&&(i[t]=[]),i[t].push({manifestEntry:e,groupOffset:r,sizeBytes:l})};null!=n?n.forEach(function(t,n){t===e.name&&(c(),o[n]=!0)}):c(),s.push(e.name),r+=l})}),!o.every(function(e){return e}))throw u=n.filter(function(e,t){return!o[t]}),new Error("Could not find weights in manifest with names: "+u.join(", ")+". \nManifest JSON has weights with names: "+s.join(", ")+".");return l=a.reduce(function(e,t,n){return t&&e.push(n),e},[]),f=[],l.forEach(function(n){e[n].paths.forEach(function(e){var n=t+(t.endsWith("/")?"":"/")+e;f.push(n)})}),[4,zs(f,r)];case 1:return p=c.sent(),h={},d=0,l.forEach(function(t){for(var n=e[t].paths.length,r=0,a=0;a0,"Class being registered has an empty-string as its className, which is disallowed."),Js.register(e)}var Qs=Object.freeze({Serializable:Ys,SerializationMap:Js,registerClass:Zs});function eu(e,t,n){if(null==n&&(n=me.get("TEST_EPSILON")),e instanceof Q||t instanceof Q){if(e instanceof Q&&t instanceof Q){if(e.dtype!==t.dtype)throw new Error("Arrays are of different type actual: "+e.dtype+" vs expected: "+t.dtype+".");if(!w(e.shape,t.shape))throw new Error("Arrays are of different shape actual: "+e.shape+" vs expected: "+t.shape+".")}}else{var r=e.constructor.name,a=t.constructor.name;if(r!==a)throw new Error("Arrays are of different type actual: "+r+" vs expected: "+a)}var i,o;if(i=e instanceof Q?e.dataSync():e,o=t instanceof Q?t.dataSync():t,i.length!==o.length)throw new Error("Arrays have different lengths actual: "+i.length+" vs expected: "+o.length+".\nActual: "+i+".\nExpected: "+o+".");for(var s=0;sn)}var nu=Object.freeze({WEBGL_ENVS:{HAS_WEBGL:!0},NODE_ENVS:{IS_NODE:!0},CHROME_ENVS:{IS_CHROME:!0},BROWSER_ENVS:{IS_BROWSER:!0},CPU_ENVS:{HAS_WEBGL:!1},ALL_ENVS:{},expectArraysClose:eu,expectPromiseToFail:function(e,t){e().then(function(){return t.fail()},function(){return t()})},expectArraysEqual:function(e,t){return eu(e,t,0)},expectNumbersClose:function(e,t,n){if(null==n&&(n=me.get("TEST_EPSILON")),!tu(e,t,n))throw new Error("Numbers differ: actual === "+e+", expected === "+t)},expectValuesInRange:function(e,t,n){var r;r=e instanceof Q?e.dataSync():e;for(var a=0;an)throw new Error("Value out of range:"+r[a]+" low: "+t+", high: "+n)},expectArrayBuffersEqual:function(e,t){expect(new Float32Array(e)).toEqual(new Float32Array(t))}}),ru="0.12.17",au=Object.freeze({gpgpu_util:rr,webgl_util:jn,MathBackendWebGL:ua,GPGPUContext:ar}),iu=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return s(t,e),t.prototype.minimize=function(e,t,n){void 0===t&&(t=!1);var r=this.computeGradients(e,n),a=r.value,i=r.grads;return this.applyGradients(i),Object.keys(i).forEach(function(e){return i[e].dispose()}),t?a:(a.dispose(),null)},t.prototype.computeGradients=function(e,t){return xe(e,t)},t}(Ys),ou=function(e){function t(t,n,r){void 0===r&&(r=null);var a=e.call(this)||this;return a.learningRate=t,a.rho=n,a.epsilon=r,a.accumulatedGrads={},a.accumulatedUpdates={},a.c=Ne(nt(-t)),a.rhoScalar=Ne(nt(n)),a.oneMinusRho=Ne(nt(1-n)),null===r&&(r=me.get("EPSILON")),a.epsilonScalar=Ne(nt(r)),a}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=me.engine.registeredVariables[n];null==r.accumulatedGrads[n]&&Ee(function(){t.accumulatedGrads[n]=xt(a).variable(!1)}),null==r.accumulatedUpdates[n]&&Ee(function(){t.accumulatedUpdates[n]=xt(a).variable(!1)});var i=e[n],o=r.accumulatedGrads[n],s=r.accumulatedUpdates[n];Ee(function(){var e=t.rhoScalar.mul(o).add(t.oneMinusRho.mul(i.square())),r=s.add(t.epsilonScalar).sqrt().div(o.add(t.epsilonScalar).sqrt()).mul(i),u=t.rhoScalar.mul(s).add(t.oneMinusRho.mul(r.square()));t.accumulatedGrads[n].assign(e),t.accumulatedUpdates[n].assign(u);var l=t.c.mul(r).add(a);a.assign(l)})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsilonScalar.dispose(),this.rhoScalar.dispose(),this.oneMinusRho.dispose(),null!=this.accumulatedUpdates&&(Object.keys(this.accumulatedUpdates).forEach(function(t){return e.accumulatedUpdates[t].dispose()}),Object.keys(this.accumulatedGrads).forEach(function(t){return e.accumulatedGrads[t].dispose()}))},t.prototype.getConfig=function(){return{learningRate:this.learningRate,rho:this.rho,epsilon:this.epsilon}},t.fromConfig=function(e,t){return new e(t.learningRate,t.rho,t.epsilon)},t.className="AdadeltaOptimizer",t}(iu);Zs(ou);var su=function(e){function t(t,n){void 0===n&&(n=.1);var r=e.call(this)||this;return r.learningRate=t,r.initialAccumulatorValue=n,r.accumulatedGrads={},r.c=Ne(nt(-t)),r.epsilon=Ne(nt(me.get("EPSILON"))),r}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=me.engine.registeredVariables[n];null==r.accumulatedGrads[n]&&Ee(function(){t.accumulatedGrads[n]=ft(a.shape,t.initialAccumulatorValue).variable(!1)});var i=e[n],o=r.accumulatedGrads[n];Ee(function(){var e=o.add(i.square());t.accumulatedGrads[n].assign(e);var r=t.c.mul(i.div(e.add(t.epsilon).sqrt())).add(a);a.assign(r)})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){var e=this;this.epsilon.dispose(),this.c.dispose(),null!=this.accumulatedGrads&&Object.keys(this.accumulatedGrads).forEach(function(t){return e.accumulatedGrads[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,initialAccumulatorValue:this.initialAccumulatorValue}},t.fromConfig=function(e,t){return new e(t.learningRate,t.initialAccumulatorValue)},t.className="AdagradOptimizer",t}(iu);Zs(su);var uu=function(e){function t(t,n,r,a){void 0===a&&(a=null);var i=e.call(this)||this;return i.learningRate=t,i.beta1=n,i.beta2=r,i.epsilon=a,i.accumulatedFirstMoment={},i.accumulatedSecondMoment={},i.c=Ne(nt(-t)),i.beta1Scalar=Ne(nt(n)),i.beta2Scalar=Ne(nt(r)),Ee(function(){i.accBeta1=nt(n).variable(),i.accBeta2=nt(r).variable()}),i.oneMinusBeta1=Ne(nt(1-n)),i.oneMinusBeta2=Ne(nt(1-r)),i.one=Ne(nt(1)),null===a&&(a=me.get("EPSILON")),i.epsScalar=Ne(nt(a)),i}return s(t,e),t.prototype.applyGradients=function(e){var t=this;Ee(function(){var n=t.one.sub(t.accBeta1),r=t.one.sub(t.accBeta2);for(var a in e){var i=me.engine.registeredVariables[a];if(null==t.accumulatedFirstMoment[a]){var o=!1;t.accumulatedFirstMoment[a]=xt(i).variable(o)}null==t.accumulatedSecondMoment[a]&&(o=!1,t.accumulatedSecondMoment[a]=xt(i).variable(o));var s=e[a],u=t.accumulatedFirstMoment[a],l=t.accumulatedSecondMoment[a],c=t.beta1Scalar.mul(u).add(t.oneMinusBeta1.mul(s)),f=t.beta2Scalar.mul(l).add(t.oneMinusBeta2.mul(s.square())),p=c.div(n),h=f.div(r);t.accumulatedFirstMoment[a].assign(c),t.accumulatedSecondMoment[a].assign(f);var d=t.c.mul(p.div(t.epsScalar.add(h.sqrt()))).add(i);i.assign(d)}t.accBeta1.assign(t.accBeta1.mul(t.beta1Scalar)),t.accBeta2.assign(t.accBeta2.mul(t.beta2Scalar))})},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsScalar.dispose(),this.beta1Scalar.dispose(),this.beta2Scalar.dispose(),this.accBeta1.dispose(),this.accBeta2.dispose(),this.oneMinusBeta1.dispose(),this.oneMinusBeta2.dispose(),this.one.dispose(),null!=this.accumulatedFirstMoment&&Object.keys(this.accumulatedFirstMoment).forEach(function(t){return e.accumulatedFirstMoment[t].dispose()}),null!=this.accumulatedSecondMoment&&Object.keys(this.accumulatedSecondMoment).forEach(function(t){return e.accumulatedSecondMoment[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon}},t.fromConfig=function(e,t){return new e(t.learningRate,t.beta1,t.beta2,t.epsilon)},t.className="AdamOptimizer",t}(iu);Zs(uu);var lu=function(e){function t(t,n,r,a,i){void 0===a&&(a=null),void 0===i&&(i=0);var o=e.call(this)||this;return o.learningRate=t,o.beta1=n,o.beta2=r,o.epsilon=a,o.decay=i,o.accumulatedFirstMoment={},o.accumulatedWeightedInfNorm={},o.c=Ne(nt(-t)),o.beta1Scalar=Ne(nt(n)),o.beta2Scalar=Ne(nt(r)),o.decayScalar=Ne(nt(i)),Ee(function(){o.iteration=nt(0).variable(),o.accBeta1=nt(n).variable()}),o.oneMinusBeta1=Ne(nt(1-n)),o.one=Ne(nt(1)),null===a&&(a=me.get("EPSILON")),o.epsScalar=Ne(nt(a)),o}return s(t,e),t.prototype.applyGradients=function(e){var t=this;Ee(function(){var n=t.one.sub(t.accBeta1),r=t.c.div(t.one.add(t.decayScalar.mul(t.iteration)));for(var a in e){var i=me.engine.registeredVariables[a];if(null==t.accumulatedFirstMoment[a]){var o=!1;t.accumulatedFirstMoment[a]=xt(i).variable(o)}null==t.accumulatedWeightedInfNorm[a]&&(o=!1,t.accumulatedWeightedInfNorm[a]=xt(i).variable(o));var s=e[a],u=t.accumulatedFirstMoment[a],l=t.accumulatedWeightedInfNorm[a],c=t.beta1Scalar.mul(u).add(t.oneMinusBeta1.mul(s)),f=t.beta2Scalar.mul(l),p=s.abs(),h=f.maximum(p);t.accumulatedFirstMoment[a].assign(c),t.accumulatedWeightedInfNorm[a].assign(h);var d=r.div(n).mul(c.div(t.epsScalar.add(h))).add(i);i.assign(d)}t.iteration.assign(t.iteration.add(t.one)),t.accBeta1.assign(t.accBeta1.mul(t.beta1Scalar))})},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsScalar.dispose(),this.accBeta1.dispose(),this.beta1Scalar.dispose(),this.beta2Scalar.dispose(),this.oneMinusBeta1.dispose(),this.decayScalar.dispose(),this.iteration.dispose(),this.one.dispose(),null!=this.accumulatedFirstMoment&&Object.keys(this.accumulatedFirstMoment).forEach(function(t){return e.accumulatedFirstMoment[t].dispose()}),null!=this.accumulatedWeightedInfNorm&&Object.keys(this.accumulatedWeightedInfNorm).forEach(function(t){return e.accumulatedWeightedInfNorm[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon,decay:this.decay}},t.fromConfig=function(e,t){return new e(t.learningRate,t.beta1,t.beta2,t.epsilon,t.decay)},t.className="AdamaxOptimizer",t}(iu);Zs(lu);var cu=function(e){function t(t){var n=e.call(this)||this;return n.learningRate=t,n.setLearningRate(t),n}return s(t,e),t.prototype.applyGradients=function(e){var t=this;Object.keys(e).forEach(function(n){var r=e[n],a=me.engine.registeredVariables[n];Ee(function(){var e=t.c.mul(r).add(a);a.assign(e)})})},t.prototype.setLearningRate=function(e){this.learningRate=e,null!=this.c&&this.c.dispose(),this.c=Ne(nt(-e))},t.prototype.dispose=function(){this.c.dispose()},t.prototype.getConfig=function(){return{learningRate:this.learningRate}},t.fromConfig=function(e,t){return new e(t.learningRate)},t.className="SGDOptimizer",t}(iu);Zs(cu);var fu=function(e){function t(t,n,r){void 0===r&&(r=!1);var a=e.call(this,t)||this;return a.learningRate=t,a.momentum=n,a.useNesterov=r,a.m=nt(a.momentum),a.accumulations={},a}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=me.engine.registeredVariables[n];null==r.accumulations[n]&&Ee(function(){t.accumulations[n]=xt(a).variable(!1)});var i=r.accumulations[n],o=e[n];Ee(function(){var e,r=t.m.mul(i).add(o);e=t.useNesterov?t.c.mul(o.add(r.mul(t.m))).add(a):t.c.mul(r).add(a),t.accumulations[n].assign(r),a.assign(e)})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){if(e.prototype.dispose.call(this),this.m.dispose(),null!=this.accumulations)for(var t in this.accumulations)this.accumulations[t].dispose()},t.prototype.setMomentum=function(e){this.momentum=e},t.prototype.getConfig=function(){return{learningRate:this.learningRate,momentum:this.momentum,useNesterov:this.useNesterov}},t.fromConfig=function(e,t){return new e(t.learningRate,t.momentum,t.useNesterov)},t.className="MomentumOptimizer",t}(cu);Zs(fu);var pu=function(e){function t(t,n,r,a,i){void 0===n&&(n=.9),void 0===r&&(r=0),void 0===a&&(a=null),void 0===i&&(i=!1);var o=e.call(this)||this;return o.learningRate=t,o.decay=n,o.momentum=r,o.epsilon=a,o.accumulatedMeanSquares={},o.accumulatedMeanGrads={},o.accumulatedMoments={},o.c=Ne(nt(t)),o.decayScalar=Ne(nt(n)),o.momentumScalar=Ne(nt(r)),o.oneMinusDecay=Ne(nt(1-n)),o.centered=i,null===a&&(a=me.get("EPSILON")),o.epsilonScalar=Ne(nt(a)),o}return s(t,e),t.prototype.applyGradients=function(e){var t=this,n=function(n){var a=me.engine.registeredVariables[n];null==r.accumulatedMeanSquares[n]&&Ee(function(){t.accumulatedMeanSquares[n]=xt(a).variable(!1)}),null==r.accumulatedMeanGrads[n]&&r.centered&&Ee(function(){t.accumulatedMeanGrads[n]=xt(a).variable(!1)}),null==r.accumulatedMoments[n]&&Ee(function(){t.accumulatedMoments[n]=xt(a).variable(!1)});var i=r.accumulatedMeanSquares[n],o=r.accumulatedMeanGrads[n],s=r.accumulatedMoments[n],u=e[n];Ee(function(){var e=t.decayScalar.mul(i).add(t.oneMinusDecay.mul(u.square()));if(t.centered){var r=t.decayScalar.mul(o).add(t.oneMinusDecay.mul(u)),l=t.momentumScalar.mul(s).add(t.c.mul(u).div(e.sub(r.square().add(t.epsilonScalar)).sqrt()));t.accumulatedMeanSquares[n].assign(e),t.accumulatedMeanGrads[n].assign(r),t.accumulatedMoments[n].assign(l);var c=a.sub(l);a.assign(c)}else{var f=t.decayScalar.mul(i).add(t.oneMinusDecay.mul(u.square()));l=t.momentumScalar.mul(s).add(t.c.mul(u).div(f.add(t.epsilonScalar).sqrt())),t.accumulatedMeanSquares[n].assign(f),t.accumulatedMoments[n].assign(l),c=a.sub(l),a.assign(c)}})},r=this;for(var a in e)n(a)},t.prototype.dispose=function(){var e=this;this.c.dispose(),this.epsilonScalar.dispose(),this.decayScalar.dispose(),this.momentumScalar.dispose(),this.oneMinusDecay.dispose(),null!=this.accumulatedMeanSquares&&Object.keys(this.accumulatedMeanSquares).forEach(function(t){return e.accumulatedMeanSquares[t].dispose()}),null!=this.accumulatedMeanGrads&&this.centered&&Object.keys(this.accumulatedMeanGrads).forEach(function(t){return e.accumulatedMeanGrads[t].dispose()}),null!=this.accumulatedMoments&&Object.keys(this.accumulatedMoments).forEach(function(t){return e.accumulatedMoments[t].dispose()})},t.prototype.getConfig=function(){return{learningRate:this.learningRate,decay:this.decay,momentum:this.momentum,epsilon:this.epsilon,centered:this.centered}},t.fromConfig=function(e,t){return new e(t.learningRate,t.decay,t.momentum,t.epsilon,t.centered)},t.className="RMSPropOptimizer",t}(iu);Zs(pu);var hu=function(){function e(){}return e.sgd=function(e){return new cu(e)},e.momentum=function(e,t,n){return void 0===n&&(n=!1),new fu(e,t,n)},e.rmsprop=function(e,t,n,r,a){return void 0===t&&(t=.9),void 0===n&&(n=0),void 0===r&&(r=null),void 0===a&&(a=!1),new pu(e,t,n,r,a)},e.adam=function(e,t,n,r){return void 0===e&&(e=.001),void 0===t&&(t=.9),void 0===n&&(n=.999),void 0===r&&(r=null),new uu(e,t,n,r)},e.adadelta=function(e,t,n){return void 0===e&&(e=.001),void 0===t&&(t=.95),void 0===n&&(n=null),new ou(e,t,n)},e.adamax=function(e,t,n,r,a){return void 0===e&&(e=.002),void 0===t&&(t=.9),void 0===n&&(n=.999),void 0===r&&(r=null),void 0===a&&(a=0),new lu(e,t,n,r,a)},e.adagrad=function(e,t){return void 0===t&&(t=.1),new su(e,t)},e}(),du={sgd:hu.sgd,momentum:hu.momentum,adadelta:hu.adadelta,adagrad:hu.adagrad,rmsprop:hu.rmsprop,adamax:hu.adamax,adam:hu.adam},mu=de.setBackend,gu=de.getBackend,yu=de.disposeVariables,vu=de.memory;J=Qo}.call(this,n(46),n(26),n(162).setImmediate,n(16).Buffer)},function(e,t,n){var r=n(7),a=n(44),i=n(30),o=n(29),s=n(43),u=function(e,t,n){var l,c,f,p,h=e&u.F,d=e&u.G,m=e&u.S,g=e&u.P,y=e&u.B,v=d?r:m?r[t]||(r[t]={}):(r[t]||{}).prototype,b=d?a:a[t]||(a[t]={}),w=b.prototype||(b.prototype={});for(l in d&&(n=t),n)f=((c=!h&&v&&void 0!==v[l])?v:n)[l],p=y&&c?s(f,r):g&&"function"==typeof f?s(Function.call,f):f,v&&o(v,l,f,e&u.U),b[l]!=f&&i(b,l,p),g&&w[l]!=f&&(w[l]=f)};r.core=a,u.F=1,u.G=2,u.S=4,u.P=8,u.B=16,u.W=32,u.U=64,u.R=128,e.exports=u},function(e,t,n){"use strict";n.r(t);var r=n(0),a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])}; -/** - * @license - * Copyright 2018 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 i(e,t){function n(){this.constructor=e}a(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var o=Object.assign||function(e){for(var t,n=1,r=arguments.length;n0&&a[a.length-1])&&(6===i[0]||2===i[0])){o=0;continue}if(3===i[0]&&(!a||i[1]>a[0]&&i[1]t?1:0}(e,t)}function R(e){if(null==e)return e;for(var t=[],n=0,r=e;n=0),E(r>=n),Array.isArray(e)&&e.length>=n&&e.length<=r&&e.every(function(e){return typeof e===t})}function z(e,t){return Object(r.od)(function(){return Object(r.Tc)(Object(r.dd)(Object(r.Pb)(e,e),t,!0))})}var B=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t.prototype.getConfig=function(){return{}},t}(r.Ec.Serializable),F=function(e){function t(t){var n=e.call(this)||this;return n.defaultMaxValue=2,n.defaultAxis=0,n.maxValue=null!=t.maxValue?t.maxValue:n.defaultMaxValue,n.axis=null!=t.axis?t.axis:n.defaultAxis,n}return i(t,e),t.prototype.apply=function(e){var t=this;return Object(r.od)(function(){var n=z(e,t.axis),a=Object(r.O)(n,0,t.maxValue);return Object(r.Ob)(e,Object(r.Ha)(a,Object(r.s)(y(c()),n)))})},t.prototype.getConfig=function(){return{maxValue:this.maxValue,axis:this.axis}},t.className="MaxNorm",t}(B);r.Ec.registerClass(F);var V=function(e){function t(t){var n=e.call(this)||this;return n.defaultAxis=0,n.axis=null!=t.axis?t.axis:n.defaultAxis,n}return i(t,e),t.prototype.apply=function(e){var t=this;return Object(r.od)(function(){return Object(r.Ha)(e,Object(r.s)(y(c()),z(e,t.axis)))})},t.prototype.getConfig=function(){return{axis:this.axis}},t.className="UnitNorm",t}(B);r.Ec.registerClass(V);var U=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t.prototype.apply=function(e){return Object(r.sc)(e)},t.className="NonNeg",t}(B);r.Ec.registerClass(U);var W=function(e){function t(t){var n=e.call(this)||this;return n.defaultMinValue=0,n.defaultMaxValue=1,n.defaultRate=1,n.defaultAxis=0,n.minValue=null!=t.minValue?t.minValue:n.defaultMinValue,n.maxValue=null!=t.maxValue?t.maxValue:n.defaultMaxValue,n.rate=null!=t.rate?t.rate:n.defaultRate,n.axis=null!=t.axis?t.axis:n.defaultAxis,n}return i(t,e),t.prototype.apply=function(e){var t=this;return Object(r.od)(function(){var n=z(e,t.axis),a=Object(r.s)(Object(r.Ob)(y(t.rate),Object(r.O)(n,t.minValue,t.maxValue)),Object(r.Ob)(y(1-t.rate),n));return Object(r.Ob)(e,Object(r.Ha)(a,Object(r.s)(y(c()),n)))})},t.prototype.getConfig=function(){return{minValue:this.minValue,maxValue:this.maxValue,rate:this.rate,axis:this.axis}},t.className="MinMaxNorm",t}(B);r.Ec.registerClass(W);var q={maxNorm:"MaxNorm",minMaxNorm:"MinMaxNorm",nonNeg:"NonNeg",unitNorm:"UnitNorm"};function $(e){return P(e)}function G(e,t){return void 0===t&&(t={}),T(e,r.Ec.SerializationMap.getMap().classNameMap,t,"constraint")}function H(e){return null==e?null:"string"==typeof e?G({className:e in q?q[e]:e,config:{}}):e instanceof B?e:G(e)}var K=Object.freeze({maxNorm:function(e){return new F(e)},unitNorm:function(e){return new V(e)},nonNeg:function(){return new U},minMaxNorm:function(e){return new W(e)}}),X=new Map,Y=["channelsFirst","channelsLast"];function J(e){j(Y,"DataFormat",e)}var Z=["valid","same","causal"];function Q(e){j(Z,"PaddingMode",e)}var ee=["max","avg"],te=[],ne="/";function re(e,t){te.push(e);try{var n=t();return te.pop(),n}catch(e){throw te.pop(),e}}function ae(e){if(!se(e))throw new Error("Not a valid tensor name: '"+e+"'");return(0===te.length?"":te.join(ne)+ne)+e}function ie(e){if(!se(e))throw new Error("Not a valid tensor name: '"+e+"'");X.has(e)||X.set(e,0);var t=X.get(e);if(X.set(e,X.get(e)+1),t>0){var n=e+"_"+t;return X.set(n,1),n}return e}var oe=new RegExp(/^[A-Za-z][A-Za-z0-9\._\/]*$/);function se(e){return!!e.match(oe)}function ue(e){return e===parseInt(e.toString(),10)}function le(e,t,n){null==t&&(t=0),null==n&&(n=e.length);for(var r=1,a=t;a= 2 but got x shape = "+e.shape+" and y shape = "+t.shape);if(t.rank>=3&&(a=e.shape.slice(-1)[0])!==(s=t.shape.slice(-2)[0]))throw new x("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 = "+t.shape);if(2===e.rank&&2===t.rank)return Object(r.Ab)(e,t);var n=e.shape.slice(),a=n.pop();e=e.reshape([-1,a]);var i=t.shape.slice(),o=i.pop(),s=i.pop(),u=i.concat([o]),l=Array.from({length:t.rank},function(e,n){return 0===n?t.rank-2:n<=t.rank-2?n-1:n});t=t.transpose(l).reshape([s,-1]);var c=n.concat(u);return Object(r.Ab)(e,t).reshape(c)}function Ee(e,t,n){return Object(r.od)(function(){return t=Array.isArray(t)?Object(r.hd)(t,"int32"):t.toInt(),Object(r.Xa)(e,t,n)})}function Ne(e){return Object(r.Pb)(e,e)}function Oe(e,t,n){return Object(r.od)(function(){if(null==n&&(n="channelsLast"),J(n),1!==t.rank&&t.rank!==e.rank)throw new w("Unexpected bias dimensions: "+t.rank+"; expected it to be 1 or "+e.rank);var r,a=t.shape;if(5===e.rank)"channelsFirst"===n?r=1===a.length?e.add(t.reshape([1,a[0],1,1,1])):e.add(t.reshape([1,a[3],a[0],a[1],a[2]])):"channelsLast"===n&&(r=1===a.length?e.add(t.reshape([1,1,1,1,a[0]])):e.add(t.reshape([1].concat(a))));else if(4===e.rank)"channelsFirst"===n?r=1===a.length?e.add(t.reshape([1,a[0],1,1])):e.add(t.reshape([1,a[2],a[0],a[1]])):"channelsLast"===n&&(r=1===a.length?e.add(t.reshape([1,1,1,a[0]])):e.add(t.reshape([1].concat(a))));else if(3===e.rank)"channelsFirst"===n?r=1===a.length?e.add(t.reshape([1,a[0],1])):e.add(t.reshape([1,a[1],a[0]])):"channelsLast"===n&&(r=1===a.length?e.add(t.reshape([1,1,a[0]])):e.add(t.reshape([1].concat(a))));else{if(!(e.rank<3))throw new w("Unsupported input rank by biasAdd: "+e.rank);r=e.add(t)}return r})}function ke(e,t,n,a){return Object(r.od)(function(){if(null!=n&&!r.yd.arraysEqual(e.shape,n))throw new x("Non-default noise shape is not implemented yet: "+JSON.stringify(n));if(null!=a)throw new x("seed is not implemented for dropout yet.");var i=Object(r.Zc)(Object(r.s)(Object(r.Sb)(t),Object(r.oc)(e.shape,0,1,"float32")));return i=Object(r.Ob)(Object(r.Ha)(y(1),Object(r.bd)(y(1),t)),i),Object(r.Ob)(e,i)})}function Ie(e,t,n){return void 0===n&&(n=!1),n?e():t()}var Ce=["fanIn","fanOut","fanAvg"],Ae=["normal","uniform"],Pe=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t.prototype.fromConfigUsesCustomObjects=function(){return!1},t.prototype.getConfig=function(){return{}},t}(r.Ec.Serializable),Te=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t.prototype.apply=function(e,t){return Object(r.Hd)(e,t)},t.className="Zeros",t}(Pe);r.Ec.registerClass(Te);var Me=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t.prototype.apply=function(e,t){return Object(r.Yb)(e,t)},t.className="Ones",t}(Pe);r.Ec.registerClass(Me);var Re=function(e){function t(t){var n=e.call(this)||this;if("object"!=typeof t)throw new w("Expected argument of type ConstantConfig but got "+t);if(void 0===t.value)throw new w("config must have value set but got "+t);return n.value=t.value,n}return i(t,e),t.prototype.apply=function(e,t){var n=this;return Object(r.od)(function(){return Object(r.Ob)(Object(r.Bc)(n.value),Object(r.Yb)(e,t))})},t.prototype.getConfig=function(){return{value:this.value}},t.className="Constant",t}(Pe);r.Ec.registerClass(Re);var De=function(e){function t(t){var n=e.call(this)||this;return n.DEFAULT_MINVAL=-.05,n.DEFAULT_MAXVAL=.05,n.minval=t.minval||n.DEFAULT_MINVAL,n.maxval=t.maxval||n.DEFAULT_MAXVAL,n.seed=t.seed,n}return i(t,e),t.prototype.apply=function(e,t){return Object(r.oc)(e,this.minval,this.maxval,t)},t.prototype.getConfig=function(){return{minval:this.minval,maxval:this.maxval,seed:this.seed}},t.className="RandomUniform",t}(Pe);r.Ec.registerClass(De);var je=function(e){function t(t){var n=e.call(this)||this;return n.DEFAULT_MEAN=0,n.DEFAULT_STDDEV=.05,n.mean=t.mean||n.DEFAULT_MEAN,n.stddev=t.stddev||n.DEFAULT_STDDEV,n.seed=t.seed,n}return i(t,e),t.prototype.apply=function(e,t){if("float32"!==(t=t||"float32")&&"int32"!==t)throw new x("randomNormal does not support dType "+t+".");return _e(e,this.mean,this.stddev,t,this.seed)},t.prototype.getConfig=function(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}},t.className="RandomNormal",t}(Pe);r.Ec.registerClass(je);var Le=function(e){function t(t){var n=e.call(this)||this;return n.DEFAULT_MEAN=0,n.DEFAULT_STDDEV=.05,n.mean=t.mean||n.DEFAULT_MEAN,n.stddev=t.stddev||n.DEFAULT_STDDEV,n.seed=t.seed,n}return i(t,e),t.prototype.apply=function(e,t){if("float32"!==(t=t||"float32")&&"int32"!==t)throw new x("truncatedNormal does not support dType "+t+".");return Object(r.vd)(e,this.mean,this.stddev,t,this.seed)},t.prototype.getConfig=function(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}},t.className="TruncatedNormal",t}(Pe);r.Ec.registerClass(Le);var ze=function(e){function t(t){var n=e.call(this)||this;return n.gain=null!=t.gain?Object(r.Bc)(t.gain):y(1),n}return i(t,e),t.prototype.apply=function(e,t){var n=this;return Object(r.od)(function(){if(2!==e.length||e[0]!==e[1])throw new w("Identity matrix initializer can only be used for 2D square matrices.");return Object(r.Ob)(n.gain,Object(r.Sa)(e[0]))})},t.prototype.getConfig=function(){return{gain:this.gain.get()}},t.className="Identity",t}(Pe);r.Ec.registerClass(ze);var Be=function(e){function t(t){var n=e.call(this)||this;if(t.scale<0)throw new w("scale must be a positive float. Got: "+t.scale);return n.scale=null==t.scale?1:t.scale,n.mode=t.mode,function(e){j(Ce,"FanMode",e)}(n.mode),n.distribution=t.distribution,function(e){j(Ae,"Distribution",e)}(n.distribution),n.seed=t.seed,n}return i(t,e),t.prototype.apply=function(e,t){var n=function(e,t){var n,r;if(void 0===t&&(t="channelsLast"),J(t),2===e.length)n=e[0],r=e[1];else if(-1!==[3,4,5].indexOf(e.length))if("channelsFirst"===t){var a=le(e,2);n=e[1]*a,r=e[0]*a}else"channelsLast"===t&&(a=le(e,0,e.length-2),n=e[e.length-2]*a,r=e[e.length-1]*a);else{var i=le(e);n=Math.sqrt(i),r=Math.sqrt(i)}return[n,r]}(e),a=n[0],i=n[1],o=this.scale;if("fanIn"===this.mode?o/=Math.max(1,a):"fanOut"===this.mode?o/=Math.max(1,i):o/=Math.max(1,(a+i)/2),"normal"===this.distribution){var s=Math.sqrt(o);if("float32"!==(t=t||"float32")&&"int32"!==t)throw new x(this.getClassName()+" does not support dType "+t+".");return Object(r.vd)(e,0,s,t,this.seed)}var u=Math.sqrt(3*o);return Object(r.oc)(e,-u,u,t)},t.prototype.getConfig=function(){return{scale:this.scale,mode:this.mode,distribution:this.distribution,seed:this.seed}},t.className="VarianceScaling",t}(Pe);r.Ec.registerClass(Be);var Fe=function(e){function t(t){return e.call(this,{scale:1,mode:"fanAvg",distribution:"uniform",seed:null==t?null:t.seed})||this}return i(t,e),t.prototype.getClassName=function(){return Be.className},t}(Be),Ve=function(e){function t(t){return e.call(this,{scale:1,mode:"fanAvg",distribution:"normal",seed:null==t?null:t.seed})||this}return i(t,e),t.prototype.getClassName=function(){return Be.className},t}(Be),Ue=function(e){function t(t){return e.call(this,{scale:2,mode:"fanIn",distribution:"normal",seed:null==t?null:t.seed})||this}return i(t,e),t.prototype.getClassName=function(){return Be.className},t}(Be),We=function(e){function t(t){return e.call(this,{scale:1,mode:"fanIn",distribution:"normal",seed:null==t?null:t.seed})||this}return i(t,e),t.prototype.getClassName=function(){return Be.className},t}(Be),qe=function(e){function t(t){var n=e.call(this)||this;if(n.DEFAULT_GAIN=1,n.gain=null==t.gain?n.DEFAULT_GAIN:t.gain,n.seed=t.seed,null!=n.seed)throw new x("Random seed is not implemented for Orthogonal Initializer yet.");return n}return i(t,e),t.prototype.apply=function(e,t){var n=this;return Object(r.od)(function(){if(2!==e.length)throw new x("The Orthogonal Initializer does not support non-2D shapes yet.");e[0]*e[1]>2e3&&console.warn("Orthogonal initializer is being called on a matrix with more than 2000 ("+e[0]*e[1]+") elements: Slowness may result.");var t=_e(e[0]>e[1]?[e[1],e[0]]:e,0,1,"float32"),a=r.ob.gramSchmidt(t);return e[0]>e[1]&&(a=a.transpose()),Object(r.Ob)(y(n.gain),a)})},t.prototype.getConfig=function(){return{gain:this.gain,seed:this.seed}},t.className="Orthogonal",t}(Pe);r.Ec.registerClass(qe);var $e={constant:"Constant",glorotNormal:"GlorotNormal",glorotUniform:"GlorotUniform",heNormal:"HeNormal",identity:"Identity",leCunNormal:"LeCunNormal",ones:"Ones",orthogonal:"Orthogonal",randomNormal:"RandomNormal",randomUniform:"RandomUniform",truncatedNormal:"TruncatedNormal",varianceScaling:"VarianceScaling",zeros:"Zeros"};function Ge(e,t){return void 0===t&&(t={}),T(e,r.Ec.SerializationMap.getMap().classNameMap,t,"initializer")}function He(e){return P(e)}function Ke(e){if("string"==typeof e){var t=e in $e?$e[e]:e;return"GlorotUniform"===t?new Fe:"GlorotNormal"===t?new Ve:"HeNormal"===t?new Ue:"LeCunNormal"===t?new We:Ge({className:t,config:{}})}return e instanceof Pe?e:Ge(e)}var Xe=Object.freeze({zeros:function(){return new Te},ones:function(){return new Me},constant:function(e){return new Re(e)},randomUniform:function(e){return new De(e)},randomNormal:function(e){return new je(e)},truncatedNormal:function(e){return new Le(e)},identity:function(e){return new ze(e)},varianceScaling:function(e){return new Be(e)},glorotUniform:function(e){return new Fe(e)},glorotNormal:function(e){return new Ve(e)},heNormal:function(e){return new Ue(e)},leCunNormal:function(e){return new We(e)},orthogonal:function(e){return new qe(e)}});function Ye(e){return Array.isArray(e)&&Array.isArray(e[0])}function Je(e){return 0===e.length?[]:Array.isArray(e[0])?e:[e]}function Ze(e){var t;if(Array.isArray(e)){if(1!==e.length)throw new w("Expected Tensor length to be 1; got "+e.length);t=e[0]}else t=e;return t}function Qe(e){if(Array.isArray(e)&&Array.isArray(e[0])){if(1===e.length)return(e=e)[0];throw new w("Expected exactly 1 Shape; got "+e.length)}return e}function et(e){for(var t=0,n=0,r=e;n1)throw new v("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 v("Layer "+this.name+" is not connected, no input to return.");return O(this.getNodeAtIndex(0,"input").inputTensors)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"output",{get:function(){if(0===this.inboundNodes.length)throw new v("Layer "+this.name+" has no inbound nodes.");if(this.inboundNodes.length>1)throw new v("Layer "+this.name+' has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use `getOutputAt(nodeIndex)` instead.');return O(this.getNodeAtIndex(0,"output").outputTensors)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"losses",{get:function(){return this._losses},enumerable:!0,configurable:!0}),t.prototype.calculateLosses=function(){return this.losses.map(function(e){return e()})},Object.defineProperty(t.prototype,"updates",{get:function(){return this._updates},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"built",{get:function(){return this._built},set:function(e){this._built=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"trainableWeights",{get:function(){return this.trainable?this._trainableWeights:[]},set:function(e){this._trainableWeights=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"nonTrainableWeights",{get:function(){return this.trainable?this._nonTrainableWeights:this._trainableWeights.concat(this._nonTrainableWeights)},set:function(e){this._nonTrainableWeights=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"weights",{get:function(){return this.trainableWeights.concat(this.nonTrainableWeights)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"stateful",{get:function(){return this._stateful},enumerable:!0,configurable:!0}),t.prototype.resetStates=function(){if(!this.stateful)throw new Error("Cannot call the resetStates() method of a non-stateful Layer object.")},t.prototype.assertInputCompatibility=function(e){if(e=k(e),null!=this.inputSpec&&0!==this.inputSpec.length){var t=k(this.inputSpec);if(e.length!==t.length)throw new w("Layer "+this.name+" expects "+t.length+" inputs, but it received "+e.length+" input tensors. Input received: "+e);for(var n=0;n=0?o[u]:o[o.length+u];if(null!=l&&-1===[l,null].indexOf(c))throw new w("Input "+n+" is incompatible with layer "+this.name+": expected axis "+u+" of input shape to have value "+l+" but got shape "+o+".")}}if(null!=a.shape)for(var f=0;f0&&Array.isArray(d[0])?d.map(function(r,a){return new ot(g,r,n,k(e),t,n.name,a)}):new ot(g,d,n,k(e),t,n.name),n.addInboundNode(e,m,null,null,h,d,t),n._refCount++,null!=n.activityRegularizer)throw new x("Layer invocation in the presence of activity regularizer(s) is not supported yet.");return m})},t.prototype.warnOnIncompatibleInputShape=function(e){if(null!=this.batchInputShape)if(e.length!==this.batchInputShape.length)console.warn("The rank of the input tensor provided (shape: "+JSON.stringify(e)+") does not match that of the batchInputShape ("+JSON.stringify(this.batchInputShape)+") of the layer "+this.name);else{var t=!1;this.batchInputShape.forEach(function(n,r){null!=n&&null!=e[r]&&e[r]!==n&&(t=!0)}),t&&console.warn("The shape of the input tensor ("+JSON.stringify(e)+") does not match the expectation of layer "+this.name+": "+JSON.stringify(this.batchInputShape))}},Object.defineProperty(t.prototype,"outputShape",{get:function(){if(null==this.inboundNodes||0===this.inboundNodes.length)throw new v("The layer "+this.name+" has never been called and thus has no defined output shape.");for(var e=[],t=0,n=this.inboundNodes;te.SKIP_FIRST_BATCHES&&(this.batchDurationsMillis.push(n-this.batchStartMillis),this.batchDurationsMillis.length>=e.DECISION_BATCH_COUNT&&(a=this.batchDurationsMillis.reduce(function(e,t){return e+t})/this.batchDurationsMillis.length,this.autoYieldEveryBatches=Math.round(e.THRESHOLD_MILLIS/a),this.autoYieldEveryBatches<1&&(this.autoYieldEveryBatches=1))),this.batchStartMillis=r.yd.now(),this.lastYieldBatchCount=this.batchCount,[3,6];case 3:return this.batchCount-this.lastYieldBatchCount>=this.autoYieldEveryBatches?[4,Object(r.Tb)()]:[3,6];case 4:return i.sent(),[4,this.resolveOneTensorInLogs(t)];case 5:i.sent(),this.lastYieldBatchCount=this.batchCount,i.label=6;case 6:return[3,9];case 7:return"batch"!==this.yieldEvery?[3,9]:[4,Object(r.Tb)()];case 8:i.sent(),i.label=9;case 9:return[2]}})})},e.prototype.maybeYieldOnEpoch=function(){return s(this,void 0,void 0,function(){return u(this,function(e){switch(e.label){case 0:return"epoch"!==this.yieldEvery?[3,2]:[4,Object(r.Tb)()];case 1:e.sent(),e.label=2;case 2:return[2]}})})},e.SKIP_FIRST_BATCHES=1,e.DECISION_BATCH_COUNT=2,e.THRESHOLD_MILLIS=16,e}(),yt=function(e){function t(t){var n=e.call(this)||this;return n.yieldEvery=t||"auto",n}return i(t,e),t.prototype.onTrainBegin=function(e){return s(this,void 0,void 0,function(){return u(this,function(e){return this.autoYielder=new gt(this.yieldEvery),[2]})})},t.prototype.onEpochBegin=function(e){return s(this,void 0,void 0,function(){return u(this,function(e){return this.seen=0,this.totals={},[2]})})},t.prototype.onBatchEnd=function(e,t){return s(this,void 0,void 0,function(){var e,n,a,i,o=this;return u(this,function(s){switch(s.label){case 0:return[4,this.autoYielder.maybeYieldOnBatch(t)];case 1:for(i in s.sent(),null==t&&(t={}),e=null==t.size?0:t.size,this.seen+=e,n=function(n){var i=t[n];if("number"==typeof i)a.totals.hasOwnProperty(n)||(a.totals[n]=0),a.totals[n]=a.totals[n]+i*e;else{var s=void 0;n in a.totals?s=a.totals[n]:a.totals[n]=y(0),a.totals[n]=Object(r.od)(function(){return Object(r.s)(o.totals[n],Object(r.Ob)(i,y(e)))}),null!=s&&s.dispose()}},a=this,t)n(i);return[2]}})})},t.prototype.onEpochEnd=function(e,t){return s(this,void 0,void 0,function(){var e,n,a,i,o,s=this;return u(this,function(u){switch(u.label){case 0:return[4,this.autoYielder.maybeYieldOnEpoch()];case 1:if(u.sent(),null!=t)for(e=function(e){if(null==n.totals[e])return"continue";"number"==typeof n.totals[e]?t[e]=n.totals[e]/n.seen:Object(r.od)(function(){t[e]=Object(r.Ob)(Object(r.Ha)(y(1),y(s.seen)),s.totals[e]),s.totals[e].dispose(),Object(r.ib)(t[e])})},n=this,a=0,i=this.params.metrics;a=0&&Number.isInteger(t),"Verbosity level is expected to be an integer >= 0, but got "+t),e.checkForDuplicate(n),null==e.constructors[t]&&(e.constructors[t]=[]),e.constructors[t].push(n)},e.checkForDuplicate=function(t){for(var n in e.constructors)e.constructors[+n].forEach(function(e){if(e===t)throw new w("Duplicate callback constructor.")})},e.clear=function(){e.constructors={}},e.createCallbacks=function(t){var n=[];for(var r in e.constructors){var a=+r;t>=a&&n.push.apply(n,e.constructors[a])}return n.map(function(e){return new e})},e}();function xt(e,t){return Object(r.od)(function(){var n=Object(r.dd)(Ne(e),t,!0),a=Object(r.Ob)(Object(r.Bc)(c()),Object(r.Zb)(e)),i=Object(r.Tc)(Object(r.Db)(n,a));return Object(r.Ha)(e,i)})}function _t(e,t){return Object(r.od)(function(){return Object(r.Fb)(Ne(Object(r.bd)(t,e)),-1)})}function St(e,t){return Object(r.od)(function(){return Object(r.Fb)(Object(r.p)(Object(r.bd)(t,e)),-1)})}function Et(e,t){return Object(r.od)(function(){var n=Object(r.bd)(e,t),a=Object(r.O)(Object(r.p)(e),c(),Number.MAX_VALUE),i=Object(r.p)(Object(r.Ha)(n,a));return Object(r.Ob)(y(100),Object(r.Fb)(i,-1))})}function Nt(e,t){return Object(r.od)(function(){var n=y(1),a=Object(r.O)(t,c(),Number.MAX_VALUE),i=Object(r.rb)(Object(r.s)(n,a)),o=Object(r.O)(e,c(),Number.MAX_VALUE),s=Object(r.rb)(Object(r.s)(n,o));return Object(r.Fb)(Ne(Object(r.bd)(i,s)),-1)})}function Ot(e,t){return Object(r.od)(function(){var n=y(0),a=y(1),i=Object(r.Db)(n,Object(r.bd)(a,Object(r.Ob)(e,t)));return Object(r.Fb)(Ne(i),-1)})}function kt(e,t){return Object(r.od)(function(){var n=y(0),a=y(1),i=Object(r.Db)(n,Object(r.bd)(a,Object(r.Ob)(e,t)));return Object(r.Fb)(i,-1)})}function It(e,t){return Object(r.od)(function(){var n=y(0),a=y(1),i=Object(r.dd)(Object(r.Ob)(e,t),-1),o=Object(r.Bb)(Object(r.Ob)(Object(r.bd)(a,e),t),-1);return Object(r.Db)(n,Object(r.s)(a,Object(r.bd)(o,i)))})}function Ct(e,t){return Object(r.od)(function(){var n=y(Math.log(2)),a=Object(r.bd)(t,e),i=Object(r.bd)(Object(r.s)(a,Object(r.Qc)(Object(r.Ob)(y(-2),a))),n);return Object(r.Fb)(i,-1)})}function At(e,t,n){return void 0===n&&(n=!1),Object(r.od)(function(){if(n)t=Object(r.Pc)(t);else{var a=Object(r.dd)(t,t.shape.length-1,!0);t=Object(r.Ha)(t,a)}return t=Object(r.O)(t,c(),1-c()),Object(r.Sb)(Object(r.dd)(Object(r.Ob)(e.toFloat(),Object(r.rb)(t)),t.shape.length-1))})}function Pt(e,t,n){return void 0===n&&(n=!1),Object(r.od)(function(){var a=Object(r.Ua)(function(e){var t=[le(e.shape)];return e.reshape(t)}(e)).toInt(),i=t.shape;return At(Object(r.Xb)(a,i[i.length-1]).reshape(i),t,n)})}function Tt(e,t){return Object(r.od)(function(){var n;return n=Object(r.O)(t,c(),1-c()),n=Object(r.rb)(Object(r.Ha)(n,Object(r.bd)(Object(r.Zb)(n),n))),Object(r.Fb)(function(e,t){return Object(r.od)(function(){var n=Object(r.Db)(t,Object(r.Id)(t)),a=Object(r.Ob)(t,e),i=Object(r.rb)(Object(r.s)(y(1),Object(r.Pa)(Object(r.Sb)(Object(r.p)(t)))));return Object(r.s)(Object(r.bd)(n,a),i)})}(e,n),-1)})}function Mt(e,t){return Object(r.od)(function(){var n=Object(r.O)(e,c(),1),a=Object(r.O)(t,c(),1);return Object(r.dd)(Object(r.Ob)(e,Object(r.rb)(Object(r.Ha)(n,a))),-1)})}function Rt(e,t){return Object(r.od)(function(){var n=Object(r.rb)(Object(r.s)(y(c()),t));return Object(r.Fb)(Object(r.bd)(t,Object(r.Ob)(e,n)),-1)})}function Dt(e,t){return Object(r.od)(function(){var n=xt(e,-1),a=xt(t,-1),i=Object(r.Ob)(n,a);return Object(r.Sb)(Object(r.dd)(i,-1))})}function jt(e){var t={meanSquaredError:_t,meanAbsoluteError:St,meanAbsolutePercentageError:Et,meanSquaredLogarithmicError:Nt,squaredHinge:Ot,hinge:kt,categoricalHinge:It,logcosh:Ct,categoricalCrossentropy:At,sparseCategoricalCrossentropy:Pt,binaryCrossentropy:Tt,kullbackLeiblerDivergence:Mt,poisson:Rt,cosineProximity:Dt};if("string"==typeof e){if(e in t)return t[e];var n="Unknown loss "+e;throw e.toLowerCase().includes("softmaxcrossentropy")&&(n="Unknown loss "+e+'. Use "categoricalCrossentropy" as the string name for tf.losses.softmaxCrossEntropy'),new w(n)}return e}function Lt(e,t){return Object(r.od)(function(){var n=Object(r.Ob)(y(.5),Object(r.Zb)(t)),a=de(Object(r.bb)(t,n),e.dtype);return Object(r.Fb)(Object(r.Ma)(e,a),-1)})}function zt(e,t){return Object(r.od)(function(){return de(Object(r.Ma)(Object(r.x)(e,-1),Object(r.x)(t,-1)),"float32")})}function Bt(e,t){return Object(r.od)(function(){var n=y(1);return Object(r.vb)(e.equal(n),t.equal(n)).sum().cast("float32")})}function Ft(e,t){return Object(r.od)(function(){var n=y(0),a=Bt(e,t),i=function(e,t){return Object(r.od)(function(){var n=y(1),a=y(0);return Object(r.vb)(e.equal(a),t.equal(n)).sum().cast("float32")})}(e,t),o=a.add(i);return Object(r.Fd)(Object(r.bb)(o,n),a.div(o),n).cast("float32")})}function Vt(e,t){return Tt(e,t)}function Ut(e,t){throw new x}var Wt=_t,qt=_t,$t=St,Gt=St,Ht=Et,Kt=Et,Xt=At,Yt=Dt,Jt=Pt;function Zt(e,t,n){void 0===n&&(n=console.log);for(var r="",a=0;a0&&(r=r.slice(0,r.length-1)+" "),r=(r+=e[a]).slice(0,t[a]),r+=" ".repeat(t[a]-r.length);n(r)}function Qt(e,t,n){var r;try{r=JSON.stringify(e.outputShape)}catch(e){r="multiple"}Zt([e.name+" ("+e.getClassName()+")",r,e.countParams().toString()],t,n)}function en(e,t,n,r){var a;try{a=JSON.stringify(e.outputShape)}catch(e){a="multiple"}for(var i=[],o=0,s=e.inboundNodes;o0&&-1===n.indexOf(u)))for(var l=0;l1 nodes"),E(0===l,"input layer has >1 tensors"),r.inputLayers.push(s),r.inputLayersNodeIndices.push(u),r.inputLayersTensorIndices.push(l);r.inputNames=[],r.outputNames=[],r.feedInputShapes=[],r.feedInputNames=[],r.feedOutputNames=[];for(var p=0;p=0;)a.splice(a.indexOf(u),1);S.push(u)}},O=[],k=[],I=0,C=r.outputs;Ir?1:0});for(var K=0,X=H;K0)throw new w("Container instance unexpectedly contains _trainableWeights.The trainable weights of a Container are a union of the trainable weights of its consituent Layers. Its own _trainableWeights must remain an empty Array.");if(!this.trainable)return[];for(var e=[],t=0,n=this.layers;t0)throw new w(p.length+" of "+a+" weights are not set: "+p)}at(c)}(e,this.layers,a):function(e,t,n){void 0===n&&(n=!1);for(var a=e.keras_version,i=(e.backend,t.map(function(e){return e.name})),o={},s=0,u=t;s1)for(var s=0,u=o;s0){for(var h=[],d=0;d0&&e.apply(O(i),r)}function o(e){var r=e.name,i=tn(e,null!=t.customObjects?t.customObjects:{});n[r]=i;for(var o=0,s=e.inboundNodes;o0)i=!0;else if(pn(e)){for(var o in e)if(e.hasOwnProperty(o)){i=!0;break}}else i=!0;if(i)throw new w("Error when checking model "+a+" expected no data, but got "+e)}return[]}if(null==e)return t.map(function(e){return null});var s;if(pn(e)){e=e,s=[];for(var u=0,l=t;u1)throw new w("The model "+a+" expects "+t.length+" Tensor(s), but only received one Tensor. Found: Tensor with shape "+e.shape);s=[e]}for(var f=0;f=0&&d!==m)throw new w("Error when checking "+a+": expected "+t[f]+" to have shape ["+n[f]+"], but got array with shape ["+p.shape+"].")}}return s}function dn(e,t){for(var n=[],r=0,a=null;r=e&&(a=e),n.push([r,a]),r=a;return n}function mn(e,t,n){return null==e?[null]:Array.isArray(e)?e.map(function(e){return ge(e,t,n-t)}):ge(e,t,n-t)}function gn(e,t){return Object(r.od)(function(){return null==e?null:Array.isArray(e)?e.map(function(e){return gn(e,t)}):Ee(e,"int32"===t.dtype?t:t.toInt())})}function yn(e,t,n,r,a){var i;if(void 0===r&&(r=!0),void 0===a&&(a=""),Array.isArray(e)){if(e.length!==t.length)throw new w("Error when checking model "+a+": the Array of Tensors that you are passing to your model is not the size the the model expected. Expected to see "+t.length+" Tensor(s), but instead got "+e.length+" Tensors(s).");i=e}else{if(t.length>1)throw new w("The model expects "+t.length+" "+a+" Tensors, but only received one Tensor. Found: array with shape "+JSON.stringify(e.shape)+".");i=[e]}if(null!=n)for(var o=0;o0&&Number.isInteger(e),"batchSize is required to be a positive integer, but got "+e)}!function(e){e[e.SILENT=0]="SILENT",e[e.VERBOSE=1]="VERBOSE"}(on||(on={}));var bn=function(e){function t(t){var n=e.call(this,t)||this;return n.isTraining=!1,n}return i(t,e),t.prototype.summary=function(e,t,n){if(void 0===n&&(n=console.log),!this.built)throw new w("This model has never been called, thus its weights have not been created yet. So no summary can be displayed. Build the model first (e.g., by calling it on some test data).");!function(e,t,n,r){void 0===r&&(r=console.log);var a,i=function(e){var t=!0,n=[],r=[];for(var a in e.nodesByDepth)n.push(e.nodesByDepth[a]);for(var i=0,o=n;i1||1===s.length&&s[0].inboundLayers.length>1){t=!1;break}r.push.apply(r,s)}if(t)for(var u=0,l=e.layers;u1&&(t.metricsTensors.push([n,e]),t.metricsNames.push(t.outputNames[e]+"_loss"))}});var h=function(e,t){if(null==e||Array.isArray(e)&&0===e.length)return t.map(function(e){return[]});if(Array.isArray(e))return t.map(function(t){return e});if(null!=e){for(var n=[],r=0,a=t;r1&&(n=t.outputNames[e]+"_"+n),t.metricsNames.push(n),t.metricsTensors.push([r,e])}(e,r,l)},s=0,u=h[e];s0){var c=[];throw t.forEach(function(t,n){null==t&&c.push(e[n])}),new w("Cannot find SymbolicTensors for output name(s): "+JSON.stringify(c))}return t},t.prototype.predictLoop=function(e,t,n){var a=this;return void 0===t&&(t=32),void 0===n&&(n=!1),Object(r.od)(function(){var i=a.checkNumSamples(e);if(n)throw new x("Verbose predictLoop() is not implemented yet.");for(var o=dn(i,t),s=[],u=function(t){var n=Object(r.od)(function(){var n=o[t][0],r=o[t][1],i=mn(e,n,r),s=[];if(Array.isArray(i))for(var u=0;u1)throw new w("All input Tensors (x) should have the same number of samples. Got array shapes: "+JSON.stringify(e.map(function(e){return e.shape})));if(i.length>1)throw new w("All target Tensors (y) should have the same number of samples. Got array shapes: "+JSON.stringify(t.map(function(e){return e.shape})));if(a.length>0&&i.length>0&&!r.yd.arraysEqual(a,i))throw new w("Input Tensors should have the same number of samples as target Tensors. Found "+a[0]+" input sample(s) and "+i[0]+" target sample(s).")}(e=hn(e,this.feedInputNames,this.feedInputShapes,!1,"input"),t=hn(t,this.feedOutputNames,i,!1,"target")),function(e,t,n){for(var r=[_t,Tt,At],a=0;a0&&e[0].shape[0]%a!=0)throw new w("In a stateful network, you should only pass inputs with a number of samples that is divisible by the batch size "+a+". Found: "+e[0].shape[0]+" sample(s).");return[e,t,null]},t.prototype.fitLoop=function(e,t,n,a,i,o,l,c,f,p,h,d,m,g,y){return s(this,void 0,void 0,function(){var s,v,b,_,S,E,N,O,k=this;return u(this,function(I){switch(I.label){case 0:if(null==a&&(a=32),null==i&&(i=1),null==p&&(p=!0),null==d&&(d=0),s=!1,null!=c&&null!=f&&(s=!0),null!=g&&(s=!0,null==m))throw new w("Can only use `validationSteps` when doing step-wise training, i.e., `stepsPerEpoch` must be set.");return null!=(v=this.checkNumSamples(t,a,m,"steps_per_epoch"))&&(b=he(0,v)),null==o&&(o=1),this.history=new vt,_=[new yt(y)].concat(wt.createCallbacks(o)),null!=l&&_.push.apply(_,l),_.push(this.history),(S=new mt(_)).setModel(this),S.setParams({epochs:i,initialEpoch:d,samples:v,steps:m,batchSize:a,verbose:o,doValidation:s,metrics:h}),[4,S.onTrainBegin()];case 1:I.sent(),this.stopTraining_=!1,E=function(i){var o,l,h,d,g;return u(this,function(y){switch(y.label){case 0:return[4,S.onEpochBegin(i)];case 1:if(y.sent(),o={},null==m)return[3,2];throw new x("stepsPerEpoch mode is not implemented yet.");case 2:if("batch"===p)throw new x("batch shuffling is not implemneted yet");p&&r.yd.shuffle(b),l=Object(r.hd)(b),h=dn(v,a),d=function(i){var p;return u(this,function(u){switch(u.label){case 0:return p={},[4,S.onBatchBegin(i,p)];case 1:return u.sent(),Object(r.od)(function(){var u=h[i][0],d=h[i][1],m=ge(l,u,d-u);p.batch=i,p.size=d-u;for(var g=gn(t,m),y=e(g),v=0;v1&&(a+="_"+N(e.slice(0,n),r)),t.push(a)}return t},t.prototype.makeTestFunction=function(){var e=this;this.testFunction=function(t){return Object(r.od)(function(){for(var n,a=[],i=t.slice(0,e.inputs.length),o=t.slice(e.inputs.length,e.inputs.length+e.outputs.length),s=[],u=0;u0){if(l=!0,2!==n.validationData.length)throw 3===n.validationData.length?new x("validationData including sample weights is not supported yet."):new w("When passing validation data, it must contain 2 (valX, valY) or 3 (valX, valY, valSampleWeight) items; "+n.validationData+" is invalid.");c=n.validationData[0],f=n.validationData[1],d=this.standardizeUserData(c,f,!0,a),c=d[0],f=d[1],p=c.concat(f)}else null!=n.validationSplit&&n.validationSplit>0&&n.validationSplit<1?(l=!0,m=Math.floor(o[0].shape[0]*(1-n.validationSplit)),g=o[0].shape[0],c=mn(o,m,g),o=mn(o,0,m),f=mn(s,m,g),s=mn(s,0,m),h=!0,p=c.concat(f)):null!=n.validationSteps&&(l=!0);return y=o.concat(s),this.checkTrainableWeightsConsistency(),v=function(e){var t=e.slice(0,O.inputs.length),n=e.slice(O.inputs.length,O.inputs.length+O.outputs.length),a=[],i=O.collectedTrainableWeights.map(function(e){return e.read()});return[O.optimizer.minimize(function(){for(var e=[],i=0;i1)throw new w("Found more than one ("+n.length+") save handlers for URL '"+e+"'");e=n[0]}if(null==e.save)throw new w("Model.save() cannot proceed because the IOHandler provided does not have the `save` attribute defined.");return[4,r.hb.encodeWeights(this.getNamedWeights(t))];case 1:return a=u.sent(),i=!1,o=null,s=this.toJSON(o,i),[2,e.save({modelTopology:s,weightData:a.data,weightSpecs:a.specs})]}})})},t.className="Model",t}(sn);r.Ec.registerClass(bn);var wn=function(e){function t(t){var n=e.call(this,{inputs:[],outputs:[]})||this;if(t=t||{},n.trainable=!0,n._updatable=!0,n.built=!1,n.name=null!=t.name?t.name:d("sequential_"),null!=t.layers)for(var r=0,a=t.layers;r0)&&(n=t.sourceLayer,r=t.nodeIndex),0===n.inboundNodes.length)return[t];var a=n.inboundNodes[r];if(0===a.inboundLayers.length)return a.inputTensors;for(var i=[],o=0;o1)throw new w("Found more than one ("+n.length+") load handlers for URL '"+e+"'");e=n[0]}return[2,function(e,t,n){return void 0===n&&(n=!0),s(this,void 0,void 0,function(){var t,a,i,o,s;return u(this,function(u){switch(u.label){case 0:if(null==e.load)throw new w("Cannot proceed with model loading because the IOHandler provided does not have the `load` method implemented.");return[4,e.load()];case 1:if(t=u.sent(),null!=(a=t.modelTopology).model_config&&(a=a.model_config),i=tn(function e(t,n){if(null===t)return null;if("string"==typeof t)return C(t);if("number"==typeof t||"boolean"==typeof t)return t;if(t instanceof Array){for(var r=[],a=t.length,i=0;i 0 but got "+JSON.stringify(e.filters))},t}(nr),ar=function(e){function t(n){var r=e.call(this,2,n)||this;return t.verifyConfig(n),r}return i(t,e),t.prototype.getConfig=function(){var t=e.prototype.getConfig.call(this);return delete t.rank,t},t.verifyConfig=function(e){if("number"!=typeof e.kernelSize&&!L(e.kernelSize,"number",1,2))throw new w("Conv2D expects config.kernelSize to be number or number[] with length 1 or 2, but received "+JSON.stringify(e.kernelSize)+".")},t.className="Conv2D",t}(rr);r.Ec.registerClass(ar);var ir=function(e){function t(t){var n=e.call(this,t)||this;if(n.inputSpec=[new it({ndim:4})],"same"!==n.padding&&"valid"!==n.padding)throw new w("Conv2DTranspose currently supports only padding modes 'same' and 'valid', but received padding mode "+n.padding);return n}return i(t,e),t.prototype.build=function(e){if(4!==(e=Qe(e)).length)throw new w("Input should have rank 4; Received input shape: "+JSON.stringify(e));var t="channelsFirst"===this.dataFormat?1:e.length-1;if(null==e[t])throw new w("The channel dimension of the inputs should be defined. Found `None`.");var n,r=e[t],a=this.kernelSize.concat([this.filters,r]);this.kernel=this.addWeight("kernel",a,"float32",this.kernelInitializer,this.kernelRegularizer,!0,this.kernelConstraint),this.useBias&&(this.bias=this.addWeight("bias",[this.filters],"float32",this.biasInitializer,this.biasRegularizer,!0,this.biasConstraint)),this.inputSpec=[new it({ndim:4,axes:(n={},n[t]=r,n)})],this.built=!0},t.prototype.call=function(e,t){var n=this;return Object(r.od)(function(){var t=Ze(e);if(4!==t.shape.length)throw new w("Conv2DTranspose.call() expects input tensor to be rank-4, but received a tensor of rank-"+t.shape.length);var a,i,o=t.shape,s=o[0];"channelsFirst"===n.dataFormat?(a=2,i=3):(a=1,i=2);var u=o[a],l=o[i],c=n.kernelSize[0],f=n.kernelSize[1],p=n.strides[0],h=n.strides[1],d=[s,er(u,p,c,n.padding),er(l,h,f,n.padding),n.filters];"channelsLast"!==n.dataFormat&&(t=Object(r.ud)(t,[0,2,3,1]));var m=Object(r.Y)(t,n.kernel.read(),d,n.strides,n.padding);return"channelsLast"!==n.dataFormat&&(m=Object(r.ud)(m,[0,3,1,2])),null!=n.bias&&(m=Oe(m,n.bias.read(),n.dataFormat)),null!=n.activation&&(m=n.activation.apply(m)),m})},t.prototype.computeOutputShape=function(e){var t,n,r,a=(e=Qe(e)).slice();"channelsFirst"===this.dataFormat?(t=1,n=2,r=3):(t=3,n=1,r=2);var i=this.kernelSize[0],o=this.kernelSize[1],s=this.strides[0],u=this.strides[1];return a[t]=this.filters,a[n]=er(a[n],s,i,this.padding),a[r]=er(a[r],u,o,this.padding),a},t.prototype.getConfig=function(){var t=e.prototype.getConfig.call(this);return delete t.dilationRate,t},t.className="Conv2DTranspose",t}(ar);r.Ec.registerClass(ir);var or=function(e){function t(t){return e.call(this,2,t)||this}return i(t,e),t.className="SeparableConv2D",t}(function(e){function t(t,n){var r=e.call(this,t,n)||this;if(r.DEFAULT_DEPTHWISE_INITIALIZER="glorotUniform",r.DEFAULT_POINTWISE_INITIALIZER="glorotUniform",r.depthwiseKernel=null,r.pointwiseKernel=null,null==n.filters)throw new w("The `filters` configuration field is required by SeparableConv, but is unspecified.");if(null!=n.kernelInitializer||null!=n.kernelRegularizer||null!=n.kernelConstraint)throw new w("Fields kernelInitializer, kernelRegularizer and kernelConstraint are invalid for SeparableConv2D. Use depthwiseInitializer, depthwiseRegularizer, depthwiseConstraint, pointwiseInitializer, pointwiseRegularizer and pointwiseConstraint instead.");if(null!=n.padding&&"same"!==n.padding&&"valid"!==n.padding)throw new w("SeparableConv"+r.rank+"D supports only padding modes: 'same' and 'valid', but received "+JSON.stringify(n.padding));return r.depthMultiplier=null==n.depthMultiplier?1:n.depthMultiplier,r.depthwiseInitializer=Ke(n.depthwiseInitializer||r.DEFAULT_DEPTHWISE_INITIALIZER),r.depthwiseRegularizer=Jn(n.depthwiseRegularizer),r.depthwiseConstraint=H(n.depthwiseConstraint),r.pointwiseInitializer=Ke(n.depthwiseInitializer||r.DEFAULT_POINTWISE_INITIALIZER),r.pointwiseRegularizer=Jn(n.pointwiseRegularizer),r.pointwiseConstraint=H(n.pointwiseConstraint),r}return i(t,e),t.prototype.build=function(e){if((e=Qe(e)).length1)throw new w("Can not merge tensors with different batch sizes. Got tensors with shapes: "+JSON.stringify(e)+".");for(var a=null==e[0]?null:e[0].slice(1),i=1;i1){var v=he(1,u).concat([0]);t.push(Object(r.ud)(h,v)),c=!0}else t.push(h)}var b=n.mergeFunction(t),w=b.rank;if(c)if(null==w){var x=b.shape;g=[m=x[x.length-1]].concat(x.slice(0,x.length-1)),b=Object(r.ud)(b.reshape([-1,m]),[1,0]).reshape(g)}else w>1&&(v=[w-1].concat(he(0,w-1)),b=Object(r.ud)(b,v));return b}return n.mergeFunction(e)})},t.prototype.computeOutputShape=function(e){var t;t=null==(e=e)[0]?null:e[0].slice(1);for(var n=1;n1)throw new w("A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got input shapes: "+JSON.stringify(e))}},t.prototype.mergeFunction=function(e){var t=this;return Object(r.od)(function(){return be(e,t.axis)})},t.prototype.computeOutputShape=function(e){if(!Array.isArray(e)||!Array.isArray(e[0]))throw new w("A `Concatenate` layer should be called on a list of inputs.");for(var t=e,n=t[0].slice(),r=this.axis<0?n.length+this.axis:this.axis,a=0,i=t.slice(1);a=0?this.axis:this.axis+e.length,n=e[t];if(null==n)throw new w("Axis "+t+" of input tensor should have a defined dimension but the layer received an input with shape "+JSON.stringify(e)+".");this.inputSpec=[new it({ndim:e.length,axes:(r={},r[t]=n,r)})];var r,a=[n];this.scale&&(this.gamma=this.addWeight("gamma",a,null,this.gammaInitializer,this.gammaRegularizer,!0,this.gammaConstraint)),this.center&&(this.beta=this.addWeight("beta",a,null,this.betaInitializer,this.betaRegularizer,!0,this.betaConstraint)),this.movingMean=this.addWeight("moving_mean",a,null,this.movingMeanInitializer,null,!1),this.movingVariance=this.addWeight("moving_variance",a,null,this.movingVarianceInitializer,null,!1),this.built=!0},t.prototype.call=function(e,t){var n=this;return Object(r.od)(function(){var a=null!=t.training&&t.training,i=Ze(e),o=i.shape,s=o.length,u=he(0,s),l=n.axis>=0?n.axis:n.axis+s;u.splice(l,1);var c=S(1,s);c[l]=o[l];var f=u.slice();f.sort();var p=!r.yd.arraysEqual(f,he(0,s).slice(0,s-1));if(!a)return function(){if(p){var e=n.movingMean.read().reshape(c),t=n.movingVariance.read().reshape(c),r=n.center?n.beta.read().reshape(c):null,a=n.scale?n.gamma.read().reshape(c):null;return Or(i,e,t,r,a,n.epsilon)}return Or(i,n.movingMean.read(),n.movingVariance.read(),null==n.beta?null:n.beta.read(),null==n.gamma?null:n.gamma.read(),n.epsilon)}();var h=function(e,t,n,a,i){return void 0===i&&(i=.001),r.yd.arraysEqual(a.slice().sort(),he(0,e.rank-1))?function(e,t,n,a,i){return void 0===i&&(i=.001),Object(r.od)(function(){var o=Object(r.Mb)(e,a),s=o.mean,u=o.variance;return[Or(e,s,u,n,t,i),s,u]})}(e,t,n,a,i):function(e,t,n,a,i){return void 0===i&&(i=.001),Object(r.od)(function(){for(var o=Object(r.Mb)(e,a),s=o.mean,u=o.variance,l=[],c=0,f=he(0,e.rank);c=0?e[2]+this.padding[0][0]+this.padding[0][1]:null,n=null!=e[3]&&e[3]>=0?e[3]+this.padding[1][0]+this.padding[1][1]:null,[e[0],e[1],t,n]):(t=null!=e[1]&&e[1]>=0?e[1]+this.padding[0][0]+this.padding[0][1]:null,n=null!=e[2]&&e[2]>=0?e[2]+this.padding[1][0]+this.padding[1][1]:null,[e[0],t,n,e[3]])},t.prototype.call=function(e,t){var n=this;return Object(r.od)(function(){return function(e,t,n){return Object(r.od)(function(){if(4!==e.rank)throw new w("temporalPadding expects input tensor to be 4-D, but received a "+e.rank+"-D tensor.");if(null==t&&(t=[[1,1],[1,1]]),2!==t.length||2!==t[0].length||2!==t[1].length)throw new w("spatial2dPadding expects `padding` to be an Array of two Arrays, each of which is an Array of two integers.");if(null==n&&(n="channelsLast"),"channelsLast"!==n&&"channelsFirst"!==n)throw new w("Unknown data format: "+n+". Supported data formats are 'channelsLast' and 'channelsFirst.");var a;return a="channelsFirst"===n?[[0,0],[0,0],t[0],t[1]]:[[0,0],t[0],t[1],[0,0]],Object(r.cc)(e,a)})}(Ze(e),n.padding,n.dataFormat)})},t.prototype.getConfig=function(){var t={padding:this.padding,dataFormat:this.dataFormat},n=e.prototype.getConfig.call(this);return Object.assign(t,n),t},t.className="ZeroPadding2D",t}(ct);function Cr(e,t,n,a,i,o){return Object(r.od)(function(){var s;J(i),j(ee,"PoolMode",o),Q(a),null==n&&(n=[1,1]),null==a&&(a="valid"),null==i&&(i="channelsLast"),null==o&&(o="max"),e=tr(e,i);var u="same"===a?"same":"valid";return s="max"===o?Object(r.Cb)(e,t,n,u):Object(r.E)(e,t,n,u),"channelsFirst"===i&&(s=Object(r.ud)(s,[0,3,1,2])),s})}r.Ec.registerClass(Ir);var Ar=function(e){function t(t){var n=this;if(null==t.poolSize&&(t.poolSize=2),n=e.call(this,t)||this,"number"==typeof t.poolSize)n.poolSize=[t.poolSize];else{if(!Array.isArray(t.poolSize)||1!==t.poolSize.length||"number"!=typeof t.poolSize[0])throw new w("poolSize for 1D convolutional layer must be a number or an Array of a single number, but received "+JSON.stringify(t.poolSize));n.poolSize=t.poolSize}if(null==t.strides)n.strides=n.poolSize;else if("number"==typeof t.strides)n.strides=[t.strides];else{if(!Array.isArray(t.strides)||1!==t.strides.length||"number"!=typeof t.strides[0])throw new w("strides for 1D convolutional layer must be a number or an Array of a single number, but received "+JSON.stringify(t.strides));n.strides=t.strides}return n.padding=null==t.padding?"valid":t.padding,Q(n.padding),n.inputSpec=[new it({ndim:3})],n}return i(t,e),t.prototype.computeOutputShape=function(e){var t=Qn((e=Qe(e))[1],this.poolSize[0],this.padding,this.strides[0]);return[e[0],t,e[2]]},t.prototype.call=function(e,t){var n=this;return Object(r.od)(function(){n.invokeCallHook(e,t),e=me(Ze(e),2);var a=n.poolingFunction(Ze(e),[n.poolSize[0],1],[n.strides[0],1],n.padding,"channelsLast");return Object(r.Xc)(a,[2])})},t.prototype.getConfig=function(){var t={poolSize:this.poolSize,padding:this.padding,strides:this.strides},n=e.prototype.getConfig.call(this);return Object.assign(t,n),t},t}(ct),Pr=function(e){function t(t){return e.call(this,t)||this}return i(t,e),t.prototype.poolingFunction=function(e,t,n,r,a){return J(a),Q(r),Cr(e,t,n,r,a,"max")},t.className="MaxPooling1D",t}(Ar);r.Ec.registerClass(Pr);var Tr=function(e){function t(t){return e.call(this,t)||this}return i(t,e),t.prototype.poolingFunction=function(e,t,n,r,a){return J(a),Q(r),Cr(e,t,n,r,a,"avg")},t.className="AveragePooling1D",t}(Ar);r.Ec.registerClass(Tr);var Mr=function(e){function t(t){var n=this;if(null==t.poolSize&&(t.poolSize=[2,2]),(n=e.call(this,t)||this).poolSize=Array.isArray(t.poolSize)?t.poolSize:[t.poolSize,t.poolSize],null==t.strides)n.strides=n.poolSize;else if(Array.isArray(t.strides)){if(2!==t.strides.length)throw new w("If the strides property of a 2D pooling layer is an Array, it is expected to have a length of 2, but received length "+t.strides.length+".");n.strides=t.strides}else n.strides=[t.strides,t.strides];return n.padding=null==t.padding?"valid":t.padding,n.dataFormat=null==t.dataFormat?"channelsLast":t.dataFormat,J(n.dataFormat),Q(n.padding),n.inputSpec=[new it({ndim:4})],n}return i(t,e),t.prototype.computeOutputShape=function(e){e=Qe(e);var t="channelsFirst"===this.dataFormat?e[2]:e[1],n="channelsFirst"===this.dataFormat?e[3]:e[2];return t=Qn(t,this.poolSize[0],this.padding,this.strides[0]),n=Qn(n,this.poolSize[1],this.padding,this.strides[1]),"channelsFirst"===this.dataFormat?[e[0],e[1],t,n]:[e[0],t,n,e[3]]},t.prototype.call=function(e,t){var n=this;return Object(r.od)(function(){return n.invokeCallHook(e,t),n.poolingFunction(Ze(e),n.poolSize,n.strides,n.padding,n.dataFormat)})},t.prototype.getConfig=function(){var t={poolSize:this.poolSize,padding:this.padding,strides:this.strides,dataFormat:this.dataFormat},n=e.prototype.getConfig.call(this);return Object.assign(t,n),t},t}(ct),Rr=function(e){function t(t){return e.call(this,t)||this}return i(t,e),t.prototype.poolingFunction=function(e,t,n,r,a){return J(a),Q(r),Cr(e,t,n,r,a,"max")},t.className="MaxPooling2D",t}(Mr);r.Ec.registerClass(Rr);var Dr=function(e){function t(t){return e.call(this,t)||this}return i(t,e),t.prototype.poolingFunction=function(e,t,n,r,a){return J(a),Q(r),Cr(e,t,n,r,a,"avg")},t.className="AveragePooling2D",t}(Mr);r.Ec.registerClass(Dr);var jr=function(e){function t(t){var n=e.call(this,t)||this;return n.inputSpec=[new it({ndim:3})],n}return i(t,e),t.prototype.computeOutputShape=function(e){return[e[0],e[2]]},t.prototype.call=function(e,t){throw new x},t}(ct),Lr=function(e){function t(t){return e.call(this,t)||this}return i(t,e),t.prototype.call=function(e,t){return Object(r.od)(function(){var t=Ze(e);return Object(r.Fb)(t,1)})},t.className="GlobalAveragePooling1D",t}(jr);r.Ec.registerClass(Lr);var zr=function(e){function t(t){return e.call(this,t)||this}return i(t,e),t.prototype.call=function(e,t){return Object(r.od)(function(){var t=Ze(e);return Object(r.Bb)(t,1)})},t.className="GlobalMaxPooling1D",t}(jr);r.Ec.registerClass(zr);var Br=function(e){function t(t){var n=e.call(this,t)||this;return n.dataFormat=null==t.dataFormat?"channelsLast":t.dataFormat,J(n.dataFormat),n.inputSpec=[new it({ndim:4})],n}return i(t,e),t.prototype.computeOutputShape=function(e){return e=e,"channelsLast"===this.dataFormat?[e[0],e[3]]:[e[0],e[1]]},t.prototype.call=function(e,t){throw new x},t.prototype.getConfig=function(){var t={dataFormat:this.dataFormat},n=e.prototype.getConfig.call(this);return Object.assign(t,n),t},t}(ct),Fr=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t.prototype.call=function(e,t){var n=this;return Object(r.od)(function(){var t=Ze(e);return"channelsLast"===n.dataFormat?Object(r.Fb)(t,[1,2]):Object(r.Fb)(t,[2,3])})},t.className="GlobalAveragePooling2D",t}(Br);r.Ec.registerClass(Fr);var Vr=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t.prototype.call=function(e,t){var n=this;return Object(r.od)(function(){var t=Ze(e);return"channelsLast"===n.dataFormat?Object(r.Bb)(t,[1,2]):Object(r.Bb)(t,[2,3])})},t.className="GlobalMaxPooling2D",t}(Br);function Ur(e,t,n,r){if(Array.isArray(e)){if(null!=t||null!=n)throw new w("When inputs is an array, neither initialState or constants should be provided");null!=r&&(n=e.slice(e.length-r,e.length),e=e.slice(0,e.length-r)),e.length>1&&(t=e.slice(1,e.length)),e=e[0]}function a(e){return null==e||Array.isArray(e)?e:[e]}return{inputs:e,initialState:t=a(t),constants:n=a(n)}}function Wr(e,t,n,a,i,o,s,u){void 0===a&&(a=!1),void 0===s&&(s=!1),void 0===u&&(u=!1);var l=t.shape.length;if(l<3)throw new w("Input should be at least 3D, but is "+l+"D.");var c,f,p=[1,0].concat(he(2,l));if(t=Object(r.ud)(t,p),null!=i)throw new x("The rnn() function of the deeplearn.js backend does not support masking yet.");if(null!=o)throw new x("The rnn() functoin of the deeplearn.js backend does not support constants yet.");s&&console.warn("Backend rnn(): the unroll = true option is not applicable to the imperative deeplearn.js backend."),a&&(t=Object(r.uc)(t,0));for(var h=n,d=t.shape[0],m=function(n){var a=ge(t,n,1);a=a.reshape(a.shape.slice(1));var i=Object(r.od)(function(){return e(a,h)});if(f=i[0],u)if(0===n)c=f.expandDims(1);else{var o=Object(r.R)([c,f.expandDims(1)],1);c.dispose(),c=o}h=i[1]},g=0;g1?xe(n,[1,e]):n}):t.cell.stateSize>1?[xe(n,[1,t.cell.stateSize])]:[n]})},Object.defineProperty(t.prototype,"trainableWeights",{get:function(){return this.trainable?this.cell.trainableWeights:[]},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"nonTrainableWeights",{get:function(){return this.trainable?this.cell.nonTrainableWeights:this.cell.weights},enumerable:!0,configurable:!0}),t.prototype.getConfig=function(){var t={returnSequences:this.returnSequences,returnState:this.returnState,goBackwards:this.goBackwards,stateful:this.stateful,unroll:this.unroll};null!=this.numConstants&&(t.numConstants=this.numConstants);var n=this.cell.getConfig();t.cell={className:this.cell.getClassName(),config:n};var r=e.prototype.getConfig.call(this);return Object.assign(t,r),t},t.className="RNN",t}(ct);r.Ec.registerClass(qr);var $r=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return i(t,e),t}(ct),Gr=function(e){function t(t){var n=e.call(this,t)||this;return n.DEFAULT_ACTIVATION="tanh",n.DEFAULT_KERNEL_INITIALIZER="glorotNormal",n.DEFAULT_RECURRENT_INITIALIZER="orthogonal",n.DEFAULT_BIAS_INITIALIZER="zeros",n.units=t.units,n.activation=Fn(null==t.activation?n.DEFAULT_ACTIVATION:t.activation),n.useBias=null==t.useBias||t.useBias,n.kernelInitializer=Ke(t.kernelInitializer||n.DEFAULT_KERNEL_INITIALIZER),n.recurrentInitializer=Ke(t.recurrentInitializer||n.DEFAULT_RECURRENT_INITIALIZER),n.biasInitializer=Ke(t.biasInitializer||n.DEFAULT_BIAS_INITIALIZER),n.kernelRegularizer=Jn(t.kernelRegularizer),n.recurrentRegularizer=Jn(t.recurrentRegularizer),n.biasRegularizer=Jn(t.biasRegularizer),n.kernelConstraint=H(t.kernelConstraint),n.recurrentConstraint=H(t.recurrentConstraint),n.biasConstraint=H(t.biasConstraint),n.dropout=fe([1,pe([0,null==t.dropout?0:t.dropout])]),n.recurrentDropout=fe([1,pe([0,null==t.recurrentDropout?0:t.recurrentDropout])]),n.stateSize=n.units,n.dropoutMask=null,n.recurrentDropoutMask=null,n}return i(t,e),t.prototype.build=function(e){e=Qe(e),this.kernel=this.addWeight("kernel",[e[e.length-1],this.units],null,this.kernelInitializer,this.kernelRegularizer,!0,this.kernelConstraint),this.recurrentKernel=this.addWeight("recurrent_kernel",[this.units,this.units],null,this.recurrentInitializer,this.recurrentRegularizer,!0,this.recurrentConstraint),this.useBias?this.bias=this.addWeight("bias",[this.units],null,this.biasInitializer,this.biasRegularizer,!0,this.biasConstraint):this.bias=null,this.built=!0},t.prototype.call=function(e,t){var n=this;return Object(r.od)(function(){if(2!==(e=e).length)throw new w("SimpleRNNCell expects 2 input Tensors, got "+e.length+".");var a=e[1];e=e[0];var i,o=null!=t.training&&t.training;01){for(var o=[],s=0;s= 3D, but received input shape "+JSON.stringify(t));this.inputSpec=[{shape:t}];var n=[t[0]].concat(t.slice(2));this.layer.built||(this.layer.build(n),this.layer.built=!0),e.prototype.build.call(this,t)},t.prototype.computeOutputShape=function(e){var t=[(e=Qe(e))[0]].concat(e.slice(2)),n=this.layer.computeOutputShape(t),r=e[1];return[n[0],r].concat(n.slice(1))},t.prototype.call=function(e,t){var n=this;return Object(r.od)(function(){return Wr(function(e,r){return[Ze(n.layer.call(e,t)),[]]},e=Ze(e),[],!1,null,null,!1,!0)[1]})},t.className="TimeDistributed",t}(ea);r.Ec.registerClass(ta);var na=["sum","mul","concat","ave"],ra=function(e){function t(t){var n=e.call(this,t)||this,r=t.layer.getConfig();if(n.forwardLayer=tn({className:t.layer.getClassName(),config:r}),r.goBackwards=!0!==r.goBackwards,n.backwardLayer=tn({className:t.layer.getClassName(),config:r}),n.forwardLayer.name="forward_"+n.forwardLayer.name,n.backwardLayer.name="backward_"+n.backwardLayer.name,function(e){j(na,"BidirectionalMergeMode",e)}(t.mergeMode),n.mergeMode=t.mergeMode,t.weights)throw new x("weights support is not implemented for Bidirectional layer yet.");return n._stateful=t.layer.stateful,n.returnSequences=t.layer.returnSequences,n.returnState=t.layer.returnState,n.supportsMasking=!0,n._trainable=!0,n.inputSpec=t.layer.inputSpec,n.numConstants=null,n}return i(t,e),Object.defineProperty(t.prototype,"trainable",{get:function(){return this._trainable},set:function(e){this._trainable=e,null!=this.forwardLayer&&(this.forwardLayer.trainable=e),null!=this.backwardLayer&&(this.backwardLayer.trainable=e)},enumerable:!0,configurable:!0}),t.prototype.getWeights=function(){return this.forwardLayer.getWeights().concat(this.backwardLayer.getWeights())},t.prototype.setWeights=function(e){var t=e.length,n=Math.floor(t/2);this.forwardLayer.setWeights(e.slice(0,n)),this.backwardLayer.setWeights(e.slice(n))},t.prototype.computeOutputShape=function(e){var t,n,r,a=this.forwardLayer.computeOutputShape(e);return Array.isArray(a)&&Array.isArray(a[0])||(a=[a]),a=a,this.returnState?(r=a.slice(1),t=a[0]):t=a[0],t=t,"concat"===this.mergeMode?(t[t.length-1]*=2,n=[t]):n=null==this.mergeMode?[t,t.slice()]:[t],this.returnState?null==this.mergeMode?n.concat(r).concat(r.slice()):[t].concat(r).concat(r.slice()):O(n)},t.prototype.apply=function(t,n){var r=null==n?null:n.initialState,a=null==n?null:n.constants;null==n&&(n={});var i=Ur(t,r,a,this.numConstants);if(t=i.inputs,r=i.initialState,a=i.constants,Array.isArray(t)&&(r=t.slice(1),t=t[0]),(null==r||0===r.length)&&null==a)return e.prototype.apply.call(this,t,n);var o=[],s=[];if(null!=r){var u=r.length;if(u%2>0)throw new w("When passing `initialState` to a Bidrectional RNN, the state should be an Array containing the states of the underlying RNNs.");n.initialState=r,o.push.apply(o,r);var l=r.map(function(e){return new it({shape:e.shape})});this.forwardLayer.stateSpec=l.slice(0,u/2),this.backwardLayer.stateSpec=l.slice(u/2),s.push.apply(s,l)}if(null!=a)throw new x("Support for constants in Bidirectional layers is not implemented yet.");for(var c=o[0]instanceof ot,f=0,p=o;f0?a(r(e),9007199254740991):0}},function(e,t){var n=e.exports={version:"2.6.1"};"number"==typeof __e&&(__e=n)},function(e,t,n){var r=n(4),a=n(293),i=n(52),o=Object.defineProperty;t.f=n(14)?Object.defineProperty:function(e,t,n){if(r(e),t=i(t,!0),r(n),a)try{return o(e,t,n)}catch(e){}if("get"in n||"set"in n)throw TypeError("Accessors not supported!");return"value"in n&&(e[t]=n.value),e}},function(e,t,n){e.exports=!n(8)(function(){return 7!=Object.defineProperty({},"a",{get:function(){return 7}}).a})},function(e,t,n){(function(e){!function(e,t){"use strict";function r(e,t){if(!e)throw new Error(t||"Assertion failed")}function a(e,t){e.super_=t;var n=function(){};n.prototype=t.prototype,e.prototype=new n,e.prototype.constructor=e}function i(e,t,n){if(i.isBN(e))return e;this.negative=0,this.words=null,this.length=0,this.red=null,null!==e&&("le"!==t&&"be"!==t||(n=t,t=10),this._init(e||0,t||10,n||"be"))}var o;"object"==typeof e?e.exports=i:t.BN=i,i.BN=i,i.wordSize=26;try{o=n(435).Buffer}catch(e){}function s(e,t,n){for(var r=0,a=Math.min(e.length,n),i=t;i=49&&o<=54?o-49+10:o>=17&&o<=22?o-17+10:15&o}return r}function u(e,t,n,r){for(var a=0,i=Math.min(e.length,n),o=t;o=49?s-49+10:s>=17?s-17+10:s}return a}i.isBN=function(e){return e instanceof i||null!==e&&"object"==typeof e&&e.constructor.wordSize===i.wordSize&&Array.isArray(e.words)},i.max=function(e,t){return e.cmp(t)>0?e:t},i.min=function(e,t){return e.cmp(t)<0?e:t},i.prototype._init=function(e,t,n){if("number"==typeof e)return this._initNumber(e,t,n);if("object"==typeof e)return this._initArray(e,t,n);"hex"===t&&(t=16),r(t===(0|t)&&t>=2&&t<=36);var a=0;"-"===(e=e.toString().replace(/\s+/g,""))[0]&&a++,16===t?this._parseHex(e,a):this._parseBase(e,t,a),"-"===e[0]&&(this.negative=1),this.strip(),"le"===n&&this._initArray(this.toArray(),t,n)},i.prototype._initNumber=function(e,t,n){e<0&&(this.negative=1,e=-e),e<67108864?(this.words=[67108863&e],this.length=1):e<4503599627370496?(this.words=[67108863&e,e/67108864&67108863],this.length=2):(r(e<9007199254740992),this.words=[67108863&e,e/67108864&67108863,1],this.length=3),"le"===n&&this._initArray(this.toArray(),t,n)},i.prototype._initArray=function(e,t,n){if(r("number"==typeof e.length),e.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(e.length/3),this.words=new Array(this.length);for(var a=0;a=0;a-=3)o=e[a]|e[a-1]<<8|e[a-2]<<16,this.words[i]|=o<>>26-s&67108863,(s+=24)>=26&&(s-=26,i++);else if("le"===n)for(a=0,i=0;a>>26-s&67108863,(s+=24)>=26&&(s-=26,i++);return this.strip()},i.prototype._parseHex=function(e,t){this.length=Math.ceil((e.length-t)/6),this.words=new Array(this.length);for(var n=0;n=t;n-=6)a=s(e,n,n+6),this.words[r]|=a<>>26-i&4194303,(i+=24)>=26&&(i-=26,r++);n+6!==t&&(a=s(e,t,n+6),this.words[r]|=a<>>26-i&4194303),this.strip()},i.prototype._parseBase=function(e,t,n){this.words=[0],this.length=1;for(var r=0,a=1;a<=67108863;a*=t)r++;r--,a=a/t|0;for(var i=e.length-n,o=i%r,s=Math.min(i,i-o)+n,l=0,c=n;c1&&0===this.words[this.length-1];)this.length--;return this._normSign()},i.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},i.prototype.inspect=function(){return(this.red?""};var l=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],c=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],f=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function p(e,t,n){n.negative=t.negative^e.negative;var r=e.length+t.length|0;n.length=r,r=r-1|0;var a=0|e.words[0],i=0|t.words[0],o=a*i,s=67108863&o,u=o/67108864|0;n.words[0]=s;for(var l=1;l>>26,f=67108863&u,p=Math.min(l,t.length-1),h=Math.max(0,l-e.length+1);h<=p;h++){var d=l-h|0;c+=(o=(a=0|e.words[d])*(i=0|t.words[h])+f)/67108864|0,f=67108863&o}n.words[l]=0|f,u=0|c}return 0!==u?n.words[l]=0|u:n.length--,n.strip()}i.prototype.toString=function(e,t){var n;if(e=e||10,t=0|t||1,16===e||"hex"===e){n="";for(var a=0,i=0,o=0;o>>24-a&16777215)||o!==this.length-1?l[6-u.length]+u+n:u+n,(a+=2)>=26&&(a-=26,o--)}for(0!==i&&(n=i.toString(16)+n);n.length%t!=0;)n="0"+n;return 0!==this.negative&&(n="-"+n),n}if(e===(0|e)&&e>=2&&e<=36){var p=c[e],h=f[e];n="";var d=this.clone();for(d.negative=0;!d.isZero();){var m=d.modn(h).toString(e);n=(d=d.idivn(h)).isZero()?m+n:l[p-m.length]+m+n}for(this.isZero()&&(n="0"+n);n.length%t!=0;)n="0"+n;return 0!==this.negative&&(n="-"+n),n}r(!1,"Base should be between 2 and 36")},i.prototype.toNumber=function(){var e=this.words[0];return 2===this.length?e+=67108864*this.words[1]:3===this.length&&1===this.words[2]?e+=4503599627370496+67108864*this.words[1]:this.length>2&&r(!1,"Number can only safely store up to 53 bits"),0!==this.negative?-e:e},i.prototype.toJSON=function(){return this.toString(16)},i.prototype.toBuffer=function(e,t){return r(void 0!==o),this.toArrayLike(o,e,t)},i.prototype.toArray=function(e,t){return this.toArrayLike(Array,e,t)},i.prototype.toArrayLike=function(e,t,n){var a=this.byteLength(),i=n||Math.max(1,a);r(a<=i,"byte array longer than desired length"),r(i>0,"Requested array length <= 0"),this.strip();var o,s,u="le"===t,l=new e(i),c=this.clone();if(u){for(s=0;!c.isZero();s++)o=c.andln(255),c.iushrn(8),l[s]=o;for(;s=4096&&(n+=13,t>>>=13),t>=64&&(n+=7,t>>>=7),t>=8&&(n+=4,t>>>=4),t>=2&&(n+=2,t>>>=2),n+t},i.prototype._zeroBits=function(e){if(0===e)return 26;var t=e,n=0;return 0==(8191&t)&&(n+=13,t>>>=13),0==(127&t)&&(n+=7,t>>>=7),0==(15&t)&&(n+=4,t>>>=4),0==(3&t)&&(n+=2,t>>>=2),0==(1&t)&&n++,n},i.prototype.bitLength=function(){var e=this.words[this.length-1],t=this._countBits(e);return 26*(this.length-1)+t},i.prototype.zeroBits=function(){if(this.isZero())return 0;for(var e=0,t=0;te.length?this.clone().ior(e):e.clone().ior(this)},i.prototype.uor=function(e){return this.length>e.length?this.clone().iuor(e):e.clone().iuor(this)},i.prototype.iuand=function(e){var t;t=this.length>e.length?e:this;for(var n=0;ne.length?this.clone().iand(e):e.clone().iand(this)},i.prototype.uand=function(e){return this.length>e.length?this.clone().iuand(e):e.clone().iuand(this)},i.prototype.iuxor=function(e){var t,n;this.length>e.length?(t=this,n=e):(t=e,n=this);for(var r=0;re.length?this.clone().ixor(e):e.clone().ixor(this)},i.prototype.uxor=function(e){return this.length>e.length?this.clone().iuxor(e):e.clone().iuxor(this)},i.prototype.inotn=function(e){r("number"==typeof e&&e>=0);var t=0|Math.ceil(e/26),n=e%26;this._expand(t),n>0&&t--;for(var a=0;a0&&(this.words[a]=~this.words[a]&67108863>>26-n),this.strip()},i.prototype.notn=function(e){return this.clone().inotn(e)},i.prototype.setn=function(e,t){r("number"==typeof e&&e>=0);var n=e/26|0,a=e%26;return this._expand(n+1),this.words[n]=t?this.words[n]|1<e.length?(n=this,r=e):(n=e,r=this);for(var a=0,i=0;i>>26;for(;0!==a&&i>>26;if(this.length=n.length,0!==a)this.words[this.length]=a,this.length++;else if(n!==this)for(;ie.length?this.clone().iadd(e):e.clone().iadd(this)},i.prototype.isub=function(e){if(0!==e.negative){e.negative=0;var t=this.iadd(e);return e.negative=1,t._normSign()}if(0!==this.negative)return this.negative=0,this.iadd(e),this.negative=1,this._normSign();var n,r,a=this.cmp(e);if(0===a)return this.negative=0,this.length=1,this.words[0]=0,this;a>0?(n=this,r=e):(n=e,r=this);for(var i=0,o=0;o>26,this.words[o]=67108863&t;for(;0!==i&&o>26,this.words[o]=67108863&t;if(0===i&&o>>13,h=0|o[1],d=8191&h,m=h>>>13,g=0|o[2],y=8191&g,v=g>>>13,b=0|o[3],w=8191&b,x=b>>>13,_=0|o[4],S=8191&_,E=_>>>13,N=0|o[5],O=8191&N,k=N>>>13,I=0|o[6],C=8191&I,A=I>>>13,P=0|o[7],T=8191&P,M=P>>>13,R=0|o[8],D=8191&R,j=R>>>13,L=0|o[9],z=8191&L,B=L>>>13,F=0|s[0],V=8191&F,U=F>>>13,W=0|s[1],q=8191&W,$=W>>>13,G=0|s[2],H=8191&G,K=G>>>13,X=0|s[3],Y=8191&X,J=X>>>13,Z=0|s[4],Q=8191&Z,ee=Z>>>13,te=0|s[5],ne=8191&te,re=te>>>13,ae=0|s[6],ie=8191&ae,oe=ae>>>13,se=0|s[7],ue=8191&se,le=se>>>13,ce=0|s[8],fe=8191&ce,pe=ce>>>13,he=0|s[9],de=8191&he,me=he>>>13;n.negative=e.negative^t.negative,n.length=19;var ge=(l+(r=Math.imul(f,V))|0)+((8191&(a=(a=Math.imul(f,U))+Math.imul(p,V)|0))<<13)|0;l=((i=Math.imul(p,U))+(a>>>13)|0)+(ge>>>26)|0,ge&=67108863,r=Math.imul(d,V),a=(a=Math.imul(d,U))+Math.imul(m,V)|0,i=Math.imul(m,U);var ye=(l+(r=r+Math.imul(f,q)|0)|0)+((8191&(a=(a=a+Math.imul(f,$)|0)+Math.imul(p,q)|0))<<13)|0;l=((i=i+Math.imul(p,$)|0)+(a>>>13)|0)+(ye>>>26)|0,ye&=67108863,r=Math.imul(y,V),a=(a=Math.imul(y,U))+Math.imul(v,V)|0,i=Math.imul(v,U),r=r+Math.imul(d,q)|0,a=(a=a+Math.imul(d,$)|0)+Math.imul(m,q)|0,i=i+Math.imul(m,$)|0;var ve=(l+(r=r+Math.imul(f,H)|0)|0)+((8191&(a=(a=a+Math.imul(f,K)|0)+Math.imul(p,H)|0))<<13)|0;l=((i=i+Math.imul(p,K)|0)+(a>>>13)|0)+(ve>>>26)|0,ve&=67108863,r=Math.imul(w,V),a=(a=Math.imul(w,U))+Math.imul(x,V)|0,i=Math.imul(x,U),r=r+Math.imul(y,q)|0,a=(a=a+Math.imul(y,$)|0)+Math.imul(v,q)|0,i=i+Math.imul(v,$)|0,r=r+Math.imul(d,H)|0,a=(a=a+Math.imul(d,K)|0)+Math.imul(m,H)|0,i=i+Math.imul(m,K)|0;var be=(l+(r=r+Math.imul(f,Y)|0)|0)+((8191&(a=(a=a+Math.imul(f,J)|0)+Math.imul(p,Y)|0))<<13)|0;l=((i=i+Math.imul(p,J)|0)+(a>>>13)|0)+(be>>>26)|0,be&=67108863,r=Math.imul(S,V),a=(a=Math.imul(S,U))+Math.imul(E,V)|0,i=Math.imul(E,U),r=r+Math.imul(w,q)|0,a=(a=a+Math.imul(w,$)|0)+Math.imul(x,q)|0,i=i+Math.imul(x,$)|0,r=r+Math.imul(y,H)|0,a=(a=a+Math.imul(y,K)|0)+Math.imul(v,H)|0,i=i+Math.imul(v,K)|0,r=r+Math.imul(d,Y)|0,a=(a=a+Math.imul(d,J)|0)+Math.imul(m,Y)|0,i=i+Math.imul(m,J)|0;var we=(l+(r=r+Math.imul(f,Q)|0)|0)+((8191&(a=(a=a+Math.imul(f,ee)|0)+Math.imul(p,Q)|0))<<13)|0;l=((i=i+Math.imul(p,ee)|0)+(a>>>13)|0)+(we>>>26)|0,we&=67108863,r=Math.imul(O,V),a=(a=Math.imul(O,U))+Math.imul(k,V)|0,i=Math.imul(k,U),r=r+Math.imul(S,q)|0,a=(a=a+Math.imul(S,$)|0)+Math.imul(E,q)|0,i=i+Math.imul(E,$)|0,r=r+Math.imul(w,H)|0,a=(a=a+Math.imul(w,K)|0)+Math.imul(x,H)|0,i=i+Math.imul(x,K)|0,r=r+Math.imul(y,Y)|0,a=(a=a+Math.imul(y,J)|0)+Math.imul(v,Y)|0,i=i+Math.imul(v,J)|0,r=r+Math.imul(d,Q)|0,a=(a=a+Math.imul(d,ee)|0)+Math.imul(m,Q)|0,i=i+Math.imul(m,ee)|0;var xe=(l+(r=r+Math.imul(f,ne)|0)|0)+((8191&(a=(a=a+Math.imul(f,re)|0)+Math.imul(p,ne)|0))<<13)|0;l=((i=i+Math.imul(p,re)|0)+(a>>>13)|0)+(xe>>>26)|0,xe&=67108863,r=Math.imul(C,V),a=(a=Math.imul(C,U))+Math.imul(A,V)|0,i=Math.imul(A,U),r=r+Math.imul(O,q)|0,a=(a=a+Math.imul(O,$)|0)+Math.imul(k,q)|0,i=i+Math.imul(k,$)|0,r=r+Math.imul(S,H)|0,a=(a=a+Math.imul(S,K)|0)+Math.imul(E,H)|0,i=i+Math.imul(E,K)|0,r=r+Math.imul(w,Y)|0,a=(a=a+Math.imul(w,J)|0)+Math.imul(x,Y)|0,i=i+Math.imul(x,J)|0,r=r+Math.imul(y,Q)|0,a=(a=a+Math.imul(y,ee)|0)+Math.imul(v,Q)|0,i=i+Math.imul(v,ee)|0,r=r+Math.imul(d,ne)|0,a=(a=a+Math.imul(d,re)|0)+Math.imul(m,ne)|0,i=i+Math.imul(m,re)|0;var _e=(l+(r=r+Math.imul(f,ie)|0)|0)+((8191&(a=(a=a+Math.imul(f,oe)|0)+Math.imul(p,ie)|0))<<13)|0;l=((i=i+Math.imul(p,oe)|0)+(a>>>13)|0)+(_e>>>26)|0,_e&=67108863,r=Math.imul(T,V),a=(a=Math.imul(T,U))+Math.imul(M,V)|0,i=Math.imul(M,U),r=r+Math.imul(C,q)|0,a=(a=a+Math.imul(C,$)|0)+Math.imul(A,q)|0,i=i+Math.imul(A,$)|0,r=r+Math.imul(O,H)|0,a=(a=a+Math.imul(O,K)|0)+Math.imul(k,H)|0,i=i+Math.imul(k,K)|0,r=r+Math.imul(S,Y)|0,a=(a=a+Math.imul(S,J)|0)+Math.imul(E,Y)|0,i=i+Math.imul(E,J)|0,r=r+Math.imul(w,Q)|0,a=(a=a+Math.imul(w,ee)|0)+Math.imul(x,Q)|0,i=i+Math.imul(x,ee)|0,r=r+Math.imul(y,ne)|0,a=(a=a+Math.imul(y,re)|0)+Math.imul(v,ne)|0,i=i+Math.imul(v,re)|0,r=r+Math.imul(d,ie)|0,a=(a=a+Math.imul(d,oe)|0)+Math.imul(m,ie)|0,i=i+Math.imul(m,oe)|0;var Se=(l+(r=r+Math.imul(f,ue)|0)|0)+((8191&(a=(a=a+Math.imul(f,le)|0)+Math.imul(p,ue)|0))<<13)|0;l=((i=i+Math.imul(p,le)|0)+(a>>>13)|0)+(Se>>>26)|0,Se&=67108863,r=Math.imul(D,V),a=(a=Math.imul(D,U))+Math.imul(j,V)|0,i=Math.imul(j,U),r=r+Math.imul(T,q)|0,a=(a=a+Math.imul(T,$)|0)+Math.imul(M,q)|0,i=i+Math.imul(M,$)|0,r=r+Math.imul(C,H)|0,a=(a=a+Math.imul(C,K)|0)+Math.imul(A,H)|0,i=i+Math.imul(A,K)|0,r=r+Math.imul(O,Y)|0,a=(a=a+Math.imul(O,J)|0)+Math.imul(k,Y)|0,i=i+Math.imul(k,J)|0,r=r+Math.imul(S,Q)|0,a=(a=a+Math.imul(S,ee)|0)+Math.imul(E,Q)|0,i=i+Math.imul(E,ee)|0,r=r+Math.imul(w,ne)|0,a=(a=a+Math.imul(w,re)|0)+Math.imul(x,ne)|0,i=i+Math.imul(x,re)|0,r=r+Math.imul(y,ie)|0,a=(a=a+Math.imul(y,oe)|0)+Math.imul(v,ie)|0,i=i+Math.imul(v,oe)|0,r=r+Math.imul(d,ue)|0,a=(a=a+Math.imul(d,le)|0)+Math.imul(m,ue)|0,i=i+Math.imul(m,le)|0;var Ee=(l+(r=r+Math.imul(f,fe)|0)|0)+((8191&(a=(a=a+Math.imul(f,pe)|0)+Math.imul(p,fe)|0))<<13)|0;l=((i=i+Math.imul(p,pe)|0)+(a>>>13)|0)+(Ee>>>26)|0,Ee&=67108863,r=Math.imul(z,V),a=(a=Math.imul(z,U))+Math.imul(B,V)|0,i=Math.imul(B,U),r=r+Math.imul(D,q)|0,a=(a=a+Math.imul(D,$)|0)+Math.imul(j,q)|0,i=i+Math.imul(j,$)|0,r=r+Math.imul(T,H)|0,a=(a=a+Math.imul(T,K)|0)+Math.imul(M,H)|0,i=i+Math.imul(M,K)|0,r=r+Math.imul(C,Y)|0,a=(a=a+Math.imul(C,J)|0)+Math.imul(A,Y)|0,i=i+Math.imul(A,J)|0,r=r+Math.imul(O,Q)|0,a=(a=a+Math.imul(O,ee)|0)+Math.imul(k,Q)|0,i=i+Math.imul(k,ee)|0,r=r+Math.imul(S,ne)|0,a=(a=a+Math.imul(S,re)|0)+Math.imul(E,ne)|0,i=i+Math.imul(E,re)|0,r=r+Math.imul(w,ie)|0,a=(a=a+Math.imul(w,oe)|0)+Math.imul(x,ie)|0,i=i+Math.imul(x,oe)|0,r=r+Math.imul(y,ue)|0,a=(a=a+Math.imul(y,le)|0)+Math.imul(v,ue)|0,i=i+Math.imul(v,le)|0,r=r+Math.imul(d,fe)|0,a=(a=a+Math.imul(d,pe)|0)+Math.imul(m,fe)|0,i=i+Math.imul(m,pe)|0;var Ne=(l+(r=r+Math.imul(f,de)|0)|0)+((8191&(a=(a=a+Math.imul(f,me)|0)+Math.imul(p,de)|0))<<13)|0;l=((i=i+Math.imul(p,me)|0)+(a>>>13)|0)+(Ne>>>26)|0,Ne&=67108863,r=Math.imul(z,q),a=(a=Math.imul(z,$))+Math.imul(B,q)|0,i=Math.imul(B,$),r=r+Math.imul(D,H)|0,a=(a=a+Math.imul(D,K)|0)+Math.imul(j,H)|0,i=i+Math.imul(j,K)|0,r=r+Math.imul(T,Y)|0,a=(a=a+Math.imul(T,J)|0)+Math.imul(M,Y)|0,i=i+Math.imul(M,J)|0,r=r+Math.imul(C,Q)|0,a=(a=a+Math.imul(C,ee)|0)+Math.imul(A,Q)|0,i=i+Math.imul(A,ee)|0,r=r+Math.imul(O,ne)|0,a=(a=a+Math.imul(O,re)|0)+Math.imul(k,ne)|0,i=i+Math.imul(k,re)|0,r=r+Math.imul(S,ie)|0,a=(a=a+Math.imul(S,oe)|0)+Math.imul(E,ie)|0,i=i+Math.imul(E,oe)|0,r=r+Math.imul(w,ue)|0,a=(a=a+Math.imul(w,le)|0)+Math.imul(x,ue)|0,i=i+Math.imul(x,le)|0,r=r+Math.imul(y,fe)|0,a=(a=a+Math.imul(y,pe)|0)+Math.imul(v,fe)|0,i=i+Math.imul(v,pe)|0;var Oe=(l+(r=r+Math.imul(d,de)|0)|0)+((8191&(a=(a=a+Math.imul(d,me)|0)+Math.imul(m,de)|0))<<13)|0;l=((i=i+Math.imul(m,me)|0)+(a>>>13)|0)+(Oe>>>26)|0,Oe&=67108863,r=Math.imul(z,H),a=(a=Math.imul(z,K))+Math.imul(B,H)|0,i=Math.imul(B,K),r=r+Math.imul(D,Y)|0,a=(a=a+Math.imul(D,J)|0)+Math.imul(j,Y)|0,i=i+Math.imul(j,J)|0,r=r+Math.imul(T,Q)|0,a=(a=a+Math.imul(T,ee)|0)+Math.imul(M,Q)|0,i=i+Math.imul(M,ee)|0,r=r+Math.imul(C,ne)|0,a=(a=a+Math.imul(C,re)|0)+Math.imul(A,ne)|0,i=i+Math.imul(A,re)|0,r=r+Math.imul(O,ie)|0,a=(a=a+Math.imul(O,oe)|0)+Math.imul(k,ie)|0,i=i+Math.imul(k,oe)|0,r=r+Math.imul(S,ue)|0,a=(a=a+Math.imul(S,le)|0)+Math.imul(E,ue)|0,i=i+Math.imul(E,le)|0,r=r+Math.imul(w,fe)|0,a=(a=a+Math.imul(w,pe)|0)+Math.imul(x,fe)|0,i=i+Math.imul(x,pe)|0;var ke=(l+(r=r+Math.imul(y,de)|0)|0)+((8191&(a=(a=a+Math.imul(y,me)|0)+Math.imul(v,de)|0))<<13)|0;l=((i=i+Math.imul(v,me)|0)+(a>>>13)|0)+(ke>>>26)|0,ke&=67108863,r=Math.imul(z,Y),a=(a=Math.imul(z,J))+Math.imul(B,Y)|0,i=Math.imul(B,J),r=r+Math.imul(D,Q)|0,a=(a=a+Math.imul(D,ee)|0)+Math.imul(j,Q)|0,i=i+Math.imul(j,ee)|0,r=r+Math.imul(T,ne)|0,a=(a=a+Math.imul(T,re)|0)+Math.imul(M,ne)|0,i=i+Math.imul(M,re)|0,r=r+Math.imul(C,ie)|0,a=(a=a+Math.imul(C,oe)|0)+Math.imul(A,ie)|0,i=i+Math.imul(A,oe)|0,r=r+Math.imul(O,ue)|0,a=(a=a+Math.imul(O,le)|0)+Math.imul(k,ue)|0,i=i+Math.imul(k,le)|0,r=r+Math.imul(S,fe)|0,a=(a=a+Math.imul(S,pe)|0)+Math.imul(E,fe)|0,i=i+Math.imul(E,pe)|0;var Ie=(l+(r=r+Math.imul(w,de)|0)|0)+((8191&(a=(a=a+Math.imul(w,me)|0)+Math.imul(x,de)|0))<<13)|0;l=((i=i+Math.imul(x,me)|0)+(a>>>13)|0)+(Ie>>>26)|0,Ie&=67108863,r=Math.imul(z,Q),a=(a=Math.imul(z,ee))+Math.imul(B,Q)|0,i=Math.imul(B,ee),r=r+Math.imul(D,ne)|0,a=(a=a+Math.imul(D,re)|0)+Math.imul(j,ne)|0,i=i+Math.imul(j,re)|0,r=r+Math.imul(T,ie)|0,a=(a=a+Math.imul(T,oe)|0)+Math.imul(M,ie)|0,i=i+Math.imul(M,oe)|0,r=r+Math.imul(C,ue)|0,a=(a=a+Math.imul(C,le)|0)+Math.imul(A,ue)|0,i=i+Math.imul(A,le)|0,r=r+Math.imul(O,fe)|0,a=(a=a+Math.imul(O,pe)|0)+Math.imul(k,fe)|0,i=i+Math.imul(k,pe)|0;var Ce=(l+(r=r+Math.imul(S,de)|0)|0)+((8191&(a=(a=a+Math.imul(S,me)|0)+Math.imul(E,de)|0))<<13)|0;l=((i=i+Math.imul(E,me)|0)+(a>>>13)|0)+(Ce>>>26)|0,Ce&=67108863,r=Math.imul(z,ne),a=(a=Math.imul(z,re))+Math.imul(B,ne)|0,i=Math.imul(B,re),r=r+Math.imul(D,ie)|0,a=(a=a+Math.imul(D,oe)|0)+Math.imul(j,ie)|0,i=i+Math.imul(j,oe)|0,r=r+Math.imul(T,ue)|0,a=(a=a+Math.imul(T,le)|0)+Math.imul(M,ue)|0,i=i+Math.imul(M,le)|0,r=r+Math.imul(C,fe)|0,a=(a=a+Math.imul(C,pe)|0)+Math.imul(A,fe)|0,i=i+Math.imul(A,pe)|0;var Ae=(l+(r=r+Math.imul(O,de)|0)|0)+((8191&(a=(a=a+Math.imul(O,me)|0)+Math.imul(k,de)|0))<<13)|0;l=((i=i+Math.imul(k,me)|0)+(a>>>13)|0)+(Ae>>>26)|0,Ae&=67108863,r=Math.imul(z,ie),a=(a=Math.imul(z,oe))+Math.imul(B,ie)|0,i=Math.imul(B,oe),r=r+Math.imul(D,ue)|0,a=(a=a+Math.imul(D,le)|0)+Math.imul(j,ue)|0,i=i+Math.imul(j,le)|0,r=r+Math.imul(T,fe)|0,a=(a=a+Math.imul(T,pe)|0)+Math.imul(M,fe)|0,i=i+Math.imul(M,pe)|0;var Pe=(l+(r=r+Math.imul(C,de)|0)|0)+((8191&(a=(a=a+Math.imul(C,me)|0)+Math.imul(A,de)|0))<<13)|0;l=((i=i+Math.imul(A,me)|0)+(a>>>13)|0)+(Pe>>>26)|0,Pe&=67108863,r=Math.imul(z,ue),a=(a=Math.imul(z,le))+Math.imul(B,ue)|0,i=Math.imul(B,le),r=r+Math.imul(D,fe)|0,a=(a=a+Math.imul(D,pe)|0)+Math.imul(j,fe)|0,i=i+Math.imul(j,pe)|0;var Te=(l+(r=r+Math.imul(T,de)|0)|0)+((8191&(a=(a=a+Math.imul(T,me)|0)+Math.imul(M,de)|0))<<13)|0;l=((i=i+Math.imul(M,me)|0)+(a>>>13)|0)+(Te>>>26)|0,Te&=67108863,r=Math.imul(z,fe),a=(a=Math.imul(z,pe))+Math.imul(B,fe)|0,i=Math.imul(B,pe);var Me=(l+(r=r+Math.imul(D,de)|0)|0)+((8191&(a=(a=a+Math.imul(D,me)|0)+Math.imul(j,de)|0))<<13)|0;l=((i=i+Math.imul(j,me)|0)+(a>>>13)|0)+(Me>>>26)|0,Me&=67108863;var Re=(l+(r=Math.imul(z,de))|0)+((8191&(a=(a=Math.imul(z,me))+Math.imul(B,de)|0))<<13)|0;return l=((i=Math.imul(B,me))+(a>>>13)|0)+(Re>>>26)|0,Re&=67108863,u[0]=ge,u[1]=ye,u[2]=ve,u[3]=be,u[4]=we,u[5]=xe,u[6]=_e,u[7]=Se,u[8]=Ee,u[9]=Ne,u[10]=Oe,u[11]=ke,u[12]=Ie,u[13]=Ce,u[14]=Ae,u[15]=Pe,u[16]=Te,u[17]=Me,u[18]=Re,0!==l&&(u[19]=l,n.length++),n};function d(e,t,n){return(new m).mulp(e,t,n)}function m(e,t){this.x=e,this.y=t}Math.imul||(h=p),i.prototype.mulTo=function(e,t){var n=this.length+e.length;return 10===this.length&&10===e.length?h(this,e,t):n<63?p(this,e,t):n<1024?function(e,t,n){n.negative=t.negative^e.negative,n.length=e.length+t.length;for(var r=0,a=0,i=0;i>>26)|0)>>>26,o&=67108863}n.words[i]=s,r=o,o=a}return 0!==r?n.words[i]=r:n.length--,n.strip()}(this,e,t):d(this,e,t)},m.prototype.makeRBT=function(e){for(var t=new Array(e),n=i.prototype._countBits(e)-1,r=0;r>=1;return r},m.prototype.permute=function(e,t,n,r,a,i){for(var o=0;o>>=1)a++;return 1<>>=13,n[2*o+1]=8191&i,i>>>=13;for(o=2*t;o>=26,t+=a/67108864|0,t+=i>>>26,this.words[n]=67108863&i}return 0!==t&&(this.words[n]=t,this.length++),this},i.prototype.muln=function(e){return this.clone().imuln(e)},i.prototype.sqr=function(){return this.mul(this)},i.prototype.isqr=function(){return this.imul(this.clone())},i.prototype.pow=function(e){var t=function(e){for(var t=new Array(e.bitLength()),n=0;n>>a}return t}(e);if(0===t.length)return new i(1);for(var n=this,r=0;r=0);var t,n=e%26,a=(e-n)/26,i=67108863>>>26-n<<26-n;if(0!==n){var o=0;for(t=0;t>>26-n}o&&(this.words[t]=o,this.length++)}if(0!==a){for(t=this.length-1;t>=0;t--)this.words[t+a]=this.words[t];for(t=0;t=0),a=t?(t-t%26)/26:0;var i=e%26,o=Math.min((e-i)/26,this.length),s=67108863^67108863>>>i<o)for(this.length-=o,l=0;l=0&&(0!==c||l>=a);l--){var f=0|this.words[l];this.words[l]=c<<26-i|f>>>i,c=f&s}return u&&0!==c&&(u.words[u.length++]=c),0===this.length&&(this.words[0]=0,this.length=1),this.strip()},i.prototype.ishrn=function(e,t,n){return r(0===this.negative),this.iushrn(e,t,n)},i.prototype.shln=function(e){return this.clone().ishln(e)},i.prototype.ushln=function(e){return this.clone().iushln(e)},i.prototype.shrn=function(e){return this.clone().ishrn(e)},i.prototype.ushrn=function(e){return this.clone().iushrn(e)},i.prototype.testn=function(e){r("number"==typeof e&&e>=0);var t=e%26,n=(e-t)/26,a=1<=0);var t=e%26,n=(e-t)/26;if(r(0===this.negative,"imaskn works only with positive numbers"),this.length<=n)return this;if(0!==t&&n++,this.length=Math.min(n,this.length),0!==t){var a=67108863^67108863>>>t<=67108864;t++)this.words[t]-=67108864,t===this.length-1?this.words[t+1]=1:this.words[t+1]++;return this.length=Math.max(this.length,t+1),this},i.prototype.isubn=function(e){if(r("number"==typeof e),r(e<67108864),e<0)return this.iaddn(-e);if(0!==this.negative)return this.negative=0,this.iaddn(e),this.negative=1,this;if(this.words[0]-=e,1===this.length&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var t=0;t>26)-(u/67108864|0),this.words[a+n]=67108863&i}for(;a>26,this.words[a+n]=67108863&i;if(0===s)return this.strip();for(r(-1===s),s=0,a=0;a>26,this.words[a]=67108863&i;return this.negative=1,this.strip()},i.prototype._wordDiv=function(e,t){var n=(this.length,e.length),r=this.clone(),a=e,o=0|a.words[a.length-1];0!=(n=26-this._countBits(o))&&(a=a.ushln(n),r.iushln(n),o=0|a.words[a.length-1]);var s,u=r.length-a.length;if("mod"!==t){(s=new i(null)).length=u+1,s.words=new Array(s.length);for(var l=0;l=0;f--){var p=67108864*(0|r.words[a.length+f])+(0|r.words[a.length+f-1]);for(p=Math.min(p/o|0,67108863),r._ishlnsubmul(a,p,f);0!==r.negative;)p--,r.negative=0,r._ishlnsubmul(a,1,f),r.isZero()||(r.negative^=1);s&&(s.words[f]=p)}return s&&s.strip(),r.strip(),"div"!==t&&0!==n&&r.iushrn(n),{div:s||null,mod:r}},i.prototype.divmod=function(e,t,n){return r(!e.isZero()),this.isZero()?{div:new i(0),mod:new i(0)}:0!==this.negative&&0===e.negative?(s=this.neg().divmod(e,t),"mod"!==t&&(a=s.div.neg()),"div"!==t&&(o=s.mod.neg(),n&&0!==o.negative&&o.iadd(e)),{div:a,mod:o}):0===this.negative&&0!==e.negative?(s=this.divmod(e.neg(),t),"mod"!==t&&(a=s.div.neg()),{div:a,mod:s.mod}):0!=(this.negative&e.negative)?(s=this.neg().divmod(e.neg(),t),"div"!==t&&(o=s.mod.neg(),n&&0!==o.negative&&o.isub(e)),{div:s.div,mod:o}):e.length>this.length||this.cmp(e)<0?{div:new i(0),mod:this}:1===e.length?"div"===t?{div:this.divn(e.words[0]),mod:null}:"mod"===t?{div:null,mod:new i(this.modn(e.words[0]))}:{div:this.divn(e.words[0]),mod:new i(this.modn(e.words[0]))}:this._wordDiv(e,t);var a,o,s},i.prototype.div=function(e){return this.divmod(e,"div",!1).div},i.prototype.mod=function(e){return this.divmod(e,"mod",!1).mod},i.prototype.umod=function(e){return this.divmod(e,"mod",!0).mod},i.prototype.divRound=function(e){var t=this.divmod(e);if(t.mod.isZero())return t.div;var n=0!==t.div.negative?t.mod.isub(e):t.mod,r=e.ushrn(1),a=e.andln(1),i=n.cmp(r);return i<0||1===a&&0===i?t.div:0!==t.div.negative?t.div.isubn(1):t.div.iaddn(1)},i.prototype.modn=function(e){r(e<=67108863);for(var t=(1<<26)%e,n=0,a=this.length-1;a>=0;a--)n=(t*n+(0|this.words[a]))%e;return n},i.prototype.idivn=function(e){r(e<=67108863);for(var t=0,n=this.length-1;n>=0;n--){var a=(0|this.words[n])+67108864*t;this.words[n]=a/e|0,t=a%e}return this.strip()},i.prototype.divn=function(e){return this.clone().idivn(e)},i.prototype.egcd=function(e){r(0===e.negative),r(!e.isZero());var t=this,n=e.clone();t=0!==t.negative?t.umod(e):t.clone();for(var a=new i(1),o=new i(0),s=new i(0),u=new i(1),l=0;t.isEven()&&n.isEven();)t.iushrn(1),n.iushrn(1),++l;for(var c=n.clone(),f=t.clone();!t.isZero();){for(var p=0,h=1;0==(t.words[0]&h)&&p<26;++p,h<<=1);if(p>0)for(t.iushrn(p);p-- >0;)(a.isOdd()||o.isOdd())&&(a.iadd(c),o.isub(f)),a.iushrn(1),o.iushrn(1);for(var d=0,m=1;0==(n.words[0]&m)&&d<26;++d,m<<=1);if(d>0)for(n.iushrn(d);d-- >0;)(s.isOdd()||u.isOdd())&&(s.iadd(c),u.isub(f)),s.iushrn(1),u.iushrn(1);t.cmp(n)>=0?(t.isub(n),a.isub(s),o.isub(u)):(n.isub(t),s.isub(a),u.isub(o))}return{a:s,b:u,gcd:n.iushln(l)}},i.prototype._invmp=function(e){r(0===e.negative),r(!e.isZero());var t=this,n=e.clone();t=0!==t.negative?t.umod(e):t.clone();for(var a,o=new i(1),s=new i(0),u=n.clone();t.cmpn(1)>0&&n.cmpn(1)>0;){for(var l=0,c=1;0==(t.words[0]&c)&&l<26;++l,c<<=1);if(l>0)for(t.iushrn(l);l-- >0;)o.isOdd()&&o.iadd(u),o.iushrn(1);for(var f=0,p=1;0==(n.words[0]&p)&&f<26;++f,p<<=1);if(f>0)for(n.iushrn(f);f-- >0;)s.isOdd()&&s.iadd(u),s.iushrn(1);t.cmp(n)>=0?(t.isub(n),o.isub(s)):(n.isub(t),s.isub(o))}return(a=0===t.cmpn(1)?o:s).cmpn(0)<0&&a.iadd(e),a},i.prototype.gcd=function(e){if(this.isZero())return e.abs();if(e.isZero())return this.abs();var t=this.clone(),n=e.clone();t.negative=0,n.negative=0;for(var r=0;t.isEven()&&n.isEven();r++)t.iushrn(1),n.iushrn(1);for(;;){for(;t.isEven();)t.iushrn(1);for(;n.isEven();)n.iushrn(1);var a=t.cmp(n);if(a<0){var i=t;t=n,n=i}else if(0===a||0===n.cmpn(1))break;t.isub(n)}return n.iushln(r)},i.prototype.invm=function(e){return this.egcd(e).a.umod(e)},i.prototype.isEven=function(){return 0==(1&this.words[0])},i.prototype.isOdd=function(){return 1==(1&this.words[0])},i.prototype.andln=function(e){return this.words[0]&e},i.prototype.bincn=function(e){r("number"==typeof e);var t=e%26,n=(e-t)/26,a=1<>>26,s&=67108863,this.words[o]=s}return 0!==i&&(this.words[o]=i,this.length++),this},i.prototype.isZero=function(){return 1===this.length&&0===this.words[0]},i.prototype.cmpn=function(e){var t,n=e<0;if(0!==this.negative&&!n)return-1;if(0===this.negative&&n)return 1;if(this.strip(),this.length>1)t=1;else{n&&(e=-e),r(e<=67108863,"Number is too big");var a=0|this.words[0];t=a===e?0:ae.length)return 1;if(this.length=0;n--){var r=0|this.words[n],a=0|e.words[n];if(r!==a){ra&&(t=1);break}}return t},i.prototype.gtn=function(e){return 1===this.cmpn(e)},i.prototype.gt=function(e){return 1===this.cmp(e)},i.prototype.gten=function(e){return this.cmpn(e)>=0},i.prototype.gte=function(e){return this.cmp(e)>=0},i.prototype.ltn=function(e){return-1===this.cmpn(e)},i.prototype.lt=function(e){return-1===this.cmp(e)},i.prototype.lten=function(e){return this.cmpn(e)<=0},i.prototype.lte=function(e){return this.cmp(e)<=0},i.prototype.eqn=function(e){return 0===this.cmpn(e)},i.prototype.eq=function(e){return 0===this.cmp(e)},i.red=function(e){return new _(e)},i.prototype.toRed=function(e){return r(!this.red,"Already a number in reduction context"),r(0===this.negative,"red works only with positives"),e.convertTo(this)._forceRed(e)},i.prototype.fromRed=function(){return r(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)},i.prototype._forceRed=function(e){return this.red=e,this},i.prototype.forceRed=function(e){return r(!this.red,"Already a number in reduction context"),this._forceRed(e)},i.prototype.redAdd=function(e){return r(this.red,"redAdd works only with red numbers"),this.red.add(this,e)},i.prototype.redIAdd=function(e){return r(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,e)},i.prototype.redSub=function(e){return r(this.red,"redSub works only with red numbers"),this.red.sub(this,e)},i.prototype.redISub=function(e){return r(this.red,"redISub works only with red numbers"),this.red.isub(this,e)},i.prototype.redShl=function(e){return r(this.red,"redShl works only with red numbers"),this.red.shl(this,e)},i.prototype.redMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.mul(this,e)},i.prototype.redIMul=function(e){return r(this.red,"redMul works only with red numbers"),this.red._verify2(this,e),this.red.imul(this,e)},i.prototype.redSqr=function(){return r(this.red,"redSqr works only with red numbers"),this.red._verify1(this),this.red.sqr(this)},i.prototype.redISqr=function(){return r(this.red,"redISqr works only with red numbers"),this.red._verify1(this),this.red.isqr(this)},i.prototype.redSqrt=function(){return r(this.red,"redSqrt works only with red numbers"),this.red._verify1(this),this.red.sqrt(this)},i.prototype.redInvm=function(){return r(this.red,"redInvm works only with red numbers"),this.red._verify1(this),this.red.invm(this)},i.prototype.redNeg=function(){return r(this.red,"redNeg works only with red numbers"),this.red._verify1(this),this.red.neg(this)},i.prototype.redPow=function(e){return r(this.red&&!e.red,"redPow(normalNum)"),this.red._verify1(this),this.red.pow(this,e)};var g={k256:null,p224:null,p192:null,p25519:null};function y(e,t){this.name=e,this.p=new i(t,16),this.n=this.p.bitLength(),this.k=new i(1).iushln(this.n).isub(this.p),this.tmp=this._tmp()}function v(){y.call(this,"k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}function b(){y.call(this,"p224","ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001")}function w(){y.call(this,"p192","ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff")}function x(){y.call(this,"25519","7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed")}function _(e){if("string"==typeof e){var t=i._prime(e);this.m=t.p,this.prime=t}else r(e.gtn(1),"modulus must be greater than 1"),this.m=e,this.prime=null}function S(e){_.call(this,e),this.shift=this.m.bitLength(),this.shift%26!=0&&(this.shift+=26-this.shift%26),this.r=new i(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}y.prototype._tmp=function(){var e=new i(null);return e.words=new Array(Math.ceil(this.n/13)),e},y.prototype.ireduce=function(e){var t,n=e;do{this.split(n,this.tmp),t=(n=(n=this.imulK(n)).iadd(this.tmp)).bitLength()}while(t>this.n);var r=t0?n.isub(this.p):n.strip(),n},y.prototype.split=function(e,t){e.iushrn(this.n,0,t)},y.prototype.imulK=function(e){return e.imul(this.k)},a(v,y),v.prototype.split=function(e,t){for(var n=Math.min(e.length,9),r=0;r>>22,a=i}a>>>=22,e.words[r-10]=a,0===a&&e.length>10?e.length-=10:e.length-=9},v.prototype.imulK=function(e){e.words[e.length]=0,e.words[e.length+1]=0,e.length+=2;for(var t=0,n=0;n>>=26,e.words[n]=a,t=r}return 0!==t&&(e.words[e.length++]=t),e},i._prime=function(e){if(g[e])return g[e];var t;if("k256"===e)t=new v;else if("p224"===e)t=new b;else if("p192"===e)t=new w;else{if("p25519"!==e)throw new Error("Unknown prime "+e);t=new x}return g[e]=t,t},_.prototype._verify1=function(e){r(0===e.negative,"red works only with positives"),r(e.red,"red works only with red numbers")},_.prototype._verify2=function(e,t){r(0==(e.negative|t.negative),"red works only with positives"),r(e.red&&e.red===t.red,"red works only with red numbers")},_.prototype.imod=function(e){return this.prime?this.prime.ireduce(e)._forceRed(this):e.umod(this.m)._forceRed(this)},_.prototype.neg=function(e){return e.isZero()?e.clone():this.m.sub(e)._forceRed(this)},_.prototype.add=function(e,t){this._verify2(e,t);var n=e.add(t);return n.cmp(this.m)>=0&&n.isub(this.m),n._forceRed(this)},_.prototype.iadd=function(e,t){this._verify2(e,t);var n=e.iadd(t);return n.cmp(this.m)>=0&&n.isub(this.m),n},_.prototype.sub=function(e,t){this._verify2(e,t);var n=e.sub(t);return n.cmpn(0)<0&&n.iadd(this.m),n._forceRed(this)},_.prototype.isub=function(e,t){this._verify2(e,t);var n=e.isub(t);return n.cmpn(0)<0&&n.iadd(this.m),n},_.prototype.shl=function(e,t){return this._verify1(e),this.imod(e.ushln(t))},_.prototype.imul=function(e,t){return this._verify2(e,t),this.imod(e.imul(t))},_.prototype.mul=function(e,t){return this._verify2(e,t),this.imod(e.mul(t))},_.prototype.isqr=function(e){return this.imul(e,e.clone())},_.prototype.sqr=function(e){return this.mul(e,e)},_.prototype.sqrt=function(e){if(e.isZero())return e.clone();var t=this.m.andln(3);if(r(t%2==1),3===t){var n=this.m.add(new i(1)).iushrn(2);return this.pow(e,n)}for(var a=this.m.subn(1),o=0;!a.isZero()&&0===a.andln(1);)o++,a.iushrn(1);r(!a.isZero());var s=new i(1).toRed(this),u=s.redNeg(),l=this.m.subn(1).iushrn(1),c=this.m.bitLength();for(c=new i(2*c*c).toRed(this);0!==this.pow(c,l).cmp(u);)c.redIAdd(u);for(var f=this.pow(c,a),p=this.pow(e,a.addn(1).iushrn(1)),h=this.pow(e,a),d=o;0!==h.cmp(s);){for(var m=h,g=0;0!==m.cmp(s);g++)m=m.redSqr();r(g=0;r--){for(var l=t.words[r],c=u-1;c>=0;c--){var f=l>>c&1;a!==n[0]&&(a=this.sqr(a)),0!==f||0!==o?(o<<=1,o|=f,(4==++s||0===r&&0===c)&&(a=this.mul(a,n[o]),s=0,o=0)):s=0}u=26}return a},_.prototype.convertTo=function(e){var t=e.umod(this.m);return t===e?t.clone():t},_.prototype.convertFrom=function(e){var t=e.clone();return t.red=null,t},i.mont=function(e){return new S(e)},a(S,_),S.prototype.convertTo=function(e){return this.imod(e.ushln(this.shift))},S.prototype.convertFrom=function(e){var t=this.imod(e.mul(this.rinv));return t.red=null,t},S.prototype.imul=function(e,t){if(e.isZero()||t.isZero())return e.words[0]=0,e.length=1,e;var n=e.imul(t),r=n.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),a=n.isub(r).iushrn(this.shift),i=a;return a.cmp(this.m)>=0?i=a.isub(this.m):a.cmpn(0)<0&&(i=a.iadd(this.m)),i._forceRed(this)},S.prototype.mul=function(e,t){if(e.isZero()||t.isZero())return new i(0)._forceRed(this);var n=e.mul(t),r=n.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),a=n.isub(r).iushrn(this.shift),o=a;return a.cmp(this.m)>=0?o=a.isub(this.m):a.cmpn(0)<0&&(o=a.iadd(this.m)),o._forceRed(this)},S.prototype.invm=function(e){return this.imod(e._invmp(this.m).mul(this.r2))._forceRed(this)}}(void 0===e||e,this)}).call(this,n(436)(e))},function(e,t,n){"use strict";(function(e){ + */function o(t,e){function n(){this.constructor=t}i(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var a,s=function(){return(s=Object.assign||function(t){for(var e,n=1,r=arguments.length;n0&&i[i.length-1])&&(6===o[0]||2===o[0])){a=0;continue}if(3===o[0]&&(!i||o[1]>i[0]&&o[1]e?1:0}(t,e)}function N(t){if(null==t)return t;for(var e=[],n=0,r=t;n=0),g(r>=n),Array.isArray(t)&&t.length>=n&&t.length<=r&&t.every(function(t){return typeof t===e})}function T(t,e){Array.isArray(t)?(r.util.assert(t.length>0,function(){return e+" is unexpectedly an empty array."}),t.forEach(function(t,n){return T(t,"element "+(n+1)+" of "+e)})):r.util.assert(Number.isInteger(t)&&t>0,function(){return"Expected "+e+" to be a positive integer, but got "+function t(e){return null===e?"null":Array.isArray(e)?"["+e.map(function(e){return t(e)}).join(",")+"]":"string"==typeof e?'"'+e+'"':""+e}(t)+"."})}function M(t,e){return Object(r.tidy)(function(){return Object(r.sqrt)(Object(r.sum)(Object(r.mulStrict)(t,t),e,!0))})}var R=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.getConfig=function(){return{}},e}(r.serialization.Serializable),D=function(t){function e(e){var n=t.call(this)||this;return n.defaultMaxValue=2,n.defaultAxis=0,n.maxValue=null!=e.maxValue?e.maxValue:n.defaultMaxValue,n.axis=null!=e.axis?e.axis:n.defaultAxis,n}return o(e,t),e.prototype.apply=function(t){var e=this;return Object(r.tidy)(function(){var n=M(t,e.axis),i=Object(r.clipByValue)(n,0,e.maxValue);return Object(r.mul)(t,Object(r.div)(i,Object(r.add)(l(),n)))})},e.prototype.getConfig=function(){return{maxValue:this.maxValue,axis:this.axis}},e.className="MaxNorm",e}(R);r.serialization.registerClass(D);var j=function(t){function e(e){var n=t.call(this)||this;return n.defaultAxis=0,n.axis=null!=e.axis?e.axis:n.defaultAxis,n}return o(e,t),e.prototype.apply=function(t){var e=this;return Object(r.tidy)(function(){return Object(r.div)(t,Object(r.add)(l(),M(t,e.axis)))})},e.prototype.getConfig=function(){return{axis:this.axis}},e.className="UnitNorm",e}(R);r.serialization.registerClass(j);var P=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return Object(r.relu)(t)},e.className="NonNeg",e}(R);r.serialization.registerClass(P);var z=function(t){function e(e){var n=t.call(this)||this;return n.defaultMinValue=0,n.defaultMaxValue=1,n.defaultRate=1,n.defaultAxis=0,n.minValue=null!=e.minValue?e.minValue:n.defaultMinValue,n.maxValue=null!=e.maxValue?e.maxValue:n.defaultMaxValue,n.rate=null!=e.rate?e.rate:n.defaultRate,n.axis=null!=e.axis?e.axis:n.defaultAxis,n}return o(e,t),e.prototype.apply=function(t){var e=this;return Object(r.tidy)(function(){var n=M(t,e.axis),i=Object(r.add)(Object(r.mul)(e.rate,Object(r.clipByValue)(n,e.minValue,e.maxValue)),Object(r.mul)(1-e.rate,n));return Object(r.mul)(t,Object(r.div)(i,Object(r.add)(l(),n)))})},e.prototype.getConfig=function(){return{minValue:this.minValue,maxValue:this.maxValue,rate:this.rate,axis:this.axis}},e.className="MinMaxNorm",e}(R);r.serialization.registerClass(z);var L={maxNorm:"MaxNorm",minMaxNorm:"MinMaxNorm",nonNeg:"NonNeg",unitNorm:"UnitNorm"};function F(t){return _(t)}function B(t,e){return void 0===e&&(e={}),E(t,r.serialization.SerializationMap.getMap().classNameMap,e,"constraint")}function U(t){return null==t?null:"string"==typeof t?B({className:t in L?L[t]:t,config:{}}):t instanceof R?t:B(t)}var V=Object.freeze({maxNorm:function(t){return new D(t)},unitNorm:function(t){return new j(t)},nonNeg:function(){return new P},minMaxNorm:function(t){return new z(t)}}),W=["channelsFirst","channelsLast"],q=["valid","same","causal"],H=["max","avg"],G=["sum","mul","concat","ave"],K=new Map;function X(t){O(W,"DataFormat",t)}function $(t){O(q,"PaddingMode",t)}var Y=[],J="/";function Z(t,e){Y.push(t);try{var n=e();return Y.pop(),n}catch(t){throw Y.pop(),t}}function Q(t){if(!nt(t))throw new Error("Not a valid tensor name: '"+t+"'");return(0===Y.length?"":Y.join(J)+J)+t}function tt(t){if(!nt(t))throw new Error("Not a valid tensor name: '"+t+"'");K.has(t)||K.set(t,0);var e=K.get(t);if(K.set(t,K.get(t)+1),e>0){var n=t+"_"+e;return K.set(n,1),n}return t}var et=new RegExp(/^[A-Za-z][-A-Za-z0-9\._\/]*$/);function nt(t){return!!t.match(et)}function rt(t){return t===parseInt(t.toString(),10)}function it(t,e,n){null==e&&(e=0),null==n&&(n=t.length);for(var r=1,i=e;i= 2 but got x shape = "+t.shape+" and y shape = "+e.shape);if(e.rank>=3&&(u=t.shape.slice(-1)[0])!==(f=e.shape.slice(-2)[0]))throw new p("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){var o=!1,a=!1;return r.fused.matMul(t,e,o,a,i?xt(t.rank,i,"channelsLast"):null,n)}var s=t.shape.slice(),u=s.pop();t=t.reshape([-1,u]);var c=e.shape.slice(),l=c.pop(),f=c.pop(),h=c.concat([l]),d=Array.from({length:e.rank},function(t,n){return 0===n?e.rank-2:n<=e.rank-2?n-1:n});e=e.transpose(d).reshape([f,-1]);var m=s.concat(h);return o=!1,a=!1,r.fused.matMul(t,e,o,a,i?xt(t.rank,i,"channelsLast"):null,n).reshape(m)}function bt(t,e,n){return Object(r.tidy)(function(){return e=Array.isArray(e)?Object(r.tensor1d)(e,"int32"):e.toInt(),Object(r.gather)(t,e,n)})}function wt(t){return Object(r.mulStrict)(t,t)}function xt(t,e,n){var r=e.shape;if(1!==e.rank&&e.rank!==t)throw new d("Unexpected bias dimensions: "+e.rank+"; expected it to be 1 or "+t);if(5===t){if("channelsFirst"===n)return 1===r.length?e.reshape([1,r[0],1,1,1]):e.reshape([1,r[3],r[0],r[1],r[2]]);if("channelsLast"===n)return 1===r.length?e.reshape([1,1,1,1,r[0]]):e.reshape([1].concat(r))}else if(4===t){if("channelsFirst"===n)return 1===r.length?e.reshape([1,r[0],1,1]):e.reshape([1,r[2],r[0],r[1]]);if("channelsLast"===n)return 1===r.length?e.reshape([1,1,1,r[0]]):e.reshape([1].concat(r))}else if(3===t){if("channelsFirst"===n)return 1===r.length?e.reshape([1,r[0],1]):e.reshape([1,r[1],r[0]]);if("channelsLast"===n)return 1===r.length?e.reshape([1,1,r[0]]):e.reshape([1].concat(r))}else if(t<3)return e;throw new d("Unsupported input rank by biasAdd: "+e.rank)}function kt(t,e,n){return Object(r.tidy)(function(){return null==n&&(n="channelsLast"),X(n),t.add(xt(t.rank,e,n))})}function St(t,e,n,i){return Object(r.tidy)(function(){if(null!=n&&!r.util.arraysEqual(t.shape,n))throw new p("Non-default noise shape is not implemented yet: "+JSON.stringify(n));if(null!=i)throw new p("seed is not implemented for dropout yet.");var o=Object(r.step)(Object(r.add)(-e,Object(r.randomUniform)(t.shape,0,1,"float32")));return o=Object(r.mul)(1/(1-e),o),Object(r.mul)(t,o)})}function _t(t,e,n){return void 0===n&&(n=!1),n?t():e()}var Et=["fanIn","fanOut","fanAvg"],Ct=["normal","uniform","truncatedNormal"],Nt=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.fromConfigUsesCustomObjects=function(){return!1},e.prototype.getConfig=function(){return{}},e}(r.serialization.Serializable),At=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t,e){return Object(r.zeros)(t,e)},e.className="Zeros",e}(Nt);r.serialization.registerClass(At);var Ot=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t,e){return Object(r.ones)(t,e)},e.className="Ones",e}(Nt);r.serialization.registerClass(Ot);var It=function(t){function e(e){var n=t.call(this)||this;if("object"!=typeof e)throw new d("Expected argument of type ConstantConfig but got "+e);if(void 0===e.value)throw new d("config must have value set but got "+e);return n.value=e.value,n}return o(e,t),e.prototype.apply=function(t,e){var n=this;return Object(r.tidy)(function(){return Object(r.mul)(Object(r.scalar)(n.value),Object(r.ones)(t,e))})},e.prototype.getConfig=function(){return{value:this.value}},e.className="Constant",e}(Nt);r.serialization.registerClass(It);var Tt=function(t){function e(e){var n=t.call(this)||this;return n.DEFAULT_MINVAL=-.05,n.DEFAULT_MAXVAL=.05,n.minval=e.minval||n.DEFAULT_MINVAL,n.maxval=e.maxval||n.DEFAULT_MAXVAL,n.seed=e.seed,n}return o(e,t),e.prototype.apply=function(t,e){return Object(r.randomUniform)(t,this.minval,this.maxval,e)},e.prototype.getConfig=function(){return{minval:this.minval,maxval:this.maxval,seed:this.seed}},e.className="RandomUniform",e}(Nt);r.serialization.registerClass(Tt);var Mt=function(t){function e(e){var n=t.call(this)||this;return n.DEFAULT_MEAN=0,n.DEFAULT_STDDEV=.05,n.mean=e.mean||n.DEFAULT_MEAN,n.stddev=e.stddev||n.DEFAULT_STDDEV,n.seed=e.seed,n}return o(e,t),e.prototype.apply=function(t,e){if("float32"!==(e=e||"float32")&&"int32"!==e)throw new p("randomNormal does not support dType "+e+".");return gt(t,this.mean,this.stddev,e,this.seed)},e.prototype.getConfig=function(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}},e.className="RandomNormal",e}(Nt);r.serialization.registerClass(Mt);var Rt=function(t){function e(e){var n=t.call(this)||this;return n.DEFAULT_MEAN=0,n.DEFAULT_STDDEV=.05,n.mean=e.mean||n.DEFAULT_MEAN,n.stddev=e.stddev||n.DEFAULT_STDDEV,n.seed=e.seed,n}return o(e,t),e.prototype.apply=function(t,e){if("float32"!==(e=e||"float32")&&"int32"!==e)throw new p("truncatedNormal does not support dType "+e+".");return Object(r.truncatedNormal)(t,this.mean,this.stddev,e,this.seed)},e.prototype.getConfig=function(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}},e.className="TruncatedNormal",e}(Nt);r.serialization.registerClass(Rt);var Dt=function(t){function e(e){var n=t.call(this)||this;return n.gain=null!=e.gain?e.gain:1,n}return o(e,t),e.prototype.apply=function(t,e){var n=this;return Object(r.tidy)(function(){if(2!==t.length||t[0]!==t[1])throw new d("Identity matrix initializer can only be used for 2D square matrices.");return Object(r.mul)(n.gain,Object(r.eye)(t[0]))})},e.prototype.getConfig=function(){return{gain:this.gain}},e.className="Identity",e}(Nt);r.serialization.registerClass(Dt);var jt=function(t){function e(e){var n=t.call(this)||this;if(e.scale<0)throw new d("scale must be a positive float. Got: "+e.scale);return n.scale=null==e.scale?1:e.scale,n.mode=null==e.mode?"fanIn":e.mode,function(t){O(Et,"FanMode",t)}(n.mode),n.distribution=null==e.distribution?"normal":e.distribution,function(t){O(Ct,"Distribution",t)}(n.distribution),n.seed=e.seed,n}return o(e,t),e.prototype.apply=function(t,e){var n=function(t,e){var n,r;if(void 0===e&&(e="channelsLast"),X(e),2===t.length)n=t[0],r=t[1];else if(-1!==[3,4,5].indexOf(t.length))if("channelsFirst"===e){var i=it(t,2);n=t[1]*i,r=t[0]*i}else"channelsLast"===e&&(i=it(t,0,t.length-2),n=t[t.length-2]*i,r=t[t.length-1]*i);else{var o=it(t);n=Math.sqrt(o),r=Math.sqrt(o)}return[n,r]}(t),i=n[0],o=n[1],a=this.scale;if("fanIn"===this.mode?a/=Math.max(1,i):"fanOut"===this.mode?a/=Math.max(1,o):a/=Math.max(1,(i+o)/2),"normal"===this.distribution){var s=Math.sqrt(a);if("float32"!==(e=e||"float32")&&"int32"!==e)throw new p(this.getClassName()+" does not support dType "+e+".");return Object(r.truncatedNormal)(t,0,s,e,this.seed)}var u=Math.sqrt(3*a);return Object(r.randomUniform)(t,-u,u,e)},e.prototype.getConfig=function(){return{scale:this.scale,mode:this.mode,distribution:this.distribution,seed:this.seed}},e.className="VarianceScaling",e}(Nt);r.serialization.registerClass(jt);var Pt=function(t){function e(e){return t.call(this,{scale:1,mode:"fanAvg",distribution:"uniform",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return jt.className},e.className="GlorotUniform",e}(jt);r.serialization.registerClass(Pt);var zt=function(t){function e(e){return t.call(this,{scale:1,mode:"fanAvg",distribution:"normal",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return jt.className},e.className="GlorotNormal",e}(jt);r.serialization.registerClass(zt);var Lt=function(t){function e(e){return t.call(this,{scale:2,mode:"fanIn",distribution:"normal",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return jt.className},e.className="HeNormal",e}(jt);r.serialization.registerClass(Lt);var Ft=function(t){function e(e){return t.call(this,{scale:2,mode:"fanIn",distribution:"uniform",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return jt.className},e.className="HeUniform",e}(jt);r.serialization.registerClass(Ft);var Bt=function(t){function e(e){return t.call(this,{scale:1,mode:"fanIn",distribution:"normal",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return jt.className},e.className="LeCunNormal",e}(jt);r.serialization.registerClass(Bt);var Ut=function(t){function e(e){return t.call(this,{scale:1,mode:"fanIn",distribution:"uniform",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return jt.className},e.className="LeCunNormal",e}(jt);r.serialization.registerClass(Ut);var Vt=function(t){function e(e){var n=t.call(this)||this;if(n.DEFAULT_GAIN=1,n.gain=null==e.gain?n.DEFAULT_GAIN:e.gain,n.seed=e.seed,null!=n.seed)throw new p("Random seed is not implemented for Orthogonal Initializer yet.");return n}return o(e,t),e.prototype.apply=function(t,e){var n=this;return Object(r.tidy)(function(){if(2!==t.length)throw new p("The Orthogonal Initializer does not support non-2D shapes yet.");t[0]*t[1]>2e3&&console.warn("Orthogonal initializer is being called on a matrix with more than 2000 ("+t[0]*t[1]+") elements: Slowness may result.");var e=gt(t[0]>t[1]?[t[1],t[0]]:t,0,1,"float32"),i=r.linalg.gramSchmidt(e);return t[0]>t[1]&&(i=i.transpose()),Object(r.mul)(n.gain,i)})},e.prototype.getConfig=function(){return{gain:this.gain,seed:this.seed}},e.className="Orthogonal",e}(Nt);r.serialization.registerClass(Vt);var Wt={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 qt(t,e){return void 0===e&&(e={}),E(t,r.serialization.SerializationMap.getMap().classNameMap,e,"initializer")}function Ht(t){return _(t)}function Gt(t){if("string"==typeof t){var e=t in Wt?Wt[t]:t;if("GlorotNormal"===e)return new zt;if("GlorotUniform"===e)return new Pt;if("HeNormal"===e)return new Lt;if("HeUniform"===e)return new Ft;if("LeCunNormal"===e)return new Bt;if("LeCunUniform"===e)return new Ut;var n={};return n.className=e,n.config={},qt(n)}return t instanceof Nt?t:qt(t)}var Kt=Object.freeze({zeros:function(){return new At},ones:function(){return new Ot},constant:function(t){return new It(t)},randomUniform:function(t){return new Tt(t)},randomNormal:function(t){return new Mt(t)},truncatedNormal:function(t){return new Rt(t)},identity:function(t){return new Dt(t)},varianceScaling:function(t){return new jt(t)},glorotUniform:function(t){return new Pt(t)},glorotNormal:function(t){return new zt(t)},heNormal:function(t){return new Lt(t)},heUniform:function(t){return new Ft(t)},leCunNormal:function(t){return new Bt(t)},leCunUniform:function(t){return new Ut(t)},orthogonal:function(t){return new Vt(t)}}),Xt=0;function $t(){return Xt++}var Yt={};function Jt(t){return void 0===t&&(t=""),t in Yt||(Yt[t]=0),Yt[t]+=1,t+Yt[t].toString()}function Zt(t){return Array.isArray(t)&&Array.isArray(t[0])}function Qt(t){return 0===t.length?[]:Array.isArray(t[0])?t:[t]}function te(t){var e;if(Array.isArray(t)){if(1!==t.length)throw new d("Expected Tensor length to be 1; got "+t.length);e=t[0]}else e=t;return e}function ee(t){if(Array.isArray(t)&&Array.isArray(t[0])){if(1===t.length)return(t=t)[0];throw new d("Expected exactly 1 Shape; got "+t.length)}return t}function ne(t){for(var e=0,n=0,r=t;n1)throw new f("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 f("Layer "+this.name+" is not connected, no input to return.");return b(this.getNodeAtIndex(0,"input").inputTensors)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"output",{get:function(){if(0===this.inboundNodes.length)throw new f("Layer "+this.name+" has no inbound nodes.");if(this.inboundNodes.length>1)throw new f("Layer "+this.name+' has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use `getOutputAt(nodeIndex)` instead.');return b(this.getNodeAtIndex(0,"output").outputTensors)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"losses",{get:function(){return this._losses},enumerable:!0,configurable:!0}),e.prototype.calculateLosses=function(){return this.losses.map(function(t){return t()})},Object.defineProperty(e.prototype,"updates",{get:function(){return this._updates},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"built",{get:function(){return this._built},set:function(t){this._built=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"trainable",{get:function(){return this.trainable_},set:function(t){this._trainableWeights.forEach(function(e){return e.trainable=t}),this.trainable_=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"trainableWeights",{get:function(){return this.trainable_?this._trainableWeights.filter(function(t){return t.trainable}):[]},set:function(t){this._trainableWeights=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"nonTrainableWeights",{get:function(){return this.trainable?this._trainableWeights.filter(function(t){return!t.trainable}).concat(this._nonTrainableWeights):this._trainableWeights.concat(this._nonTrainableWeights)},set:function(t){this._nonTrainableWeights=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"weights",{get:function(){return this.trainableWeights.concat(this.nonTrainableWeights)},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"stateful",{get:function(){return this._stateful},enumerable:!0,configurable:!0}),e.prototype.resetStates=function(){if(!this.stateful)throw new Error("Cannot call the resetStates() method of a non-stateful Layer object.")},e.prototype.assertInputCompatibility=function(t){if(t=w(t),null!=this.inputSpec&&0!==this.inputSpec.length){var e=w(this.inputSpec);if(t.length!==e.length)throw new d("Layer "+this.name+" expects "+e.length+" inputs, but it received "+t.length+" input tensors. Input received: "+t);for(var n=0;n=0?a[u]:a[a.length+u];if(null!=c&&-1===[c,null].indexOf(l))throw new d("Input "+n+" is incompatible with layer "+this.name+": expected axis "+u+" of input shape to have value "+c+" but got shape "+a+".")}}if(null!=i.shape)for(var f=0;f0&&Array.isArray(m[0])?m.map(function(r,i){return new ce(g,r,n,w(t),e,n.name,i)}):new ce(g,m,n,w(t),e,n.name),n.addInboundNode(t,v,null,null,d,m,e),n._refCount++,null!=n.activityRegularizer)throw new p("Layer invocation in the presence of activity regularizer(s) is not supported yet.");return v})},e.prototype.warnOnIncompatibleInputShape=function(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{var e=!1;this.batchInputShape.forEach(function(n,r){null!=n&&null!=t[r]&&t[r]!==n&&(e=!0)}),e&&console.warn("The shape of the input tensor ("+JSON.stringify(t)+") does not match the expectation of layer "+this.name+": "+JSON.stringify(this.batchInputShape))}},Object.defineProperty(e.prototype,"outputShape",{get:function(){if(null==this.inboundNodes||0===this.inboundNodes.length)throw new f("The layer "+this.name+" has never been called and thus has no defined output shape.");for(var t=[],e=0,n=this.inboundNodes;e0?[4,Promise.all(e)]:[3,2];case 1:for(u=c.sent(),l=0;l=0&&Number.isInteger(e),function(){return"Verbosity level is expected to be an integer >= 0, but got "+e}),t.checkForDuplicate(n),null==t.constructors[e]&&(t.constructors[e]=[]),t.constructors[e].push(n)},t.checkForDuplicate=function(e){for(var n in t.constructors)t.constructors[+n].forEach(function(t){if(t===e)throw new d("Duplicate callback constructor.")})},t.clear=function(){t.constructors={}},t.createCallbacks=function(e){var n=[];for(var r in t.constructors){var i=+r;e>=i&&n.push.apply(n,t.constructors[i])}return n.map(function(t){return new t})},t.constructors={},t}();function Ce(t,e,n,r,i,o,a,s,u){var c=new ke,l=[new xe].concat(Ee.createCallbacks(e));null!=t&&l.push.apply(l,t),l.push(c);var f=new we(l);return f.setParams({epochs:n,initialEpoch:r,samples:i,steps:o,batchSize:a,verbose:e,doValidation:s,metrics:u}),{callbackList:f,history:c}}function Ne(t,e){return Object(r.tidy)(function(){"float32"!==t.dtype&&(t=t.asType("float32"));var n=Object(r.sum)(wt(t),e,!0),i=Object(r.fill)(n.shape,l()),o=Object(r.sqrt)(Object(r.maximum)(n,i));return Object(r.div)(t,o)})}function Ae(t,e){return Object(r.tidy)(function(){return Object(r.mean)(wt(Object(r.sub)(e,t)),-1)})}function Oe(t,e){return Object(r.tidy)(function(){return Object(r.mean)(Object(r.abs)(Object(r.sub)(e,t)),-1)})}function Ie(t,e){return Object(r.tidy)(function(){var n=Object(r.sub)(t,e),i=Object(r.clipByValue)(Object(r.abs)(t),l(),Number.MAX_VALUE),o=Object(r.abs)(Object(r.div)(n,i));return Object(r.mul)(100,Object(r.mean)(o,-1))})}function Te(t,e){return Object(r.tidy)(function(){var n=Object(r.clipByValue)(e,l(),Number.MAX_VALUE),i=Object(r.log)(Object(r.add)(1,n)),o=Object(r.clipByValue)(t,l(),Number.MAX_VALUE),a=Object(r.log)(Object(r.add)(1,o));return Object(r.mean)(wt(Object(r.sub)(i,a)),-1)})}function Me(t,e){return Object(r.tidy)(function(){var n=Object(r.maximum)(0,Object(r.sub)(1,Object(r.mul)(t,e)));return Object(r.mean)(wt(n),-1)})}function Re(t,e){return Object(r.tidy)(function(){var n=Object(r.maximum)(0,Object(r.sub)(1,Object(r.mul)(t,e)));return Object(r.mean)(n,-1)})}function De(t,e){return Object(r.tidy)(function(){var n=Object(r.sum)(Object(r.mul)(t,e),-1),i=Object(r.max)(Object(r.mul)(Object(r.sub)(1,t),e),-1);return Object(r.maximum)(0,Object(r.add)(1,Object(r.sub)(i,n)))})}function je(t,e){return Object(r.tidy)(function(){var n=Math.log(2),i=Object(r.sub)(e,t),o=Object(r.sub)(Object(r.add)(i,Object(r.softplus)(Object(r.mul)(-2,i))),n);return Object(r.mean)(o,-1)})}function Pe(t,e,n){return void 0===n&&(n=!1),Object(r.tidy)(function(){if(n)e=Object(r.softmax)(e);else{var i=Object(r.sum)(e,e.shape.length-1,!0);e=Object(r.div)(e,i)}return e=Object(r.clipByValue)(e,l(),1-l()),Object(r.neg)(Object(r.sum)(Object(r.mul)(t.toFloat(),Object(r.log)(e)),e.shape.length-1))})}function ze(t,e){return Object(r.tidy)(function(){var n=Object(r.floor)(function(t){var e=[it(t.shape)];return t.reshape(e)}(t)).toInt(),i=(e=Object(r.clipByValue)(e,l(),1-l())).shape;return Pe(Object(r.oneHot)(n,i[i.length-1]).reshape(i),e,!1)})}function Le(t,e){return Object(r.tidy)(function(){var n;return n=Object(r.clipByValue)(e,l(),1-l()),n=Object(r.log)(Object(r.div)(n,Object(r.sub)(1,n))),Object(r.mean)(function(t,e){if(!r.util.arraysEqual(t.shape,e.shape))throw new d("logits and labels must have the same shape, but got shapes "+JSON.stringify(t.shape)+" and "+JSON.stringify(e.shape));return Object(r.tidy)(function(){var n=e.relu(),r=e.abs().neg();return n.sub(e.mul(t)).add(r.exp().log1p())})}(t,n),-1)})}function Fe(t,e){return Object(r.tidy)(function(){var n=Object(r.clipByValue)(t,l(),1),i=Object(r.clipByValue)(e,l(),1);return Object(r.sum)(Object(r.mul)(t,Object(r.log)(Object(r.div)(n,i))),-1)})}function Be(t,e){return Object(r.tidy)(function(){var n=Object(r.log)(Object(r.add)(l(),e));return Object(r.mean)(Object(r.sub)(e,Object(r.mul)(t,n)),-1)})}function Ue(t,e){return Object(r.tidy)(function(){var n=Ne(t,-1),i=Ne(e,-1),o=Object(r.mul)(n,i);return Object(r.neg)(Object(r.sum)(o,-1))})}function Ve(t){var e={meanSquaredError:Ae,meanAbsoluteError:Oe,meanAbsolutePercentageError:Ie,meanSquaredLogarithmicError:Te,squaredHinge:Me,hinge:Re,categoricalHinge:De,logcosh:je,categoricalCrossentropy:Pe,sparseCategoricalCrossentropy:ze,binaryCrossentropy:Le,kullbackLeiblerDivergence:Fe,poisson:Be,cosineProximity:Ue};if("string"==typeof t){if(t in e)return e[t];var n="Unknown loss "+t;throw t.toLowerCase().includes("softmaxcrossentropy")&&(n="Unknown loss "+t+'. Use "categoricalCrossentropy" as the string name for tf.losses.softmaxCrossEntropy'),new d(n)}return t}function We(t,e){return Object(r.tidy)(function(){var n=Object(r.mul)(.5,Object(r.onesLike)(e)),i=ct(Object(r.greater)(e,n),t.dtype);return Object(r.mean)(Object(r.equal)(t,i),-1)})}function qe(t,e){return Object(r.tidy)(function(){return ct(Object(r.equal)(Object(r.argMax)(t,-1),Object(r.argMax)(e,-1)),"float32")})}function He(t,e){return Object(r.tidy)(function(){return Object(r.logicalAnd)(t.equal(1),e.equal(1)).sum().cast("float32")})}function Ge(t,e){return Object(r.tidy)(function(){var n=He(t,e),i=function(t,e){return Object(r.tidy)(function(){return Object(r.logicalAnd)(t.equal(0),e.equal(1)).sum().cast("float32")})}(t,e),o=n.add(i);return Object(r.where)(Object(r.greater)(o,0),n.div(o),0).cast("float32")})}function Ke(t,e){return Le(t,e)}function Xe(t,e){return t.rank===e.rank&&(t=t.squeeze([t.rank-1])),(e=e.argMax(-1)).dtype!==t.dtype&&(e=e.asType(t.dtype)),Object(r.equal)(t,e).asType("float32")}var $e=Ae,Ye=Ae,Je=Oe,Ze=Oe,Qe=Ie,tn=Ie,en=Pe,nn=Ue,rn=ze;function on(t,e,n){void 0===n&&(n=console.log);for(var r="",i=0;i0&&(r=r.slice(0,r.length-1)+" "),r=(r+=t[i]).slice(0,e[i]),r+=" ".repeat(e[i]-r.length);n(r)}function an(t,e,n){var r;try{r=JSON.stringify(t.outputShape)}catch(t){r="multiple"}on([t.name+" ("+t.getClassName()+")",r,t.countParams().toString()],e,n)}function sn(t,e,n,r){var i;try{i=JSON.stringify(t.outputShape)}catch(t){i="multiple"}for(var o=[],a=0,s=t.inboundNodes;a0&&-1===n.indexOf(u)))for(var c=0;c0,function(){return"Expected at least one fetch, got none"});var n=[],i={};if(1===t.length){var o=mn(t[0],e);n=o.sorted,i=o.recipientMap}else for(var a=new Set,s=0,u=t;si.maxNumTensors&&(i.maxNumTensors=x),x0;){var l=u[u.length-1];if(n.has(l.name))u.pop();else{var f=c[c.length-1]===u.length-1;if(0===l.inputs.length||f)u.pop(),r.push(l),n.add(l.name),f&&c.pop();else{c.push(u.length-1);for(var h=0,d=l.inputs;h0&&Number.isInteger(t),function(){return"batchSize is required to be a positive integer, but got "+t})}function kn(t,e,n){return null==t?[null]:Array.isArray(t)?t.map(function(t){return ft(t,e,n-e)}):ft(t,e,n-e)}function Sn(t,e){return Object(r.tidy)(function(){return null==t?null:Array.isArray(t)?t.map(function(t){return Sn(t,e)}):bt(t,"int32"===e.dtype?e:e.toInt())})}function _n(t,e){for(var n=[],r=0,i=null;r=t&&(i=t),n.push([r,i]),r=i;return n}function En(t,e,n,i){return void 0===i&&(i={}),u(this,void 0,void 0,function(){var o,a,s,l,f,h,m,v,g,y,b,w,x,k,S,_,E,C,N;return c(this,function(A){switch(A.label){case 0:if(t.isTraining)throw new Error("Cannot start training because another fit() call is ongoing.");t.isTraining=!0,A.label=1;case 1:if(A.trys.push([1,,3,4]),xn(m=null==i.batchSize?32:i.batchSize),v=t.standardizeUserData(e,n,!1,m),o=v[0],a=v[1],g=!1,y=void 0,null!=i.validationData&&i.validationData.length>0){if(g=!0,2!==i.validationData.length)throw 3===i.validationData.length?new p("validationData including sample weights is not supported yet."):new d("When passing validation data, it must contain 2 (valX, valY) or 3 (valX, valY, valSampleWeight) items; "+i.validationData+" is invalid.");s=i.validationData[0],l=i.validationData[1],b=t.standardizeUserData(s,l,!0,m),f=b[0],h=b[1],y=f.concat(h)}else null!=i.validationSplit&&i.validationSplit>0&&i.validationSplit<1?(g=!0,w=Math.floor(o[0].shape[0]*(1-i.validationSplit)),x=o[0].shape[0],f=kn(o,w,x),o=kn(o,0,w),h=kn(a,w,x),a=kn(a,0,w),y=f.concat(h)):null!=i.validationSteps&&(g=!0);return k=o.concat(a),t.checkTrainableWeightsConsistency(),S=t.makeTrainFunction(),_=t.getDedupedMetricsNames(),E=void 0,C=void 0,g?(t.makeTestFunction(),E=t.testFunction,C=_.slice().concat(_.map(function(t){return"val_"+t}))):(E=null,y=[],C=_.slice()),N=_e(i.callbacks,i.yieldEvery),[4,function(t,e,n,i,o,a,s,l,f,h,d,m,v,g,y){return u(this,void 0,void 0,function(){var u,y,b,w,x,k,S,_;return c(this,function(E){switch(E.label){case 0:return null==o&&(o=32),null==a&&(a=1),null==d&&(d=!0),null==v&&(v=0),u=!1,null!=f&&null!=h&&(u=!0),null!=(y=t.checkNumSamples(n,o,g,"steps_per_epoch"))&&(b=ut(0,y)),null==s&&(s=1),w=Ce(l,s,a,v,y,g,o,u,m),x=w.callbackList,k=w.history,x.setModel(t),t.history=k,[4,x.onTrainBegin()];case 1:E.sent(),t.stopTraining_=!1,S=function(a){var s,l,m,v,g;return c(this,function(w){switch(w.label){case 0:return[4,x.onEpochBegin(a)];case 1:return w.sent(),s={},[3,2];case 2:if("batch"===d)throw new p("batch shuffling is not implemneted yet");d&&r.util.shuffle(b),l=Object(r.tensor1d)(b),m=_n(y,o),v=function(a){var d;return c(this,function(c){switch(c.label){case 0:return d={},[4,x.onBatchBegin(a,d)];case 1:return c.sent(),Object(r.tidy)(function(){var c=m[a][0],p=m[a][1],v=ft(l,c,p-c);d.batch=a,d.size=p-c;for(var g=Sn(n,v),y=e(g),b=0;b0)o=!0;else if(On(t)){for(var a in t)if(t.hasOwnProperty(a)){o=!0;break}}else o=!0;if(o)throw new d("Error when checking model "+i+" expected no data, but got "+t)}return[]}if(null==t)return e.map(function(t){return null});var s;if(On(t)){t=t,s=[];for(var u=0,c=e;u1)throw new d("The model "+i+" expects "+e.length+" Tensor(s), but only received one Tensor. Found: Tensor with shape "+t.shape);s=[t]}if(s=Cn(s),null!=n)for(var f=0;f=0&&m!==v)throw new d("Error when checking "+i+": expected "+e[f]+" to have shape ["+n[f]+"], but got array with shape ["+h.shape+"].")}}return s}function Tn(t,e,n,r,i){var o;if(void 0===r&&(r=!0),void 0===i&&(i=""),Array.isArray(t)){if(t.length!==e.length)throw new d("Error when checking model "+i+": the Array of Tensors that you are passing to your model is not the size the the model expected. Expected to see "+e.length+" Tensor(s), but instead got "+t.length+" Tensors(s).");o=t}else{if(e.length>1)throw new d("The model expects "+e.length+" "+i+" Tensors, but only received one Tensor. Found: array with shape "+JSON.stringify(t.shape)+".");o=[t]}if(null!=n)for(var a=0;a1||1===s.length&&s[0].inboundLayers.length>1){e=!1;break}r.push.apply(r,s)}if(e)for(var u=0,c=t.layers;u1&&(e.metricsTensors.push([n,t]),e.metricsNames.push(e.outputNames[t]+"_loss"))}});var v=function(t,e){if(null==t||Array.isArray(t)&&0===t.length)return e.map(function(t){return[]});if(Array.isArray(t))return e.map(function(e){return t});if(null!=t){for(var n=[],r=0,i=e;r1&&(n=e.outputNames[t]+"_"+n),e.metricsNames.push(n),e.metricsTensors.push([r,t])}(t,r,c)},s=0,u=v[t];s0)throw new p("Verbose mode is not implemented yet.");return r.util.assert(!i||n.batches>0&&Number.isInteger(n.batches),function(){return"Test loop expects `batches` to be a positive integer, but received "+JSON.stringify(n.batches)}),"function"==typeof e.next?(u=e,[3,3]):[3,1];case 1:return[4,e.iterator()];case 2:u=v.sent(),v.label=3;case 3:s=u,l=0,f=0,h=function(){var e;return c(this,function(u){switch(u.label){case 0:return[4,s.next()];case 1:return e=u.sent(),a=Object(r.tidy)(function(){if(e.value){var n=yn(t,e.value),i=Object(r.tidy)(function(){return o(n)});if(Object(r.dispose)(n),0===f)for(var s=0;s0&&Object(r.dispose)(n)};for(s=0;s0){var l=[];throw e.forEach(function(e,n){null==e&&l.push(t[n])}),new d("Cannot find SymbolicTensors for output name(s): "+JSON.stringify(l))}return e},e.prototype.predictLoop=function(t,e,n){var i=this;return void 0===e&&(e=32),void 0===n&&(n=!1),Object(r.tidy)(function(){var o=i.checkNumSamples(t);if(n)throw new p("Verbose predictLoop() is not implemented yet.");for(var a=_n(o,e),s=i.outputs.map(function(t){return[]}),u=function(e){Object(r.tidy)(function(){var n=a[e][0],r=a[e][1],o=kn(t,n,r),s=[];if(Array.isArray(o))for(var u=0;u1)throw new d("All input Tensors (x) should have the same number of samples. Got array shapes: "+JSON.stringify(t.map(function(t){return t.shape})));if(o.length>1)throw new d("All target Tensors (y) should have the same number of samples. Got array shapes: "+JSON.stringify(e.map(function(t){return t.shape})));if(i.length>0&&o.length>0&&!r.util.arraysEqual(i,o))throw new d("Input Tensors should have the same number of samples as target Tensors. Found "+i[0]+" input sample(s) and "+o[0]+" target sample(s).")}(t=In(t,this.feedInputNames,this.feedInputShapes,!1,"input"),e=In(e,this.feedOutputNames,o,!1,"target")),function(t,e,n){for(var r=[Ae,Le,Pe],i=0;i0&&t[0].shape[0]%i!=0)throw new d("In a stateful network, you should only pass inputs with a number of samples that is divisible by the batch size "+i+". Found: "+t[0].shape[0]+" sample(s).");return[t,e,null]},e.prototype.testLoop=function(t,e,n,i,o){var a=this;return void 0===i&&(i=0),Object(r.tidy)(function(){var s=a.checkNumSamples(e,n,o,"steps"),u=[];if(i>0)throw new p("Verbose mode is not implemented yet.");if(null!=o)throw new p("steps mode in testLoop() is not implemented yet");for(var c=_n(s,n),l=Object(r.tensor1d)(ut(0,s)),f=0;f1&&(i+="_"+y(t.slice(0,n),r)),e.push(i)}return e},e.prototype.makeTrainFunction=function(){var t=this;return function(e){var n=e.slice(0,t.inputs.length),i=e.slice(t.inputs.length,t.inputs.length+t.outputs.length),o=[],a=t.collectedTrainableWeights.map(function(t){return t.read()});return[t.optimizer_.minimize(function(){for(var e=[],a=0;a0&&Number.isInteger(n.epochs),function(){return"For fitDataset(), config.epochs is expected to be a positive integer, but got "+n.epochs}),r.util.assert(!i||n.batchesPerEpoch>0&&Number.isInteger(n.batchesPerEpoch),function(){return"For fitDataset(), config.batchesPerEpoch is expected to be a positive integer if specified, but got "+n.batchesPerEpoch}),r.util.assert(null==n.validationSplit,function(){return"`validationSplit` is not supported by `fitDataset()`. Use validationData instead."}),t.isTraining)throw new Error("Cannot start training because another fit() call is ongoing.");t.isTraining=!0,c.label=1;case 1:return c.trys.push([1,,22,23]),o=null!=n.validationData,a=void 0,s=void 0,o&&(wn(n.validationData)?r.util.assert(null==n.validationBatches||n.validationBatches>0&&Number.isInteger(n.validationBatches),function(){return"For fitDataset() with dataset-based validation, config.validationBatches is expected not to be provided, or to be a positive integer, but got "+n.validationBatches}):(u=function(t){if(3===t.length)throw new p("Validation with sample weights is not implemented yet.");return{xs:t[0],ys:t[1]}}(n.validationData),a=u.xs,s=u.ys)),l=t.makeTrainFunction(),f=t.getDedupedMetricsNames(),h=void 0,h=o?f.slice().concat(f.map(function(t){return"val_"+t})):f.slice(),d=_e(n.callbacks,n.yieldEvery),m=null==n.verbose?1:n.verbose,v=Ce(d,m,n.epochs,null,null,function(t,e){var n=null;return null!=e.batchesPerEpoch?n=e.batchesPerEpoch:Number.isFinite(t.size)&&(n=t.size),n}(e,n),null,o,h),g=v.callbackList,y=v.history,g.setModel(t),t.history=y,[4,g.onTrainBegin()];case 2:return c.sent(),t.stopTraining_=!1,b=null==n.initialEpoch?0:n.initialEpoch,[4,e.iterator()];case 3:x=c.sent(),c.label=4;case 4:return b=n.batchesPerEpoch:E.done)?o?(T=void 0,wn(n.validationData)?(M=w,[4,t.evaluateDataset(n.validationData,{batches:n.validationBatches})]):[3,13]):[3,15]:[3,16];case 12:return T=M.apply(void 0,[c.sent()]),[3,14];case 13:T=w(t.evaluate(a,s,{batchSize:null==n.validationBatchSize?gn:n.validationBatchSize,verbose:0})),c.label=14;case 14:for(R=0;R1)throw new d("Found more than one ("+n.length+") save handlers for URL '"+t+"'");t=n[0]}if(null==t.save)throw new d("LayersModel.save() cannot proceed because the IOHandler provided does not have the `save` attribute defined.");return[4,r.io.encodeWeights(this.getNamedWeights(e))];case 1:return i=u.sent(),o=!1,a=null,s=this.toJSON(a,o),[2,t.save({modelTopology:s,weightData:i.data,weightSpecs:i.specs,format:"layers-model",generatedBy:"TensorFlow.js tfjs-layers v1.1.2",convertedBy:null})]}})})},e.className="Model",e}(function(t){function e(n){var r=t.call(this,{})||this;if(r.containerNodes=new Set,r.name=n.name,null==r.name){var i=r.getClassName().toLowerCase();r.name=Jt(i)}if(r.supportsMasking=!1,r.trainable_=!0,r.updatable=!0,Array.isArray(n.inputs)?r.inputs=n.inputs.slice():r.inputs=[n.inputs],Array.isArray(n.outputs)?r.outputs=n.outputs.slice():r.outputs=[n.outputs],N(r.inputs).length!==r.inputs.length)throw new d("The list of inputs passed to the model is redundant. All inputs should only appear once. Found: "+r.inputs.map(function(t){return t.name}));N(r.outputs).length!==r.outputs.length&&console.warn("The list of outputs passed to the model is redundant. All outputs should only appear once. Found: "+r.outputs.map(function(t){return t.name})),r.inputLayers=[],r.inputLayersNodeIndices=[],r.inputLayersTensorIndices=[],r.outputLayers=[],r.outputLayersNodeIndices=[],r.outputLayersTensorIndices=[],r.layers=[];for(var o=0,a=r.outputs;o1 nodes"),g(0===c,"input layer has >1 tensors"),r.inputLayers.push(s),r.inputLayersNodeIndices.push(u),r.inputLayersTensorIndices.push(c);r.inputNames=[],r.outputNames=[],r.feedInputShapes=[],r.feedInputNames=[],r.feedOutputNames=[];for(var p=0;p=0;)i.splice(i.indexOf(u),1);S.push(u)}},E=[],A=[],O=0,I=r.outputs;Or?1:0});for(var K=0,X=G;K0)throw new d("Container instance unexpectedly contains _trainableWeights.The trainable weights of a Container are a union of the trainable weights of its consituent Layers. Its own _trainableWeights must remain an empty Array.");if(!this.trainable)return[];for(var t=[],e=0,n=this.layers;e0)throw new d(f.length+" of "+r+" weights are not set: "+f)}ae(c)},e.prototype.updatedConfig=function(){var t=this.getConfig(),e={};return e.className=this.getClassName(),e.config=t,e.kerasVersion="tfjs-layers 1.1.2",e.backend="TensorFlow.js",e},e.prototype.toJSON=function(t,e){void 0===e&&(e=!0);var n=function t(e,n){if(null===e||void 0===e)return null;if("string"==typeof e)return x(e);if("number"==typeof e||"boolean"==typeof e)return e;if(e instanceof Array){for(var r=[],i=e.length,o=0;o1)for(var s=0,u=a;s0){for(var d=[],p=0;p0&&t.apply(b(r),n)}function u(t){var n=t.name,o=un(t,null!=e.customObjects?e.customObjects:{});o.setFastWeightInitDuringBuild(r),i[n]=o,t.inboundNodes.forEach(function(t){if(!(t instanceof Array))throw new d("Corrupted configuration, expected array for nodeData: "+t);a(o,t)})}for(var c=e.name,l=e.layers,f=0,h=l;f0)&&(n=e.sourceLayer,r=e.nodeIndex),0===n.inboundNodes.length)return[e];var i=n.inboundNodes[r];if(0===i.inboundLayers.length)return i.inputTensors;for(var o=[],a=0;a1)throw new d("Found more than one ("+n.length+") load handlers for URL '"+t+"'");t=n[0]}return[2,function(t,e,n){return u(this,void 0,void 0,function(){var e,i,o,a,s,u;return c(this,function(c){switch(c.label){case 0:if(null==n&&(n={}),null==t.load)throw new d("Cannot proceed with model loading because the IOHandler provided does not have the `load` method implemented.");return[4,t.load()];case 1:if(e=c.sent(),null!=(i=e.modelTopology).model_config&&(i=i.model_config),o=null==n.strict||n.strict,a=null!=e.weightData&&null!=e.weightSpecs&&o,s=un(ln(i),void 0,a),null!=e.weightData){if(null==e.weightSpecs)throw new d("LayersModel artifacts contains weight data, but not weight specs. Therefore loading of weights cannot proceed.");u=r.io.decodeWeights(e.weightData,e.weightSpecs),s.loadWeights(u,o),Object(r.dispose)(u)}return[2,s]}})})}(t,0,e)]})})}(t,e)}function zn(t){return me(t)}function Ln(t,e){Ee.registerCallbackConstructor(t,e)}r.serialization.registerClass(Rn);var Fn=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.getConfig=function(){return{}},e}(r.serialization.Serializable),Bn=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t,e){return void 0===e&&(e=1),function(t,e){if(void 0===e&&(e=1),1!==e)throw new p("Support for alpha values other than 1 ("+e+") is not implemented yet.");return Object(r.elu)(t)}(t,e)},e.className="elu",e}(Fn);r.serialization.registerClass(Bn);var Un=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return Object(r.selu)(t)},e.className="selu",e}(Fn);r.serialization.registerClass(Un);var Vn=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return Object(r.relu)(t)},e.className="relu",e}(Fn);r.serialization.registerClass(Vn);var Wn=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return Object(r.tidy)(function(){return Object(r.minimum)(6,Object(r.relu)(t))})},e.className="relu6",e}(Fn);r.serialization.registerClass(Wn);var qn=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return t},e.className="linear",e}(Fn);r.serialization.registerClass(qn);var Hn=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return Object(r.sigmoid)(t)},e.className="sigmoid",e}(Fn);r.serialization.registerClass(Hn);var Gn=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return function(t){return Object(r.tidy)(function(){var e=Object(r.add)(.5,Object(r.mul)(.2,t));return Object(r.clipByValue)(e,0,1)})}(t)},e.className="hardSigmoid",e}(Fn);r.serialization.registerClass(Gn);var Kn=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return Object(r.softplus)(t)},e.className="softplus",e}(Fn);r.serialization.registerClass(Kn);var Xn=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return function(t){return Object(r.tidy)(function(){return Object(r.div)(t,Object(r.abs)(t).add(1))})}(t)},e.className="softsign",e}(Fn);r.serialization.registerClass(Xn);var $n=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t){return Object(r.tanh)(t)},e.className="tanh",e}(Fn);r.serialization.registerClass($n);var Yn=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.apply=function(t,e){return void 0===e&&(e=-1),Object(r.softmax)(t,e)},e.className="softmax",e}(Fn);function Jn(t){return t.getClassName()}function Zn(t,e){return void 0===e&&(e={}),E(t,r.serialization.SerializationMap.getMap().classNameMap,e,"activation")}function Qn(t){var e;return null==t?Zn(e={className:"linear",config:{}}):"string"==typeof t?((e={}).className=t,e.config={},Zn(e)):t instanceof Fn?t:Zn(t)}r.serialization.registerClass(Yn);var tr=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e}(r.serialization.Serializable),er=function(t){function e(e){var n=t.call(this)||this;return n.l1=null==e||null==e.l1?.01:e.l1,n.l2=null==e||null==e.l2?.01:e.l2,n.hasL1=0!==n.l1,n.hasL2=0!==n.l2,n}return o(e,t),e.prototype.apply=function(t){var e=this;return Object(r.tidy)(function(){var n=Object(r.zeros)([1]);return e.hasL1&&(n=Object(r.add)(n,Object(r.sum)(Object(r.mul)(e.l1,Object(r.abs)(t))))),e.hasL2&&(n=Object(r.add)(n,Object(r.sum)(Object(r.mul)(e.l2,wt(t))))),n.asScalar()})},e.prototype.getConfig=function(){return{l1:this.l1,l2:this.l2}},e.fromConfig=function(t,e){return new t({l1:e.l1,l2:e.l2})},e.className="L1L2",e}(tr);r.serialization.registerClass(er);var nr={l1l2:"L1L2"};function rr(t){return _(t)}function ir(t,e){return void 0===e&&(e={}),E(t,r.serialization.SerializationMap.getMap().classNameMap,e,"regularizer")}function or(t){return null==t?null:"string"==typeof t?ir({className:t in nr?nr[t]:t,config:{}}):t instanceof tr?t:ir(t)}var ar=function(t){function e(e){var n=t.call(this,null==e?{}:e)||this;return n.supportsMasking=!0,null!=e&&(n.maxValue=e.maxValue),n}return o(e,t),e.prototype.call=function(t,e){t=te(t);var n=Object(r.relu)(t);return null!=this.maxValue&&(n=Object(r.clipByValue)(n,0,this.maxValue)),n},e.prototype.computeOutputShape=function(t){return t},e.prototype.getConfig=function(){var e={maxValue:this.maxValue},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},e.className="ReLU",e}(de);r.serialization.registerClass(ar);var sr=function(t){function e(e){var n=t.call(this,null==e?{}:e)||this;return n.DEFAULT_ALPHA=.3,null==e&&(e={}),n.alpha=null==e.alpha?n.DEFAULT_ALPHA:e.alpha,n}return o(e,t),e.prototype.call=function(t,e){var n=te(t);return Object(r.leakyRelu)(n,this.alpha)},e.prototype.computeOutputShape=function(t){return t},e.prototype.getConfig=function(){var e={alpha:this.alpha},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},e.className="LeakyReLU",e}(de);r.serialization.registerClass(sr);var ur=function(t){function e(e){var n=t.call(this,null==e?{}:e)||this;if(n.DEFAULT_ALPHA_INITIALIZER="zeros",null==e&&(e={}),n.supportsMasking=!0,n.alphaInitializer=Gt(e.alphaInitializer||n.DEFAULT_ALPHA_INITIALIZER),n.alphaRegularizer=or(e.alphaRegularizer),n.alphaConstraint=U(e.alphaConstraint),null==e.sharedAxes)n.sharedAxes=null;else if(Array.isArray(e.sharedAxes))n.sharedAxes=e.sharedAxes;else{if("number"!=typeof e.sharedAxes)throw new d("Expected sharedAxes to be a number or an array of numbers, but got "+e.sharedAxes);n.sharedAxes=[e.sharedAxes]}return n}return o(e,t),e.prototype.build=function(t){var e=(t=ee(t)).slice(1);if(null!=this.sharedAxes)for(var n=0,r=this.sharedAxes;n 0 but got "+JSON.stringify(t.filters))},e}(vr),yr=function(t){function e(n){var r=t.call(this,2,n)||this;return e.verifyArgs(n),r}return o(e,t),e.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this);return delete e.rank,e},e.verifyArgs=function(t){if("number"!=typeof t.kernelSize&&!I(t.kernelSize,"number",1,2))throw new d("Conv2D expects config.kernelSize to be number or number[] with length 1 or 2, but received "+JSON.stringify(t.kernelSize)+".")},e.className="Conv2D",e}(gr);r.serialization.registerClass(yr);var br=function(t){function e(n){var r=t.call(this,3,n)||this;return e.verifyArgs(n),r}return o(e,t),e.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this);return delete e.rank,e},e.verifyArgs=function(t){if("number"!=typeof t.kernelSize&&(!Array.isArray(t.kernelSize)||1!==t.kernelSize.length&&3!==t.kernelSize.length))throw new d("Conv3D expects config.kernelSize to be number or [number, number, number], but received "+JSON.stringify(t.kernelSize)+".")},e.className="Conv3D",e}(gr);r.serialization.registerClass(br);var wr=function(t){function e(e){var n=t.call(this,e)||this;if(n.inputSpec=[new ue({ndim:4})],"same"!==n.padding&&"valid"!==n.padding)throw new d("Conv2DTranspose currently supports only padding modes 'same' and 'valid', but received padding mode "+n.padding);return n}return o(e,t),e.prototype.build=function(t){var e;if(4!==(t=ee(t)).length)throw new d("Input should have rank 4; Received input shape: "+JSON.stringify(t));var n="channelsFirst"===this.dataFormat?1:t.length-1;if(null==t[n])throw new d("The channel dimension of the inputs should be defined. Found `None`.");var r=t[n],i=this.kernelSize.concat([this.filters,r]);this.kernel=this.addWeight("kernel",i,"float32",this.kernelInitializer,this.kernelRegularizer,!0,this.kernelConstraint),this.useBias&&(this.bias=this.addWeight("bias",[this.filters],"float32",this.biasInitializer,this.biasRegularizer,!0,this.biasConstraint)),this.inputSpec=[new ue({ndim:4,axes:(e={},e[n]=r,e)})],this.built=!0},e.prototype.call=function(t,e){var n=this;return Object(r.tidy)(function(){var e=te(t);if(4!==e.shape.length)throw new d("Conv2DTranspose.call() expects input tensor to be rank-4, but received a tensor of rank-"+e.shape.length);var i,o,a=e.shape,s=a[0];"channelsFirst"===n.dataFormat?(i=2,o=3):(i=1,o=2);var u=a[i],c=a[o],l=n.kernelSize[0],f=n.kernelSize[1],h=n.strides[0],p=n.strides[1],m=[s,pr(u,h,l,n.padding),pr(c,p,f,n.padding),n.filters];"channelsLast"!==n.dataFormat&&(e=Object(r.transpose)(e,[0,2,3,1]));var v=Object(r.conv2dTranspose)(e,n.kernel.read(),m,n.strides,n.padding);return"channelsLast"!==n.dataFormat&&(v=Object(r.transpose)(v,[0,3,1,2])),null!=n.bias&&(v=kt(v,n.bias.read(),n.dataFormat)),null!=n.activation&&(v=n.activation.apply(v)),v})},e.prototype.computeOutputShape=function(t){var e,n,r,i=(t=ee(t)).slice();"channelsFirst"===this.dataFormat?(e=1,n=2,r=3):(e=3,n=1,r=2);var o=this.kernelSize[0],a=this.kernelSize[1],s=this.strides[0],u=this.strides[1];return i[e]=this.filters,i[n]=pr(i[n],s,o,this.padding),i[r]=pr(i[r],u,a,this.padding),i},e.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this);return delete e.dilationRate,e},e.className="Conv2DTranspose",e}(yr);r.serialization.registerClass(wr);var xr=function(t){function e(e){return t.call(this,2,e)||this}return o(e,t),e.className="SeparableConv2D",e}(function(t){function e(e,n){var r=t.call(this,e,n)||this;if(r.DEFAULT_DEPTHWISE_INITIALIZER="glorotUniform",r.DEFAULT_POINTWISE_INITIALIZER="glorotUniform",r.depthwiseKernel=null,r.pointwiseKernel=null,null==n.filters)throw new d("The `filters` configuration field is required by SeparableConv, but is unspecified.");if(null!=n.kernelInitializer||null!=n.kernelRegularizer||null!=n.kernelConstraint)throw new d("Fields kernelInitializer, kernelRegularizer and kernelConstraint are invalid for SeparableConv2D. Use depthwiseInitializer, depthwiseRegularizer, depthwiseConstraint, pointwiseInitializer, pointwiseRegularizer and pointwiseConstraint instead.");if(null!=n.padding&&"same"!==n.padding&&"valid"!==n.padding)throw new d("SeparableConv"+r.rank+"D supports only padding modes: 'same' and 'valid', but received "+JSON.stringify(n.padding));return r.depthMultiplier=null==n.depthMultiplier?1:n.depthMultiplier,r.depthwiseInitializer=Gt(n.depthwiseInitializer||r.DEFAULT_DEPTHWISE_INITIALIZER),r.depthwiseRegularizer=or(n.depthwiseRegularizer),r.depthwiseConstraint=U(n.depthwiseConstraint),r.pointwiseInitializer=Gt(n.depthwiseInitializer||r.DEFAULT_POINTWISE_INITIALIZER),r.pointwiseRegularizer=or(n.pointwiseRegularizer),r.pointwiseConstraint=U(n.pointwiseConstraint),r}return o(e,t),e.prototype.build=function(t){var e;if((t=ee(t)).length1)throw new d("Can not merge tensors with different batch sizes. Got tensors with shapes: "+JSON.stringify(t)+".");for(var i=null==t[0]?null:t[0].slice(1),o=1;o1){var y=ut(1,u).concat([0]);e.push(Object(r.transpose)(d,y)),l=!0}else e.push(d)}var b=n.mergeFunction(e),w=b.rank;if(l)if(null==w){var x=b.shape;v=[m=x[x.length-1]].concat(x.slice(0,x.length-1)),b=Object(r.transpose)(b.reshape([-1,m]),[1,0]).reshape(v)}else w>1&&(y=[w-1].concat(ut(0,w-1)),b=Object(r.transpose)(b,y));return b}return n.mergeFunction(t)})},e.prototype.computeOutputShape=function(t){var e;e=null==(t=t)[0]?null:t[0].slice(1);for(var n=1;n1)throw new d("A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got input shapes: "+JSON.stringify(t))}},e.prototype.mergeFunction=function(t){var e=this;return Object(r.tidy)(function(){return pt(t,e.axis)})},e.prototype.computeOutputShape=function(t){if(!Array.isArray(t)||!Array.isArray(t[0]))throw new d("A `Concatenate` layer should be called on a list of inputs.");for(var e=t,n=e[0].slice(),r=this.axis<0?n.length+this.axis:this.axis,i=0,o=e.slice(1);i3||n.length>3)throw new p("Dot layer does not support tensors of 4D or higher rank yet.");var i=this.interpretAxes(e,n);if(e[i[0]]!==n[i[1]])throw new d("Dimension incompatibility: "+e[i[0]]+" !== "+n[i[1]])},e.prototype.mergeFunction=function(t){if(2!==t.length)throw new d("A `Dot` layer must be called on exactly 2 inputs, but received "+t.length+" input(s).");var e,n=t[0],i=t[1];return e=Array.isArray(this.axes)?this.axes.map(function(e,n){return Vr(e,t[n].shape.length)}):[Vr(this.axes,n.shape.length),Vr(this.axes,i.shape.length)],this.normalize&&(n=Ne(n,e[0]),i=Ne(i,e[1])),function(t,e,n){if(t.shape.length>3||e.shape.length>3)throw new p("batchDot is not implemented for tensors of 4D or higher rank yet");if(r.util.assert(t.shape.length>=2,function(){return"batchDot requires the rank of x to be >= 2, but got "+t.shape.length}),r.util.assert(t.shape.length>=2,function(){return"batchDot requires the rank of y to be >= 2, but got "+e.shape.length}),"number"==typeof n&&(n=[n,n]),"complex64"===t.dtype||"complex64"===e.dtype)throw new p("batchDot is not implemented for complex64-type Tensors yet.");var i=t.shape.length,o=e.shape.length;null==n&&(n=[i-1,o-2]);var a=n;return Object(r.tidy)(function(){var n,r;if(i>o){n=i-o;for(var s=[],u=0;ui){for(n=o-i,s=[],u=0;u0){var f,h=[];for(u=f=i>o?i+o-3:i-1;u3||n.length>3)throw new p("Dot layer does not support tensors of 4D or higher rank yet.");var i=this.interpretAxes(e,n);e.splice(i[0],1),n.splice(i[1],1),n.splice(0,1);var o=e.concat(n);return 1===o.length&&o.push(1),o},e.prototype.computeMask=function(t,e){return null},e.prototype.getConfig=function(){var e={axes:this.axes,normalize:this.normalize},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},e.className="Dot",e}(jr);function qr(t,e,n,i,o,a){var s;if(void 0===a&&(a=.001),2===t.rank)s=Object(r.batchNorm2d)(t,e,n,i,o,a);else if(3===t.rank)s=Object(r.batchNorm3d)(t,e,n,i,o,a);else{if(4!==t.rank)throw new p("batchNormalization is not implemented for array of rank "+t.rank+" yet");s=Object(r.batchNorm4d)(t,e,n,i,o,a)}return s}r.serialization.registerClass(Wr);var Hr=function(t){function e(e){var n=this;return null==e&&(e={}),(n=t.call(this,e)||this).supportsMasking=!0,n.axis=null==e.axis?-1:e.axis,n.momentum=null==e.momentum?.99:e.momentum,n.epsilon=null==e.epsilon?.001:e.epsilon,n.center=null==e.center||e.center,n.scale=null==e.scale||e.scale,n.betaInitializer=Gt(e.betaInitializer||"zeros"),n.gammaInitializer=Gt(e.gammaInitializer||"ones"),n.movingMeanInitializer=Gt(e.movingMeanInitializer||"zeros"),n.movingVarianceInitializer=Gt(e.movingVarianceInitializer||"ones"),n.betaConstraint=U(e.betaConstraint),n.gammaConstraint=U(e.gammaConstraint),n.betaRegularizer=or(e.betaRegularizer),n.gammaRegularizer=or(e.gammaRegularizer),n}return o(e,t),e.prototype.build=function(t){var e;t=ee(t);var n=this.axis>=0?this.axis:this.axis+t.length,r=t[n];if(null==r)throw new d("Axis "+n+" of input tensor should have a defined dimension but the layer received an input with shape "+JSON.stringify(t)+".");this.inputSpec=[new ue({ndim:t.length,axes:(e={},e[n]=r,e)})];var i=[r];this.scale&&(this.gamma=this.addWeight("gamma",i,null,this.gammaInitializer,this.gammaRegularizer,!0,this.gammaConstraint)),this.center&&(this.beta=this.addWeight("beta",i,null,this.betaInitializer,this.betaRegularizer,!0,this.betaConstraint)),this.movingMean=this.addWeight("moving_mean",i,null,this.movingMeanInitializer,null,!1),this.movingVariance=this.addWeight("moving_variance",i,null,this.movingVarianceInitializer,null,!1),this.built=!0},e.prototype.call=function(t,e){var n=this;return Object(r.tidy)(function(){var i=null!=e.training&&e.training,o=te(t),a=o.shape,s=a.length,u=ut(0,s),c=n.axis>=0?n.axis:n.axis+s;u.splice(c,1);var l=v(1,s);l[c]=a[c];var f=u.slice();f.sort();var h=!r.util.arraysEqual(f,ut(0,s).slice(0,s-1));if(!i)return function(){if(h){var t=n.movingMean.read().reshape(l),e=n.movingVariance.read().reshape(l),r=n.center?n.beta.read().reshape(l):null,i=n.scale?n.gamma.read().reshape(l):null;return qr(o,t,e,r,i,n.epsilon)}return qr(o,n.movingMean.read(),n.movingVariance.read(),null==n.beta?null:n.beta.read(),null==n.gamma?null:n.gamma.read(),n.epsilon)}();var d=function(t,e,n,i,o){return void 0===o&&(o=.001),r.util.arraysEqual(i.slice().sort(),ut(0,t.rank-1))?function(t,e,n,i,o){return void 0===o&&(o=.001),Object(r.tidy)(function(){var a=Object(r.moments)(t,i),s=a.mean,u=a.variance;return[qr(t,s,u,n,e,o),s,u]})}(t,e,n,i,o):function(t,e,n,i,o){return void 0===o&&(o=.001),Object(r.tidy)(function(){for(var a=Object(r.moments)(t,i),s=a.mean,u=a.variance,c=[],l=0,f=ut(0,t.rank);l=0?t[2]+this.padding[0][0]+this.padding[0][1]:null,n=null!=t[3]&&t[3]>=0?t[3]+this.padding[1][0]+this.padding[1][1]:null,[t[0],t[1],e,n]):(e=null!=t[1]&&t[1]>=0?t[1]+this.padding[0][0]+this.padding[0][1]:null,n=null!=t[2]&&t[2]>=0?t[2]+this.padding[1][0]+this.padding[1][1]:null,[t[0],e,n,t[3]])},e.prototype.call=function(t,e){var n=this;return Object(r.tidy)(function(){return function(t,e,n){return Object(r.tidy)(function(){if(4!==t.rank)throw new d("temporalPadding expects input tensor to be 4-D, but received a "+t.rank+"-D tensor.");if(null==e&&(e=[[1,1],[1,1]]),2!==e.length||2!==e[0].length||2!==e[1].length)throw new d("spatial2dPadding expects `padding` to be an Array of two Arrays, each of which is an Array of two integers.");if(null==n&&(n="channelsLast"),"channelsLast"!==n&&"channelsFirst"!==n)throw new d("Unknown data format: "+n+". Supported data formats are 'channelsLast' and 'channelsFirst.");var i;return i="channelsFirst"===n?[[0,0],[0,0],e[0],e[1]]:[[0,0],e[0],e[1],[0,0]],Object(r.pad)(t,i)})}(te(t),n.padding,n.dataFormat)})},e.prototype.getConfig=function(){var e={padding:this.padding,dataFormat:this.dataFormat},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},e.className="ZeroPadding2D",e}(de);function Kr(t,e,n,i,o,a){return Object(r.tidy)(function(){var s;X(o),O(H,"PoolMode",a),$(i),null==n&&(n=[1,1]),null==i&&(i="valid"),null==o&&(o="channelsLast"),null==a&&(a="max"),t=mr(t,o);var u="same"===i?"same":"valid";return s="max"===a?Object(r.maxPool)(t,e,n,u):Object(r.avgPool)(t,e,n,u),"channelsFirst"===o&&(s=Object(r.transpose)(s,[0,3,1,2])),s})}r.serialization.registerClass(Gr);var Xr=function(t){function e(e){var n=this;if(null==e.poolSize&&(e.poolSize=2),n=t.call(this,e)||this,"number"==typeof e.poolSize)n.poolSize=[e.poolSize];else{if(!Array.isArray(e.poolSize)||1!==e.poolSize.length||"number"!=typeof e.poolSize[0])throw new d("poolSize for 1D convolutional layer must be a number or an Array of a single number, but received "+JSON.stringify(e.poolSize));n.poolSize=e.poolSize}if(T(n.poolSize,"poolSize"),null==e.strides)n.strides=n.poolSize;else if("number"==typeof e.strides)n.strides=[e.strides];else{if(!Array.isArray(e.strides)||1!==e.strides.length||"number"!=typeof e.strides[0])throw new d("strides for 1D convolutional layer must be a number or an Array of a single number, but received "+JSON.stringify(e.strides));n.strides=e.strides}return T(n.strides,"strides"),n.padding=null==e.padding?"valid":e.padding,$(n.padding),n.inputSpec=[new ue({ndim:3})],n}return o(e,t),e.prototype.computeOutputShape=function(t){var e=dr((t=ee(t))[1],this.poolSize[0],this.padding,this.strides[0]);return[t[0],e,t[2]]},e.prototype.call=function(t,e){var n=this;return Object(r.tidy)(function(){n.invokeCallHook(t,e),t=lt(te(t),2);var i=n.poolingFunction(te(t),[n.poolSize[0],1],[n.strides[0],1],n.padding,"channelsLast");return Object(r.squeeze)(i,[2])})},e.prototype.getConfig=function(){var e={poolSize:this.poolSize,padding:this.padding,strides:this.strides},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},e}(de),$r=function(t){function e(e){return t.call(this,e)||this}return o(e,t),e.prototype.poolingFunction=function(t,e,n,r,i){return X(i),$(r),Kr(t,e,n,r,i,"max")},e.className="MaxPooling1D",e}(Xr);r.serialization.registerClass($r);var Yr=function(t){function e(e){return t.call(this,e)||this}return o(e,t),e.prototype.poolingFunction=function(t,e,n,r,i){return X(i),$(r),Kr(t,e,n,r,i,"avg")},e.className="AveragePooling1D",e}(Xr);r.serialization.registerClass(Yr);var Jr=function(t){function e(e){var n=this;if(null==e.poolSize&&(e.poolSize=[2,2]),(n=t.call(this,e)||this).poolSize=Array.isArray(e.poolSize)?e.poolSize:[e.poolSize,e.poolSize],null==e.strides)n.strides=n.poolSize;else if(Array.isArray(e.strides)){if(2!==e.strides.length)throw new d("If the strides property of a 2D pooling layer is an Array, it is expected to have a length of 2, but received length "+e.strides.length+".");n.strides=e.strides}else n.strides=[e.strides,e.strides];return T(n.poolSize,"poolSize"),T(n.strides,"strides"),n.padding=null==e.padding?"valid":e.padding,n.dataFormat=null==e.dataFormat?"channelsLast":e.dataFormat,X(n.dataFormat),$(n.padding),n.inputSpec=[new ue({ndim:4})],n}return o(e,t),e.prototype.computeOutputShape=function(t){t=ee(t);var e="channelsFirst"===this.dataFormat?t[2]:t[1],n="channelsFirst"===this.dataFormat?t[3]:t[2];return e=dr(e,this.poolSize[0],this.padding,this.strides[0]),n=dr(n,this.poolSize[1],this.padding,this.strides[1]),"channelsFirst"===this.dataFormat?[t[0],t[1],e,n]:[t[0],e,n,t[3]]},e.prototype.call=function(t,e){var n=this;return Object(r.tidy)(function(){return n.invokeCallHook(t,e),n.poolingFunction(te(t),n.poolSize,n.strides,n.padding,n.dataFormat)})},e.prototype.getConfig=function(){var e={poolSize:this.poolSize,padding:this.padding,strides:this.strides,dataFormat:this.dataFormat},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},e}(de),Zr=function(t){function e(e){return t.call(this,e)||this}return o(e,t),e.prototype.poolingFunction=function(t,e,n,r,i){return X(i),$(r),Kr(t,e,n,r,i,"max")},e.className="MaxPooling2D",e}(Jr);r.serialization.registerClass(Zr);var Qr=function(t){function e(e){return t.call(this,e)||this}return o(e,t),e.prototype.poolingFunction=function(t,e,n,r,i){return X(i),$(r),Kr(t,e,n,r,i,"avg")},e.className="AveragePooling2D",e}(Jr);r.serialization.registerClass(Qr);var ti=function(t){function e(e){var n=t.call(this,e)||this;return n.inputSpec=[new ue({ndim:3})],n}return o(e,t),e.prototype.computeOutputShape=function(t){return[t[0],t[2]]},e.prototype.call=function(t,e){throw new p},e}(de),ei=function(t){function e(e){return t.call(this,e||{})||this}return o(e,t),e.prototype.call=function(t,e){return Object(r.tidy)(function(){var e=te(t);return Object(r.mean)(e,1)})},e.className="GlobalAveragePooling1D",e}(ti);r.serialization.registerClass(ei);var ni=function(t){function e(e){return t.call(this,e||{})||this}return o(e,t),e.prototype.call=function(t,e){return Object(r.tidy)(function(){var e=te(t);return Object(r.max)(e,1)})},e.className="GlobalMaxPooling1D",e}(ti);r.serialization.registerClass(ni);var ri=function(t){function e(e){var n=t.call(this,e)||this;return n.dataFormat=null==e.dataFormat?"channelsLast":e.dataFormat,X(n.dataFormat),n.inputSpec=[new ue({ndim:4})],n}return o(e,t),e.prototype.computeOutputShape=function(t){return t=t,"channelsLast"===this.dataFormat?[t[0],t[3]]:[t[0],t[1]]},e.prototype.call=function(t,e){throw new p},e.prototype.getConfig=function(){var e={dataFormat:this.dataFormat},n=t.prototype.getConfig.call(this);return Object.assign(e,n),e},e}(de),ii=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.call=function(t,e){var n=this;return Object(r.tidy)(function(){var e=te(t);return"channelsLast"===n.dataFormat?Object(r.mean)(e,[1,2]):Object(r.mean)(e,[2,3])})},e.className="GlobalAveragePooling2D",e}(ri);r.serialization.registerClass(ii);var oi=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.call=function(t,e){var n=this;return Object(r.tidy)(function(){var e=te(t);return"channelsLast"===n.dataFormat?Object(r.max)(e,[1,2]):Object(r.max)(e,[2,3])})},e.className="GlobalMaxPooling2D",e}(ri);function ai(t,e,n,r){if(Array.isArray(t)){if(null!=e||null!=n)throw new d("When inputs is an array, neither initialState or constants should be provided");null!=r&&(n=t.slice(t.length-r,t.length),t=t.slice(0,t.length-r)),t.length>1&&(e=t.slice(1,t.length)),t=t[0]}function i(t){return null==t||Array.isArray(t)?t:[t]}return{inputs:t,initialState:e=i(e),constants:n=i(n)}}function si(t,e,n,i,o,a,s,u){return void 0===i&&(i=!1),void 0===s&&(s=!1),void 0===u&&(u=!1),Object(r.tidy)(function(){var c=e.shape.length;if(c<3)throw new d("Input should be at least 3D, but is "+c+"D.");var l=[1,0].concat(ut(2,c));if(e=Object(r.transpose)(e,l),null!=a)throw new p("The rnn() functoin of the deeplearn.js backend does not support constants yet.");s&&console.warn("Backend rnn(): the unroll = true option is not applicable to the imperative deeplearn.js backend."),null!=o&&((o=o.asType("bool").asType("float32")).rank===c-1&&(o=Object(r.expandDims)(o,-1)),o=Object(r.transpose)(o,l)),i&&(e=Object(r.reverse)(e,0),null!=o&&(o=Object(r.reverse)(o,0)));var f,h,m=[],v=n,g=e.shape[0],y=Object(r.unstack)(e);null!=o&&(h=Object(r.unstack)(o));for(var b,w=function(e){var n=y[e],i=Object(r.tidy)(function(){return t(n,v)});if(null==o)f=i[0],v=i[1];else{var a=Object(r.tidy)(function(){var t=h[e],n=Object(r.onesLike)(t).sub(t);return{output:i[0].mul(t).addStrict(v[0].mul(n)),newStates:v.map(function(e,r){return i[1][r].mul(t).addStrict(e.mul(n))})}});f=a.output,v=a.newStates}u&&m.push(f)},x=0;x1?vt(n,[1,t]):n}):e.cell.stateSize>1?[vt(n,[1,e.cell.stateSize])]:[n]})},Object.defineProperty(e.prototype,"trainableWeights",{get:function(){return this.trainable?this.cell.trainableWeights:[]},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"nonTrainableWeights",{get:function(){return this.trainable?this.cell.nonTrainableWeights:this.cell.weights},enumerable:!0,configurable:!0}),e.prototype.setFastWeightInitDuringBuild=function(e){t.prototype.setFastWeightInitDuringBuild.call(this,e),null!=this.cell&&this.cell.setFastWeightInitDuringBuild(e)},e.prototype.getConfig=function(){var e={returnSequences:this.returnSequences,returnState:this.returnState,goBackwards:this.goBackwards,stateful:this.stateful,unroll:this.unroll};null!=this.numConstants&&(e.numConstants=this.numConstants);var n=this.cell.getConfig();e.cell={className:this.cell.getClassName(),config:n};var r=t.prototype.getConfig.call(this);return Object.assign(e,r),e},e.className="RNN",e}(de);r.serialization.registerClass(ui);var ci=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e}(de),li=function(t){function e(e){var n=t.call(this,e)||this;return n.DEFAULT_ACTIVATION="tanh",n.DEFAULT_KERNEL_INITIALIZER="glorotNormal",n.DEFAULT_RECURRENT_INITIALIZER="orthogonal",n.DEFAULT_BIAS_INITIALIZER="zeros",n.units=e.units,T(n.units,"units"),n.activation=Qn(null==e.activation?n.DEFAULT_ACTIVATION:e.activation),n.useBias=null==e.useBias||e.useBias,n.kernelInitializer=Gt(e.kernelInitializer||n.DEFAULT_KERNEL_INITIALIZER),n.recurrentInitializer=Gt(e.recurrentInitializer||n.DEFAULT_RECURRENT_INITIALIZER),n.biasInitializer=Gt(e.biasInitializer||n.DEFAULT_BIAS_INITIALIZER),n.kernelRegularizer=or(e.kernelRegularizer),n.recurrentRegularizer=or(e.recurrentRegularizer),n.biasRegularizer=or(e.biasRegularizer),n.kernelConstraint=U(e.kernelConstraint),n.recurrentConstraint=U(e.recurrentConstraint),n.biasConstraint=U(e.biasConstraint),n.dropout=at([1,st([0,null==e.dropout?0:e.dropout])]),n.recurrentDropout=at([1,st([0,null==e.recurrentDropout?0:e.recurrentDropout])]),n.stateSize=n.units,n.dropoutMask=null,n.recurrentDropoutMask=null,n}return o(e,t),e.prototype.build=function(t){t=ee(t),this.kernel=this.addWeight("kernel",[t[t.length-1],this.units],null,this.kernelInitializer,this.kernelRegularizer,!0,this.kernelConstraint),this.recurrentKernel=this.addWeight("recurrent_kernel",[this.units,this.units],null,this.recurrentInitializer,this.recurrentRegularizer,!0,this.recurrentConstraint),this.useBias?this.bias=this.addWeight("bias",[this.units],null,this.biasInitializer,this.biasRegularizer,!0,this.biasConstraint):this.bias=null,this.built=!0},e.prototype.call=function(t,e){var n=this;return Object(r.tidy)(function(){if(2!==(t=t).length)throw new d("SimpleRNNCell expects 2 input Tensors, got "+t.length+".");var i=t[1];t=t[0];var o,a=null!=e.training&&e.training;01){for(var a=[],s=0;s= 3D, but received input shape "+JSON.stringify(e));this.inputSpec=[{shape:e}];var n=[e[0]].concat(e.slice(2));this.layer.built||(this.layer.build(n),this.layer.built=!0),t.prototype.build.call(this,e)},e.prototype.computeOutputShape=function(t){var e=[(t=ee(t))[0]].concat(t.slice(2)),n=this.layer.computeOutputShape(e),r=t[1];return[n[0],r].concat(n.slice(1))},e.prototype.call=function(t,e){var n=this;return Object(r.tidy)(function(){return si(function(t,r){return[te(n.layer.call(t,e)),[]]},t=te(t),[],!1,null,null,!1,!0)[1]})},e.className="TimeDistributed",e}(yi);r.serialization.registerClass(bi);var wi="concat",xi=function(t){function e(e){var n=t.call(this,e)||this,r=e.layer.getConfig(),i={};i.className=e.layer.getClassName(),i.config=r,n.forwardLayer=un(i),r.goBackwards=!0!==r.goBackwards;var o={};if(o.className=e.layer.getClassName(),o.config=r,n.backwardLayer=un(o),n.forwardLayer.name="forward_"+n.forwardLayer.name,n.backwardLayer.name="backward_"+n.backwardLayer.name,n.mergeMode=void 0===e.mergeMode?wi:e.mergeMode,function(t){O(G,"BidirectionalMergeMode",t)}(n.mergeMode),e.weights)throw new p("weights support is not implemented for Bidirectional layer yet.");return n._stateful=e.layer.stateful,n.returnSequences=e.layer.returnSequences,n.returnState=e.layer.returnState,n.supportsMasking=!0,n._trainable=!0,n.inputSpec=e.layer.inputSpec,n.numConstants=null,n}return o(e,t),Object.defineProperty(e.prototype,"trainable",{get:function(){return this._trainable},set:function(t){this._trainable=t,null!=this.forwardLayer&&(this.forwardLayer.trainable=t),null!=this.backwardLayer&&(this.backwardLayer.trainable=t)},enumerable:!0,configurable:!0}),e.prototype.getWeights=function(){return this.forwardLayer.getWeights().concat(this.backwardLayer.getWeights())},e.prototype.setWeights=function(t){var e=t.length,n=Math.floor(e/2);this.forwardLayer.setWeights(t.slice(0,n)),this.backwardLayer.setWeights(t.slice(n))},e.prototype.computeOutputShape=function(t){var e,n,r,i=this.forwardLayer.computeOutputShape(t);return Array.isArray(i)&&Array.isArray(i[0])||(i=[i]),i=i,this.returnState?(r=i.slice(1),e=i[0]):e=i[0],e=e,"concat"===this.mergeMode?(e[e.length-1]*=2,n=[e]):n=null==this.mergeMode?[e,e.slice()]:[e],this.returnState?null==this.mergeMode?n.concat(r).concat(r.slice()):[e].concat(r).concat(r.slice()):b(n)},e.prototype.apply=function(e,n){var r=null==n?null:n.initialState,i=null==n?null:n.constants;null==n&&(n={});var o=ai(e,r,i,this.numConstants);if(e=o.inputs,r=o.initialState,i=o.constants,Array.isArray(e)&&(r=e.slice(1),e=e[0]),(null==r||0===r.length)&&null==i)return t.prototype.apply.call(this,e,n);var a=[],s=[];if(null!=r){var u=r.length;if(u%2>0)throw new d("When passing `initialState` to a Bidrectional RNN, the state should be an Array containing the states of the underlying RNNs.");n.initialState=r,a.push.apply(a,r);var c=r.map(function(t){return new ue({shape:t.shape})});this.forwardLayer.stateSpec=c.slice(0,u/2),this.backwardLayer.stateSpec=c.slice(u/2),s.push.apply(s,c)}if(null!=i)throw new p("Support for constants in Bidirectional layers is not implemented yet.");for(var l=a[0]instanceof ce,f=0,h=a;f0&&n.rate<1?_t(function(){var t=Math.sqrt(n.rate/(1-n.rate));return yt(r,gt(r.shape,1,t))},function(){return r},e.training||!1):r})},e.className="GaussianDropout",e}(de);r.serialization.registerClass(Si);var _i=function(t){function e(e){var n=t.call(this,e)||this;return n.supportsMasking=!0,n.rate=e.rate,n.noiseShape=e.noiseShape,n}return o(e,t),e.prototype._getNoiseShape=function(t){return this.noiseShape||te(t).shape},e.prototype.computeOutputShape=function(t){return t},e.prototype.getConfig=function(){var e=t.prototype.getConfig.call(this),n={rate:this.rate};return Object.assign(n,e),n},e.prototype.call=function(t,e){var n=this;return Object(r.tidy)(function(){if(n.rate<1&&n.rate>0){var i=n._getNoiseShape(t);return _t(function(){var e=te(t),o=-1.7580993408473766,a=Object(r.greaterEqual)(Object(r.randomUniform)(i),n.rate);a=ct(a,"float32");var s=Math.pow((1-n.rate)*(1+n.rate*Math.pow(o,2)),-.5),u=-s*o*n.rate;return yt(e,a).add(a.add(-1).mul(o)).mul(s).add(u)},function(){return te(t)},e.training||!1)}return t})},e.className="AlphaDropout",e}(de);function Ei(t){return new Yr(t)}function Ci(t){return new Qr(t)}function Ni(t){return new ni(t)}function Ai(t){return new oi(t)}function Oi(t){return new $r(t)}function Ii(t){return new Zr(t)}r.serialization.registerClass(_i);var Ti=Ni,Mi=Ai,Ri=Oi,Di=Ii,ji=Object.freeze({inputLayer:function(t){return new pe(t)},elu:function(t){return new cr(t)},reLU:function(t){return new ar(t)},leakyReLU:function(t){return new sr(t)},prelu:function(t){return new ur(t)},softmax:function(t){return new fr(t)},thresholdedReLU:function(t){return new lr(t)},conv1d:function(t){return new kr(t)},conv2d:function(t){return new yr(t)},conv2dTranspose:function(t){return new wr(t)},conv3d:function(t){return new br(t)},separableConv2d:function(t){return new xr(t)},cropping2D:function(t){return new Sr(t)},upSampling2d:function(t){return new _r(t)},depthwiseConv2d:function(t){return new Er(t)},activation:function(t){return new Or(t)},dense:function(t){return new Nr(t)},dropout:function(t){return new Cr(t)},flatten:function(t){return new Ar(t)},repeatVector:function(t){return new Ir(t)},reshape:function(t){return new Tr(t)},permute:function(t){return new Mr(t)},embedding:function(t){return new Dr(t)},add:function(t){return new Pr(t)},average:function(t){return new Lr(t)},concatenate:function(t){return new Ur(t)},maximum:function(t){return new Fr(t)},minimum:function(t){return new Br(t)},multiply:function(t){return new zr(t)},dot:function(t){return new Wr(t)},batchNormalization:function(t){return new Hr(t)},zeroPadding2d:function(t){return new Gr(t)},averagePooling1d:Ei,avgPool1d:function(t){return Ei(t)},avgPooling1d:function(t){return Ei(t)},averagePooling2d:Ci,avgPool2d:function(t){return Ci(t)},avgPooling2d:function(t){return Ci(t)},globalAveragePooling1d:function(t){return new ei(t)},globalAveragePooling2d:function(t){return new ii(t)},globalMaxPooling1d:Ni,globalMaxPooling2d:Ai,maxPooling1d:Oi,maxPooling2d:Ii,gru:function(t){return new di(t)},gruCell:function(t){return new hi(t)},lstm:function(t){return new mi(t)},lstmCell:function(t){return new pi(t)},simpleRNN:function(t){return new fi(t)},simpleRNNCell:function(t){return new li(t)},rnn:function(t){return new ui(t)},stackedRNNCells:function(t){return new vi(t)},bidirectional:function(t){return new xi(t)},timeDistributed:function(t){return new bi(t)},globalMaxPool1d:Ti,globalMaxPool2d:Mi,maxPool1d:Ri,maxPool2d:Di,Layer:de,RNN:ui,RNNCell:ci,input:zn,gaussianNoise:function(t){return new ki(t)},gaussianDropout:function(t){return new Si(t)},alphaDropout:function(t){return new _i(t)},masking:function(t){return new Rr(t)}}),Pi=Object.freeze({binaryAccuracy:function(t,e){return We(t,e)},binaryCrossentropy:function(t,e){return Ke(t,e)},sparseCategoricalAccuracy:function(t,e){return Xe(t,e)},categoricalAccuracy:function(t,e){return qe(t,e)},categoricalCrossentropy:function(t,e){return en(t,e)},precision:function(t,e){return Ge(t,e)},recall:function(t,e){return function(t,e){return Object(r.tidy)(function(){var n=He(t,e),i=function(t,e){return Object(r.tidy)(function(){return Object(r.logicalAnd)(t.equal(1),e.equal(0)).sum().cast("float32")})}(t,e),o=n.add(i);return Object(r.where)(Object(r.greater)(o,0),n.div(o),0).cast("float32")})}(t,e)},cosineProximity:function(t,e){return Ue(t,e)},meanAbsoluteError:function(t,e){return Oe(t,e)},meanAbsolutePercentageError:function(t,e){return Ie(t,e)},MAPE:function(t,e){return Ie(t,e)},mape:function(t,e){return Ie(t,e)},meanSquaredError:function(t,e){return Ae(t,e)},MSE:function(t,e){return Ae(t,e)},mse:function(t,e){return Ae(t,e)}}),zi=Object.freeze({modelFromJSON:function(t,e){return u(this,void 0,void 0,function(){var n,i,o,a,s,u,l,f;return c(this,function(c){switch(c.label){case 0:return"modelTopology"in t||(t={modelTopology:t}),null!=(n=(t=t).modelTopology).model_config&&(n=n.model_config),i=ln(n),o=un(i,e),null==t.weightsManifest?[3,2]:[4,r.io.loadWeights(t.weightsManifest,t.pathPrefix,o.weights.map(function(t){return t.originalName}))];case 1:for(a=c.sent(),s={},u=0,l=o.weights;ue}var Vi=function(t){function e(e){var n=t.call(this)||this;if(null==e&&(e={}),e.restoreBestWeights)throw new p("restoreBestWeights = True is not implemented in EarlyStopping yet.");return n.monitor=e.monitor||"val_loss",n.minDelta=Math.abs(e.minDelta||0),n.patience=e.patience||0,n.verbose=e.verbose||0,n.mode=e.mode||"auto",n.baseline=e.baseline,-1===["auto","min","max"].indexOf(n.mode)&&(console.warn("EarlyStopping mode '"+n.mode+"' is invalid. Falling back to mode 'auto'."),n.mode="auto"),"min"===n.mode?n.monitorFunc=Bi:"max"===n.mode?n.monitorFunc=Ui:-1!==n.monitor.indexOf("acc")?n.monitorFunc=Ui:n.monitorFunc=Bi,n.monitorFunc===Bi&&(n.minDelta*=-1),n}return o(e,t),e.prototype.onTrainBegin=function(t){return u(this,void 0,void 0,function(){return c(this,function(t){return this.wait=0,this.stoppedEpoch=0,null!=this.baseline?this.best=this.baseline:this.best=this.monitorFunc===Bi?1/0:-1/0,[2]})})},e.prototype.onEpochEnd=function(t,e){return u(this,void 0,void 0,function(){var n;return c(this,function(r){switch(r.label){case 0:return[4,ve(e)];case 1:return r.sent(),null==(n=this.getMonitorValue(e))?[2]:(this.monitorFunc(n-this.minDelta,this.best)?(this.best=n,this.wait=0):(this.wait++,this.wait>=this.patience&&(this.stoppedEpoch=t,this.model.stopTraining=!0)),[2])}})})},e.prototype.onTrainEnd=function(t){return u(this,void 0,void 0,function(){return c(this,function(t){return this.stoppedEpoch>0&&this.verbose&&console.log("Epoch "+this.stoppedEpoch+": early stopping."),[2]})})},e.prototype.getMonitorValue=function(t){null==t&&(t={});var e=t[this.monitor];return null==e&&console.warn("Metric for EarlyStopping "+this.monitor+" is not available. Available metrics are: "+Object.keys(t)),e},e}(Fi),Wi={earlyStopping:function(t){return new Vi(t)}},qi=n(138),Hi=n(137);n.d(e,"version",function(){return Gi}),n.d(e,"AdadeltaOptimizer",function(){return r.AdadeltaOptimizer}),n.d(e,"AdagradOptimizer",function(){return r.AdagradOptimizer}),n.d(e,"AdamOptimizer",function(){return r.AdamOptimizer}),n.d(e,"AdamaxOptimizer",function(){return r.AdamaxOptimizer}),n.d(e,"DataStorage",function(){return r.DataStorage}),n.d(e,"ENV",function(){return r.ENV}),n.d(e,"Environment",function(){return r.Environment}),n.d(e,"KernelBackend",function(){return r.KernelBackend}),n.d(e,"MomentumOptimizer",function(){return r.MomentumOptimizer}),n.d(e,"Optimizer",function(){return r.Optimizer}),n.d(e,"RMSPropOptimizer",function(){return r.RMSPropOptimizer}),n.d(e,"Rank",function(){return r.Rank}),n.d(e,"Reduction",function(){return r.Reduction}),n.d(e,"SGDOptimizer",function(){return r.SGDOptimizer}),n.d(e,"Tensor",function(){return r.Tensor}),n.d(e,"TensorBuffer",function(){return r.TensorBuffer}),n.d(e,"Variable",function(){return r.Variable}),n.d(e,"abs",function(){return r.abs}),n.d(e,"acos",function(){return r.acos}),n.d(e,"acosh",function(){return r.acosh}),n.d(e,"add",function(){return r.add}),n.d(e,"addN",function(){return r.addN}),n.d(e,"addStrict",function(){return r.addStrict}),n.d(e,"all",function(){return r.all}),n.d(e,"any",function(){return r.any}),n.d(e,"argMax",function(){return r.argMax}),n.d(e,"argMin",function(){return r.argMin}),n.d(e,"asin",function(){return r.asin}),n.d(e,"asinh",function(){return r.asinh}),n.d(e,"atan",function(){return r.atan}),n.d(e,"atan2",function(){return r.atan2}),n.d(e,"atanh",function(){return r.atanh}),n.d(e,"avgPool",function(){return r.avgPool}),n.d(e,"backend",function(){return r.backend}),n.d(e,"basicLSTMCell",function(){return r.basicLSTMCell}),n.d(e,"batchNorm",function(){return r.batchNorm}),n.d(e,"batchNorm2d",function(){return r.batchNorm2d}),n.d(e,"batchNorm3d",function(){return r.batchNorm3d}),n.d(e,"batchNorm4d",function(){return r.batchNorm4d}),n.d(e,"batchNormalization",function(){return r.batchNormalization}),n.d(e,"batchNormalization2d",function(){return r.batchNormalization2d}),n.d(e,"batchNormalization3d",function(){return r.batchNormalization3d}),n.d(e,"batchNormalization4d",function(){return r.batchNormalization4d}),n.d(e,"batchToSpaceND",function(){return r.batchToSpaceND}),n.d(e,"browser",function(){return r.browser}),n.d(e,"buffer",function(){return r.buffer}),n.d(e,"cast",function(){return r.cast}),n.d(e,"ceil",function(){return r.ceil}),n.d(e,"clipByValue",function(){return r.clipByValue}),n.d(e,"clone",function(){return r.clone}),n.d(e,"complex",function(){return r.complex}),n.d(e,"concat",function(){return r.concat}),n.d(e,"concat1d",function(){return r.concat1d}),n.d(e,"concat2d",function(){return r.concat2d}),n.d(e,"concat3d",function(){return r.concat3d}),n.d(e,"concat4d",function(){return r.concat4d}),n.d(e,"conv1d",function(){return r.conv1d}),n.d(e,"conv2d",function(){return r.conv2d}),n.d(e,"conv2dDerFilter",function(){return r.conv2dDerFilter}),n.d(e,"conv2dTranspose",function(){return r.conv2dTranspose}),n.d(e,"conv3d",function(){return r.conv3d}),n.d(e,"cos",function(){return r.cos}),n.d(e,"cosh",function(){return r.cosh}),n.d(e,"cumsum",function(){return r.cumsum}),n.d(e,"customGrad",function(){return r.customGrad}),n.d(e,"deprecationWarn",function(){return r.deprecationWarn}),n.d(e,"depthToSpace",function(){return r.depthToSpace}),n.d(e,"depthwiseConv2d",function(){return r.depthwiseConv2d}),n.d(e,"disableDeprecationWarnings",function(){return r.disableDeprecationWarnings}),n.d(e,"dispose",function(){return r.dispose}),n.d(e,"disposeVariables",function(){return r.disposeVariables}),n.d(e,"div",function(){return r.div}),n.d(e,"divStrict",function(){return r.divStrict}),n.d(e,"dot",function(){return r.dot}),n.d(e,"dropout",function(){return r.dropout}),n.d(e,"elu",function(){return r.elu}),n.d(e,"enableDebugMode",function(){return r.enableDebugMode}),n.d(e,"enableProdMode",function(){return r.enableProdMode}),n.d(e,"environment",function(){return r.environment}),n.d(e,"equal",function(){return r.equal}),n.d(e,"equalStrict",function(){return r.equalStrict}),n.d(e,"erf",function(){return r.erf}),n.d(e,"exp",function(){return r.exp}),n.d(e,"expandDims",function(){return r.expandDims}),n.d(e,"expm1",function(){return r.expm1}),n.d(e,"eye",function(){return r.eye}),n.d(e,"fft",function(){return r.fft}),n.d(e,"fill",function(){return r.fill}),n.d(e,"findBackend",function(){return r.findBackend}),n.d(e,"findBackendFactory",function(){return r.findBackendFactory}),n.d(e,"floor",function(){return r.floor}),n.d(e,"floorDiv",function(){return r.floorDiv}),n.d(e,"fused",function(){return r.fused}),n.d(e,"gather",function(){return r.gather}),n.d(e,"gatherND",function(){return r.gatherND}),n.d(e,"getBackend",function(){return r.getBackend}),n.d(e,"grad",function(){return r.grad}),n.d(e,"grads",function(){return r.grads}),n.d(e,"greater",function(){return r.greater}),n.d(e,"greaterEqual",function(){return r.greaterEqual}),n.d(e,"greaterEqualStrict",function(){return r.greaterEqualStrict}),n.d(e,"greaterStrict",function(){return r.greaterStrict}),n.d(e,"hammingWindow",function(){return r.hammingWindow}),n.d(e,"hannWindow",function(){return r.hannWindow}),n.d(e,"ifft",function(){return r.ifft}),n.d(e,"imag",function(){return r.imag}),n.d(e,"image",function(){return r.image}),n.d(e,"io",function(){return r.io}),n.d(e,"irfft",function(){return r.irfft}),n.d(e,"isFinite",function(){return r.isFinite}),n.d(e,"isInf",function(){return r.isInf}),n.d(e,"isNaN",function(){return r.isNaN}),n.d(e,"keep",function(){return r.keep}),n.d(e,"leakyRelu",function(){return r.leakyRelu}),n.d(e,"less",function(){return r.less}),n.d(e,"lessEqual",function(){return r.lessEqual}),n.d(e,"lessEqualStrict",function(){return r.lessEqualStrict}),n.d(e,"lessStrict",function(){return r.lessStrict}),n.d(e,"linalg",function(){return r.linalg}),n.d(e,"linspace",function(){return r.linspace}),n.d(e,"localResponseNormalization",function(){return r.localResponseNormalization}),n.d(e,"log",function(){return r.log}),n.d(e,"log1p",function(){return r.log1p}),n.d(e,"logSigmoid",function(){return r.logSigmoid}),n.d(e,"logSoftmax",function(){return r.logSoftmax}),n.d(e,"logSumExp",function(){return r.logSumExp}),n.d(e,"logicalAnd",function(){return r.logicalAnd}),n.d(e,"logicalNot",function(){return r.logicalNot}),n.d(e,"logicalOr",function(){return r.logicalOr}),n.d(e,"logicalXor",function(){return r.logicalXor}),n.d(e,"losses",function(){return r.losses}),n.d(e,"matMul",function(){return r.matMul}),n.d(e,"math",function(){return r.math}),n.d(e,"max",function(){return r.max}),n.d(e,"maxPool",function(){return r.maxPool}),n.d(e,"maximum",function(){return r.maximum}),n.d(e,"maximumStrict",function(){return r.maximumStrict}),n.d(e,"mean",function(){return r.mean}),n.d(e,"memory",function(){return r.memory}),n.d(e,"min",function(){return r.min}),n.d(e,"minimum",function(){return r.minimum}),n.d(e,"minimumStrict",function(){return r.minimumStrict}),n.d(e,"mod",function(){return r.mod}),n.d(e,"modStrict",function(){return r.modStrict}),n.d(e,"moments",function(){return r.moments}),n.d(e,"movingAverage",function(){return r.movingAverage}),n.d(e,"mul",function(){return r.mul}),n.d(e,"mulStrict",function(){return r.mulStrict}),n.d(e,"multiRNNCell",function(){return r.multiRNNCell}),n.d(e,"multinomial",function(){return r.multinomial}),n.d(e,"neg",function(){return r.neg}),n.d(e,"nextFrame",function(){return r.nextFrame}),n.d(e,"norm",function(){return r.norm}),n.d(e,"notEqual",function(){return r.notEqual}),n.d(e,"notEqualStrict",function(){return r.notEqualStrict}),n.d(e,"oneHot",function(){return r.oneHot}),n.d(e,"ones",function(){return r.ones}),n.d(e,"onesLike",function(){return r.onesLike}),n.d(e,"op",function(){return r.op}),n.d(e,"outerProduct",function(){return r.outerProduct}),n.d(e,"pad",function(){return r.pad}),n.d(e,"pad1d",function(){return r.pad1d}),n.d(e,"pad2d",function(){return r.pad2d}),n.d(e,"pad3d",function(){return r.pad3d}),n.d(e,"pad4d",function(){return r.pad4d}),n.d(e,"pool",function(){return r.pool}),n.d(e,"pow",function(){return r.pow}),n.d(e,"powStrict",function(){return r.powStrict}),n.d(e,"prelu",function(){return r.prelu}),n.d(e,"print",function(){return r.print}),n.d(e,"prod",function(){return r.prod}),n.d(e,"profile",function(){return r.profile}),n.d(e,"rand",function(){return r.rand}),n.d(e,"randomNormal",function(){return r.randomNormal}),n.d(e,"randomUniform",function(){return r.randomUniform}),n.d(e,"range",function(){return r.range}),n.d(e,"ready",function(){return r.ready}),n.d(e,"real",function(){return r.real}),n.d(e,"reciprocal",function(){return r.reciprocal}),n.d(e,"registerBackend",function(){return r.registerBackend}),n.d(e,"relu",function(){return r.relu}),n.d(e,"removeBackend",function(){return r.removeBackend}),n.d(e,"reshape",function(){return r.reshape}),n.d(e,"reverse",function(){return r.reverse}),n.d(e,"reverse1d",function(){return r.reverse1d}),n.d(e,"reverse2d",function(){return r.reverse2d}),n.d(e,"reverse3d",function(){return r.reverse3d}),n.d(e,"reverse4d",function(){return r.reverse4d}),n.d(e,"rfft",function(){return r.rfft}),n.d(e,"round",function(){return r.round}),n.d(e,"rsqrt",function(){return r.rsqrt}),n.d(e,"scalar",function(){return r.scalar}),n.d(e,"scatterND",function(){return r.scatterND}),n.d(e,"selu",function(){return r.selu}),n.d(e,"separableConv2d",function(){return r.separableConv2d}),n.d(e,"serialization",function(){return r.serialization}),n.d(e,"setBackend",function(){return r.setBackend}),n.d(e,"setdiff1dAsync",function(){return r.setdiff1dAsync}),n.d(e,"sigmoid",function(){return r.sigmoid}),n.d(e,"sign",function(){return r.sign}),n.d(e,"sin",function(){return r.sin}),n.d(e,"sinh",function(){return r.sinh}),n.d(e,"slice",function(){return r.slice}),n.d(e,"slice1d",function(){return r.slice1d}),n.d(e,"slice2d",function(){return r.slice2d}),n.d(e,"slice3d",function(){return r.slice3d}),n.d(e,"slice4d",function(){return r.slice4d}),n.d(e,"softmax",function(){return r.softmax}),n.d(e,"softplus",function(){return r.softplus}),n.d(e,"spaceToBatchND",function(){return r.spaceToBatchND}),n.d(e,"sparseToDense",function(){return r.sparseToDense}),n.d(e,"spectral",function(){return r.spectral}),n.d(e,"split",function(){return r.split}),n.d(e,"sqrt",function(){return r.sqrt}),n.d(e,"square",function(){return r.square}),n.d(e,"squaredDifference",function(){return r.squaredDifference}),n.d(e,"squaredDifferenceStrict",function(){return r.squaredDifferenceStrict}),n.d(e,"squeeze",function(){return r.squeeze}),n.d(e,"stack",function(){return r.stack}),n.d(e,"step",function(){return r.step}),n.d(e,"stridedSlice",function(){return r.stridedSlice}),n.d(e,"sub",function(){return r.sub}),n.d(e,"subStrict",function(){return r.subStrict}),n.d(e,"sum",function(){return r.sum}),n.d(e,"tan",function(){return r.tan}),n.d(e,"tanh",function(){return r.tanh}),n.d(e,"tensor",function(){return r.tensor}),n.d(e,"tensor1d",function(){return r.tensor1d}),n.d(e,"tensor2d",function(){return r.tensor2d}),n.d(e,"tensor3d",function(){return r.tensor3d}),n.d(e,"tensor4d",function(){return r.tensor4d}),n.d(e,"tensor5d",function(){return r.tensor5d}),n.d(e,"tensor6d",function(){return r.tensor6d}),n.d(e,"tensor_util",function(){return r.tensor_util}),n.d(e,"test_util",function(){return r.test_util}),n.d(e,"tidy",function(){return r.tidy}),n.d(e,"tile",function(){return r.tile}),n.d(e,"time",function(){return r.time}),n.d(e,"topk",function(){return r.topk}),n.d(e,"train",function(){return r.train}),n.d(e,"transpose",function(){return r.transpose}),n.d(e,"truncatedNormal",function(){return r.truncatedNormal}),n.d(e,"unsortedSegmentSum",function(){return r.unsortedSegmentSum}),n.d(e,"unstack",function(){return r.unstack}),n.d(e,"util",function(){return r.util}),n.d(e,"valueAndGrad",function(){return r.valueAndGrad}),n.d(e,"valueAndGrads",function(){return r.valueAndGrads}),n.d(e,"variable",function(){return r.variable}),n.d(e,"variableGrads",function(){return r.variableGrads}),n.d(e,"version_core",function(){return r.version_core}),n.d(e,"webgl",function(){return r.webgl}),n.d(e,"where",function(){return r.where}),n.d(e,"whereAsync",function(){return r.whereAsync}),n.d(e,"zeros",function(){return r.zeros}),n.d(e,"zerosLike",function(){return r.zerosLike}),n.d(e,"constraints",function(){return V}),n.d(e,"initializers",function(){return Kt}),n.d(e,"layers",function(){return ji}),n.d(e,"metrics",function(){return Pi}),n.d(e,"models",function(){return zi}),n.d(e,"regularizers",function(){return Li}),n.d(e,"CallbackList",function(){return we}),n.d(e,"CustomCallback",function(){return Se}),n.d(e,"History",function(){return ke}),n.d(e,"Callback",function(){return Fi}),n.d(e,"callbacks",function(){return Wi}),n.d(e,"EarlyStopping",function(){return Vi}),n.d(e,"InputSpec",function(){return ue}),n.d(e,"SymbolicTensor",function(){return ce}),n.d(e,"LayersModel",function(){return Mn}),n.d(e,"input",function(){return zn}),n.d(e,"loadLayersModel",function(){return Pn}),n.d(e,"model",function(){return Dn}),n.d(e,"registerCallbackConstructor",function(){return Ln}),n.d(e,"sequential",function(){return jn}),n.d(e,"RNN",function(){return ui}),n.d(e,"Sequential",function(){return Rn}),n.d(e,"LayerVariable",function(){return ie}),n.d(e,"version_layers",function(){return"1.1.2"}),n.d(e,"GraphModel",function(){return qi.a}),n.d(e,"loadGraphModel",function(){return qi.b}),n.d(e,"version_converter",function(){return qi.c}),n.d(e,"data",function(){return Hi});var Gi={"tfjs-core":r.version_core,"tfjs-data":Hi.version_data,"tfjs-layers":"1.1.2","tfjs-converter":qi.c,tfjs:"1.1.2"}},function(t,e,n){var r=n(8);t.exports=function(t){if(!r(t))throw TypeError(t+" is not an object!");return t}},function(t,e){"function"==typeof Object.create?t.exports=function(t,e){t.super_=e,t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}})}:t.exports=function(t,e){t.super_=e;var n=function(){};n.prototype=e.prototype,t.prototype=new n,t.prototype.constructor=t}},function(t,e,n){var r=n(14),i=r.Buffer;function o(t,e){for(var n in t)e[n]=t[n]}function a(t,e,n){return i(t,e,n)}i.from&&i.alloc&&i.allocUnsafe&&i.allocUnsafeSlow?t.exports=r:(o(r,e),e.Buffer=a),o(i,a),a.from=function(t,e,n){if("number"==typeof t)throw new TypeError("Argument must not be a number");return i(t,e,n)},a.alloc=function(t,e,n){if("number"!=typeof t)throw new TypeError("Argument must be a number");var r=i(t);return void 0!==e?"string"==typeof n?r.fill(e,n):r.fill(e):r.fill(0),r},a.allocUnsafe=function(t){if("number"!=typeof t)throw new TypeError("Argument must be a number");return i(t)},a.allocUnsafeSlow=function(t){if("number"!=typeof t)throw new TypeError("Argument must be a number");return r.SlowBuffer(t)}},function(t,e){var n=t.exports="undefined"!=typeof window&&window.Math==Math?window:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")();"number"==typeof __g&&(__g=n)},function(t,e){t.exports=function(t){try{return!!t()}catch(t){return!0}}},function(t,e){t.exports=function(t){return"object"==typeof t?null!==t:"function"==typeof t}},function(t,e,n){var r=n(136)("wks"),i=n(73),o=n(6).Symbol,a="function"==typeof o;(t.exports=function(t){return r[t]||(r[t]=a&&o[t]||(a?o:i)("Symbol."+t))}).store=r},function(t,e,n){var r=n(40),i=Math.min;t.exports=function(t){return t>0?i(r(t),9007199254740991):0}},function(t,e){var n=t.exports={version:"2.6.1"};"number"==typeof __e&&(__e=n)},function(t,e,n){"use strict";e.__esModule=!0;var r=function(t){return t&&t.__esModule?t:{default:t}}(n(52));e.default=function(t){return function(){var e=t.apply(this,arguments);return new r.default(function(t,n){return function i(o,a){try{var s=e[o](a),u=s.value}catch(t){return void n(t)}if(!s.done)return r.default.resolve(u).then(function(t){i("next",t)},function(t){i("throw",t)});t(u)}("next")})}}},function(t,e,n){t.exports=n(391)},function(t,e,n){"use strict";(function(t){ /*! * The buffer module from node.js, for the browser. * * @author Feross Aboukhadijeh * @license MIT */ -var r=n(472),a=n(471),i=n(254);function o(){return u.TYPED_ARRAY_SUPPORT?2147483647:1073741823}function s(e,t){if(o()=o())throw new RangeError("Attempt to allocate Buffer larger than maximum size: 0x"+o().toString(16)+" bytes");return 0|e}function d(e,t){if(u.isBuffer(e))return e.length;if("undefined"!=typeof ArrayBuffer&&"function"==typeof ArrayBuffer.isView&&(ArrayBuffer.isView(e)||e instanceof ArrayBuffer))return e.byteLength;"string"!=typeof e&&(e=""+e);var n=e.length;if(0===n)return 0;for(var r=!1;;)switch(t){case"ascii":case"latin1":case"binary":return n;case"utf8":case"utf-8":case void 0:return F(e).length;case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return 2*n;case"hex":return n>>>1;case"base64":return V(e).length;default:if(r)return F(e).length;t=(""+t).toLowerCase(),r=!0}}function m(e,t,n){var r=e[t];e[t]=e[n],e[n]=r}function g(e,t,n,r,a){if(0===e.length)return-1;if("string"==typeof n?(r=n,n=0):n>2147483647?n=2147483647:n<-2147483648&&(n=-2147483648),n=+n,isNaN(n)&&(n=a?0:e.length-1),n<0&&(n=e.length+n),n>=e.length){if(a)return-1;n=e.length-1}else if(n<0){if(!a)return-1;n=0}if("string"==typeof t&&(t=u.from(t,r)),u.isBuffer(t))return 0===t.length?-1:y(e,t,n,r,a);if("number"==typeof t)return t&=255,u.TYPED_ARRAY_SUPPORT&&"function"==typeof Uint8Array.prototype.indexOf?a?Uint8Array.prototype.indexOf.call(e,t,n):Uint8Array.prototype.lastIndexOf.call(e,t,n):y(e,[t],n,r,a);throw new TypeError("val must be string, number or Buffer")}function y(e,t,n,r,a){var i,o=1,s=e.length,u=t.length;if(void 0!==r&&("ucs2"===(r=String(r).toLowerCase())||"ucs-2"===r||"utf16le"===r||"utf-16le"===r)){if(e.length<2||t.length<2)return-1;o=2,s/=2,u/=2,n/=2}function l(e,t){return 1===o?e[t]:e.readUInt16BE(t*o)}if(a){var c=-1;for(i=n;is&&(n=s-u),i=n;i>=0;i--){for(var f=!0,p=0;pa&&(r=a):r=a;var i=t.length;if(i%2!=0)throw new TypeError("Invalid hex string");r>i/2&&(r=i/2);for(var o=0;o>8,a=n%256,i.push(a),i.push(r);return i}(t,e.length-n),e,n,r)}function E(e,t,n){return 0===t&&n===e.length?r.fromByteArray(e):r.fromByteArray(e.slice(t,n))}function N(e,t,n){n=Math.min(e.length,n);for(var r=[],a=t;a239?4:l>223?3:l>191?2:1;if(a+f<=n)switch(f){case 1:l<128&&(c=l);break;case 2:128==(192&(i=e[a+1]))&&(u=(31&l)<<6|63&i)>127&&(c=u);break;case 3:i=e[a+1],o=e[a+2],128==(192&i)&&128==(192&o)&&(u=(15&l)<<12|(63&i)<<6|63&o)>2047&&(u<55296||u>57343)&&(c=u);break;case 4:i=e[a+1],o=e[a+2],s=e[a+3],128==(192&i)&&128==(192&o)&&128==(192&s)&&(u=(15&l)<<18|(63&i)<<12|(63&o)<<6|63&s)>65535&&u<1114112&&(c=u)}null===c?(c=65533,f=1):c>65535&&(c-=65536,r.push(c>>>10&1023|55296),c=56320|1023&c),r.push(c),a+=f}return function(e){var t=e.length;if(t<=O)return String.fromCharCode.apply(String,e);for(var n="",r=0;rthis.length)return"";if((void 0===n||n>this.length)&&(n=this.length),n<=0)return"";if((n>>>=0)<=(t>>>=0))return"";for(e||(e="utf8");;)switch(e){case"hex":return C(this,t,n);case"utf8":case"utf-8":return N(this,t,n);case"ascii":return k(this,t,n);case"latin1":case"binary":return I(this,t,n);case"base64":return E(this,t,n);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return A(this,t,n);default:if(r)throw new TypeError("Unknown encoding: "+e);e=(e+"").toLowerCase(),r=!0}}.apply(this,arguments)},u.prototype.equals=function(e){if(!u.isBuffer(e))throw new TypeError("Argument must be a Buffer");return this===e||0===u.compare(this,e)},u.prototype.inspect=function(){var e="",n=t.INSPECT_MAX_BYTES;return this.length>0&&(e=this.toString("hex",0,n).match(/.{2}/g).join(" "),this.length>n&&(e+=" ... ")),""},u.prototype.compare=function(e,t,n,r,a){if(!u.isBuffer(e))throw new TypeError("Argument must be a Buffer");if(void 0===t&&(t=0),void 0===n&&(n=e?e.length:0),void 0===r&&(r=0),void 0===a&&(a=this.length),t<0||n>e.length||r<0||a>this.length)throw new RangeError("out of range index");if(r>=a&&t>=n)return 0;if(r>=a)return-1;if(t>=n)return 1;if(t>>>=0,n>>>=0,r>>>=0,a>>>=0,this===e)return 0;for(var i=a-r,o=n-t,s=Math.min(i,o),l=this.slice(r,a),c=e.slice(t,n),f=0;fa)&&(n=a),e.length>0&&(n<0||t<0)||t>this.length)throw new RangeError("Attempt to write outside buffer bounds");r||(r="utf8");for(var i=!1;;)switch(r){case"hex":return v(this,e,t,n);case"utf8":case"utf-8":return b(this,e,t,n);case"ascii":return w(this,e,t,n);case"latin1":case"binary":return x(this,e,t,n);case"base64":return _(this,e,t,n);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return S(this,e,t,n);default:if(i)throw new TypeError("Unknown encoding: "+r);r=(""+r).toLowerCase(),i=!0}},u.prototype.toJSON=function(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};var O=4096;function k(e,t,n){var r="";n=Math.min(e.length,n);for(var a=t;ar)&&(n=r);for(var a="",i=t;in)throw new RangeError("Trying to access beyond buffer length")}function T(e,t,n,r,a,i){if(!u.isBuffer(e))throw new TypeError('"buffer" argument must be a Buffer instance');if(t>a||te.length)throw new RangeError("Index out of range")}function M(e,t,n,r){t<0&&(t=65535+t+1);for(var a=0,i=Math.min(e.length-n,2);a>>8*(r?a:1-a)}function R(e,t,n,r){t<0&&(t=4294967295+t+1);for(var a=0,i=Math.min(e.length-n,4);a>>8*(r?a:3-a)&255}function D(e,t,n,r,a,i){if(n+r>e.length)throw new RangeError("Index out of range");if(n<0)throw new RangeError("Index out of range")}function j(e,t,n,r,i){return i||D(e,0,n,4),a.write(e,t,n,r,23,4),n+4}function L(e,t,n,r,i){return i||D(e,0,n,8),a.write(e,t,n,r,52,8),n+8}u.prototype.slice=function(e,t){var n,r=this.length;if(e=~~e,t=void 0===t?r:~~t,e<0?(e+=r)<0&&(e=0):e>r&&(e=r),t<0?(t+=r)<0&&(t=0):t>r&&(t=r),t0&&(a*=256);)r+=this[e+--t]*a;return r},u.prototype.readUInt8=function(e,t){return t||P(e,1,this.length),this[e]},u.prototype.readUInt16LE=function(e,t){return t||P(e,2,this.length),this[e]|this[e+1]<<8},u.prototype.readUInt16BE=function(e,t){return t||P(e,2,this.length),this[e]<<8|this[e+1]},u.prototype.readUInt32LE=function(e,t){return t||P(e,4,this.length),(this[e]|this[e+1]<<8|this[e+2]<<16)+16777216*this[e+3]},u.prototype.readUInt32BE=function(e,t){return t||P(e,4,this.length),16777216*this[e]+(this[e+1]<<16|this[e+2]<<8|this[e+3])},u.prototype.readIntLE=function(e,t,n){e|=0,t|=0,n||P(e,t,this.length);for(var r=this[e],a=1,i=0;++i=(a*=128)&&(r-=Math.pow(2,8*t)),r},u.prototype.readIntBE=function(e,t,n){e|=0,t|=0,n||P(e,t,this.length);for(var r=t,a=1,i=this[e+--r];r>0&&(a*=256);)i+=this[e+--r]*a;return i>=(a*=128)&&(i-=Math.pow(2,8*t)),i},u.prototype.readInt8=function(e,t){return t||P(e,1,this.length),128&this[e]?-1*(255-this[e]+1):this[e]},u.prototype.readInt16LE=function(e,t){t||P(e,2,this.length);var n=this[e]|this[e+1]<<8;return 32768&n?4294901760|n:n},u.prototype.readInt16BE=function(e,t){t||P(e,2,this.length);var n=this[e+1]|this[e]<<8;return 32768&n?4294901760|n:n},u.prototype.readInt32LE=function(e,t){return t||P(e,4,this.length),this[e]|this[e+1]<<8|this[e+2]<<16|this[e+3]<<24},u.prototype.readInt32BE=function(e,t){return t||P(e,4,this.length),this[e]<<24|this[e+1]<<16|this[e+2]<<8|this[e+3]},u.prototype.readFloatLE=function(e,t){return t||P(e,4,this.length),a.read(this,e,!0,23,4)},u.prototype.readFloatBE=function(e,t){return t||P(e,4,this.length),a.read(this,e,!1,23,4)},u.prototype.readDoubleLE=function(e,t){return t||P(e,8,this.length),a.read(this,e,!0,52,8)},u.prototype.readDoubleBE=function(e,t){return t||P(e,8,this.length),a.read(this,e,!1,52,8)},u.prototype.writeUIntLE=function(e,t,n,r){e=+e,t|=0,n|=0,r||T(this,e,t,n,Math.pow(2,8*n)-1,0);var a=1,i=0;for(this[t]=255&e;++i=0&&(i*=256);)this[t+a]=e/i&255;return t+n},u.prototype.writeUInt8=function(e,t,n){return e=+e,t|=0,n||T(this,e,t,1,255,0),u.TYPED_ARRAY_SUPPORT||(e=Math.floor(e)),this[t]=255&e,t+1},u.prototype.writeUInt16LE=function(e,t,n){return e=+e,t|=0,n||T(this,e,t,2,65535,0),u.TYPED_ARRAY_SUPPORT?(this[t]=255&e,this[t+1]=e>>>8):M(this,e,t,!0),t+2},u.prototype.writeUInt16BE=function(e,t,n){return e=+e,t|=0,n||T(this,e,t,2,65535,0),u.TYPED_ARRAY_SUPPORT?(this[t]=e>>>8,this[t+1]=255&e):M(this,e,t,!1),t+2},u.prototype.writeUInt32LE=function(e,t,n){return e=+e,t|=0,n||T(this,e,t,4,4294967295,0),u.TYPED_ARRAY_SUPPORT?(this[t+3]=e>>>24,this[t+2]=e>>>16,this[t+1]=e>>>8,this[t]=255&e):R(this,e,t,!0),t+4},u.prototype.writeUInt32BE=function(e,t,n){return e=+e,t|=0,n||T(this,e,t,4,4294967295,0),u.TYPED_ARRAY_SUPPORT?(this[t]=e>>>24,this[t+1]=e>>>16,this[t+2]=e>>>8,this[t+3]=255&e):R(this,e,t,!1),t+4},u.prototype.writeIntLE=function(e,t,n,r){if(e=+e,t|=0,!r){var a=Math.pow(2,8*n-1);T(this,e,t,n,a-1,-a)}var i=0,o=1,s=0;for(this[t]=255&e;++i>0)-s&255;return t+n},u.prototype.writeIntBE=function(e,t,n,r){if(e=+e,t|=0,!r){var a=Math.pow(2,8*n-1);T(this,e,t,n,a-1,-a)}var i=n-1,o=1,s=0;for(this[t+i]=255&e;--i>=0&&(o*=256);)e<0&&0===s&&0!==this[t+i+1]&&(s=1),this[t+i]=(e/o>>0)-s&255;return t+n},u.prototype.writeInt8=function(e,t,n){return e=+e,t|=0,n||T(this,e,t,1,127,-128),u.TYPED_ARRAY_SUPPORT||(e=Math.floor(e)),e<0&&(e=255+e+1),this[t]=255&e,t+1},u.prototype.writeInt16LE=function(e,t,n){return e=+e,t|=0,n||T(this,e,t,2,32767,-32768),u.TYPED_ARRAY_SUPPORT?(this[t]=255&e,this[t+1]=e>>>8):M(this,e,t,!0),t+2},u.prototype.writeInt16BE=function(e,t,n){return e=+e,t|=0,n||T(this,e,t,2,32767,-32768),u.TYPED_ARRAY_SUPPORT?(this[t]=e>>>8,this[t+1]=255&e):M(this,e,t,!1),t+2},u.prototype.writeInt32LE=function(e,t,n){return e=+e,t|=0,n||T(this,e,t,4,2147483647,-2147483648),u.TYPED_ARRAY_SUPPORT?(this[t]=255&e,this[t+1]=e>>>8,this[t+2]=e>>>16,this[t+3]=e>>>24):R(this,e,t,!0),t+4},u.prototype.writeInt32BE=function(e,t,n){return e=+e,t|=0,n||T(this,e,t,4,2147483647,-2147483648),e<0&&(e=4294967295+e+1),u.TYPED_ARRAY_SUPPORT?(this[t]=e>>>24,this[t+1]=e>>>16,this[t+2]=e>>>8,this[t+3]=255&e):R(this,e,t,!1),t+4},u.prototype.writeFloatLE=function(e,t,n){return j(this,e,t,!0,n)},u.prototype.writeFloatBE=function(e,t,n){return j(this,e,t,!1,n)},u.prototype.writeDoubleLE=function(e,t,n){return L(this,e,t,!0,n)},u.prototype.writeDoubleBE=function(e,t,n){return L(this,e,t,!1,n)},u.prototype.copy=function(e,t,n,r){if(n||(n=0),r||0===r||(r=this.length),t>=e.length&&(t=e.length),t||(t=0),r>0&&r=this.length)throw new RangeError("sourceStart out of bounds");if(r<0)throw new RangeError("sourceEnd out of bounds");r>this.length&&(r=this.length),e.length-t=0;--a)e[a+t]=this[a+n];else if(i<1e3||!u.TYPED_ARRAY_SUPPORT)for(a=0;a>>=0,n=void 0===n?this.length:n>>>0,e||(e=0),"number"==typeof e)for(i=t;i55295&&n<57344){if(!a){if(n>56319){(t-=3)>-1&&i.push(239,191,189);continue}if(o+1===r){(t-=3)>-1&&i.push(239,191,189);continue}a=n;continue}if(n<56320){(t-=3)>-1&&i.push(239,191,189),a=n;continue}n=65536+(a-55296<<10|n-56320)}else a&&(t-=3)>-1&&i.push(239,191,189);if(a=null,n<128){if((t-=1)<0)break;i.push(n)}else if(n<2048){if((t-=2)<0)break;i.push(n>>6|192,63&n|128)}else if(n<65536){if((t-=3)<0)break;i.push(n>>12|224,n>>6&63|128,63&n|128)}else{if(!(n<1114112))throw new Error("Invalid code point");if((t-=4)<0)break;i.push(n>>18|240,n>>12&63|128,n>>6&63|128,63&n|128)}}return i}function V(e){return r.toByteArray(function(e){if((e=function(e){return e.trim?e.trim():e.replace(/^\s+|\s+$/g,"")}(e).replace(z,"")).length<2)return"";for(;e.length%4!=0;)e+="=";return e}(e))}function U(e,t,n,r){for(var a=0;a=t.length||a>=e.length);++a)t[a+n]=e[a];return a}}).call(this,n(26))},function(e,t,n){var r=n(51);e.exports=function(e){return Object(r(e))}},function(e,t,n){"use strict";t.__esModule=!0;var r=function(e){return e&&e.__esModule?e:{default:e}}(n(61));t.default=function(e){return function(){var t=e.apply(this,arguments);return new r.default(function(e,n){return function a(i,o){try{var s=t[i](o),u=s.value}catch(e){return void n(e)}if(!s.done)return r.default.resolve(u).then(function(e){a("next",e)},function(e){a("throw",e)});e(u)}("next")})}}},function(e,t,n){e.exports=n(387)},function(e,t,n){var r=n(24),a=n(12),i=n(53),o=n(57),s=n(64),u=function(e,t,n){var l,c,f,p=e&u.F,h=e&u.G,d=e&u.S,m=e&u.P,g=e&u.B,y=e&u.W,v=h?a:a[t]||(a[t]={}),b=v.prototype,w=h?r:d?r[t]:(r[t]||{}).prototype;for(l in h&&(n=t),n)(c=!p&&w&&void 0!==w[l])&&s(v,l)||(f=c?w[l]:n[l],v[l]=h&&"function"!=typeof w[l]?n[l]:g&&c?i(f,r):y&&w[l]==f?function(e){var t=function(t,n,r){if(this instanceof e){switch(arguments.length){case 0:return new e;case 1:return new e(t);case 2:return new e(t,n)}return new e(t,n,r)}return e.apply(this,arguments)};return t.prototype=e.prototype,t}(f):m&&"function"==typeof f?i(Function.call,f):f,m&&((v.virtual||(v.virtual={}))[l]=f,e&u.R&&b&&!b[l]&&o(b,l,f)))};u.F=1,u.G=2,u.S=4,u.P=8,u.B=16,u.W=32,u.U=64,u.R=128,e.exports=u},function(e,t,n){"use strict";t.__esModule=!0;var r=function(e){return e&&e.__esModule?e:{default:e}}(n(373));t.default=function(){function e(e,t){for(var n=0;n"+a+""};e.exports=function(e,t){var n={};n[e]=t(s),r(r.P+r.F*a(function(){var t=""[e]('"');return t!==t.toLowerCase()||t.split('"').length>3}),"String",n)}},function(e,t,n){var r=n(7),a=n(30),i=n(35),o=n(74)("src"),s=Function.toString,u=(""+s).split("toString");n(44).inspectSource=function(e){return s.call(e)},(e.exports=function(e,t,n,s){var l="function"==typeof n;l&&(i(n,"name")||a(n,"name",t)),e[t]!==n&&(l&&(i(n,o)||a(n,o,e[t]?""+e[t]:u.join(String(t)))),e===r?e[t]=n:s?e[t]?e[t]=n:a(e,t,n):(delete e[t],a(e,t,n)))})(Function.prototype,"toString",function(){return"function"==typeof this&&this[o]||s.call(this)})},function(e,t,n){var r=n(13),a=n(75);e.exports=n(14)?function(e,t,n){return r.f(e,t,a(1,n))}:function(e,t,n){return e[t]=n,e}},function(e,t){function n(e,t){if(!e)throw new Error(t||"Assertion failed")}e.exports=n,n.equal=function(e,t,n){if(e!=t)throw new Error(n||"Assertion failed: "+e+" != "+t)}},function(e,t,n){var r=n(35),a=n(17),i=n(196)("IE_PROTO"),o=Object.prototype;e.exports=Object.getPrototypeOf||function(e){return e=a(e),r(e,i)?e[i]:"function"==typeof e.constructor&&e instanceof e.constructor?e.constructor.prototype:e instanceof Object?o:null}},function(e,t,n){var r=n(105),a=n(75),i=n(34),o=n(52),s=n(35),u=n(293),l=Object.getOwnPropertyDescriptor;t.f=n(14)?l:function(e,t){if(e=i(e),t=o(t,!0),u)try{return l(e,t)}catch(e){}if(s(e,t))return a(!r.f.call(e,t),e[t])}},function(e,t,n){var r=n(106),a=n(51);e.exports=function(e){return r(a(e))}},function(e,t){var n={}.hasOwnProperty;e.exports=function(e,t){return n.call(e,t)}},function(e,t,n){"use strict";t.__esModule=!0;var r=o(n(369)),a=o(n(367)),i="function"==typeof a.default&&"symbol"==typeof r.default?function(e){return typeof e}:function(e){return e&&"function"==typeof a.default&&e.constructor===a.default&&e!==a.default.prototype?"symbol":typeof e};function o(e){return e&&e.__esModule?e:{default:e}}t.default="function"==typeof a.default&&"symbol"===i(r.default)?function(e){return void 0===e?"undefined":i(e)}:function(e){return e&&"function"==typeof a.default&&e.constructor===a.default&&e!==a.default.prototype?"symbol":void 0===e?"undefined":i(e)}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t){return t&&e.then(function(e){return t(void 0,e),e}).catch(function(e){return t(e),e}),e}},function(e,t){e.exports=function(e){return"object"==typeof e?null!==e:"function"==typeof e}},function(e,t,n){var r=n(48),a=n(256),i=n(170),o=Object.defineProperty;t.f=n(47)?Object.defineProperty:function(e,t,n){if(r(e),t=i(t,!0),r(n),a)try{return o(e,t,n)}catch(e){}if("get"in n||"set"in n)throw TypeError("Accessors not supported!");return"value"in n&&(e[t]=n.value),e}},function(e,t,n){"use strict";var r=n(8);e.exports=function(e,t){return!!e&&r(function(){t?e.call(null,function(){},1):e.call(null)})}},function(e,t){var n=Math.ceil,r=Math.floor;e.exports=function(e){return isNaN(e=+e)?0:(e>0?r:n)(e)}},function(e,t){var n={}.toString;e.exports=function(e){return n.call(e).slice(8,-1)}},function(e,t,n){var r=n(25);e.exports=function(e,t,n){if(r(e),void 0===t)return e;switch(n){case 1:return function(n){return e.call(t,n)};case 2:return function(n,r){return e.call(t,n,r)};case 3:return function(n,r,a){return e.call(t,n,r,a)}}return function(){return e.apply(t,arguments)}}},function(e,t){var n=e.exports={version:"2.6.1"};"number"==typeof __e&&(__e=n)},function(e,t,n){"use strict";var r=n(31),a=n(5);function i(e,t){return 55296==(64512&e.charCodeAt(t))&&!(t<0||t+1>=e.length)&&56320==(64512&e.charCodeAt(t+1))}function o(e){return(e>>>24|e>>>8&65280|e<<8&16711680|(255&e)<<24)>>>0}function s(e){return 1===e.length?"0"+e:e}function u(e){return 7===e.length?"0"+e:6===e.length?"00"+e:5===e.length?"000"+e:4===e.length?"0000"+e:3===e.length?"00000"+e:2===e.length?"000000"+e:1===e.length?"0000000"+e:e}t.inherits=a,t.toArray=function(e,t){if(Array.isArray(e))return e.slice();if(!e)return[];var n=[];if("string"==typeof e)if(t){if("hex"===t)for((e=e.replace(/[^a-z0-9]+/gi,"")).length%2!=0&&(e="0"+e),a=0;a>6|192,n[r++]=63&o|128):i(e,a)?(o=65536+((1023&o)<<10)+(1023&e.charCodeAt(++a)),n[r++]=o>>18|240,n[r++]=o>>12&63|128,n[r++]=o>>6&63|128,n[r++]=63&o|128):(n[r++]=o>>12|224,n[r++]=o>>6&63|128,n[r++]=63&o|128)}else for(a=0;a>>0}return o},t.split32=function(e,t){for(var n=new Array(4*e.length),r=0,a=0;r>>24,n[a+1]=i>>>16&255,n[a+2]=i>>>8&255,n[a+3]=255&i):(n[a+3]=i>>>24,n[a+2]=i>>>16&255,n[a+1]=i>>>8&255,n[a]=255&i)}return n},t.rotr32=function(e,t){return e>>>t|e<<32-t},t.rotl32=function(e,t){return e<>>32-t},t.sum32=function(e,t){return e+t>>>0},t.sum32_3=function(e,t,n){return e+t+n>>>0},t.sum32_4=function(e,t,n,r){return e+t+n+r>>>0},t.sum32_5=function(e,t,n,r,a){return e+t+n+r+a>>>0},t.sum64=function(e,t,n,r){var a=e[t],i=r+e[t+1]>>>0,o=(i>>0,e[t+1]=i},t.sum64_hi=function(e,t,n,r){return(t+r>>>0>>0},t.sum64_lo=function(e,t,n,r){return t+r>>>0},t.sum64_4_hi=function(e,t,n,r,a,i,o,s){var u=0,l=t;return u+=(l=l+r>>>0)>>0)>>0)>>0},t.sum64_4_lo=function(e,t,n,r,a,i,o,s){return t+r+i+s>>>0},t.sum64_5_hi=function(e,t,n,r,a,i,o,s,u,l){var c=0,f=t;return c+=(f=f+r>>>0)>>0)>>0)>>0)>>0},t.sum64_5_lo=function(e,t,n,r,a,i,o,s,u,l){return t+r+i+s+l>>>0},t.rotr64_hi=function(e,t,n){return(t<<32-n|e>>>n)>>>0},t.rotr64_lo=function(e,t,n){return(e<<32-n|t>>>n)>>>0},t.shr64_hi=function(e,t,n){return e>>>n},t.shr64_lo=function(e,t,n){return(e<<32-n|t>>>n)>>>0}},function(e,t){var n,r,a=e.exports={};function i(){throw new Error("setTimeout has not been defined")}function o(){throw new Error("clearTimeout has not been defined")}function s(e){if(n===setTimeout)return setTimeout(e,0);if((n===i||!n)&&setTimeout)return n=setTimeout,setTimeout(e,0);try{return n(e,0)}catch(t){try{return n.call(null,e,0)}catch(t){return n.call(this,e,0)}}}!function(){try{n="function"==typeof setTimeout?setTimeout:i}catch(e){n=i}try{r="function"==typeof clearTimeout?clearTimeout:o}catch(e){r=o}}();var u,l=[],c=!1,f=-1;function p(){c&&u&&(c=!1,u.length?l=u.concat(l):f=-1,l.length&&h())}function h(){if(!c){var e=s(p);c=!0;for(var t=l.length;t;){for(u=l,l=[];++f1)for(var n=1;nx;x++)if((p||x in v)&&(g=b(m=v[x],x,y),e))if(n)_[x]=g;else if(g)switch(e){case 3:return!0;case 5:return m;case 6:return x;case 2:_.push(m)}else if(c)return!1;return f?-1:l||c?c:_}}},function(e,t,n){var r=n(2),a=n(44),i=n(8);e.exports=function(e,t){var n=(a.Object||{})[e]||Object[e],o={};o[e]=t(n),r(r.S+r.F*i(function(){n(1)}),"Object",o)}},function(e,t){e.exports=function(e){if(void 0==e)throw TypeError("Can't call method on "+e);return e}},function(e,t,n){var r=n(9);e.exports=function(e,t){if(!r(e))return e;var n,a;if(t&&"function"==typeof(n=e.toString)&&!r(a=n.call(e)))return a;if("function"==typeof(n=e.valueOf)&&!r(a=n.call(e)))return a;if(!t&&"function"==typeof(n=e.toString)&&!r(a=n.call(e)))return a;throw TypeError("Can't convert object to primitive value")}},function(e,t,n){var r=n(102);e.exports=function(e,t,n){if(r(e),void 0===t)return e;switch(n){case 1:return function(n){return e.call(t,n)};case 2:return function(n,r){return e.call(t,n,r)};case 3:return function(n,r,a){return e.call(t,n,r,a)}}return function(){return e.apply(t,arguments)}}},function(e,t,n){var r=n(270),a=n(2),i=n(131)("metadata"),o=i.store||(i.store=new(n(267))),s=function(e,t,n){var a=o.get(e);if(!a){if(!n)return;o.set(e,a=new r)}var i=a.get(t);if(!i){if(!n)return;a.set(t,i=new r)}return i};e.exports={store:o,map:s,has:function(e,t,n){var r=s(t,n,!1);return void 0!==r&&r.has(e)},get:function(e,t,n){var r=s(t,n,!1);return void 0===r?void 0:r.get(e)},set:function(e,t,n,r){s(n,r,!0).set(e,t)},keys:function(e,t){var n=s(e,t,!1),r=[];return n&&n.forEach(function(e,t){r.push(t)}),r},key:function(e){return void 0===e||"symbol"==typeof e?e:String(e)},exp:function(e){a(a.S,"Reflect",e)}}},function(e,t,n){"use strict";if(n(14)){var r=n(59),a=n(7),i=n(8),o=n(2),s=n(120),u=n(172),l=n(43),c=n(68),f=n(75),p=n(30),h=n(66),d=n(41),m=n(11),g=n(265),y=n(72),v=n(52),b=n(35),w=n(85),x=n(9),_=n(17),S=n(183),E=n(71),N=n(32),O=n(70).f,k=n(181),I=n(74),C=n(10),A=n(49),P=n(130),T=n(103),M=n(178),R=n(83),D=n(125),j=n(69),L=n(179),z=n(276),B=n(13),F=n(33),V=B.f,U=F.f,W=a.RangeError,q=a.TypeError,$=a.Uint8Array,G=Array.prototype,H=u.ArrayBuffer,K=u.DataView,X=A(0),Y=A(2),J=A(3),Z=A(4),Q=A(5),ee=A(6),te=P(!0),ne=P(!1),re=M.values,ae=M.keys,ie=M.entries,oe=G.lastIndexOf,se=G.reduce,ue=G.reduceRight,le=G.join,ce=G.sort,fe=G.slice,pe=G.toString,he=G.toLocaleString,de=C("iterator"),me=C("toStringTag"),ge=I("typed_constructor"),ye=I("def_constructor"),ve=s.CONSTR,be=s.TYPED,we=s.VIEW,xe=A(1,function(e,t){return Oe(T(e,e[ye]),t)}),_e=i(function(){return 1===new $(new Uint16Array([1]).buffer)[0]}),Se=!!$&&!!$.prototype.set&&i(function(){new $(1).set({})}),Ee=function(e,t){var n=d(e);if(n<0||n%t)throw W("Wrong offset!");return n},Ne=function(e){if(x(e)&&be in e)return e;throw q(e+" is not a typed array!")},Oe=function(e,t){if(!(x(e)&&ge in e))throw q("It is not a typed array constructor!");return new e(t)},ke=function(e,t){return Ie(T(e,e[ye]),t)},Ie=function(e,t){for(var n=0,r=t.length,a=Oe(e,r);r>n;)a[n]=t[n++];return a},Ce=function(e,t,n){V(e,t,{get:function(){return this._d[n]}})},Ae=function(e){var t,n,r,a,i,o,s=_(e),u=arguments.length,c=u>1?arguments[1]:void 0,f=void 0!==c,p=k(s);if(void 0!=p&&!S(p)){for(o=p.call(s),r=[],t=0;!(i=o.next()).done;t++)r.push(i.value);s=r}for(f&&u>2&&(c=l(c,arguments[2],2)),t=0,n=m(s.length),a=Oe(this,n);n>t;t++)a[t]=f?c(s[t],t):s[t];return a},Pe=function(){for(var e=0,t=arguments.length,n=Oe(this,t);t>e;)n[e]=arguments[e++];return n},Te=!!$&&i(function(){he.call(new $(1))}),Me=function(){return he.apply(Te?fe.call(Ne(this)):Ne(this),arguments)},Re={copyWithin:function(e,t){return z.call(Ne(this),e,t,arguments.length>2?arguments[2]:void 0)},every:function(e){return Z(Ne(this),e,arguments.length>1?arguments[1]:void 0)},fill:function(e){return L.apply(Ne(this),arguments)},filter:function(e){return ke(this,Y(Ne(this),e,arguments.length>1?arguments[1]:void 0))},find:function(e){return Q(Ne(this),e,arguments.length>1?arguments[1]:void 0)},findIndex:function(e){return ee(Ne(this),e,arguments.length>1?arguments[1]:void 0)},forEach:function(e){X(Ne(this),e,arguments.length>1?arguments[1]:void 0)},indexOf:function(e){return ne(Ne(this),e,arguments.length>1?arguments[1]:void 0)},includes:function(e){return te(Ne(this),e,arguments.length>1?arguments[1]:void 0)},join:function(e){return le.apply(Ne(this),arguments)},lastIndexOf:function(e){return oe.apply(Ne(this),arguments)},map:function(e){return xe(Ne(this),e,arguments.length>1?arguments[1]:void 0)},reduce:function(e){return se.apply(Ne(this),arguments)},reduceRight:function(e){return ue.apply(Ne(this),arguments)},reverse:function(){for(var e,t=Ne(this).length,n=Math.floor(t/2),r=0;r1?arguments[1]:void 0)},sort:function(e){return ce.call(Ne(this),e)},subarray:function(e,t){var n=Ne(this),r=n.length,a=y(e,r);return new(T(n,n[ye]))(n.buffer,n.byteOffset+a*n.BYTES_PER_ELEMENT,m((void 0===t?r:y(t,r))-a))}},De=function(e,t){return ke(this,fe.call(Ne(this),e,t))},je=function(e){Ne(this);var t=Ee(arguments[1],1),n=this.length,r=_(e),a=m(r.length),i=0;if(a+t>n)throw W("Wrong length!");for(;i255?255:255&r),a.v[h](n*t+a.o,r,_e)}(this,n,e)},enumerable:!0})};b?(d=n(function(e,n,r,a){c(e,d,l,"_d");var i,o,s,u,f=0,h=0;if(x(n)){if(!(n instanceof H||"ArrayBuffer"==(u=w(n))||"SharedArrayBuffer"==u))return be in n?Ie(d,n):Ae.call(d,n);i=n,h=Ee(r,t);var y=n.byteLength;if(void 0===a){if(y%t)throw W("Wrong length!");if((o=y-h)<0)throw W("Wrong length!")}else if((o=m(a)*t)+h>y)throw W("Wrong length!");s=o/t}else s=g(n),i=new H(o=s*t);for(p(e,"_d",{b:i,o:h,l:o,e:s,v:new K(i)});fb;b++)if((g=t?v(o(d=e[b])[0],d[1]):v(e[b]))===l||g===c)return g}else for(m=y.call(e);!(d=m.next()).done;)if((g=a(m,v,d.value,t))===l||g===c)return g}).BREAK=l,t.RETURN=c},function(e,t){e.exports=function(e,t,n,r){if(!(e instanceof t)||void 0!==r&&r in e)throw TypeError(n+": incorrect invocation!");return e}},function(e,t,n){"use strict";var r=n(7),a=n(13),i=n(14),o=n(10)("species");e.exports=function(e){var t=r[e];i&&t&&!t[o]&&a.f(t,o,{configurable:!0,get:function(){return this}})}},function(e,t,n){var r=n(291),a=n(195).concat("length","prototype");t.f=Object.getOwnPropertyNames||function(e){return r(e,a)}},function(e,t,n){var r=n(4),a=n(290),i=n(195),o=n(196)("IE_PROTO"),s=function(){},u=function(){var e,t=n(198)("iframe"),r=i.length;for(t.style.display="none",n(194).appendChild(t),t.src="javascript:",(e=t.contentWindow.document).open(),e.write("