UNPKG

@tensorflow/tfjs-layers

Version:

TensorFlow layers API in JavaScript

17 lines 426 kB
/** * @license * Copyright 2024 Google LLC. All Rights Reserved. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============================================================================= */ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@tensorflow/tfjs-core")):"function"==typeof define&&define.amd?define(["exports","@tensorflow/tfjs-core"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).tf=t.tf||{},t.tf)}(this,(function(t,e){"use strict";function n(t){var e=Object.create(null);return t&&Object.keys(t).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}})),e.default=t,e}function r(t,e){return e.forEach((function(e){e&&"string"!=typeof e&&!Array.isArray(e)&&Object.keys(e).forEach((function(n){if("default"!==n&&!(n in t)){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}}))})),t}var i=n(e),a=function(t,e){return a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},a(t,e)};function o(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}a(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function s(t,e,n,r){return new(n||(n=Promise))((function(i,a){function o(t){try{u(r.next(t))}catch(t){a(t)}}function s(t){try{u(r.throw(t))}catch(t){a(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(o,s)}u((r=r.apply(t,e||[])).next())}))}function u(t,e){var n,r,i,a,o={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return a={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function s(a){return function(s){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;o;)try{if(n=1,r&&(i=2&a[0]?r.return:a[0]?r.throw||((i=r.return)&&i.call(r),0):r.next)&&!(i=i.call(r,a[1])).done)return i;switch(r=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,r=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(i=o.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){o.label=a[1];break}if(6===a[0]&&o.label<i[1]){o.label=i[1],i=a;break}if(i&&o.label<i[2]){o.label=i[2],o.ops.push(a);break}i[2]&&o.ops.pop(),o.trys.pop();continue}a=e.call(t,o)}catch(t){a=[6,t],r=0}finally{n=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,s])}}}function l(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function c(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,a=n.call(t),o=[];try{for(;(void 0===e||e-- >0)&&!(r=a.next()).done;)o.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(n=a.return)&&n.call(a)}finally{if(i)throw i.error}}return o}function h(t,e,n){if(n||2===arguments.length)for(var r,i=0,a=e.length;i<a;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))}var p=function(t){function e(n){var r=t.call(this,n)||this;return Object.setPrototypeOf(r,e.prototype),r}return o(e,t),e}(Error),f=function(t){function e(n){var r=t.call(this,n)||this;return Object.setPrototypeOf(r,e.prototype),r}return o(e,t),e}(Error),d=function(t){function e(n){var r=t.call(this,n)||this;return Object.setPrototypeOf(r,e.prototype),r}return o(e,t),e}(Error),g=function(t){function e(n){var r=t.call(this,n)||this;return Object.setPrototypeOf(r,e.prototype),r}return o(e,t),e}(Error),v=function(t){function e(n){var r=t.call(this,n)||this;return Object.setPrototypeOf(r,e.prototype),r}return o(e,t),e}(Error);!function(t){function e(n){var r=t.call(this,n)||this;return Object.setPrototypeOf(r,e.prototype),r}o(e,t)}(Error);var y=function(){function t(t){this.maxEntries=t||100,this.cache=new Map}return t.prototype.get=function(t){var e;return this.cache.has(t)&&(e=this.cache.get(t),this.cache.delete(t),this.cache.set(t,e)),e},t.prototype.put=function(t,e){if(this.cache.has(t))this.cache.delete(t);else if(this.cache.size>=this.maxEntries){var n=this.cache.keys().next().value;this.cache.delete(n)}this.cache.set(t,e)},t.prototype.getMaxEntries=function(){return this.maxEntries},t.prototype.setMaxEntries=function(t){if(t<0)throw new Error("The maxEntries of LRU caches must be at least 0, but got ".concat(t,"."));if(this.maxEntries>t)for(var e=0;e<this.maxEntries-t;e++){var n=this.cache.keys().next().value;this.cache.delete(n)}this.maxEntries=t},t}();function m(t,e){if(Array.isArray(t)){for(var n=[],r=0;r<e;r++)n=n.concat(t);return n}return(n=new Array(e)).fill(t),n}function b(t,e){if(!t)throw new v(e)}function w(t,e){var n,r,i=0;try{for(var a=l(t),o=a.next();!o.done;o=a.next()){o.value===e&&i++}}catch(t){n={error:t}}finally{try{o&&!o.done&&(r=a.return)&&r.call(a)}finally{if(n)throw n.error}}return i}function k(t){return 1===t.length?t[0]:t}function x(t){return Array.isArray(t)?t:[t]}function S(t){var e=t.replace(/(.)([A-Z][a-z0-9]+)/g,"$1_$2").replace(/([a-z])([A-Z])/g,"$1_$2").toLowerCase();return"_"!==e[0]?e:"private"+e}function N(t){return t.length<=1||-1===t.indexOf("_")?t:t.replace(/[_]+(\w|$)/g,(function(t,e){return e.toUpperCase()}))}var I={};function A(t){if(null==t)return null;var e={};return e.className=t.getClassName(),e.config=t.getConfig(),e}function z(t){var e,n;if(null!=t&&"object"==typeof t)if(Array.isArray(t))t.forEach((function(t){return z(t)}));else{var r=Object.keys(t);try{for(var i=l(r),a=i.next();!a.done;a=i.next()){var o=a.value,s=t[o];null!=s&&"object"==typeof s&&(Array.isArray(s)||"ndarray"!==s.type||"number"!=typeof s.value?z(s):t[o]=s.value)}}catch(t){e={error:t}}finally{try{a&&!a.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}}}function E(t,e,n,r,i){var a,o,s,u,h,p,f,g,v,y,m;if(void 0===e&&(e={}),void 0===n&&(n={}),void 0===r&&(r="object"),void 0===i&&(i=!1),"string"==typeof t){var b=t,w=void 0;if(b in n)w=n[b];else if(b in I)w=I[b];else if(null==(w=e[b]))throw new d("Unknown ".concat(r,": ").concat(t,". ")+"This may be due to one of the following reasons:\n"+"1. The ".concat(r," is defined in Python, in which ")+"case it needs to be ported to TensorFlow.js or your JavaScript code.\n"+"2. The custom ".concat(r," is defined in JavaScript, ")+"but is not registered properly with tf.serialization.registerClass().");return w}var k=t;if(null==k.className||null==k.config)throw new d("".concat(r,": Improper config format: ")+"".concat(JSON.stringify(k),".\n")+"'className' and 'config' must set.");var x=k.className,S=void 0,N=void 0;if(x in n?(S=(a=c(n[x],2))[0],N=a[1]):x in I?(S=(o=c(I.className,2))[0],N=o[1]):x in e&&(S=(s=c(e[x],2))[0],N=s[1]),null==S)throw new d("Unknown ".concat(r,": ").concat(x,". ")+"This may be due to one of the following reasons:\n"+"1. The ".concat(r," is defined in Python, in which ")+"case it needs to be ported to TensorFlow.js or your JavaScript code.\n"+"2. The custom ".concat(r," is defined in JavaScript, ")+"but is not registered properly with tf.serialization.registerClass().");if(null!=N){var A={};try{for(var E=l(Object.keys(I)),T=E.next();!T.done;T=E.next()){A[R=T.value]=I[R]}}catch(t){u={error:t}}finally{try{T&&!T.done&&(h=E.return)&&h.call(E)}finally{if(u)throw u.error}}try{for(var C=l(Object.keys(n)),F=C.next();!F.done;F=C.next()){A[R=F.value]=n[R]}}catch(t){p={error:t}}finally{try{F&&!F.done&&(f=C.return)&&f.call(C)}finally{if(p)throw p.error}}k.config.customObjects=A;var D=Object.assign({},I);try{for(var _=l(Object.keys(n)),L=_.next();!L.done;L=_.next()){var R=L.value;I[R]=n[R]}}catch(t){g={error:t}}finally{try{L&&!L.done&&(v=_.return)&&v.call(_)}finally{if(g)throw g.error}}z(k.config);var O=N(S,k.config,n,i);return I=Object.assign({},D),O}D=Object.assign({},I);try{for(var M=l(Object.keys(n)),B=M.next();!B.done;B=M.next()){R=B.value;I[R]=n[R]}}catch(t){y={error:t}}finally{try{B&&!B.done&&(m=M.return)&&m.call(M)}finally{if(y)throw y.error}}O=new S(k.config);return I=Object.assign({},D),O}function T(t,e){return-1*function(t,e){return t<e?-1:t>e?1:0}(t,e)}function C(t){var e,n;if(null==t)return t;var r=[];try{for(var i=l(t),a=i.next();!a.done;a=i.next()){var o=a.value;-1===r.indexOf(o)&&r.push(o)}}catch(t){e={error:t}}finally{try{a&&!a.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}return r}function F(t){if(null==t)throw new d("Invalid value in obj: ".concat(JSON.stringify(t)));for(var e in t)if(t.hasOwnProperty(e))return!1;return!0}function D(t,e,n){if(null!=n&&t.indexOf(n)<0)throw new d("".concat(n," is not a valid ").concat(e,". Valid values are ").concat(t," or null/undefined."))}function _(t,e,n,r){return void 0===n&&(n=0),void 0===r&&(r=1/0),b(n>=0),b(r>=n),Array.isArray(t)&&t.length>=n&&t.length<=r&&t.every((function(t){return typeof t===e}))}function L(t,n){Array.isArray(t)?(e.util.assert(t.length>0,(function(){return"".concat(n," is unexpectedly an empty array.")})),t.forEach((function(t,e){return L(t,"element ".concat(e+1," of ").concat(n))}))):e.util.assert(Number.isInteger(t)&&t>0,(function(){return"Expected ".concat(n," to be a positive integer, but got ")+"".concat(R(t),".")}))}function R(t){return null===t?"null":Array.isArray(t)?"["+t.map((function(t){return R(t)})).join(",")+"]":"string"==typeof t?'"'.concat(t,'"'):"".concat(t)}function O(t){return"relu"===t?"relu":"linear"===t?"linear":"elu"===t?"elu":null}var M=0;function B(){return M++}var P={};function j(t){return void 0===t&&(t=""),t in P||(P[t]=0),P[t]+=1,t+P[t].toString()}var U=["channelsFirst","channelsLast"],W=["nearest","bilinear"],q=["valid","same","causal"],V=["max","avg"],G=["sum","mul","concat","ave"],K=new Map;function H(t){D(U,"DataFormat",t)}function J(t){D(q,"PaddingMode",t)}function Z(t){D(V,"PoolMode",t)}var Y=[];function X(t,e){Y.push(t);try{var n=e();return Y.pop(),n}catch(t){throw Y.pop(),t}}function $(t){if(!nt(t))throw new Error("Not a valid tensor name: '"+t+"'");return(0===Y.length?"":Y.join("/")+"/")+t}function Q(t){if(!nt(t))throw new Error("Not a valid tensor name: '"+t+"'");K.has(t)||K.set(t,0);var e=K.get(t);if(K.set(t,K.get(t)+1),e>0){var n="".concat(t,"_").concat(e);return K.set(n,1),n}return t}var tt,et=new RegExp(/^[A-Za-z0-9][-A-Za-z0-9\._\/]*$/);function nt(t){return!!t.match(et)}function rt(t,e,n){null==e&&(e=0),null==n&&(n=t.length);for(var r=1,i=e;i<n;++i)r*=t[i];return r}function it(t){if(0===t.length)return Number.NaN;for(var e=Number.POSITIVE_INFINITY,n=0;n<t.length;n++){var r=t[n];r<e&&(e=r)}return e}function at(t){if(0===t.length)return Number.NaN;for(var e=Number.NEGATIVE_INFINITY,n=0;n<t.length;n++){var r=t[n];r>e&&(e=r)}return e}function ot(t,e){if(e<t)throw new d("end (".concat(e,") < begin (").concat(t,") is forbidden."));for(var n=[],r=t;r<e;++r)n.push(r);return n}function st(){return null==tt&&(tt=e.backend().epsilon()),tt}function ut(t,e){return i.cast(t,e)}function lt(t,e){void 0===e&&(e=-1);var n=t.shape.slice();return e<0&&(e=n.length+e+1),n.splice(e,0,1),i.reshape(t,n)}function ct(t,n,r){return e.tidy((function(){switch(t.rank){case 1:return i.slice1d(t,n,r);case 2:return i.slice2d(t,[n,0],[r,t.shape[1]]);case 3:return i.slice3d(t,[n,0,0],[r,t.shape[1],t.shape[2]]);case 4:return i.slice4d(t,[n,0,0,0],[r,t.shape[1],t.shape[2],t.shape[3]]);case 5:return i.slice(t,[n,0,0,0,0],[r,t.shape[1],t.shape[2],t.shape[3],t.shape[4]]);case 6:return i.slice(t,[n,0,0,0,0,0],[r,t.shape[1],t.shape[2],t.shape[3],t.shape[4],t.shape[5]]);default:throw new d("sliceAlongFirstAxis() received an unsupported tensor rank: "+"".concat(t.rank))}}))}function ht(t,n,r){return e.tidy((function(){switch(t.rank){case 1:return i.slice1d(t,n,r);case 2:return i.slice2d(t,[0,n],[t.shape[0],r]);case 3:return i.slice3d(t,[0,0,n],[t.shape[0],t.shape[1],r]);case 4:return i.slice4d(t,[0,0,0,n],[t.shape[0],t.shape[1],t.shape[2],r]);default:throw new d("sliceAlongLastAxis() received an unsupported tensor rank: "+"".concat(t.rank))}}))}function pt(t,n,r,a){return e.tidy((function(){switch(t.rank){case 1:return i.slice1d(t,n,r);case 2:switch(a){case 1:return ct(t,n,r);case 2:return ht(t,n,r);default:throw new d("The axis is not within the rank of the tensor "+"".concat(a))}case 3:switch(a){case 1:return ct(t,n,r);case 2:return i.slice3d(t,[0,n,0],[t.shape[0],r,t.shape[2]]);case 3:return ht(t,n,r);default:throw new d("The axis is not within the rank of the tensor "+"".concat(a))}case 4:switch(a){case 1:return ct(t,n,r);case 2:return i.slice4d(t,[0,n,0,0],[t.shape[0],r,t.shape[2],t.shape[3]]);case 3:return i.slice4d(t,[0,0,n,0],[t.shape[0],t.shape[1],r,t.shape[3]]);case 4:return ht(t,n,r);default:throw new d("The axis is not within the rank of the tensor "+"".concat(a))}default:throw new d("sliceAlongLastAxis() received an unsupported tensor rank: "+"".concat(t.rank))}}))}function ft(t,e){var n;return void 0===e&&(e=-1),e<0&&(e=0!==(n=t[0].rank)?n:0),e===t[0].rank&&(e=-1),i.concat(t,e)}function dt(t,e){switch(t.rank){case 1:return i.concat1d([t,e]);case 2:return i.concat2d([t,e],0);case 3:return i.concat3d([t,e],0);case 4:return i.concat4d([t,e],0);default:throw new d("concatAlongFirstAxis() received an unsupported "+"tensor rank: ".concat(t.rank))}}function gt(t,e){if(Array.isArray(e)||(e=[e]),t.rank!==e.length)throw new d("The length of input n (".concat(e.length,") does not match ")+"the number of dimensions in input x (".concat(t.rank,")"));return i.tile(t,e)}function vt(t,e,n,r,a){return void 0===e&&(e=0),void 0===n&&(n=1),i.randomNormal(t,e,n,r,a)}function yt(t,e,n,r){if(t.rank<2||e.rank<2)throw new g("dot requires both inputs to be rank >= 2"+" but got x shape = ".concat(t.shape," and y shape = ").concat(e.shape));if(e.rank>=3&&t.shape.slice(-1)[0]!==(f=e.shape.slice(-2)[0]))throw new g("If rank y >= 3, then the second last dim"+" of y must equal the last dim of x but got x shape = ".concat(t.shape," and ")+" y shape = ".concat(e.shape));if(2===t.rank&&2===e.rank){var a=!1,o=!1;return i.fused.matMul({a:t,b:e,transposeA:a,transposeB:o,bias:r?wt(t.rank,r,"channelsLast"):null,activation:n})}var s=t.shape.slice(),u=s.pop();t=i.reshape(t,[-1,u]);var l=e.shape.slice(),p=l.pop(),f=l.pop(),d=h(h([],c(l),!1),[p],!1),v=Array.from({length:e.rank},(function(t,n){return 0===n?e.rank-2:n<=e.rank-2?n-1:n}));e=i.reshape(i.transpose(e,v),[f,-1]);var y=h(h([],c(s),!1),c(d),!1);a=!1,o=!1;return i.reshape(i.fused.matMul({a:t,b:e,transposeA:a,transposeB:o,bias:r?wt(t.rank,r,"channelsLast"):null,activation:n}),y)}function mt(t,n,r){return e.tidy((function(){return n=Array.isArray(n)?e.tensor1d(n,"int32"):i.cast(n,"int32"),i.gather(t,n,r)}))}function bt(t){return i.mul(t,t)}function wt(t,e,n){var r=e.shape;if(1!==e.rank&&e.rank!==t)throw new d("Unexpected bias dimensions: ".concat(e.rank)+"; expected it to be 1 or ".concat(t));if(5===t){if("channelsFirst"===n)return 1===r.length?i.reshape(e,[1,r[0],1,1,1]):i.reshape(e,[1,r[3],r[0],r[1],r[2]]);if("channelsLast"===n)return 1===r.length?i.reshape(e,[1,1,1,1,r[0]]):i.reshape(e,[1].concat(r))}else if(4===t){if("channelsFirst"===n)return 1===r.length?i.reshape(e,[1,r[0],1,1]):i.reshape(e,[1,r[2],r[0],r[1]]);if("channelsLast"===n)return 1===r.length?i.reshape(e,[1,1,1,r[0]]):i.reshape(e,[1].concat(r))}else if(3===t){if("channelsFirst"===n)return 1===r.length?i.reshape(e,[1,r[0],1]):i.reshape(e,[1,r[1],r[0]]);if("channelsLast"===n)return 1===r.length?i.reshape(e,[1,1,r[0]]):i.reshape(e,[1].concat(r))}else if(t<3)return e;throw new d("Unsupported input rank by biasAdd: ".concat(e.rank))}function kt(t,n,r){return e.tidy((function(){return null==r&&(r="channelsLast"),H(r),i.add(t,wt(t.rank,n,r))}))}function xt(t,n,r,a){return e.tidy((function(){return i.dropout(t,n,r,a)}))}function St(t,e,n){return void 0===n&&(n=!1),n?t():e()}var Nt=["fanIn","fanOut","fanAvg"],It=["normal","uniform","truncatedNormal"];var At=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return o(e,t),e.prototype.fromConfigUsesCustomObjects=function(){return!1},e.prototype.getConfig=function(){return{}},e}(e.serialization.Serializable),zt=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.apply=function(t,n){return e.zeros(t,n)},n}(At);zt.className="Zeros",e.serialization.registerClass(zt);var Et=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return o(n,t),n.prototype.apply=function(t,n){return e.ones(t,n)},n}(At);Et.className="Ones",e.serialization.registerClass(Et);var Tt=function(t){function n(e){var n=t.call(this)||this;if("object"!=typeof e)throw new d("Expected argument of type ConstantConfig but got ".concat(e));if(void 0===e.value)throw new d("config must have value set but got ".concat(e));return n.value=e.value,n}return o(n,t),n.prototype.apply=function(t,n){var r=this;return e.tidy((function(){return e.mul(e.scalar(r.value),e.ones(t,n))}))},n.prototype.getConfig=function(){return{value:this.value}},n}(At);Tt.className="Constant",e.serialization.registerClass(Tt);var Ct=function(t){function n(e){var n=t.call(this)||this;return n.DEFAULT_MINVAL=-.05,n.DEFAULT_MAXVAL=.05,n.minval=e.minval||n.DEFAULT_MINVAL,n.maxval=e.maxval||n.DEFAULT_MAXVAL,n.seed=e.seed,n}return o(n,t),n.prototype.apply=function(t,n){return e.randomUniform(t,this.minval,this.maxval,n,this.seed)},n.prototype.getConfig=function(){return{minval:this.minval,maxval:this.maxval,seed:this.seed}},n}(At);Ct.className="RandomUniform",e.serialization.registerClass(Ct);var Ft=function(t){function e(e){var n=t.call(this)||this;return n.DEFAULT_MEAN=0,n.DEFAULT_STDDEV=.05,n.mean=e.mean||n.DEFAULT_MEAN,n.stddev=e.stddev||n.DEFAULT_STDDEV,n.seed=e.seed,n}return o(e,t),e.prototype.apply=function(t,e){if("float32"!==(e=e||"float32")&&"int32"!==e)throw new g("randomNormal does not support dType ".concat(e,"."));return vt(t,this.mean,this.stddev,e,this.seed)},e.prototype.getConfig=function(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}},e}(At);Ft.className="RandomNormal",e.serialization.registerClass(Ft);var Dt=function(t){function n(e){var n=t.call(this)||this;return n.DEFAULT_MEAN=0,n.DEFAULT_STDDEV=.05,n.mean=e.mean||n.DEFAULT_MEAN,n.stddev=e.stddev||n.DEFAULT_STDDEV,n.seed=e.seed,n}return o(n,t),n.prototype.apply=function(t,n){if("float32"!==(n=n||"float32")&&"int32"!==n)throw new g("truncatedNormal does not support dType ".concat(n,"."));return e.truncatedNormal(t,this.mean,this.stddev,n,this.seed)},n.prototype.getConfig=function(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}},n}(At);Dt.className="TruncatedNormal",e.serialization.registerClass(Dt);var _t=function(t){function n(e){var n=t.call(this)||this;return n.gain=null!=e.gain?e.gain:1,n}return o(n,t),n.prototype.apply=function(t,n){var r=this;return e.tidy((function(){if(2!==t.length||t[0]!==t[1])throw new d("Identity matrix initializer can only be used for 2D square matrices.");return e.mul(r.gain,e.eye(t[0]))}))},n.prototype.getConfig=function(){return{gain:this.gain}},n}(At);_t.className="Identity",e.serialization.registerClass(_t);var Lt=function(t){function n(e){var n,r=t.call(this)||this;if(e.scale<0)throw new d("scale must be a positive float. Got: ".concat(e.scale));return r.scale=null==e.scale?1:e.scale,r.mode=null==e.mode?"fanIn":e.mode,n=r.mode,D(Nt,"FanMode",n),r.distribution=null==e.distribution?"normal":e.distribution,function(t){D(It,"Distribution",t)}(r.distribution),r.seed=e.seed,r}return o(n,t),n.prototype.apply=function(t,n){var r=function(t,e){var n,r;if(void 0===e&&(e="channelsLast"),H(e),2===t.length)n=t[0],r=t[1];else if(-1!==[3,4,5].indexOf(t.length))if("channelsFirst"===e){var i=rt(t,2);n=t[1]*i,r=t[0]*i}else"channelsLast"===e&&(i=rt(t,0,t.length-2),n=t[t.length-2]*i,r=t[t.length-1]*i);else{var a=rt(t);n=Math.sqrt(a),r=Math.sqrt(a)}return[n,r]}(t),i=r[0],a=r[1],o=this.scale;if("fanIn"===this.mode?o/=Math.max(1,i):"fanOut"===this.mode?o/=Math.max(1,a):o/=Math.max(1,(i+a)/2),"normal"===this.distribution){var s=Math.sqrt(o);if("float32"!==(n=n||"float32")&&"int32"!==n)throw new g("".concat(this.getClassName()," does not support dType ").concat(n,"."));return e.truncatedNormal(t,0,s,n,this.seed)}var u=Math.sqrt(3*o);return e.randomUniform(t,-u,u,n,this.seed)},n.prototype.getConfig=function(){return{scale:this.scale,mode:this.mode,distribution:this.distribution,seed:this.seed}},n}(At);Lt.className="VarianceScaling",e.serialization.registerClass(Lt);var Rt=function(t){function e(e){return t.call(this,{scale:1,mode:"fanAvg",distribution:"uniform",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);Rt.className="GlorotUniform",e.serialization.registerClass(Rt);var Ot=function(t){function e(e){return t.call(this,{scale:1,mode:"fanAvg",distribution:"normal",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);Ot.className="GlorotNormal",e.serialization.registerClass(Ot);var Mt=function(t){function e(e){return t.call(this,{scale:2,mode:"fanIn",distribution:"normal",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);Mt.className="HeNormal",e.serialization.registerClass(Mt);var Bt=function(t){function e(e){return t.call(this,{scale:2,mode:"fanIn",distribution:"uniform",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);Bt.className="HeUniform",e.serialization.registerClass(Bt);var Pt=function(t){function e(e){return t.call(this,{scale:1,mode:"fanIn",distribution:"normal",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);Pt.className="LeCunNormal",e.serialization.registerClass(Pt);var jt=function(t){function e(e){return t.call(this,{scale:1,mode:"fanIn",distribution:"uniform",seed:null==e?null:e.seed})||this}return o(e,t),e.prototype.getClassName=function(){return Lt.className},e}(Lt);jt.className="LeCunUniform",e.serialization.registerClass(jt);var Ut=function(t){function n(e){var n=t.call(this)||this;return n.DEFAULT_GAIN=1,n.ELEMENTS_WARN_SLOW=2e3,n.gain=null==e.gain?n.DEFAULT_GAIN:e.gain,n.seed=e.seed,n}return o(n,t),n.prototype.apply=function(t,n){var r=this;return e.tidy((function(){if(t.length<2)throw new g("Shape must be at least 2D.");if("int32"!==n&&"float32"!==n&&void 0!==n)throw new TypeError("Unsupported data type ".concat(n,"."));var i=e.util.sizeFromShape(t.slice(0,-1)),a=t[t.length-1],o=i*a;o>r.ELEMENTS_WARN_SLOW&&console.warn("Orthogonal initializer is being called on a matrix with more "+"than ".concat(r.ELEMENTS_WARN_SLOW," (").concat(o,") elements: ")+"Slowness may result.");var s=vt([Math.max(a,i),Math.min(a,i)],0,1,n,r.seed),u=e.linalg.qr(s,!1),l=u[0],c=u[1].flatten().stridedSlice([0],[Math.min(a,i)*Math.min(a,i)],[Math.min(a,i)+1]);return l=e.mul(l,c.sign()),i<a&&(l=l.transpose()),e.mul(e.scalar(r.gain),l.reshape(t))}))},n.prototype.getConfig=function(){return{gain:this.gain,seed:this.seed}},n}(At);Ut.className="Orthogonal",e.serialization.registerClass(Ut);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,n){return void 0===n&&(n={}),E(t,e.serialization.SerializationMap.getMap().classNameMap,n,"initializer")}function Vt(t){return A(t)}function Gt(t){if("string"==typeof t){var e=t in Wt?Wt[t]:t;if("GlorotNormal"===e)return new Ot;if("GlorotUniform"===e)return new Rt;if("HeNormal"===e)return new Mt;if("HeUniform"===e)return new Bt;if("LeCunNormal"===e)return new Pt;if("LeCunUniform"===e)return new jt;var n={};return n.className=e,n.config={},qt(n)}return t instanceof At?t:qt(t)}function Kt(t){return Array.isArray(t)&&Array.isArray(t[0])}function Ht(t){return 0===t.length?[]:Array.isArray(t[0])?t:[t]}function Jt(t){var e;if(Array.isArray(t)){if(1!==t.length)throw new d("Expected Tensor length to be 1; got ".concat(t.length));e=t[0]}else e=t;return e}function Zt(t){if(Array.isArray(t)&&Array.isArray(t[0])){if(1===t.length)return t[0];throw new d("Expected exactly 1 Shape; got ".concat(t.length))}return t}function Yt(t){var e,n,r=0;try{for(var i=l(t),a=i.next();!a.done;a=i.next()){var o=a.value;0===o.shape.length?r+=1:r+=o.shape.reduce((function(t,e){return t*e}))}}catch(t){e={error:t}}finally{try{a&&!a.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}return r}var Xt="Variable",$t=function(){function t(t,e,n,r,a){void 0===e&&(e="float32"),void 0===n&&(n=Xt),void 0===r&&(r=!0),void 0===a&&(a=null),this.dtype=null==e?"float32":e,this.shape=t.shape,this.id=B(),n=null==n?Xt:n,this.originalName=$(n),this.name=Q(this.originalName),this.trainable_=r,this.constraint=a,this.val=i.variable(t,this.trainable_,this.name,this.dtype)}return t.prototype.read=function(){return this.assertNotDisposed(),this.val},t.prototype.write=function(t){return this.assertNotDisposed(),function(t,e){if(t.shape.toString()!==e.shape.toString())throw new Error("Shape mismatch: "+JSON.stringify(t.shape)+" vs. "+JSON.stringify(e.shape))}(this.val,t),this.val.id!==t.id&&(this.val.assign(t),null!=this.constraint&&this.val.assign(this.constraint.apply(this.val))),this},t.prototype.dispose=function(){this.assertNotDisposed(),this.val.dispose()},t.prototype.assertNotDisposed=function(){if(this.val.isDisposed)throw new Error("LayersVariable ".concat(this.name," is already disposed."))},Object.defineProperty(t.prototype,"trainable",{get:function(){return this.trainable_},set:function(t){this.trainable_=t,this.val.trainable=t},enumerable:!1,configurable:!0}),t}();function Qt(t){return t.map((function(t){return t.read()}))}function te(t){t.forEach((function(t){t[0].write(t[1])}))}var ee=function(t){this.dtype=t.dtype,this.shape=t.shape,null!=t.shape?this.ndim=t.shape.length:this.ndim=t.ndim,this.maxNDim=t.maxNDim,this.minNDim=t.minNDim,this.axes=t.axes||{}},ne=function(t,e,n,r,i,a,o){this.dtype=t,this.shape=e,this.sourceLayer=n,this.inputs=r,this.callArgs=i,this.outputTensorIndex=o,this.id=B(),null!=a&&(this.originalName=$(a),this.name=Q(this.originalName)),this.rank=e.length},re=0,ie=function(){function t(t,e){var n,r;this.callArgs=e,this.id=re++,this.outboundLayer=t.outboundLayer,this.inboundLayers=t.inboundLayers,this.nodeIndices=t.nodeIndices,this.tensorIndices=t.tensorIndices,this.inputTensors=t.inputTensors,this.outputTensors=t.outputTensors,this.inputMasks=t.inputMasks,this.outputMasks=t.outputMasks,this.inputShapes=t.inputShapes,this.outputShapes=t.outputShapes;try{for(var i=l(t.inboundLayers),a=i.next();!a.done;a=i.next()){var o=a.value;null!=o&&o.outboundNodes.push(this)}}catch(t){n={error:t}}finally{try{a&&!a.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}t.outboundLayer.inboundNodes.push(this)}return t.prototype.getConfig=function(){var t,e,n=[];try{for(var r=l(this.inboundLayers),i=r.next();!i.done;i=r.next()){var a=i.value;null!=a?n.push(a.name):n.push(null)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(e=r.return)&&e.call(r)}finally{if(t)throw t.error}}return{outboundLayer:this.outboundLayer?this.outboundLayer.name:null,inboundLayers:n,nodeIndices:this.nodeIndices,tensorIndices:this.tensorIndices}},t}(),ae=0,oe=function(t){function n(e){void 0===e&&(e={});var n=t.call(this)||this;n._callHook=null,n._addedWeightNames=[],n._stateful=!1,n.id=ae++,n.activityRegularizer=null,n.inputSpec=null,n.supportsMasking=!1,n._trainableWeights=[],n._nonTrainableWeights=[],n._losses=[],n._updates=[],n._built=!1,n.inboundNodes=[],n.outboundNodes=[];var r=e.name;if(!r){var i=n.getClassName();r=S(i)+"_"+j(i)}if(n.name=r,n.trainable_=null==e.trainable||e.trainable,null!=e.inputShape||null!=e.batchInputShape){var a=void 0;if(null!=e.batchInputShape)a=e.batchInputShape;else if(null!=e.inputShape){var o=null;null!=e.batchSize&&(o=e.batchSize),a=[o].concat(e.inputShape)}n.batchInputShape=a;var s=e.dtype;null==s&&(s=e.inputDType),null==s&&(s="float32"),n.dtype=s}return null!=e.weights?n.initialWeights=e.weights:n.initialWeights=null,n._refCount=null,n.fastWeightInitDuringBuild=!1,n}return o(n,t),n.nodeKey=function(t,e){return t.name+"_ib-"+e.toString()},n.prototype.getNodeAtIndex=function(t,e){if(0===this.inboundNodes.length)throw new f("The layer has never been called "+"and thus has no defined ".concat(e,"."));if(this.inboundNodes.length<=t)throw new d("Asked to get ".concat(e," at node ").concat(t,", ")+"but the layer has only ".concat(this.inboundNodes.length," inbound nodes."));return this.inboundNodes[t]},n.prototype.getInputAt=function(t){return k(this.getNodeAtIndex(t,"input").inputTensors)},n.prototype.getOutputAt=function(t){return k(this.getNodeAtIndex(t,"output").outputTensors)},Object.defineProperty(n.prototype,"input",{get:function(){if(this.inboundNodes.length>1)throw new p("Layer ".concat(this.name)+' has multiple inbound nodes, hence the notion of "layer input" is ill-defined. Use `getInputAt(nodeIndex)` instead.');if(0===this.inboundNodes.length)throw new p("Layer ".concat(this.name)+" is not connected, no input to return.");return k(this.getNodeAtIndex(0,"input").inputTensors)},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"output",{get:function(){if(0===this.inboundNodes.length)throw new p("Layer ".concat(this.name)+" has no inbound nodes.");if(this.inboundNodes.length>1)throw new p("Layer ".concat(this.name)+' has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use `getOutputAt(nodeIndex)` instead.');return k(this.getNodeAtIndex(0,"output").outputTensors)},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"losses",{get:function(){return this._losses},enumerable:!1,configurable:!0}),n.prototype.calculateLosses=function(){return this.losses.map((function(t){return t()}))},Object.defineProperty(n.prototype,"updates",{get:function(){return this._updates},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"built",{get:function(){return this._built},set:function(t){this._built=t},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"trainable",{get:function(){return this.trainable_},set:function(t){this._trainableWeights.forEach((function(e){return e.trainable=t})),this.trainable_=t},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"trainableWeights",{get:function(){return this.trainable_?this._trainableWeights.filter((function(t){return t.trainable})):[]},set:function(t){this._trainableWeights=t},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"nonTrainableWeights",{get:function(){return this.trainable?this._trainableWeights.filter((function(t){return!t.trainable})).concat(this._nonTrainableWeights):this._trainableWeights.concat(this._nonTrainableWeights)},set:function(t){this._nonTrainableWeights=t},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"weights",{get:function(){return this.trainableWeights.concat(this.nonTrainableWeights)},enumerable:!1,configurable:!0}),Object.defineProperty(n.prototype,"stateful",{get:function(){return this._stateful},enumerable:!1,configurable:!0}),n.prototype.resetStates=function(){if(!this.stateful)throw new Error("Cannot call the resetStates() method of a non-stateful Layer object.")},n.prototype.assertInputCompatibility=function(t){var e=x(t);if(null!=this.inputSpec&&0!==this.inputSpec.length){var n=x(this.inputSpec);if(e.length!==n.length)throw new d("Layer ".concat(this.name," expects ").concat(n.length," inputs, ")+"but it received ".concat(e.length," input tensors. ")+"Input received: ".concat(t));for(var r=0;r<e.length;r++){var i=e[r],a=n[r];if(null!=a){var o=i.rank;if(null!=a.ndim&&o!==a.ndim)throw new d("Input ".concat(r," is incompatible with layer ").concat(this.name,": ")+"expected ndim=".concat(a.ndim,", found ndim=").concat(o));if(null!=a.maxNDim&&o>a.maxNDim)throw new d("Input ".concat(r," is incompatible with layer ").concat(this.name)+": expected max_ndim=".concat(a.maxNDim,", found ndim=").concat(o));if(null!=a.minNDim&&o<a.minNDim)throw new d("Input ".concat(r," is incompatible with layer ").concat(this.name)+": expected min_ndim=".concat(a.minNDim,", found ndim=").concat(o,"."));if(null!=a.dtype&&i.dtype!==a.dtype)throw new d("Input ".concat(r," is incompatible with layer ").concat(this.name," ")+": expected dtype=".concat(a.dtype,", found dtype=").concat(i.dtype,"."));if(a.axes){var s=i.shape;for(var u in a.axes){var l=Number(u),c=a.axes[u],h=l>=0?s[l]:s[s.length+l];if(null!=c&&-1===[c,null].indexOf(h))throw new d("Input ".concat(r," is incompatible with layer ")+"".concat(this.name,": expected axis ").concat(l," of input shape to ")+"have value ".concat(c," but got shape ").concat(s,"."))}}if(null!=a.shape)for(var p=0;p<a.shape.length;++p){var f=a.shape[p],g=i.shape[p];if(null!=f&&null!=g&&f!==g)throw new d("Input ".concat(r," is incompatible with layer ")+"".concat(this.name,": expected shape=").concat(a.shape,", ")+"found shape=".concat(i.shape,"."))}}}}},n.prototype.call=function(t,e){return t},n.prototype.invokeCallHook=function(t,e){null!=this._callHook&&this._callHook(t,e)},n.prototype.setCallHook=function(t){this._callHook=t},n.prototype.clearCallHook=function(){this._callHook=null},n.prototype.apply=function(t,e){var n=this;e=e||{},this.assertNotDisposed();var r=x(t),i=function(t){var e,n,r=!0;try{for(var i=l(x(t)),a=i.next();!a.done;a=i.next()){if(!(a.value instanceof ne)){r=!1;break}}}catch(t){e={error:t}}finally{try{a&&!a.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}return r}(t),a=function(t){var e,n,r=!0;try{for(var i=l(x(t)),a=i.next();!a.done;a=i.next()){if(a.value instanceof ne){r=!1;break}}}catch(t){e={error:t}}finally{try{a&&!a.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}return r}(t);if(i===a)throw new d("Arguments to apply() must be all SymbolicTensors or all Tensors");return X(this.name,(function(){var i,o,s,u;if(!n.built){n.assertInputCompatibility(t);var c=[];try{for(var h=l(x(t)),p=h.next();!p.done;p=h.next()){var f=p.value;c.push(f.shape)}}catch(t){i={error:t}}finally{try{p&&!p.done&&(o=h.return)&&o.call(h)}finally{if(i)throw i.error}}n.build(k(c)),n.built=!0,n.initialWeights&&n.setWeights(n.initialWeights),null===n._refCount&&a&&(n._refCount=1)}if(n.assertInputCompatibility(t),a){var d=n.call(t,e);n.supportsMasking&&n.setMaskMetadata(t,d);var v=x(d),y=[];try{for(var m=l(v),b=m.next();!b.done;b=m.next()){var w=b.value;-1!==r.indexOf(w)&&(w=w.clone()),y.push(w)}}catch(t){s={error:t}}finally{try{b&&!b.done&&(u=m.return)&&u.call(m)}finally{if(s)throw s.error}}if(d=k(y),null!=n.activityRegularizer)throw new g("Layer invocation in the presence of activity regularizer(s) is not supported yet.");return d}var S=function(t){var e,n;t=x(t);var r=[];try{for(var i=l(t),a=i.next();!a.done;a=i.next()){var o=a.value;r.push(o.shape)}}catch(t){e={error:t}}finally{try{a&&!a.done&&(n=i.return)&&n.call(i)}finally{if(e)throw e.error}}return k(r)}(t),N=n.computeOutputShape(S),I=(d=void 0,"float32");if(n.warnOnIncompatibleInputShape(Array.isArray(t)?S[0]:S),d=null!=N&&N.length>0&&Array.isArray(N[0])?N.map((function(r,i){return new ne(I,r,n,x(t),e,n.name,i)})):new ne(I,N,n,x(t),e,n.name),n.addInboundNode(t,d,null,null,S,N,e),n._refCount++,null!=n.activityRegularizer)throw new g("Layer invocation in the presence of activity regularizer(s) is not supported yet.");return d}))},n.prototype.warnOnIncompatibleInputShape=function(t){if(null!=this.batchInputShape)if(t.length!==this.batchInputShape.length)console.warn("The rank of the input tensor provided (shape: "+"".concat(JSON.stringify(t),") does not match that of the ")+"batchInputShape (".concat(JSON.stringify(this.batchInputShape),") ")+"of the layer ".concat(this.name));else{var e=!1;this.batchInputShape.forEach((function(n,r){null!=n&&null!=t[r]&&t[r]!==n&&(e=!0)})),e&&console.warn("The shape of the input tensor "+"(".concat(JSON.stringify(t),") does not ")+"match the expectation of layer ".concat(this.name,": ")+"".concat(JSON.stringify(this.batchInputShape)))}},Object.defineProperty(n.prototype,"outputShape",{get:function(){var t,e;if(null==this.inboundNodes||0===this.inboundNodes.length)throw new p("The layer ".concat(this.name," has never been called and thus has no ")+"defined output shape.");var n=[];try{for(var r=l(this.inboundNodes),i=r.next();!i.done;i=r.next()){var a=i.value,o=JSON.stringify(a.outputShapes);-1===n.indexOf(o)&&n.push(o)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(e=r.return)&&e.call(r)}finally{if(t)throw t.error}}if(1===n.length){var s=this.inboundNodes[0].outputShapes;return Array.isArray(s)&&Array.isArray(s[0])&&1===s.length?s[0]:s}throw new p("The layer ".concat(this.name," has multiple inbound nodes with different ")+'output shapes. Hence the notion of "output shape" is ill-defined for the layer.')},enumerable:!1,configurable:!0}),n.prototype.countParams=function(){if(!this.built)throw new f("You tried to call countParams() on ".concat(this.name,", ")+"but the layer is not built yet. Build it first by calling build(batchInputShape).");return Yt(this.weights)},n.prototype.build=function(t){this.built=!0},n.prototype.getWeights=function(t){return void 0===t&&(t=!1),Qt(t?this.trainableWeights:this.weights)},n.prototype.setWeights=function(t){var n=this;e.tidy((function(){var r=n.weights;if(r.length!==t.length)throw new d('You called setWeights(weights) on layer "'.concat(n.name,'" ')+"with a weight list of length ".concat(t.length,", ")+"but the layer was expecting ".concat(r.length," weights. ")+"Provided weights: ".concat(t,"..."));if(0!==r.length){for(var i=[],a=Qt(r),o=0;o<a.length;++o){var s=a[o],u=r[o],l=t[o];if(!e.util.arraysEqual(s.shape,l.shape))throw new d("Layer weight shape ".concat(s.shape," ")+"not compatible with provided weight shape ".concat(l.shape));i.push([u,l])}te(i)}}))},n.prototype.addWeight=function(t,e,n,r,i,a,o,s){if(-1!==this._addedWeightNames.indexOf(t))throw new d("Duplicate weight name ".concat(t," for layer ").concat(this.name));this._addedWeightNames.push(t),null==n&&(n="float32"),this.fastWeightInitDuringBuild&&(r=null!=s?s():Gt("zeros"));var u=r.apply(e,n),l=new $t(u,n,t,a,o);return u.dispose(),null!=i&&this.addLoss((function(){return i.apply(l.read())})),null==a&&(a=!0),a?this._trainableWeights.push(l):this._nonTrainableWeights.push(l),l},n.prototype.setFastWeightInitDuringBuild=function(t){this.fastWeightInitDuringBuild=t},n.prototype.addLoss=function(t){var e;null==t||Array.isArray(t)&&0===t.length||(t=x(t),void 0!==this._losses&&null!==this._losses&&(e=this.losses).push.apply(e,h([],c(t),!1)))},n.prototype.computeOutputShape=function(t){return t},n.prototype.computeMask=function(t,e){var n=this;if(!this.supportsMasking){if(null!=e){if(!Array.isArray(e))throw new TypeError("Layer ".concat(this.name," does not support masking, ")+"but was passed an inputMask.");e.forEach((function(t){if(null!=t)throw new TypeError("Layer ".concat(n.name," does not support masking, ")+"but was passed an inputMask.")}))}return null}return e},n.prototype.setMaskMetadata=function(t,e,n){if(this.supportsMasking){var r=this.computeMask(t,n),i=x(e),a=x(r);if(i.length!==a.length)throw new Error("".concat(this.name," outputs ").concat(i.length," tensors ")+"but ".concat(i.length," masks for those tensors"));for(var o=0;o<i.length;o++)i[o].kerasMask=a[o]}},n.prototype.addInboundNode=function(t,e,n,r,i,a,o){var s,u;void 0===o&&(o=null);var c=x(t);e=x(e),n=x(n),r=x(r),i=Ht(i),a=Ht(a);var h=[],p=[],f=[];try{for(var d=l(c),g=d.next();!g.done;g=d.next()){var v=g.value;h.push(v.sourceLayer),p.push(v.nodeIndex),f.push(v.tensorIndex)}}catch(t){s={error:t}}finally{try{g&&!g.done&&(u=d.return)&&u.call(d)}finally{if(s)throw s.error}}new ie({outboundLayer:this,inboundLayers:h,nodeIndices:p,tensorIndices:f,inputTensors:c,outputTensors:e,inputMasks:n,outputMasks:r,inputShapes:i,outputShapes:a},o);for(var y=0;y<e.length;y++)e[y].sourceLayer=this,e[y].nodeIndex=this.inboundNodes.length-1,e[y].tensorIndex=y},n.prototype.getConfig=function(){var t={name:this.name,trainable:this.trainable};return null!=this.batchInputShape&&(t.batchInputShape=this.batchInputShape),null!=this.dtype&&(t.dtype=this.dtype),t},n.prototype.disposeWeights=function(){return this.weights.forEach((function(t){return t.dispose()})),this.weights.length},n.prototype.assertNotDisposed=function(){if(0===this._refCount)throw new Error("Layer '".concat(this.name,"' is already disposed."))},n.prototype.dispose=function(){if(!this.built)throw new Error("Cannot dispose Layer ".concat(this.name," because it has not been ")+"built yet.");if(null===this._refCount)throw new Error("Cannot dispose Layer ".concat(this.name," because it has not been used ")+"yet.");this.assertNotDisposed();var t=0;return 0==--this._refCount&&(t=this.disposeWeights()),{refCountAfterDispose:this._refCount,numDisposedVariables:t}},n}(e.serialization.Serializable);function se(t,e,n){var r,i;if((null==e||null!=n&&n>0)&&(e=t.sourceLayer,n=t.nodeIndex),0===e.inboundNodes.length)return[t];var a=e.inboundNodes[n];if(0===a.inboundLayers.length)return a.inputTensors;for(var o=[],s=0;s<a.inboundLayers.length;s++){var u=se(a.inputTensors[s],a.inboundLayers[s],a.nodeIndices[s]);try{for(var c=(r=void 0,l(u)),h=c.next();!h.done;h=c.next()){var p=h.value;-1===o.indexOf(p)&&o.push(p)}}catch(t){r={error:t}}finally{try{h&&!h.done&&(i=c.return)&&i.call(c)}finally{if(r)throw r.error}}}return o}var ue=function(t){function e(e){var n=t.call(this,{dtype:e.dtype,name:null!=e.name?e.name:j("input").toString()})||this;if(null==e.batchSize&&(e.batchSize=null),null==e.sparse&&(e.sparse=!1),n.trainable=!1,n.built=!0,n.sparse=e.sparse,null!=e.inputShape&&null!=e.batchInputShape)throw new d("Only provide the inputShape OR batchInputShape argument to inputLayer, not both at the same time.");var r=e.batchInputShape;if(null==r){if(null==e.inputShape)throw new d("An InputLayer should be passed either a `batchInputShape` or an `inputShape`.");r=[e.batchSize].concat(e.inputShape)}else if(null!=e.batchSize)throw new d("Cannot specify batchSize if batchInputShape is specified when creating an InputLayer.");var i=e.dtype||"float32";n.batchInputShape=r,n.dtype=i,n.inputSpec=[{shape:r}];var a=new ne(n.dtype,n.batchInputShape,n,[],{},n.name);return a.nodeIndex=0,a.tensorIndex=0,new ie({outboundLayer:n,inboundLayers:[],nodeIndices:[],tensorIndices:[],inputTensors:[a],outputTensors:[a],inputMasks:[null],outputMasks:[null],inputShapes:[r],outputShapes:[r]}),n}return o(e,t),e.prototype.apply=function(t,e){throw new d("Cannot pass any input to an "+"InputLayer's apply() method. InputLayer name: ".concat(this.name))},e.prototype.dispose=function(){return{refCountAfterDispose:this._refCount,numDisposedVariables:0}},e.prototype.getConfig=function(){return{batchInputShape:this.batchInputShape,dtype:this.dtype,sparse:this.sparse,name:this.name}},e}(oe);function le(t){if(null==t.batchShape&&null==t.shape)throw new Error("Please provide to Input either a `shape` or a `batchShape` argument. Note that `shape` does not include the batch dimension.");if(null!=t.batchShape&&null!=t.shape)throw new d("Please provide either a `shape` or `batchShape` argument to Input, but not both.");var e=t.batchShape;null!=t.shape&&null==e&&(e=[null].concat(t.shape));var n=t.dtype;return null==n&&(n="float32"),new ue({batchInputShape:e,name:t.name,dtype:n,sparse:t.sparse}).inboundNodes[0].outputTensors[0]}ue.className="InputLayer",e.serialization.registerClass(ue);var ce=function(){function t(e){var n,r;if(this.id2Value={},this.id2Mask={},this.name2Id={},e instanceof t)for(var i in e.id2Value)this.id2Value[i]=e.id2Value[i],i in e.id2Mask&&(this.id2Mask[i]=e.id2Mask[i]);else{if(null==e)return;try{for(var a=l(e),o=a.next();!o.done;o=a.next()){var s=o.value;this.add(s.key,s.value)}}catch(t){n={error:t}}finally{try{o&&!o.done&&(r=a.return)&&r.call(a)}finally{if(n)throw n.error}}}}return t.prototype.add=function(t,n,r){if(null!=this.id2Value[t.id])throw new d("Duplicate key: name=".concat(t.name,", id=").concat(t.id));return this.id2Value[t.id]=function(t,n){if(null==t.dtype||t.dtype===n.dtype)return n;try{return e.cast(n,t.dtype)}catch(e){throw new d("The dtype of the feed (".concat(n.dtype,") can not be cast to the dtype ")+"of the key '".concat(t.name,"' (").concat(t.dtype,")."))}}(t,n),this.name2Id[t.name]=t.id,null!=r&&(this.id2Mask[t.id]=r),this},t.prototype.addFeed=function(t){this.add(t.key,t.value)},t.prototype.hasKey=function(t){return null!=this.id2Value[t.id]},t.prototype.names=function(){return Object.keys(this.name2Id)},t.prototype.getValue=function(t){if(t instanceof ne){if(null==this.id2Value[t.id])throw new d("Nonexistent key: ".concat(t.name));return this.id2Value[t.id]}var e=this.name2Id[t];if(null==e)throw new d("Feed dict has no SymbolicTensor name: ".concat(t));return this.id2Value[e]},t.prototype.getMask=function(t){if(t instanceof ne){if(null==this.id2Value[t.id])throw new d("Nonexistent key: ".concat(t.name));return this.id2Mask[t.id]}var e=this.name2Id[t];if(null==e)throw new d("Feed dict has no SymbolicTensor name: ".concat(t));return this.id2Mask[e]},t.prototype.disposeMasks=function(){null!=this.id2Mask&&e.dispose(this.id2Mask)},t}(),he=new y,pe=new y;function fe(t,n,r,i){var a,o,s,u,c=null!=r&&r.training,h=Array.isArray(t),p=h?t:[t],f=p.map((function(t){return t.name})),d=[],g=n.names();try{for(var v=l(f),y=v.next();!y.done;y=v.next()){var m=y.value;-1!==g.indexOf(m)?d.push(n.getValue(m)):d.push(null)}}catch(t){a={error:t}}finally{try{y&&!y.done&&(o=v.return)&&o.call(v)}finally{if(a)throw a.error}}null!=i&&(i.maxNumTensors=-1/0,i.minNumTensors=1/0);var b,w=f.join(",")+"|"+n.names().sort().join(","),k=he.get(w);if(null==k){var S=function(t,n){var r,i,a,o;e.util.assert(null!=t&&t.length>0,(function(){return"Expected at least one fetch, got none"}));var s=[],u={};if(1===t.length){var c=ge(t[0],n);s=c.sorted,u=c.recipientMap}else{var h=new Set;try{for(var p=l(t),f=p.next();!f.done;f=p.next()){var d=ge(f.value,n),g=d.sorted,v=d.recipientMap;try{for(var y=(a=void 0,l(g)),m=y.next();!m.done;m=y.next()){var b=m.value;h.has(b.name)||(s.push(b),h.add(b.name))}}catch(t){a={error:t}}finally{try{m&&!m.done&&(o=y.return)&&o.call(y)}finally{if(a)throw a.error}}var w=function(t){null==u[t]&&(u[t]=new Set),v[t].forEach((function(e){return u[t].add(e)}))};for(var k in v)w(k)}}catch(t){r={error:t}}finally{try{f&&!f.done&&(i=p.return)&&i.call(p)}finally{if(r)throw r.error}}}return{sorted:s,recipientCounts:de(u)}}(p,n);k=S.sorted,b=S.recipientCounts,he.put(w,k),pe.put(w,b)}b={},c||Object.assign(b,pe.get(w));for(var N=new ce(n),I=0;I<k.length;++I){if(null!=i){var A=e.memory().numTensors;A>i.maxNumTensors&&(i.maxNumTensors=A),A<i.minNumTensors&&(i.minNumTensors=A)}var z=k[I],E=z.sourceLayer;if(!(E instanceof ue)){var T=[],C=[],F=[],D=!1;try{for(var _=(s=void 0,l(z.inputs)),L=_.next();!L.done;L=_.next()){var R=L.value,O=N.getValue(R),M=N.getMask(R);T.push(O),C.push(M),null!=M&&(D=!0),c||(b[R.name]--,0!==b[R.name]||n.hasKey(R)||-1!==f.indexOf(R.name)||O.isDisposed||!0===R.sourceLayer.stateful||F.push(O))}}catch(t){s={error:t}}finally{try{L&&!L.done&&(u=_.return)&&u.call(_)}finally{if(s)throw s.error}}D&&((r=r||{}).mask=C[0]);var B=x(E.apply(T,r)),P=null;E.supportsMasking&&(P=E.computeMask(T,C));for(var j=ve(z),U=Array.isArray(j)?j:[j],W=0;W<U.length;++W){N.hasKey(U[W])||N.add(U[W],B[W],Array.isArray(P)?P[0]:P);var q=f.indexOf(U[W].name);-1!==q&&(d[q]=B[W])}c||e.dispose(F)}}return N.disposeMasks(),h?d:d[0]}function de(t){var e={};for(var n in t)e[n]=t[n].size;return e}function ge(t,e){var n,r,i,a,o=new Set,s=[],u={};try{for(var c=l(e.names()),h=c.next();!h.done;h=c.next()){var p=h.value;o.add(p)}}catch(t){n={error:t}}finally{try{h&&!h.done&&(r=c.return)&&r.call(c)}finally{if(n)throw n.error}}var f=[],d=[];for(f.push(t);f.length>0;){var g=f[f.length-1];if(o.has(g.name))f.pop();else{var v=d[d.length-1]===f.length-1;if(0===g.inputs.length||v)f.pop(),s.push(g),o.add(g.name),v&&d.pop();else{d.push(f.length-1);try{for(var y=(i=void 0,l(g.inputs)),m=y.next();!m.done;m=y.next()){var b=m.value;null==u[b.name]&&(u[b.name]=new Set),u[b.name].add(g.name),o.has(b.name)||f.push(b)}}catch(t){i={error:t}}finally{try{m&&!m.done&&(a=y.return)&&a.call(y)}finally{if(i)throw i.error}}}}}return{sorted:s,recipientMap:u}}function ve(t){var e,n,r;if(1===t.sourceLayer.inboundNodes.length)r=t.sourceLayer.output;else{for(var i=null,a=0;a<t.sourceLayer.inboundNodes.length;++a)try{for(var o=(e=void 0,l(t.sourceLayer.inboundNodes[a].outputTensors)),s=o.next();!s.done;s=o.next()){if(s.value.id===t.id){i=a;break}}}catch(t){e={error:t}}finally{try{s&&!s.done&&(n=o.return)&&n.call(o)}finally{if(e)throw e.error}}r=t.sourceLayer.getOutputAt(i)}return r}e.env().registerFlag("TOPOLOGICAL_SORT_CACHE_MAX_ENTRIES",(function(){return 100}),(functi