@tensorflow/tfjs-core
Version:
Hardware-accelerated JavaScript library for machine intelligence
17 lines • 334 kB
JavaScript
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.tf=t.tf||{})}(this,function(t){"use strict";var e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};function n(t,n){function r(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}var r=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t};function o(t,e,n,r){return new(n||(n=Promise))(function(o,a){function i(t){try{s(r.next(t))}catch(t){a(t)}}function u(t){try{s(r.throw(t))}catch(t){a(t)}}function s(t){t.done?o(t.value):new n(function(e){e(t.value)}).then(i,u)}s((r=r.apply(t,e||[])).next())})}function a(t,e){var n,r,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function u(a){return function(u){return function(a){if(n)throw new TypeError("Generator is already executing.");for(;i;)try{if(n=1,r&&(o=2&a[0]?r.return:a[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,a[1])).done)return o;switch(r=0,o&&(a=[2&a[0],o.value]),a[0]){case 0:case 1:o=a;break;case 4:return i.label++,{value:a[1],done:!1};case 5:i.label++,r=a[1],a=[0];continue;case 7:a=i.ops.pop(),i.trys.pop();continue;default:if(!(o=(o=i.trys).length>0&&o[o.length-1])&&(6===a[0]||2===a[0])){i=0;continue}if(3===a[0]&&(!o||a[1]>o[0]&&a[1]<o[3])){i.label=a[1];break}if(6===a[0]&&i.label<o[1]){i.label=o[1],o=a;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(a);break}o[2]&&i.ops.pop(),i.trys.pop();continue}a=e.call(t,i)}catch(t){a=[6,t],r=0}finally{n=o=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,u])}}}function i(t){for(var e=t.length,n=0,r=0;e>0;)r=Math.random()*e|0,n=t[--e],t[e]=t[r],t[r]=n}function u(t,e,n){return Math.max(t,Math.min(e,n))}function s(t,e){return Math.random()*(e-t)+t}function l(t,e){if(!t)throw new Error("string"==typeof e?e:e())}function c(t,e,n){void 0===n&&(n=""),l(v(t,e),n+" Shapes "+t+" and "+e+" must match")}function h(t){l(null!=t,"The input to the tensor constructor must be a non-null value.")}function f(t,e){if(void 0===e&&(e=[]),Array.isArray(t))for(var n=0;n<t.length;++n)f(t[n],e);else e.push(t);return e}function p(t){var e=t;if(T(t))return[t.length];if(!Array.isArray(t))return[];for(var n=[];e instanceof Array;)n.push(e.length),e=e[0];return t instanceof Array&&function t(e,n,r){r=r||[];if(!(e instanceof Array))return void l(0===n.length,function(){return"Element arr["+r.join("][")+"] is a primitive, but should be an array of "+n[0]+" elements"});l(n.length>0,function(){return"Element arr["+r.join("][")+"] should be a primitive, but is an array of "+e.length+" elements"});l(e.length===n[0],function(){return"Element arr["+r.join("][")+"] should have "+n[0]+" elements, but has "+e.length+" elements"});var o=n.slice(1);for(var a=0;a<e.length;++a)t(e[a],o,r.concat(a))}(t,n,[]),n}function d(t){if(0===t.length)return 1;for(var e=t[0],n=1;n<t.length;n++)e*=t[n];return e}function v(t,e){if(t.length!==e.length)return!1;for(var n=0;n<t.length;n++)if(t[n]!==e[n])return!1;return!0}function m(t){return t%1==0}function g(t){if(null!=Math.tanh)return Math.tanh(t);if(t===1/0)return 1;if(t===-1/0)return-1;var e=Math.exp(2*t);return(e-1)/(e+1)}function y(t){for(var e=Math.floor(Math.sqrt(t));e>1;--e)if(t%e==0)return[e,t/e];return[1,t]}function x(t,e){return e<=t.length?t:t+" ".repeat(e-t.length)}function b(t,e,n){return void 0===e&&(e=function(t){return 0}),new Promise(function(r,o){var a=0,i=function(){if(t())r();else{var u=e(++a);null!=n&&a>=n?o():setTimeout(i,u)}};i()})}function w(t,e){for(var n=1,r=-1,o=0;o<t.length;++o)if(t[o]>=0)n*=t[o];else if(-1===t[o]){if(-1!==r)throw Error("Shapes can only have 1 implicit size. Found -1 at dim "+r+" and dim "+o);r=o}else if(t[o]<0)throw Error("Shapes can not be < 0. Found "+t[o]+" at dim "+o);if(-1===r){if(e>0&&e!==n)throw Error("Size("+e+") must match the product of shape "+t);return t}if(0===n)throw Error("Cannot infer the missing size in ["+t+"] when there are 0 elements");if(e%n!=0)throw Error("The implicit shape can't be a fractional number. Got "+e+" / "+n);var a=t.slice();return a[r]=e/n,a}function E(t,e){for(var n=[],r=[],o=0,a=0;a<t.length;++a){if(null!=e){if(e[o]===a&&1!==t[a])throw new Error("Can't squeeze axis "+a+" since its dim '"+t[a]+"' is not 1");(null==e[o]||e[o]>a)&&1===t[a]&&(n.push(t[a]),r.push(a)),e[o]<=a&&o++}1!==t[a]&&(n.push(t[a]),r.push(a))}return{newShape:n,keptDims:r}}function S(t,e){var n=null;if(null==t||"float32"===t)n=new Float32Array(e);else if("int32"===t)n=new Int32Array(e);else{if("bool"!==t)throw new Error("Unknown data type "+t);n=new Uint8Array(e)}return n}function R(t,e,n){if("float32"===e)for(var r=0;r<t.length;r++)if(isNaN(t[r]))throw Error("The result of the '"+n+"' has NaNs.")}function C(t,e){if("float32"!==e)for(var n=0;n<t.length;n++)if(isNaN(t[n]))throw Error("NaN is not a valid value for dtype: '"+e+"'.")}function k(t,e){return"float32"!==e&&(("int32"!==e||"float32"===t)&&("bool"!==e||"bool"!==t))}function T(t){return t instanceof Float32Array||t instanceof Int32Array||t instanceof Uint8Array}function I(t){if("float32"===t||"int32"===t)return 4;if("bool"===t)return 1;throw new Error("Unknown dtype "+t)}function A(t){return!!(t&&t.constructor&&t.call&&t.apply)}function D(t,e){for(var n=e;n<t;++n)if(t%n==0)return n;return t}function N(t){var e=t.length;if(e<2)return[];var n=new Array(e-1);n[e-2]=t[e-1];for(var r=e-3;r>=0;--r)n[r]=n[r+1]*t[r+1];return n}function M(t,e,n){return function(t,e){return t instanceof Float32Array&&"float32"===e||t instanceof Int32Array&&"int32"===e||t instanceof Uint8Array&&"bool"===e}(t,e)?t:(Array.isArray(t)&&(t=f(t)),function(t,e,n){if(null==e||"float32"===e)return new Float32Array(t);if("int32"===e)return n&&C(t,e),new Int32Array(t);if("bool"===e){for(var r=new Uint8Array(t.length),o=0;o<r.length;++o)0!==Math.round(t[o])&&(r[o]=1);return r}throw new Error("Unknown data type "+e)}(t,e,n))}function _(t,e){for(var n=O(t,e),r=0;r<n.length;r++)n[r]=1;return n}function O(t,e){if(null==e||"float32"===e)return new Float32Array(t);if("int32"===e)return new Int32Array(t);if("bool"===e)return new Uint8Array(t);throw new Error("Unknown data type "+e)}function F(){if("undefined"!=typeof performance)return performance.now();if("undefined"!=typeof process){var t=process.hrtime();return 1e3*t[0]+t[1]/1e6}throw new Error("Can not measure time in this environment. You should run tf.js in the browser or in Node.js")}var B=Object.freeze({shuffle:i,clamp:u,randUniform:s,distSquared:function(t,e){for(var n=0,r=0;r<t.length;r++){var o=Number(t[r])-Number(e[r]);n+=o*o}return n},assert:l,assertShapesMatch:c,assertNonNull:h,flatten:f,inferShape:p,sizeFromShape:d,isScalarShape:function(t){return 0===t.length},arraysEqual:v,isInt:m,tanh:g,sizeToSquarishShape:y,createShuffledIndices:function(t){for(var e=new Uint32Array(t),n=0;n<t;++n)e[n]=n;return i(e),e},rightPad:x,repeatedTry:b,inferFromImplicitShape:w,squeezeShape:E,getTypedArrayFromDType:S,checkComputationForNaN:R,checkConversionForNaN:C,hasEncodingLoss:k,isTypedArray:T,bytesPerElement:I,isFunction:A,nearestDivisor:D,computeStrides:N,toTypedArray:M,makeOnesTypedArray:_,makeZerosTypedArray:O,now:F}),L=function(){function t(t,e){this.backendTimer=t,this.logger=e,null==e&&(this.logger=new P)}return t.prototype.profileKernel=function(t,e){var n,r=this,o=this.backendTimer.time(function(){n=e()});return(Array.isArray(n)?n:[n]).forEach(function(e){var n=e.dataSync();R(n,e.dtype,t),o.then(function(o){r.logger.logKernelProfile(t,e,n,o.kernelMs)})}),n},t}(),P=function(){function t(){}return t.prototype.logKernelProfile=function(t,e,n,r){var o=x(r+"ms",9),a=x(t,25),i=e.rank,u=e.size,s=x(e.shape.toString(),14);console.log("%c"+a+"\t%c"+o+"\t%c"+i+"D "+s+"\t%c"+u,"font-weight:bold","color:red","color:blue","color: orange")},t}();var U=20,z=3,W=7;function V(t,e,n,r){var o=N(e),a=function(t,e,n){var r=d(e),o=n[n.length-1],a=new Array(o).fill(0);if(e.length>1)for(var i=0;i<r/o;i++)for(var u=i*o,s=0;s<o;s++)a[s]=Math.max(a[s],G(t[u+s],0).length);return a}(t,e,o),i=e.length,u=function t(e,n,r,o,a){void 0===a&&(a=!0);var i=n[0];var u=n.length;if(0===u)return[e[0].toString()];if(1===u){if(i>U){var s=Array.from(e.subarray(0,z)),l=Array.from(e.subarray(i-z,i));return["["+s.map(function(t,e){return G(t,o[e])}).join(", ")+", ..., "+l.map(function(t,e){return G(t,o[i-z+e])}).join(", ")+"]"]}return["["+Array.from(e).map(function(t,e){return G(t,o[e])}).join(", ")+"]"]}var c=n.slice(1);var h=r.slice(1);var f=r[0];var p=[];if(i>U){for(var d=0;d<z;d++){var v=d*f,m=v+f;p.push.apply(p,t(e.subarray(v,m),c,h,o,!1))}p.push("...");for(var d=i-z;d<i;d++){var v=d*f,m=v+f;p.push.apply(p,t(e.subarray(v,m),c,h,o,d===i-1))}}else for(var d=0;d<i;d++){var v=d*f,m=v+f;p.push.apply(p,t(e.subarray(v,m),c,h,o,d===i-1))}var g=2===u?",":"";p[0]="["+p[0]+g;for(var d=1;d<p.length-1;d++)p[d]=" "+p[d]+g;var y=",\n";for(var d=2;d<u;d++)y+="\n";p[p.length-1]=" "+p[p.length-1]+"]"+(a?"":y);return p}(t,e,o,a),s=["Tensor"];return r&&(s.push(" dtype: "+n),s.push(" rank: "+i),s.push(" shape: ["+e+"]"),s.push(" values:")),s.push(u.map(function(t){return" "+t}).join("\n")),s.join("\n")}function G(t,e){return x(parseFloat(t.toFixed(W)).toString(),e)}var q=function(){function t(t,e,n){if(this.dtype=e,null!=n){var r=n.length,o=d(t);l(r===o,"Length of values '"+r+"' does not match the size inferred by the shape '"+o+"'")}this.shape=t.slice(),this.values=n||S(e,d(t)),this.strides=N(t),this.size=d(t)}return t.prototype.set=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];0===e.length&&(e=[0]),l(e.length===this.rank,"The number of provided coordinates ("+e.length+") must match the rank ("+this.rank+")");var r=this.locToIndex(e);this.values[r]=t},t.prototype.get=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];0===t.length&&(t=[0]);for(var n=t[t.length-1],r=0;r<t.length-1;++r)n+=this.strides[r]*t[r];return this.values[n]},t.prototype.locToIndex=function(t){if(0===this.rank)return 0;if(1===this.rank)return t[0];for(var e=t[t.length-1],n=0;n<t.length-1;++n)e+=this.strides[n]*t[n];return e},t.prototype.indexToLoc=function(t){if(0===this.rank)return[];if(1===this.rank)return[t];for(var e=new Array(this.shape.length),n=0;n<e.length-1;++n)e[n]=Math.floor(t/this.strides[n]),t-=e[n]*this.strides[n];return e[e.length-1]=t,e},Object.defineProperty(t.prototype,"rank",{get:function(){return this.shape.length},enumerable:!0,configurable:!0}),t.prototype.toTensor=function(){return X.make(this.shape,{values:this.values},this.dtype)},t}(),H=null,$=null;function j(t){H=t}var X=function(){function t(e,n,r,o){this.isDisposedInternal=!1,this.size=d(e),null!=r&&l(this.size===r.length,"Based on the provided shape, ["+e+"], the tensor should have "+this.size+" values but has "+r.length),this.shape=e.slice(),this.dtype=n||"float32",this.strides=N(e),this.dataId=null!=o?o:{},this.id=t.nextId++,this.rankType=this.rank<5?this.rank.toString():"higher",H().registerTensor(this),null!=r&&H().write(this.dataId,r)}return t.make=function(e,n,r){return new t(e,r,n.values,n.dataId)},t.prototype.flatten=function(){return this.throwIfDisposed(),this.as1D()},t.prototype.asScalar=function(){return this.throwIfDisposed(),l(1===this.size,"The array must have only 1 element."),this.reshape([])},t.prototype.as1D=function(){return this.throwIfDisposed(),this.reshape([this.size])},t.prototype.as2D=function(t,e){return this.throwIfDisposed(),this.reshape([t,e])},t.prototype.as3D=function(t,e,n){return this.throwIfDisposed(),this.reshape([t,e,n])},t.prototype.as4D=function(t,e,n,r){return this.throwIfDisposed(),this.reshape([t,e,n,r])},t.prototype.asType=function(t){return this.throwIfDisposed(),$.cast(this,t)},Object.defineProperty(t.prototype,"rank",{get:function(){return this.shape.length},enumerable:!0,configurable:!0}),t.prototype.get=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];l(t.length===this.rank,"Number of coordinates in get() must match the rank of the tensor"),this.throwIfDisposed(),0===t.length&&(t=[0]);for(var n=t[t.length-1],r=0;r<t.length-1;++r)n+=this.strides[r]*t[r];return this.dataSync()[n]},t.prototype.buffer=function(){return $.buffer(this.shape,this.dtype,this.dataSync())},t.prototype.data=function(){return o(this,void 0,void 0,function(){return a(this,function(t){return this.throwIfDisposed(),[2,H().read(this.dataId)]})})},t.prototype.dataSync=function(){return this.throwIfDisposed(),H().readSync(this.dataId)},t.prototype.dispose=function(){this.isDisposed||(H().disposeTensor(this),this.isDisposedInternal=!0)},Object.defineProperty(t.prototype,"isDisposed",{get:function(){return this.isDisposedInternal},enumerable:!0,configurable:!0}),t.prototype.throwIfDisposed=function(){if(this.isDisposed)throw new Error("Tensor is disposed.")},t.prototype.toFloat=function(){return this.asType("float32")},t.prototype.toInt=function(){return this.asType("int32")},t.prototype.toBool=function(){return this.asType("bool")},t.prototype.print=function(t){return void 0===t&&(t=!1),$.print(this,t)},t.prototype.reshape=function(t){return this.throwIfDisposed(),$.reshape(this,t)},t.prototype.reshapeAs=function(t){return this.throwIfDisposed(),this.reshape(t.shape)},t.prototype.expandDims=function(t){return void 0===t&&(t=0),$.expandDims(this,t)},t.prototype.cumsum=function(t,e,n){return void 0===t&&(t=0),void 0===e&&(e=!1),void 0===n&&(n=!1),$.cumsum(this,t,e,n)},t.prototype.squeeze=function(t){return this.throwIfDisposed(),$.squeeze(this,t)},t.prototype.clone=function(){return this.throwIfDisposed(),$.clone(this)},t.prototype.toString=function(t){return void 0===t&&(t=!1),V(this.dataSync(),this.shape,this.dtype,t)},t.prototype.tile=function(t){return this.throwIfDisposed(),$.tile(this,t)},t.prototype.gather=function(t,e){return void 0===e&&(e=0),this.throwIfDisposed(),$.gather(this,t,e)},t.prototype.matMul=function(t,e,n){return void 0===e&&(e=!1),void 0===n&&(n=!1),this.throwIfDisposed(),$.matMul(this,t,e,n)},t.prototype.dot=function(t){return this.throwIfDisposed(),$.dot(this,t)},t.prototype.norm=function(t,e,n){return void 0===t&&(t="euclidean"),void 0===e&&(e=null),void 0===n&&(n=!1),this.throwIfDisposed(),$.norm(this,t,e,n)},t.prototype.slice=function(t,e){return this.throwIfDisposed(),$.slice(this,t,e)},t.prototype.reverse=function(t){return this.throwIfDisposed(),$.reverse(this,t)},t.prototype.concat=function(t,e){return void 0===e&&(e=0),this.throwIfDisposed(),$.concat([this,t],e)},t.prototype.stack=function(t,e){return void 0===e&&(e=0),$.stack([this,t],e)},t.prototype.unstack=function(t,e){return void 0===e&&(e=0),$.unstack(this,e)},t.prototype.pad=function(t,e){return void 0===e&&(e=0),$.pad(this,t,e)},t.prototype.batchNormalization=function(t,e,n,r,o){return void 0===n&&(n=.001),this.throwIfDisposed(),$.batchNormalization(this,t,e,n,r,o)},t.prototype.all=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),$.all(this,t,e)},t.prototype.any=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),$.any(this,t,e)},t.prototype.logSumExp=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),$.logSumExp(this,t,e)},t.prototype.sum=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),$.sum(this,t,e)},t.prototype.mean=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),$.mean(this,t,e)},t.prototype.min=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),$.min(this,t,e)},t.prototype.max=function(t,e){return void 0===t&&(t=null),void 0===e&&(e=!1),this.throwIfDisposed(),$.max(this,t,e)},t.prototype.argMin=function(t){return void 0===t&&(t=null),this.throwIfDisposed(),$.argMin(this,t)},t.prototype.argMax=function(t){return void 0===t&&(t=null),this.throwIfDisposed(),$.argMax(this,t)},t.prototype.cast=function(t){return this.throwIfDisposed(),$.cast(this,t)},t.prototype.add=function(t){return this.throwIfDisposed(),$.add(this,t)},t.prototype.addStrict=function(t){return this.throwIfDisposed(),$.addStrict(this,t)},t.prototype.sub=function(t){return this.throwIfDisposed(),$.sub(this,t)},t.prototype.subStrict=function(t){return this.throwIfDisposed(),$.subStrict(this,t)},t.prototype.pow=function(t){return this.throwIfDisposed(),$.pow(this,t)},t.prototype.powStrict=function(t){return this.throwIfDisposed(),$.powStrict(this,t)},t.prototype.mul=function(t){return this.throwIfDisposed(),$.mul(this,t)},t.prototype.mulStrict=function(t){return this.throwIfDisposed(),$.mulStrict(this,t)},t.prototype.div=function(t){return this.throwIfDisposed(),$.div(this,t)},t.prototype.floorDiv=function(t){return this.throwIfDisposed(),$.floorDiv(this,t)},t.prototype.divStrict=function(t){return this.throwIfDisposed(),$.divStrict(this,t)},t.prototype.minimum=function(t){return this.throwIfDisposed(),$.minimum(this,t)},t.prototype.minimumStrict=function(t){return this.throwIfDisposed(),$.minimumStrict(this,t)},t.prototype.maximum=function(t){return this.throwIfDisposed(),$.maximum(this,t)},t.prototype.maximumStrict=function(t){return this.throwIfDisposed(),$.maximumStrict(this,t)},t.prototype.mod=function(t){return this.throwIfDisposed(),$.mod(this,t)},t.prototype.modStrict=function(t){return this.throwIfDisposed(),$.modStrict(this,t)},t.prototype.squaredDifference=function(t){return this.throwIfDisposed(),$.squaredDifference(this,t)},t.prototype.squaredDifferenceStrict=function(t){return this.throwIfDisposed(),$.squaredDifferenceStrict(this,t)},t.prototype.transpose=function(t){return this.throwIfDisposed(),$.transpose(this,t)},t.prototype.notEqual=function(t){return this.throwIfDisposed(),$.notEqual(this,t)},t.prototype.notEqualStrict=function(t){return this.throwIfDisposed(),$.notEqualStrict(this,t)},t.prototype.less=function(t){return this.throwIfDisposed(),$.less(this,t)},t.prototype.lessStrict=function(t){return this.throwIfDisposed(),$.lessStrict(this,t)},t.prototype.equal=function(t){return this.throwIfDisposed(),$.equal(this,t)},t.prototype.equalStrict=function(t){return this.throwIfDisposed(),$.equalStrict(this,t)},t.prototype.lessEqual=function(t){return this.throwIfDisposed(),$.lessEqual(this,t)},t.prototype.lessEqualStrict=function(t){return this.throwIfDisposed(),$.lessEqualStrict(this,t)},t.prototype.greater=function(t){return this.throwIfDisposed(),$.greater(this,t)},t.prototype.greaterStrict=function(t){return this.throwIfDisposed(),$.greaterStrict(this,t)},t.prototype.greaterEqual=function(t){return this.throwIfDisposed(),$.greaterEqual(this,t)},t.prototype.greaterEqualStrict=function(t){return this.throwIfDisposed(),$.greaterEqualStrict(this,t)},t.prototype.logicalAnd=function(t){return this.throwIfDisposed(),$.logicalAnd(this,t)},t.prototype.logicalOr=function(t){return this.throwIfDisposed(),$.logicalOr(this,t)},t.prototype.logicalNot=function(){return this.throwIfDisposed(),$.logicalNot(this)},t.prototype.logicalXor=function(t){return this.throwIfDisposed(),$.logicalXor(this,t)},t.prototype.where=function(t,e){return this.throwIfDisposed(),$.where(t,this,e)},t.prototype.neg=function(){return this.throwIfDisposed(),$.neg(this)},t.prototype.ceil=function(){return this.throwIfDisposed(),$.ceil(this)},t.prototype.floor=function(){return this.throwIfDisposed(),$.floor(this)},t.prototype.sign=function(){return this.throwIfDisposed(),$.sign(this)},t.prototype.exp=function(){return this.throwIfDisposed(),$.exp(this)},t.prototype.expm1=function(){return this.throwIfDisposed(),$.expm1(this)},t.prototype.log=function(){return this.throwIfDisposed(),$.log(this)},t.prototype.log1p=function(){return this.throwIfDisposed(),$.log1p(this)},t.prototype.sqrt=function(){return this.throwIfDisposed(),$.sqrt(this)},t.prototype.rsqrt=function(){return this.throwIfDisposed(),$.rsqrt(this)},t.prototype.square=function(){return this.throwIfDisposed(),$.square(this)},t.prototype.reciprocal=function(){return this.throwIfDisposed(),$.reciprocal(this)},t.prototype.abs=function(){return this.throwIfDisposed(),$.abs(this)},t.prototype.clipByValue=function(t,e){return this.throwIfDisposed(),$.clipByValue(this,t,e)},t.prototype.relu=function(){return this.throwIfDisposed(),$.relu(this)},t.prototype.elu=function(){return this.throwIfDisposed(),$.elu(this)},t.prototype.selu=function(){return this.throwIfDisposed(),$.selu(this)},t.prototype.leakyRelu=function(t){return void 0===t&&(t=.2),this.throwIfDisposed(),$.leakyRelu(this,t)},t.prototype.prelu=function(t){return this.throwIfDisposed(),$.prelu(this,t)},t.prototype.sigmoid=function(){return this.throwIfDisposed(),$.sigmoid(this)},t.prototype.logSigmoid=function(){return this.throwIfDisposed(),$.logSigmoid(this)},t.prototype.softplus=function(){return this.throwIfDisposed(),$.softplus(this)},t.prototype.sin=function(){return this.throwIfDisposed(),$.sin(this)},t.prototype.cos=function(){return this.throwIfDisposed(),$.cos(this)},t.prototype.tan=function(){return this.throwIfDisposed(),$.tan(this)},t.prototype.asin=function(){return this.throwIfDisposed(),$.asin(this)},t.prototype.acos=function(){return this.throwIfDisposed(),$.acos(this)},t.prototype.atan=function(){return this.throwIfDisposed(),$.atan(this)},t.prototype.sinh=function(){return this.throwIfDisposed(),$.sinh(this)},t.prototype.cosh=function(){return this.throwIfDisposed(),$.cosh(this)},t.prototype.tanh=function(){return this.throwIfDisposed(),$.tanh(this)},t.prototype.asinh=function(){return this.throwIfDisposed(),$.asinh(this)},t.prototype.acosh=function(){return this.throwIfDisposed(),$.acosh(this)},t.prototype.atanh=function(){return this.throwIfDisposed(),$.atanh(this)},t.prototype.erf=function(){return this.throwIfDisposed(),$.erf(this)},t.prototype.round=function(){return this.throwIfDisposed(),$.round(this)},t.prototype.step=function(t){return void 0===t&&(t=0),this.throwIfDisposed(),$.step(this,t)},t.prototype.softmax=function(t){return void 0===t&&(t=-1),this.throwIfDisposed(),$.softmax(this,t)},t.prototype.resizeBilinear=function(t,e){return void 0===e&&(e=!1),this.throwIfDisposed(),$.image.resizeBilinear(this,t,e)},t.prototype.resizeNearestNeighbor=function(t,e){return void 0===e&&(e=!1),this.throwIfDisposed(),$.image.resizeNearestNeighbor(this,t,e)},t.prototype.conv1d=function(t,e,n,r,o,a){return void 0===r&&(r="NWC"),void 0===o&&(o=1),this.throwIfDisposed(),$.conv1d(this,t,e,n,r,o,a)},t.prototype.conv2d=function(t,e,n,r,o,a){return void 0===r&&(r="NHWC"),void 0===o&&(o=[1,1]),this.throwIfDisposed(),$.conv2d(this,t,e,n,r,o,a)},t.prototype.conv2dTranspose=function(t,e,n,r,o){return this.throwIfDisposed(),$.conv2dTranspose(this,t,e,n,r,o)},t.prototype.depthwiseConv2D=function(t,e,n,r,o,a){return void 0===r&&(r="NHWC"),void 0===o&&(o=[1,1]),this.throwIfDisposed(),$.depthwiseConv2d(this,t,e,n,r,o,a)},t.prototype.avgPool=function(t,e,n,r){return this.throwIfDisposed(),$.avgPool(this,t,e,n,r)},t.prototype.maxPool=function(t,e,n,r){return this.throwIfDisposed(),$.maxPool(this,t,e,n,r)},t.prototype.localResponseNormalization=function(t,e,n,r){return void 0===t&&(t=5),void 0===e&&(e=1),void 0===n&&(n=1),void 0===r&&(r=.5),$.localResponseNormalization(this,t,e,n,r)},t.prototype.variable=function(t,e,n){return void 0===t&&(t=!0),this.throwIfDisposed(),K.variable(this,t,e,n)},t.prototype.unsortedSegmentSum=function(t,e){return this.throwIfDisposed(),$.unsortedSegmentSum(this,t,e)},t.prototype.batchToSpaceND=function(t,e){return this.throwIfDisposed(),$.batchToSpaceND(this,t,e)},t.prototype.spaceToBatchND=function(t,e){return this.throwIfDisposed(),$.spaceToBatchND(this,t,e)},t.prototype.topk=function(t,e){return void 0===t&&(t=1),void 0===e&&(e=!0),this.throwIfDisposed(),$.topk(this,t,e)},t.prototype.stridedSlice=function(t,e,n,r,o){return void 0===r&&(r=0),void 0===o&&(o=0),this.throwIfDisposed(),$.stridedSlice(this,t,e,n,r,o)},t.nextId=0,t}();Object.defineProperty(X,Symbol.hasInstance,{value:function(t){return!!t&&null!=t.shape&&null!=t.dtype}});var K=function(t){function e(n,r,o){void 0===r&&(r=!0);var a=t.call(this,n.shape,n.dtype,null,n.dataId)||this;a.trainable=r,a.name=o,null==a.name&&(a.name=e.nextVarId.toString(),e.nextVarId++);try{H().registerVariable(a)}catch(t){throw H().disposeTensor(a),t}return a}return n(e,t),e.variable=function(t,n,r,o){return void 0===n&&(n=!0),null!=o&&o!==t.dtype&&(t=t.asType(o)),new e(t,n,r)},e.prototype.assign=function(t){if(t.dtype!==this.dtype)throw new Error("dtype of the new value ("+t.dtype+") and previous value ("+this.dtype+") must match");if(!v(t.shape,this.shape))throw new Error("shape of the new value ("+t.shape+") and previous value ("+this.shape+") must match");H().disposeTensor(this),this.dataId=t.dataId,H().registerTensor(this)},e.nextVarId=0,e}(X);Object.defineProperty(K,Symbol.hasInstance,{value:function(t){return t instanceof X&&null!=t.assign&&t.assign instanceof Function}});var Y=K.variable;function Q(t,e){l(t.dtype===e.dtype," The dtypes of the first("+t.dtype+") and second("+e.dtype+") input must match")}function J(t){var e=[];return function t(e,n,r){if(null==e)return;if(e instanceof X)return void n.push(e);if(o=e,!Array.isArray(o)&&"object"!=typeof o)return;var o;var a=e;for(var i in a){var u=a[i];r.has(u)||(r.add(u),t(u,n,r))}}(t,e,new Set),e}var Z,tt=function(){function t(t,e,n){this.backend=t,this.safeMode=e,this.debugMode=n,this.registeredVariables={},this.refCounter=new WeakMap,this.nextTapeNodeId=0,this.numBytes=0,this.numTensors=0,this.numDataBuffers=0,this.gradientScopeCount=0,this.customGradientDepth=0,this.keepTensors=new Set,this.activeScope={track:[],name:"default scope"},this.scopeStack=[this.activeScope],this.profiler=new L(t)}return t.prototype.tidy=function(t,e,n){var r=this;void 0===n&&(n=!1);var o,a=null;if(null==e){if("function"!=typeof t)throw new Error("Please provide a function to tidy()");e=t}else{if("string"!=typeof t&&!(t instanceof String))throw new Error("When calling with two arguments, the first argument to tidy() must be a string");if("function"!=typeof e)throw new Error("When calling with two arguments, the 2nd argument to tidy() must be a function");a=t}return this.scopedRun(function(){return r.startScope(a,n)},function(){return r.endScope(o,n)},function(){return(o=e())instanceof Promise&&console.error("Cannot return a Promise inside of tidy."),o})},t.prototype.scopedRun=function(t,e,n){t();try{var r=n();return e(),r}catch(t){throw e(),t}},t.prototype.runKernel=function(t,e,n){var r,o=this,a=[],i=function(t){return a.push(t),t},u=this.activeScope.name;if(this.scopedRun(function(){return o.customGradientDepth++},function(){return o.customGradientDepth--},function(){r=o.debugMode()?o.profiler.profileKernel(u,function(){return t(o.backend,i)}):t(o.backend,i)}),this.shouldRecord()){var s={id:this.nextTapeNodeId++,name:u,inputs:e,output:Array.isArray(r)?r[0]:r};null!=n&&(s.gradient=function(t){return n(t,a)}),this.activeTape.push(s)}return r},t.prototype.registerTensor=function(t){var e=this.refCounter.has(t.dataId)?this.refCounter.get(t.dataId):0;this.numTensors++,0===e&&(this.numDataBuffers++,this.numBytes+=d(t.shape)*I(t.dtype),this.backend.register(t.dataId,t.shape,t.dtype)),this.refCounter.set(t.dataId,e+1),t instanceof K||this.track(t)},t.prototype.registerVariable=function(t){if(null!=this.registeredVariables[t.name])throw new Error("Variable with name "+t.name+" was already registered");this.registeredVariables[t.name]=t},t.prototype.disposeTensor=function(t){if(this.refCounter.has(t.dataId)){this.keepTensors.has(t.id)&&this.keepTensors.delete(t.id),this.numTensors--;var e=this.refCounter.get(t.dataId);e<=1?(this.refCounter.delete(t.dataId),this.backend.disposeData(t.dataId),this.numDataBuffers--,this.numBytes-=d(t.shape)*I(t.dtype)):this.refCounter.set(t.dataId,e-1)}},t.prototype.disposeVariables=function(){for(var t in this.registeredVariables){var e=this.registeredVariables[t];this.disposeTensor(e),delete this.registeredVariables[t]}},t.prototype.memory=function(){var t=this.backend.memory();return t.numTensors=this.numTensors,t.numDataBuffers=this.numDataBuffers,t.numBytes=this.numBytes,t},t.prototype.shouldRecord=function(){return null!=this.activeTape&&0===this.customGradientDepth},t.prototype.addTapeNode=function(t,e,n){var r={};t.forEach(function(t,e){r[e]=t});var o={id:this.nextTapeNodeId++,name:this.activeScope.name,inputs:r,output:e,gradient:function(t){var e={};return n(t).forEach(function(t,n){e[n]=function(){return t}}),e}};this.activeTape.push(o)},t.prototype.keep=function(t){if(1===this.scopeStack.length&&this.safeMode)throw new Error("Safe mode is ON. Enclose all tensor operations inside tf.tidy(): tf.tidy(() => {...}) to avoid memory leaks.");return this.keepTensors.add(t.id),t},t.prototype.startScope=function(t,e){void 0===e&&(e=!1),e&&0===this.gradientScopeCount&&(this.activeTape=[]),e&&this.gradientScopeCount++;var n={track:[],name:"unnamed scope"};t&&(n.name=t),this.scopeStack.push(n),this.activeScope=n},t.prototype.endScope=function(t,e){var n=this;void 0===e&&(e=!1),e&&(this.gradientScopeCount--,0===this.gradientScopeCount&&(this.activeTape=null));var r=new Set(this.keepTensors),o=J(t);o.forEach(function(t){return r.add(t.id)});for(var a=0;a<this.activeScope.track.length;a++){var i=this.activeScope.track[a];r.has(i.id)||(null!=this.activeTape?o.push(i):i.dispose())}var u=this.scopeStack.pop();this.activeScope=0===this.scopeStack.length?{track:[],name:"default scope"}:this.scopeStack[this.scopeStack.length-1],o.forEach(function(t){!n.keepTensors.has(t.id)&&function(t,e){for(var n=0;n<e.length;n++)if(e[n].id===t.id)return!0;return!1}(t,u.track)&&n.track(t)})},t.prototype.gradients=function(t,e,n,r){var o=this;return void 0===r&&(r=!1),l(e.length>0,"gradients() received an empty list of xs."),this.tidy("gradients",function(){var a=t();l(a instanceof X,"The result y returned by f() must be a tensor.");var i=function(t,e,n){for(var r={},o={},a=0;a<e.length;a++)r[e[a].id]=!0;for(a=0;a<t.length;a++){var i=(v=t[a]).inputs;for(var u in i){for(var s=i[u],l=!1,c=0;c<e.length;c++)if(r[s.id]){r[v.output.id]=!0,l=!0,o[v.id]=!0;break}if(l)break}}var h={};h[n.id]=!0;var f={};for(a=t.length-1;a>=0;a--){i=(v=t[a]).inputs;var p=[];for(p.push(v.output),c=0;c<p.length;c++)if(h[p[c].id]){for(var u in i)h[i[u].id]=!0,f[v.id]=!0;break}}var d=[];for(a=0;a<t.length;a++){var v;if(o[(v=t[a]).id]&&f[v.id]){var m={};for(var u in v.inputs){var g=v.inputs[u];r[g.id]&&(m[u]=g)}var y=Object.assign({},v);y.inputs=m,y.output=v.output,d.push(y)}}return d}(o.activeTape,e,a);if(!r&&0===i.length&&e.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.");var u,s,c={};return c[a.id]=null==n?(u=a.shape,s=_(d(u),"float32"),X.make(u,{values:s})):n,function(t,e){for(var n=e.length-1;n>=0;n--){var r=e[n],o=t[r.output.id];if(null==r.gradient)throw new Error("Cannot compute gradient: gradient function not found for "+r.name+".");var a=r.gradient(o);for(var i in r.inputs){if(!(i in a))throw new Error("Cannot backprop through input "+i+". Available gradients found: "+Object.keys(a)+".");var u=a[i](),s=r.inputs[i];if(!v(u.shape,s.shape))throw new Error("Error in gradient for op "+r.name+". The gradient of input '"+i+"' has shape '"+u.shape+"', which does not match the shape of the input '"+s.shape+"'");if(null==t[s.id])t[s.id]=u;else{var l=t[s.id];t[s.id]=l.add(u),l.dispose()}}}}(c,i),{value:a,grads:e.map(function(t){return c[t.id]})}},!0)},t.prototype.customGrad=function(t){var e=this;return l(A(t),"The f passed in customGrad(f) must be a function."),function(){for(var n,r,o=[],a=0;a<arguments.length;a++)o[a]=arguments[a];if(l(o.every(function(t){return t instanceof X}),"The args passed in customGrad(f)(x1, x2,...) must all be tensors"),e.scopedRun(function(){return e.customGradientDepth++},function(){return e.customGradientDepth--},function(){r=e.tidy(t.name,function(){var e=t.apply(void 0,o),r=e.value,a=e.gradFunc;return l(r instanceof X,"The function f passed in customGrad(f) must return an object where `obj.value` is a tensor"),l(A(a),"The function f passed in customGrad(f) must return an object where `obj.gradFunc` is a function."),n=a,r},!0)}),e.shouldRecord()){e.addTapeNode(o,r,function(t){var e=n(t),r=Array.isArray(e)?e:[e];return l(r.length===o.length,"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(...)."),l(r.every(function(t){return t instanceof X}),"The function f passed in customGrad(f) must return an object where `obj.gradFunc` is a function that returns a list of only tensors."),r})}return r}},t.prototype.write=function(t,e){this.backend.write(t,e)},t.prototype.readSync=function(t){return this.backend.readSync(t)},t.prototype.read=function(t){return this.backend.read(t)},t.prototype.fromPixels=function(t,e){return this.backend.fromPixels(t,e)},t.prototype.time=function(t){return o(this,void 0,void 0,function(){var e,n;return a(this,function(r){switch(r.label){case 0:return e=F(),[4,this.backend.time(t)];case 1:return(n=r.sent()).wallMs=F()-e,[2,n]}})})},t.prototype.track=function(t){if(1===this.scopeStack.length&&this.safeMode)throw new Error("Safe mode is ON. Enclose all tensor operations inside tf.tidy(): tf.tidy(() => {op();...}); to avoid memory leaks.");return this.activeScope.track.push(t),t},t}();!function(t){t[t.NUMBER=0]="NUMBER",t[t.BOOLEAN=1]="BOOLEAN",t[t.STRING=2]="STRING"}(Z||(Z={}));var et=[{name:"DEBUG",type:Z.BOOLEAN},{name:"IS_BROWSER",type:Z.BOOLEAN},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION",type:Z.NUMBER},{name:"WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE",type:Z.BOOLEAN},{name:"WEBGL_VERSION",type:Z.NUMBER},{name:"WEBGL_RENDER_FLOAT32_ENABLED",type:Z.BOOLEAN},{name:"WEBGL_DOWNLOAD_FLOAT_ENABLED",type:Z.BOOLEAN},{name:"WEBGL_FENCE_API_ENABLED",type:Z.BOOLEAN},{name:"BACKEND",type:Z.STRING}];function nt(t,e){var n;try{n=it(t,e)}catch(t){return!1}return null!=n&&(ut(n),!0)}var rt="tfjsflags";function ot(){var t={};if("undefined"==typeof window||void 0===window.location)return t;var e,n,r=(e=window.location.search,n={},e.replace(/[?&]([^=?&]+)(?:=([^&]*))?/g,function(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];return function(t,e,n){t[decodeURIComponent(e)]=decodeURIComponent(n||"")}(n,e[0],e[1]),e.join("=")}),n);if(rt in r){var o={};r[rt].split(",").forEach(function(t){var e=t.split(":"),n=e[0],r=e[1];o[n]=r}),et.forEach(function(e){e.name in o&&(console.log("Setting feature override from URL "+e.name+": "+o[e.name]),e.type===Z.NUMBER?t[e.name]=+o[e.name]:e.type===Z.BOOLEAN?t[e.name]="true"===o[e.name]:e.type===Z.STRING?t[e.name]=o[e.name]:console.warn("Unknown URL param: "+e.name+"."))})}return t}function at(t,e){return null!=t.getExtension(e)}function it(t,e){if(0===t||!e)throw new Error("Cannot get WebGL rendering context, WebGL is disabled.");var n=document.createElement("canvas");return 1===t?n.getContext("webgl")||n.getContext("experimental-webgl"):n.getContext("webgl2")}function ut(t){if(null!=t){var e=t.getExtension("WEBGL_lose_context");if(null==e)throw new Error("Extension WEBGL_lose_context not supported on this browser.");e.loseContext()}}function st(t,e){var n=t.createFramebuffer(),r=t.createTexture();t.bindTexture(t.TEXTURE_2D,r);var o=2===e?t.RGBA32F:t.RGBA;t.texImage2D(t.TEXTURE_2D,0,o,1,1,0,t.RGBA,t.FLOAT,null),t.bindFramebuffer(t.FRAMEBUFFER,n),t.framebufferTexture2D(t.FRAMEBUFFER,t.COLOR_ATTACHMENT0,t.TEXTURE_2D,r,0)}var lt=function(){function t(t){this.features={},this.registry={},null!=t&&(this.features=t),this.get("DEBUG")&&console.warn("Debugging mode is ON. The output of every math call will be downloaded to CPU and checked for NaNs. This significantly impacts performance.")}return t.setBackend=function(t,e){if(void 0===e&&(e=!1),!(t in ht.registry))throw new Error("Backend name '"+t+"' not found in registry");ht.initBackend(t,e)},t.getBackend=function(){return ht.initDefaultBackend(),ht.backendName},t.disposeVariables=function(){ht.engine.disposeVariables()},t.memory=function(){return ht.engine.memory()},t.tidy=function(t,e,n){return void 0===n&&(n=!1),ht.engine.tidy(t,e,n)},t.dispose=function(t){J(t).forEach(function(t){return t.dispose()})},t.keep=function(t){return ht.engine.keep(t)},t.time=function(t){return ht.engine.time(t)},t.prototype.get=function(t){return t in this.features?this.features[t]:(this.features[t]=this.evaluateFeature(t),this.features[t])},t.prototype.getFeatures=function(){return this.features},t.prototype.set=function(t,e){this.features[t]=e},t.prototype.getBestBackendName=function(){var t=this;if(0===Object.keys(this.registry).length)throw new Error("No backend found in registry.");return Object.keys(this.registry).map(function(e){return{name:e,entry:t.registry[e]}}).sort(function(t,e){return e.entry.priority-t.entry.priority})[0].name},t.prototype.evaluateFeature=function(t){if("DEBUG"===t)return!1;if("IS_BROWSER"===t)return"undefined"!=typeof window;if("IS_NODE"===t)return"undefined"!=typeof process&&void 0!==process.versions.node;if("IS_CHROME"===t)return"undefined"!=typeof navigator&&null!=navigator&&null!=navigator.userAgent&&/Chrome/.test(navigator.userAgent)&&/Google Inc/.test(navigator.vendor);if("IS_TEST"===t)return!1;if("BACKEND"===t)return this.getBestBackendName();if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"===t){var e=this.get("WEBGL_VERSION");return 0===e?0:e>0?0:function(t,e){if(0===t)return 0;var n,r=it(t,e);return n=at(r,"EXT_disjoint_timer_query_webgl2")&&2===t?2:at(r,"EXT_disjoint_timer_query")?1:0,null!=r&&ut(r),n}(e,this.get("IS_BROWSER"))}if("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE"===t)return this.get("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0&&(n=navigator.userAgent||navigator.vendor||window.opera,!(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(n)||/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(n.substr(0,4))));if("HAS_WEBGL"===t)return this.get("WEBGL_VERSION")>0;if("WEBGL_VERSION"===t)return nt(2,this.get("IS_BROWSER"))?2:nt(1,this.get("IS_BROWSER"))?1:0;if("WEBGL_RENDER_FLOAT32_ENABLED"===t)return function(t,e){if(0===t)return!1;var n=it(t,e);if(1===t){if(!at(n,"OES_texture_float"))return!1}else if(!at(n,"EXT_color_buffer_float"))return!1;st(n,t);var r=n.checkFramebufferStatus(n.FRAMEBUFFER)===n.FRAMEBUFFER_COMPLETE;return ut(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("WEBGL_DOWNLOAD_FLOAT_ENABLED"===t)return function(t,e){if(0===t)return!1;var n=it(t,e);if(1===t){if(!at(n,"OES_texture_float"))return!1}else if(!at(n,"EXT_color_buffer_float"))return!1;st(n,t),n.readPixels(0,0,1,1,n.RGBA,n.FLOAT,new Float32Array(4));var r=n.getError()===n.NO_ERROR;return ut(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("WEBGL_FENCE_API_ENABLED"===t)return function(t,e){if(2!==t)return!1;var n=it(t,e),r=null!=n.fenceSync;return ut(n),r}(this.get("WEBGL_VERSION"),this.get("IS_BROWSER"));if("TEST_EPSILON"===t)return this.get("WEBGL_RENDER_FLOAT32_ENABLED")?.001:.1;var n;throw new Error("Unknown feature "+t+".")},t.prototype.setFeatures=function(t){this.features=Object.assign({},t)},t.prototype.reset=function(){this.features=ot(),null!=this.globalEngine&&(this.globalEngine=null)},t.prototype.initBackend=function(t,e){var n=this;void 0===e&&(e=!1),this.backendName=t,this.backend=this.findBackend(t),this.globalEngine=new tt(this.backend,e,function(){return n.get("DEBUG")})},t.prototype.findBackend=function(t){return t in this.registry?this.registry[t].backend:null},t.prototype.registerBackend=function(t,e,n,r){var o=this;if(void 0===n&&(n=1),t in this.registry)return console.warn(t+" backend was already registered. Reusing existing backend"),null!=r&&r(function(){return o.engine}),!1;try{var a=e();return this.registry[t]={backend:a,priority:n},!0}catch(e){return console.warn("Registration of backend "+t+" failed"),console.warn(e.stack||e.message),!1}},t.prototype.removeBackend=function(t){if(!(t in this.registry))throw new Error(t+" backend not found in registry");this.registry[t].backend.dispose(),delete this.registry[t]},Object.defineProperty(t.prototype,"engine",{get:function(){return this.initDefaultBackend(),this.globalEngine},enumerable:!0,configurable:!0}),t.prototype.initDefaultBackend=function(){null==this.globalEngine&&this.initBackend(this.get("BACKEND"),!1)},t}();var ct,ht=(null==(ct=function(){var t;if("undefined"!=typeof window)t=window;else{if("undefined"==typeof process)throw new Error("Could not find a global object");t=process}return t}()).ENV&&(ct.ENV=new lt(ot()),j(function(){return ct.ENV.engine})),ct.ENV),ft=Object.freeze({Environment:lt,ENV:ht});function pt(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];ht.get("IS_TEST")||console.warn.apply(console,t)}function dt(t,e,n,r){void 0===r&&(r=!0);var o=[];if(r)(o=o.concat(e.slice(0))).push(t[0]/n),o=o.concat(t.slice(1));else{o=o.concat(t[0]);for(var a=e.length,i=0;i<a;++i)o=o.concat([t[i+1]/e[i],e[i]]);o=o.concat(t.slice(a+1))}return o}function vt(t,e,n){void 0===n&&(n=!0);var r=[];if(n){r.push(e);for(var o=e+1;o<t;++o)o<=2*e?(r.push(o),r.push(o-(e+1))):r.push(o)}else{var a=[],i=[];for(o=1;o<t;++o)o>=2*e+1||o%2==1?i.push(o):a.push(o);r.push.apply(r,a),r.push(0),r.push.apply(r,i)}return r}function mt(t,e,n,r){void 0===r&&(r=!0);var o=[];r?o.push(t[0]/n):o.push(t[0]*n);for(var a=1;a<t.length;++a)a<=e.length?r?o.push(e[a-1]*t[a]):o.push(t[a]/e[a-1]):o.push(t[a]);return o}function gt(t,e){for(var n=[0],r=0;r<e;++r)n.push(t[r][0]);return n}function yt(t,e,n){for(var r=t.slice(0,1),o=0;o<n;++o)r.push(t[o+1]-e[o][0]-e[o][1]);return r}function xt(t,e){for(var n=0;n<t.length;++n)if(t[t.length-n-1]!==e-1-n)return!1;return!0}function bt(t,e){for(var n=[],r=t.length,o=0;o<r;o++)-1===e.indexOf(o)&&n.push(t[o]);return[n,e.map(function(e){return t[e]})]}function wt(t,e){return function(t,e,n){for(var r=t.length+e.length,o=[],a=0,i=0,u=0;u<r;u++)-1===n.indexOf(u)?o.push(t[a++]):o.push(e[i++]);return o}(t,e.map(function(t){return 1}),e)}function Et(t,e){var n=e.length;return l((t=null==t?e.map(function(t,e){return e}):[].concat(t)).every(function(t){return t>=-n&&t<n}),"All values in axis param must be in range [-"+n+", "+n+") but got axis "+t),l(t.every(function(t){return m(t)}),"All values in axis param must be integers but got axis "+t),t.map(function(t){return t<0?n+t:t})}function St(t,e,n){l(xt(e,n),t+" supports only inner-most axes for now. Got axes "+e+" and rank-"+n+" input.")}function Rt(t,e){if(xt(t,e))return null;for(var n=[],r=0;r<e;++r)-1===t.indexOf(r)&&n.push(r);return t.forEach(function(t){return n.push(t)}),n}function Ct(t){return t.map(function(t,e){return[e,t]}).sort(function(t,e){return t[1]-e[1]}).map(function(t){return t[0]})}function kt(t,e){for(var n=[],r=e-t;r<e;++r)n.push(r);return n}var Tt=30;function It(t){return t<=Tt?t:D(t,Math.floor(Math.sqrt(t)))}function At(t,e,n,r,o,a,i,u,s){if(void 0===o&&(o=0),void 0===a&&(a=0),void 0===i&&(i=0),void 0===u&&(u=0),void 0===s&&(s=0),0!==i)throw new Error("ellipsis mask is not yet supported");if(0!==u)throw new Error("new axis mask is not yet supported");for(var l=[],c=[],h=[],f=0;f<t.length;f++)l[f]=Dt(o,e,r,t,f),c[f]=Nt(a,n,r,t,f),s&1<<f&&(c[f]=l[f]+1,h.push(f));var p=new Array(t.length).fill(0);return p=p.map(function(t,e){for(var n=0,o=l[e];!(r[e]>0?o>=c[e]:o<=c[e]);o+=r[e])n+=1;return n}),[l,p,h]}function Dt(t,e,n,r,o){var a=e[o];t&1<<o&&(a=n[o]>0?Number.MIN_SAFE_INTEGER:Number.MAX_SAFE_INTEGER);var i=r[o];return a<0&&(a+=i),a=u(0,a,i-1)}function Nt(t,e,n,r,o){var a=e[o];t&1<<o&&(a=n[o]>0?Number.MAX_SAFE_INTEGER:Number.MIN_SAFE_INTEGER);var i=r[o];return a<0&&(a+=i),a=n[o]>0?u(0,a,i):u(-1,a,i-1)}function Mt(t,e){if(l(A(t),"The f passed in variableGrads(f) must be a function"),l(null==e||Array.isArray(e)&&e.every(function(t){return t instanceof K}),"The varList passed in variableGrads(f, varList) must be an array of variables"),null==e)for(var n in e=[],ht.engine.registeredVariables)e.push(ht.engine.registeredVariables[n]);var r=e.length;l((e=e.filter(function(t){return t.trainable})).length>0,"variableGrads() expects at least one of the input variables to be trainable, but none of the "+r+" variables is trainable.");var o=ht.engine.gradients(t,e,null,!0),a=o.value,i=o.grads;l(i.some(function(t){return null!=t}),"Cannot find a connection between any variable and the result of the loss function y=f(x). Please make sure the operations that use variables are inside the function f passed to minimize()."),l(0===a.rank,"The f passed in variableGrads(f) must return a scalar, but it returned a rank-"+a.rank+" tensor");var u={};return e.forEach(function(t,e){null!=i[e]&&(u[t.name]=i[e])}),{value:a,grads:u}}function _t(t){return ht.engine.customGrad(t)}function Ot(t){if(t.filter(function(t){return null==t}).length>0)throw new Error("Cannot compute gradient of y=f(x) with respect to x. Make sure that\n the f you passed encloses all operations that lead from x to y.")}function Ft(t,e,n,r){if(void 0===r&&(r="float32"),r=r||"float32",t instanceof X)return t;if(!T(t)&&!Array.isArray(t)&&"number"!=typeof t&&"boolean"!=typeof t)throw new Error("Argument '"+e+"' passed to '"+n+"' must be a Tensor or TensorLike, but got "+t.constructor.name);var o=p(t);return T(t)||Array.isArray(t)||(t=[t]),X.make(o,{values:M(t,r,ht.get("DEBUG"))},r)}function Bt(t,e,n){if(!Array.isArray(t))throw new Error("Argument "+e+" passed to "+n+" must be a `Tensor[]` or `TensorLike[]`");return t.map(function(t,r){return Ft(t,e+"["+r+"]",n)})}function Lt(t){var e=Object.keys(t);if(1!==e.length)throw new Error("Please provide an object with a single key (operation name) mapping to a function. Got an object with "+e.length+" keys.");var n=e[0],r=t[n];n.endsWith("_")&&(n=n.substring(0,n.length-1));var o=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];ht.engine.startScope(n);try{var o=r.apply(void 0,t);return o instanceof Promise&&console.error("Cannot return a Promise inside of tidy."),ht.engine.endScope(o),o}catch(t){throw ht.engine.endScope(null),t}};return Object.defineProperty(o,"name",{value:n,configurable:!0}),o}var Pt=Lt({softmax_:function(t,e){void 0===e&&(e=-1);var n=Ft(t,"logits","softmax");if(-1===e&&(e=n.rank-1),e!==n.rank-1)throw Error("Softmax along a non-last dimension is not yet supported. Logits was rank "+n.rank+" and dim was "+e);return _t(function(t){var n=t.logSumExp([e],!0),r=t.toFloat().sub(n).exp();return{value:r,gradFunc:function(t){var n=t.mul(r);return n.sub(n.sum([e],!0).mul(r))}}})(n)}});function Ut(t,e,n){if(void 0===n&&(n="float32"),!T(t)&&!Array.isArray(t)&&"number"!=typeof t&&"boolean"!=typeof t)throw new Error("values passed to tensor(values) must be an array of numbers or booleans, or a TypedArray");var r=p(t);return null!=e&&1!==r.length&&c(e,r,"Error creating a new Tensor. Inferred shape ("+r+") does not match the provided shape ("+e+"). "),T(t)||Array.isArray(t)||(t=[t]),e=e||r,X.make(e,{values:M(t,n,ht.get("DEBUG"))},n)}function zt(t,e){if(void 0===e&&(e="float32"),T(t)||Array.isArray(t))throw new Error("Error creating a new Scalar: value must be a primiti