@tensorflow/tfjs-core
Version:
Hardware-accelerated JavaScript library for machine intelligence
17 lines • 237 kB
JavaScript
/**
* @license
* Copyright 2021 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(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).tf=e.tf||{})}(this,(function(e){"use strict";var t=function(e,n){return(t=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])})(e,n)};function n(e,n){function r(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}function r(e,t,n,r){return new(n||(n=Promise))((function(a,o){function i(e){try{u(r.next(e))}catch(e){o(e)}}function s(e){try{u(r.throw(e))}catch(e){o(e)}}function u(e){e.done?a(e.value):new n((function(t){t(e.value)})).then(i,s)}u((r=r.apply(e,t||[])).next())}))}function a(e,t){var n,r,a,o,i={label:0,sent:function(){if(1&a[0])throw a[1];return a[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;i;)try{if(n=1,r&&(a=2&o[0]?r.return:o[0]?r.throw||((a=r.return)&&a.call(r),0):r.next)&&!(a=a.call(r,o[1])).done)return a;switch(r=0,a&&(o=[2&o[0],a.value]),o[0]){case 0:case 1:a=o;break;case 4:return i.label++,{value:o[1],done:!1};case 5:i.label++,r=o[1],o=[0];continue;case 7:o=i.ops.pop(),i.trys.pop();continue;default:if(!(a=i.trys,(a=a.length>0&&a[a.length-1])||6!==o[0]&&2!==o[0])){i=0;continue}if(3===o[0]&&(!a||o[1]>a[0]&&o[1]<a[3])){i.label=o[1];break}if(6===o[0]&&i.label<a[1]){i.label=a[1],a=o;break}if(a&&i.label<a[2]){i.label=a[2],i.ops.push(o);break}a[2]&&i.ops.pop(),i.trys.pop();continue}o=t.call(e,i)}catch(e){o=[6,e],r=0}finally{n=a=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}var o=function(){function e(e,t){this.backend=e,this.dataMover=t,this.data=new WeakMap,this.dataIdsCount=0}return e.prototype.get=function(e){return this.data.has(e)||this.dataMover.moveData(this.backend,e),this.data.get(e)},e.prototype.set=function(e,t){this.dataIdsCount++,this.data.set(e,t)},e.prototype.has=function(e){return this.data.has(e)},e.prototype.delete=function(e){return this.dataIdsCount--,this.data.delete(e)},e.prototype.numDataIds=function(){return this.dataIdsCount},e}(),i=function(){function e(){}return e.prototype.refCount=function(e){return s("refCount")},e.prototype.incRef=function(e){return s("incRef")},e.prototype.timerAvailable=function(){return!0},e.prototype.time=function(e){return s("time")},e.prototype.read=function(e){return s("read")},e.prototype.readSync=function(e){return s("readSync")},e.prototype.numDataIds=function(){return s("numDataIds")},e.prototype.disposeData=function(e,t){return s("disposeData")},e.prototype.write=function(e,t,n){return s("write")},e.prototype.move=function(e,t,n,r,a){return s("move")},e.prototype.memory=function(){return s("memory")},e.prototype.floatPrecision=function(){return s("floatPrecision")},e.prototype.epsilon=function(){return 32===this.floatPrecision()?1e-7:1e-4},e.prototype.dispose=function(){return s("dispose")},e}();function s(e){throw new Error("'"+e+"' not yet implemented or not found in the registry. This kernel may not be supported by the tfjs backend you have chosen")}function u(e){for(var t=e.length,n=0,r=0;t>0;)r=Math.random()*t|0,n=e[--t],e[t]=e[r],e[r]=n}function l(e,t,n){return Math.max(e,Math.min(t,n))}function c(e,t){if(!e)throw new Error("string"==typeof t?t:t())}function f(e,t,n){void 0===n&&(n=""),c(v(e,t),(function(){return n+" Shapes "+e+" and "+t+" must match"}))}function h(e){c(null!=e,(function(){return"The input to the tensor constructor must be a non-null value."}))}function d(e,t,n){if(void 0===t&&(t=[]),void 0===n&&(n=!1),null==t&&(t=[]),Array.isArray(e)||S(e)&&!n)for(var r=0;r<e.length;++r)d(e[r],t,n);else t.push(e);return t}function p(e){if(0===e.length)return 1;for(var t=e[0],n=1;n<e.length;n++)t*=e[n];return t}function v(e,t){if(e===t)return!0;if(null==e||null==t)return!1;if(e.length!==t.length)return!1;for(var n=0;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}function m(e){return e%1==0}function g(e,t){return t<=e.length?e:e+" ".repeat(t-e.length)}function y(e,t){var n=t.length;return c((e=null==e?t.map((function(e,t){return t})):[].concat(e)).every((function(e){return e>=-n&&e<n})),(function(){return"All values in axis param must be in range [-"+n+", "+n+") but got axis "+e})),c(e.every((function(e){return m(e)})),(function(){return"All values in axis param must be integers but got axis "+e})),e.map((function(e){return e<0?n+e:e}))}function b(e,t){for(var n=[],r=[],a=null!=t&&Array.isArray(t)&&0===t.length,o=null==t||a?null:y(t,e).sort(),i=0,s=0;s<e.length;++s){if(null!=o){if(o[i]===s&&1!==e[s])throw new Error("Can't squeeze axis "+s+" since its dim '"+e[s]+"' is not 1");(null==o[i]||o[i]>s)&&1===e[s]&&(n.push(e[s]),r.push(s)),o[i]<=s&&i++}1!==e[s]&&(n.push(e[s]),r.push(s))}return{newShape:n,keptDims:r}}function w(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 k(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)n=new Uint8Array(t);else{if("string"!==e)throw new Error("Unknown data type "+e);n=new Array(t)}return n}function x(e,t){for(var n=0;n<e.length;n++){var r=e[n];if(isNaN(r)||!isFinite(r))throw Error("A tensor of type "+t+" being uploaded contains "+r+".")}}function E(e){return"bool"===e||"complex64"===e||"float32"===e||"int32"===e||"string"===e}function S(e){return e instanceof Float32Array||e instanceof Int32Array||e instanceof Uint8Array}function A(e){if("float32"===e||"int32"===e)return 4;if("complex64"===e)return 8;if("bool"===e)return 1;throw new Error("Unknown dtype "+e)}function M(e){if(null==e)return 0;var t=0;return e.forEach((function(e){return t+=e.length})),t}function N(e){return"string"==typeof e||e instanceof String}function I(e){return"boolean"==typeof e}function _(e){return"number"==typeof e}function T(e){return Array.isArray(e)?T(e[0]):e instanceof Float32Array?"float32":e instanceof Int32Array||e instanceof Uint8Array?"int32":_(e)?"float32":N(e)?"string":I(e)?"bool":"float32"}function D(e){return!!(e&&e.constructor&&e.call&&e.apply)}function R(e,t){for(var n=t;n<e;++n)if(e%n==0)return n;return e}function C(e){var t=e.length;if(t<2)return[];var n=new Array(t-1);n[t-2]=e[t-1];for(var r=t-3;r>=0;--r)n[r]=n[r+1]*e[r+1];return n}function F(e,t){if(0===e.length)return t[0];var n=e.reduce((function(e,t){return e*t}));if(0===n)return[];if(n!==t.length)throw new Error("["+e+"] does not match the input size "+t.length+".");return function e(t,n,r){var a=new Array;if(1===n.length)for(var o=n[0],i=0;i<o;i++)a[i]=r[t+i];else{o=n[0];var s=n.slice(1),u=s.reduce((function(e,t){return e*t}));for(i=0;i<o;i++)a[i]=e(t+i*u,s,r)}return a}(0,e,t)}function B(e,t){for(var n=P(e,t),r=0;r<n.length;r++)n[r]=1;return n}function P(e,t){if(null==t||"float32"===t||"complex64"===t)return new Float32Array(e);if("int32"===t)return new Int32Array(e);if("bool"===t)return new Uint8Array(e);throw new Error("Unknown data type "+t)}function L(e){e.forEach((function(t){c(Number.isInteger(t)&&t>=0,(function(){return"Tensor must have a shape comprised of positive integers but got shape ["+e+"]."}))}))}function z(e){return e&&e.then&&"function"==typeof e.then}var O,K=function(){function e(e){this.global=e,this.flags={},this.flagRegistry={},this.urlFlags={},this.populateURLFlags()}return e.prototype.setPlatform=function(e,t){null!=this.platform&&console.warn("Platform "+this.platformName+" has already been set. Overwriting the platform with "+t+"."),this.platformName=e,this.platform=t},e.prototype.registerFlag=function(e,t,n){if(this.flagRegistry[e]={evaluationFn:t,setHook:n},null!=this.urlFlags[e]){var r=this.urlFlags[e];console.warn("Setting feature override from URL "+e+": "+r+"."),this.set(e,r)}},e.prototype.getAsync=function(e){return r(this,void 0,void 0,(function(){var t,n;return a(this,(function(r){switch(r.label){case 0:return e in this.flags?[2,this.flags[e]]:(t=this.flags,n=e,[4,this.evaluateFlag(e)]);case 1:return t[n]=r.sent(),[2,this.flags[e]]}}))}))},e.prototype.get=function(e){if(e in this.flags)return this.flags[e];var t=this.evaluateFlag(e);if(z(t))throw new Error("Flag "+e+" cannot be synchronously evaluated. Please use getAsync() instead.");return this.flags[e]=t,this.flags[e]},e.prototype.getNumber=function(e){return this.get(e)},e.prototype.getBool=function(e){return this.get(e)},e.prototype.getFlags=function(){return this.flags},Object.defineProperty(e.prototype,"features",{get:function(){return this.flags},enumerable:!0,configurable:!0}),e.prototype.set=function(e,t){if(null==this.flagRegistry[e])throw new Error("Cannot set flag "+e+" as it has not been registered.");this.flags[e]=t,null!=this.flagRegistry[e].setHook&&this.flagRegistry[e].setHook(t)},e.prototype.evaluateFlag=function(e){if(null==this.flagRegistry[e])throw new Error("Cannot evaluate flag '"+e+"': no evaluation function found.");return this.flagRegistry[e].evaluationFn()},e.prototype.setFlags=function(e){this.flags=Object.assign({},e)},e.prototype.reset=function(){this.flags={},this.urlFlags={},this.populateURLFlags()},e.prototype.populateURLFlags=function(){var e=this;if(void 0!==this.global&&void 0!==this.global.location&&void 0!==this.global.location.search){var t,n,r=(t=this.global.location.search,n={},t.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,(function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return W(n,t[0],t[1]),t.join("=")})),n);if("tfjsflags"in r)r.tfjsflags.split(",").forEach((function(t){var n=t.split(":"),r=n[0],a=n[1];e.urlFlags[r]=function(e,t){if("true"===(t=t.toLowerCase())||"false"===t)return"true"===t;if(""+ +t===t)return+t;throw new Error("Could not parse value flag value "+t+" for flag "+e+".")}(r,a)}))}},e}();function W(e,t,n){e[decodeURIComponent(t)]=decodeURIComponent(n||"")}function q(){return e.ENV}function U(){if(null==O){var e=void 0;if("undefined"!=typeof window)e=window;else if("undefined"!=typeof global)e=global;else if("undefined"!=typeof process)e=process;else{if("undefined"==typeof self)throw new Error("Could not find a global object");e=self}O=e}return O}function G(e,t){var n,r=(null==(n=U())._tfGlobals&&(n._tfGlobals=new Map),n._tfGlobals);if(r.has(e))return r.get(e);var a=t();return r.set(e,a),r.get(e)}e.ENV=null;var V=G("kernelRegistry",(function(){return new Map})),H=G("gradRegistry",(function(){return new Map}));function j(e,t){var n=X(e,t);return V.get(n)}function J(e){return H.get(e)}function Y(e){for(var t=V.entries(),n=[];;){var r=t.next(),a=r.done,o=r.value;if(a)break;var i=o[0],s=o[1];i.split("_")[0]===e&&n.push(s)}return n}function Z(e){var t=e.kernelName,n=e.backendName,r=X(t,n);V.has(r)&&console.warn("The kernel '"+t+"' for backend '"+n+"' is already registered"),V.set(r,e)}function X(e,t){return t+"_"+e}function $(e,t){if("string"===t)throw new Error("Cannot convert a string[] to a TypedArray");if(Array.isArray(e)&&(e=d(e)),q().getBool("DEBUG")&&x(e,t),function(e,t){return e instanceof Float32Array&&"float32"===t||e instanceof Int32Array&&"int32"===t||e instanceof Uint8Array&&"bool"===t}(e,t))return e;if(null==t||"float32"===t||"complex64"===t)return new Float32Array(e);if("int32"===t)return new Int32Array(e);if("bool"===t){for(var n=new Uint8Array(e.length),r=0;r<n.length;++r)0!==Math.round(e[r])&&(n[r]=1);return n}throw new Error("Unknown data type "+t)}function Q(){return q().platform.now()}function ee(e,t){return void 0===t&&(t="utf-8"),t=t||"utf-8",q().platform.encode(e,t)}function te(e,t){return void 0===t&&(t="utf-8"),t=t||"utf-8",q().platform.decode(e,t)}var ne={__proto__:null,createScalarValue:function(e,t){return"string"===t?ee(e):$([e],t)},toTypedArray:$,now:Q,fetch:function(e,t){return q().platform.fetch(e,t)},encodeString:ee,decodeString:te,shuffle:u,shuffleCombo:function(e,t){if(e.length!==t.length)throw new Error("Array sizes must match to be shuffled together First array length was "+e.length+"Second array length was "+t.length);for(var n,r,a=e.length,o=0;a>0;)o=Math.random()*a|0,n=e[--a],r=t[a],e[a]=e[o],t[a]=t[o],e[o]=n,t[o]=r},clamp:l,nearestLargerEven:function(e){return e%2==0?e:e+1},sum:function(e){for(var t=0,n=0;n<e.length;n++)t+=e[n];return t},randUniform:function(e,t){var n=Math.random();return t*n+(1-n)*e},distSquared:function(e,t){for(var n=0,r=0;r<e.length;r++){var a=Number(e[r])-Number(t[r]);n+=a*a}return n},assert:c,assertShapesMatch:f,assertNonNull:h,flatten:d,sizeFromShape:p,isScalarShape:function(e){return 0===e.length},arraysEqual:v,isInt:m,tanh:function(e){if(null!=Math.tanh)return Math.tanh(e);if(e===1/0)return 1;if(e===-1/0)return-1;var t=Math.exp(2*e);return(t-1)/(t+1)},sizeToSquarishShape:function(e){var t=Math.ceil(Math.sqrt(e));return[t,Math.ceil(e/t)]},createShuffledIndices:function(e){for(var t=new Uint32Array(e),n=0;n<e;++n)t[n]=n;return u(t),t},rightPad:g,repeatedTry:function(e,t,n){return void 0===t&&(t=function(e){return 0}),new Promise((function(r,a){var o=0,i=function(){if(e())r();else{o++;var s=t(o);null!=n&&o>=n?a():setTimeout(i,s)}};i()}))},inferFromImplicitShape:function(e,t){for(var n=1,r=-1,a=0;a<e.length;++a)if(e[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 o=e.slice();return o[r]=t/n,o},parseAxisParam:y,squeezeShape:b,getTypedArrayFromDType:w,getArrayFromDType:k,checkConversionForErrors:x,isValidDtype:E,hasEncodingLoss:function(e,t){return"complex64"!==t&&(("float32"!==t||"complex64"===e)&&(("int32"!==t||"float32"===e||"complex64"===e)&&("bool"!==t||"bool"!==e)))},isTypedArray:S,bytesPerElement:A,bytesFromStringArray:M,isString:N,isBoolean:I,isNumber:_,inferDtype:T,isFunction:D,nearestDivisor:R,computeStrides:C,toNestedArray:F,makeOnesTypedArray:B,makeZerosTypedArray:P,makeZerosNestedTypedArray:function(e,t){var n=e.reduce((function(e,t){return e*t}),1);if(null==t||"float32"===t)return F(e,new Float32Array(n));if("int32"===t)return F(e,new Int32Array(n));if("bool"===t)return F(e,new Uint8Array(n));throw new Error("Unknown data type "+t)},assertNonNegativeIntegerDimensions:L,locToIndex:function(e,t,n){if(0===t)return 0;if(1===t)return e[0];for(var r=e[e.length-1],a=0;a<e.length-1;++a)r+=n[a]*e[a];return r},indexToLoc:function(e,t,n){if(0===t)return[];if(1===t)return[e];for(var r=new Array(t),a=0;a<r.length-1;++a)r[a]=Math.floor(e/n[a]),e-=r[a]*n[a];return r[r.length-1]=e,r},isPromise:z},re=function(){function e(e,t){this.backendTimer=e,this.logger=t,null==t&&(this.logger=new ae)}return e.prototype.profileKernel=function(e,t,n){var r,a,o=function(){r=n()},i=Q();if(this.backendTimer.timerAvailable())a=this.backendTimer.time(o);else{o();for(var s=0,u=r;s<u.length;s++){u[s].dataSync()}a=Promise.resolve({kernelMs:Q()-i})}if(q().getBool("CHECK_COMPUTATION_FOR_ERRORS"))for(var l=function(t){var n=r[t];n.data().then((function(t){!function(e,t,n){if("float32"!==t)return!1;for(var r=0;r<e.length;r++){var a=e[r];if(isNaN(a)||!isFinite(a))return console.warn("Found "+a+" in the result of '"+n+"'"),!0}}(t,n.dtype,e)}))},c=0;c<r.length;c++)l(c);return{kernelName:e,outputs:r,inputs:t,timeMs:a.then((function(e){return e.kernelMs})),extraInfo:a.then((function(e){return null!=e.getExtraProfileInfo?e.getExtraProfileInfo():""}))}},e.prototype.logKernelProfile=function(e){var t=this,n=e.kernelName,r=e.outputs,a=e.timeMs,o=e.inputs,i=e.extraInfo;r.forEach((function(e){Promise.all([e.data(),a,i]).then((function(r){t.logger.logKernelProfile(n,e,r[0],r[1],o,r[2])}))}))},e}();var ae=function(){function e(){}return e.prototype.logKernelProfile=function(e,t,n,r,a,o){var i="number"==typeof r?g(r+"ms",9):r.error,s=g(e,25),u=t.rank,l=t.size,c=g(t.shape.toString(),14),f="";for(var h in a){var d=a[h];if(null!=d){var p=d.shape||t.shape,v=p.length;f+=h+": "+v+"D "+(v>0?p:"")+" "}}console.log("%c"+s+"\t%c"+i+"\t%c"+u+"D "+c+"\t%c"+l+"\t%c"+f+"\t%c"+o,"font-weight:bold","color:red","color:blue","color: orange","color: green","color: steelblue")},e}();function oe(e,t,n,r){var a=C(t),o=function(e,t,n,r){var a=p(t),o=r[r.length-1],i=new Array(o).fill(0),s=t.length,u="complex64"===n?ue(e):e;if(s>1)for(var l=0;l<a/o;l++)for(var c=l*o,f=0;f<o;f++)i[f]=Math.max(i[f],ie(u[c+f],0,n).length);return i}(e,t,n,a),i=t.length,s=function e(t,n,r,a,o,i){void 0===i&&(i=!0);var s="complex64"===r?2:1,u=n[0],l=n.length;if(0===l){return"complex64"===r?[ie(ue(t)[0],0,r)]:"bool"===r?[se(t[0])]:[t[0].toString()]}if(1===l){if(u>20){var c=3*s,f=Array.from(t.slice(0,c)),h=Array.from(t.slice((u-3)*s,u*s));return"complex64"===r&&(f=ue(f),h=ue(h)),["["+f.map((function(e,t){return ie(e,o[t],r)})).join(", ")+", ..., "+h.map((function(e,t){return ie(e,o[u-3+t],r)})).join(", ")+"]"]}return["["+("complex64"===r?ue(t):Array.from(t)).map((function(e,t){return ie(e,o[t],r)})).join(", ")+"]"]}var d=n.slice(1),p=a.slice(1),v=a[0]*s,m=[];if(u>20){for(var g=0;g<3;g++){var y=(b=g*v)+v;m.push.apply(m,e(t.slice(b,y),d,r,p,o,!1))}m.push("...");for(g=u-3;g<u;g++){y=(b=g*v)+v;m.push.apply(m,e(t.slice(b,y),d,r,p,o,g===u-1))}}else for(g=0;g<u;g++){var b;y=(b=g*v)+v;m.push.apply(m,e(t.slice(b,y),d,r,p,o,g===u-1))}var w=2===l?",":"";m[0]="["+m[0]+w;for(g=1;g<m.length-1;g++)m[g]=" "+m[g]+w;var k=",\n";for(g=2;g<l;g++)k+="\n";return m[m.length-1]=" "+m[m.length-1]+"]"+(i?"":k),m}(e,t,n,a,o),u=["Tensor"];return r&&(u.push(" dtype: "+n),u.push(" rank: "+i),u.push(" shape: ["+t+"]"),u.push(" values:")),u.push(s.map((function(e){return" "+e})).join("\n")),u.join("\n")}function ie(e,t,n){return g(Array.isArray(e)?parseFloat(e[0].toFixed(7))+" + "+parseFloat(e[1].toFixed(7))+"j":N(e)?"'"+e+"'":"bool"===n?se(e):parseFloat(e.toFixed(7)).toString(),t)}function se(e){return 0===e?"false":"true"}function ue(e){for(var t=[],n=0;n<e.length;n+=2)t.push([e[n],e[n+1]]);return t}var le=function(){function e(e,t,n){var r=this;if(this.dtype=t,this.shape=e.slice(),this.size=p(e),null!=n){var a=n.length;c(a===this.size,(function(){return"Length of values '"+a+"' does not match the size inferred by the shape '"+r.size+"'."}))}if("complex64"===t)throw new Error("complex64 dtype TensorBuffers are not supported. Please create a TensorBuffer for the real and imaginary parts separately and call tf.complex(real, imag).");this.values=n||k(t,this.size),this.strides=C(e)}return e.prototype.set=function(e){for(var t=this,n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];0===n.length&&(n=[0]),c(n.length===this.rank,(function(){return"The number of provided coordinates ("+n.length+") must match the rank ("+t.rank+")"}));var a=this.locToIndex(n);this.values[a]=e},e.prototype.get=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];0===e.length&&(e=[0]);for(var n=0,r=0,a=e;r<a.length;r++){var o=a[r];if(o<0||o>=this.shape[n]){var i="Requested out of range element at "+e+". Buffer shape="+this.shape;throw new Error(i)}n++}for(var s=e[e.length-1],u=0;u<e.length-1;++u)s+=this.strides[u]*e[u];return this.values[s]},e.prototype.locToIndex=function(e){if(0===this.rank)return 0;if(1===this.rank)return e[0];for(var t=e[e.length-1],n=0;n<e.length-1;++n)t+=this.strides[n]*e[n];return t},e.prototype.indexToLoc=function(e){if(0===this.rank)return[];if(1===this.rank)return[e];for(var t=new Array(this.shape.length),n=0;n<t.length-1;++n)t[n]=Math.floor(e/this.strides[n]),e-=t[n]*this.strides[n];return t[t.length-1]=e,t},Object.defineProperty(e.prototype,"rank",{get:function(){return this.shape.length},enumerable:!0,configurable:!0}),e.prototype.toTensor=function(){return ce().makeTensor(this.values,this.shape,this.dtype)},e}(),ce=null,fe=null;var he=function(){function e(e,t,n,r){this.kept=!1,this.isDisposedInternal=!1,this.shape=e.slice(),this.dtype=t||"float32",this.size=p(e),this.strides=C(e),this.dataId=n,this.id=r,this.rankType=this.rank<5?this.rank.toString():"higher"}return Object.defineProperty(e.prototype,"rank",{get:function(){return this.shape.length},enumerable:!0,configurable:!0}),e.prototype.buffer=function(){return r(this,void 0,void 0,(function(){var e;return a(this,(function(t){switch(t.label){case 0:return[4,this.data()];case 1:return e=t.sent(),[2,fe.buffer(this.shape,this.dtype,e)]}}))}))},e.prototype.bufferSync=function(){return fe.buffer(this.shape,this.dtype,this.dataSync())},e.prototype.array=function(){return r(this,void 0,void 0,(function(){var e;return a(this,(function(t){switch(t.label){case 0:return[4,this.data()];case 1:return e=t.sent(),[2,F(this.shape,e)]}}))}))},e.prototype.arraySync=function(){return F(this.shape,this.dataSync())},e.prototype.data=function(){return r(this,void 0,void 0,(function(){var e,t;return a(this,(function(n){switch(n.label){case 0:return this.throwIfDisposed(),e=ce().read(this.dataId),"string"!==this.dtype?[3,2]:[4,e];case 1:t=n.sent();try{return[2,t.map((function(e){return te(e)}))]}catch(e){throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes().")}n.label=2;case 2:return[2,e]}}))}))},e.prototype.dataSync=function(){this.throwIfDisposed();var e=ce().readSync(this.dataId);if("string"===this.dtype)try{return e.map((function(e){return te(e)}))}catch(e){throw new Error("Failed to decode the string bytes into utf-8. To get the original bytes, call tensor.bytes().")}return e},e.prototype.bytes=function(){return r(this,void 0,void 0,(function(){var e;return a(this,(function(t){switch(t.label){case 0:return this.throwIfDisposed(),[4,ce().read(this.dataId)];case 1:return e=t.sent(),"string"===this.dtype?[2,e]:[2,new Uint8Array(e.buffer)]}}))}))},e.prototype.dispose=function(){this.isDisposed||(ce().disposeTensor(this),this.isDisposedInternal=!0)},Object.defineProperty(e.prototype,"isDisposed",{get:function(){return this.isDisposedInternal},enumerable:!0,configurable:!0}),e.prototype.throwIfDisposed=function(){if(this.isDisposed)throw new Error("Tensor is disposed.")},e.prototype.print=function(e){return void 0===e&&(e=!1),fe.print(this,e)},e.prototype.clone=function(){return this.throwIfDisposed(),fe.clone(this)},e.prototype.toString=function(e){return void 0===e&&(e=!1),oe(this.dataSync(),this.shape,this.dtype,e)},e.prototype.cast=function(e){return this.throwIfDisposed(),fe.cast(this,e)},e.prototype.variable=function(e,t,n){return void 0===e&&(e=!0),this.throwIfDisposed(),ce().makeVariable(this,e,t,n)},e}();Object.defineProperty(he,Symbol.hasInstance,{value:function(e){return!!e&&null!=e.data&&null!=e.dataSync&&null!=e.throwIfDisposed}}),G("Tensor",(function(){return he}));var de,pe,ve,me,ge,ye=function(e){function t(t,n,r,a){var o=e.call(this,t.shape,t.dtype,t.dataId,a)||this;return o.trainable=n,o.name=r,o}return n(t,e),t.prototype.assign=function(e){if(e.dtype!==this.dtype)throw new Error("dtype of the new value ("+e.dtype+") and previous value ("+this.dtype+") must match");if(!v(e.shape,this.shape))throw new Error("shape of the new value ("+e.shape+") and previous value ("+this.shape+") must match");ce().disposeTensor(this),this.dataId=e.dataId,ce().incRef(this,null)},t.prototype.dispose=function(){ce().disposeVariable(this),this.isDisposedInternal=!0},t}(he);Object.defineProperty(ye,Symbol.hasInstance,{value:function(e){return e instanceof he&&null!=e.assign&&e.assign instanceof Function}}),(de=e.Rank||(e.Rank={})).R0="R0",de.R1="R1",de.R2="R2",de.R3="R3",de.R4="R4",de.R5="R5",de.R6="R6",function(e){e.float32="float32",e.int32="int32",e.bool="int32",e.complex64="complex64"}(pe||(pe={})),function(e){e.float32="float32",e.int32="int32",e.bool="bool",e.complex64="complex64"}(ve||(ve={})),function(e){e.float32="float32",e.int32="float32",e.bool="float32",e.complex64="complex64"}(me||(me={})),function(e){e.float32="complex64",e.int32="complex64",e.bool="complex64",e.complex64="complex64"}(ge||(ge={}));var be={float32:me,int32:pe,bool:ve,complex64:ge};function we(e,t){if("string"===e||"string"===t){if("string"===e&&"string"===t)return"string";throw new Error("Can not upcast "+e+" with "+t)}return be[e][t]}function ke(e,t){if(e.dtype===t.dtype)return[e,t];var n=we(e.dtype,t.dtype);return[e.cast(n),t.cast(n)]}function xe(e,t){c(e.dtype===t.dtype,(function(){return"The dtypes of the first("+e.dtype+") and second("+t.dtype+") input must match"}))}function Ee(e){var t=[];return function e(t,n,r){if(null==t)return;if(t instanceof he)return void n.push(t);if(a=t,!Array.isArray(a)&&"object"!=typeof a)return;var a;var o=t;for(var i in o){var s=o[i];r.has(s)||(r.add(s),e(s,n,r))}}(e,t,new Set),t}var Se={__proto__:null,makeTypesMatch:ke,assertTypesMatch:xe,isTensorInList:function(e,t){return t.some((function(t){return t.id===e.id}))},getTensorsInContainer:Ee};function Ae(e){return null!=e.kernelName}var Me=function(){function e(){this.registeredVariables={},this.nextTapeNodeId=0,this.numBytes=0,this.numTensors=0,this.numStringTensors=0,this.numDataBuffers=0,this.gradientDepth=0,this.kernelDepth=0,this.scopeStack=[],this.numDataMovesStack=[],this.nextScopeId=0,this.tensorInfo=new WeakMap,this.profiling=!1,this.activeProfile={newBytes:0,newTensors:0,peakBytes:0,kernels:[],result:null,get kernelNames(){return Array.from(new Set(this.kernels.map((function(e){return e.name}))))}}}return e.prototype.dispose=function(){for(var e in this.registeredVariables)this.registeredVariables[e].dispose()},e}(),Ne=function(){function e(e){this.ENV=e,this.registry={},this.registryFactory={},this.pendingBackendInitId=0,this.state=new Me}return e.prototype.ready=function(){return r(this,void 0,void 0,(function(){var e,t,n;return a(this,(function(r){switch(r.label){case 0:if(null!=this.pendingBackendInit)return[2,this.pendingBackendInit.then((function(){}))];if(null!=this.backendInstance)return[2];e=this.getSortedBackends(),t=0,r.label=1;case 1:return t<e.length?(n=e[t],[4,this.initializeBackend(n).success]):[3,5];case 2:return r.sent()?[4,this.setBackend(n)]:[3,4];case 3:return r.sent(),[2];case 4:return t++,[3,1];case 5:throw new Error("Could not initialize any backends, all backend initializations failed.")}}))}))},Object.defineProperty(e.prototype,"backend",{get:function(){if(null!=this.pendingBackendInit)throw new Error("Backend '"+this.backendName+"' has not yet been initialized. Make sure to await tf.ready() or await tf.setBackend() before calling other methods");if(null==this.backendInstance){var e=this.initializeBackendsAndReturnBest(),t=e.name;if(e.asyncInit)throw new Error("The highest priority backend '"+t+"' has not yet been initialized. Make sure to await tf.ready() or await tf.setBackend() before calling other methods");this.setBackend(t)}return this.backendInstance},enumerable:!0,configurable:!0}),e.prototype.backendNames=function(){return Object.keys(this.registryFactory)},e.prototype.findBackend=function(e){if(!(e in this.registry)){if(!(e in this.registryFactory))return null;if(this.initializeBackend(e).asyncInit)return null}return this.registry[e]},e.prototype.findBackendFactory=function(e){return e in this.registryFactory?this.registryFactory[e].factory:null},e.prototype.registerBackend=function(e,t,n){return void 0===n&&(n=1),e in this.registryFactory?(console.warn(e+" backend was already registered. Reusing existing backend factory."),!1):(this.registryFactory[e]={factory:t,priority:n},!0)},e.prototype.setBackend=function(e){return r(this,void 0,void 0,(function(){var t,n,r;return a(this,(function(a){switch(a.label){case 0:if(null==this.registryFactory[e])throw new Error("Backend name '"+e+"' not found in registry");return this.backendName=e,null!=this.registry[e]?[3,4]:(this.backendInstance=null,t=this.initializeBackend(e),n=t.success,t.asyncInit?[4,n]:[3,2]);case 1:return r=a.sent(),[3,3];case 2:r=n,a.label=3;case 3:if(!r)return[2,!1];a.label=4;case 4:return this.backendInstance=this.registry[e],this.setupRegisteredKernels(),this.profiler=new re(this.backendInstance),[2,!0]}}))}))},e.prototype.setupRegisteredKernels=function(){var e=this;Y(this.backendName).forEach((function(t){null!=t.setupFunc&&t.setupFunc(e.backendInstance)}))},e.prototype.disposeRegisteredKernels=function(e){var t=this;Y(e).forEach((function(n){null!=n.disposeFunc&&n.disposeFunc(t.registry[e])}))},e.prototype.initializeBackend=function(e){var t=this,n=this.registryFactory[e];if(null==n)throw new Error("Cannot initialize backend "+e+", no registration found.");try{var r=n.factory();if(!r||r instanceof i||"function"!=typeof r.then)return this.registry[e]=r,{success:!0,asyncInit:!1};var a=++this.pendingBackendInitId,o=r.then((function(n){return!(a<t.pendingBackendInitId)&&(t.registry[e]=n,t.pendingBackendInit=null,!0)})).catch((function(n){return a<t.pendingBackendInitId||(t.pendingBackendInit=null,console.warn("Initialization of backend "+e+" failed"),console.warn(n.stack||n.message)),!1}));return this.pendingBackendInit=o,{success:o,asyncInit:!0}}catch(t){return console.warn("Initialization of backend "+e+" failed"),console.warn(t.stack||t.message),{success:!1,asyncInit:!1}}},e.prototype.removeBackend=function(e){if(!(e in this.registryFactory))throw new Error(e+" backend not found in registry");this.backendName===e&&null!=this.pendingBackendInit&&this.pendingBackendInitId++,e in this.registry&&(this.disposeRegisteredKernels(e),this.registry[e].dispose(),delete this.registry[e]),delete this.registryFactory[e],this.backendName===e&&(this.pendingBackendInit=null,this.backendName=null,this.backendInstance=null)},e.prototype.getSortedBackends=function(){var e=this;if(0===Object.keys(this.registryFactory).length)throw new Error("No backend found in registry.");return Object.keys(this.registryFactory).sort((function(t,n){return e.registryFactory[n].priority-e.registryFactory[t].priority}))},e.prototype.initializeBackendsAndReturnBest=function(){for(var e=this.getSortedBackends(),t=0;t<e.length;t++){var n=e[t],r=this.initializeBackend(n),a=r.success,o=r.asyncInit;if(o||a)return{name:n,asyncInit:o}}throw new Error("Could not initialize any backends, all backend initializations failed.")},e.prototype.moveData=function(e,t){var n=this.state.tensorInfo.get(t),r=n.backend,a=this.readSync(t),o=r.refCount(t);r.disposeData(t,!0),n.backend=e,e.move(t,a,n.shape,n.dtype,o),this.shouldCheckForMemLeaks()&&this.state.numDataMovesStack[this.state.numDataMovesStack.length-1]++},e.prototype.tidy=function(e,t){var n,r=this,a=null;if(null==t){if("function"!=typeof e)throw new Error("Please provide a function to tidy()");t=e}else{if("string"!=typeof e&&!(e instanceof String))throw new Error("When calling with two arguments, the first argument to tidy() must be a string");if("function"!=typeof t)throw new Error("When calling with two arguments, the 2nd argument to tidy() must be a function");a=e}return this.scopedRun((function(){return r.startScope(a)}),(function(){return r.endScope(n)}),(function(){return(n=t())instanceof Promise&&console.error("Cannot return a Promise inside of tidy."),n}))},e.prototype.scopedRun=function(e,t,n){e();try{var r=n();return t(),r}catch(e){throw t(),e}},e.prototype.nextTensorId=function(){return e.nextTensorId++},e.prototype.nextVariableId=function(){return e.nextVariableId++},e.prototype.clone=function(e){var t=_e.runKernel("Identity",{x:e}),n={x:e};return this.addTapeNode(this.state.activeScope.name,n,[t],(function(e){return{x:function(){var t={x:e},n={dtype:"float32"};return _e.runKernel("Cast",t,n)}}}),[],{}),t},e.prototype.runKernel=function(e,t,n){if(!(null!=j(e,this.backendName)))throw new Error("Kernel '"+e+"' not registered for backend '"+this.backendName+"'");return this.runKernelFunc({kernelName:e,inputs:t,attrs:n})},e.prototype.shouldCheckForMemLeaks=function(){return this.ENV.getBool("IS_TEST")},e.prototype.checkKernelForMemLeak=function(e,t,n){var r=this.backend.numDataIds(),a=0;n.forEach((function(e){a+="complex64"===e.dtype?3:1}));var o=this.state.numDataMovesStack[this.state.numDataMovesStack.length-1],i=r-t-a-o;if(i>0)throw new Error("Backend '"+this.backendName+"' has an internal memory leak ("+i+" data ids) after running '"+e+"'")},e.prototype.runKernelFunc=function(e){var t,n,r,a=this,o=[],i=this.isTapeOn(),s=this.state.numBytes,u=this.state.numTensors;this.shouldCheckForMemLeaks()&&this.state.numDataMovesStack.push(0),null==this.backendName&&this.backend;var l=Ae(e)?e.kernelName:null!=this.state.activeScope?this.state.activeScope.name:"";if(Ae(e)){var f=e.kernelName,h=e.inputs,d=e.attrs;null==this.backendName&&this.backend;var p=j(f,this.backendName);c(null!=p,(function(){return"Cannot find registered kernel '"+f+"' for backend '"+a.backendName+"'"})),n=function(){var e=a.backend.numDataIds();r=p.kernelFunc({inputs:h,attrs:d,backend:a.backend});var t=Array.isArray(r)?r:[r];a.shouldCheckForMemLeaks()&&a.checkKernelForMemLeak(f,e,t);var n=t.map((function(e){if(null!=e.rank)return e;var t=e,n=t.dataId,r=t.shape,o=t.dtype;return a.makeTensorFromDataId(n,r,o)}));if(i){var s=a.getTensorsForGradient(f,h,n);o=a.saveTensorsForBackwardMode(s)}return n}}else{var v=e.forwardFunc,m=function(e){i&&(o=e.map((function(e){return a.keep(a.clone(e))})))};n=function(){var e=a.backend.numDataIds();r=a.tidy((function(){return v(a.backend,m)}));var t=Array.isArray(r)?r:[r];return a.shouldCheckForMemLeaks()&&a.checkKernelForMemLeak(l,e,t),t}}var g,y=e.inputs,b=e.attrs,w=Ae(e)?null:e.backwardsFunc;return this.scopedRun((function(){return a.state.kernelDepth++}),(function(){return a.state.kernelDepth--}),(function(){a.ENV.getBool("DEBUG")||a.state.profiling?(g=a.profiler.profileKernel(l,y,(function(){return n()})),a.ENV.getBool("DEBUG")&&a.profiler.logKernelProfile(g),t=g.outputs):t=n()})),i&&this.addTapeNode(l,y,t,w,o,b),this.state.profiling&&this.state.activeProfile.kernels.push({name:l,bytesAdded:this.state.numBytes-s,totalBytesSnapshot:this.state.numBytes,tensorsAdded:this.state.numTensors-u,totalTensorsSnapshot:this.state.numTensors,inputShapes:Object.keys(y).map((function(e){return null!=y[e]?y[e].shape:null})),outputShapes:t.map((function(e){return e.shape})),kernelTimeMs:g.timeMs,extraInfo:g.extraInfo}),Array.isArray(r)?t:t[0]},e.prototype.saveTensorsForBackwardMode=function(e){var t=this;return e.map((function(e){return t.keep(t.clone(e))}))},e.prototype.getTensorsForGradient=function(e,t,n){var r=J(e);if(null!=r){var a=r.inputsToSave||[],o=r.outputsToSave||[],i=void 0;r.saveAllInputs?(c(Array.isArray(t),(function(){return"saveAllInputs is true, expected inputs to be an array."})),i=Object.keys(t).map((function(e){return t[e]}))):i=a.map((function(e){return t[e]}));var s=n.filter((function(e,t){return o[t]}));return i.concat(s)}return[]},e.prototype.makeTensor=function(e,t,n,r){if(null==e)throw new Error("Values passed to engine.makeTensor() are null");n=n||"float32",r=r||this.backend;var a=e;"string"===n&&N(e[0])&&(a=e.map((function(e){return ee(e)})));var o=r.write(a,t,n),i=new he(t,n,o,this.nextTensorId());if(this.trackTensor(i,r),"string"===n){var s=this.state.tensorInfo.get(o),u=M(a);this.state.numBytes+=u-s.bytes,s.bytes=u}return i},e.prototype.makeTensorFromDataId=function(e,t,n,r){var a=new he(t,n=n||"float32",e,this.nextTensorId());return this.trackTensor(a,r),a},e.prototype.makeVariable=function(e,t,n,r){void 0===t&&(t=!0),n=n||this.nextVariableId().toString(),null!=r&&r!==e.dtype&&(e=e.cast(r));var a=new ye(e,t,n,this.nextTensorId());if(null!=this.state.registeredVariables[a.name])throw new Error("Variable with name "+a.name+" was already registered");return this.state.registeredVariables[a.name]=a,this.incRef(a,this.backend),a},e.prototype.trackTensor=function(e,t){this.state.numTensors++,"string"===e.dtype&&this.state.numStringTensors++;var n=0;"complex64"!==e.dtype&&"string"!==e.dtype&&(n=e.size*A(e.dtype)),this.state.numBytes+=n,this.state.tensorInfo.has(e.dataId)||(this.state.numDataBuffers++,this.state.tensorInfo.set(e.dataId,{backend:t||this.backend,dtype:e.dtype,shape:e.shape,bytes:n})),e instanceof ye||this.track(e)},e.prototype.incRef=function(e,t){this.trackTensor(e,t),this.backend.incRef(e.dataId)},e.prototype.removeDataId=function(e,t){this.state.tensorInfo.has(e)&&this.state.tensorInfo.get(e).backend===t&&(this.state.tensorInfo.delete(e),this.state.numDataBuffers--)},e.prototype.disposeTensor=function(e){if(this.state.tensorInfo.has(e.dataId)){var t=this.state.tensorInfo.get(e.dataId);if(this.state.numTensors--,"string"===e.dtype&&(this.state.numStringTensors--,this.state.numBytes-=t.bytes),"complex64"!==e.dtype&&"string"!==e.dtype){var n=e.size*A(e.dtype);this.state.numBytes-=n}t.backend.disposeData(e.dataId)&&this.removeDataId(e.dataId,t.backend)}},e.prototype.disposeVariables=function(){for(var e in this.state.registeredVariables){var t=this.state.registeredVariables[e];this.disposeVariable(t)}},e.prototype.disposeVariable=function(e){this.disposeTensor(e),null!=this.state.registeredVariables[e.name]&&delete this.state.registeredVariables[e.name]},e.prototype.memory=function(){var e=this.backend.memory();return e.numTensors=this.state.numTensors,e.numDataBuffers=this.state.numDataBuffers,e.numBytes=this.state.numBytes,this.state.numStringTensors>0&&(e.unreliable=!0,null==e.reasons&&(e.reasons=[]),e.reasons.push("Memory usage by string tensors is approximate (2 bytes per character)")),e},e.prototype.profile=function(e){return r(this,void 0,void 0,(function(){var t,n,r,o,i,s,u,l;return a(this,(function(a){switch(a.label){case 0:return this.state.profiling=!0,t=this.state.numBytes,n=this.state.numTensors,this.state.activeProfile.kernels=[],r=this.state.activeProfile,[4,e()];case 1:r.result=a.sent(),this.state.profiling=!1,this.state.activeProfile.peakBytes=Math.max.apply(Math,this.state.activeProfile.kernels.map((function(e){return e.totalBytesSnapshot}))),this.state.activeProfile.newBytes=this.state.numBytes-t,this.state.activeProfile.newTensors=this.state.numTensors-n,o=0,i=this.state.activeProfile.kernels,a.label=2;case 2:return o<i.length?(s=i[o],u=s,[4,s.kernelTimeMs]):[3,6];case 3:return u.kernelTimeMs=a.sent(),l=s,[4,s.extraInfo];case 4:l.extraInfo=a.sent(),a.label=5;case 5:return o++,[3,2];case 6:return[2,this.state.activeProfile]}}))}))},e.prototype.isTapeOn=function(){return this.state.gradientDepth>0&&0===this.state.kernelDepth},e.prototype.addTapeNode=function(e,t,n,r,a,o){var i=this,s={id:this.state.nextTapeNodeId++,kernelName:e,inputs:t,outputs:n,saved:a},u=J(e);null!=u&&(r=u.gradFunc),null!=r&&(s.gradient=function(e){return e=e.map((function(e,t){if(null==e){var r=n[t],a=P(r.size,r.dtype);return i.makeTensor(a,r.shape,r.dtype)}return e})),r(e.length>1?e:e[0],a,o)}),this.state.activeTape.push(s)},e.prototype.keep=function(e){return e.kept=!0,e},e.prototype.startTape=function(){0===this.state.gradientDepth&&(this.state.activeTape=[]),this.state.gradientDepth++},e.prototype.endTape=function(){this.state.gradientDepth--},e.prototype.startScope=function(e){var t={track:[],name:"unnamed scope",id:this.state.nextScopeId++};e&&(t.name=e),this.state.scopeStack.push(t),this.state.activeScope=t},e.prototype.endScope=function(e){for(var t=this,n=Ee(e),r=new Set(n.map((function(e){return e.id}))),a=0;a<this.state.activeScope.track.length;a++){var o=this.state.activeScope.track[a];o.kept||r.has(o.id)||o.dispose()}var i=this.state.scopeStack.pop();this.state.activeScope=0===this.state.scopeStack.length?null:this.state.scopeStack[this.state.scopeStack.length-1],n.forEach((function(e){e.kept||e.scopeId!==i.id||t.track(e)}))},e.prototype.gradients=function(e,t,n,r){var a=this;if(void 0===r&&(r=!1),c(t.length>0,(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 a.startTape()}),(function(){return a.endTape()}),(function(){return a.tidy("forward",e)}));c(o instanceof he,(function(){return"The result y returned by f() must be a tensor."}));var i=function(e,t,n){for(var r={},a={},o=0;o<t.length;o++)r[t[o].id]=!0;for(o=0;o<e.length;o++){var i=(p=e[o]).inputs;for(var s in i){for(var u=i[s],l=!1,c=0;c<t.length;c++)if(r[u.id]){p.outputs.forEach((function(e){return r[e.id]=!0})),l=!0,a[p.id]=!0;break}if(l)break}}var f={};f[n.id]=!0;var h={};for(o=e.length-1;o>=0;o--)for(i=(p=e[o]).inputs,c=0;c<p.outputs.length;c++)if(f[p.outputs[c].id]){for(var s in i)f[i[s].id]=!0,h[p.id]=!0;break}var d=[];for(o=0;o<e.length;o++){var p;if(a[(p=e[o]).id]&&h[p.id]){var v={};for(var s in p.inputs){var m=p.inputs[s];r[m.id]&&(v[s]=m)}var g=Object.assign({},p);g.inputs=v,g.outputs=p.outputs,d.push(g)}}return d}(this.state.activeTape,t,o);if(!r&&0===i.length&&t.length>0)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 e,r,s={};s[o.id]=null==n?(e=o.shape,r=B(p(e),"float32"),_e.makeTensor(r,e,"float32")):n,function(e,t,n,r){for(var a=function(a){var o=t[a],i=[];if(o.outputs.forEach((function(t){var n=e[t.id];null!=n?i.push(n):i.push(null)})),null==o.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+o.kernelName+".");var s=o.gradient(i),u=function(t){if(!(t in s))throw new Error("Cannot backprop through input "+t+". Available gradients found: "+Object.keys(s)+".");var a=n((function(){return s[t]()}));if("float32"!==a.dtype)throw new Error("Error in gradient for op "+o.kernelName+". The gradient of input "+t+" must have 'float32' dtype, but has '"+a.dtype+"'");var i=o.inputs[t];if(!v(a.shape,i.shape))throw new Error("Error in gradient for op "+o.kernelName+". The gradient of input '"+t+"' has shape '"+a.shape+"', which does not match the shape of the input '"+i.shape+"'");if(null==e[i.id])e[i.id]=a;else{var u=e[i.id];e[i.id]=r(u,a),u.dispose()}};for(var l in o.inputs)u(l)},o=t.length-1;o>=0;o--)a(o)}(s,i,(function(e){return a.tidy(e)}),Te);var u=t.map((function(e){return s[e.id]}));return 0===a.state.gradientDepth&&(a.state.activeTape.forEach((function(e){for(var t=0,n=e.saved;t<n.length;t++){n[t].dispose()}})),a.state.activeTape=null),{value:o,grads:u}}))},e.prototype.customGrad=function(e){var t=this;return c(D(e),(function(){return"The f passed in customGrad(f) must be a function."})),function(){for(var n,r=[],a=0;a<arguments.length;a++)r[a]=arguments[a];c(r.every((function(e){return e instanceof he})),(function(){return"The args passed in customGrad(f)(x1, x2,...) must all be tensors"}));var o={};r.forEach((function(e,t){o[t]=e}));var i=function(t,a){return c((n=e.apply(void 0,r.concat([a]))).value instanceof he,(function(){return"The function f passed in customGrad(f) must return an object where `obj.value` is a tensor"})),c(D(n.gradFunc),(function(){return"The function f passed in customGrad(f) must return an object where `obj.gradFunc` is a function."})),n.value},s=function(e,t){var a=n.gradFunc(e,t),o=Array.isArray(a)?a:[a];c(o.length===r.length,(function(){return"The function f passed in customGrad(f) must return an object where `obj.gradFunc` is a function that returns the same number of tensors as inputs passed to f(...)."})),c(o.every((function(e){return e instanceof he})),(function(){return"The function f passed in customGrad(f) must return an object where `obj.gradFunc` is a function that returns a list of only tensors."}));var i={};return o.forEach((function(e,t){i[t]=function(){return e}})),i};return t.runKernelFunc({forwardFunc:i,backwardsFunc:s,inputs:o})}},e.prototype.readSync=function(e){return this.state.tensorInfo.get(e).backend.readSync(e)},e.prototype.read=function(e){return this.state.tensorInfo.get(e).backend.read(e)},e.prototype.time=function(e){return r(this,void 0,void 0,(function(){var t,n;return a(this,(function(r){switch(r.label){case 0:return t=Q(),[4,this.backend.time(e)];case 1:return(n=r.sent()).wallMs=Q()-t,[2,n]}}))}))},e.prototype.track=function(e){return null!=this.state.activeScope&&(e.scopeId=this.state.activeScope.id,this.state.activeScope.track.push(e)),e},Object.defineProperty(e.prototype,"registeredVariables",{get:function(){return this.state.registeredVariables},enumerable:!0,configurable:!0}),e.prototype.reset=function(){for(var e in this.pendingBackendInitId++,this.state.dispose(),this.ENV.reset(),this.state=new Me,this.registry)this.disposeRegisteredKernels(e),this.registry[e].dispose(),delete this.registry[e];this.backendName=null,this.backendInstance=null,this.pendingBackendInit=null},e.nextTensorId=0,e.nextVariableId=0,e}();function Ie(){var t=U();if(null==t._tfengine){var n=new K(t);t._tfengine=new Ne(n)}return function(t){e.ENV=t}(t._tfengine.ENV),ce=function(){return t._tfengine},t._tfengine}var _e=Ie();function Te(e,t){var n={a:e,b:t};return _e.runKernel("Add",n)}function De(){return"undefined"!=typeof window&&null!=window.document||"undefined"!=typeof WorkerGlobalScope}var Re={__proto__:null,isMobile:function(){if("undefined"!=typeof navigator&&null!=navigator){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))}return!1},isBrowser:De},Ce=q();function Fe(e,t){var n=e;if(S(e))return"string"===t?[]:[e.length];if(!Array.isArray(e))return[];for(var r=[];Array.isArray(n)||S(n)&&"string"!==t;)r.push(n.length),n=n[0];return Array.isArray(e)&&q().getBool("TENSORLIKE_CHECK_SHAPE_CONSISTENCY")&&function e(t,n,r){if(r=r||[],!Array.isArray(t)&&!S(t))return void c(0===n.length,(function(){return"Element arr["+r.join("][")+"] is a primitive, but should be an array/TypedArray of "+n[0]+" elements"}));c(n.length>0,(function(){return"Element arr["+r.join("][")+"] should be a primitive, but is an array of "+t.length+" elements"})),c(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),o=0;o<t.length;++o)e(t[o],a,r.concat(o))}(e,r,[]),r}function Be(e,t,n,r){if("string_or_numeric"!==e){if(null==e)throw new Error("Expected dtype cannot be null.");if("numeric"!==e&&e!==t||"numeric"===e&&"string"===t)throw new Error("Argument '"+n+"' passed to '"+r+"' must be "+e+" tensor, but got "+t+" tensor")}}function Pe(e,t,n,r){if(void 0===r&&(r="numeric"),e instanceof he)return Be(r,e.dtype,t,n),e;var a=T(e);if("string"!==a&&["bool","int32","float32"].indexOf(r)>=0&&(a=r),Be(r,a,t,n),null==e||!S(e)&&!Array.isArray(e)&&"number"!=typeof e&&"boolean"!=typeof e&&"string"!=typeof e){var o=null==e?"null":e.constructor.name;throw new Error("Argument '"+t+"' passed to '"+n+"' must be a Tensor or TensorLike, but got '"+o+"'")}var i=Fe(e,a);S(e)||Array.isArray(e)||(e=[e]);var s="string"!==a?$(e,a):d(e,[],!0);return _e.makeTensor(s,i,a)}function Le(e,t,n,r){if(void 0===r&&(r="numeric"),!Array.isArray(e))throw new Error("Argument "+t+" passed to "+n+" must be a `Tensor[]` or `TensorLike[]`");return e.map((function(e,a){return Pe(e,t+"["+a+"]",n,r)}))}Ce.registerFlag("DEBUG",(function(){return!1}),(function(e){e&&console.warn("Debugging mode is ON. The output of every math call w