@robotical/martyblocks 2.1.4 → 2.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib.min.js +141 -141
- package/dist/static/MLModelWorker.js +68 -68
- package/package.json +1 -1
|
@@ -602,7 +602,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
602
602
|
* See the License for the specific language governing permissions and
|
|
603
603
|
* limitations under the License.
|
|
604
604
|
* =============================================================================
|
|
605
|
-
*/function G$(n,e){let t=N(n,"a","div"),s=N(e,"b","div");if([t,s]=rt(t,s),t.dtype==="int32"&&s.dtype==="int32")return Yd(t,s);const r={a:t,b:s},a={};return R.runKernel(Ao,r,a)}const
|
|
605
|
+
*/function G$(n,e){let t=N(n,"a","div"),s=N(e,"b","div");if([t,s]=rt(t,s),t.dtype==="int32"&&s.dtype==="int32")return Yd(t,s);const r={a:t,b:s},a={};return R.runKernel(Ao,r,a)}const fe=E({div_:G$});/**
|
|
606
606
|
* @license
|
|
607
607
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
608
608
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -1372,7 +1372,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
1372
1372
|
* See the License for the specific language governing permissions and
|
|
1373
1373
|
* limitations under the License.
|
|
1374
1374
|
* =============================================================================
|
|
1375
|
-
*/function rC(n,e){let t=N(n,"a","div"),s=N(e,"b","div");[t,s]=rt(t,s);const r=
|
|
1375
|
+
*/function rC(n,e){let t=N(n,"a","div"),s=N(e,"b","div");[t,s]=rt(t,s);const r=fe(t,s),a=Re(r),o=Jn(s,a);return Vt(o,a,r)}const Ly=E({divNoNan_:rC});/**
|
|
1376
1376
|
* @license
|
|
1377
1377
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
1378
1378
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -1522,7 +1522,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
1522
1522
|
* See the License for the specific language governing permissions and
|
|
1523
1523
|
* limitations under the License.
|
|
1524
1524
|
* =============================================================================
|
|
1525
|
-
*/function
|
|
1525
|
+
*/function pe(n,e){if((vn(n)&&e!=="string"||Array.isArray(n))&&e!=="complex64")throw new Error("Error creating a new Scalar: value must be a primitive (number|boolean|string)");if(e==="string"&&vn(n)&&!(n instanceof Uint8Array))throw new Error("When making a scalar from encoded string, the value must be `Uint8Array`.");return Zs(n,[],[],e)}/**
|
|
1526
1526
|
* @license
|
|
1527
1527
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
1528
1528
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -1582,7 +1582,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
1582
1582
|
* See the License for the specific language governing permissions and
|
|
1583
1583
|
* limitations under the License.
|
|
1584
1584
|
* =============================================================================
|
|
1585
|
-
*/function yC(n,e="euclidean",t=null,s=!1){n=N(n,"x","norm");const r=By(n,e,t);let a=r.shape;if(s){const o=Ce(t,n.shape);a=it(r.shape,o)}return _(r,a)}function By(n,e,t=null){if(n.rank===0)return yt(n);if(n.rank!==1&&t===null)return By(_(n,[-1]),e,t);if(n.rank===1||typeof t=="number"||Array.isArray(t)&&t.length===1){if(e===1)return ue(yt(n),t);if(e===1/0)return Sn(yt(n),t);if(e===-1/0)return Si(yt(n),t);if(e==="euclidean"||e===2)return _t(ue(rr(yt(n),
|
|
1585
|
+
*/function yC(n,e="euclidean",t=null,s=!1){n=N(n,"x","norm");const r=By(n,e,t);let a=r.shape;if(s){const o=Ce(t,n.shape);a=it(r.shape,o)}return _(r,a)}function By(n,e,t=null){if(n.rank===0)return yt(n);if(n.rank!==1&&t===null)return By(_(n,[-1]),e,t);if(n.rank===1||typeof t=="number"||Array.isArray(t)&&t.length===1){if(e===1)return ue(yt(n),t);if(e===1/0)return Sn(yt(n),t);if(e===-1/0)return Si(yt(n),t);if(e==="euclidean"||e===2)return _t(ue(rr(yt(n),pe(2,"int32")),t));throw new Error(`Error in norm: invalid ord value: ${e}`)}if(Array.isArray(t)&&t.length===2){if(e===1)return Sn(ue(yt(n),t[0]),t[1]-1);if(e===1/0)return Sn(ue(yt(n),t[1]),t[0]);if(e===-1/0)return Si(ue(yt(n),t[1]),t[0]);if(e==="fro"||e==="euclidean")return _t(ue(Be(n),t));throw new Error(`Error in norm: invalid ord value: ${e}`)}throw new Error(`Error in norm: invalid axis: ${t}`)}const $i=E({norm_:yC});/**
|
|
1586
1586
|
* @license
|
|
1587
1587
|
* Copyright 2022 Google LLC. All Rights Reserved.
|
|
1588
1588
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -2933,7 +2933,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
2933
2933
|
* See the License for the specific language governing permissions and
|
|
2934
2934
|
* limitations under the License.
|
|
2935
2935
|
* =============================================================================
|
|
2936
|
-
*/function WT(n){const e=n.shape[n.shape.length-1],t=n.size/e;let s;if(e<=2){const r=_(n,[t,e]);s=Ei(r)}else{const r=[t,2*(e-1)],a=_(ki(n),[t,e]),o=_(Pc(n),[t,e]),i=Ln(Ae(a,[0,1],[t,e-2]),1),l=D(Ln(Ae(o,[0,1],[t,e-2]),1),
|
|
2936
|
+
*/function WT(n){const e=n.shape[n.shape.length-1],t=n.size/e;let s;if(e<=2){const r=_(n,[t,e]);s=Ei(r)}else{const r=[t,2*(e-1)],a=_(ki(n),[t,e]),o=_(Pc(n),[t,e]),i=Ln(Ae(a,[0,1],[t,e-2]),1),l=D(Ln(Ae(o,[0,1],[t,e-2]),1),pe(-1)),c=pt([a,i],1),u=pt([o,l],1),h=_(Ys(c,u),[r[0],r[1]]);s=Ei(h)}if(s=ki(s),n.rank===3&&n.shape[0]!==0){const r=s,a=n.shape[0];s=_(s,[a,s.shape[0]/a,s.shape[1]]),r.dispose()}return s}const kp=E({irfft_:WT});/**
|
|
2937
2937
|
* @license
|
|
2938
2938
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
2939
2939
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -3338,7 +3338,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
3338
3338
|
* See the License for the specific language governing permissions and
|
|
3339
3339
|
* limitations under the License.
|
|
3340
3340
|
* =============================================================================
|
|
3341
|
-
*/function h2(n,e,t,s,r=!0){const a=N(n,"v","movingAverage"),o=N(e,"x","movingAverage"),i=N(t,"decay","movingAverage");o$(a,o),S(Ee(a.shape,o.shape),()=>"Shape mismatch in v and x");const l=
|
|
3341
|
+
*/function h2(n,e,t,s,r=!0){const a=N(n,"v","movingAverage"),o=N(e,"x","movingAverage"),i=N(t,"decay","movingAverage");o$(a,o),S(Ee(a.shape,o.shape),()=>"Shape mismatch in v and x");const l=pe(1),c=ie(l,i);let u=D(ie(o,a),c);if(r){S(s!=null,()=>"When using zeroDebias: true, step is required.");const h=N(s,"step","movingAverage");u=fe(u,ie(l,rr(i,h)))}return X(a,u)}const d2=E({movingAverage_:h2});/**
|
|
3342
3342
|
* @license
|
|
3343
3343
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
3344
3344
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -3413,7 +3413,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
3413
3413
|
* See the License for the specific language governing permissions and
|
|
3414
3414
|
* limitations under the License.
|
|
3415
3415
|
* =============================================================================
|
|
3416
|
-
*/function I2(n,e,t,s){const r=N(n,"x","dropout");if(S(r.dtype==="float32",()=>`x has to be a floating point tensor since it's going to be scaled, but got a ${r.dtype} tensor instead.`),S(e>=0&&e<1,()=>`rate must be a float in the range [0, 1), but got ${e}.`),e===0)return n instanceof Oe?r.clone():r;const a=w2(r,t),o=1-e,i=
|
|
3416
|
+
*/function I2(n,e,t,s){const r=N(n,"x","dropout");if(S(r.dtype==="float32",()=>`x has to be a floating point tensor since it's going to be scaled, but got a ${r.dtype} tensor instead.`),S(e>=0&&e<1,()=>`rate must be a float in the range [0, 1), but got ${e}.`),e===0)return n instanceof Oe?r.clone():r;const a=w2(r,t),o=1-e,i=fe(Ni(X(Ur(a,0,1,"float32",s),o)),o);return D(r,i)}const hb=E({dropout_:I2});/**
|
|
3417
3417
|
* @license
|
|
3418
3418
|
* Copyright 2019 Google LLC. All Rights Reserved.
|
|
3419
3419
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -3833,7 +3833,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
3833
3833
|
* See the License for the specific language governing permissions and
|
|
3834
3834
|
* limitations under the License.
|
|
3835
3835
|
* =============================================================================
|
|
3836
|
-
*/function uE(o,i,l){return O(this,arguments,function*(n,e,t,s=.5,r=Number.NEGATIVE_INFINITY,a=!1){const c=N(n,"boxes","nonMaxSuppressionAsync"),u=N(e,"scores","nonMaxSuppressionAsync"),h=Ga(c,u,t,s,r,null),d=h.maxOutputSize,p=h.iouThreshold,f=h.scoreThreshold,[m,g]=yield Promise.all([c.data(),u.data()]),{selectedIndices:y,validOutputs:b}=Pp(m,g,d,p,f,a);return c!==n&&c.dispose(),u!==e&&u.dispose(),{selectedIndices:ft(y,"int32"),validOutputs:
|
|
3836
|
+
*/function uE(o,i,l){return O(this,arguments,function*(n,e,t,s=.5,r=Number.NEGATIVE_INFINITY,a=!1){const c=N(n,"boxes","nonMaxSuppressionAsync"),u=N(e,"scores","nonMaxSuppressionAsync"),h=Ga(c,u,t,s,r,null),d=h.maxOutputSize,p=h.iouThreshold,f=h.scoreThreshold,[m,g]=yield Promise.all([c.data(),u.data()]),{selectedIndices:y,validOutputs:b}=Pp(m,g,d,p,f,a);return c!==n&&c.dispose(),u!==e&&u.dispose(),{selectedIndices:ft(y,"int32"),validOutputs:pe(b,"int32")}})}const hE=uE;/**
|
|
3837
3837
|
* @license
|
|
3838
3838
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
3839
3839
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -3878,7 +3878,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
3878
3878
|
* See the License for the specific language governing permissions and
|
|
3879
3879
|
* limitations under the License.
|
|
3880
3880
|
* =============================================================================
|
|
3881
|
-
*/function fE(n,e="binary",t=!1,s=.5){const r=N(n,"image","threshold"),a=.2989,o=.587,i=.114,l=r.shape[0]*r.shape[1];let c=D(ft([s]),255),u,h,d,p;if(S(r.rank===3,()=>`Error in threshold: image must be rank 3,but got rank ${r.rank}.`),S(r.shape[2]===3||r.shape[2]===1,()=>`Error in threshold: image color channel must be equal to 3 or 1but got ${r.shape[2]}.`),S(r.dtype==="int32"||r.dtype==="float32",()=>`Error in dtype: image dtype must be int32 or float32,but got dtype ${r.dtype}.`),S(e==="otsu"||e==="binary",()=>`Method must be binary or otsu, but was ${e}`),r.shape[2]===3){[u,h,d]=mn(r,[1,1,1],-1);const g=D(u,a),y=D(h,o),b=D(d,i);p=X(X(g,y),b)}else p=n;if(e==="otsu"){const g=Ny(re(wp(p),"int32"),Bt([]),256);c=mE(g,l)}const f=t?Mr(p,c):pn(p,c);return re(D(f,255),"int32")}function mE(n,e){let t=ft([-1]),s=ft([0]),r=ft([0]),a,o,i,l,c,u;for(let h=0;h<n.size-1;h++){a=Ae(n,0,h+1),o=Ae(n,h+1),c=
|
|
3881
|
+
*/function fE(n,e="binary",t=!1,s=.5){const r=N(n,"image","threshold"),a=.2989,o=.587,i=.114,l=r.shape[0]*r.shape[1];let c=D(ft([s]),255),u,h,d,p;if(S(r.rank===3,()=>`Error in threshold: image must be rank 3,but got rank ${r.rank}.`),S(r.shape[2]===3||r.shape[2]===1,()=>`Error in threshold: image color channel must be equal to 3 or 1but got ${r.shape[2]}.`),S(r.dtype==="int32"||r.dtype==="float32",()=>`Error in dtype: image dtype must be int32 or float32,but got dtype ${r.dtype}.`),S(e==="otsu"||e==="binary",()=>`Method must be binary or otsu, but was ${e}`),r.shape[2]===3){[u,h,d]=mn(r,[1,1,1],-1);const g=D(u,a),y=D(h,o),b=D(d,i);p=X(X(g,y),b)}else p=n;if(e==="otsu"){const g=Ny(re(wp(p),"int32"),Bt([]),256);c=mE(g,l)}const f=t?Mr(p,c):pn(p,c);return re(D(f,255),"int32")}function mE(n,e){let t=ft([-1]),s=ft([0]),r=ft([0]),a,o,i,l,c,u;for(let h=0;h<n.size-1;h++){a=Ae(n,0,h+1),o=Ae(n,h+1),c=fe(ue(a),e),u=fe(ue(o),e);const d=ue(D(a,Ba(0,a.size)));i=fe(d,ue(a));const p=_a(o.shape,a.size),f=X(Ba(0,o.size),p),m=D(o,f);l=fe(ue(m),ue(o));const g=ie(i,l),y=ie(i,l),b=D(c,u);r=D(D(b,g),y);const w=pn(r,s);s=Vt(w,r,s),t=Vt(w,ft([h]),t)}return t}const gE=E({threshold_:fE});/**
|
|
3882
3882
|
* @license
|
|
3883
3883
|
* Copyright 2021 Google LLC. All Rights Reserved.
|
|
3884
3884
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -3923,7 +3923,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
3923
3923
|
* See the License for the specific language governing permissions and
|
|
3924
3924
|
* limitations under the License.
|
|
3925
3925
|
* =============================================================================
|
|
3926
|
-
*/function IE(n){let e;if(Array.isArray(n)){e=!1,S(n!=null&&n.length>0,()=>"Gram-Schmidt process: input must not be null, undefined, or empty");const r=n[0].shape[0];for(let a=1;a<n.length;++a)S(n[a].shape[0]===r,()=>`Gram-Schmidt: Non-unique lengths found in the input vectors: (${n[a].shape[0]} vs. ${r})`)}else e=!0,n=mn(n,n.shape[0],0).map(r=>Rs(r,[0]));S(n.length<=n[0].shape[0],()=>`Gram-Schmidt: Number of vectors (${n.length}) exceeds number of dimensions (${n[0].shape[0]}).`);const t=[],s=n;for(let r=0;r<n.length;++r)t.push(R.tidy(()=>{let a=s[r];if(r>0)for(let o=0;o<r;++o){const i=D(ue(D(t[o],a)),t[o]);a=ie(a,i)}return
|
|
3926
|
+
*/function IE(n){let e;if(Array.isArray(n)){e=!1,S(n!=null&&n.length>0,()=>"Gram-Schmidt process: input must not be null, undefined, or empty");const r=n[0].shape[0];for(let a=1;a<n.length;++a)S(n[a].shape[0]===r,()=>`Gram-Schmidt: Non-unique lengths found in the input vectors: (${n[a].shape[0]} vs. ${r})`)}else e=!0,n=mn(n,n.shape[0],0).map(r=>Rs(r,[0]));S(n.length<=n[0].shape[0],()=>`Gram-Schmidt: Number of vectors (${n.length}) exceeds number of dimensions (${n[0].shape[0]}).`);const t=[],s=n;for(let r=0;r<n.length;++r)t.push(R.tidy(()=>{let a=s[r];if(r>0)for(let o=0;o<r;++o){const i=D(ue(D(t[o],a)),t[o]);a=ie(a,i)}return fe(a,$i(a,"euclidean"))}));return e?Wt(t,0):t}const vE=E({gramSchmidt_:IE});/**
|
|
3927
3927
|
* @license
|
|
3928
3928
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
3929
3929
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -3938,7 +3938,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
3938
3938
|
* See the License for the specific language governing permissions and
|
|
3939
3939
|
* limitations under the License.
|
|
3940
3940
|
* =============================================================================
|
|
3941
|
-
*/function SE(n,e=!1){if(S(n.rank>=2,()=>`qr() requires input tensor to have a rank >= 2, but got rank ${n.rank}`),n.rank===2)return Ib(n,e);{const t=n.shape.slice(0,n.shape.length-2).reduce((l,c)=>l*c),s=Cn(_(n,[t,n.shape[n.shape.length-2],n.shape[n.shape.length-1]]),0),r=[],a=[];s.forEach(l=>{const[c,u]=Ib(l,e);r.push(c),a.push(u)});const o=_(Wt(r,0),n.shape),i=_(Wt(a,0),n.shape);return[o,i]}}function Ib(n,e=!1){return R.tidy(()=>{S(n.shape.length===2,()=>`qr2d() requires a 2D Tensor, but got a ${n.shape.length}D Tensor.`);const t=n.shape[0],s=n.shape[1];let r=ip(t),a=Ns(n);const o=Wa([[1]],[1,1]);let i=Ns(o);const l=t>=s?s:t;for(let c=0;c<l;++c){const u=a,h=i,d=r;[i,a,r]=R.tidy(()=>{const p=Ae(a,[c,c],[t-c,1]),f=$i(p),m=Ae(a,[c,c],[1,1]),g=Vt(pn(m,0),Wa([[-1]]),Wa([[1]])),y=ie(m,D(g,f)),b=
|
|
3941
|
+
*/function SE(n,e=!1){if(S(n.rank>=2,()=>`qr() requires input tensor to have a rank >= 2, but got rank ${n.rank}`),n.rank===2)return Ib(n,e);{const t=n.shape.slice(0,n.shape.length-2).reduce((l,c)=>l*c),s=Cn(_(n,[t,n.shape[n.shape.length-2],n.shape[n.shape.length-1]]),0),r=[],a=[];s.forEach(l=>{const[c,u]=Ib(l,e);r.push(c),a.push(u)});const o=_(Wt(r,0),n.shape),i=_(Wt(a,0),n.shape);return[o,i]}}function Ib(n,e=!1){return R.tidy(()=>{S(n.shape.length===2,()=>`qr2d() requires a 2D Tensor, but got a ${n.shape.length}D Tensor.`);const t=n.shape[0],s=n.shape[1];let r=ip(t),a=Ns(n);const o=Wa([[1]],[1,1]);let i=Ns(o);const l=t>=s?s:t;for(let c=0;c<l;++c){const u=a,h=i,d=r;[i,a,r]=R.tidy(()=>{const p=Ae(a,[c,c],[t-c,1]),f=$i(p),m=Ae(a,[c,c],[1,1]),g=Vt(pn(m,0),Wa([[-1]]),Wa([[1]])),y=ie(m,D(g,f)),b=fe(p,y);b.shape[0]===1?i=Ns(o):i=pt([o,Ae(b,[1,0],[b.shape[0]-1,b.shape[1]])],0);const w=Xe(fe(Te(g,y),f)),x=Ae(a,[c,0],[t-c,s]),v=D(w,i),$=De(i);if(c===0)a=ie(x,Te(v,Te($,x)));else{const T=ie(x,Te(v,Te($,x)));a=pt([Ae(a,[0,0],[c,s]),T],0)}const C=De(v),k=Ae(r,[0,c],[t,r.shape[1]-c]);if(c===0)r=ie(k,Te(Te(k,i),C));else{const T=ie(k,Te(Te(k,i),C));r=pt([Ae(r,[0,0],[t,c]),T],1)}return[i,a,r]}),be([u,h,d])}return!e&&t>s&&(r=Ae(r,[0,0],[t,s]),a=Ae(a,[0,0],[s,s])),[r,a]})}const $E=E({qr_:SE});/**
|
|
3942
3942
|
* @license
|
|
3943
3943
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
3944
3944
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -3953,7 +3953,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
3953
3953
|
* See the License for the specific language governing permissions and
|
|
3954
3954
|
* limitations under the License.
|
|
3955
3955
|
* =============================================================================
|
|
3956
|
-
*/var en;(function(n){n[n.NONE=0]="NONE",n[n.MEAN=1]="MEAN",n[n.SUM=2]="SUM",n[n.SUM_BY_NONZERO_WEIGHTS=3]="SUM_BY_NONZERO_WEIGHTS"})(en||(en={}));function NE(n,e,t=en.SUM_BY_NONZERO_WEIGHTS){const s=N(n,"losses","computeWeightedLoss");let r=null;e!=null&&(r=N(e,"weights","computeWeightedLoss"));const a=r==null?s:D(s,r);if(t===en.NONE)return a;if(t===en.SUM)return ue(a);if(t===en.MEAN){if(r==null)return tt(a);{const o=s.size/r.size,i=
|
|
3956
|
+
*/var en;(function(n){n[n.NONE=0]="NONE",n[n.MEAN=1]="MEAN",n[n.SUM=2]="SUM",n[n.SUM_BY_NONZERO_WEIGHTS=3]="SUM_BY_NONZERO_WEIGHTS"})(en||(en={}));function NE(n,e,t=en.SUM_BY_NONZERO_WEIGHTS){const s=N(n,"losses","computeWeightedLoss");let r=null;e!=null&&(r=N(e,"weights","computeWeightedLoss"));const a=r==null?s:D(s,r);if(t===en.NONE)return a;if(t===en.SUM)return ue(a);if(t===en.MEAN){if(r==null)return tt(a);{const o=s.size/r.size,i=fe(ue(a),ue(r));return o>1?fe(i,pe(o)):i}}if(t===en.SUM_BY_NONZERO_WEIGHTS){if(r==null)return fe(ue(a),pe(s.size));{const o=D(r,fn(s.shape)),i=re(ue(za(o,pe(0))),"float32");return fe(ue(a),i)}}throw Error(`Unknown reduction: ${t}`)}const Ds=E({computeWeightedLoss_:NE});/**
|
|
3957
3957
|
* @license
|
|
3958
3958
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
3959
3959
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -3968,7 +3968,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
3968
3968
|
* See the License for the specific language governing permissions and
|
|
3969
3969
|
* limitations under the License.
|
|
3970
3970
|
* =============================================================================
|
|
3971
|
-
*/function CE(n,e,t,s=en.SUM_BY_NONZERO_WEIGHTS){const r=N(n,"labels","absoluteDifference"),a=N(e,"predictions","absoluteDifference");let o=null;t!=null&&(o=N(t,"weights","absoluteDifference")),on(r.shape,a.shape,"Error in absoluteDifference: ");const i=yt(ie(r,a));return Ds(i,o,s)}const kE=E({absoluteDifference_:CE});function TE(n,e,t,s,r=en.SUM_BY_NONZERO_WEIGHTS){const a=N(n,"labels","cosineDistance"),o=N(e,"predictions","cosineDistance");let i=null;s!=null&&(i=N(s,"weights","cosineDistance")),on(a.shape,o.shape,"Error in cosineDistance: ");const l=
|
|
3971
|
+
*/function CE(n,e,t,s=en.SUM_BY_NONZERO_WEIGHTS){const r=N(n,"labels","absoluteDifference"),a=N(e,"predictions","absoluteDifference");let o=null;t!=null&&(o=N(t,"weights","absoluteDifference")),on(r.shape,a.shape,"Error in absoluteDifference: ");const i=yt(ie(r,a));return Ds(i,o,s)}const kE=E({absoluteDifference_:CE});function TE(n,e,t,s,r=en.SUM_BY_NONZERO_WEIGHTS){const a=N(n,"labels","cosineDistance"),o=N(e,"predictions","cosineDistance");let i=null;s!=null&&(i=N(s,"weights","cosineDistance")),on(a.shape,o.shape,"Error in cosineDistance: ");const l=pe(1),c=ie(l,ue(D(a,o),t,!0));return Ds(c,i,r)}const EE=E({cosineDistance_:TE});function AE(n,e,t,s=en.SUM_BY_NONZERO_WEIGHTS){let r=N(n,"labels","hingeLoss");const a=N(e,"predictions","hingeLoss");let o=null;t!=null&&(o=N(t,"weights","hingeLoss")),on(r.shape,a.shape,"Error in hingeLoss: ");const i=pe(1);r=ie(D(pe(2),r),i);const l=ps(ie(i,D(r,a)));return Ds(l,o,s)}const RE=E({hingeLoss_:AE});/**
|
|
3972
3972
|
* @license
|
|
3973
3973
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
3974
3974
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -3983,7 +3983,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
3983
3983
|
* See the License for the specific language governing permissions and
|
|
3984
3984
|
* limitations under the License.
|
|
3985
3985
|
* =============================================================================
|
|
3986
|
-
*/function DE(n,e,t,s=1,r=en.SUM_BY_NONZERO_WEIGHTS){const a=N(n,"labels","huberLoss"),o=N(e,"predictions","huberLoss");let i=null;t!=null&&(i=N(t,"weights","huberLoss")),on(a.shape,o.shape,"Error in huberLoss: ");const l=
|
|
3986
|
+
*/function DE(n,e,t,s=1,r=en.SUM_BY_NONZERO_WEIGHTS){const a=N(n,"labels","huberLoss"),o=N(e,"predictions","huberLoss");let i=null;t!=null&&(i=N(t,"weights","huberLoss")),on(a.shape,o.shape,"Error in huberLoss: ");const l=pe(s),c=yt(ie(o,a)),u=Br(c,l),h=ie(c,u),d=X(D(pe(.5),Be(u)),D(l,h));return Ds(d,i,r)}const _E=E({huberLoss_:DE});/**
|
|
3987
3987
|
* @license
|
|
3988
3988
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
3989
3989
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -3998,7 +3998,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
3998
3998
|
* See the License for the specific language governing permissions and
|
|
3999
3999
|
* limitations under the License.
|
|
4000
4000
|
* =============================================================================
|
|
4001
|
-
*/function FE(n,e,t,s=1e-7,r=en.SUM_BY_NONZERO_WEIGHTS){const a=N(n,"labels","logLoss"),o=N(e,"predictions","logLoss");let i=null;t!=null&&(i=N(t,"weights","logLoss")),on(a.shape,o.shape,"Error in logLoss: ");const l=
|
|
4001
|
+
*/function FE(n,e,t,s=1e-7,r=en.SUM_BY_NONZERO_WEIGHTS){const a=N(n,"labels","logLoss"),o=N(e,"predictions","logLoss");let i=null;t!=null&&(i=N(t,"weights","logLoss")),on(a.shape,o.shape,"Error in logLoss: ");const l=pe(1),c=pe(s),u=Xe(D(a,$n(X(o,c)))),h=D(ie(l,a),$n(X(ie(l,o),c))),d=ie(u,h);return Ds(d,i,r)}const OE=E({logLoss_:FE});/**
|
|
4002
4002
|
* @license
|
|
4003
4003
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
4004
4004
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4028,7 +4028,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
4028
4028
|
* See the License for the specific language governing permissions and
|
|
4029
4029
|
* limitations under the License.
|
|
4030
4030
|
* =============================================================================
|
|
4031
|
-
*/function zE(n,e){const t=N(n,"labels","sigmoidCrossEntropyWithLogits"),s=N(e,"logits","sigmoidCrossEntropyWithLogits");on(t.shape,s.shape,"Error in sigmoidCrossEntropyWithLogits: ");const r=ps(s),a=D(s,t),o=Mc(dn(Xe(yt(s))));return X(ie(r,a),o)}function ME(n,e,t,s=0,r=en.SUM_BY_NONZERO_WEIGHTS){let a=N(n,"multiClassLabels","sigmoidCrossEntropy");const o=N(e,"logits","sigmoidCrossEntropy");let i=null;if(t!=null&&(i=N(t,"weights","sigmoidCrossEntropy")),on(a.shape,o.shape,"Error in sigmoidCrossEntropy: "),s>0){const c=
|
|
4031
|
+
*/function zE(n,e){const t=N(n,"labels","sigmoidCrossEntropyWithLogits"),s=N(e,"logits","sigmoidCrossEntropyWithLogits");on(t.shape,s.shape,"Error in sigmoidCrossEntropyWithLogits: ");const r=ps(s),a=D(s,t),o=Mc(dn(Xe(yt(s))));return X(ie(r,a),o)}function ME(n,e,t,s=0,r=en.SUM_BY_NONZERO_WEIGHTS){let a=N(n,"multiClassLabels","sigmoidCrossEntropy");const o=N(e,"logits","sigmoidCrossEntropy");let i=null;if(t!=null&&(i=N(t,"weights","sigmoidCrossEntropy")),on(a.shape,o.shape,"Error in sigmoidCrossEntropy: "),s>0){const c=pe(s),u=pe(1),h=pe(.5);a=X(D(a,ie(u,c)),D(h,c))}const l=zE(a,o);return Ds(l,i,r)}const BE=E({sigmoidCrossEntropy_:ME});/**
|
|
4032
4032
|
* @license
|
|
4033
4033
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
4034
4034
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4043,7 +4043,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
4043
4043
|
* See the License for the specific language governing permissions and
|
|
4044
4044
|
* limitations under the License.
|
|
4045
4045
|
* =============================================================================
|
|
4046
|
-
*/function VE(n,e,t=-1){if(t===-1&&(t=e.rank-1),t!==e.rank-1)throw Error(`Softmax cross entropy along a non-last dimension is not yet supported. Labels / logits was rank ${e.rank} and dim was ${t}`);return Es((r,a,o)=>{const l=Bc(a,[t],!0),c=ie(re(a,"float32"),l);o([r,c]);const u=Xe(D(c,r));return{value:ue(u,[t]),gradFunc:(p,f)=>{const[m,g]=f,y=it(p.shape,[t]);return[D(_(p,y),ie(re(m,"float32"),dn(g))),D(_(p,y),ie(dn(g),re(m,"float32")))]}}})(n,e)}function WE(n,e,t,s=0,r=en.SUM_BY_NONZERO_WEIGHTS){let a=N(n,"onehotLabels","softmaxCrossEntropy");const o=N(e,"logits","softmaxCrossEntropy");let i=null;if(t!=null&&(i=N(t,"weights","softmaxCrossEntropy")),on(a.shape,o.shape,"Error in softmaxCrossEntropy: "),s>0){const c=
|
|
4046
|
+
*/function VE(n,e,t=-1){if(t===-1&&(t=e.rank-1),t!==e.rank-1)throw Error(`Softmax cross entropy along a non-last dimension is not yet supported. Labels / logits was rank ${e.rank} and dim was ${t}`);return Es((r,a,o)=>{const l=Bc(a,[t],!0),c=ie(re(a,"float32"),l);o([r,c]);const u=Xe(D(c,r));return{value:ue(u,[t]),gradFunc:(p,f)=>{const[m,g]=f,y=it(p.shape,[t]);return[D(_(p,y),ie(re(m,"float32"),dn(g))),D(_(p,y),ie(dn(g),re(m,"float32")))]}}})(n,e)}function WE(n,e,t,s=0,r=en.SUM_BY_NONZERO_WEIGHTS){let a=N(n,"onehotLabels","softmaxCrossEntropy");const o=N(e,"logits","softmaxCrossEntropy");let i=null;if(t!=null&&(i=N(t,"weights","softmaxCrossEntropy")),on(a.shape,o.shape,"Error in softmaxCrossEntropy: "),s>0){const c=pe(s),u=pe(1),h=pe(a.shape[1]);a=X(D(a,ie(u,c)),fe(c,h))}const l=VE(a,o);return Ds(l,i,r)}const UE=E({softmaxCrossEntropy_:WE});/**
|
|
4047
4047
|
* @license
|
|
4048
4048
|
* Copyright 2021 Google LLC. All Rights Reserved.
|
|
4049
4049
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4214,7 +4214,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
4214
4214
|
* See the License for the specific language governing permissions and
|
|
4215
4215
|
* limitations under the License.
|
|
4216
4216
|
* =============================================================================
|
|
4217
|
-
*/class ir extends Ha{minimize(e,t=!1,s){const{value:r,grads:a}=this.computeGradients(e,s);if(s!=null){const o=s.map(i=>({name:i.name,tensor:a[i.name]}));this.applyGradients(o)}else this.applyGradients(a);return be(a),t?r:(r.dispose(),null)}get iterations(){return this.iterations_==null&&(this.iterations_=0),this.iterations_}incrementIterations(){this.iterations_=this.iterations+1}computeGradients(e,t){return MC(e,t)}dispose(){this.iterations_!=null&&be(this.iterations_)}saveIterations(){return O(this,null,function*(){return this.iterations_==null&&(this.iterations_=0),{name:"iter",tensor:
|
|
4217
|
+
*/class ir extends Ha{minimize(e,t=!1,s){const{value:r,grads:a}=this.computeGradients(e,s);if(s!=null){const o=s.map(i=>({name:i.name,tensor:a[i.name]}));this.applyGradients(o)}else this.applyGradients(a);return be(a),t?r:(r.dispose(),null)}get iterations(){return this.iterations_==null&&(this.iterations_=0),this.iterations_}incrementIterations(){this.iterations_=this.iterations+1}computeGradients(e,t){return MC(e,t)}dispose(){this.iterations_!=null&&be(this.iterations_)}saveIterations(){return O(this,null,function*(){return this.iterations_==null&&(this.iterations_=0),{name:"iter",tensor:pe(this.iterations_,"int32")}})}getWeights(){return O(this,null,function*(){throw new Error("getWeights() is not implemented for this optimizer yet.")})}setWeights(e){return O(this,null,function*(){throw new Error(`setWeights() is not implemented for this optimizer class ${this.getClassName()}`)})}extractIterations(e){return O(this,null,function*(){return this.iterations_=(yield e[0].tensor.data())[0],e.slice(1)})}}Object.defineProperty(ir,Symbol.hasInstance,{value:n=>n.minimize!=null&&n.computeGradients!=null&&n.applyGradients!=null});/**
|
|
4218
4218
|
* @license
|
|
4219
4219
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
4220
4220
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4229,7 +4229,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
4229
4229
|
* See the License for the specific language governing permissions and
|
|
4230
4230
|
* limitations under the License.
|
|
4231
4231
|
* =============================================================================
|
|
4232
|
-
*/class Sb extends ir{static get className(){return"Adadelta"}constructor(e,t,s=null){super(),this.learningRate=e,this.rho=t,this.epsilon=s,this.accumulatedGrads=[],this.accumulatedUpdates=[],s==null&&(this.epsilon=R.backend.epsilon())}applyGradients(e){(Array.isArray(e)?e.map(s=>s.name):Object.keys(e)).forEach((s,r)=>{const a=R.registeredVariables[s],o=!1;this.accumulatedGrads[r]==null&&(this.accumulatedGrads[r]={originalName:`${s}/accum_grad`,variable:P(()=>Re(a).variable(o))}),this.accumulatedUpdates[r]==null&&(this.accumulatedUpdates[r]={originalName:`${s}/accum_var`,variable:P(()=>Re(a).variable(o))});const i=Array.isArray(e)?e[r].tensor:e[s];if(i==null)return;const l=this.accumulatedGrads[r].variable,c=this.accumulatedUpdates[r].variable;P(()=>{const u=X(D(l,this.rho),D(Be(i),1-this.rho)),h=D(
|
|
4232
|
+
*/class Sb extends ir{static get className(){return"Adadelta"}constructor(e,t,s=null){super(),this.learningRate=e,this.rho=t,this.epsilon=s,this.accumulatedGrads=[],this.accumulatedUpdates=[],s==null&&(this.epsilon=R.backend.epsilon())}applyGradients(e){(Array.isArray(e)?e.map(s=>s.name):Object.keys(e)).forEach((s,r)=>{const a=R.registeredVariables[s],o=!1;this.accumulatedGrads[r]==null&&(this.accumulatedGrads[r]={originalName:`${s}/accum_grad`,variable:P(()=>Re(a).variable(o))}),this.accumulatedUpdates[r]==null&&(this.accumulatedUpdates[r]={originalName:`${s}/accum_var`,variable:P(()=>Re(a).variable(o))});const i=Array.isArray(e)?e[r].tensor:e[s];if(i==null)return;const l=this.accumulatedGrads[r].variable,c=this.accumulatedUpdates[r].variable;P(()=>{const u=X(D(l,this.rho),D(Be(i),1-this.rho)),h=D(fe(_t(X(c,this.epsilon)),_t(X(l,this.epsilon))),i),d=X(D(c,this.rho),D(Be(h),1-this.rho));l.assign(u),c.assign(d);const p=X(D(h,-this.learningRate),a);a.assign(p)})}),this.incrementIterations()}dispose(){this.accumulatedUpdates!=null&&(be(this.accumulatedGrads.map(e=>e.variable)),be(this.accumulatedUpdates.map(e=>e.variable)))}getWeights(){return O(this,null,function*(){const e=[...this.accumulatedGrads,...this.accumulatedUpdates];return[yield this.saveIterations()].concat(e.map(t=>({name:t.originalName,tensor:t.variable})))})}setWeights(e){return O(this,null,function*(){e=yield this.extractIterations(e);const t=e.length/2,s=!1;this.accumulatedGrads=e.slice(0,t).map(r=>({originalName:r.name,variable:r.tensor.variable(s)})),this.accumulatedUpdates=e.slice(t,t*2).map(r=>({originalName:r.name,variable:r.tensor.variable(s)}))})}getConfig(){return{learningRate:this.learningRate,rho:this.rho,epsilon:this.epsilon}}static fromConfig(e,t){return new e(t.learningRate,t.rho,t.epsilon)}}/**
|
|
4233
4233
|
* @license
|
|
4234
4234
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
4235
4235
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4244,7 +4244,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
4244
4244
|
* See the License for the specific language governing permissions and
|
|
4245
4245
|
* limitations under the License.
|
|
4246
4246
|
* =============================================================================
|
|
4247
|
-
*/class $b extends ir{static get className(){return"Adagrad"}constructor(e,t=.1){super(),this.learningRate=e,this.initialAccumulatorValue=t,this.accumulatedGrads=[]}applyGradients(e){(Array.isArray(e)?e.map(s=>s.name):Object.keys(e)).forEach((s,r)=>{const a=R.registeredVariables[s];this.accumulatedGrads[r]==null&&(this.accumulatedGrads[r]={originalName:`${s}/accumulator`,variable:P(()=>_a(a.shape,this.initialAccumulatorValue).variable(!1))});const o=Array.isArray(e)?e[r].tensor:e[s];if(o==null)return;const i=this.accumulatedGrads[r].variable;P(()=>{const l=X(i,Be(o));i.assign(l);const c=X(D(
|
|
4247
|
+
*/class $b extends ir{static get className(){return"Adagrad"}constructor(e,t=.1){super(),this.learningRate=e,this.initialAccumulatorValue=t,this.accumulatedGrads=[]}applyGradients(e){(Array.isArray(e)?e.map(s=>s.name):Object.keys(e)).forEach((s,r)=>{const a=R.registeredVariables[s];this.accumulatedGrads[r]==null&&(this.accumulatedGrads[r]={originalName:`${s}/accumulator`,variable:P(()=>_a(a.shape,this.initialAccumulatorValue).variable(!1))});const o=Array.isArray(e)?e[r].tensor:e[s];if(o==null)return;const i=this.accumulatedGrads[r].variable;P(()=>{const l=X(i,Be(o));i.assign(l);const c=X(D(fe(o,_t(X(l,R.backend.epsilon()))),-this.learningRate),a);a.assign(c)})}),this.incrementIterations()}dispose(){this.accumulatedGrads!=null&&be(this.accumulatedGrads.map(e=>e.variable))}getWeights(){return O(this,null,function*(){return[yield this.saveIterations()].concat(this.accumulatedGrads.map(e=>({name:e.originalName,tensor:e.variable})))})}setWeights(e){return O(this,null,function*(){e=yield this.extractIterations(e);const t=!1;this.accumulatedGrads=e.map(s=>({originalName:s.name,variable:s.tensor.variable(t)}))})}getConfig(){return{learningRate:this.learningRate,initialAccumulatorValue:this.initialAccumulatorValue}}static fromConfig(e,t){return new e(t.learningRate,t.initialAccumulatorValue)}}/**
|
|
4248
4248
|
* @license
|
|
4249
4249
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
4250
4250
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4259,7 +4259,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
4259
4259
|
* See the License for the specific language governing permissions and
|
|
4260
4260
|
* limitations under the License.
|
|
4261
4261
|
* =============================================================================
|
|
4262
|
-
*/class Nb extends ir{static get className(){return"Adam"}constructor(e,t,s,r=null){super(),this.learningRate=e,this.beta1=t,this.beta2=s,this.epsilon=r,this.accumulatedFirstMoment=[],this.accumulatedSecondMoment=[],P(()=>{this.accBeta1=
|
|
4262
|
+
*/class Nb extends ir{static get className(){return"Adam"}constructor(e,t,s,r=null){super(),this.learningRate=e,this.beta1=t,this.beta2=s,this.epsilon=r,this.accumulatedFirstMoment=[],this.accumulatedSecondMoment=[],P(()=>{this.accBeta1=pe(t).variable(),this.accBeta2=pe(s).variable()}),r==null&&(this.epsilon=R.backend.epsilon())}applyGradients(e){const t=Array.isArray(e)?e.map(s=>s.name):Object.keys(e);P(()=>{const s=ie(1,this.accBeta1),r=ie(1,this.accBeta2);t.forEach((a,o)=>{const i=R.registeredVariables[a],l=!1;this.accumulatedFirstMoment[o]==null&&(this.accumulatedFirstMoment[o]={originalName:`${a}/m`,variable:P(()=>Re(i).variable(l))}),this.accumulatedSecondMoment[o]==null&&(this.accumulatedSecondMoment[o]={originalName:`${a}/v`,variable:P(()=>Re(i).variable(l))});const c=Array.isArray(e)?e[o].tensor:e[a];if(c==null)return;const u=this.accumulatedFirstMoment[o].variable,h=this.accumulatedSecondMoment[o].variable,d=X(D(u,this.beta1),D(c,1-this.beta1)),p=X(D(h,this.beta2),D(Be(c),1-this.beta2)),f=fe(d,s),m=fe(p,r);u.assign(d),h.assign(p);const g=X(D(fe(f,X(_t(m),this.epsilon)),-this.learningRate),i);i.assign(g)}),this.accBeta1.assign(D(this.accBeta1,this.beta1)),this.accBeta2.assign(D(this.accBeta2,this.beta2))}),this.incrementIterations()}dispose(){this.accBeta1.dispose(),this.accBeta2.dispose(),this.accumulatedFirstMoment!=null&&be(this.accumulatedFirstMoment.map(e=>e.variable)),this.accumulatedSecondMoment!=null&&be(this.accumulatedSecondMoment.map(e=>e.variable))}getWeights(){return O(this,null,function*(){const e=[...this.accumulatedFirstMoment,...this.accumulatedSecondMoment];return[yield this.saveIterations()].concat(e.map(t=>({name:t.originalName,tensor:t.variable})))})}setWeights(e){return O(this,null,function*(){e=yield this.extractIterations(e),P(()=>{this.accBeta1.assign(rr(this.beta1,this.iterations_+1)),this.accBeta2.assign(rr(this.beta2,this.iterations_+1))});const t=e.length/2,s=!1;this.accumulatedFirstMoment=e.slice(0,t).map(r=>({originalName:r.name,variable:r.tensor.variable(s)})),this.accumulatedSecondMoment=e.slice(t,t*2).map(r=>({originalName:r.name,variable:r.tensor.variable(s)}))})}getConfig(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon}}static fromConfig(e,t){return new e(t.learningRate,t.beta1,t.beta2,t.epsilon)}}/**
|
|
4263
4263
|
* @license
|
|
4264
4264
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
4265
4265
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4274,7 +4274,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
4274
4274
|
* See the License for the specific language governing permissions and
|
|
4275
4275
|
* limitations under the License.
|
|
4276
4276
|
* =============================================================================
|
|
4277
|
-
*/class Cb extends ir{static get className(){return"Adamax"}constructor(e,t,s,r=null,a=0){super(),this.learningRate=e,this.beta1=t,this.beta2=s,this.epsilon=r,this.decay=a,this.accumulatedFirstMoment=[],this.accumulatedWeightedInfNorm=[],P(()=>{this.iteration=
|
|
4277
|
+
*/class Cb extends ir{static get className(){return"Adamax"}constructor(e,t,s,r=null,a=0){super(),this.learningRate=e,this.beta1=t,this.beta2=s,this.epsilon=r,this.decay=a,this.accumulatedFirstMoment=[],this.accumulatedWeightedInfNorm=[],P(()=>{this.iteration=pe(0).variable(),this.accBeta1=pe(t).variable()}),r==null&&(this.epsilon=R.backend.epsilon())}applyGradients(e){const t=Array.isArray(e)?e.map(s=>s.name):Object.keys(e);P(()=>{const s=ie(1,this.accBeta1),r=fe(-this.learningRate,X(D(this.iteration,this.decay),1));t.forEach((a,o)=>{const i=R.registeredVariables[a],l=!1;this.accumulatedFirstMoment[o]==null&&(this.accumulatedFirstMoment[o]={originalName:`${a}/m`,variable:Re(i).variable(l)}),this.accumulatedWeightedInfNorm[o]==null&&(this.accumulatedWeightedInfNorm[o]={originalName:`${a}/v`,variable:Re(i).variable(l)});const c=Array.isArray(e)?e[o].tensor:e[a];if(c==null)return;const u=this.accumulatedFirstMoment[o].variable,h=this.accumulatedWeightedInfNorm[o].variable,d=X(D(u,this.beta1),D(c,1-this.beta1)),p=D(h,this.beta2),f=yt(c),m=As(p,f);u.assign(d),h.assign(m);const g=X(D(fe(r,s),fe(d,X(m,this.epsilon))),i);i.assign(g)}),this.iteration.assign(X(this.iteration,1)),this.accBeta1.assign(D(this.accBeta1,this.beta1))}),this.incrementIterations()}dispose(){this.accBeta1.dispose(),this.iteration.dispose(),this.accumulatedFirstMoment!=null&&be(this.accumulatedFirstMoment.map(e=>e.variable)),this.accumulatedWeightedInfNorm!=null&&be(this.accumulatedWeightedInfNorm.map(e=>e.variable))}getWeights(){return O(this,null,function*(){throw new Error("getWeights() is not implemented for Adamax yet.")})}setWeights(e){return O(this,null,function*(){throw new Error("setWeights() is not implemented for Adamax yet.")})}getConfig(){return{learningRate:this.learningRate,beta1:this.beta1,beta2:this.beta2,epsilon:this.epsilon,decay:this.decay}}static fromConfig(e,t){return new e(t.learningRate,t.beta1,t.beta2,t.epsilon,t.decay)}}/**
|
|
4278
4278
|
* @license
|
|
4279
4279
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
4280
4280
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4289,7 +4289,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
4289
4289
|
* See the License for the specific language governing permissions and
|
|
4290
4290
|
* limitations under the License.
|
|
4291
4291
|
* =============================================================================
|
|
4292
|
-
*/class Bp extends ir{static get className(){return"SGD"}constructor(e){super(),this.learningRate=e,this.setLearningRate(e)}applyGradients(e){(Array.isArray(e)?e.map(s=>s.name):Object.keys(e)).forEach((s,r)=>{const a=Array.isArray(e)?e[r].tensor:e[s];if(a==null)return;const o=R.registeredVariables[s];P(()=>{const i=X(D(this.c,a),o);o.assign(i)})}),this.incrementIterations()}setLearningRate(e){this.learningRate=e,this.c!=null&&this.c.dispose(),this.c=Nt(
|
|
4292
|
+
*/class Bp extends ir{static get className(){return"SGD"}constructor(e){super(),this.learningRate=e,this.setLearningRate(e)}applyGradients(e){(Array.isArray(e)?e.map(s=>s.name):Object.keys(e)).forEach((s,r)=>{const a=Array.isArray(e)?e[r].tensor:e[s];if(a==null)return;const o=R.registeredVariables[s];P(()=>{const i=X(D(this.c,a),o);o.assign(i)})}),this.incrementIterations()}setLearningRate(e){this.learningRate=e,this.c!=null&&this.c.dispose(),this.c=Nt(pe(-e))}dispose(){this.c.dispose()}getWeights(){return O(this,null,function*(){return[yield this.saveIterations()]})}setWeights(e){return O(this,null,function*(){if(e=yield this.extractIterations(e),e.length!==0)throw new Error("SGD optimizer does not have settable weights.")})}getConfig(){return{learningRate:this.learningRate}}static fromConfig(e,t){return new e(t.learningRate)}}/**
|
|
4293
4293
|
* @license
|
|
4294
4294
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
4295
4295
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4304,7 +4304,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
4304
4304
|
* See the License for the specific language governing permissions and
|
|
4305
4305
|
* limitations under the License.
|
|
4306
4306
|
* =============================================================================
|
|
4307
|
-
*/class kb extends Bp{static get className(){return"Momentum"}constructor(e,t,s=!1){super(e),this.learningRate=e,this.momentum=t,this.useNesterov=s,this.accumulations=[],this.m=
|
|
4307
|
+
*/class kb extends Bp{static get className(){return"Momentum"}constructor(e,t,s=!1){super(e),this.learningRate=e,this.momentum=t,this.useNesterov=s,this.accumulations=[],this.m=pe(this.momentum)}applyGradients(e){(Array.isArray(e)?e.map(s=>s.name):Object.keys(e)).forEach((s,r)=>{const a=R.registeredVariables[s];this.accumulations[r]==null&&(this.accumulations[r]={originalName:`${s}/momentum`,variable:P(()=>Re(a).variable(!1))});const o=this.accumulations[r].variable,i=Array.isArray(e)?e[r].tensor:e[s];i!=null&&P(()=>{let l;const c=X(D(this.m,o),i);this.useNesterov?l=X(D(this.c,X(i,D(c,this.m))),a):l=X(D(this.c,c),a),o.assign(c),a.assign(l)})}),this.incrementIterations()}dispose(){this.m.dispose(),this.accumulations!=null&&be(this.accumulations.map(e=>e.variable))}setMomentum(e){this.momentum=e}getWeights(){return O(this,null,function*(){return[yield this.saveIterations()].concat(this.accumulations.map(e=>({name:e.originalName,tensor:e.variable})))})}setWeights(e){return O(this,null,function*(){e=yield this.extractIterations(e);const t=!1;this.accumulations=e.map(s=>({originalName:s.name,variable:s.tensor.variable(t)}))})}getConfig(){return{learningRate:this.learningRate,momentum:this.momentum,useNesterov:this.useNesterov}}static fromConfig(e,t){return new e(t.learningRate,t.momentum,t.useNesterov)}}/**
|
|
4308
4308
|
* @license
|
|
4309
4309
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
4310
4310
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4319,7 +4319,7 @@ var m8=Object.defineProperty;var IS=Object.getOwnPropertySymbols;var g8=Object.p
|
|
|
4319
4319
|
* See the License for the specific language governing permissions and
|
|
4320
4320
|
* limitations under the License.
|
|
4321
4321
|
* =============================================================================
|
|
4322
|
-
*/class Tb extends ir{static get className(){return"RMSProp"}constructor(e,t=.9,s=0,r=null,a=!1){if(super(),this.learningRate=e,this.decay=t,this.momentum=s,this.epsilon=r,this.accumulatedMeanSquares=[],this.accumulatedMoments=[],this.accumulatedMeanGrads=[],this.centered=a,r==null&&(this.epsilon=R.backend.epsilon()),e==null)throw new Error("learningRate for RMSPropOptimizer must be defined.")}applyGradients(e){(Array.isArray(e)?e.map(s=>s.name):Object.keys(e)).forEach((s,r)=>{const a=R.registeredVariables[s],o=!1;this.accumulatedMeanSquares[r]==null&&(this.accumulatedMeanSquares[r]={originalName:`${s}/rms`,variable:P(()=>Re(a).variable(o))}),this.accumulatedMoments[r]==null&&(this.accumulatedMoments[r]={originalName:`${s}/momentum`,variable:P(()=>Re(a).variable(o))}),this.accumulatedMeanGrads[r]==null&&this.centered&&(this.accumulatedMeanGrads[r]={originalName:`${s}/mg`,variable:P(()=>Re(a).variable(o))});const i=Array.isArray(e)?e[r].tensor:e[s];if(i==null)return;const l=this.accumulatedMeanSquares[r].variable,c=this.accumulatedMoments[r].variable;P(()=>{const u=X(D(l,this.decay),D(Be(i),1-this.decay));if(this.centered){const h=this.accumulatedMeanGrads[r].variable,d=X(D(h,this.decay),D(i,1-this.decay)),p=
|
|
4322
|
+
*/class Tb extends ir{static get className(){return"RMSProp"}constructor(e,t=.9,s=0,r=null,a=!1){if(super(),this.learningRate=e,this.decay=t,this.momentum=s,this.epsilon=r,this.accumulatedMeanSquares=[],this.accumulatedMoments=[],this.accumulatedMeanGrads=[],this.centered=a,r==null&&(this.epsilon=R.backend.epsilon()),e==null)throw new Error("learningRate for RMSPropOptimizer must be defined.")}applyGradients(e){(Array.isArray(e)?e.map(s=>s.name):Object.keys(e)).forEach((s,r)=>{const a=R.registeredVariables[s],o=!1;this.accumulatedMeanSquares[r]==null&&(this.accumulatedMeanSquares[r]={originalName:`${s}/rms`,variable:P(()=>Re(a).variable(o))}),this.accumulatedMoments[r]==null&&(this.accumulatedMoments[r]={originalName:`${s}/momentum`,variable:P(()=>Re(a).variable(o))}),this.accumulatedMeanGrads[r]==null&&this.centered&&(this.accumulatedMeanGrads[r]={originalName:`${s}/mg`,variable:P(()=>Re(a).variable(o))});const i=Array.isArray(e)?e[r].tensor:e[s];if(i==null)return;const l=this.accumulatedMeanSquares[r].variable,c=this.accumulatedMoments[r].variable;P(()=>{const u=X(D(l,this.decay),D(Be(i),1-this.decay));if(this.centered){const h=this.accumulatedMeanGrads[r].variable,d=X(D(h,this.decay),D(i,1-this.decay)),p=fe(D(i,this.learningRate),_t(ie(u,X(Be(d),this.epsilon)))),f=X(D(c,this.momentum),p);l.assign(u),h.assign(d),c.assign(f);const m=ie(a,f);a.assign(m)}else{const h=X(D(l,this.decay),D(Be(i),1-this.decay)),d=X(D(c,this.momentum),fe(D(i,this.learningRate),_t(X(h,this.epsilon))));l.assign(h),c.assign(d);const p=ie(a,d);a.assign(p)}})}),this.incrementIterations()}dispose(){this.accumulatedMeanSquares!=null&&be(this.accumulatedMeanSquares.map(e=>e.variable)),this.accumulatedMeanGrads!=null&&this.centered&&be(this.accumulatedMeanGrads.map(e=>e.variable)),this.accumulatedMoments!=null&&be(this.accumulatedMoments.map(e=>e.variable))}getWeights(){return O(this,null,function*(){const e=[...this.accumulatedMeanSquares,...this.accumulatedMoments];return this.centered&&e.push(...this.accumulatedMeanGrads),[yield this.saveIterations()].concat(e.map(t=>({name:t.originalName,tensor:t.variable})))})}setWeights(e){return O(this,null,function*(){e=yield this.extractIterations(e);const t=this.centered?e.length/3:e.length/2,s=!1;this.accumulatedMeanSquares=e.slice(0,t).map(r=>({originalName:r.name,variable:r.tensor.variable(s)})),this.accumulatedMoments=e.slice(t,t*2).map(r=>({originalName:r.name,variable:r.tensor.variable(s)})),this.centered&&(this.accumulatedMeanGrads=e.slice(t*2,t*3).map(r=>({originalName:r.name,variable:r.tensor.variable(s)})))})}getConfig(){return{learningRate:this.learningRate,decay:this.decay,momentum:this.momentum,epsilon:this.epsilon,centered:this.centered}}static fromConfig(e,t){return new e(t.learningRate,t.decay,t.momentum,t.epsilon,t.centered)}}/**
|
|
4323
4323
|
* @license
|
|
4324
4324
|
* Copyright 2022 Google LLC.
|
|
4325
4325
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4758,7 +4758,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
4758
4758
|
* See the License for the specific language governing permissions and
|
|
4759
4759
|
* limitations under the License.
|
|
4760
4760
|
* =============================================================================
|
|
4761
|
-
*/const VA={kernelName:bo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>{const s=Be(re(t,"float32")),r=_t(ie(
|
|
4761
|
+
*/const VA={kernelName:bo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>{const s=Be(re(t,"float32")),r=_t(ie(pe(1),s));return Xe(fe(n,r))}}}};/**
|
|
4762
4762
|
* @license
|
|
4763
4763
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
4764
4764
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4773,7 +4773,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
4773
4773
|
* See the License for the specific language governing permissions and
|
|
4774
4774
|
* limitations under the License.
|
|
4775
4775
|
* =============================================================================
|
|
4776
|
-
*/const WA={kernelName:xo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>{const s=_t(ie(Be(re(t,"float32")),1));return
|
|
4776
|
+
*/const WA={kernelName:xo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>{const s=_t(ie(Be(re(t,"float32")),1));return fe(n,s)}}}};/**
|
|
4777
4777
|
* @license
|
|
4778
4778
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
4779
4779
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4848,7 +4848,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
4848
4848
|
* See the License for the specific language governing permissions and
|
|
4849
4849
|
* limitations under the License.
|
|
4850
4850
|
* =============================================================================
|
|
4851
|
-
*/const qA={kernelName:wo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>
|
|
4851
|
+
*/const qA={kernelName:wo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>fe(n,_t(ie(pe(1),Be(re(t,"float32")))))}}};/**
|
|
4852
4852
|
* @license
|
|
4853
4853
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
4854
4854
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4863,7 +4863,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
4863
4863
|
* See the License for the specific language governing permissions and
|
|
4864
4864
|
* limitations under the License.
|
|
4865
4865
|
* =============================================================================
|
|
4866
|
-
*/const KA={kernelName:Io,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>{const s=_t(X(
|
|
4866
|
+
*/const KA={kernelName:Io,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>{const s=_t(X(pe(1),Be(re(t,"float32"))));return fe(n,s)}}}};/**
|
|
4867
4867
|
* @license
|
|
4868
4868
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
4869
4869
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4878,7 +4878,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
4878
4878
|
* See the License for the specific language governing permissions and
|
|
4879
4879
|
* limitations under the License.
|
|
4880
4880
|
* =============================================================================
|
|
4881
|
-
*/const XA={kernelName:$o,inputsToSave:["a","b"],gradFunc:(n,e)=>{const[t,s]=e,r=Ie(t.shape,s.shape);return{a:()=>{const i=X(Be(t),Be(s));let l=D(n,
|
|
4881
|
+
*/const XA={kernelName:$o,inputsToSave:["a","b"],gradFunc:(n,e)=>{const[t,s]=e,r=Ie(t.shape,s.shape);return{a:()=>{const i=X(Be(t),Be(s));let l=D(n,fe(s,i));const c=bt(t.shape,r);return c.length>0&&(l=ue(l,c)),_(l,t.shape)},b:()=>{const i=X(Be(t),Be(s));let l=Xe(D(n,fe(t,i)));const c=bt(s.shape,r);return c.length>0&&(l=ue(l,c)),_(l,s.shape)}}}};/**
|
|
4882
4882
|
* @license
|
|
4883
4883
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
4884
4884
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4893,7 +4893,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
4893
4893
|
* See the License for the specific language governing permissions and
|
|
4894
4894
|
* limitations under the License.
|
|
4895
4895
|
* =============================================================================
|
|
4896
|
-
*/const YA={kernelName:vo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>
|
|
4896
|
+
*/const YA={kernelName:vo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>fe(n,X(Be(re(t,"float32")),1))}}};/**
|
|
4897
4897
|
* @license
|
|
4898
4898
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
4899
4899
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -4908,7 +4908,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
4908
4908
|
* See the License for the specific language governing permissions and
|
|
4909
4909
|
* limitations under the License.
|
|
4910
4910
|
* =============================================================================
|
|
4911
|
-
*/const ZA={kernelName:So,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>
|
|
4911
|
+
*/const ZA={kernelName:So,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>fe(n,ie(pe(1),Be(re(t,"float32"))))}}};/**
|
|
4912
4912
|
* @license
|
|
4913
4913
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
4914
4914
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -5328,7 +5328,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
5328
5328
|
* See the License for the specific language governing permissions and
|
|
5329
5329
|
* limitations under the License.
|
|
5330
5330
|
* =============================================================================
|
|
5331
|
-
*/const TR={kernelName:Lo,inputsToSave:["a","b"],gradFunc:(n,e)=>{const[t,s]=e,r=Ie(t.shape,s.shape);return{a:()=>{const i=
|
|
5331
|
+
*/const TR={kernelName:Lo,inputsToSave:["a","b"],gradFunc:(n,e)=>{const[t,s]=e,r=Ie(t.shape,s.shape);return{a:()=>{const i=fe(n,re(s,"float32")),l=bt(t.shape,r);return l.length>0?_(ue(i,l),t.shape):i},b:()=>{let i=D(n,re(t,"float32"));const l=bt(s.shape,r);l.length>0&&(i=_(ue(i,l),s.shape));const c=Be(s);return Xe(fe(i,re(c,"float32")))}}}};/**
|
|
5332
5332
|
* @license
|
|
5333
5333
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5334
5334
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -5343,7 +5343,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
5343
5343
|
* See the License for the specific language governing permissions and
|
|
5344
5344
|
* limitations under the License.
|
|
5345
5345
|
* =============================================================================
|
|
5346
|
-
*/const ER={kernelName:Ll,inputsToSave:["x","mean","variance","scale"],gradFunc:(n,e,t)=>{const{varianceEpsilon:s}=t,[r,a,o,i]=e,l=i==null?
|
|
5346
|
+
*/const ER={kernelName:Ll,inputsToSave:["x","mean","variance","scale"],gradFunc:(n,e,t)=>{const{varianceEpsilon:s}=t,[r,a,o,i]=e,l=i==null?pe(1):i,c=bt(a.shape,r.shape),u=[];if(a.rank===1){for(let x=0;x<r.shape.length-1;++x)u.push(r.shape[x]);u.push(1)}const h=ie(r,a),d=D(n,l),p=Ip(X(o,pe(s))),f=D(D(D(p,p),p),pe(-.5));return{x:()=>a.rank===1?_(D(D(n,On(_(p,[1,1,1,a.shape[0]]),u)),l),r.shape):_(D(D(n,p),l),r.shape),mean:()=>{let x=D(D(p,pe(-1)),d);return a.rank===1&&(x=ue(x,c)),_(x,a.shape)},variance:()=>{let x=D(D(f,h),d);return a.rank===1&&(x=ue(x,c)),_(x,a.shape)},scale:()=>{const x=D(h,p);let v=D(n,x);return a.rank===1&&(v=ue(v,c)),_(v,a.shape)},offset:()=>{let x=n;return a.rank===1&&(x=ue(x,c)),_(x,a.shape)}}}};/**
|
|
5347
5347
|
* @license
|
|
5348
5348
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5349
5349
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -5463,7 +5463,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
5463
5463
|
* See the License for the specific language governing permissions and
|
|
5464
5464
|
* limitations under the License.
|
|
5465
5465
|
* =============================================================================
|
|
5466
|
-
*/const PR={kernelName:Uo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>
|
|
5466
|
+
*/const PR={kernelName:Uo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>fe(n,X(t,1))}}};/**
|
|
5467
5467
|
* @license
|
|
5468
5468
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5469
5469
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -5478,7 +5478,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
5478
5478
|
* See the License for the specific language governing permissions and
|
|
5479
5479
|
* limitations under the License.
|
|
5480
5480
|
* =============================================================================
|
|
5481
|
-
*/const zR={kernelName:Wo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>
|
|
5481
|
+
*/const zR={kernelName:Wo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>fe(n,re(t,"float32"))}}};/**
|
|
5482
5482
|
* @license
|
|
5483
5483
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5484
5484
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -5643,7 +5643,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
5643
5643
|
* See the License for the specific language governing permissions and
|
|
5644
5644
|
* limitations under the License.
|
|
5645
5645
|
* =============================================================================
|
|
5646
|
-
*/const YR={kernelName:Xl,inputsToSave:["x"],gradFunc:(n,e,t)=>{const[s]=e,{axis:r}=t,a=Ce(r,s.shape),i=kt(s.shape,a)[1],l=U(i);return{x:()=>{const u=s.shape.slice();a.forEach(p=>{u[p]=1});const h=_(n,u);return
|
|
5646
|
+
*/const YR={kernelName:Xl,inputsToSave:["x"],gradFunc:(n,e,t)=>{const[s]=e,{axis:r}=t,a=Ce(r,s.shape),i=kt(s.shape,a)[1],l=U(i);return{x:()=>{const u=s.shape.slice();a.forEach(p=>{u[p]=1});const h=_(n,u);return fe(D(h,fn(s.shape,"float32")),l)}}}};/**
|
|
5647
5647
|
* @license
|
|
5648
5648
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5649
5649
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -5703,7 +5703,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
5703
5703
|
* See the License for the specific language governing permissions and
|
|
5704
5704
|
* limitations under the License.
|
|
5705
5705
|
* =============================================================================
|
|
5706
|
-
*/const eD={kernelName:jo,inputsToSave:["a","b"],gradFunc:(n,e)=>{const[t,s]=e,r=Ie(t.shape,s.shape);return{a:()=>{const i=bt(t.shape,r);return i.length>0?_(ue(n,i),t.shape):n},b:()=>{const i=D(n,Xe(Ni(
|
|
5706
|
+
*/const eD={kernelName:jo,inputsToSave:["a","b"],gradFunc:(n,e)=>{const[t,s]=e,r=Ie(t.shape,s.shape);return{a:()=>{const i=bt(t.shape,r);return i.length>0?_(ue(n,i),t.shape):n},b:()=>{const i=D(n,Xe(Ni(fe(t,s)))),l=bt(s.shape,r);return l.length>0?_(ue(i,l),s.shape):i}}}};/**
|
|
5707
5707
|
* @license
|
|
5708
5708
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5709
5709
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -5808,7 +5808,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
5808
5808
|
* See the License for the specific language governing permissions and
|
|
5809
5809
|
* limitations under the License.
|
|
5810
5810
|
* =============================================================================
|
|
5811
|
-
*/const oD={kernelName:Ko,inputsToSave:["a","b"],outputsToSave:[!0],gradFunc:(n,e)=>{const[t,s,r]=e,a=t,o=s,i=Ie(a.shape,o.shape);return{a:()=>{const u=re(o,"float32");let h=D(n,D(u,rr(a,ie(u,
|
|
5811
|
+
*/const oD={kernelName:Ko,inputsToSave:["a","b"],outputsToSave:[!0],gradFunc:(n,e)=>{const[t,s,r]=e,a=t,o=s,i=Ie(a.shape,o.shape);return{a:()=>{const u=re(o,"float32");let h=D(n,D(u,rr(a,ie(u,pe(1)))));const d=bt(a.shape,i);return d.length>0&&(h=ue(h,d)),_(h,a.shape)},b:()=>{const u=pn(a,0),h=Vt(u,$n(a),Re(a));let d=D(n,D(r,h));const p=bt(o.shape,i);return p.length>0&&(d=ue(d,p)),_(d,o.shape)}}}};/**
|
|
5812
5812
|
* @license
|
|
5813
5813
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5814
5814
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -5853,7 +5853,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
5853
5853
|
* See the License for the specific language governing permissions and
|
|
5854
5854
|
* limitations under the License.
|
|
5855
5855
|
* =============================================================================
|
|
5856
|
-
*/const hD={kernelName:Ao,inputsToSave:["a","b"],gradFunc:(n,e)=>{const[t,s]=e,r=Ie(t.shape,s.shape);return{a:()=>{const i=
|
|
5856
|
+
*/const hD={kernelName:Ao,inputsToSave:["a","b"],gradFunc:(n,e)=>{const[t,s]=e,r=Ie(t.shape,s.shape);return{a:()=>{const i=fe(n,re(s,"float32")),l=bt(t.shape,r);return l.length>0?_(ue(i,l),t.shape):i},b:()=>{let i=D(n,re(t,"float32"));const l=bt(s.shape,r);l.length>0&&(i=_(ue(i,l),s.shape));const c=Be(s);return Xe(fe(i,re(c,"float32")))}}}};/**
|
|
5857
5857
|
* @license
|
|
5858
5858
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5859
5859
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -5868,7 +5868,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
5868
5868
|
* See the License for the specific language governing permissions and
|
|
5869
5869
|
* limitations under the License.
|
|
5870
5870
|
* =============================================================================
|
|
5871
|
-
*/const dD={kernelName:Xo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>
|
|
5871
|
+
*/const dD={kernelName:Xo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>fe(n,Xe(Be(t)))}}};/**
|
|
5872
5872
|
* @license
|
|
5873
5873
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5874
5874
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -5988,7 +5988,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
5988
5988
|
* See the License for the specific language governing permissions and
|
|
5989
5989
|
* limitations under the License.
|
|
5990
5990
|
* =============================================================================
|
|
5991
|
-
*/const wD={kernelName:Qo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>Xe(
|
|
5991
|
+
*/const wD={kernelName:Qo,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>Xe(fe(n,D(rr(t,1.5),2)))}}};/**
|
|
5992
5992
|
* @license
|
|
5993
5993
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
5994
5994
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -6018,7 +6018,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
6018
6018
|
* See the License for the specific language governing permissions and
|
|
6019
6019
|
* limitations under the License.
|
|
6020
6020
|
* =============================================================================
|
|
6021
|
-
*/const vD={kernelName:ei,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>{const s=pn(t,
|
|
6021
|
+
*/const vD={kernelName:ei,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>{const s=pn(t,pe(0)),r=pe(su),a=pe(ru),o=D(n,a),i=D(D(n,r),dn(re(t,"float32")));return Vt(s,o,i)}}}};/**
|
|
6022
6022
|
* @license
|
|
6023
6023
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
6024
6024
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -6033,7 +6033,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
6033
6033
|
* See the License for the specific language governing permissions and
|
|
6034
6034
|
* limitations under the License.
|
|
6035
6035
|
* =============================================================================
|
|
6036
|
-
*/const SD={kernelName:ri,outputsToSave:[!0],gradFunc:(n,e)=>{const[t]=e;return{x:()=>D(n,D(t,ie(
|
|
6036
|
+
*/const SD={kernelName:ri,outputsToSave:[!0],gradFunc:(n,e)=>{const[t]=e;return{x:()=>D(n,D(t,ie(pe(1),t)))}}};/**
|
|
6037
6037
|
* @license
|
|
6038
6038
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
6039
6039
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -6168,7 +6168,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
6168
6168
|
* See the License for the specific language governing permissions and
|
|
6169
6169
|
* limitations under the License.
|
|
6170
6170
|
* =============================================================================
|
|
6171
|
-
*/const AD={kernelName:oi,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>
|
|
6171
|
+
*/const AD={kernelName:oi,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>fe(n,D(_t(re(t,"float32")),2))}}};/**
|
|
6172
6172
|
* @license
|
|
6173
6173
|
* Copyright 2019 Google LLC. All Rights Reserved.
|
|
6174
6174
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -6198,7 +6198,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
6198
6198
|
* See the License for the specific language governing permissions and
|
|
6199
6199
|
* limitations under the License.
|
|
6200
6200
|
* =============================================================================
|
|
6201
|
-
*/const DD={kernelName:ii,inputsToSave:["a","b"],gradFunc:(n,e)=>{const[t,s]=e,r=
|
|
6201
|
+
*/const DD={kernelName:ii,inputsToSave:["a","b"],gradFunc:(n,e)=>{const[t,s]=e,r=pe(2);return{a:()=>D(n,D(r,ie(t,s))),b:()=>D(n,D(r,ie(s,t)))}}};/**
|
|
6202
6202
|
* @license
|
|
6203
6203
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
6204
6204
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -6258,7 +6258,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
6258
6258
|
* See the License for the specific language governing permissions and
|
|
6259
6259
|
* limitations under the License.
|
|
6260
6260
|
* =============================================================================
|
|
6261
|
-
*/const LD={kernelName:ci,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>
|
|
6261
|
+
*/const LD={kernelName:ci,inputsToSave:["x"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>fe(n,Be(Oc(t)))}}};/**
|
|
6262
6262
|
* @license
|
|
6263
6263
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
6264
6264
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -6273,7 +6273,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
6273
6273
|
* See the License for the specific language governing permissions and
|
|
6274
6274
|
* limitations under the License.
|
|
6275
6275
|
* =============================================================================
|
|
6276
|
-
*/const PD={kernelName:ui,outputsToSave:[!0],gradFunc:(n,e)=>{const[t]=e;return{x:()=>D(ie(
|
|
6276
|
+
*/const PD={kernelName:ui,outputsToSave:[!0],gradFunc:(n,e)=>{const[t]=e;return{x:()=>D(ie(pe(1),Be(t)),n)}}};/**
|
|
6277
6277
|
* @license
|
|
6278
6278
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
6279
6279
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -6333,7 +6333,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
6333
6333
|
* See the License for the specific language governing permissions and
|
|
6334
6334
|
* limitations under the License.
|
|
6335
6335
|
* =============================================================================
|
|
6336
|
-
*/const VD={kernelName:bc,inputsToSave:["segmentIds"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>WD(n,t)}}};function WD(n,e){const t=As(e,Re(e)),s=Ts(n,t);let r=ar(e,
|
|
6336
|
+
*/const VD={kernelName:bc,inputsToSave:["segmentIds"],gradFunc:(n,e)=>{const[t]=e;return{x:()=>WD(n,t)}}};function WD(n,e){const t=As(e,Re(e)),s=Ts(n,t);let r=ar(e,pe(0,"int32"));const a=s.rank-r.rank;for(let i=0;i<a;++i)r=Kt(r,i+1);r=ds(r,fn(s.shape,"bool"));const o=Re(s);return Vt(r,s,o)}/**
|
|
6337
6337
|
* @license
|
|
6338
6338
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
6339
6339
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -6948,7 +6948,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
6948
6948
|
* See the License for the specific language governing permissions and
|
|
6949
6949
|
* limitations under the License.
|
|
6950
6950
|
* =============================================================================
|
|
6951
|
-
*/H().prototype.div=function(n){return this.throwIfDisposed(),
|
|
6951
|
+
*/H().prototype.div=function(n){return this.throwIfDisposed(),fe(this,n)};/**
|
|
6952
6952
|
* @license
|
|
6953
6953
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
6954
6954
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -8450,7 +8450,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
8450
8450
|
* license that can be found in the LICENSE file or at
|
|
8451
8451
|
* https://opensource.org/licenses/MIT.
|
|
8452
8452
|
* =============================================================================
|
|
8453
|
-
*/function ys(n,e){return re(n,e)}function Oi(n,e=-1){const t=n.shape.slice();return e<0&&(e=t.length+e+1),t.splice(e,0,1),_(n,t)}function r_(n,e){return P(()=>{if(n.shape.length!==2)throw new F(`repeat() expects a rank-2 tensor, but received a rank-${n.shape.length} tensor.`);const t=Oi(n,1);return kf(t,[1,e,1])})}function a_(n){const e=[cr(n.shape)];return _(n,e)}function o_(n){if(n.rank<=1)throw new F(`batchFlatten requires a minimum rank of 2. Got rank: ${n.rank}.`);const e=[n.shape[0],cr(n.shape,1)];return _(n,e)}function Jr(n,e,t){return P(()=>{switch(n.rank){case 1:return jc(n,e,t);case 2:return Cp(n,[e,0],[t,n.shape[1]]);case 3:return qc(n,[e,0,0],[t,n.shape[1],n.shape[2]]);case 4:return Ti(n,[e,0,0,0],[t,n.shape[1],n.shape[2],n.shape[3]]);case 5:return Ae(n,[e,0,0,0,0],[t,n.shape[1],n.shape[2],n.shape[3],n.shape[4]]);case 6:return Ae(n,[e,0,0,0,0,0],[t,n.shape[1],n.shape[2],n.shape[3],n.shape[4],n.shape[5]]);default:throw new F(`sliceAlongFirstAxis() received an unsupported tensor rank: ${n.rank}`)}})}function Nf(n,e,t){return P(()=>{switch(n.rank){case 1:return jc(n,e,t);case 2:return Cp(n,[0,e],[n.shape[0],t]);case 3:return qc(n,[0,0,e],[n.shape[0],n.shape[1],t]);case 4:return Ti(n,[0,0,0,e],[n.shape[0],n.shape[1],n.shape[2],t]);default:throw new F(`sliceAlongLastAxis() received an unsupported tensor rank: ${n.rank}`)}})}function lu(n,e,t,s){return P(()=>{switch(n.rank){case 1:return jc(n,e,t);case 2:switch(s){case 1:return Jr(n,e,t);case 2:return Nf(n,e,t);default:throw new F(`The axis is not within the rank of the tensor ${s}`)}case 3:switch(s){case 1:return Jr(n,e,t);case 2:return qc(n,[0,e,0],[n.shape[0],t,n.shape[2]]);case 3:return Nf(n,e,t);default:throw new F(`The axis is not within the rank of the tensor ${s}`)}case 4:switch(s){case 1:return Jr(n,e,t);case 2:return Ti(n,[0,e,0,0],[n.shape[0],t,n.shape[2],n.shape[3]]);case 3:return Ti(n,[0,0,e,0],[n.shape[0],n.shape[1],t,n.shape[3]]);case 4:return Nf(n,e,t);default:throw new F(`The axis is not within the rank of the tensor ${s}`)}default:throw new F(`sliceAlongLastAxis() received an unsupported tensor rank: ${n.rank}`)}})}function Cf(n,e=-1){let t;return e<0&&(t=n[0].rank,t!==0?e=t:e=0),e===n[0].rank&&(e=-1),pt(n,e)}function Bx(n,e){switch(n.rank){case 1:return ky([n,e]);case 2:return Ty([n,e],0);case 3:return Ey([n,e],0);case 4:return Ay([n,e],0);default:throw new F(`concatAlongFirstAxis() received an unsupported tensor rank: ${n.rank}`)}}function kf(n,e){if(Array.isArray(e)||(e=[e]),n.rank!==e.length)throw new F(`The length of input n (${e.length}) does not match the number of dimensions in input x (${n.rank})`);return On(n,e)}function cu(n,e=0,t=1,s,r){return bp(n,e,t,s,r)}function bs(n,e,t,s){if(n.rank<2||e.rank<2)throw new ve(`dot requires both inputs to be rank >= 2 but got x shape = ${n.shape} and y shape = ${e.shape}`);if(e.rank>=3){const r=n.shape.slice(-1)[0],a=e.shape.slice(-2)[0];if(r!==a)throw new ve(`If rank y >= 3, then the second last dim of y must equal the last dim of x but got x shape = ${n.shape} and y shape = ${e.shape}`)}if(n.rank===2&&e.rank===2)return Op({a:n,b:e,transposeA:!1,transposeB:!1,bias:s?Tf(n.rank,s,ts()):null,activation:t});{const r=n.shape.slice(),a=r.pop();n=_(n,[-1,a]);const o=e.shape.slice(),i=o.pop(),l=o.pop(),c=[...o,i],u=Array.from({length:e.rank},(f,m)=>m===0?e.rank-2:m<=e.rank-2?m-1:m);e=_(De(e,u),[l,-1]);const h=[...r,...c];return _(Op({a:n,b:e,transposeA:!1,transposeB:!1,bias:s?Tf(n.rank,s,ts()):null,activation:t}),h)}}function Vx(n,e,t){return P(()=>(Array.isArray(e)?e=ft(e,"int32"):e=re(e,"int32"),Ts(n,e,t)))}function Li(n){return D(n,n)}function Tf(n,e,t){const s=e.shape;if(e.rank!==1&&e.rank!==n)throw new F(`Unexpected bias dimensions: ${e.rank}; expected it to be 1 or ${n}`);if(n===5){if(t==="channelsFirst")return s.length===1?_(e,[1,s[0],1,1,1]):_(e,[1,s[3],s[0],s[1],s[2]]);if(t==="channelsLast")return s.length===1?_(e,[1,1,1,1,s[0]]):_(e,[1].concat(s))}else if(n===4){if(t==="channelsFirst")return s.length===1?_(e,[1,s[0],1,1]):_(e,[1,s[2],s[0],s[1]]);if(t==="channelsLast")return s.length===1?_(e,[1,1,1,s[0]]):_(e,[1].concat(s))}else if(n===3){if(t==="channelsFirst")return s.length===1?_(e,[1,s[0],1]):_(e,[1,s[1],s[0]]);if(t==="channelsLast")return s.length===1?_(e,[1,1,s[0]]):_(e,[1].concat(s))}else if(n<3)return e;throw new F(`Unsupported input rank by biasAdd: ${e.rank}`)}function ns(n,e,t){return P(()=>(t==null&&(t=ts()),ut(t),X(n,Tf(n.rank,e,t))))}function i_(n,e=1){if(e!==1)throw new ve(`Support for alpha values other than 1 (${e}) is not implemented yet.`);return vi(n)}function l_(n){return P(()=>
|
|
8453
|
+
*/function ys(n,e){return re(n,e)}function Oi(n,e=-1){const t=n.shape.slice();return e<0&&(e=t.length+e+1),t.splice(e,0,1),_(n,t)}function r_(n,e){return P(()=>{if(n.shape.length!==2)throw new F(`repeat() expects a rank-2 tensor, but received a rank-${n.shape.length} tensor.`);const t=Oi(n,1);return kf(t,[1,e,1])})}function a_(n){const e=[cr(n.shape)];return _(n,e)}function o_(n){if(n.rank<=1)throw new F(`batchFlatten requires a minimum rank of 2. Got rank: ${n.rank}.`);const e=[n.shape[0],cr(n.shape,1)];return _(n,e)}function Jr(n,e,t){return P(()=>{switch(n.rank){case 1:return jc(n,e,t);case 2:return Cp(n,[e,0],[t,n.shape[1]]);case 3:return qc(n,[e,0,0],[t,n.shape[1],n.shape[2]]);case 4:return Ti(n,[e,0,0,0],[t,n.shape[1],n.shape[2],n.shape[3]]);case 5:return Ae(n,[e,0,0,0,0],[t,n.shape[1],n.shape[2],n.shape[3],n.shape[4]]);case 6:return Ae(n,[e,0,0,0,0,0],[t,n.shape[1],n.shape[2],n.shape[3],n.shape[4],n.shape[5]]);default:throw new F(`sliceAlongFirstAxis() received an unsupported tensor rank: ${n.rank}`)}})}function Nf(n,e,t){return P(()=>{switch(n.rank){case 1:return jc(n,e,t);case 2:return Cp(n,[0,e],[n.shape[0],t]);case 3:return qc(n,[0,0,e],[n.shape[0],n.shape[1],t]);case 4:return Ti(n,[0,0,0,e],[n.shape[0],n.shape[1],n.shape[2],t]);default:throw new F(`sliceAlongLastAxis() received an unsupported tensor rank: ${n.rank}`)}})}function lu(n,e,t,s){return P(()=>{switch(n.rank){case 1:return jc(n,e,t);case 2:switch(s){case 1:return Jr(n,e,t);case 2:return Nf(n,e,t);default:throw new F(`The axis is not within the rank of the tensor ${s}`)}case 3:switch(s){case 1:return Jr(n,e,t);case 2:return qc(n,[0,e,0],[n.shape[0],t,n.shape[2]]);case 3:return Nf(n,e,t);default:throw new F(`The axis is not within the rank of the tensor ${s}`)}case 4:switch(s){case 1:return Jr(n,e,t);case 2:return Ti(n,[0,e,0,0],[n.shape[0],t,n.shape[2],n.shape[3]]);case 3:return Ti(n,[0,0,e,0],[n.shape[0],n.shape[1],t,n.shape[3]]);case 4:return Nf(n,e,t);default:throw new F(`The axis is not within the rank of the tensor ${s}`)}default:throw new F(`sliceAlongLastAxis() received an unsupported tensor rank: ${n.rank}`)}})}function Cf(n,e=-1){let t;return e<0&&(t=n[0].rank,t!==0?e=t:e=0),e===n[0].rank&&(e=-1),pt(n,e)}function Bx(n,e){switch(n.rank){case 1:return ky([n,e]);case 2:return Ty([n,e],0);case 3:return Ey([n,e],0);case 4:return Ay([n,e],0);default:throw new F(`concatAlongFirstAxis() received an unsupported tensor rank: ${n.rank}`)}}function kf(n,e){if(Array.isArray(e)||(e=[e]),n.rank!==e.length)throw new F(`The length of input n (${e.length}) does not match the number of dimensions in input x (${n.rank})`);return On(n,e)}function cu(n,e=0,t=1,s,r){return bp(n,e,t,s,r)}function bs(n,e,t,s){if(n.rank<2||e.rank<2)throw new ve(`dot requires both inputs to be rank >= 2 but got x shape = ${n.shape} and y shape = ${e.shape}`);if(e.rank>=3){const r=n.shape.slice(-1)[0],a=e.shape.slice(-2)[0];if(r!==a)throw new ve(`If rank y >= 3, then the second last dim of y must equal the last dim of x but got x shape = ${n.shape} and y shape = ${e.shape}`)}if(n.rank===2&&e.rank===2)return Op({a:n,b:e,transposeA:!1,transposeB:!1,bias:s?Tf(n.rank,s,ts()):null,activation:t});{const r=n.shape.slice(),a=r.pop();n=_(n,[-1,a]);const o=e.shape.slice(),i=o.pop(),l=o.pop(),c=[...o,i],u=Array.from({length:e.rank},(f,m)=>m===0?e.rank-2:m<=e.rank-2?m-1:m);e=_(De(e,u),[l,-1]);const h=[...r,...c];return _(Op({a:n,b:e,transposeA:!1,transposeB:!1,bias:s?Tf(n.rank,s,ts()):null,activation:t}),h)}}function Vx(n,e,t){return P(()=>(Array.isArray(e)?e=ft(e,"int32"):e=re(e,"int32"),Ts(n,e,t)))}function Li(n){return D(n,n)}function Tf(n,e,t){const s=e.shape;if(e.rank!==1&&e.rank!==n)throw new F(`Unexpected bias dimensions: ${e.rank}; expected it to be 1 or ${n}`);if(n===5){if(t==="channelsFirst")return s.length===1?_(e,[1,s[0],1,1,1]):_(e,[1,s[3],s[0],s[1],s[2]]);if(t==="channelsLast")return s.length===1?_(e,[1,1,1,1,s[0]]):_(e,[1].concat(s))}else if(n===4){if(t==="channelsFirst")return s.length===1?_(e,[1,s[0],1,1]):_(e,[1,s[2],s[0],s[1]]);if(t==="channelsLast")return s.length===1?_(e,[1,1,1,s[0]]):_(e,[1].concat(s))}else if(n===3){if(t==="channelsFirst")return s.length===1?_(e,[1,s[0],1]):_(e,[1,s[1],s[0]]);if(t==="channelsLast")return s.length===1?_(e,[1,1,s[0]]):_(e,[1].concat(s))}else if(n<3)return e;throw new F(`Unsupported input rank by biasAdd: ${e.rank}`)}function ns(n,e,t){return P(()=>(t==null&&(t=ts()),ut(t),X(n,Tf(n.rank,e,t))))}function i_(n,e=1){if(e!==1)throw new ve(`Support for alpha values other than 1 (${e}) is not implemented yet.`);return vi(n)}function l_(n){return P(()=>fe(n,X(yt(n),1)))}function Wx(n,e,t,s){return P(()=>hb(n,e,t,s))}function c_(n){return P(()=>{const e=X(.5,D(.2,n));return hn(e,0,1)})}function Pi(n,e,t=!1){return t?n():e()}/**
|
|
8454
8454
|
* @license
|
|
8455
8455
|
* Copyright 2018 Google LLC
|
|
8456
8456
|
*
|
|
@@ -8466,7 +8466,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
8466
8466
|
* license that can be found in the LICENSE file or at
|
|
8467
8467
|
* https://opensource.org/licenses/MIT.
|
|
8468
8468
|
* =============================================================================
|
|
8469
|
-
*/function d_(n){Yr(u_,"FanMode",n)}function p_(n){Yr(h_,"Distribution",n)}class Vn extends Ha{fromConfigUsesCustomObjects(){return!1}getConfig(){return{}}}class Ux extends Vn{apply(e,t){return ct(e,t)}}Ux.className="Zeros",Y(Ux);class Ef extends Vn{apply(e,t){return fn(e,t)}}Ef.className="Ones",Y(Ef);class Gx extends Vn{constructor(e){if(super(),typeof e!="object")throw new F(`Expected argument of type ConstantConfig but got ${e}`);if(e.value===void 0)throw new F(`config must have value set but got ${e}`);this.value=e.value}apply(e,t){return P(()=>D(
|
|
8469
|
+
*/function d_(n){Yr(u_,"FanMode",n)}function p_(n){Yr(h_,"Distribution",n)}class Vn extends Ha{fromConfigUsesCustomObjects(){return!1}getConfig(){return{}}}class Ux extends Vn{apply(e,t){return ct(e,t)}}Ux.className="Zeros",Y(Ux);class Ef extends Vn{apply(e,t){return fn(e,t)}}Ef.className="Ones",Y(Ef);class Gx extends Vn{constructor(e){if(super(),typeof e!="object")throw new F(`Expected argument of type ConstantConfig but got ${e}`);if(e.value===void 0)throw new F(`config must have value set but got ${e}`);this.value=e.value}apply(e,t){return P(()=>D(pe(this.value),fn(e,t)))}getConfig(){return{value:this.value}}}Gx.className="Constant",Y(Gx);class Hx extends Vn{constructor(e){super(),this.DEFAULT_MINVAL=-.05,this.DEFAULT_MAXVAL=.05,this.minval=e.minval||this.DEFAULT_MINVAL,this.maxval=e.maxval||this.DEFAULT_MAXVAL,this.seed=e.seed}apply(e,t){return Ur(e,this.minval,this.maxval,t,this.seed)}getConfig(){return{minval:this.minval,maxval:this.maxval,seed:this.seed}}}Hx.className="RandomUniform",Y(Hx);class jx extends Vn{constructor(e){super(),this.DEFAULT_MEAN=0,this.DEFAULT_STDDEV=.05,this.mean=e.mean||this.DEFAULT_MEAN,this.stddev=e.stddev||this.DEFAULT_STDDEV,this.seed=e.seed}apply(e,t){if(t=t||"float32",t!=="float32"&&t!=="int32")throw new ve(`randomNormal does not support dType ${t}.`);return cu(e,this.mean,this.stddev,t,this.seed)}getConfig(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}}}jx.className="RandomNormal",Y(jx);class qx extends Vn{constructor(e){super(),this.DEFAULT_MEAN=0,this.DEFAULT_STDDEV=.05,this.mean=e.mean||this.DEFAULT_MEAN,this.stddev=e.stddev||this.DEFAULT_STDDEV,this.seed=e.seed}apply(e,t){if(t=t||"float32",t!=="float32"&&t!=="int32")throw new ve(`truncatedNormal does not support dType ${t}.`);return Ap(e,this.mean,this.stddev,t,this.seed)}getConfig(){return{mean:this.mean,stddev:this.stddev,seed:this.seed}}}qx.className="TruncatedNormal",Y(qx);class Kx extends Vn{constructor(e){super(),this.gain=e.gain!=null?e.gain:1}apply(e,t){return P(()=>{if(e.length!==2||e[0]!==e[1])throw new F("Identity matrix initializer can only be used for 2D square matrices.");return D(this.gain,ip(e[0]))})}getConfig(){return{gain:this.gain}}}Kx.className="Identity",Y(Kx);function f_(n,e="channelsLast"){let t,s;if(ut(e),n.length===2)t=n[0],s=n[1];else if([3,4,5].indexOf(n.length)!==-1){if(e==="channelsFirst"){const r=cr(n,2);t=n[1]*r,s=n[0]*r}else if(e==="channelsLast"){const r=cr(n,0,n.length-2);t=n[n.length-2]*r,s=n[n.length-1]*r}}else{const r=cr(n);t=Math.sqrt(r),s=Math.sqrt(r)}return[t,s]}class nn extends Vn{constructor(e){if(super(),e.scale<0)throw new F(`scale must be a positive float. Got: ${e.scale}`);this.scale=e.scale==null?1:e.scale,this.mode=e.mode==null?"fanIn":e.mode,d_(this.mode),this.distribution=e.distribution==null?"normal":e.distribution,p_(this.distribution),this.seed=e.seed}apply(e,t){const s=f_(e),r=s[0],a=s[1];let o=this.scale;if(this.mode==="fanIn"?o/=Math.max(1,r):this.mode==="fanOut"?o/=Math.max(1,a):o/=Math.max(1,(r+a)/2),this.distribution==="normal"){const i=Math.sqrt(o);if(t=t||"float32",t!=="float32"&&t!=="int32")throw new ve(`${this.getClassName()} does not support dType ${t}.`);return Ap(e,0,i,t,this.seed)}else{const i=Math.sqrt(3*o);return Ur(e,-i,i,t,this.seed)}}getConfig(){return{scale:this.scale,mode:this.mode,distribution:this.distribution,seed:this.seed}}}nn.className="VarianceScaling",Y(nn);class Af extends nn{constructor(e){super({scale:1,mode:"fanAvg",distribution:"uniform",seed:e==null?null:e.seed})}getClassName(){return nn.className}}Af.className="GlorotUniform",Y(Af);class Rf extends nn{constructor(e){super({scale:1,mode:"fanAvg",distribution:"normal",seed:e==null?null:e.seed})}getClassName(){return nn.className}}Rf.className="GlorotNormal",Y(Rf);class Df extends nn{constructor(e){super({scale:2,mode:"fanIn",distribution:"normal",seed:e==null?null:e.seed})}getClassName(){return nn.className}}Df.className="HeNormal",Y(Df);class _f extends nn{constructor(e){super({scale:2,mode:"fanIn",distribution:"uniform",seed:e==null?null:e.seed})}getClassName(){return nn.className}}_f.className="HeUniform",Y(_f);class Ff extends nn{constructor(e){super({scale:1,mode:"fanIn",distribution:"normal",seed:e==null?null:e.seed})}getClassName(){return nn.className}}Ff.className="LeCunNormal",Y(Ff);class Of extends nn{constructor(e){super({scale:1,mode:"fanIn",distribution:"uniform",seed:e==null?null:e.seed})}getClassName(){return nn.className}}Of.className="LeCunUniform",Y(Of);class Xx extends Vn{constructor(e){super(),this.DEFAULT_GAIN=1,this.ELEMENTS_WARN_SLOW=2e3,this.gain=e.gain==null?this.DEFAULT_GAIN:e.gain,this.seed=e.seed}apply(e,t){return P(()=>{if(e.length<2)throw new ve("Shape must be at least 2D.");if(t!=="int32"&&t!=="float32"&&t!==void 0)throw new TypeError(`Unsupported data type ${t}.`);t=t;const s=U(e.slice(0,-1)),r=e[e.length-1],a=s*r;a>this.ELEMENTS_WARN_SLOW&&console.warn(`Orthogonal initializer is being called on a matrix with more than ${this.ELEMENTS_WARN_SLOW} (${a}) elements: Slowness may result.`);const o=[Math.max(r,s),Math.min(r,s)],i=cu(o,0,1,t,this.seed),l=vb.qr(i,!1);let c=l[0];const h=l[1].flatten().stridedSlice([0],[Math.min(r,s)*Math.min(r,s)],[Math.min(r,s)+1]);return c=D(c,h.sign()),s<r&&(c=c.transpose()),D(pe(this.gain),c.reshape(e))})}getConfig(){return{gain:this.gain,seed:this.seed}}}Xx.className="Orthogonal",Y(Xx);const Yx={constant:"Constant",glorotNormal:"GlorotNormal",glorotUniform:"GlorotUniform",heNormal:"HeNormal",heUniform:"HeUniform",identity:"Identity",leCunNormal:"LeCunNormal",leCunUniform:"LeCunUniform",ones:"Ones",orthogonal:"Orthogonal",randomNormal:"RandomNormal",randomUniform:"RandomUniform",truncatedNormal:"TruncatedNormal",varianceScaling:"VarianceScaling",zeros:"Zeros"};function Zx(n,e={}){return _i(n,zn.getMap().classNameMap,e,"initializer")}function nt(n){return If(n)}function Ye(n){if(typeof n=="string"){const e=n in Yx?Yx[n]:n;if(e==="GlorotNormal")return new Rf;if(e==="GlorotUniform")return new Af;if(e==="HeNormal")return new Df;if(e==="HeUniform")return new _f;if(e==="LeCunNormal")return new Ff;if(e==="LeCunUniform")return new Of;{const t={};return t.className=e,t.config={},Zx(t)}}else return n instanceof Vn?n:Zx(n)}/**
|
|
8470
8470
|
* @license
|
|
8471
8471
|
* Copyright 2018 Google LLC
|
|
8472
8472
|
*
|
|
@@ -8537,7 +8537,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
8537
8537
|
* license that can be found in the LICENSE file or at
|
|
8538
8538
|
* https://opensource.org/licenses/MIT.
|
|
8539
8539
|
* =============================================================================
|
|
8540
|
-
*/function Mf(n,e){return P(()=>_t(ue(D(n,n),e,!0)))}class Bi extends Ha{getConfig(){return{}}}class t0 extends Bi{constructor(e){super(),this.defaultMaxValue=2,this.defaultAxis=0,this.maxValue=e.maxValue!=null?e.maxValue:this.defaultMaxValue,this.axis=e.axis!=null?e.axis:this.defaultAxis}apply(e){return P(()=>{const t=Mf(e,this.axis),s=hn(t,0,this.maxValue);return D(e,
|
|
8540
|
+
*/function Mf(n,e){return P(()=>_t(ue(D(n,n),e,!0)))}class Bi extends Ha{getConfig(){return{}}}class t0 extends Bi{constructor(e){super(),this.defaultMaxValue=2,this.defaultAxis=0,this.maxValue=e.maxValue!=null?e.maxValue:this.defaultMaxValue,this.axis=e.axis!=null?e.axis:this.defaultAxis}apply(e){return P(()=>{const t=Mf(e,this.axis),s=hn(t,0,this.maxValue);return D(e,fe(s,X(xt(),t)))})}getConfig(){return{maxValue:this.maxValue,axis:this.axis}}}t0.className="MaxNorm",Y(t0);class n0 extends Bi{constructor(e){super(),this.defaultAxis=0,this.axis=e.axis!=null?e.axis:this.defaultAxis}apply(e){return P(()=>fe(e,X(xt(),Mf(e,this.axis))))}getConfig(){return{axis:this.axis}}}n0.className="UnitNorm",Y(n0);class s0 extends Bi{apply(e){return ps(e)}}s0.className="NonNeg",Y(s0);class r0 extends Bi{constructor(e){super(),this.defaultMinValue=0,this.defaultMaxValue=1,this.defaultRate=1,this.defaultAxis=0,this.minValue=e.minValue!=null?e.minValue:this.defaultMinValue,this.maxValue=e.maxValue!=null?e.maxValue:this.defaultMaxValue,this.rate=e.rate!=null?e.rate:this.defaultRate,this.axis=e.axis!=null?e.axis:this.defaultAxis}apply(e){return P(()=>{const t=Mf(e,this.axis),s=X(D(this.rate,hn(t,this.minValue,this.maxValue)),D(1-this.rate,t));return D(e,fe(s,X(xt(),t)))})}getConfig(){return{minValue:this.minValue,maxValue:this.maxValue,rate:this.rate,axis:this.axis}}}r0.className="MinMaxNorm",Y(r0);const a0={maxNorm:"MaxNorm",minMaxNorm:"MinMaxNorm",nonNeg:"NonNeg",unitNorm:"UnitNorm"};function It(n){return If(n)}function o0(n,e={}){return _i(n,zn.getMap().classNameMap,e,"constraint")}function vt(n){if(n==null)return null;if(typeof n=="string"){const t={className:n in a0?a0[n]:n,config:{}};return o0(t)}else return n instanceof Bi?n:o0(n)}/**
|
|
8541
8541
|
* @license
|
|
8542
8542
|
* Copyright 2018 Google LLC
|
|
8543
8543
|
*
|
|
@@ -8561,7 +8561,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
8561
8561
|
* license that can be found in the LICENSE file or at
|
|
8562
8562
|
* https://opensource.org/licenses/MIT.
|
|
8563
8563
|
* =============================================================================
|
|
8564
|
-
*/var l0;(function(n){n[n.SILENT=0]="SILENT",n[n.VERBOSE=1]="VERBOSE"})(l0||(l0={}));const A_=125;class Vi{constructor(){this.validationData=null}setParams(e){this.params=e}onEpochBegin(e,t){return O(this,null,function*(){})}onEpochEnd(e,t){return O(this,null,function*(){})}onBatchBegin(e,t){return O(this,null,function*(){})}onBatchEnd(e,t){return O(this,null,function*(){})}onTrainBegin(e){return O(this,null,function*(){})}onTrainEnd(e){return O(this,null,function*(){})}setModel(e){}}class R_{constructor(e,t=10){e==null&&(e=[]),this.callbacks=e,this.queueLength=t}append(e){this.callbacks.push(e)}setParams(e){for(const t of this.callbacks)t.setParams(e)}setModel(e){for(const t of this.callbacks)t.setModel(e)}onEpochBegin(e,t){return O(this,null,function*(){t==null&&(t={});for(const s of this.callbacks)yield s.onEpochBegin(e,t)})}onEpochEnd(e,t){return O(this,null,function*(){t==null&&(t={});for(const s of this.callbacks)yield s.onEpochEnd(e,t)})}onBatchBegin(e,t){return O(this,null,function*(){t==null&&(t={});for(const s of this.callbacks)yield s.onBatchBegin(e,t)})}onBatchEnd(e,t){return O(this,null,function*(){t==null&&(t={});for(const s of this.callbacks)yield s.onBatchEnd(e,t)})}onTrainBegin(e){return O(this,null,function*(){e==null&&(e={});for(const t of this.callbacks)yield t.onTrainBegin(e)})}onTrainEnd(e){return O(this,null,function*(){e==null&&(e={});for(const t of this.callbacks)yield t.onTrainEnd(e)})}}class D_ extends Vi{constructor(){super()}onEpochBegin(e){return O(this,null,function*(){this.seen=0,this.totals={}})}onBatchEnd(e,t){return O(this,null,function*(){t==null&&(t={});const s=t.size==null?0:t.size;this.seen+=s;for(const r in t){const a=t[r];if(typeof a=="number")this.totals.hasOwnProperty(r)||(this.totals[r]=0),this.totals[r]=this.totals[r]+a*s;else{let o;r in this.totals?o=this.totals[r]:this.totals[r]=0;const i=P(()=>X(this.totals[r],D(a,s)));this.totals[r]=i,o!=null&&o.dispose()}}})}onEpochEnd(e,t){return O(this,null,function*(){if(t!=null)for(const s of this.params.metrics)this.totals[s]!=null&&(typeof this.totals[s]=="number"?t[s]=this.totals[s]/this.seen:P(()=>{const r=D(
|
|
8564
|
+
*/var l0;(function(n){n[n.SILENT=0]="SILENT",n[n.VERBOSE=1]="VERBOSE"})(l0||(l0={}));const A_=125;class Vi{constructor(){this.validationData=null}setParams(e){this.params=e}onEpochBegin(e,t){return O(this,null,function*(){})}onEpochEnd(e,t){return O(this,null,function*(){})}onBatchBegin(e,t){return O(this,null,function*(){})}onBatchEnd(e,t){return O(this,null,function*(){})}onTrainBegin(e){return O(this,null,function*(){})}onTrainEnd(e){return O(this,null,function*(){})}setModel(e){}}class R_{constructor(e,t=10){e==null&&(e=[]),this.callbacks=e,this.queueLength=t}append(e){this.callbacks.push(e)}setParams(e){for(const t of this.callbacks)t.setParams(e)}setModel(e){for(const t of this.callbacks)t.setModel(e)}onEpochBegin(e,t){return O(this,null,function*(){t==null&&(t={});for(const s of this.callbacks)yield s.onEpochBegin(e,t)})}onEpochEnd(e,t){return O(this,null,function*(){t==null&&(t={});for(const s of this.callbacks)yield s.onEpochEnd(e,t)})}onBatchBegin(e,t){return O(this,null,function*(){t==null&&(t={});for(const s of this.callbacks)yield s.onBatchBegin(e,t)})}onBatchEnd(e,t){return O(this,null,function*(){t==null&&(t={});for(const s of this.callbacks)yield s.onBatchEnd(e,t)})}onTrainBegin(e){return O(this,null,function*(){e==null&&(e={});for(const t of this.callbacks)yield t.onTrainBegin(e)})}onTrainEnd(e){return O(this,null,function*(){e==null&&(e={});for(const t of this.callbacks)yield t.onTrainEnd(e)})}}class D_ extends Vi{constructor(){super()}onEpochBegin(e){return O(this,null,function*(){this.seen=0,this.totals={}})}onBatchEnd(e,t){return O(this,null,function*(){t==null&&(t={});const s=t.size==null?0:t.size;this.seen+=s;for(const r in t){const a=t[r];if(typeof a=="number")this.totals.hasOwnProperty(r)||(this.totals[r]=0),this.totals[r]=this.totals[r]+a*s;else{let o;r in this.totals?o=this.totals[r]:this.totals[r]=0;const i=P(()=>X(this.totals[r],D(a,s)));this.totals[r]=i,o!=null&&o.dispose()}}})}onEpochEnd(e,t){return O(this,null,function*(){if(t!=null)for(const s of this.params.metrics)this.totals[s]!=null&&(typeof this.totals[s]=="number"?t[s]=this.totals[s]/this.seen:P(()=>{const r=D(fe(1,this.seen),this.totals[s]);t[s]=r,this.totals[s].dispose(),Nt(t[s])}))})}}class __ extends Vi{onTrainBegin(e){return O(this,null,function*(){this.epoch=[],this.history={}})}onEpochEnd(e,t){return O(this,null,function*(){t==null&&(t={}),this.epoch.push(e);for(const s in t)this.history[s]==null&&(this.history[s]=[]),this.history[s].push(t[s])})}syncData(){return O(this,null,function*(){const e=[],t=[],s=[];for(const a in this.history){const o=this.history[a];for(let i=0;i<o.length;++i)if(typeof o[i]!="number"){const l=o[i];e.push(l.data()),t.push(a),s.push(i)}}const r=yield Promise.all(e);for(let a=0;a<r.length;++a)this.history[t[a]][s[a]].dispose(),this.history[t[a]][s[a]]=r[a][0]})}}class F_ extends Vi{constructor(e,t){if(super(),this.currentEpoch=0,this.nowFunc=e.nowFunc,this.nextFrameFunc=e.nextFrameFunc||Xb,this.yieldEvery=t||"auto",this.yieldEvery==="auto"&&(this.yieldEvery=A_),this.yieldEvery==="never"&&e.onYield!=null)throw new Error("yieldEvery is `never` but you provided an `onYield` callback. Either change `yieldEvery` or remove the callback");ph(this.yieldEvery)&&(this.maybeWait=qD(this.maybeWait.bind(this),this.yieldEvery,this.nowFunc)),this.trainBegin=e.onTrainBegin,this.trainEnd=e.onTrainEnd,this.epochBegin=e.onEpochBegin,this.epochEnd=e.onEpochEnd,this.batchBegin=e.onBatchBegin,this.batchEnd=e.onBatchEnd,this.yield=e.onYield}maybeWait(e,t,s){return O(this,null,function*(){const r=[];this.yield!=null&&(yield Qr(s),r.push(this.yield(e,t,s))),r.push(this.nextFrameFunc()),yield Promise.all(r)})}onEpochBegin(e,t){return O(this,null,function*(){this.currentEpoch=e,this.epochBegin!=null&&(yield Qr(t),yield this.epochBegin(e,t))})}onEpochEnd(e,t){return O(this,null,function*(){const s=[];this.epochEnd!=null&&(yield Qr(t),s.push(this.epochEnd(e,t))),this.yieldEvery==="epoch"&&s.push(this.nextFrameFunc()),yield Promise.all(s)})}onBatchBegin(e,t){return O(this,null,function*(){this.batchBegin!=null&&(yield Qr(t),yield this.batchBegin(e,t))})}onBatchEnd(e,t){return O(this,null,function*(){const s=[];this.batchEnd!=null&&(yield Qr(t),s.push(this.batchEnd(e,t))),this.yieldEvery==="batch"?s.push(this.nextFrameFunc()):ph(this.yieldEvery)&&s.push(this.maybeWait(this.currentEpoch,e,t)),yield Promise.all(s)})}onTrainBegin(e){return O(this,null,function*(){this.trainBegin!=null&&(yield Qr(e),yield this.trainBegin(e))})}onTrainEnd(e){return O(this,null,function*(){this.trainEnd!=null&&(yield Qr(e),yield this.trainEnd(e))})}}function c0(n,e){return n==null&&(n={}),n instanceof Vi?[n]:Array.isArray(n)&&n[0]instanceof Vi?n:Ve(n).map(s=>new F_(s,e))}class Wn{constructor(){}static registerCallbackConstructor(e,t){S(e>=0&&Number.isInteger(e),()=>`Verbosity level is expected to be an integer >= 0, but got ${e}`),Wn.checkForDuplicate(t),Wn.constructors[e]==null&&(Wn.constructors[e]=[]),Wn.constructors[e].push(t)}static checkForDuplicate(e){for(const t in Wn.constructors)Wn.constructors[+t].forEach(r=>{if(r===e)throw new F("Duplicate callback constructor.")})}static clear(){Wn.constructors={}}static createCallbacks(e){const t=[];for(const s in Wn.constructors){const r=+s;e>=r&&t.push(...Wn.constructors[r])}return t.map(s=>new s)}}Wn.constructors={};function u0(n,e,t,s,r,a,o,i,l){const c=new __,u=[new D_,...Wn.createCallbacks(e)];n!=null&&u.push(...n),u.push(c);const h=new R_(u);return h.setParams({epochs:t,initialEpoch:s,samples:r,steps:a,batchSize:o,verbose:e,doValidation:i,metrics:l}),{callbackList:h,history:c}}/**
|
|
8565
8565
|
* @license
|
|
8566
8566
|
* Copyright 2018 Google LLC
|
|
8567
8567
|
*
|
|
@@ -8577,7 +8577,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
8577
8577
|
* license that can be found in the LICENSE file or at
|
|
8578
8578
|
* https://opensource.org/licenses/MIT.
|
|
8579
8579
|
* =============================================================================
|
|
8580
|
-
*/function mu(n,e){return P(()=>{n.dtype!=="float32"&&(n=re(n,"float32"));const t=ue(Li(n),e,!0),s=_a(t.shape,xt()),r=_t(As(t,s));return
|
|
8580
|
+
*/function mu(n,e){return P(()=>{n.dtype!=="float32"&&(n=re(n,"float32"));const t=ue(Li(n),e,!0),s=_a(t.shape,xt()),r=_t(As(t,s));return fe(n,r)})}function gu(n,e){return P(()=>tt(Li(ie(e,n)),-1))}function Bf(n,e){return P(()=>tt(yt(ie(e,n)),-1))}function Vf(n,e){return P(()=>{const t=ie(n,e),s=hn(yt(n),xt(),Number.MAX_VALUE),r=yt(fe(t,s));return D(100,tt(r,-1))})}function O_(n,e){return P(()=>{const t=hn(e,xt(),Number.MAX_VALUE),s=$n(X(1,t)),r=hn(n,xt(),Number.MAX_VALUE),a=$n(X(1,r));return tt(Li(ie(s,a)),-1)})}function L_(n,e){return P(()=>{const t=As(0,ie(1,D(n,e)));return tt(Li(t),-1)})}function P_(n,e){return P(()=>{const t=As(0,ie(1,D(n,e)));return tt(t,-1)})}function z_(n,e){return P(()=>{const t=ue(D(n,e),-1),s=Sn(D(ie(1,n),e),-1);return As(0,X(1,ie(s,t)))})}function M_(n,e){return P(()=>{const t=Math.log(2),s=ie(e,n),r=ie(X(s,La(D(-2,s))),t);return tt(r,-1)})}function Wi(n,e,t=!1){return P(()=>{if(t)e=Kc(e);else{const s=ue(e,e.shape.length-1,!0);e=fe(e,s)}return e=hn(e,xt(),1-xt()),Xe(ue(D(re(n,"float32"),$n(e)),e.shape.length-1))})}function yu(n,e,t=!1){return P(()=>{const s=re(Ni(a_(n)),"int32");e=hn(e,xt(),1-xt());const r=e.shape,a=_(Vr(s,r[r.length-1]),r);return Wi(a,e,t)})}function B_(n,e){if(!Ee(n.shape,e.shape))throw new F(`logits and labels must have the same shape, but got shapes ${JSON.stringify(n.shape)} and ${JSON.stringify(e.shape)}`);return P(()=>{const t=ps(e),s=Xe(yt(e));return X(ie(t,D(e,n)),Mc(dn(s)))})}function bu(n,e){return P(()=>{let t;return t=hn(e,xt(),1-xt()),t=$n(fe(t,ie(1,t))),tt(B_(n,t),-1)})}function V_(n,e){return P(()=>{const t=hn(n,xt(),1),s=hn(e,xt(),1);return ue(D(n,$n(fe(t,s))),-1)})}function W_(n,e){return P(()=>{const t=$n(X(xt(),e));return tt(ie(e,D(n,t)),-1)})}function h0(n,e){return P(()=>{const t=mu(n,-1),s=mu(e,-1),r=D(t,s);return Xe(ue(r,-1))})}const xu={meanSquaredError:gu,meanAbsoluteError:Bf,meanAbsolutePercentageError:Vf,meanSquaredLogarithmicError:O_,squaredHinge:L_,hinge:P_,categoricalHinge:z_,logcosh:M_,categoricalCrossentropy:Wi,sparseCategoricalCrossentropy:yu,binaryCrossentropy:bu,kullbackLeiblerDivergence:V_,poisson:W_,cosineProximity:h0};function Wf(n){if(typeof n=="string"){if(n in xu)return xu[n];let e=`Unknown loss ${n}`;throw n.toLowerCase().includes("softmaxcrossentropy")&&(e=`Unknown loss ${n}. Use "categoricalCrossentropy" as the string name for tf.losses.softmaxCrossEntropy`),new F(e)}else return n}/**
|
|
8581
8581
|
* @license
|
|
8582
8582
|
* Copyright 2018 Google LLC
|
|
8583
8583
|
*
|
|
@@ -8585,7 +8585,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
8585
8585
|
* license that can be found in the LICENSE file or at
|
|
8586
8586
|
* https://opensource.org/licenses/MIT.
|
|
8587
8587
|
* =============================================================================
|
|
8588
|
-
*/function d0(n,e){return P(()=>{const t=D(.5,Nn(e)),s=ys(pn(e,t),n.dtype);return tt(Jn(n,s),-1)})}function p0(n,e){return P(()=>ys(Jn(Qs(n,-1),Qs(e,-1)),"float32"))}function U_(n,e){return P(()=>re(ue(ds(Jn(n,1),Jn(e,1))),"float32"))}function G_(n,e){return P(()=>re(ue(ds(Jn(n,0),Jn(e,1))),"float32"))}function H_(n,e){return P(()=>{const t=U_(n,e),s=G_(n,e),r=X(t,s);return re(Vt(pn(r,0),
|
|
8588
|
+
*/function d0(n,e){return P(()=>{const t=D(.5,Nn(e)),s=ys(pn(e,t),n.dtype);return tt(Jn(n,s),-1)})}function p0(n,e){return P(()=>ys(Jn(Qs(n,-1),Qs(e,-1)),"float32"))}function U_(n,e){return P(()=>re(ue(ds(Jn(n,1),Jn(e,1))),"float32"))}function G_(n,e){return P(()=>re(ue(ds(Jn(n,0),Jn(e,1))),"float32"))}function H_(n,e){return P(()=>{const t=U_(n,e),s=G_(n,e),r=X(t,s);return re(Vt(pn(r,0),fe(t,r),0),"float32")})}function j_(n,e){return bu(n,e)}function q_(n,e){return n.rank===e.rank&&(n=Rs(n,[n.rank-1])),e=Qs(e,-1),e.dtype!==n.dtype&&(e=re(e,n.dtype)),re(Jn(n,e),"float32")}const K_=gu,X_=gu,Y_=Bf,Z_=Bf,J_=Vf,Q_=Vf,f0=Wi,eF=h0,m0=yu,wu={binaryAccuracy:d0,categoricalAccuracy:p0,precision:H_,categoricalCrossentropy:f0,sparseCategoricalCrossentropy:m0,mse:K_,MSE:X_,mae:Y_,MAE:Z_,mape:J_,MAPE:Q_,cosine:eF};function tF(n){if(typeof n=="string"&&n in wu)return wu[n];if(typeof n!="string"&&n!=null)return n;throw new F(`Unknown metric ${n}`)}function Iu(n){if(gs(n!==null,`Unknown LossOrMetricFn ${n}`),typeof n=="string")return n;{let e;for(const t of Object.keys(xu))if(xu[t]===n){e=t;break}if(e!==void 0)return e;for(const t of Object.keys(wu))if(wu[t]===n){e=t;break}return e!==void 0?e:n.name}}/**
|
|
8589
8589
|
* @license
|
|
8590
8590
|
* Copyright 2018 Google LLC
|
|
8591
8591
|
*
|
|
@@ -8641,7 +8641,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
8641
8641
|
* license that can be found in the LICENSE file or at
|
|
8642
8642
|
* https://opensource.org/licenses/MIT.
|
|
8643
8643
|
* =============================================================================
|
|
8644
|
-
*/const hF=32;function v0(n,e){let t,s;const r=e;t=r.xs,s=r.ys,S(t!=null&&s!=null,()=>`A Dataset iterator for fitDataset() is expected to generate objects of the form \`{xs: xVal, ys: yVal}\`, where the two values may be \`tf.Tensor\`, an array of Tensors, or a map of string to Tensor. The provided Dataset instead generates ${e}`);const a=S0("input",n.inputNames,t),o=S0("output",n.outputNames,s),i=a[0].shape[0];S(a.length===n.inputs.length,()=>`LayersModel has ${n.inputs.length} inputs, but the dataset provides ${a.length} inputs. (Expected input keys: ${JSON.stringify(n.inputNames)})`),S(o.length===n.outputs.length,()=>`LayersModel has ${n.outputs.length} outputs, but the dataset provides ${o.length} outputs. (Expected output keys: ${JSON.stringify(n.outputNames)})`);for(let l=0;l<a.length;l++)S(a[l].shape[0]===i,()=>`Batch size mismatch: input ${n.inputNames[l]} has ${a[l].shape[0]}; expected ${i} based on input ${n.inputNames[0]}.`);for(let l=0;l<o.length;l++)S(o[l].shape[0]===i,()=>`Batch size mismatch: output ${n.outputNames[l]} has ${o[l].shape[0]}; expected ${i} based on input ${n.inputNames[0]}.`);return{xs:a,ys:o}}function S0(n,e,t){if(t instanceof Oe)return[t];if(Array.isArray(t))return S(t.length===e.length,()=>`Received an array of ${t.length} Tensors, but expected ${e.length} to match the ${n} keys ${e}.`),t;{const s=[];for(const r of e){if(t[r]==null)throw new F(`The feature data generated by the dataset lacks the required ${n} key '${r}'.`);s.push(t[r])}return s}}function dF(n){if(n.length===3)throw new ve("Validation with sample weights is not implemented yet.");return{xs:n[0],ys:n[1]}}function pF(n,e,t){return O(this,null,function*(){const s=t.batchesPerEpoch!=null;if(S(n.optimizer!=null,()=>"You must compile a model before training/testing. Use LayersModel.compile(modelCompileConfig)."),S(t!=null,()=>"For fitDataset(), the 2nd argument (config) is required, but it is not provided in this call."),S(t.epochs!=null&&t.epochs>0&&Number.isInteger(t.epochs),()=>`For fitDataset(), config.epochs is expected to be a positive integer, but got ${t.epochs}`),S(!s||t.batchesPerEpoch>0&&Number.isInteger(t.batchesPerEpoch),()=>`For fitDataset(), config.batchesPerEpoch is expected to be a positive integer if specified, but got ${t.batchesPerEpoch}`),S(t.validationSplit==null,()=>"`validationSplit` is not supported by `fitDataset()`. Use validationData instead."),n.isTraining)throw new Error("Cannot start training because another fit() call is ongoing.");n.isTraining=!0;try{const r=t.validationData!=null;let a,o;if(r)if($0(t.validationData))S(t.validationBatches==null||t.validationBatches>0&&Number.isInteger(t.validationBatches),()=>`For fitDataset() with dataset-based validation, config.validationBatches is expected not to be provided, or to be a positive integer, but got ${t.validationBatches}`);else{const g=dF(t.validationData);a=g.xs,o=g.ys}const i=n.makeTrainFunction(),l=n.getDedupedMetricsNames();let c;r?c=l.slice().concat(l.map(g=>"val_"+g)):c=l.slice();const u=c0(t.callbacks,t.yieldEvery),h=t.verbose==null?1:t.verbose,{callbackList:d,history:p}=u0(u,h,t.epochs,null,null,fF(e,t),null,r,c);d.setModel(n),n.history=p,yield d.onTrainBegin(),n.stopTraining_=!1;let f=t.initialEpoch==null?0:t.initialEpoch,m=yield e.iterator();for(;f<t.epochs;){const g={};yield d.onEpochBegin(f);let y=0,b=0;for(s||(m=yield e.iterator());!s||y<t.batchesPerEpoch;){const w=yield m.next();if(s&&w.done){console.warn(`You provided \`batchesPerEpoch\` as ${t.batchesPerEpoch}, but your dataset iterator ran out of data after ${y} batches; interrupting training. Make sure that your dataset can generate at least \`batchesPerEpoch * epochs\` batches (in this case, ${t.batchesPerEpoch*t.epochs} batches). You may need to use the repeat() function when building your dataset.`);break}if(w.value!=null){const{xs:x,ys:v}=v0(n,w.value),$={};$.batch=b,$.size=x[0].shape[0],yield d.onBatchBegin(b,$);const C=[];if(t.classWeight!=null){const A=w0(t.classWeight,n.outputNames);for(let L=0;L<A.length;++L)C.push(yield I0(v[L],null,A[L]))}const k=x.concat(v).concat(C),T=i(k);be(k);for(let A=0;A<l.length;++A){const L=l[A],z=T[A];$[L]=z,Nt(z)}yield d.onBatchEnd(b,$),i0($),b++,y++}if(s?y>=t.batchesPerEpoch:w.done){if(r){let x;$0(t.validationData)?x=Ve(yield n.evaluateDataset(t.validationData,{batches:t.validationBatches})):x=Ve(n.evaluate(a,o,{batchSize:t.validationBatchSize==null?hF:t.validationBatchSize,verbose:0}));for(let v=0;v<n.metricsNames.length;++v)g[`val_${n.metricsNames[v]}`]=x[v]}break}if(n.stopTraining_)break}if(yield d.onEpochEnd(f,g),f++,n.stopTraining_)break}return yield d.onTrainEnd(),yield n.history.syncData(),n.history}finally{n.isTraining=!1}})}function fF(n,e){let t=null;return e.batchesPerEpoch!=null?t=e.batchesPerEpoch:Number.isFinite(n.size)&&(t=n.size),t}function $0(n){return typeof n.iterator=="function"}function mF(n){return typeof n.next=="function"}function gF(n,e,t){return O(this,null,function*(){t=t||{};const s=t.batches!=null,r=n.testFunction;let a=[];if(t.verbose>0)throw new ve("Verbose mode is not implemented yet.");S(!s||t.batches>0&&Number.isInteger(t.batches),()=>`Test loop expects \`batches\` to be a positive integer, but received ${JSON.stringify(t.batches)}`);const o=mF(e)?e:yield e.iterator();let i=0,l=0;for(;!s||l<t.batches;){const c=yield o.next();if(a=P(()=>{if(c.value){const{xs:u,ys:h}=v0(n,c.value),d=u.concat(h),p=P(()=>r(d));if(be(d),l===0)for(let m=0;m<p.length;++m)a.push(
|
|
8644
|
+
*/const hF=32;function v0(n,e){let t,s;const r=e;t=r.xs,s=r.ys,S(t!=null&&s!=null,()=>`A Dataset iterator for fitDataset() is expected to generate objects of the form \`{xs: xVal, ys: yVal}\`, where the two values may be \`tf.Tensor\`, an array of Tensors, or a map of string to Tensor. The provided Dataset instead generates ${e}`);const a=S0("input",n.inputNames,t),o=S0("output",n.outputNames,s),i=a[0].shape[0];S(a.length===n.inputs.length,()=>`LayersModel has ${n.inputs.length} inputs, but the dataset provides ${a.length} inputs. (Expected input keys: ${JSON.stringify(n.inputNames)})`),S(o.length===n.outputs.length,()=>`LayersModel has ${n.outputs.length} outputs, but the dataset provides ${o.length} outputs. (Expected output keys: ${JSON.stringify(n.outputNames)})`);for(let l=0;l<a.length;l++)S(a[l].shape[0]===i,()=>`Batch size mismatch: input ${n.inputNames[l]} has ${a[l].shape[0]}; expected ${i} based on input ${n.inputNames[0]}.`);for(let l=0;l<o.length;l++)S(o[l].shape[0]===i,()=>`Batch size mismatch: output ${n.outputNames[l]} has ${o[l].shape[0]}; expected ${i} based on input ${n.inputNames[0]}.`);return{xs:a,ys:o}}function S0(n,e,t){if(t instanceof Oe)return[t];if(Array.isArray(t))return S(t.length===e.length,()=>`Received an array of ${t.length} Tensors, but expected ${e.length} to match the ${n} keys ${e}.`),t;{const s=[];for(const r of e){if(t[r]==null)throw new F(`The feature data generated by the dataset lacks the required ${n} key '${r}'.`);s.push(t[r])}return s}}function dF(n){if(n.length===3)throw new ve("Validation with sample weights is not implemented yet.");return{xs:n[0],ys:n[1]}}function pF(n,e,t){return O(this,null,function*(){const s=t.batchesPerEpoch!=null;if(S(n.optimizer!=null,()=>"You must compile a model before training/testing. Use LayersModel.compile(modelCompileConfig)."),S(t!=null,()=>"For fitDataset(), the 2nd argument (config) is required, but it is not provided in this call."),S(t.epochs!=null&&t.epochs>0&&Number.isInteger(t.epochs),()=>`For fitDataset(), config.epochs is expected to be a positive integer, but got ${t.epochs}`),S(!s||t.batchesPerEpoch>0&&Number.isInteger(t.batchesPerEpoch),()=>`For fitDataset(), config.batchesPerEpoch is expected to be a positive integer if specified, but got ${t.batchesPerEpoch}`),S(t.validationSplit==null,()=>"`validationSplit` is not supported by `fitDataset()`. Use validationData instead."),n.isTraining)throw new Error("Cannot start training because another fit() call is ongoing.");n.isTraining=!0;try{const r=t.validationData!=null;let a,o;if(r)if($0(t.validationData))S(t.validationBatches==null||t.validationBatches>0&&Number.isInteger(t.validationBatches),()=>`For fitDataset() with dataset-based validation, config.validationBatches is expected not to be provided, or to be a positive integer, but got ${t.validationBatches}`);else{const g=dF(t.validationData);a=g.xs,o=g.ys}const i=n.makeTrainFunction(),l=n.getDedupedMetricsNames();let c;r?c=l.slice().concat(l.map(g=>"val_"+g)):c=l.slice();const u=c0(t.callbacks,t.yieldEvery),h=t.verbose==null?1:t.verbose,{callbackList:d,history:p}=u0(u,h,t.epochs,null,null,fF(e,t),null,r,c);d.setModel(n),n.history=p,yield d.onTrainBegin(),n.stopTraining_=!1;let f=t.initialEpoch==null?0:t.initialEpoch,m=yield e.iterator();for(;f<t.epochs;){const g={};yield d.onEpochBegin(f);let y=0,b=0;for(s||(m=yield e.iterator());!s||y<t.batchesPerEpoch;){const w=yield m.next();if(s&&w.done){console.warn(`You provided \`batchesPerEpoch\` as ${t.batchesPerEpoch}, but your dataset iterator ran out of data after ${y} batches; interrupting training. Make sure that your dataset can generate at least \`batchesPerEpoch * epochs\` batches (in this case, ${t.batchesPerEpoch*t.epochs} batches). You may need to use the repeat() function when building your dataset.`);break}if(w.value!=null){const{xs:x,ys:v}=v0(n,w.value),$={};$.batch=b,$.size=x[0].shape[0],yield d.onBatchBegin(b,$);const C=[];if(t.classWeight!=null){const A=w0(t.classWeight,n.outputNames);for(let L=0;L<A.length;++L)C.push(yield I0(v[L],null,A[L]))}const k=x.concat(v).concat(C),T=i(k);be(k);for(let A=0;A<l.length;++A){const L=l[A],z=T[A];$[L]=z,Nt(z)}yield d.onBatchEnd(b,$),i0($),b++,y++}if(s?y>=t.batchesPerEpoch:w.done){if(r){let x;$0(t.validationData)?x=Ve(yield n.evaluateDataset(t.validationData,{batches:t.validationBatches})):x=Ve(n.evaluate(a,o,{batchSize:t.validationBatchSize==null?hF:t.validationBatchSize,verbose:0}));for(let v=0;v<n.metricsNames.length;++v)g[`val_${n.metricsNames[v]}`]=x[v]}break}if(n.stopTraining_)break}if(yield d.onEpochEnd(f,g),f++,n.stopTraining_)break}return yield d.onTrainEnd(),yield n.history.syncData(),n.history}finally{n.isTraining=!1}})}function fF(n,e){let t=null;return e.batchesPerEpoch!=null?t=e.batchesPerEpoch:Number.isFinite(n.size)&&(t=n.size),t}function $0(n){return typeof n.iterator=="function"}function mF(n){return typeof n.next=="function"}function gF(n,e,t){return O(this,null,function*(){t=t||{};const s=t.batches!=null,r=n.testFunction;let a=[];if(t.verbose>0)throw new ve("Verbose mode is not implemented yet.");S(!s||t.batches>0&&Number.isInteger(t.batches),()=>`Test loop expects \`batches\` to be a positive integer, but received ${JSON.stringify(t.batches)}`);const o=mF(e)?e:yield e.iterator();let i=0,l=0;for(;!s||l<t.batches;){const c=yield o.next();if(a=P(()=>{if(c.value){const{xs:u,ys:h}=v0(n,c.value),d=u.concat(h),p=P(()=>r(d));if(be(d),l===0)for(let m=0;m<p.length;++m)a.push(pe(0));const f=d[0].shape[0];for(let m=0;m<p.length;++m){const g=p[m],y=a[m];a[m]=P(()=>X(a[m],D(f,g))),l>0&&be(y)}be(p),i+=f,++l}return a}),c.done){s&&console.warn(`Your dataset iterator ran out of data during evaluateDataset(). Interrupting evalution. Make sure that your dataset can generate at least \`batches\` batches (in this case, ${t.batches} batches). You may need to use the repeat() function when building your dataset.`);break}}for(let c=0;c<a.length;++c){const u=a[c];a[c]=fe(a[c],i),be(u)}return tn(a)})}/**
|
|
8645
8645
|
* @license
|
|
8646
8646
|
* Copyright 2018 Google LLC
|
|
8647
8647
|
*
|
|
@@ -8657,7 +8657,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
8657
8657
|
* license that can be found in the LICENSE file or at
|
|
8658
8658
|
* https://opensource.org/licenses/MIT.
|
|
8659
8659
|
* =============================================================================
|
|
8660
|
-
*/function yF(n){return n instanceof Oe}function Kf(n){return Array.isArray(n)}function C0(n){return!yF(n)&&!Kf(n)}function k0(n,e,t,s=!0,r=""){if(e==null||e.length===0){if(n!=null){let o=!1;if(Kf(n)&&n.length>0)o=!0;else if(C0(n)){for(const i in n)if(n.hasOwnProperty(i)){o=!0;break}}else o=!0;if(o)throw new F(`Error when checking model ${r} expected no data, but got ${n}`)}return[]}if(n==null)return e.map(o=>null);let a;if(C0(n)){n=n,a=[];for(const o of e){if(n[o]==null)throw new F(`No data provided for "${o}". Need data for each key in: ${e}`);a.push(n[o])}}else if(Kf(n)){if(n=n,n.length!==e.length)throw new F(`Error when checking model ${r}: the Array of Tensors that you are passing to your model is not the size the model expected. Expected to see ${e.length} Tensor(s), but instead got the following list of Tensor(s): ${n}`);a=n}else{if(n=n,e.length>1)throw new F(`The model ${r} expects ${e.length} Tensor(s), but only received one Tensor. Found: Tensor with shape ${n.shape}`);a=[n]}if(a=N0(a),t!=null)for(let o=0;o<e.length;++o){if(t[o]==null)continue;const i=a[o];if(i.shape.length!==t[o].length)throw new F(`Error when checking ${r}: expected ${e[o]} to have ${t[o].length} dimension(s). but got array with shape ${i.shape}`);for(let l=0;l<t[o].length;++l){if(l===0&&!s)continue;const c=i.shape[l],u=t[o][l];if(u!=null&&u>=0&&c!==u)throw new F(`${r} expected a batch of elements where each example has shape [${t[o].slice(1,t[o].length)}] (i.e.,tensor shape [*,${t[o].slice(1,t[o].length)}]) but the ${r} received an input with ${i.shape[0]} examples, each with shape [${i.shape.slice(1,i.shape.length)}] (tensor shape [${i.shape}])`)}}return a}function bF(n,e,t){const s=lr(n.map(a=>a.shape[0]));s.sort();const r=lr(e.map(a=>a.shape[0]));if(r.sort(),s.length>1)throw new F(`All input Tensors (x) should have the same number of samples. Got array shapes: ${JSON.stringify(n.map(a=>a.shape))}`);if(r.length>1)throw new F(`All target Tensors (y) should have the same number of samples. Got array shapes: ${JSON.stringify(e.map(a=>a.shape))}`);if(s.length>0&&r.length>0&&!Ee(s,r))throw new F(`Input Tensors should have the same number of samples as target Tensors. Found ${s[0]} input sample(s) and ${r[0]} target sample(s).`)}function xF(n,e,t){const s=[gu,bu,Wi];for(let r=0;r<n.length;++r){const a=n[r],o=e[r],i=t[r];if(o!=null){if(o===Wi&&a.shape[a.shape.length-1]===1)throw new F(`You are passing a target array of shape ${a.shape} while using a loss 'categorical_crossentropy'. 'categorical_crossentropy'expects targets to be binary matrices (1s and 0s) of shape [samples, classes].`);if(s.indexOf(o)!==-1){const l=a.shape.slice(1),c=i.slice(1);for(let u=0;u<l.length;++u){const h=l[u],d=c[u];if(d!=null&&h!==d)throw new F(`A target Tensor with shape ${a.shape} was passed for an output of shape ${i}, while using a loss function that expects targets to have the same shape as the output.`)}}}}}function T0(n,e,t,s=!0,r=""){let a;if(Array.isArray(n)){if(n.length!==e.length)throw new F(`Error when checking model ${r}: the Array of Tensors that you are passing to your model is not the size the the model expected. Expected to see ${e.length} Tensor(s), but instead got ${n.length} Tensors(s).`);a=n}else{if(e.length>1)throw new F(`The model expects ${e.length} ${r} Tensors, but only received one Tensor. Found: array with shape ${JSON.stringify(n.shape)}.`);a=[n]}if(t!=null)for(let o=0;o<e.length;++o){if(t[o]==null)continue;const i=a[o];if(i.shape.length!==t[o].length)throw new F(`Error when checking ${r}: expected ${e[o]} to have ${t[o].length} dimension(s), but got array with shape ${JSON.stringify(i.shape)}`);for(let l=0;l<t[o].length;++l){if(l===0&&!s)continue;const c=i.shape[l],u=t[o][l];if(u!=null&&u!==c)throw new F(`Error when checking ${r}: expected ${e[o]} to have shape ${JSON.stringify(t[o])} but got array with shape ${JSON.stringify(i.shape)}.`)}}}function wF(n,e){if(n==null||Array.isArray(n)&&n.length===0)return e.map(s=>[]);let t;if(typeof n=="string"||typeof n=="function")t=[n];else if(Array.isArray(n)||typeof n=="object")t=n;else throw new TypeError(`Type of metrics argument not understood. Expected an string,function, Array, or Object, found: ${n}`);if(Array.isArray(t))return e.map(s=>t);{const s=[];for(const r of e){let a=t.hasOwnProperty(r)?t[r]:[];Array.isArray(a)||(a=[a]),s.push(a)}return s}}const IF="layers-model";class ea extends rs{constructor(e){super(e),this.isTraining=!1}summary(e,t,s=console.log){if(!this.built)throw new F("This model has never been called, thus its weights have not been created yet. So no summary can be displayed. Build the model first (e.g., by calling it on some test data).");sF(this,e,t,s)}compile(e){if(e.loss==null&&(e.loss=[]),this.loss=e.loss,typeof e.optimizer=="string")this.optimizer_=nF(e.optimizer),this.isOptimizerOwned=!0;else{if(!(e.optimizer instanceof ir))throw new F("User-defined optimizer must be an instance of tf.Optimizer.");this.optimizer_=e.optimizer,this.isOptimizerOwned=!1}let t=[];if(!Array.isArray(e.loss)&&typeof e.loss!="string"&&typeof e.loss!="function"){e.loss=e.loss;for(const o in e.loss)if(this.outputNames.indexOf(o)===-1)throw new F(`Unknown entry in loss dictionary: "${o}". Only expected the following keys: ${this.outputNames}`);for(const o of this.outputNames)e.loss[o]==null&&console.warn(`Output "${o}" is missing from loss dictionary. We assume this was done on purpose, and we will not be expecting data to be passed to ${o} during training`),t.push(Wf(e.loss[o]))}else if(Array.isArray(e.loss)){if(e.loss.length!==this.outputs.length)throw new F(`When passing an Array as loss, it should have one entry per model output. The model has ${this.outputs.length} output(s), but you passed loss=${e.loss}.`);t=e.loss.map(i=>Wf(i))}else{const o=Wf(e.loss);this.outputs.forEach(i=>{t.push(o)})}this.lossFunctions=t,this.feedOutputNames=[],this.feedOutputShapes=[],this.feedLossFns=[];for(let o=0;o<this.outputs.length;++o){const i=this.internalOutputShapes[o],l=this.outputNames[o];this.feedOutputNames.push(l),this.feedOutputShapes.push(i),this.feedLossFns.push(this.lossFunctions[o])}const s=[];this.metrics=e.metrics,this.metricsNames=["loss"],this.metricsTensors=[],Zr("loss",()=>{for(let o=0;o<this.outputs.length;++o){if(s.indexOf(o)!==-1)continue;const i=this.lossFunctions[o];this.outputs.length>1&&(this.metricsTensors.push([i,o]),this.metricsNames.push(this.outputNames[o]+"_loss"))}});const r=wF(e.metrics,this.outputNames),a=(o,i,l)=>{this.outputNames.length>1&&(i=this.outputNames[o]+"_"+i),this.metricsNames.push(i),this.metricsTensors.push([l,o])};Zr("metric",()=>{for(let o=0;o<this.outputs.length;++o){if(s.indexOf(o)!==-1)continue;const i=r[o];(c=>{const u="";let h,d,p;for(const f of c){if(typeof f=="string"&&["accuracy","acc","crossentropy","ce"].indexOf(f)!==-1){const g=this.internalOutputShapes[o];g[g.length-1]===1||this.lossFunctions[o]===bu?["accuracy","acc"].indexOf(f)!==-1?d=d0:["crossentropy","ce"].indexOf(f)!==-1&&(d=j_):this.lossFunctions[o]===yu?["accuracy","acc"].indexOf(f)!==-1?d=q_:["crossentropy","ce"].indexOf(f)!==-1&&(d=m0):["accuracy","acc"].indexOf(f)!==-1?d=p0:["crossentropy","ce"].indexOf(f)!==-1&&(d=f0);let y;["accuracy","acc"].indexOf(f)!==-1?y="acc":["crossentropy","ce"].indexOf(f)!==-1&&(y="ce"),p=d,h=u+y}else p=tF(f),h=u+Iu(f);let m;Zr(h,()=>{m=p}),a(o,h,m)}})(i)}}),this.collectedTrainableWeights=this.trainableWeights}checkTrainableWeightsConsistency(){this.collectedTrainableWeights!=null&&this.trainableWeights.length!==this.collectedTrainableWeights.length&&console.warn("Discrepancy between trainableweights and collected trainable weights. Did you set `model.trainable` without calling `model.compile()` afterwards?")}evaluate(e,t,s={}){const r=s.batchSize==null?32:s.batchSize;Hf(r);const a=!0,o=this.standardizeUserDataXY(e,t,a,r);try{const i=o[0].concat(o[1]);this.makeTestFunction();const l=this.testFunction,c=this.testLoop(l,i,r,s.verbose,s.steps);return tn(c)}finally{as(o[0],e),as(o[1],t)}}evaluateDataset(e,t){return O(this,null,function*(){return this.makeTestFunction(),gF(this,e,t)})}checkNumSamples(e,t,s,r="steps"){let a;if(s!=null){if(a=null,t!=null)throw new F(`If ${r} is set, batchSize must be null or undefined.Got batchSize = ${t}`)}else if(e!=null)Array.isArray(e)?a=e[0].shape[0]:a=e.shape[0];else throw new F(`Either the input data should have a defined shape, or ${r} shoud be specified.`);return a}execute(e,t){if(Array.isArray(t)&&t.length===0)throw new F("`outputs` is an empty Array, which is not allowed.");const s=Array.isArray(t),r=s?t:[t],a=this.retrieveSymbolicTensors(r),o=new hr;if(e instanceof Oe&&(e=[e]),Array.isArray(e)){if(e.length!==this.inputs.length)throw new F(`The number of inputs provided (${e.length}) does not match the number of inputs of this model (${this.inputs.length}).`);for(let l=0;l<this.inputs.length;++l)o.add(this.inputs[l],e[l])}else for(const l of this.inputs){const c=e[l.name];if(c==null)throw new F(`No value is provided for the model's input ${l.name}`);o.add(l,c)}const i=Mi(a,o);return s?i:i[0]}retrieveSymbolicTensors(e){const t=Kr(null,e.length);let s=e.length;for(const r of this.layers){const a=Array.isArray(r.output)?r.output:[r.output],o=a.map(i=>i.name);for(let i=0;i<e.length;++i){const l=o.indexOf(e[i]);if(l!==-1&&(t[i]=a[l],s--),s===0)break}if(s===0)break}if(s>0){const r=[];throw t.forEach((a,o)=>{a==null&&r.push(e[o])}),new F(`Cannot find SymbolicTensors for output name(s): ${JSON.stringify(r)}`)}return t}predictLoop(e,t=32,s=!1){return P(()=>{const r=this.checkNumSamples(e);if(s)throw new ve("Verbose predictLoop() is not implemented yet.");const a=qf(r,t),o=this.outputs.map(i=>[]);for(let i=0;i<a.length;++i)P(()=>{const c=a[i][0],u=a[i][1],h=Gi(e,c,u),d=[];if(Array.isArray(h))for(let f=0;f<h.length;++f)d.push({key:this.inputs[f],value:h[f]});else d.push({key:this.inputs[0],value:h});const p=new hr(d);return Mi(this.outputs,p)}).forEach((c,u)=>o[u].push(c));return tn(o.map(i=>pt(i,0)))})}predict(e,t={}){const s=N0(e);T0(s,this.inputNames,this.feedInputShapes,!1);try{const r=t.batchSize==null?32:t.batchSize;return Hf(r),this.predictLoop(s,r)}finally{as(s,e)}}predictOnBatch(e){T0(e,this.inputNames,this.feedInputShapes,!0);const t=(Array.isArray(e)?e[0]:e).shape[0];return this.predictLoop(e,t)}standardizeUserDataXY(e,t,s=!0,r){if(this.optimizer_==null)throw new Mn("You must compile a model before training/testing. Use LayersModel.compile(modelCompileArgs).");const a=[];for(let o=0;o<this.feedOutputShapes.length;++o){const i=this.feedOutputShapes[o];this.feedLossFns[o]===yu?a.push(i.slice(0,i.length-1).concat([1])):a.push(i)}if(e=k0(e,this.feedInputNames,this.feedInputShapes,!1,"input"),t=k0(t,this.feedOutputNames,a,!1,"target"),bF(e,t),xF(t,this.feedLossFns,this.feedOutputShapes),this.stateful&&r!=null&&r>0&&e[0].shape[0]%r!==0)throw new F(`In a stateful network, you should only pass inputs with a number of samples that is divisible by the batch size ${r}. Found: ${e[0].shape[0]} sample(s).`);return[e,t]}standardizeUserData(e,t,s,r,a=!0,o){return O(this,null,function*(){const[i,l]=this.standardizeUserDataXY(e,t,a,o);if(s!=null)throw new Error("sample weight is not supported yet.");let c=null;if(r!=null){const u=w0(r,this.outputNames);c=[];for(let h=0;h<u.length;++h)c.push(yield I0(l[h],null,u[h]))}return[i,l,c]})}testLoop(e,t,s,r=0,a){return P(()=>{const o=this.checkNumSamples(t,s,a,"steps"),i=[];if(r>0)throw new ve("Verbose mode is not implemented yet.");if(a!=null)throw new ve("steps mode in testLoop() is not implemented yet");{const l=qf(o,s),c=ft(es(0,o));for(let u=0;u<l.length;++u){const h=l[u][0],d=l[u][1],p=Jr(c,h,d-h),f=jf(t,p),m=e(f);if(u===0)for(let g=0;g<m.length;++g)i.push(fe(0));for(let g=0;g<m.length;++g){const y=m[g];i[g]=X(i[g],D(d-h,y))}}for(let u=0;u<i.length;++u)i[u]=pe(i[u],o)}return i})}getDedupedMetricsNames(){const e=this.metricsNames,t=[];for(let s=0;s<e.length;++s){const r=e[s];let a=r;if(Rx(e,r)>1){const o=Rx(e.slice(0,s),r);a+=`_${o}`}t.push(a)}return t}makeTrainFunction(){return e=>{const t=[],s=e.slice(0,this.inputs.length),r=e.slice(this.inputs.length,this.inputs.length+this.outputs.length),a=e.slice(this.inputs.length+this.outputs.length,this.inputs.length+this.outputs.length*2),o=[],i=()=>{const h=[];for(let m=0;m<this.inputs.length;++m)h.push({key:this.inputs[m],value:s[m]});const d=new hr(h),p=Mi(this.outputs,d,{training:!0});let f;for(let m=0;m<this.lossFunctions.length;++m){const g=this.lossFunctions[m];let y=g(r[m],p[m]);a[m]!=null&&(y=uF(y,a[m]));const b=tt(y);t.push(b),m===0?f=y:f=X(f,y)}for(let m=0;m<this.metricsTensors.length;++m){let g;if(this.outputs.length>1&&m<this.outputs.length)g=t[m];else{const y=this.metricsTensors[m][0],b=this.metricsTensors[m][1];g=tt(y(r[b],p[b]))}Nt(g),o.push(g)}return f=tt(f),this.calculateLosses().forEach(m=>{f=X(f,m)}),f},l=this.collectedTrainableWeights.map(h=>h.read()),c=!0;return[this.optimizer_.minimize(i,c,l)].concat(o)}}makeTestFunction(){this.testFunction=e=>P(()=>{const t=[];let s;const r=e.slice(0,this.inputs.length),a=e.slice(this.inputs.length,this.inputs.length+this.outputs.length),o=[];for(let c=0;c<this.inputs.length;++c)o.push({key:this.inputs[c],value:r[c]});const i=new hr(o),l=Mi(this.outputs,i);for(let c=0;c<this.lossFunctions.length;++c){const u=this.lossFunctions[c],h=tt(u(a[c],l[c]));c===0?s=h:s=X(s,h),t.push(s)}for(let c=0;c<this.metricsTensors.length;++c){const u=this.metricsTensors[c][0],h=this.metricsTensors[c][1],d=tt(u(a[h],l[h]));t.push(d)}return t})}fit(r,a){return O(this,arguments,function*(e,t,s={}){if(this.isTraining)throw new Error("Cannot start training because another fit() call is ongoing.");this.isTraining=!0;let o,i,l,c,u,h,d,p,f;try{const m=s.batchSize==null?32:s.batchSize;Hf(m);const g=!1,y=yield this.standardizeUserData(e,t,s.sampleWeight,s.classWeight,g,m);o=y[0],i=y[1],f=y[2];let b=!1,w;if(s.validationData!=null&&s.validationData.length>0){if(b=!0,s.validationData.length===2)u=s.validationData[0],h=s.validationData[1];else throw s.validationData.length===3?new ve("validationData including sample weights is not supported yet."):new F(`When passing validation data, it must contain 2 (valX, valY) or 3 (valX, valY, valSampleWeight) items; ${s.validationData} is invalid.`);const L=!0,z=yield this.standardizeUserData(u,h,null,null,L,m);d=z[0],p=z[1],w=d.concat(p)}else if(s.validationSplit!=null&&s.validationSplit>0&&s.validationSplit<1){b=!0;const L=Math.floor(o[0].shape[0]*(1-s.validationSplit)),z=o[0].shape[0];d=Gi(o,L,z),l=o,o=Gi(o,0,L),p=Gi(i,L,z),c=i,i=Gi(i,0,L),w=d.concat(p)}else s.validationSteps!=null&&(b=!0);const x=o.concat(i).concat(f);this.checkTrainableWeightsConsistency();const v=this.makeTrainFunction(),$=this.getDedupedMetricsNames();let C,k;b?(this.makeTestFunction(),C=this.testFunction,k=$.slice().concat($.map(L=>"val_"+L))):(C=null,w=[],k=$.slice());const T=c0(s.callbacks,s.yieldEvery);return yield this.fitLoop(v,x,$,m,s.epochs,s.verbose,T,C,w,s.shuffle,k,s.initialEpoch,null,null)}finally{this.isTraining=!1,as(o,e),as(i,t),as(l,e),as(c,t),as(d,u),as(p,h),f!=null&&be(f)}})}fitLoop(e,t,s,r,a,o,i,l,c,u,h,d,p,f){return O(this,null,function*(){r==null&&(r=32),a==null&&(a=1),u==null&&(u=!0),d==null&&(d=0);let m=!1;if(l!=null&&c!=null&&(m=!0),f!=null&&(m=!0,p==null))throw new F("Can only use `validationSteps` when doing step-wise training, i.e., `stepsPerEpoch` must be set.");const g=this.checkNumSamples(t,r,p,"steps_per_epoch");let y;g!=null&&(y=es(0,g)),o==null&&(o=1);const{callbackList:b,history:w}=u0(i,o,a,d,g,p,r,m,h);b.setModel(this),this.history=w,yield b.onTrainBegin(),this.stopTraining_=!1;for(let x=d;x<a;++x){yield b.onEpochBegin(x);const v={};if(p!=null)throw new ve("stepsPerEpoch mode is not implemented yet.");{if(u==="batch")throw new ve("batch shuffling is not implemneted yet");u&&Cr(y);const $=ft(y),C=qf(g,r);for(let k=0;k<C.length;++k){const T={};if(yield b.onBatchBegin(k,T),P(()=>{const A=C[k][0],L=C[k][1],z=Jr($,A,L-A);T.batch=k,T.size=L-A;const B=jf(t,z),V=e(B);for(let W=0;W<s.length;++W){const G=s[W],q=V[W];T[G]=q,Nt(q)}if(k===C.length-1&&m){const W=this.testLoop(l,c,r);for(let G=0;G<s.length;++G){const q=s[G],j=W[G];Nt(j),v["val_"+q]=j}}}),yield b.onBatchEnd(k,T),i0(T),this.stopTraining_)break}$.dispose()}if(yield b.onEpochEnd(x,v),this.stopTraining_)break}return yield b.onTrainEnd(),yield this.history.syncData(),this.history})}fitDataset(e,t){return O(this,null,function*(){return pF(this,e,t)})}trainOnBatch(e,t){return O(this,null,function*(){const s=yield this.standardizeUserData(e,t),r=s[0],a=s[1],i=this.makeTrainFunction()(r.concat(a)),l=[];for(const c of i){const u=yield c.data();l.push(u[0])}return be(i),as(s[0],e),as(s[1],t),tn(l)})}getNamedWeights(e){const t=[],s=e!=null&&e.trainableOnly,r=s?this.trainableWeights:this.weights,a=this.getWeights(s);for(let o=0;o<r.length;++o)s&&!r[o].trainable||t.push({name:r[o].originalName,tensor:a[o]});return t}set stopTraining(e){this.stopTraining_=e}get stopTraining(){return this.stopTraining_}get optimizer(){return this.optimizer_}set optimizer(e){this.optimizer_!==e&&(this.optimizer_=e,this.isOptimizerOwned=!1)}dispose(){const e=super.dispose();if(e.refCountAfterDispose===0&&this.optimizer!=null&&this.isOptimizerOwned){const t=Kd().numTensors;this.optimizer_.dispose(),e.numDisposedVariables+=t-Kd().numTensors}return e}getLossIdentifiers(){let e;if(typeof this.loss=="string")e=Os(this.loss);else if(Array.isArray(this.loss)){for(const t of this.loss)if(typeof t!="string")throw new Error("Serialization of non-string loss is not supported.");e=this.loss.map(t=>Os(t))}else{const t=Object.keys(this.loss);e={};const s=this.loss;for(const r of t)if(typeof s[r]=="string")e[r]=Os(s[r]);else throw new Error("Serialization of non-string loss is not supported.")}return e}getMetricIdentifiers(){if(typeof this.metrics=="string"||typeof this.metrics=="function")return[Os(Iu(this.metrics))];if(Array.isArray(this.metrics))return this.metrics.map(e=>Os(Iu(e)));{const e={};for(const t in this.metrics)e[t]=Os(Iu(this.metrics[t]));return e}}getTrainingConfig(){return{loss:this.getLossIdentifiers(),metrics:this.getMetricIdentifiers(),optimizer_config:{class_name:this.optimizer.getClassName(),config:this.optimizer.getConfig()}}}loadTrainingConfig(e){if(e.weighted_metrics!=null)throw new Error("Loading weight_metrics is not supported yet.");if(e.loss_weights!=null)throw new Error("Loading loss_weights is not supported yet.");if(e.sample_weight_mode!=null)throw new Error("Loading sample_weight_mode is not supported yet.");const t=Ui(e.optimizer_config),s=ss(t);let r;if(typeof e.loss=="string")r=Xr(e.loss);else if(Array.isArray(e.loss))r=e.loss.map(o=>Xr(o));else if(e.loss!=null){r={};for(const o in e.loss)r[o]=Xr(e.loss[o])}let a;if(Array.isArray(e.metrics))a=e.metrics.map(o=>Xr(o));else if(e.metrics!=null){a={};for(const o in e.metrics)a[o]=Xr(e.metrics[o])}this.compile({loss:r,metrics:a,optimizer:s})}save(e,t){return O(this,null,function*(){if(typeof e=="string"){const c=ny(e);if(c.length===0)throw new F(`Cannot find any save handlers for URL '${e}'`);if(c.length>1)throw new F(`Found more than one (${c.length}) save handlers for URL '${e}'`);e=c[0]}if(e.save==null)throw new F("LayersModel.save() cannot proceed because the IOHandler provided does not have the `save` attribute defined.");const s=yield Bd(this.getNamedWeights(t)),r=!1,a=null,i={modelTopology:this.toJSON(a,r),format:IF,generatedBy:`TensorFlow.js tfjs-layers v${x0}`,convertedBy:null};if((t==null?!1:t.includeOptimizer)&&this.optimizer!=null){i.trainingConfig=this.getTrainingConfig();const c="optimizer",{data:u,specs:h}=yield Bd(yield this.optimizer.getWeights(),c);s.specs.push(...h),s.data=Zg([s.data,u])}return this.userDefinedMetadata!=null&&(y0(this.userDefinedMetadata,this.name,!0),i.userDefinedMetadata=this.userDefinedMetadata),i.weightData=s.data,i.weightSpecs=s.specs,e.save(i)})}setUserDefinedMetadata(e){y0(e,this.name),this.userDefinedMetadata=e}getUserDefinedMetadata(){return this.userDefinedMetadata}}ea.className="Model",Y(ea);class E0 extends ea{}E0.className="Functional",Y(E0);/**
|
|
8660
|
+
*/function yF(n){return n instanceof Oe}function Kf(n){return Array.isArray(n)}function C0(n){return!yF(n)&&!Kf(n)}function k0(n,e,t,s=!0,r=""){if(e==null||e.length===0){if(n!=null){let o=!1;if(Kf(n)&&n.length>0)o=!0;else if(C0(n)){for(const i in n)if(n.hasOwnProperty(i)){o=!0;break}}else o=!0;if(o)throw new F(`Error when checking model ${r} expected no data, but got ${n}`)}return[]}if(n==null)return e.map(o=>null);let a;if(C0(n)){n=n,a=[];for(const o of e){if(n[o]==null)throw new F(`No data provided for "${o}". Need data for each key in: ${e}`);a.push(n[o])}}else if(Kf(n)){if(n=n,n.length!==e.length)throw new F(`Error when checking model ${r}: the Array of Tensors that you are passing to your model is not the size the model expected. Expected to see ${e.length} Tensor(s), but instead got the following list of Tensor(s): ${n}`);a=n}else{if(n=n,e.length>1)throw new F(`The model ${r} expects ${e.length} Tensor(s), but only received one Tensor. Found: Tensor with shape ${n.shape}`);a=[n]}if(a=N0(a),t!=null)for(let o=0;o<e.length;++o){if(t[o]==null)continue;const i=a[o];if(i.shape.length!==t[o].length)throw new F(`Error when checking ${r}: expected ${e[o]} to have ${t[o].length} dimension(s). but got array with shape ${i.shape}`);for(let l=0;l<t[o].length;++l){if(l===0&&!s)continue;const c=i.shape[l],u=t[o][l];if(u!=null&&u>=0&&c!==u)throw new F(`${r} expected a batch of elements where each example has shape [${t[o].slice(1,t[o].length)}] (i.e.,tensor shape [*,${t[o].slice(1,t[o].length)}]) but the ${r} received an input with ${i.shape[0]} examples, each with shape [${i.shape.slice(1,i.shape.length)}] (tensor shape [${i.shape}])`)}}return a}function bF(n,e,t){const s=lr(n.map(a=>a.shape[0]));s.sort();const r=lr(e.map(a=>a.shape[0]));if(r.sort(),s.length>1)throw new F(`All input Tensors (x) should have the same number of samples. Got array shapes: ${JSON.stringify(n.map(a=>a.shape))}`);if(r.length>1)throw new F(`All target Tensors (y) should have the same number of samples. Got array shapes: ${JSON.stringify(e.map(a=>a.shape))}`);if(s.length>0&&r.length>0&&!Ee(s,r))throw new F(`Input Tensors should have the same number of samples as target Tensors. Found ${s[0]} input sample(s) and ${r[0]} target sample(s).`)}function xF(n,e,t){const s=[gu,bu,Wi];for(let r=0;r<n.length;++r){const a=n[r],o=e[r],i=t[r];if(o!=null){if(o===Wi&&a.shape[a.shape.length-1]===1)throw new F(`You are passing a target array of shape ${a.shape} while using a loss 'categorical_crossentropy'. 'categorical_crossentropy'expects targets to be binary matrices (1s and 0s) of shape [samples, classes].`);if(s.indexOf(o)!==-1){const l=a.shape.slice(1),c=i.slice(1);for(let u=0;u<l.length;++u){const h=l[u],d=c[u];if(d!=null&&h!==d)throw new F(`A target Tensor with shape ${a.shape} was passed for an output of shape ${i}, while using a loss function that expects targets to have the same shape as the output.`)}}}}}function T0(n,e,t,s=!0,r=""){let a;if(Array.isArray(n)){if(n.length!==e.length)throw new F(`Error when checking model ${r}: the Array of Tensors that you are passing to your model is not the size the the model expected. Expected to see ${e.length} Tensor(s), but instead got ${n.length} Tensors(s).`);a=n}else{if(e.length>1)throw new F(`The model expects ${e.length} ${r} Tensors, but only received one Tensor. Found: array with shape ${JSON.stringify(n.shape)}.`);a=[n]}if(t!=null)for(let o=0;o<e.length;++o){if(t[o]==null)continue;const i=a[o];if(i.shape.length!==t[o].length)throw new F(`Error when checking ${r}: expected ${e[o]} to have ${t[o].length} dimension(s), but got array with shape ${JSON.stringify(i.shape)}`);for(let l=0;l<t[o].length;++l){if(l===0&&!s)continue;const c=i.shape[l],u=t[o][l];if(u!=null&&u!==c)throw new F(`Error when checking ${r}: expected ${e[o]} to have shape ${JSON.stringify(t[o])} but got array with shape ${JSON.stringify(i.shape)}.`)}}}function wF(n,e){if(n==null||Array.isArray(n)&&n.length===0)return e.map(s=>[]);let t;if(typeof n=="string"||typeof n=="function")t=[n];else if(Array.isArray(n)||typeof n=="object")t=n;else throw new TypeError(`Type of metrics argument not understood. Expected an string,function, Array, or Object, found: ${n}`);if(Array.isArray(t))return e.map(s=>t);{const s=[];for(const r of e){let a=t.hasOwnProperty(r)?t[r]:[];Array.isArray(a)||(a=[a]),s.push(a)}return s}}const IF="layers-model";class ea extends rs{constructor(e){super(e),this.isTraining=!1}summary(e,t,s=console.log){if(!this.built)throw new F("This model has never been called, thus its weights have not been created yet. So no summary can be displayed. Build the model first (e.g., by calling it on some test data).");sF(this,e,t,s)}compile(e){if(e.loss==null&&(e.loss=[]),this.loss=e.loss,typeof e.optimizer=="string")this.optimizer_=nF(e.optimizer),this.isOptimizerOwned=!0;else{if(!(e.optimizer instanceof ir))throw new F("User-defined optimizer must be an instance of tf.Optimizer.");this.optimizer_=e.optimizer,this.isOptimizerOwned=!1}let t=[];if(!Array.isArray(e.loss)&&typeof e.loss!="string"&&typeof e.loss!="function"){e.loss=e.loss;for(const o in e.loss)if(this.outputNames.indexOf(o)===-1)throw new F(`Unknown entry in loss dictionary: "${o}". Only expected the following keys: ${this.outputNames}`);for(const o of this.outputNames)e.loss[o]==null&&console.warn(`Output "${o}" is missing from loss dictionary. We assume this was done on purpose, and we will not be expecting data to be passed to ${o} during training`),t.push(Wf(e.loss[o]))}else if(Array.isArray(e.loss)){if(e.loss.length!==this.outputs.length)throw new F(`When passing an Array as loss, it should have one entry per model output. The model has ${this.outputs.length} output(s), but you passed loss=${e.loss}.`);t=e.loss.map(i=>Wf(i))}else{const o=Wf(e.loss);this.outputs.forEach(i=>{t.push(o)})}this.lossFunctions=t,this.feedOutputNames=[],this.feedOutputShapes=[],this.feedLossFns=[];for(let o=0;o<this.outputs.length;++o){const i=this.internalOutputShapes[o],l=this.outputNames[o];this.feedOutputNames.push(l),this.feedOutputShapes.push(i),this.feedLossFns.push(this.lossFunctions[o])}const s=[];this.metrics=e.metrics,this.metricsNames=["loss"],this.metricsTensors=[],Zr("loss",()=>{for(let o=0;o<this.outputs.length;++o){if(s.indexOf(o)!==-1)continue;const i=this.lossFunctions[o];this.outputs.length>1&&(this.metricsTensors.push([i,o]),this.metricsNames.push(this.outputNames[o]+"_loss"))}});const r=wF(e.metrics,this.outputNames),a=(o,i,l)=>{this.outputNames.length>1&&(i=this.outputNames[o]+"_"+i),this.metricsNames.push(i),this.metricsTensors.push([l,o])};Zr("metric",()=>{for(let o=0;o<this.outputs.length;++o){if(s.indexOf(o)!==-1)continue;const i=r[o];(c=>{const u="";let h,d,p;for(const f of c){if(typeof f=="string"&&["accuracy","acc","crossentropy","ce"].indexOf(f)!==-1){const g=this.internalOutputShapes[o];g[g.length-1]===1||this.lossFunctions[o]===bu?["accuracy","acc"].indexOf(f)!==-1?d=d0:["crossentropy","ce"].indexOf(f)!==-1&&(d=j_):this.lossFunctions[o]===yu?["accuracy","acc"].indexOf(f)!==-1?d=q_:["crossentropy","ce"].indexOf(f)!==-1&&(d=m0):["accuracy","acc"].indexOf(f)!==-1?d=p0:["crossentropy","ce"].indexOf(f)!==-1&&(d=f0);let y;["accuracy","acc"].indexOf(f)!==-1?y="acc":["crossentropy","ce"].indexOf(f)!==-1&&(y="ce"),p=d,h=u+y}else p=tF(f),h=u+Iu(f);let m;Zr(h,()=>{m=p}),a(o,h,m)}})(i)}}),this.collectedTrainableWeights=this.trainableWeights}checkTrainableWeightsConsistency(){this.collectedTrainableWeights!=null&&this.trainableWeights.length!==this.collectedTrainableWeights.length&&console.warn("Discrepancy between trainableweights and collected trainable weights. Did you set `model.trainable` without calling `model.compile()` afterwards?")}evaluate(e,t,s={}){const r=s.batchSize==null?32:s.batchSize;Hf(r);const a=!0,o=this.standardizeUserDataXY(e,t,a,r);try{const i=o[0].concat(o[1]);this.makeTestFunction();const l=this.testFunction,c=this.testLoop(l,i,r,s.verbose,s.steps);return tn(c)}finally{as(o[0],e),as(o[1],t)}}evaluateDataset(e,t){return O(this,null,function*(){return this.makeTestFunction(),gF(this,e,t)})}checkNumSamples(e,t,s,r="steps"){let a;if(s!=null){if(a=null,t!=null)throw new F(`If ${r} is set, batchSize must be null or undefined.Got batchSize = ${t}`)}else if(e!=null)Array.isArray(e)?a=e[0].shape[0]:a=e.shape[0];else throw new F(`Either the input data should have a defined shape, or ${r} shoud be specified.`);return a}execute(e,t){if(Array.isArray(t)&&t.length===0)throw new F("`outputs` is an empty Array, which is not allowed.");const s=Array.isArray(t),r=s?t:[t],a=this.retrieveSymbolicTensors(r),o=new hr;if(e instanceof Oe&&(e=[e]),Array.isArray(e)){if(e.length!==this.inputs.length)throw new F(`The number of inputs provided (${e.length}) does not match the number of inputs of this model (${this.inputs.length}).`);for(let l=0;l<this.inputs.length;++l)o.add(this.inputs[l],e[l])}else for(const l of this.inputs){const c=e[l.name];if(c==null)throw new F(`No value is provided for the model's input ${l.name}`);o.add(l,c)}const i=Mi(a,o);return s?i:i[0]}retrieveSymbolicTensors(e){const t=Kr(null,e.length);let s=e.length;for(const r of this.layers){const a=Array.isArray(r.output)?r.output:[r.output],o=a.map(i=>i.name);for(let i=0;i<e.length;++i){const l=o.indexOf(e[i]);if(l!==-1&&(t[i]=a[l],s--),s===0)break}if(s===0)break}if(s>0){const r=[];throw t.forEach((a,o)=>{a==null&&r.push(e[o])}),new F(`Cannot find SymbolicTensors for output name(s): ${JSON.stringify(r)}`)}return t}predictLoop(e,t=32,s=!1){return P(()=>{const r=this.checkNumSamples(e);if(s)throw new ve("Verbose predictLoop() is not implemented yet.");const a=qf(r,t),o=this.outputs.map(i=>[]);for(let i=0;i<a.length;++i)P(()=>{const c=a[i][0],u=a[i][1],h=Gi(e,c,u),d=[];if(Array.isArray(h))for(let f=0;f<h.length;++f)d.push({key:this.inputs[f],value:h[f]});else d.push({key:this.inputs[0],value:h});const p=new hr(d);return Mi(this.outputs,p)}).forEach((c,u)=>o[u].push(c));return tn(o.map(i=>pt(i,0)))})}predict(e,t={}){const s=N0(e);T0(s,this.inputNames,this.feedInputShapes,!1);try{const r=t.batchSize==null?32:t.batchSize;return Hf(r),this.predictLoop(s,r)}finally{as(s,e)}}predictOnBatch(e){T0(e,this.inputNames,this.feedInputShapes,!0);const t=(Array.isArray(e)?e[0]:e).shape[0];return this.predictLoop(e,t)}standardizeUserDataXY(e,t,s=!0,r){if(this.optimizer_==null)throw new Mn("You must compile a model before training/testing. Use LayersModel.compile(modelCompileArgs).");const a=[];for(let o=0;o<this.feedOutputShapes.length;++o){const i=this.feedOutputShapes[o];this.feedLossFns[o]===yu?a.push(i.slice(0,i.length-1).concat([1])):a.push(i)}if(e=k0(e,this.feedInputNames,this.feedInputShapes,!1,"input"),t=k0(t,this.feedOutputNames,a,!1,"target"),bF(e,t),xF(t,this.feedLossFns,this.feedOutputShapes),this.stateful&&r!=null&&r>0&&e[0].shape[0]%r!==0)throw new F(`In a stateful network, you should only pass inputs with a number of samples that is divisible by the batch size ${r}. Found: ${e[0].shape[0]} sample(s).`);return[e,t]}standardizeUserData(e,t,s,r,a=!0,o){return O(this,null,function*(){const[i,l]=this.standardizeUserDataXY(e,t,a,o);if(s!=null)throw new Error("sample weight is not supported yet.");let c=null;if(r!=null){const u=w0(r,this.outputNames);c=[];for(let h=0;h<u.length;++h)c.push(yield I0(l[h],null,u[h]))}return[i,l,c]})}testLoop(e,t,s,r=0,a){return P(()=>{const o=this.checkNumSamples(t,s,a,"steps"),i=[];if(r>0)throw new ve("Verbose mode is not implemented yet.");if(a!=null)throw new ve("steps mode in testLoop() is not implemented yet");{const l=qf(o,s),c=ft(es(0,o));for(let u=0;u<l.length;++u){const h=l[u][0],d=l[u][1],p=Jr(c,h,d-h),f=jf(t,p),m=e(f);if(u===0)for(let g=0;g<m.length;++g)i.push(pe(0));for(let g=0;g<m.length;++g){const y=m[g];i[g]=X(i[g],D(d-h,y))}}for(let u=0;u<i.length;++u)i[u]=fe(i[u],o)}return i})}getDedupedMetricsNames(){const e=this.metricsNames,t=[];for(let s=0;s<e.length;++s){const r=e[s];let a=r;if(Rx(e,r)>1){const o=Rx(e.slice(0,s),r);a+=`_${o}`}t.push(a)}return t}makeTrainFunction(){return e=>{const t=[],s=e.slice(0,this.inputs.length),r=e.slice(this.inputs.length,this.inputs.length+this.outputs.length),a=e.slice(this.inputs.length+this.outputs.length,this.inputs.length+this.outputs.length*2),o=[],i=()=>{const h=[];for(let m=0;m<this.inputs.length;++m)h.push({key:this.inputs[m],value:s[m]});const d=new hr(h),p=Mi(this.outputs,d,{training:!0});let f;for(let m=0;m<this.lossFunctions.length;++m){const g=this.lossFunctions[m];let y=g(r[m],p[m]);a[m]!=null&&(y=uF(y,a[m]));const b=tt(y);t.push(b),m===0?f=y:f=X(f,y)}for(let m=0;m<this.metricsTensors.length;++m){let g;if(this.outputs.length>1&&m<this.outputs.length)g=t[m];else{const y=this.metricsTensors[m][0],b=this.metricsTensors[m][1];g=tt(y(r[b],p[b]))}Nt(g),o.push(g)}return f=tt(f),this.calculateLosses().forEach(m=>{f=X(f,m)}),f},l=this.collectedTrainableWeights.map(h=>h.read()),c=!0;return[this.optimizer_.minimize(i,c,l)].concat(o)}}makeTestFunction(){this.testFunction=e=>P(()=>{const t=[];let s;const r=e.slice(0,this.inputs.length),a=e.slice(this.inputs.length,this.inputs.length+this.outputs.length),o=[];for(let c=0;c<this.inputs.length;++c)o.push({key:this.inputs[c],value:r[c]});const i=new hr(o),l=Mi(this.outputs,i);for(let c=0;c<this.lossFunctions.length;++c){const u=this.lossFunctions[c],h=tt(u(a[c],l[c]));c===0?s=h:s=X(s,h),t.push(s)}for(let c=0;c<this.metricsTensors.length;++c){const u=this.metricsTensors[c][0],h=this.metricsTensors[c][1],d=tt(u(a[h],l[h]));t.push(d)}return t})}fit(r,a){return O(this,arguments,function*(e,t,s={}){if(this.isTraining)throw new Error("Cannot start training because another fit() call is ongoing.");this.isTraining=!0;let o,i,l,c,u,h,d,p,f;try{const m=s.batchSize==null?32:s.batchSize;Hf(m);const g=!1,y=yield this.standardizeUserData(e,t,s.sampleWeight,s.classWeight,g,m);o=y[0],i=y[1],f=y[2];let b=!1,w;if(s.validationData!=null&&s.validationData.length>0){if(b=!0,s.validationData.length===2)u=s.validationData[0],h=s.validationData[1];else throw s.validationData.length===3?new ve("validationData including sample weights is not supported yet."):new F(`When passing validation data, it must contain 2 (valX, valY) or 3 (valX, valY, valSampleWeight) items; ${s.validationData} is invalid.`);const L=!0,z=yield this.standardizeUserData(u,h,null,null,L,m);d=z[0],p=z[1],w=d.concat(p)}else if(s.validationSplit!=null&&s.validationSplit>0&&s.validationSplit<1){b=!0;const L=Math.floor(o[0].shape[0]*(1-s.validationSplit)),z=o[0].shape[0];d=Gi(o,L,z),l=o,o=Gi(o,0,L),p=Gi(i,L,z),c=i,i=Gi(i,0,L),w=d.concat(p)}else s.validationSteps!=null&&(b=!0);const x=o.concat(i).concat(f);this.checkTrainableWeightsConsistency();const v=this.makeTrainFunction(),$=this.getDedupedMetricsNames();let C,k;b?(this.makeTestFunction(),C=this.testFunction,k=$.slice().concat($.map(L=>"val_"+L))):(C=null,w=[],k=$.slice());const T=c0(s.callbacks,s.yieldEvery);return yield this.fitLoop(v,x,$,m,s.epochs,s.verbose,T,C,w,s.shuffle,k,s.initialEpoch,null,null)}finally{this.isTraining=!1,as(o,e),as(i,t),as(l,e),as(c,t),as(d,u),as(p,h),f!=null&&be(f)}})}fitLoop(e,t,s,r,a,o,i,l,c,u,h,d,p,f){return O(this,null,function*(){r==null&&(r=32),a==null&&(a=1),u==null&&(u=!0),d==null&&(d=0);let m=!1;if(l!=null&&c!=null&&(m=!0),f!=null&&(m=!0,p==null))throw new F("Can only use `validationSteps` when doing step-wise training, i.e., `stepsPerEpoch` must be set.");const g=this.checkNumSamples(t,r,p,"steps_per_epoch");let y;g!=null&&(y=es(0,g)),o==null&&(o=1);const{callbackList:b,history:w}=u0(i,o,a,d,g,p,r,m,h);b.setModel(this),this.history=w,yield b.onTrainBegin(),this.stopTraining_=!1;for(let x=d;x<a;++x){yield b.onEpochBegin(x);const v={};if(p!=null)throw new ve("stepsPerEpoch mode is not implemented yet.");{if(u==="batch")throw new ve("batch shuffling is not implemneted yet");u&&Cr(y);const $=ft(y),C=qf(g,r);for(let k=0;k<C.length;++k){const T={};if(yield b.onBatchBegin(k,T),P(()=>{const A=C[k][0],L=C[k][1],z=Jr($,A,L-A);T.batch=k,T.size=L-A;const B=jf(t,z),V=e(B);for(let W=0;W<s.length;++W){const G=s[W],q=V[W];T[G]=q,Nt(q)}if(k===C.length-1&&m){const W=this.testLoop(l,c,r);for(let G=0;G<s.length;++G){const q=s[G],j=W[G];Nt(j),v["val_"+q]=j}}}),yield b.onBatchEnd(k,T),i0(T),this.stopTraining_)break}$.dispose()}if(yield b.onEpochEnd(x,v),this.stopTraining_)break}return yield b.onTrainEnd(),yield this.history.syncData(),this.history})}fitDataset(e,t){return O(this,null,function*(){return pF(this,e,t)})}trainOnBatch(e,t){return O(this,null,function*(){const s=yield this.standardizeUserData(e,t),r=s[0],a=s[1],i=this.makeTrainFunction()(r.concat(a)),l=[];for(const c of i){const u=yield c.data();l.push(u[0])}return be(i),as(s[0],e),as(s[1],t),tn(l)})}getNamedWeights(e){const t=[],s=e!=null&&e.trainableOnly,r=s?this.trainableWeights:this.weights,a=this.getWeights(s);for(let o=0;o<r.length;++o)s&&!r[o].trainable||t.push({name:r[o].originalName,tensor:a[o]});return t}set stopTraining(e){this.stopTraining_=e}get stopTraining(){return this.stopTraining_}get optimizer(){return this.optimizer_}set optimizer(e){this.optimizer_!==e&&(this.optimizer_=e,this.isOptimizerOwned=!1)}dispose(){const e=super.dispose();if(e.refCountAfterDispose===0&&this.optimizer!=null&&this.isOptimizerOwned){const t=Kd().numTensors;this.optimizer_.dispose(),e.numDisposedVariables+=t-Kd().numTensors}return e}getLossIdentifiers(){let e;if(typeof this.loss=="string")e=Os(this.loss);else if(Array.isArray(this.loss)){for(const t of this.loss)if(typeof t!="string")throw new Error("Serialization of non-string loss is not supported.");e=this.loss.map(t=>Os(t))}else{const t=Object.keys(this.loss);e={};const s=this.loss;for(const r of t)if(typeof s[r]=="string")e[r]=Os(s[r]);else throw new Error("Serialization of non-string loss is not supported.")}return e}getMetricIdentifiers(){if(typeof this.metrics=="string"||typeof this.metrics=="function")return[Os(Iu(this.metrics))];if(Array.isArray(this.metrics))return this.metrics.map(e=>Os(Iu(e)));{const e={};for(const t in this.metrics)e[t]=Os(Iu(this.metrics[t]));return e}}getTrainingConfig(){return{loss:this.getLossIdentifiers(),metrics:this.getMetricIdentifiers(),optimizer_config:{class_name:this.optimizer.getClassName(),config:this.optimizer.getConfig()}}}loadTrainingConfig(e){if(e.weighted_metrics!=null)throw new Error("Loading weight_metrics is not supported yet.");if(e.loss_weights!=null)throw new Error("Loading loss_weights is not supported yet.");if(e.sample_weight_mode!=null)throw new Error("Loading sample_weight_mode is not supported yet.");const t=Ui(e.optimizer_config),s=ss(t);let r;if(typeof e.loss=="string")r=Xr(e.loss);else if(Array.isArray(e.loss))r=e.loss.map(o=>Xr(o));else if(e.loss!=null){r={};for(const o in e.loss)r[o]=Xr(e.loss[o])}let a;if(Array.isArray(e.metrics))a=e.metrics.map(o=>Xr(o));else if(e.metrics!=null){a={};for(const o in e.metrics)a[o]=Xr(e.metrics[o])}this.compile({loss:r,metrics:a,optimizer:s})}save(e,t){return O(this,null,function*(){if(typeof e=="string"){const c=ny(e);if(c.length===0)throw new F(`Cannot find any save handlers for URL '${e}'`);if(c.length>1)throw new F(`Found more than one (${c.length}) save handlers for URL '${e}'`);e=c[0]}if(e.save==null)throw new F("LayersModel.save() cannot proceed because the IOHandler provided does not have the `save` attribute defined.");const s=yield Bd(this.getNamedWeights(t)),r=!1,a=null,i={modelTopology:this.toJSON(a,r),format:IF,generatedBy:`TensorFlow.js tfjs-layers v${x0}`,convertedBy:null};if((t==null?!1:t.includeOptimizer)&&this.optimizer!=null){i.trainingConfig=this.getTrainingConfig();const c="optimizer",{data:u,specs:h}=yield Bd(yield this.optimizer.getWeights(),c);s.specs.push(...h),s.data=Zg([s.data,u])}return this.userDefinedMetadata!=null&&(y0(this.userDefinedMetadata,this.name,!0),i.userDefinedMetadata=this.userDefinedMetadata),i.weightData=s.data,i.weightSpecs=s.specs,e.save(i)})}setUserDefinedMetadata(e){y0(e,this.name),this.userDefinedMetadata=e}getUserDefinedMetadata(){return this.userDefinedMetadata}}ea.className="Model",Y(ea);class E0 extends ea{}E0.className="Functional",Y(E0);/**
|
|
8661
8661
|
* @license
|
|
8662
8662
|
* Copyright 2018 Google LLC
|
|
8663
8663
|
*
|
|
@@ -8697,7 +8697,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
8697
8697
|
* license that can be found in the LICENSE file or at
|
|
8698
8698
|
* https://opensource.org/licenses/MIT.
|
|
8699
8699
|
* =============================================================================
|
|
8700
|
-
*/class q0 extends ke{constructor(e){super(e==null?{}:e),this.supportsMasking=!0,e!=null&&(this.maxValue=e.maxValue)}call(e,t){e=we(e);let s=ps(e);return this.maxValue!=null&&(s=hn(s,0,this.maxValue)),s}computeOutputShape(e){return e}getConfig(){const e={maxValue:this.maxValue},t=super.getConfig();return Object.assign(e,t),e}}q0.className="ReLU",Y(q0);class K0 extends ke{constructor(e){super(e==null?{}:e),this.DEFAULT_ALPHA=.3,e==null&&(e={}),this.alpha=e.alpha==null?this.DEFAULT_ALPHA:e.alpha}call(e,t){const s=we(e);return zc(s,this.alpha)}computeOutputShape(e){return e}getConfig(){const e={alpha:this.alpha},t=super.getConfig();return Object.assign(e,t),e}}K0.className="LeakyReLU",Y(K0);class X0 extends ke{constructor(e){if(super(e==null?{}:e),this.DEFAULT_ALPHA_INITIALIZER="zeros",e==null&&(e={}),this.supportsMasking=!0,this.alphaInitializer=Ye(e.alphaInitializer||this.DEFAULT_ALPHA_INITIALIZER),this.alphaRegularizer=Ze(e.alphaRegularizer),this.alphaConstraint=vt(e.alphaConstraint),e.sharedAxes==null)this.sharedAxes=null;else if(Array.isArray(e.sharedAxes))this.sharedAxes=e.sharedAxes;else if(typeof e.sharedAxes=="number")this.sharedAxes=[e.sharedAxes];else throw new F(`Expected sharedAxes to be a number or an array of numbers, but got ${e.sharedAxes}`)}build(e){e=Fe(e);const t=e.slice(1);if(this.sharedAxes!=null)for(const r of this.sharedAxes)t[r-1]=1;this.alpha=this.addWeight("alpha",t,"float32",this.alphaInitializer,this.alphaRegularizer,!0,this.alphaConstraint);const s={};if(this.sharedAxes!=null)for(let r=1;r<e.length;++r)s[r]=e[r];this.inputSpec=[new wt({ndim:e.length,axes:s})],this.built=!0}call(e,t){return e=we(e),Hc(e,this.alpha.read())}getConfig(){const e={alphaInitializer:nt(this.alphaInitializer),alphaRegularizer:We(this.alphaRegularizer),alphaConstraint:It(this.alphaConstraint),sharedAxes:this.sharedAxes},t=super.getConfig();return Object.assign(e,t),e}}X0.className="PReLU",Y(X0);let Y0=class extends ke{constructor(e){if(super(e==null?{}:e),this.DEFAULT_ALPHA=1,e==null&&(e={}),e.alpha!=null&&e.alpha!==this.DEFAULT_ALPHA)throw new ve(`Non-default alpha value (${e.alpha}) is not supported by the ELU layer yet.`);this.alpha=e.alpha==null?this.DEFAULT_ALPHA:e.alpha}call(e,t){const s=we(e);return vi(s)}computeOutputShape(e){return e}getConfig(){const e={alpha:this.alpha},t=super.getConfig();return Object.assign(e,t),e}};Y0.className="ELU",Y(Y0);class Z0 extends ke{constructor(e){super(e==null?{}:e),this.DEFAULT_THETA=1,e==null&&(e={}),this.theta=e.theta==null?this.DEFAULT_THETA:e.theta}call(e,t){const s=we(e);return D(s,re(pn(s,this.theta),"float32"))}computeOutputShape(e){return e}getConfig(){const e={theta:this.theta},t=super.getConfig();return Object.assign(e,t),e}}Z0.className="ThresholdedReLU",Y(Z0);class J0 extends ke{constructor(e){super(e==null?{}:e),this.DEFAULT_AXIS=1,e==null&&(e={}),this.softmax=new Xf().apply,this.axis=e.axis==null?this.DEFAULT_AXIS:e.axis}call(e,t){return P(()=>{let s=we(e);const r=t.mask;if(r!=null){const a=D(ie(fn(s.shape),re(r,s.dtype)),
|
|
8700
|
+
*/class q0 extends ke{constructor(e){super(e==null?{}:e),this.supportsMasking=!0,e!=null&&(this.maxValue=e.maxValue)}call(e,t){e=we(e);let s=ps(e);return this.maxValue!=null&&(s=hn(s,0,this.maxValue)),s}computeOutputShape(e){return e}getConfig(){const e={maxValue:this.maxValue},t=super.getConfig();return Object.assign(e,t),e}}q0.className="ReLU",Y(q0);class K0 extends ke{constructor(e){super(e==null?{}:e),this.DEFAULT_ALPHA=.3,e==null&&(e={}),this.alpha=e.alpha==null?this.DEFAULT_ALPHA:e.alpha}call(e,t){const s=we(e);return zc(s,this.alpha)}computeOutputShape(e){return e}getConfig(){const e={alpha:this.alpha},t=super.getConfig();return Object.assign(e,t),e}}K0.className="LeakyReLU",Y(K0);class X0 extends ke{constructor(e){if(super(e==null?{}:e),this.DEFAULT_ALPHA_INITIALIZER="zeros",e==null&&(e={}),this.supportsMasking=!0,this.alphaInitializer=Ye(e.alphaInitializer||this.DEFAULT_ALPHA_INITIALIZER),this.alphaRegularizer=Ze(e.alphaRegularizer),this.alphaConstraint=vt(e.alphaConstraint),e.sharedAxes==null)this.sharedAxes=null;else if(Array.isArray(e.sharedAxes))this.sharedAxes=e.sharedAxes;else if(typeof e.sharedAxes=="number")this.sharedAxes=[e.sharedAxes];else throw new F(`Expected sharedAxes to be a number or an array of numbers, but got ${e.sharedAxes}`)}build(e){e=Fe(e);const t=e.slice(1);if(this.sharedAxes!=null)for(const r of this.sharedAxes)t[r-1]=1;this.alpha=this.addWeight("alpha",t,"float32",this.alphaInitializer,this.alphaRegularizer,!0,this.alphaConstraint);const s={};if(this.sharedAxes!=null)for(let r=1;r<e.length;++r)s[r]=e[r];this.inputSpec=[new wt({ndim:e.length,axes:s})],this.built=!0}call(e,t){return e=we(e),Hc(e,this.alpha.read())}getConfig(){const e={alphaInitializer:nt(this.alphaInitializer),alphaRegularizer:We(this.alphaRegularizer),alphaConstraint:It(this.alphaConstraint),sharedAxes:this.sharedAxes},t=super.getConfig();return Object.assign(e,t),e}}X0.className="PReLU",Y(X0);let Y0=class extends ke{constructor(e){if(super(e==null?{}:e),this.DEFAULT_ALPHA=1,e==null&&(e={}),e.alpha!=null&&e.alpha!==this.DEFAULT_ALPHA)throw new ve(`Non-default alpha value (${e.alpha}) is not supported by the ELU layer yet.`);this.alpha=e.alpha==null?this.DEFAULT_ALPHA:e.alpha}call(e,t){const s=we(e);return vi(s)}computeOutputShape(e){return e}getConfig(){const e={alpha:this.alpha},t=super.getConfig();return Object.assign(e,t),e}};Y0.className="ELU",Y(Y0);class Z0 extends ke{constructor(e){super(e==null?{}:e),this.DEFAULT_THETA=1,e==null&&(e={}),this.theta=e.theta==null?this.DEFAULT_THETA:e.theta}call(e,t){const s=we(e);return D(s,re(pn(s,this.theta),"float32"))}computeOutputShape(e){return e}getConfig(){const e={theta:this.theta},t=super.getConfig();return Object.assign(e,t),e}}Z0.className="ThresholdedReLU",Y(Z0);class J0 extends ke{constructor(e){super(e==null?{}:e),this.DEFAULT_AXIS=1,e==null&&(e={}),this.softmax=new Xf().apply,this.axis=e.axis==null?this.DEFAULT_AXIS:e.axis}call(e,t){return P(()=>{let s=we(e);const r=t.mask;if(r!=null){const a=D(ie(fn(s.shape),re(r,s.dtype)),pe(-1e9));s=X(s,a)}return this.axis instanceof Array?this.axis.length>1?dn(ie(s,Bc(s,this.axis,!0))):this.softmax(s,this.axis[0]):this.softmax(s,this.axis)})}computeOutputShape(e){return e}getConfig(){const e={axis:this.axis},t=super.getConfig();return Object.assign(e,t),e}}J0.className="Softmax",Y(J0);/**
|
|
8701
8701
|
* @license
|
|
8702
8702
|
* Copyright 2018 Google LLC
|
|
8703
8703
|
*
|
|
@@ -9269,7 +9269,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
9269
9269
|
* See the License for the specific language governing permissions and
|
|
9270
9270
|
* limitations under the License.
|
|
9271
9271
|
* =============================================================================
|
|
9272
|
-
*/var Ot=Object.freeze({__proto__:null,OP_SCOPE_SUFFIX:Xg,abs:yt,acos:py,acosh:fy,add:X,addN:Y$,all:Zd,any:Dc,argMax:Qs,argMin:my,asin:gy,asinh:yy,atan:by,atan2:xy,atanh:wy,avgPool:_c,avgPool3d:Iy,basicLSTMCell:xN,batchNorm:wi,batchNorm2d:vy,batchNorm3d:Sy,batchNorm4d:$y,batchToSpaceND:Fc,bincount:Ny,bitwiseAnd:TN,booleanMaskAsync:c2,broadcastArgs:AN,broadcastTo:Da,buffer:Se,cast:re,ceil:Cy,clipByValue:hn,clone:Ns,complex:Ys,concat:pt,concat1d:ky,concat2d:Ty,concat3d:Ey,concat4d:Ay,conv1d:ep,conv2d:nr,conv2dTranspose:np,conv3d:Ry,conv3dTranspose:_y,cos:Oc,cosh:sp,cosineWindow:_p,cumprod:Lc,cumsum:rp,denseBincount:ap,depthToSpace:Fy,depthwiseConv2d:Ii,diag:QN,dilation2d:Oy,div:
|
|
9272
|
+
*/var Ot=Object.freeze({__proto__:null,OP_SCOPE_SUFFIX:Xg,abs:yt,acos:py,acosh:fy,add:X,addN:Y$,all:Zd,any:Dc,argMax:Qs,argMin:my,asin:gy,asinh:yy,atan:by,atan2:xy,atanh:wy,avgPool:_c,avgPool3d:Iy,basicLSTMCell:xN,batchNorm:wi,batchNorm2d:vy,batchNorm3d:Sy,batchNorm4d:$y,batchToSpaceND:Fc,bincount:Ny,bitwiseAnd:TN,booleanMaskAsync:c2,broadcastArgs:AN,broadcastTo:Da,buffer:Se,cast:re,ceil:Cy,clipByValue:hn,clone:Ns,complex:Ys,concat:pt,concat1d:ky,concat2d:Ty,concat3d:Ey,concat4d:Ay,conv1d:ep,conv2d:nr,conv2dTranspose:np,conv3d:Ry,conv3dTranspose:_y,cos:Oc,cosh:sp,cosineWindow:_p,cumprod:Lc,cumsum:rp,denseBincount:ap,depthToSpace:Fy,depthwiseConv2d:Ii,diag:QN,dilation2d:Oy,div:fe,divNoNan:Ly,dot:Py,dropout:hb,einsum:Oa,elu:vi,enclosingPowerOfTwo:db,ensureShape:cC,equal:Jn,erf:zy,euclideanNorm:Vy,exp:dn,expandDims:Kt,expm1:Wy,eye:ip,fft:Xc,fill:_a,floor:Ni,floorDiv:Yd,fused:R2,gather:Ts,gatherND:x2,greater:pn,greaterEqual:ar,ifft:Ei,imag:Pc,image:Pn,inTopKAsync:S2,irfft:kp,isFinite:Uy,isInf:Gy,isNaN:Hy,leakyRelu:zc,less:Ci,lessEqual:Mr,linalg:vb,linspace:OC,localResponseNormalization:jy,log:$n,log1p:Mc,logSigmoid:qy,logSoftmax:lp,logSumExp:Bc,logicalAnd:ds,logicalNot:Vc,logicalOr:cp,logicalXor:Ky,losses:lA,lowerBound:ZC,matMul:Te,max:Sn,maxPool:Uc,maxPool3d:Xy,maxPoolWithArgmax:tk,maximum:As,mean:tt,meshgrid:rk,min:Si,minimum:Br,mirrorPad:Yy,mod:Zy,moments:Pa,movingAverage:d2,mul:D,multiRNNCell:uk,multinomial:dk,neg:Xe,norm:$i,notEqual:za,oneHot:Vr,ones:fn,onesLike:Nn,op:E,outerProduct:yk,pad:or,pad1d:wk,pad2d:vk,pad3d:$k,pad4d:Ck,pool:Jy,pow:rr,prelu:Hc,print:hy,prod:Qy,raggedGather:Fk,raggedRange:Lk,raggedTensorToTensor:zk,rand:Bk,randomGamma:oT,randomNormal:bp,randomStandardNormal:cT,randomUniform:Ur,randomUniformInt:dT,range:Ba,real:ki,reciprocal:tb,relu:ps,relu6:xp,reshape:_,reverse:Ln,reverse1d:xT,reverse2d:IT,reverse3d:ST,reverse4d:NT,rfft:Yc,round:wp,rsqrt:Ip,scalar:pe,scatterND:f2,searchSorted:up,selu:vp,separableConv2d:Sp,setdiff1dAsync:RT,sigmoid:hs,sign:nb,signal:iA,sin:$p,sinh:Np,slice:Ae,slice1d:jc,slice2d:Cp,slice3d:qc,slice4d:Ti,softmax:Kc,softplus:La,spaceToBatchND:Gc,sparse:cA,sparseToDense:y2,spectral:oA,split:mn,sqrt:_t,square:Be,squaredDifference:Tp,squeeze:Rs,stack:Wt,step:Va,stridedSlice:sb,string:uA,sub:ie,sum:ue,tan:rb,tanh:Ra,tensor:Bt,tensor1d:ft,tensor2d:Wa,tensor3d:Ua,tensor4d:ab,tensor5d:ZT,tensor6d:JT,tensorScatterUpdate:e2,tile:On,topk:ib,transpose:De,truncatedNormal:Ap,unique:lb,unsortedSegmentSum:Rp,unstack:Cn,upperBound:o2,variable:cb,where:Vt,whereAsync:ub,zeros:ct,zerosLike:Re});/**
|
|
9273
9273
|
* @license
|
|
9274
9274
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
9275
9275
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -9329,7 +9329,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
9329
9329
|
* See the License for the specific language governing permissions and
|
|
9330
9330
|
* limitations under the License.
|
|
9331
9331
|
* =============================================================================
|
|
9332
|
-
*/class fO{constructor(e,t,s,r,a,o,i){this.name=e,this.dtype=t,this.maxSize=s,this.elementShape=r,this.identicalElementShapes=a,this.dynamicSize=o,this.clearAfterRead=i,this.tensors=[],this.closed_=!1,this.idTensor=
|
|
9332
|
+
*/class fO{constructor(e,t,s,r,a,o,i){this.name=e,this.dtype=t,this.maxSize=s,this.elementShape=r,this.identicalElementShapes=a,this.dynamicSize=o,this.clearAfterRead=i,this.tensors=[],this.closed_=!1,this.idTensor=pe(0),Nt(this.idTensor)}get id(){return this.idTensor.id}get closed(){return this.closed_}clearAndClose(e){this.tensors.forEach(t=>{(e==null||!e.has(t.tensor.id))&&t.tensor.dispose()}),this.tensors=[],this.closed_=!0,this.idTensor.dispose()}size(){return this.tensors.length}read(e){if(this.closed_)throw new Error(`TensorArray ${this.name} has already been closed.`);if(e<0||e>=this.size())throw new Error(`Tried to read from index ${e}, but array size is: ${this.size()}`);const t=this.tensors[e];if(t.cleared)throw new Error(`TensorArray ${this.name}: Could not read index ${e} twice because it was cleared after a previous read (perhaps try setting clear_after_read = false?).`);return this.clearAfterRead&&(t.cleared=!0),t.read=!0,t.tensor}readMany(e){return e.map(t=>this.read(t))}write(e,t){if(this.closed_)throw new Error(`TensorArray ${this.name} has already been closed.`);if(e<0||!this.dynamicSize&&e>=this.maxSize)throw new Error(`Tried to write to index ${e}, but array is not resizeable and size is: ${this.maxSize}`);const s=this.tensors[e]||{};if(t.dtype!==this.dtype)throw new Error(`TensorArray ${this.name}: Could not write to TensorArray index ${e},
|
|
9333
9333
|
because the value dtype is ${t.dtype}, but TensorArray dtype is ${this.dtype}.`);if(this.size()===0&&(this.elementShape==null||this.elementShape.length===0)&&(this.elementShape=t.shape),Gn(this.elementShape,t.shape,`TensorArray ${this.name}: Could not write to TensorArray index ${e}.`),s.read)throw new Error(`TensorArray ${this.name}: Could not write to TensorArray index ${e}, because it has already been read.`);if(s.written)throw new Error(`TensorArray ${this.name}: Could not write to TensorArray index ${e}, because it has already been written.`);s.tensor=t,Nt(t),s.written=!0,this.tensors[e]=s}writeMany(e,t){if(e.length!==t.length)throw new Error(`TensorArray ${this.name}: could not write multiple tensors,because the index size: ${e.length} is not the same as tensors size: ${t.length}.`);e.forEach((s,r)=>this.write(s,t[r]))}gather(e,t){if(t&&t!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but gather requested dtype ${t}`);if(e)e=e.slice(0,this.size());else{e=[];for(let r=0;r<this.size();r++)e.push(r)}if(e.length===0)return Bt([],[0].concat(this.elementShape));const s=this.readMany(e);return Gn(this.elementShape,s[0].shape,"TensorArray shape mismatch: "),Wt(s,0)}concat(e){if(e&&e!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but concat requested dtype ${e}`);if(this.size()===0)return Bt([],[0].concat(this.elementShape));const t=[];for(let r=0;r<this.size();r++)t.push(r);const s=this.readMany(t);return Gn(this.elementShape,s[0].shape,`TensorArray shape mismatch: tensor array shape (${this.elementShape}) vs first tensor shape (${s[0].shape})`),pt(s,0)}scatter(e,t){if(t.dtype!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but tensor has dtype ${t.dtype}`);if(e.length!==t.shape[0])throw new Error(`Expected len(indices) == tensor.shape[0], but saw: ${e.length} vs. ${t.shape[0]}`);const s=Math.max(...e);if(!this.dynamicSize&&s>=this.maxSize)throw new Error(`Max index must be < array size (${s} vs. ${this.maxSize})`);this.writeMany(e,Cn(t,0))}split(e,t){if(t.dtype!==this.dtype)throw new Error(`TensorArray dtype is ${this.dtype} but tensor has dtype ${t.dtype}`);let s=0;const r=e.map(l=>(s+=l,s));if(s!==t.shape[0])throw new Error(`Expected sum of lengths to be equal to
|
|
9334
9334
|
tensor.shape[0], but sum of lengths is
|
|
9335
9335
|
${s}, and tensor's shape is: ${t.shape}`);if(!this.dynamicSize&&e.length!==this.maxSize)throw new Error(`TensorArray's size is not equal to the size of lengths (${this.maxSize} vs. ${e.length}), and the TensorArray is not marked as dynamically resizeable`);const a=s===0?0:t.size/s,o=[];P(()=>{t=_(t,[1,s,a]);for(let l=0;l<e.length;++l){const u=[0,l===0?0:r[l-1],0],h=[1,e[l],a];o[l]=_(Ae(t,u,h),this.elementShape)}return o});const i=[];for(let l=0;l<e.length;l++)i[l]=l;this.writeMany(i,o)}}/**
|
|
@@ -9347,7 +9347,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
9347
9347
|
* See the License for the specific language governing permissions and
|
|
9348
9348
|
* limitations under the License.
|
|
9349
9349
|
* =============================================================================
|
|
9350
|
-
*/class na{get id(){return this.idTensor.id}constructor(e,t,s,r=-1){this.tensors=e,this.elementShape=t,this.elementDtype=s,e!=null&&e.forEach(a=>{if(s!==a.dtype)throw new Error(`Invalid data types; op elements ${s}, but list elements ${a.dtype}`);Gn(t,a.shape,"TensorList shape mismatch: "),Nt(a)}),this.idTensor=
|
|
9350
|
+
*/class na{get id(){return this.idTensor.id}constructor(e,t,s,r=-1){this.tensors=e,this.elementShape=t,this.elementDtype=s,e!=null&&e.forEach(a=>{if(s!==a.dtype)throw new Error(`Invalid data types; op elements ${s}, but list elements ${a.dtype}`);Gn(t,a.shape,"TensorList shape mismatch: "),Nt(a)}),this.idTensor=pe(0),this.maxNumElements=r,Nt(this.idTensor)}copy(){return new na([...this.tensors],this.elementShape,this.elementDtype)}clearAndClose(e){this.tensors.forEach(t=>{(e==null||!e.has(t.id))&&t.dispose()}),this.tensors.length=0,this.idTensor.dispose()}size(){return this.tensors.length}stack(e,t,s=-1){if(t!==this.elementDtype)throw new Error(`Invalid data types; op elements ${t}, but list elements ${this.elementDtype}`);if(s!==-1&&this.tensors.length!==s)throw new Error(`Operation expected a list with ${s} elements but got a list with ${this.tensors.length} elements.`);Gn(e,this.elementShape,"TensorList shape mismatch: ");const r=Zi(this.elementShape,this.tensors,e);return P(()=>{const a=this.tensors.map(o=>_(o,r));return Wt(a,0)})}popBack(e,t){if(t!==this.elementDtype)throw new Error(`Invalid data types; op elements ${t}, but list elements ${this.elementDtype}`);if(this.size()===0)throw new Error("Trying to pop from an empty list.");const s=Zi(this.elementShape,this.tensors,e),r=this.tensors.pop();return r.kept=!1,Gn(r.shape,e,"TensorList shape mismatch: "),_(r,s)}pushBack(e){if(e.dtype!==this.elementDtype)throw new Error(`Invalid data types; op elements ${e.dtype}, but list elements ${this.elementDtype}`);if(Gn(e.shape,this.elementShape,"TensorList shape mismatch: "),this.maxNumElements===this.size())throw new Error("Trying to push element into a full list.");Nt(e),this.tensors.push(e)}resize(e){if(e<0)throw new Error(`TensorListResize expects size to be non-negative. Got: ${e}`);if(this.maxNumElements!==-1&&e>this.maxNumElements)throw new Error(`TensorListResize input size ${e} is greater maxNumElement ${this.maxNumElements}.`);const t=new na([],this.elementShape,this.elementDtype,this.maxNumElements);t.tensors.length=e;for(let s=0;s<Math.min(this.tensors.length,e);++s)t.tensors[s]=this.tensors[s];return t}getItem(e,t,s){if(s!==this.elementDtype)throw new Error(`Invalid data types; op elements ${s}, but list elements ${this.elementDtype}`);if(e<0||e>this.tensors.length)throw new Error(`Trying to access element ${e} in a list with ${this.tensors.length} elements.`);if(this.tensors[e]==null)throw new Error(`element at index ${e} is null.`);Gn(this.tensors[e].shape,t,"TensorList shape mismatch: ");const r=Zi(this.elementShape,this.tensors,t);return _(this.tensors[e],r)}setItem(e,t){if(t.dtype!==this.elementDtype)throw new Error(`Invalid data types; op elements ${t.dtype}, but list elements ${this.elementDtype}`);if(e<0||this.maxNumElements!==-1&&e>=this.maxNumElements)throw new Error(`Trying to set element ${e} in a list with max ${this.maxNumElements} elements.`);Gn(this.elementShape,t.shape,"TensorList shape mismatch: "),Nt(t),this.tensors[e]!=null&&(this.tensors[e].kept=!1),this.tensors[e]=t}gather(e,t,s){if(t!==this.elementDtype)throw new Error(`Invalid data types; op elements ${t}, but list elements ${this.elementDtype}`);Gn(this.elementShape,s,"TensorList shape mismatch: "),e=e.slice(0,this.size());const r=Zi(this.elementShape,this.tensors,s);return e.length===0?Bt([],[0].concat(r)):P(()=>{const a=e.map(o=>_(this.tensors[o],r));return Wt(a,0)})}concat(e,t){if(e&&e!==this.elementDtype)throw new Error(`TensorList dtype is ${this.elementDtype} but concat requested dtype ${e}`);Gn(this.elementShape,t,"TensorList shape mismatch: ");const s=Zi(this.elementShape,this.tensors,t);return this.size()===0?Bt([],[0].concat(s)):P(()=>{const r=this.tensors.map(a=>_(a,s));return pt(r,0)})}}function mO(n,e,t){const s=n.dtype;if(n.shape.length<1)throw new Error(`Tensor must be at least a vector, but saw shape: ${n.shape}`);if(n.dtype!==t)throw new Error(`Invalid data types; op elements ${n.dtype}, but list elements ${t}`);const r=n.shape.slice(1);Gn(r,e,"TensorList shape mismatch: ");const a=Cn(n);return new na(a,e,s)}function gO(n,e,t,s){return new na([],n,e,s)}function yO(n,e,t,s){if(e.length!==n.shape[0])throw new Error(`Expected len(indices) == tensor.shape[0], but saw: ${e.length} vs. ${n.shape[0]}`);const r=Math.max(...e);if(s!=null&&s!==-1&&r>=s)throw new Error(`Max index must be < array size (${r} vs. ${s})`);const a=new na([],t,n.dtype,s),o=Cn(n,0);return e.forEach((i,l)=>{a.setItem(i,o[l])}),a}function bO(n,e,t){let s=0;const r=e.map(u=>(s+=u,s));if(s!==n.shape[0])throw new Error(`Expected sum of lengths to be equal to
|
|
9351
9351
|
tensor.shape[0], but sum of lengths is
|
|
9352
9352
|
${s}, and tensor's shape is: ${n.shape}`);const a=n.shape.slice(1),o=bm(a,t),i=s===0?0:n.size/s,l=P(()=>{const u=[];n=_(n,[1,s,i]);for(let h=0;h<e.length;++h){const p=[0,h===0?0:r[h-1],0],f=[1,e[h],i];u[h]=_(Ae(n,p,f),o)}return n.dispose(),u}),c=new na([],t,n.dtype,e.length);for(let u=0;u<l.length;u++)c.setItem(u,l[u]);return c}/**
|
|
9353
9353
|
* @license
|
|
@@ -9364,7 +9364,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
9364
9364
|
* See the License for the specific language governing permissions and
|
|
9365
9365
|
* limitations under the License.
|
|
9366
9366
|
* =============================================================================
|
|
9367
|
-
*/const xO=(n,e,t)=>O(this,null,function*(){switch(n.op){case"If":case"StatelessIf":{const s=I("thenBranch",n,e,t),r=I("elseBranch",n,e,t),a=I("cond",n,e,t),o=I("args",n,e,t);return(yield a.data())[0]?t.functionMap[s].executeFunctionAsync(o,t.tensorArrayMap,t.tensorListMap):t.functionMap[r].executeFunctionAsync(o,t.tensorArrayMap,t.tensorListMap)}case"While":case"StatelessWhile":{const s=I("body",n,e,t),r=I("cond",n,e,t),a=I("args",n,e,t),o=yield t.functionMap[r].executeFunctionAsync(a,t.tensorArrayMap,t.tensorListMap),i=a.map(u=>u.id);let l=yield o[0].data();o.forEach(u=>{!u.kept&&i.indexOf(u.id)===-1&&u.dispose()});let c=a;for(;l[0];){const u=c;c=yield t.functionMap[s].executeFunctionAsync(c,t.tensorArrayMap,t.tensorListMap);const h=c.map(p=>p.id);u.forEach(p=>{!p.kept&&i.indexOf(p.id)===-1&&h.indexOf(p.id)===-1&&p.dispose()});const d=yield t.functionMap[r].executeFunctionAsync(c,t.tensorArrayMap,t.tensorListMap);l=yield d[0].data(),d.forEach(p=>{!p.kept&&i.indexOf(p.id)===-1&&h.indexOf(p.id)===-1&&p.dispose()})}return c}case"LoopCond":{const s=I("pred",n,e,t);return[Ps(s)]}case"Switch":{const s=I("pred",n,e,t);let r=I("data",n,e,t);return r.kept||(r=Ps(r)),(yield s.data())[0]?[void 0,r]:[r,void 0]}case"Merge":{const s=n.inputNames.find(r=>Ft(r,e,t)!==void 0);if(s){const r=Ft(s,e,t);return[Ps(r)]}return}case"Enter":{const s=I("frameName",n,e,t),r=I("tensor",n,e,t);return t.enterFrame(s),[Ps(r)]}case"Exit":{const s=I("tensor",n,e,t);return t.exitFrame(),[Ps(s)]}case"NextIteration":{const s=I("tensor",n,e,t);return t.nextIteration(),[Ps(s)]}case"TensorArrayV3":{const s=I("size",n,e,t),r=I("dtype",n,e,t),a=I("elementShape",n,e,t),o=I("dynamicSize",n,e,t),i=I("clearAfterRead",n,e,t),l=I("identicalElementShapes",n,e,t),c=I("name",n,e,t),u=new fO(c,r,s,a,l,o,i);return t.addTensorArray(u),[u.idTensor,
|
|
9367
|
+
*/const xO=(n,e,t)=>O(this,null,function*(){switch(n.op){case"If":case"StatelessIf":{const s=I("thenBranch",n,e,t),r=I("elseBranch",n,e,t),a=I("cond",n,e,t),o=I("args",n,e,t);return(yield a.data())[0]?t.functionMap[s].executeFunctionAsync(o,t.tensorArrayMap,t.tensorListMap):t.functionMap[r].executeFunctionAsync(o,t.tensorArrayMap,t.tensorListMap)}case"While":case"StatelessWhile":{const s=I("body",n,e,t),r=I("cond",n,e,t),a=I("args",n,e,t),o=yield t.functionMap[r].executeFunctionAsync(a,t.tensorArrayMap,t.tensorListMap),i=a.map(u=>u.id);let l=yield o[0].data();o.forEach(u=>{!u.kept&&i.indexOf(u.id)===-1&&u.dispose()});let c=a;for(;l[0];){const u=c;c=yield t.functionMap[s].executeFunctionAsync(c,t.tensorArrayMap,t.tensorListMap);const h=c.map(p=>p.id);u.forEach(p=>{!p.kept&&i.indexOf(p.id)===-1&&h.indexOf(p.id)===-1&&p.dispose()});const d=yield t.functionMap[r].executeFunctionAsync(c,t.tensorArrayMap,t.tensorListMap);l=yield d[0].data(),d.forEach(p=>{!p.kept&&i.indexOf(p.id)===-1&&h.indexOf(p.id)===-1&&p.dispose()})}return c}case"LoopCond":{const s=I("pred",n,e,t);return[Ps(s)]}case"Switch":{const s=I("pred",n,e,t);let r=I("data",n,e,t);return r.kept||(r=Ps(r)),(yield s.data())[0]?[void 0,r]:[r,void 0]}case"Merge":{const s=n.inputNames.find(r=>Ft(r,e,t)!==void 0);if(s){const r=Ft(s,e,t);return[Ps(r)]}return}case"Enter":{const s=I("frameName",n,e,t),r=I("tensor",n,e,t);return t.enterFrame(s),[Ps(r)]}case"Exit":{const s=I("tensor",n,e,t);return t.exitFrame(),[Ps(s)]}case"NextIteration":{const s=I("tensor",n,e,t);return t.nextIteration(),[Ps(s)]}case"TensorArrayV3":{const s=I("size",n,e,t),r=I("dtype",n,e,t),a=I("elementShape",n,e,t),o=I("dynamicSize",n,e,t),i=I("clearAfterRead",n,e,t),l=I("identicalElementShapes",n,e,t),c=I("name",n,e,t),u=new fO(c,r,s,a,l,o,i);return t.addTensorArray(u),[u.idTensor,pe(1)]}case"TensorArrayWriteV3":{const s=I("tensorArrayId",n,e,t),r=I("index",n,e,t),a=I("tensor",n,e,t),o=t.getTensorArray(s.id);return o.write(r,a),[o.idTensor]}case"TensorArrayReadV3":{const s=I("tensorArrayId",n,e,t),r=I("index",n,e,t);return[t.getTensorArray(s.id).read(r)]}case"TensorArrayGatherV3":{const s=I("tensorArrayId",n,e,t),r=I("indices",n,e,t),a=I("dtype",n,e,t);return[t.getTensorArray(s.id).gather(r,a)]}case"TensorArrayScatterV3":{const s=I("tensorArrayId",n,e,t),r=I("indices",n,e,t),a=I("tensor",n,e,t),o=t.getTensorArray(s.id);return o.scatter(r,a),[o.idTensor]}case"TensorArrayConcatV3":{const s=I("tensorArrayId",n,e,t),r=t.getTensorArray(s.id),a=I("dtype",n,e,t);return[r.concat(a)]}case"TensorArraySplitV3":{const s=I("tensorArrayId",n,e,t),r=I("tensor",n,e,t),a=I("lengths",n,e,t),o=t.getTensorArray(s.id);return o.split(a,r),[o.idTensor]}case"TensorArraySizeV3":{const s=I("tensorArrayId",n,e,t),r=t.getTensorArray(s.id);return[pe(r.size(),"int32")]}case"TensorArrayCloseV3":{const s=I("tensorArrayId",n,e,t),r=t.getTensorArray(s.id);return r.clearAndClose(),[r.idTensor]}case"TensorListSetItem":{const s=I("tensorListId",n,e,t),r=I("index",n,e,t),a=I("tensor",n,e,t),o=t.getTensorList(s.id);return o.setItem(r,a),[o.idTensor]}case"TensorListGetItem":{const s=I("tensorListId",n,e,t),r=I("index",n,e,t),a=I("elementShape",n,e,t),o=I("elementDType",n,e,t);return[t.getTensorList(s.id).getItem(r,a,o)]}case"TensorListScatterV2":case"TensorListScatter":{const s=I("indices",n,e,t),r=I("tensor",n,e,t),a=I("elementShape",n,e,t),o=I("numElements",n,e,t),i=yO(r,s,a,o);return t.addTensorList(i),[i.idTensor]}case"TensorListReserve":case"EmptyTensorList":{const s=I("elementShape",n,e,t),r=I("elementDType",n,e,t);let a;n.op==="TensorListReserve"?a="numElements":a="maxNumElements";const o=I(a,n,e,t),i=n.op==="TensorListReserve"?-1:o,l=gO(s,r,o,i);return t.addTensorList(l),[l.idTensor]}case"TensorListGather":{const s=I("tensorListId",n,e,t),r=I("indices",n,e,t),a=I("elementShape",n,e,t),o=I("elementDType",n,e,t);return[t.getTensorList(s.id).gather(r,o,a)]}case"TensorListStack":{const s=I("tensorListId",n,e,t),r=I("elementShape",n,e,t),a=I("elementDType",n,e,t),o=I("numElements",n,e,t);return[t.getTensorList(s.id).stack(r,a,o)]}case"TensorListFromTensor":{const s=I("tensor",n,e,t),r=I("elementShape",n,e,t),a=I("elementDType",n,e,t),o=mO(s,r,a);return t.addTensorList(o),[o.idTensor]}case"TensorListConcat":case"TensorListConcatV2":{const s=I("tensorListId",n,e,t),r=t.getTensorList(s.id),a=I("dtype",n,e,t),o=I("elementShape",n,e,t);return[r.concat(a,o)]}case"TensorListPushBack":{const s=I("tensorListId",n,e,t),r=I("tensor",n,e,t),a=t.getTensorList(s.id);return a.pushBack(r),[a.idTensor]}case"TensorListPopBack":{const s=I("tensorListId",n,e,t),r=I("elementShape",n,e,t),a=I("elementDType",n,e,t);return[t.getTensorList(s.id).popBack(r,a)]}case"TensorListSplit":{const s=I("tensor",n,e,t),r=I("elementShape",n,e,t),a=I("lengths",n,e,t),o=bO(s,a,r);return t.addTensorList(o),[o.idTensor]}case"TensorListLength":{const s=I("tensorListId",n,e,t),r=t.getTensorList(s.id);return[pe(r.size(),"int32")]}case"TensorListResize":{const s=I("tensorListId",n,e,t),r=I("size",n,e,t),o=t.getTensorList(s.id).resize(r);return t.addTensorList(o),[o.idTensor]}default:throw TypeError(`Node type ${n.op} is not implemented`)}});/**
|
|
9368
9368
|
* @license
|
|
9369
9369
|
* Copyright 2018 Google LLC. All Rights Reserved.
|
|
9370
9370
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -9454,7 +9454,7 @@ Manifest JSON has weights with names: ${i.join(", ")}.`)}const l=r.reduce((p,f,m
|
|
|
9454
9454
|
* See the License for the specific language governing permissions and
|
|
9455
9455
|
* limitations under the License.
|
|
9456
9456
|
* =============================================================================
|
|
9457
|
-
*/class NO{get id(){return this.handle.id}constructor(e,t){this.keyDType=e,this.valueDType=t,this.handle=
|
|
9457
|
+
*/class NO{get id(){return this.handle.id}constructor(e,t){this.keyDType=e,this.valueDType=t,this.handle=pe(0),this.tensorMap=new Map,Nt(this.handle)}clearAndClose(){this.tensorMap.forEach(e=>e.dispose()),this.tensorMap.clear(),this.handle.dispose()}size(){return this.tensorMap.size}tensorSize(){return pe(this.size(),"int32")}import(e,t){return O(this,null,function*(){this.checkKeyAndValueTensor(e,t);const s=yield e.data();return this.tensorMap.forEach(r=>r.dispose()),this.tensorMap.clear(),P(()=>{const r=Cn(t),a=s.length,o=r.length;S(a===o,()=>`The number of elements doesn't match, keys has ${a} elements, the values has ${o} elements.`);for(let i=0;i<a;i++){const l=s[i],c=r[i];Nt(c),this.tensorMap.set(l,c)}return this.handle})})}find(e,t){return O(this,null,function*(){this.checkKeyAndValueTensor(e,t);const s=yield e.data();return P(()=>{const r=[];for(let a=0;a<s.length;a++){const o=s[a],i=this.findWithDefault(o,t);r.push(i)}return Wt(r)})})}findWithDefault(e,t){const s=this.tensorMap.get(e);return s!=null?s:t}checkKeyAndValueTensor(e,t){if(e.dtype!==this.keyDType)throw new Error(`Expect key dtype ${this.keyDType}, but got ${e.dtype}`);if(t.dtype!==this.valueDType)throw new Error(`Expect value dtype ${this.valueDType}, but got ${t.dtype}`)}}/**
|
|
9458
9458
|
* @license
|
|
9459
9459
|
* Copyright 2020 Google LLC. All Rights Reserved.
|
|
9460
9460
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -14493,7 +14493,7 @@ vec2 packedUVfrom3D(int texNumR, int texNumC,
|
|
|
14493
14493
|
* See the License for the specific language governing permissions and
|
|
14494
14494
|
* limitations under the License.
|
|
14495
14495
|
* =============================================================================
|
|
14496
|
-
*/const KU=Dp,XU=1e-7,YU=1e-4,Zu={};function ZU(n){return n in Zu||(Zu[n]={}),Zu[n]}const JU=M().getNumber("CPU_HANDOFF_SIZE_THRESHOLD"),QU=600;function eG(){return M().global.screen==null?1024:M().global.screen.height*M().global.screen.width*window.devicePixelRatio*QU/1024/1024}class Ju extends ga{nextDataId(){return Ju.nextDataId++}constructor(e){if(super(),this.pendingRead=new WeakMap,this.pendingDisposal=new WeakSet,this.dataRefCount=new WeakMap,this.numBytesInGPU=0,this.uploadWaitMs=0,this.downloadWaitMs=0,this.lastGlFlushTime=0,this.warnedAboutMemory=!1,this.pendingDeletes=0,this.disposed=!1,!M().getBool("HAS_WEBGL"))throw new Error("WebGL is not supported on this device");let t;if(e!=null){if(e instanceof Vm)t=e;else{const s=is(M().getNumber("WEBGL_VERSION"),e);t=new Vm(s)}this.binaryCache={},this.gpgpuCreatedLocally=!1}else{const s=is(M().getNumber("WEBGL_VERSION"));t=new Vm(s),this.binaryCache=ZU(M().getNumber("WEBGL_VERSION")),this.gpgpuCreatedLocally=!0}this.gpgpu=t,this.canvas=this.gpgpu.gl.canvas,this.textureManager=new _U(this.gpgpu),this.numMBBeforeWarning=eG(),this.texData=new ml(this,us())}numDataIds(){return this.texData.numDataIds()-this.pendingDeletes}writeTexture(e,t,s,r,a,o){const i=this.makeTensorInfo(t,s),l=this.texData.get(i.dataId);l.isPacked=!1,l.texture={texture:e,texShape:[r,a]},l.texShape=[r,a];const c=qu(t),u=new tv(c,!1,o),h=this.runWebGLProgram(u,[i],s,[[r,a]]);return h.shape=t,l.texture=null,this.disposeIntermediateTensorInfo(i),h.dataId}write(e,t,s){if((M().getBool("WEBGL_CHECK_NUMERICAL_PROBLEMS")||M().getBool("DEBUG"))&&this.checkNumericalProblems(e),s==="complex64"&&e!=null)throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");const r={id:this.nextDataId()};return this.texData.set(r,{shape:t,dtype:s,values:e,usage:Tn.UPLOAD,refCount:1}),r}refCount(e){return this.texData.has(e)?this.texData.get(e).refCount:0}incRef(e){const t=this.texData.get(e);t.refCount++}decRef(e){if(this.texData.has(e)){const t=this.texData.get(e);t.refCount--}}move(e,t,s,r,a){if(M().getBool("DEBUG")&&this.checkNumericalProblems(t),r==="complex64")throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");this.texData.set(e,{shape:s,dtype:r,values:t,usage:Tn.UPLOAD,refCount:a})}disposeIntermediateTensorInfo(e){this.disposeData(e.dataId)}readSync(e){const t=this.texData.get(e),{values:s,dtype:r,complexTensorInfos:a,slice:o,shape:i,isPacked:l}=t;if(o!=null){let d;l?d=new wr(i,xr):d=new Ss(i,xr);const p=this.runWebGLProgram(d,[{dataId:e,shape:i,dtype:r}],r),f=this.readSync(p.dataId);return this.disposeIntermediateTensorInfo(p),f}if(s!=null)return this.convertAndCacheOnCPU(e);if(r==="string")return s;const c=this.activeTimers!=null;let u;c&&(u=dt());let h;if(r==="complex64"){const d=this.readSync(a.real.dataId),p=this.readSync(a.imag.dataId);h=_s(d,p)}else h=this.getValuesFromTexture(e);return c&&(this.downloadWaitMs+=dt()-u),this.convertAndCacheOnCPU(e,h)}read(e){return O(this,null,function*(){if(this.pendingRead.has(e)){const f=this.pendingRead.get(e);return new Promise(m=>f.push(m))}const t=this.texData.get(e),{values:s,shape:r,slice:a,dtype:o,complexTensorInfos:i,isPacked:l}=t;if(a!=null){let f;l?f=new wr(r,xr):f=new Ss(r,xr);const m=this.runWebGLProgram(f,[{dataId:e,shape:r,dtype:o}],o),g=this.read(m.dataId);return this.disposeIntermediateTensorInfo(m),g}if(s!=null)return this.convertAndCacheOnCPU(e);if(M().getBool("DEBUG")&&!M().getBool("WEBGL_DOWNLOAD_FLOAT_ENABLED")&&M().getNumber("WEBGL_VERSION")===2)throw new Error("tensor.data() with WEBGL_DOWNLOAD_FLOAT_ENABLED=false and WEBGL_VERSION=2 not yet supported.");let c=null,u;if(o!=="complex64"&&M().get("WEBGL_BUFFER_SUPPORTED")){u=this.decode(e);const f=this.texData.get(u.dataId);c=this.gpgpu.createBufferFromTexture(f.texture.texture,...Gu(r))}this.pendingRead.set(e,[]),o!=="complex64"&&(yield this.gpgpu.createAndWaitForFence());let h;if(o==="complex64"){const f=yield Promise.all([this.read(i.real.dataId),this.read(i.imag.dataId)]),m=f[0],g=f[1];h=_s(m,g)}else if(c==null)h=this.getValuesFromTexture(e);else{const f=U(r);h=this.gpgpu.downloadFloat32MatrixFromBuffer(c,f)}if(u!=null&&this.disposeIntermediateTensorInfo(u),c!=null){const f=this.gpgpu.gl;oe(f,()=>f.deleteBuffer(c))}const d=this.convertAndCacheOnCPU(e,h),p=this.pendingRead.get(e);return this.pendingRead.delete(e),p.forEach(f=>f(d)),this.pendingDisposal.has(e)&&(this.pendingDisposal.delete(e),this.disposeData(e)&&us().removeDataId(e,this),this.pendingDeletes--),d})}readToGPU(e,t={}){const s=this.texData.get(e),{values:r,shape:a,slice:o,dtype:i,isPacked:l,texture:c}=s;if(i==="complex64")throw new Error("Does not support reading texture for complex64 dtype.");if(o!=null){let p;l?p=new wr(a,xr):p=new Ss(a,xr);const f=this.runWebGLProgram(p,[{dataId:e,shape:a,dtype:i}],i),m=this.readToGPU(f,t);return this.disposeIntermediateTensorInfo(f),m}if(c==null)throw r!=null?new Error("Data is not on GPU but on CPU."):new Error("There is no data on GPU or CPU.");const u=this.decode(e,t.customTexShape),h=us().makeTensorFromTensorInfo(u),d=this.texData.get(u.dataId);return Object.assign({tensorRef:h},d.texture)}bufferSync(e){const t=this.readSync(e.dataId);if(e.dtype==="string")try{const s=t.map(r=>qs(r));return Se(e.shape,e.dtype,s)}catch(s){throw new Error("Failed to decode encoded string bytes into utf-8")}return Se(e.shape,e.dtype,t)}checkNumericalProblems(e){if(e!=null)for(let t=0;t<e.length;t++){const s=e[t];if(!sW(s))throw M().getBool("WEBGL_RENDER_FLOAT32_CAPABLE")?Error(`The value ${s} cannot be represented with your current settings. Consider enabling float32 rendering: 'tf.env().set('WEBGL_RENDER_FLOAT32_ENABLED', true);'`):Error(`The value ${s} cannot be represented on this device.`)}}getValuesFromTexture(e){const{shape:t,dtype:s,isPacked:r}=this.texData.get(e),a=U(t);if(M().getBool("WEBGL_DOWNLOAD_FLOAT_ENABLED")){const d=this.decode(e),p=this.texData.get(d.dataId),f=this.gpgpu.downloadMatrixFromPackedTexture(p.texture.texture,...Gu(t)).subarray(0,a);return this.disposeIntermediateTensorInfo(d),f}const o=M().getBool("WEBGL_PACK")&&r===!0,i=o?qu(t):t,l=o?new x4(i):new b4(i),c=this.runWebGLProgram(l,[{shape:i,dtype:s,dataId:e}],"float32"),u=this.texData.get(c.dataId),h=this.gpgpu.downloadByteEncodedFloatMatrixFromOutputTexture(u.texture.texture,u.texShape[0],u.texShape[1]).subarray(0,a);return this.disposeIntermediateTensorInfo(c),h}timerAvailable(){return M().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0}time(e){const t=this.activeTimers,s=[];let r=!1;this.programTimersStack==null?(this.programTimersStack=s,r=!0):this.activeTimers.push(s),this.activeTimers=s,e();const a=Fr(this.activeTimers.map(l=>l.query)).filter(l=>l!=null),o=Fr(this.activeTimers.map(l=>l.name)).filter(l=>l!=null);this.activeTimers=t,r&&(this.programTimersStack=null);const i={uploadWaitMs:this.uploadWaitMs,downloadWaitMs:this.downloadWaitMs,kernelMs:null,wallMs:null};return(()=>O(this,null,function*(){if(M().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0){const l=yield Promise.all(a);i.kernelMs=vS(l),i.getExtraProfileInfo=()=>l.map((c,u)=>({name:o[u],ms:c})).map(c=>`${c.name}: ${c.ms}`).join(", ")}else i.kernelMs={error:"WebGL query timers are not supported in this environment."};return this.uploadWaitMs=0,this.downloadWaitMs=0,i}))()}memory(){return{unreliable:!1,numBytesInGPU:this.numBytesInGPU,numBytesInGPUAllocated:this.textureManager.numBytesAllocated,numBytesInGPUFree:this.textureManager.numBytesFree}}startTimer(){return M().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0?this.gpgpu.beginQuery():{startMs:dt(),endMs:null}}endTimer(e){return M().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0?(this.gpgpu.endQuery(),e):(e.endMs=dt(),e)}getQueryTime(e){return O(this,null,function*(){if(M().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0)return this.gpgpu.waitForQueryAndGetTime(e);const t=e;return t.endMs-t.startMs})}disposeData(e,t=!1){if(this.pendingDisposal.has(e))return!1;if(!this.texData.has(e))return!0;if(t?this.texData.get(e).refCount=0:this.texData.get(e).refCount--,!t&&this.texData.get(e).refCount>0)return!1;if(this.pendingRead.has(e))return this.pendingDisposal.add(e),this.pendingDeletes++,!1;this.releaseGPUData(e);const{complexTensorInfos:s}=this.texData.get(e);return s!=null&&(this.disposeData(s.real.dataId,t),this.disposeData(s.imag.dataId,t)),this.texData.delete(e),!0}releaseGPUData(e){const{texture:t,dtype:s,texShape:r,usage:a,isPacked:o,slice:i}=this.texData.get(e),l=i&&i.origDataId||e,c=this.dataRefCount.get(l);c>1?this.dataRefCount.set(l,c-1):(this.dataRefCount.delete(l),t!=null&&(this.numBytesInGPU-=this.computeBytes(r,s),this.textureManager.releaseTexture(t,r,a,o)));const u=this.texData.get(e);u.texture=null,u.texShape=null,u.isPacked=!1,u.slice=null}getTexture(e){return this.uploadToGPU(e),this.texData.get(e).texture.texture}getDataInfo(e){return this.texData.get(e)}shouldExecuteOnCPU(e,t=JU){return M().getBool("WEBGL_CPU_FORWARD")&&e.every(s=>this.texData.get(s.dataId).texture==null&&U(s.shape)<t)}getGPGPUContext(){return this.gpgpu}where(e){In("tf.where() in webgl locks the UI thread. Call tf.whereAsync() instead");const t=e.dataSync();return KU(e.shape,t)}packedUnaryOp(e,t,s){const r=new wr(e.shape,t),a=this.compileAndRun(r,[e],s);return us().makeTensorFromTensorInfo(a)}abs(e){if(this.shouldExecuteOnCPU([e])&&e.dtype!=="complex64"){const r=lv(this.texData.get(e.dataId).values);return this.makeOutput(e.shape,e.dtype,r)}if(M().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(e,mv,e.dtype);const t=new Ss(e.shape,mv),s=this.compileAndRun(t,[e]);return us().makeTensorFromTensorInfo(s)}makeTensorInfo(e,t,s){let r;if(t==="string"&&s!=null&&s.length>0&&yo(s[0])){const a=s.map(o=>js(o));r=this.write(a,e,t)}else r=this.write(s,e,t);return this.texData.get(r).usage=null,{dataId:r,shape:e,dtype:t}}makeOutput(e,t,s){return us().makeTensorFromTensorInfo(this.makeTensorInfo(e,t,s),this)}unpackTensor(e){const t=new qU(e.shape);return this.runWebGLProgram(t,[e],e.dtype)}packTensor(e){const t=new RU(e.shape),s=!0;return this.runWebGLProgram(t,[e],e.dtype,null,s)}packedReshape(e,t){const s=[no(e.shape),...so(e.shape)],r={dtype:e.dtype,shape:s,dataId:e.dataId},a=[no(t),...so(t)],o=new hv(a,s),i=!0,l=[s],c=this.runWebGLProgram(o,[r],e.dtype,l,i);return{dataId:c.dataId,shape:t,dtype:c.dtype}}decode(e,t){const s=this.texData.get(e),{isPacked:r,shape:a,dtype:o}=s;if(t!=null){const d=U(a),p=t[0]*t[1]*4;S(d<=p,()=>"customTexShape is too small. Row * Column * 4 should be equal or larger than the size of the tensor data.")}const i=qu(a);let l;r?l=new y4(i):l=new g4(i);const c=!0,u=[t!=null?t:Gu(i)],h=this.runWebGLProgram(l,[{shape:i,dtype:o,dataId:e}],o,u,c,t);return{dtype:o,shape:a,dataId:h.dataId}}runWebGLProgram(e,t,s,r,a=!1,o){const i=this.makeTensorInfo(e.outputShape,s),l=this.texData.get(i.dataId);if(e.packedOutput&&(l.isPacked=!0),e.outPackingScheme===tl.DENSE){const y=o!=null?o:Gu(e.outputShape);l.texShape=y.map(b=>b*2)}if(e.outTexUsage!=null&&(l.usage=e.outTexUsage),U(i.shape)===0)return l.values=At(i.dtype,0),i;const c=[],u=t.map(y=>{if(y.dtype==="complex64")throw new Error("GPGPUProgram does not support complex64 input. For complex64 dtypes, please separate the program into real and imaginary parts.");let b=this.texData.get(y.dataId);if(b.texture==null){if(!e.packedInputs&&U(y.shape)<=M().getNumber("WEBGL_SIZE_UPLOAD_UNIFORM"))return{shape:y.shape,texData:null,isUniform:!0,uniformValues:b.values};e.packedInputs&&(b.isPacked=!0,b.shape=y.shape)}if(this.uploadToGPU(y.dataId),!!b.isPacked!=!!e.packedInputs)y=b.isPacked?this.unpackTensor(y):this.packTensor(y),c.push(y),b=this.texData.get(y.dataId);else if(b.isPacked&&!Xu(b.shape,y.shape)){const w=y,x=y.shape;y.shape=b.shape,y=this.packedReshape(y,x),c.push(y),b=this.texData.get(y.dataId),w.shape=x}return{shape:y.shape,texData:b,isUniform:!1}});this.uploadToGPU(i.dataId);const h={shape:i.shape,texData:l,isUniform:!1},d=m4(e,u,h),p=this.getAndSaveBinary(d,()=>p4(this.gpgpu,e,u,h)),f=this.activeTimers!=null;let m;f&&(m=this.startTimer()),M().get("ENGINE_COMPILE_ONLY")||f4(this.gpgpu,p,u,h,r),c.forEach(y=>this.disposeIntermediateTensorInfo(y)),f&&(m=this.endTimer(m),this.activeTimers.push({name:e.constructor.name,query:this.getQueryTime(m)}));const g=M().getNumber("WEBGL_FLUSH_THRESHOLD");if(g>0){const y=dt();y-this.lastGlFlushTime>g&&(this.gpgpu.gl.flush(),this.lastGlFlushTime=y)}if(!M().getBool("WEBGL_LAZILY_UNPACK")&&l.isPacked&&a===!1){const y=this.unpackTensor(i);return this.disposeIntermediateTensorInfo(i),y}return i}compileAndRun(e,t,s,r,a=!1){return s=s||t[0].dtype,this.runWebGLProgram(e,t,s,r,a)}getAndSaveBinary(e,t){return e in this.binaryCache||(this.binaryCache[e]=t()),this.binaryCache[e]}getTextureManager(){return this.textureManager}dispose(){this.disposed||(M().getBool("IS_TEST")||Object.keys(this.binaryCache).forEach(t=>{this.gpgpu.deleteProgram(this.binaryCache[t].webGLProgram),delete this.binaryCache[t]}),this.textureManager.dispose(),this.canvas!=null&&typeof HTMLCanvasElement!="undefined"&&this.canvas instanceof HTMLCanvasElement?this.canvas.remove():this.canvas=null,this.gpgpuCreatedLocally&&(this.gpgpu.program=null,this.gpgpu.dispose()),this.disposed=!0)}floatPrecision(){return this.floatPrecisionValue==null&&(this.floatPrecisionValue=P(()=>{if(!M().get("WEBGL_RENDER_FLOAT32_ENABLED")){const e=M().getBool("DEBUG");M().set("DEBUG",!1);const t=this.abs(fe(1e-8)).dataSync()[0];if(M().set("DEBUG",e),t>0)return 32}return 16})),this.floatPrecisionValue}epsilon(){return this.floatPrecision()===32?XU:YU}uploadToGPU(e){const t=this.texData.get(e),{shape:s,dtype:r,values:a,texture:o,usage:i,isPacked:l}=t;if(o!=null)return;const c=this.activeTimers!=null;let u;c&&(u=dt());let h=t.texShape;if(h==null&&(h=IW(s,l),t.texShape=h),a!=null){const d=qu(s);let p,f=h[1],m=h[0];const g=a instanceof Uint8Array||a instanceof Uint8ClampedArray;(l||!g)&&([f,m]=to(h[0],h[1])),l?p=new I4(d,g):p=new tv(d,g);const y=g?[m,f]:h,b=this.makeTensorInfo(y,r),w=this.texData.get(b.dataId);g?w.usage=Tn.PIXELS:w.usage=Tn.UPLOAD,w.texShape=y,this.gpgpu.uploadDenseMatrixToTexture(this.getTexture(b.dataId),f,m,a);const x=[[m,f]],v=!0,$=this.runWebGLProgram(p,[b],r,x,v),C=this.texData.get($.dataId);t.texShape=C.texShape,t.isPacked=C.isPacked,t.usage=C.usage,M().get("ENGINE_COMPILE_ONLY")?this.disposeData($.dataId):(t.texture=C.texture,t.values=null,this.texData.delete($.dataId)),this.disposeIntermediateTensorInfo(b),c&&(this.uploadWaitMs+=dt()-u)}else{const d=this.acquireTexture(h,i,r,l);t.texture=d}}convertAndCacheOnCPU(e,t){const s=this.texData.get(e),{dtype:r}=s;return t!=null&&(s.values=tG(t,r)),s.values}acquireTexture(e,t,s,r){if(this.numBytesInGPU+=this.computeBytes(e,s),!this.warnedAboutMemory&&this.numBytesInGPU>this.numMBBeforeWarning*1024*1024){const a=(this.numBytesInGPU/1024/1024).toFixed(2);this.warnedAboutMemory=!0,console.warn(`High memory usage in GPU: ${a} MB, most likely due to a memory leak`)}return this.textureManager.acquireTexture(e,t,r)}computeBytes(e,t){return e[0]*e[1]*gl(t)}checkCompileCompletion(){for(const[,e]of Object.entries(this.binaryCache))this.checkCompletion_(e)}checkCompileCompletionAsync(){return O(this,null,function*(){const e=[];if(this.gpgpu.parallelCompilationExtension){for(const[,t]of Object.entries(this.binaryCache))e.push(this.checkCompletionAsync_(t));return Promise.all(e)}else{for(const[,t]of Object.entries(this.binaryCache)){const s=new Promise(r=>{try{this.checkCompletion_(t),r(!0)}catch(a){throw a}});e.push(s)}return Promise.all(e)}})}checkCompletionAsync_(e){return O(this,null,function*(){return this.gpgpu.gl.getProgramParameter(e.webGLProgram,this.gpgpu.parallelCompilationExtension.COMPLETION_STATUS_KHR)?this.checkCompletion_(e):(yield Xb(),this.checkCompletionAsync_(e))})}checkCompletion_(e){if(this.gpgpu.gl.getProgramParameter(e.webGLProgram,this.gpgpu.gl.LINK_STATUS)===!1)throw console.log(this.gpgpu.gl.getProgramInfoLog(e.webGLProgram)),this.gpgpu.gl.getShaderParameter(e.fragmentShader,this.gpgpu.gl.COMPILE_STATUS)===!1?(HI(e.source,this.gpgpu.gl.getShaderInfoLog(e.fragmentShader)),new Error("Failed to compile fragment shader.")):new Error("Failed to link vertex and fragment shaders.");return!0}getUniformLocations(){for(const e of Object.values(this.binaryCache)){this.gpgpu.buildVao(e.webGLProgram);const{variablesLocations:t,customUniformLocations:s,infLoc:r,nanLoc:a,outShapeLocation:o,outShapeStridesLocation:i,outTexShapeLocation:l}=QI(this.gpgpu,e.program,e.webGLProgram);e.variablesLocations=t,e.customUniformLocations=s,e.infLoc=r,e.nanLoc=a,e.outShapeLocation=o,e.outShapeStridesLocation=i,e.outTexShapeLocation=l}}createTensorFromGPUData(e,t,s){e.channels=e.channels||"RGBA";const{texture:r,height:a,width:o,channels:i}=e,l=us().backend;if(!l.gpgpu.gl.isTexture(r))throw new Error("The texture is invalid. Also, please make sure the texture and the TFJS WebGL backend are using the same canvas. If you want to use your own custom canvas, you have to create and use the custom TFJS WebGL backend created from the canvas through 'new tf.MathBackendWebGL(customCanvas)'.");const c=l.writeTexture(r,t,s,a,o,i);return us().makeTensorFromDataId(c,t,s,l)}}Ju.nextDataId=0;function tG(n,e){if(e==="float32"||e==="complex64")return n;if(e==="int32"||e==="bool"){const t=e==="int32"?new Int32Array(n.length):new Uint8Array(n.length);for(let s=0;s<t.length;++s)t[s]=Math.round(n[s]);return t}else throw new Error(`Unknown dtype ${e}`)}/**
|
|
14496
|
+
*/const KU=Dp,XU=1e-7,YU=1e-4,Zu={};function ZU(n){return n in Zu||(Zu[n]={}),Zu[n]}const JU=M().getNumber("CPU_HANDOFF_SIZE_THRESHOLD"),QU=600;function eG(){return M().global.screen==null?1024:M().global.screen.height*M().global.screen.width*window.devicePixelRatio*QU/1024/1024}class Ju extends ga{nextDataId(){return Ju.nextDataId++}constructor(e){if(super(),this.pendingRead=new WeakMap,this.pendingDisposal=new WeakSet,this.dataRefCount=new WeakMap,this.numBytesInGPU=0,this.uploadWaitMs=0,this.downloadWaitMs=0,this.lastGlFlushTime=0,this.warnedAboutMemory=!1,this.pendingDeletes=0,this.disposed=!1,!M().getBool("HAS_WEBGL"))throw new Error("WebGL is not supported on this device");let t;if(e!=null){if(e instanceof Vm)t=e;else{const s=is(M().getNumber("WEBGL_VERSION"),e);t=new Vm(s)}this.binaryCache={},this.gpgpuCreatedLocally=!1}else{const s=is(M().getNumber("WEBGL_VERSION"));t=new Vm(s),this.binaryCache=ZU(M().getNumber("WEBGL_VERSION")),this.gpgpuCreatedLocally=!0}this.gpgpu=t,this.canvas=this.gpgpu.gl.canvas,this.textureManager=new _U(this.gpgpu),this.numMBBeforeWarning=eG(),this.texData=new ml(this,us())}numDataIds(){return this.texData.numDataIds()-this.pendingDeletes}writeTexture(e,t,s,r,a,o){const i=this.makeTensorInfo(t,s),l=this.texData.get(i.dataId);l.isPacked=!1,l.texture={texture:e,texShape:[r,a]},l.texShape=[r,a];const c=qu(t),u=new tv(c,!1,o),h=this.runWebGLProgram(u,[i],s,[[r,a]]);return h.shape=t,l.texture=null,this.disposeIntermediateTensorInfo(i),h.dataId}write(e,t,s){if((M().getBool("WEBGL_CHECK_NUMERICAL_PROBLEMS")||M().getBool("DEBUG"))&&this.checkNumericalProblems(e),s==="complex64"&&e!=null)throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");const r={id:this.nextDataId()};return this.texData.set(r,{shape:t,dtype:s,values:e,usage:Tn.UPLOAD,refCount:1}),r}refCount(e){return this.texData.has(e)?this.texData.get(e).refCount:0}incRef(e){const t=this.texData.get(e);t.refCount++}decRef(e){if(this.texData.has(e)){const t=this.texData.get(e);t.refCount--}}move(e,t,s,r,a){if(M().getBool("DEBUG")&&this.checkNumericalProblems(t),r==="complex64")throw new Error("Cannot write to a complex64 dtype. Please use tf.complex(real, imag).");this.texData.set(e,{shape:s,dtype:r,values:t,usage:Tn.UPLOAD,refCount:a})}disposeIntermediateTensorInfo(e){this.disposeData(e.dataId)}readSync(e){const t=this.texData.get(e),{values:s,dtype:r,complexTensorInfos:a,slice:o,shape:i,isPacked:l}=t;if(o!=null){let d;l?d=new wr(i,xr):d=new Ss(i,xr);const p=this.runWebGLProgram(d,[{dataId:e,shape:i,dtype:r}],r),f=this.readSync(p.dataId);return this.disposeIntermediateTensorInfo(p),f}if(s!=null)return this.convertAndCacheOnCPU(e);if(r==="string")return s;const c=this.activeTimers!=null;let u;c&&(u=dt());let h;if(r==="complex64"){const d=this.readSync(a.real.dataId),p=this.readSync(a.imag.dataId);h=_s(d,p)}else h=this.getValuesFromTexture(e);return c&&(this.downloadWaitMs+=dt()-u),this.convertAndCacheOnCPU(e,h)}read(e){return O(this,null,function*(){if(this.pendingRead.has(e)){const f=this.pendingRead.get(e);return new Promise(m=>f.push(m))}const t=this.texData.get(e),{values:s,shape:r,slice:a,dtype:o,complexTensorInfos:i,isPacked:l}=t;if(a!=null){let f;l?f=new wr(r,xr):f=new Ss(r,xr);const m=this.runWebGLProgram(f,[{dataId:e,shape:r,dtype:o}],o),g=this.read(m.dataId);return this.disposeIntermediateTensorInfo(m),g}if(s!=null)return this.convertAndCacheOnCPU(e);if(M().getBool("DEBUG")&&!M().getBool("WEBGL_DOWNLOAD_FLOAT_ENABLED")&&M().getNumber("WEBGL_VERSION")===2)throw new Error("tensor.data() with WEBGL_DOWNLOAD_FLOAT_ENABLED=false and WEBGL_VERSION=2 not yet supported.");let c=null,u;if(o!=="complex64"&&M().get("WEBGL_BUFFER_SUPPORTED")){u=this.decode(e);const f=this.texData.get(u.dataId);c=this.gpgpu.createBufferFromTexture(f.texture.texture,...Gu(r))}this.pendingRead.set(e,[]),o!=="complex64"&&(yield this.gpgpu.createAndWaitForFence());let h;if(o==="complex64"){const f=yield Promise.all([this.read(i.real.dataId),this.read(i.imag.dataId)]),m=f[0],g=f[1];h=_s(m,g)}else if(c==null)h=this.getValuesFromTexture(e);else{const f=U(r);h=this.gpgpu.downloadFloat32MatrixFromBuffer(c,f)}if(u!=null&&this.disposeIntermediateTensorInfo(u),c!=null){const f=this.gpgpu.gl;oe(f,()=>f.deleteBuffer(c))}const d=this.convertAndCacheOnCPU(e,h),p=this.pendingRead.get(e);return this.pendingRead.delete(e),p.forEach(f=>f(d)),this.pendingDisposal.has(e)&&(this.pendingDisposal.delete(e),this.disposeData(e)&&us().removeDataId(e,this),this.pendingDeletes--),d})}readToGPU(e,t={}){const s=this.texData.get(e),{values:r,shape:a,slice:o,dtype:i,isPacked:l,texture:c}=s;if(i==="complex64")throw new Error("Does not support reading texture for complex64 dtype.");if(o!=null){let p;l?p=new wr(a,xr):p=new Ss(a,xr);const f=this.runWebGLProgram(p,[{dataId:e,shape:a,dtype:i}],i),m=this.readToGPU(f,t);return this.disposeIntermediateTensorInfo(f),m}if(c==null)throw r!=null?new Error("Data is not on GPU but on CPU."):new Error("There is no data on GPU or CPU.");const u=this.decode(e,t.customTexShape),h=us().makeTensorFromTensorInfo(u),d=this.texData.get(u.dataId);return Object.assign({tensorRef:h},d.texture)}bufferSync(e){const t=this.readSync(e.dataId);if(e.dtype==="string")try{const s=t.map(r=>qs(r));return Se(e.shape,e.dtype,s)}catch(s){throw new Error("Failed to decode encoded string bytes into utf-8")}return Se(e.shape,e.dtype,t)}checkNumericalProblems(e){if(e!=null)for(let t=0;t<e.length;t++){const s=e[t];if(!sW(s))throw M().getBool("WEBGL_RENDER_FLOAT32_CAPABLE")?Error(`The value ${s} cannot be represented with your current settings. Consider enabling float32 rendering: 'tf.env().set('WEBGL_RENDER_FLOAT32_ENABLED', true);'`):Error(`The value ${s} cannot be represented on this device.`)}}getValuesFromTexture(e){const{shape:t,dtype:s,isPacked:r}=this.texData.get(e),a=U(t);if(M().getBool("WEBGL_DOWNLOAD_FLOAT_ENABLED")){const d=this.decode(e),p=this.texData.get(d.dataId),f=this.gpgpu.downloadMatrixFromPackedTexture(p.texture.texture,...Gu(t)).subarray(0,a);return this.disposeIntermediateTensorInfo(d),f}const o=M().getBool("WEBGL_PACK")&&r===!0,i=o?qu(t):t,l=o?new x4(i):new b4(i),c=this.runWebGLProgram(l,[{shape:i,dtype:s,dataId:e}],"float32"),u=this.texData.get(c.dataId),h=this.gpgpu.downloadByteEncodedFloatMatrixFromOutputTexture(u.texture.texture,u.texShape[0],u.texShape[1]).subarray(0,a);return this.disposeIntermediateTensorInfo(c),h}timerAvailable(){return M().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0}time(e){const t=this.activeTimers,s=[];let r=!1;this.programTimersStack==null?(this.programTimersStack=s,r=!0):this.activeTimers.push(s),this.activeTimers=s,e();const a=Fr(this.activeTimers.map(l=>l.query)).filter(l=>l!=null),o=Fr(this.activeTimers.map(l=>l.name)).filter(l=>l!=null);this.activeTimers=t,r&&(this.programTimersStack=null);const i={uploadWaitMs:this.uploadWaitMs,downloadWaitMs:this.downloadWaitMs,kernelMs:null,wallMs:null};return(()=>O(this,null,function*(){if(M().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0){const l=yield Promise.all(a);i.kernelMs=vS(l),i.getExtraProfileInfo=()=>l.map((c,u)=>({name:o[u],ms:c})).map(c=>`${c.name}: ${c.ms}`).join(", ")}else i.kernelMs={error:"WebGL query timers are not supported in this environment."};return this.uploadWaitMs=0,this.downloadWaitMs=0,i}))()}memory(){return{unreliable:!1,numBytesInGPU:this.numBytesInGPU,numBytesInGPUAllocated:this.textureManager.numBytesAllocated,numBytesInGPUFree:this.textureManager.numBytesFree}}startTimer(){return M().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0?this.gpgpu.beginQuery():{startMs:dt(),endMs:null}}endTimer(e){return M().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0?(this.gpgpu.endQuery(),e):(e.endMs=dt(),e)}getQueryTime(e){return O(this,null,function*(){if(M().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_RELIABLE")>0)return this.gpgpu.waitForQueryAndGetTime(e);const t=e;return t.endMs-t.startMs})}disposeData(e,t=!1){if(this.pendingDisposal.has(e))return!1;if(!this.texData.has(e))return!0;if(t?this.texData.get(e).refCount=0:this.texData.get(e).refCount--,!t&&this.texData.get(e).refCount>0)return!1;if(this.pendingRead.has(e))return this.pendingDisposal.add(e),this.pendingDeletes++,!1;this.releaseGPUData(e);const{complexTensorInfos:s}=this.texData.get(e);return s!=null&&(this.disposeData(s.real.dataId,t),this.disposeData(s.imag.dataId,t)),this.texData.delete(e),!0}releaseGPUData(e){const{texture:t,dtype:s,texShape:r,usage:a,isPacked:o,slice:i}=this.texData.get(e),l=i&&i.origDataId||e,c=this.dataRefCount.get(l);c>1?this.dataRefCount.set(l,c-1):(this.dataRefCount.delete(l),t!=null&&(this.numBytesInGPU-=this.computeBytes(r,s),this.textureManager.releaseTexture(t,r,a,o)));const u=this.texData.get(e);u.texture=null,u.texShape=null,u.isPacked=!1,u.slice=null}getTexture(e){return this.uploadToGPU(e),this.texData.get(e).texture.texture}getDataInfo(e){return this.texData.get(e)}shouldExecuteOnCPU(e,t=JU){return M().getBool("WEBGL_CPU_FORWARD")&&e.every(s=>this.texData.get(s.dataId).texture==null&&U(s.shape)<t)}getGPGPUContext(){return this.gpgpu}where(e){In("tf.where() in webgl locks the UI thread. Call tf.whereAsync() instead");const t=e.dataSync();return KU(e.shape,t)}packedUnaryOp(e,t,s){const r=new wr(e.shape,t),a=this.compileAndRun(r,[e],s);return us().makeTensorFromTensorInfo(a)}abs(e){if(this.shouldExecuteOnCPU([e])&&e.dtype!=="complex64"){const r=lv(this.texData.get(e.dataId).values);return this.makeOutput(e.shape,e.dtype,r)}if(M().getBool("WEBGL_PACK_UNARY_OPERATIONS"))return this.packedUnaryOp(e,mv,e.dtype);const t=new Ss(e.shape,mv),s=this.compileAndRun(t,[e]);return us().makeTensorFromTensorInfo(s)}makeTensorInfo(e,t,s){let r;if(t==="string"&&s!=null&&s.length>0&&yo(s[0])){const a=s.map(o=>js(o));r=this.write(a,e,t)}else r=this.write(s,e,t);return this.texData.get(r).usage=null,{dataId:r,shape:e,dtype:t}}makeOutput(e,t,s){return us().makeTensorFromTensorInfo(this.makeTensorInfo(e,t,s),this)}unpackTensor(e){const t=new qU(e.shape);return this.runWebGLProgram(t,[e],e.dtype)}packTensor(e){const t=new RU(e.shape),s=!0;return this.runWebGLProgram(t,[e],e.dtype,null,s)}packedReshape(e,t){const s=[no(e.shape),...so(e.shape)],r={dtype:e.dtype,shape:s,dataId:e.dataId},a=[no(t),...so(t)],o=new hv(a,s),i=!0,l=[s],c=this.runWebGLProgram(o,[r],e.dtype,l,i);return{dataId:c.dataId,shape:t,dtype:c.dtype}}decode(e,t){const s=this.texData.get(e),{isPacked:r,shape:a,dtype:o}=s;if(t!=null){const d=U(a),p=t[0]*t[1]*4;S(d<=p,()=>"customTexShape is too small. Row * Column * 4 should be equal or larger than the size of the tensor data.")}const i=qu(a);let l;r?l=new y4(i):l=new g4(i);const c=!0,u=[t!=null?t:Gu(i)],h=this.runWebGLProgram(l,[{shape:i,dtype:o,dataId:e}],o,u,c,t);return{dtype:o,shape:a,dataId:h.dataId}}runWebGLProgram(e,t,s,r,a=!1,o){const i=this.makeTensorInfo(e.outputShape,s),l=this.texData.get(i.dataId);if(e.packedOutput&&(l.isPacked=!0),e.outPackingScheme===tl.DENSE){const y=o!=null?o:Gu(e.outputShape);l.texShape=y.map(b=>b*2)}if(e.outTexUsage!=null&&(l.usage=e.outTexUsage),U(i.shape)===0)return l.values=At(i.dtype,0),i;const c=[],u=t.map(y=>{if(y.dtype==="complex64")throw new Error("GPGPUProgram does not support complex64 input. For complex64 dtypes, please separate the program into real and imaginary parts.");let b=this.texData.get(y.dataId);if(b.texture==null){if(!e.packedInputs&&U(y.shape)<=M().getNumber("WEBGL_SIZE_UPLOAD_UNIFORM"))return{shape:y.shape,texData:null,isUniform:!0,uniformValues:b.values};e.packedInputs&&(b.isPacked=!0,b.shape=y.shape)}if(this.uploadToGPU(y.dataId),!!b.isPacked!=!!e.packedInputs)y=b.isPacked?this.unpackTensor(y):this.packTensor(y),c.push(y),b=this.texData.get(y.dataId);else if(b.isPacked&&!Xu(b.shape,y.shape)){const w=y,x=y.shape;y.shape=b.shape,y=this.packedReshape(y,x),c.push(y),b=this.texData.get(y.dataId),w.shape=x}return{shape:y.shape,texData:b,isUniform:!1}});this.uploadToGPU(i.dataId);const h={shape:i.shape,texData:l,isUniform:!1},d=m4(e,u,h),p=this.getAndSaveBinary(d,()=>p4(this.gpgpu,e,u,h)),f=this.activeTimers!=null;let m;f&&(m=this.startTimer()),M().get("ENGINE_COMPILE_ONLY")||f4(this.gpgpu,p,u,h,r),c.forEach(y=>this.disposeIntermediateTensorInfo(y)),f&&(m=this.endTimer(m),this.activeTimers.push({name:e.constructor.name,query:this.getQueryTime(m)}));const g=M().getNumber("WEBGL_FLUSH_THRESHOLD");if(g>0){const y=dt();y-this.lastGlFlushTime>g&&(this.gpgpu.gl.flush(),this.lastGlFlushTime=y)}if(!M().getBool("WEBGL_LAZILY_UNPACK")&&l.isPacked&&a===!1){const y=this.unpackTensor(i);return this.disposeIntermediateTensorInfo(i),y}return i}compileAndRun(e,t,s,r,a=!1){return s=s||t[0].dtype,this.runWebGLProgram(e,t,s,r,a)}getAndSaveBinary(e,t){return e in this.binaryCache||(this.binaryCache[e]=t()),this.binaryCache[e]}getTextureManager(){return this.textureManager}dispose(){this.disposed||(M().getBool("IS_TEST")||Object.keys(this.binaryCache).forEach(t=>{this.gpgpu.deleteProgram(this.binaryCache[t].webGLProgram),delete this.binaryCache[t]}),this.textureManager.dispose(),this.canvas!=null&&typeof HTMLCanvasElement!="undefined"&&this.canvas instanceof HTMLCanvasElement?this.canvas.remove():this.canvas=null,this.gpgpuCreatedLocally&&(this.gpgpu.program=null,this.gpgpu.dispose()),this.disposed=!0)}floatPrecision(){return this.floatPrecisionValue==null&&(this.floatPrecisionValue=P(()=>{if(!M().get("WEBGL_RENDER_FLOAT32_ENABLED")){const e=M().getBool("DEBUG");M().set("DEBUG",!1);const t=this.abs(pe(1e-8)).dataSync()[0];if(M().set("DEBUG",e),t>0)return 32}return 16})),this.floatPrecisionValue}epsilon(){return this.floatPrecision()===32?XU:YU}uploadToGPU(e){const t=this.texData.get(e),{shape:s,dtype:r,values:a,texture:o,usage:i,isPacked:l}=t;if(o!=null)return;const c=this.activeTimers!=null;let u;c&&(u=dt());let h=t.texShape;if(h==null&&(h=IW(s,l),t.texShape=h),a!=null){const d=qu(s);let p,f=h[1],m=h[0];const g=a instanceof Uint8Array||a instanceof Uint8ClampedArray;(l||!g)&&([f,m]=to(h[0],h[1])),l?p=new I4(d,g):p=new tv(d,g);const y=g?[m,f]:h,b=this.makeTensorInfo(y,r),w=this.texData.get(b.dataId);g?w.usage=Tn.PIXELS:w.usage=Tn.UPLOAD,w.texShape=y,this.gpgpu.uploadDenseMatrixToTexture(this.getTexture(b.dataId),f,m,a);const x=[[m,f]],v=!0,$=this.runWebGLProgram(p,[b],r,x,v),C=this.texData.get($.dataId);t.texShape=C.texShape,t.isPacked=C.isPacked,t.usage=C.usage,M().get("ENGINE_COMPILE_ONLY")?this.disposeData($.dataId):(t.texture=C.texture,t.values=null,this.texData.delete($.dataId)),this.disposeIntermediateTensorInfo(b),c&&(this.uploadWaitMs+=dt()-u)}else{const d=this.acquireTexture(h,i,r,l);t.texture=d}}convertAndCacheOnCPU(e,t){const s=this.texData.get(e),{dtype:r}=s;return t!=null&&(s.values=tG(t,r)),s.values}acquireTexture(e,t,s,r){if(this.numBytesInGPU+=this.computeBytes(e,s),!this.warnedAboutMemory&&this.numBytesInGPU>this.numMBBeforeWarning*1024*1024){const a=(this.numBytesInGPU/1024/1024).toFixed(2);this.warnedAboutMemory=!0,console.warn(`High memory usage in GPU: ${a} MB, most likely due to a memory leak`)}return this.textureManager.acquireTexture(e,t,r)}computeBytes(e,t){return e[0]*e[1]*gl(t)}checkCompileCompletion(){for(const[,e]of Object.entries(this.binaryCache))this.checkCompletion_(e)}checkCompileCompletionAsync(){return O(this,null,function*(){const e=[];if(this.gpgpu.parallelCompilationExtension){for(const[,t]of Object.entries(this.binaryCache))e.push(this.checkCompletionAsync_(t));return Promise.all(e)}else{for(const[,t]of Object.entries(this.binaryCache)){const s=new Promise(r=>{try{this.checkCompletion_(t),r(!0)}catch(a){throw a}});e.push(s)}return Promise.all(e)}})}checkCompletionAsync_(e){return O(this,null,function*(){return this.gpgpu.gl.getProgramParameter(e.webGLProgram,this.gpgpu.parallelCompilationExtension.COMPLETION_STATUS_KHR)?this.checkCompletion_(e):(yield Xb(),this.checkCompletionAsync_(e))})}checkCompletion_(e){if(this.gpgpu.gl.getProgramParameter(e.webGLProgram,this.gpgpu.gl.LINK_STATUS)===!1)throw console.log(this.gpgpu.gl.getProgramInfoLog(e.webGLProgram)),this.gpgpu.gl.getShaderParameter(e.fragmentShader,this.gpgpu.gl.COMPILE_STATUS)===!1?(HI(e.source,this.gpgpu.gl.getShaderInfoLog(e.fragmentShader)),new Error("Failed to compile fragment shader.")):new Error("Failed to link vertex and fragment shaders.");return!0}getUniformLocations(){for(const e of Object.values(this.binaryCache)){this.gpgpu.buildVao(e.webGLProgram);const{variablesLocations:t,customUniformLocations:s,infLoc:r,nanLoc:a,outShapeLocation:o,outShapeStridesLocation:i,outTexShapeLocation:l}=QI(this.gpgpu,e.program,e.webGLProgram);e.variablesLocations=t,e.customUniformLocations=s,e.infLoc=r,e.nanLoc=a,e.outShapeLocation=o,e.outShapeStridesLocation=i,e.outTexShapeLocation=l}}createTensorFromGPUData(e,t,s){e.channels=e.channels||"RGBA";const{texture:r,height:a,width:o,channels:i}=e,l=us().backend;if(!l.gpgpu.gl.isTexture(r))throw new Error("The texture is invalid. Also, please make sure the texture and the TFJS WebGL backend are using the same canvas. If you want to use your own custom canvas, you have to create and use the custom TFJS WebGL backend created from the canvas through 'new tf.MathBackendWebGL(customCanvas)'.");const c=l.writeTexture(r,t,s,a,o,i);return us().makeTensorFromDataId(c,t,s,l)}}Ju.nextDataId=0;function tG(n,e){if(e==="float32"||e==="complex64")return n;if(e==="int32"||e==="bool"){const t=e==="int32"?new Int32Array(n.length):new Uint8Array(n.length);for(let s=0;s<t.length;++s)t[s]=Math.round(n[s]);return t}else throw new Error(`Unknown dtype ${e}`)}/**
|
|
14497
14497
|
* @license
|
|
14498
14498
|
* Copyright 2020 Google Inc. All Rights Reserved.
|
|
14499
14499
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -22081,7 +22081,7 @@ return a / b;`,packedOpSnippet:`
|
|
|
22081
22081
|
* See the License for the specific language governing permissions and
|
|
22082
22082
|
* limitations under the License.
|
|
22083
22083
|
* =============================================================================
|
|
22084
|
-
*/const W5=[wG,vG,NG,TG,AG,_G,OG,PG,VG,UG,jG,XG,JG,nH,aH,iH,cH,pH,mH,yH,wH,CH,TH,DH,FH,MH,VH,HH,sG,KH,QH,sj,cj,dj,fj,gj,bj,vj,$j,Cj,Tj,Aj,Dj,Oj,Pj,Vj,Uj,jj,Xj,Zj,Qj,nq,rq,iq,cq,uq,dq,fq,gq,bq,wq,vq,Nq,Tq,Aq,_q,Lq,zq,Bq,nG,Wq,ZH,Gq,jq,Kq,aG,Yq,Jq,eK,sK,oK,lK,uK,dK,mK,yK,xK,SK,NK,kK,RK,_K,OK,PK,MK,UK,jK,YK,n6,lG,o6,c6,d6,m6,OH,y6,x6,I6,$6,T6,iG,A6,D6,F6,L6,P6,LH,JK,M6,W6,H6,uG,X6,J6,nX,aX,cX,hX,pX,mX,bX,IX,$X,kX,AX,DX,LX,zX,NH,e6,BX,VX,UX,HX,qX,XX,ZX,QX,t5,s5,a5,i5,c5,d5,f5,g5,b5,QK,yG,w5,v5,$5,k5,R5,F5,bG,L5,z5,V5,b6];for(const n of W5)wg(n);var ih=(n=>(n[n.None=0]="None",n[n.RunCommand=1]="RunCommand",n[n.Say=2]="Say",n))(ih||{});class Zm{constructor(e,t,s){this.name=e,this.description=t,this.type=s}}const U5="No Action";new Zm(U5,"Do nothing",ih.None),new Zm("Marty Say","Marty says",ih.Say),new Zm("Marty Move","Make Marty move",ih.RunCommand);const cS=224,uS=224;/**
|
|
22084
|
+
*/const W5=[wG,vG,NG,TG,AG,_G,OG,PG,VG,UG,jG,XG,JG,nH,aH,iH,cH,pH,mH,yH,wH,CH,TH,DH,FH,MH,VH,HH,sG,KH,QH,sj,cj,dj,fj,gj,bj,vj,$j,Cj,Tj,Aj,Dj,Oj,Pj,Vj,Uj,jj,Xj,Zj,Qj,nq,rq,iq,cq,uq,dq,fq,gq,bq,wq,vq,Nq,Tq,Aq,_q,Lq,zq,Bq,nG,Wq,ZH,Gq,jq,Kq,aG,Yq,Jq,eK,sK,oK,lK,uK,dK,mK,yK,xK,SK,NK,kK,RK,_K,OK,PK,MK,UK,jK,YK,n6,lG,o6,c6,d6,m6,OH,y6,x6,I6,$6,T6,iG,A6,D6,F6,L6,P6,LH,JK,M6,W6,H6,uG,X6,J6,nX,aX,cX,hX,pX,mX,bX,IX,$X,kX,AX,DX,LX,zX,NH,e6,BX,VX,UX,HX,qX,XX,ZX,QX,t5,s5,a5,i5,c5,d5,f5,g5,b5,QK,yG,w5,v5,$5,k5,R5,F5,bG,L5,z5,V5,b6];for(const n of W5)wg(n);var ih=(n=>(n[n.None=0]="None",n[n.RunCommand=1]="RunCommand",n[n.Say=2]="Say",n))(ih||{});class Zm{constructor(e,t,s){this.name=e,this.description=t,this.type=s}}const U5="No Action";new Zm(U5,"Do nothing",ih.None),new Zm("Marty Say","Marty says",ih.Say),new Zm("Marty Move","Make Marty move",ih.RunCommand),pe(127),pe(1);const cS=224,uS=224;/**
|
|
22085
22085
|
* @license
|
|
22086
22086
|
* Copyright 2019 Google LLC. All Rights Reserved.
|
|
22087
22087
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -22111,7 +22111,7 @@ return a / b;`,packedOpSnippet:`
|
|
|
22111
22111
|
* See the License for the specific language governing permissions and
|
|
22112
22112
|
* limitations under the License.
|
|
22113
22113
|
* =============================================================================
|
|
22114
|
-
*/function G5(n){return O(this,null,function*(){const e="http://",t="https://";if(n.indexOf(e)===0||n.indexOf(t)===0)return yield(yield fetch(n)).json();throw new Error(`Unsupported URL scheme in metadata URL: ${n}. Supported schemes are: http://, https://, and (node.js-only) file://`)})}let po=null;function hl(n){return po==null&&(po=Xd().epsilon()),P(()=>{const{mean:e,variance:t}=Pa(n);return
|
|
22114
|
+
*/function G5(n){return O(this,null,function*(){const e="http://",t="https://";if(n.indexOf(e)===0||n.indexOf(t)===0)return yield(yield fetch(n)).json();throw new Error(`Unsupported URL scheme in metadata URL: ${n}. Supported schemes are: http://, https://, and (node.js-only) file://`)})}let po=null;function hl(n){return po==null&&(po=Xd().epsilon()),P(()=>{const{mean:e,variance:t}=Pa(n);return fe(ie(n,e),X(_t(t),po))})}function H5(n){if(n.length<2)throw new Error("Cannot normalize a Float32Array with fewer than 2 elements.");return po==null&&(po=Xd().epsilon()),P(()=>{const{mean:e,variance:t}=Pa(ft(n)),s=e.arraySync(),r=Math.sqrt(t.arraySync()),a=Array.from(n).map(o=>(o-s)/(r+po));return new Float32Array(a)})}/**
|
|
22115
22115
|
* @license
|
|
22116
22116
|
* Copyright 2019 Google LLC. All Rights Reserved.
|
|
22117
22117
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -22171,4 +22171,4 @@ return a / b;`,packedOpSnippet:`
|
|
|
22171
22171
|
* See the License for the specific language governing permissions and
|
|
22172
22172
|
* limitations under the License.
|
|
22173
22173
|
* =============================================================================
|
|
22174
|
-
*/const Qm="tfjs-speech-commands-saved-model-metadata",a8="indexeddb://tfjs-speech-commands-model/";let eg={localStorage:typeof window=="undefined"?null:window.localStorage};function o8(n){return n.split(".").slice(0,2).join(".")}const yS="0.5.4",uh=.25,mo=class mo{constructor(e,t,s){ce(this,"MODEL_URL_PREFIX",`https://storage.googleapis.com/tfjs-models/tfjs/speech-commands/v${o8(yS)}/browser_fft`);ce(this,"SAMPLE_RATE_HZ",44100);ce(this,"FFT_SIZE",1024);ce(this,"DEFAULT_SUPPRESSION_TIME_MILLIS",0);ce(this,"model");ce(this,"modelWithEmbeddingOutput");ce(this,"vocabulary");ce(this,"parameters");ce(this,"words");ce(this,"streaming",!1);ce(this,"nonBatchInputShape");ce(this,"elementsPerExample");ce(this,"audioDataExtractor");ce(this,"transferRecognizers",{});ce(this,"modelArtifactsOrURL");ce(this,"metadataOrURL");ce(this,"secondLastBaseDenseLayer");S(t==null&&s==null||t!=null&&s!=null,()=>"modelURL and metadataURL must be both provided or both not provided."),t==null?(e==null?e=mo.DEFAULT_VOCABULARY_NAME:S(mo.VALID_VOCABULARY_NAMES.indexOf(e)!==-1,()=>`Invalid vocabulary name: '${e}'`),this.vocabulary=e,this.modelArtifactsOrURL=`${this.MODEL_URL_PREFIX}/${this.vocabulary}/model.json`,this.metadataOrURL=`${this.MODEL_URL_PREFIX}/${this.vocabulary}/metadata.json`):(S(e==null,()=>"vocabulary name must be null or undefined when modelURL is provided"),this.modelArtifactsOrURL=t,this.metadataOrURL=s),this.parameters={sampleRateHz:this.SAMPLE_RATE_HZ,fftSize:this.FFT_SIZE}}listen(e,t){return O(this,null,function*(){if(this.streaming)throw new Error("Cannot start streaming again when streaming is ongoing.");console.log("in listen: ensuring model is loaded"),yield this.ensureModelLoaded(),t==null&&(t={});let s=t.probabilityThreshold==null?0:t.probabilityThreshold;if(t.includeEmbedding&&(s=0),S(s>=0&&s<=1,()=>`Invalid probabilityThreshold value: ${s}`),t.invokeCallbackOnNoiseAndUnknown==null||t.invokeCallbackOnNoiseAndUnknown,t.includeEmbedding,t.suppressionTimeMillis<0)throw new Error(`suppressionTimeMillis is expected to be >= 0, but got ${t.suppressionTimeMillis}`);const r=t.overlapFactor==null?.5:t.overlapFactor;S(r>=0&&r<1,()=>`Expected overlapFactor to be >= 0 and < 1, but got ${r}`);const a=(i,l)=>O(this,null,function*(){const c=hl(i);let u,h;t.includeEmbedding?(yield this.ensureModelWithEmbeddingOutputCreated(),[u,h]=this.modelWithEmbeddingOutput.predict(c)):u=this.model.predict(c);const d=yield u.data(),p=u.argMax(-1);(yield p.data())[0];const f=Math.max(...d);if(be([u,p,c]),f<s)return!1;{let m;t.includeSpectrogram&&(m={data:yield i.data(),frameSize:this.nonBatchInputShape[1]});let g=!0;return e({scores:d,spectrogram:m,embedding:h}),g}}),o=t.suppressionTimeMillis==null?this.DEFAULT_SUPPRESSION_TIME_MILLIS:t.suppressionTimeMillis;this.audioDataExtractor=new Jm({sampleRateHz:this.parameters.sampleRateHz,numFramesPerSpectrogram:this.nonBatchInputShape[0],columnTruncateLength:this.nonBatchInputShape[1],suppressionTimeMillis:o,spectrogramCallback:a,overlapFactor:r}),yield this.audioDataExtractor.startStreamedInput(),this.streaming=!0})}ensureModelLoaded(){return O(this,null,function*(){if(this.model!=null)return;yield this.ensureMetadataLoaded();let e;if(typeof this.modelArtifactsOrURL=="string"?e=yield Hi(this.modelArtifactsOrURL):e=yield Hi(Pb(this.modelArtifactsOrURL.modelTopology||{},this.modelArtifactsOrURL.weightSpecs,this.modelArtifactsOrURL.weightData)),e.inputs.length!==1)throw new Error(`Expected model to have 1 input, but got a model with ${e.inputs.length} inputs`);if(e.inputs[0].shape.length!==4)throw new Error(`Expected model to have an input shape of rank 4, but got an input shape of rank ${e.inputs[0].shape.length}`);if(e.inputs[0].shape[3]!==1)throw new Error(`Expected model to have an input shape with 1 as the last dimension, but got input shape${JSON.stringify(e.inputs[0].shape[3])}}`);const t=e.outputShape;if(t.length!==2)throw new Error(`Expected loaded model to have an output shape of rank 2,but received shape ${JSON.stringify(t)}`);if(t[1]!==this.words.length)throw new Error(`Mismatch between the last dimension of model's output shape (${t[1]}) and number of words (${this.words.length}).`);this.model=e,this.freezeModel(),this.nonBatchInputShape=e.inputs[0].shape.slice(1),this.elementsPerExample=1,e.inputs[0].shape.slice(1).forEach(a=>this.elementsPerExample*=a),this.warmUpModel();const s=this.parameters.fftSize/this.parameters.sampleRateHz*1e3,r=e.inputs[0].shape[1];this.parameters.spectrogramDurationMillis=r*s})}ensureModelWithEmbeddingOutputCreated(){return O(this,null,function*(){if(this.modelWithEmbeddingOutput!=null)return;yield this.ensureModelLoaded();let e;for(let t=this.model.layers.length-2;t>=0;--t)if(this.model.layers[t].getClassName()==="Dense"){e=this.model.layers[t];break}if(e==null)throw new Error("Failed to find second last dense layer in the original model.");this.modelWithEmbeddingOutput=Su({inputs:this.model.inputs,outputs:[this.model.outputs[0],e.output]})})}warmUpModel(){P(()=>{const e=ct([1].concat(this.nonBatchInputShape));for(let t=0;t<3;++t)this.model.predict(e)})}ensureMetadataLoaded(){return O(this,null,function*(){if(this.words!=null)return;const e=typeof this.metadataOrURL=="string"?yield G5(this.metadataOrURL):this.metadataOrURL;if(e.wordLabels==null){const t=e.words;if(t==null)throw new Error('Cannot find field "words" or "wordLabels" in metadata JSON file');this.words=t}else this.words=e.wordLabels})}stopListening(){return O(this,null,function*(){yield this.audioDataExtractor.stop(),this.streaming=!1})}isListening(){return this.streaming}wordLabels(){return this.words}params(){return this.parameters}modelInputShape(){if(this.model==null)throw new Error("Model has not been loaded yet. Load model by calling ensureModelLoaded(), recognize(), or listen().");return this.model.inputs[0].shape}recognize(e,t){return O(this,null,function*(){t==null&&(t={}),yield this.ensureModelLoaded(),e==null&&(e=(yield this.recognizeOnline()).data);let s,r,a;if(e instanceof Oe)this.checkInputTensorShape(e),r=e,s=e.shape[0];else{if(e.length%this.elementsPerExample)throw new Error(`The length of the input Float32Array ${e.length} is not divisible by the number of tensor elements per per example expected by the model ${this.elementsPerExample}.`);s=e.length/this.elementsPerExample,r=ab(e,[s].concat(this.nonBatchInputShape))}const o={scores:[]};if(t.includeEmbedding){yield this.ensureModelWithEmbeddingOutputCreated();const i=this.modelWithEmbeddingOutput.predict(r);a=i[0],o.embedding=i[1]}else a=this.model.predict(r);if(s===1)o.scores=yield a.data();else{const i=Cn(a),l=i.map(c=>c.data());o.scores=yield Promise.all(l),be(i)}return t.includeSpectrogram&&(o.spectrogram={data:e instanceof Oe?yield e.data():e,frameSize:this.nonBatchInputShape[1]}),be(a),o})}recognizeOnline(){return O(this,null,function*(){return new Promise((e,t)=>{const s=r=>O(this,null,function*(){const a=hl(r);return yield this.audioDataExtractor.stop(),e({data:yield a.data(),frameSize:this.nonBatchInputShape[1]}),a.dispose(),!1});this.audioDataExtractor=new Jm({sampleRateHz:this.parameters.sampleRateHz,numFramesPerSpectrogram:this.nonBatchInputShape[0],columnTruncateLength:this.nonBatchInputShape[1],suppressionTimeMillis:0,spectrogramCallback:s,overlapFactor:0})})})}createTransfer(e){if(this.model==null)throw new Error("Model has not been loaded yet. Load model by calling ensureModelLoaded(), recognizer(), or listen().");S(e!=null&&typeof e=="string"&&e.length>1,()=>`Expected the name for a transfer-learning recognized to be a non-empty string, but got ${JSON.stringify(e)}`),S(this.transferRecognizers[e]==null,()=>`There is already a transfer-learning model named '${e}'`);const t=new i8(e,this.parameters,this.model);return this.transferRecognizers[e]=t,t}freezeModel(){for(const e of this.model.layers)e.trainable=!1}checkInputTensorShape(e){const t=this.model.inputs[0].shape.length;if(e.shape.length!==t)throw new Error(`Expected input Tensor to have rank ${t}, but got rank ${e.shape.length} that differs `);const s=e.shape.slice(1),r=this.model.inputs[0].shape.slice(1);if(!Ee(s,r))throw new Error(`Expected input to have shape [null,${r}], but got shape [null,${s}]`)}};ce(mo,"VALID_VOCABULARY_NAMES",["18w","directional4w"]),ce(mo,"DEFAULT_VOCABULARY_NAME","18w");let fo=mo;class i8 extends fo{constructor(t,s,r){super();ce(this,"dataset");ce(this,"transferHead");this.name=t,this.parameters=s,this.baseModel=r,S(t!=null&&typeof t=="string"&&t.length>0,()=>`The name of a transfer model must be a non-empty string, but got ${JSON.stringify(t)}`),this.nonBatchInputShape=this.baseModel.inputs[0].shape.slice(1),this.words=null,this.dataset=new gS}addExamples(t){return O(this,null,function*(){for(const s of t)for(const r of s.samples)console.log(`class ${s.name}, sample ${r}, has timeDataQueue length ${r.timeDataQueue.length}, and freqDataQueue length ${r.freqDataQueue.length}`),yield this.collectExample(s.name,r,{durationMultiplier:1,includeRawAudio:!0})})}collectExample(t,s,r){return O(this,null,function*(){if(S(!this.streaming,()=>"Cannot start collection of transfer-learning example because a streaming recognition or transfer-learning example collection is ongoing"),S(t!=null&&typeof t=="string"&&t.length>0,()=>"Must provide a non-empty string when collecting transfer-learning example"),r==null&&(r={}),r.durationMultiplier!=null&&r.durationSec!=null)throw new Error("durationMultiplier and durationSec are mutually exclusive, but are both specified.");let a;if(r.durationSec!=null){S(r.durationSec>0,()=>`Expected durationSec to be > 0, but got ${r.durationSec}`);const l=this.parameters.fftSize/this.parameters.sampleRateHz;a=Math.ceil(r.durationSec/l)}else r.durationMultiplier!=null?(S(r.durationMultiplier>=1,()=>`Expected duration multiplier to be >= 1, but got ${r.durationMultiplier}`),a=Math.round(this.nonBatchInputShape[0]*r.durationMultiplier)):a=this.nonBatchInputShape[0];r.snippetDurationSec!=null&&(S(r.snippetDurationSec>0,()=>`snippetDurationSec is expected to be > 0, but got ${r.snippetDurationSec}`),S(r.onSnippet!=null,()=>"onSnippet must be provided if snippetDurationSec is provided.")),r.onSnippet!=null&&S(r.snippetDurationSec!=null,()=>"snippetDurationSec must be provided if onSnippet is provided.");const i=this.parameters.fftSize/this.parameters.sampleRateHz*a;return this.streaming=!0,new Promise(l=>{const c=r.snippetDurationSec==null?1:r.snippetDurationSec/i,u=1-c,h=Math.round(1/c);let d=0,p=-1;const f=[],m=(g,y)=>O(this,null,function*(){if(r.onSnippet==null){const b=hl(g);this.dataset.addExample({label:t,spectrogram:{data:yield b.data(),frameSize:this.nonBatchInputShape[1]},rawAudio:r.includeRawAudio?{data:yield y.data(),sampleRateHz:this.audioDataExtractor.sampleRateHz}:void 0}),b.dispose(),yield this.audioDataExtractor.stop(),this.streaming=!1,this.collateTransferWords(),l({data:yield g.data(),frameSize:this.nonBatchInputShape[1]})}else{const b=yield g.data();p===-1&&(p=b.length);let w=p-1;for(;b[w]!==0&&w>=0;)w--;const x=p-w-1;p=w+1;const v=b.slice(b.length-x,b.length);if(f.push(v),r.onSnippet!=null&&r.onSnippet({data:v,frameSize:this.nonBatchInputShape[1]}),d++===h){yield this.audioDataExtractor.stop(),this.streaming=!1,this.collateTransferWords();const C={data:H5(j5(f)),frameSize:this.nonBatchInputShape[1]};this.dataset.addExample({label:t,spectrogram:C,rawAudio:r.includeRawAudio?{data:yield y.data(),sampleRateHz:this.audioDataExtractor.sampleRateHz}:void 0}),l(C)}}return!1});this.audioDataExtractor=new Jm({sampleRateHz:this.parameters.sampleRateHz,numFramesPerSpectrogram:a,columnTruncateLength:this.nonBatchInputShape[1],suppressionTimeMillis:0,spectrogramCallback:m,overlapFactor:u,includeRawAudio:r.includeRawAudio}),this.audioDataExtractor.start(s)})})}clearExamples(){S(this.words!=null&&this.words.length>0&&!this.dataset.empty(),()=>`No transfer learning examples exist for model name ${this.name}`),this.dataset.clear(),this.words=null}countExamples(){if(this.dataset.empty())throw new Error(`No examples have been collected for transfer-learning model named '${this.name}' yet.`);return this.dataset.getExampleCounts()}getExamples(t){return this.dataset.getExamples(t)}setExampleKeyFrameIndex(t,s){this.dataset.setExampleKeyFrameIndex(t,s)}removeExample(t){this.dataset.removeExample(t),this.collateTransferWords()}isDatasetEmpty(){return this.dataset.empty()}loadExamples(t,s=!1){const r=new gS(t);s&&this.clearExamples();const a=r.getVocabulary();for(const o of a){const i=r.getExamples(o);for(const l of i)this.dataset.addExample(l.example)}this.collateTransferWords()}serializeExamples(t){return this.dataset.serialize(t)}collateTransferWords(){this.words=this.dataset.getVocabulary()}collectTransferDataAsTensors(t,s){const r=this.nonBatchInputShape[0];t=t||uh;const a=Math.round(t*r),o=this.dataset.getData(void 0,cg({numFrames:r,hopFrames:a},s));return{xs:o.xs,ys:o.ys}}collectTransferDataAsTfDataset(t,s=.15,r=32,a){const o=this.nonBatchInputShape[0];t=t||uh;const i=Math.round(t*o);return this.dataset.getData(void 0,cg({numFrames:o,hopFrames:i,getDataset:!0,datasetBatchSize:r,datasetValidationSplit:s},a))}train(t){return O(this,null,function*(){S(this.words!=null&&this.words.length>0,()=>`Cannot train transfer-learning model '${this.name}' because no transfer learning example has been collected.`),S(this.words.length>1,()=>`Cannot train transfer-learning model '${this.name}' because only 1 word label ('${JSON.stringify(this.words)}') has been collected for transfer learning. Requires at least 2.`),t.fineTuningEpochs!=null&&S(t.fineTuningEpochs>=0&&Number.isInteger(t.fineTuningEpochs),()=>`If specified, fineTuningEpochs must be a non-negative integer, but received ${t.fineTuningEpochs}`),t==null&&(t={}),this.model==null&&this.createTransferModelFromBaseModel(),this.secondLastBaseDenseLayer.trainable=!1,this.model.compile({loss:"categoricalCrossentropy",optimizer:t.optimizer||"sgd",metrics:["acc"]});const s=t.fitDatasetDurationMillisThreshold==null?6e4:t.fitDatasetDurationMillisThreshold;return this.dataset.durationMillis()>s?(console.log(`Detected large dataset: total duration = ${this.dataset.durationMillis()} ms > ${s} ms. Training transfer model using fitDataset() instead of fit()`),this.trainOnDataset(t)):this.trainOnTensors(t)})}trainOnDataset(t){return O(this,null,function*(){S(t.epochs>0,()=>"Invalid config!.epochs");const s=t.batchSize==null?32:t.batchSize,r=t.windowHopRatio||uh,[a,o]=this.collectTransferDataAsTfDataset(r,t.validationSplit,s,{augmentByMixingNoiseRatio:t.augmentByMixingNoiseRatio}),i=dt(),l=yield this.model.fitDataset(a,{epochs:t.epochs,validationData:t.validationSplit>0?o:void 0,callbacks:t.callback==null?void 0:[t.callback]});if(console.log(`fitDataset() took ${(dt()-i).toFixed(2)} ms`),t.fineTuningEpochs!=null&&t.fineTuningEpochs>0){const c=dt(),u=yield this.fineTuningUsingTfDatasets(t,a,o);return console.log(`fitDataset() (fine-tuning) took ${(dt()-c).toFixed(2)} ms`),[l,u]}else return l})}trainOnTensors(t){return O(this,null,function*(){const s=t.windowHopRatio||uh,{xs:r,ys:a}=this.collectTransferDataAsTensors(s,{augmentByMixingNoiseRatio:t.augmentByMixingNoiseRatio});console.log(`Training data: xs.shape = ${r.shape}, ys.shape = ${a.shape}`);let o,i,l=null;try{if(t.validationSplit!=null){const u=X5(r,a,t.validationSplit);o=u.trainXs,i=u.trainYs,l=[u.valXs,u.valYs]}else o=r,i=a;const c=yield this.model.fit(o,i,{epochs:t.epochs==null?20:t.epochs,validationData:l,batchSize:t.batchSize,callbacks:t.callback==null?void 0:[t.callback]});if(t.fineTuningEpochs!=null&&t.fineTuningEpochs>0){const u=yield this.fineTuningUsingTensors(t,o,i,l);return[c,u]}else return c}finally{be([r,a,o,i,l])}})}fineTuningUsingTfDatasets(t,s,r){return O(this,null,function*(){const a=this.secondLastBaseDenseLayer.trainable;this.secondLastBaseDenseLayer.trainable=!0;const o=t.fineTuningOptimizer==null?"sgd":t.fineTuningOptimizer;this.model.compile({loss:"categoricalCrossentropy",optimizer:o,metrics:["acc"]});const i=yield this.model.fitDataset(s,{epochs:t.fineTuningEpochs,validationData:r,callbacks:t.callback==null?void 0:[t.callback]});return this.secondLastBaseDenseLayer.trainable=a,i})}fineTuningUsingTensors(t,s,r,a){return O(this,null,function*(){const o=this.secondLastBaseDenseLayer.trainable;this.secondLastBaseDenseLayer.trainable=!0;const i=t.fineTuningOptimizer==null?"sgd":t.fineTuningOptimizer;this.model.compile({loss:"categoricalCrossentropy",optimizer:i,metrics:["acc"]});const l=yield this.model.fit(s,r,{epochs:t.fineTuningEpochs,validationData:a,batchSize:t.batchSize,callbacks:t.fineTuningCallback==null?void 0:[t.fineTuningCallback]});return this.secondLastBaseDenseLayer.trainable=o,l})}evaluate(t){return O(this,null,function*(){S(t.wordProbThresholds!=null&&t.wordProbThresholds.length>0,()=>"Received null or empty wordProbThresholds");const s=0;return S(this.words[s]===dl,()=>`Cannot perform evaluation when the first tag is not ${dl}`),P(()=>{const r=[];let a=0;const{xs:o,ys:i}=this.collectTransferDataAsTensors(t.windowHopRatio),l=i.argMax(-1).dataSync(),c=this.model.predict(o),u=Sn(Ae(c,[0,1],[c.shape[0],c.shape[1]-1]),-1),h=c.shape[0];for(let d=0;d<t.wordProbThresholds.length;++d){const p=t.wordProbThresholds[d],f=u.greater(fe(p)).dataSync();let m=0,g=0,y=0,b=0;for(let v=0;v<h;++v)l[v]===s?(m++,f[v]&&y++):(g++,f[v]&&b++);const w=y/m,x=b/g;r.push({probThreshold:p,fpr:w,tpr:x}),console.log(`ROC thresh=${p}: fpr=${w.toFixed(4)}, tpr=${x.toFixed(4)}`),d>0&&(a+=Math.abs(r[d-1].fpr-r[d].fpr)*(r[d-1].tpr+r[d].tpr)/2)}return{rocCurve:r,auc:a}})})}createTransferModelFromBaseModel(t){S(this.words!=null,()=>"No word example is available for tranfer-learning model of name "+this.name);const s=this.baseModel.layers;let r=s.length-2;for(;r>=0&&s[r].getClassName().toLowerCase()!=="dense";)r--;if(r<0)throw new Error("Cannot find a hidden dense layer in the base model.");this.secondLastBaseDenseLayer=s[r];const a=this.secondLastBaseDenseLayer.output;this.transferHead=ji(),this.transferHead.add(om({units:this.words.length,activation:"softmax",inputShape:a.shape.slice(1),name:"NewHeadDense"})),t&&this.transferHead.setWeights(t);const o=this.transferHead.apply(a);this.model=Su({inputs:this.baseModel.inputs,outputs:o})}modelInputShape(){return this.baseModel.inputs[0].shape}getMetadata(){return{tfjsSpeechCommandsVersion:yS,modelName:this.name,timeStamp:new Date().toISOString(),wordLabels:this.wordLabels()}}getAudioStream(t){t.timeData&&this.audioDataExtractor&&(this.audioDataExtractor.streamedTimeData=t.timeData),t.freqData&&this.audioDataExtractor&&(this.audioDataExtractor.streamedFreqData=t.freqData)}save(t){return O(this,null,function*(){const s=t!=null;if(t=t||bS(this.name),!s){const r=eg.localStorage.getItem(Qm),a=r==null?{}:JSON.parse(r);a[this.name]=this.getMetadata(),eg.localStorage.setItem(Qm,JSON.stringify(a))}return console.log(`Saving model to ${t}`),this.model.save(t)})}load(t){return O(this,null,function*(){const s=t!=null;if(t=t||bS(this.name),!s){const r=JSON.parse(eg.localStorage.getItem(Qm)||"");if(r==null||r[this.name]==null)throw new Error(`Cannot find metadata for transfer model named ${this.name}"`);this.words=r[this.name].wordLabels,console.log(`Loaded word list for model named ${this.name}: ${this.words}`)}this.model=yield Hi(t),console.log(`Loaded model from ${t}:`),this.model.summary()})}createTransfer(t){throw new Error("Creating transfer-learned recognizer from a transfer-learned recognizer is not supported.")}}function bS(n){return`${a8}${n}`}console.log("running MLModelWorker.ts");const l8=1,c8="conv_pw_13_relu",u8="out_relu",tg=.25,ng=.35,xS=224;class sg{constructor(){ce(this,"output",[]);ce(this,"predictionIdx",-1)}}class pl{constructor(){ce(this,"isTraining",!1);ce(this,"isTrained",!1);ce(this,"isPrediction",!1);ce(this,"i",0);ce(this,"iterations",0);ce(this,"modelFitTime",0);ce(this,"lossValues",[]);ce(this,"accuracyValues",[]);ce(this,"accuracy",0);ce(this,"isCorrect",[]);ce(this,"examples",[]);ce(this,"predictions",new sg);ce(this,"modelPredictTime",0)}}const wS=(n,e)=>{if(n===1){if(e!==.25&&e!==.5&&e!==.75&&e!==1)return console.warn("Invalid alpha. Options are: 0.25, 0.50, 0.75 or 1.00."),console.log("Loading model with alpha: ",tg.toFixed(2)),tg}else if(e!==.35&&e!==.5&&e!==.75&&e!==1)return console.warn("Invalid alpha. Options are: 0.35, 0.50, 0.75 or 1.00."),console.log("Loading model with alpha: ",ng.toFixed(2)),ng;return e},h8=n=>{if(n=n||{},n.checkpointUrl&&n.trainingLayer)return(n.alpha||n.version)&&console.warn("Checkpoint URL passed to modelOptions, alpha options are ignored"),[n.checkpointUrl,n.trainingLayer];if(n.version=n.version||l8,n.version===1){n.alpha=n.alpha||tg,n.alpha=wS(n.version,n.alpha),console.log(`Loading mobilenet ${n.version} and alpha ${n.alpha}`);let e=n.alpha.toFixed(2);return e==="1.00"&&(e="1.0"),[`https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_${e}_${xS}/model.json`,c8]}else{if(n.version===2)return n.alpha=n.alpha||ng,n.alpha=wS(n.version,n.alpha),console.log(`Loading mobilenet ${n.version} and alpha ${n.alpha}`),[`https://storage.googleapis.com/teachable-machine-models/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_${n.alpha.toFixed(2)}_${xS}_no_top/model.json`,u8];throw new Error(`MobileNet V${n.version} doesn't exist`)}},fa=class fa{constructor(){ce(this,"_mobileNetModel",null);ce(this,"_stopTrainingRequested",!1);ce(this,"_resetModelRequested",!1);ce(this,"_localIsTraining",!1);ce(this,"_localModel",null);ce(this,"_speechRecognizer",null);ce(this,"_trainingData",null);ce(this,"seed")}static getInstance(){return fa._instance||(fa._instance=new fa),fa._instance}loadTruncatedMobileNet(e){return O(this,null,function*(){const[t,s]=h8(e),r=yield Hi(t);if(e&&e.version===1){const a=r.getLayer(s),o=Su({inputs:r.inputs,outputs:a.output}),i=ji();return i.add(o),i.add(WF()),i}else{const a=r.getLayer(s),o=Su({inputs:r.inputs,outputs:a.output}),i=ji();return i.add(o),i.add(UF({})),i}})}loadMobilenet(){return O(this,null,function*(){const t=yield ZO("https://tfhub.dev/google/tfjs-model/imagenet/mobilenet_v3_small_100_224/feature_vector/5/default/1",{fromTFHub:!0});return t!==void 0&&P(()=>{t.predict(ct([1,cS,uS,3]))}),t})}resetModel(){this._mobileNetModel=null,this._stopTrainingRequested=!1,this._resetModelRequested=!1,this._localIsTraining=!1,this._localModel=null,this._trainingData=null;const e=new pl;self.postMessage({type:"status",payload:e})}onmessage(e){return O(this,null,function*(){switch(e.data.command){case"PREPARE_ENGINE":console.log("MLModelWorker: prepare engine"),this._mobileNetModel||(this._mobileNetModel=yield this.loadTruncatedMobileNet());break;case"AUDIO_START_LISTENING":this._speechRecognizer&&this._speechRecognizer.listen(y=>O(this,null,function*(){let b=-1,w=-1;const x=new sg;for(let $=0;$<y.scores.length;$++){const C=y.scores[$];C>w&&(x.output.push({label:this._trainingData.classes.sort((k,T)=>k.name<T.name?-1:k.name>T.name?1:0)[$].name,confidence:C}),b=$)}x.predictionIdx=b;const v=new pl;v.isTrained=!0,v.isPrediction=!0,v.predictions=x,self.postMessage({type:"status",payload:v})}));break;case"STOP_AUDIO_LISTENING":this._speechRecognizer&&this._speechRecognizer.stopListening();break;case"TRAIN":this._stopTrainingRequested=!1,this._resetModelRequested=!1;const t=e.data.modelType;t==="image-device"?yield this.train(e):t==="audio"&&(yield this.trainAudio(e));break;case"PREDICT":yield this.predict(e);break;case"STOP_TRAINING":this._localIsTraining&&(this._stopTrainingRequested=!0);break;case"RESET_MODEL":this._localIsTraining?(this._stopTrainingRequested=!0,this._resetModelRequested=!0):this.resetModel();break;case"SAVE_MODEL":if(this._localModel){let y=null,b=null,w=null;if(e.data.modelType==="audio"){const x=this._localModel.layers[this._localModel.layers.length-1];b=x.weights.map(C=>C.read().dataSync()),w=x.weights;const $={config:x.getConfig(),weights:b};y=JSON.stringify($)}else e.data.modelType==="image-device"&&(this._localModel=this._localModel,y=this._localModel.toJSON(),b=this._localModel.weights.map(x=>x.read().dataSync()),w=this._localModel.weights);self.postMessage({type:"save_model",payload:{modelJSON:y,weights:{weightBuffers:b,weightInfo:w}}})}break;case"LOAD_TM_MODEL":const{tmModelUrl:s}=e.data;if(this._localModel&&this._localModel.dispose(),s){console.debug("MLModelWorker: Loading TM model...");const b=yield(yield fetch(s.replace("model.json","metadata.json"))).json();this._localModel=yield Hi(s),console.log("modelMetadataJson",b),console.log("modelMetadataJson.labels",b.labels),console.log("modelMetadataJson.wordLabels",b.wordLabels);const w=b.labels?"image-device":"audio";if(console.log("modelType",w),w==="audio"){const x=new fo;yield x.ensureModelLoaded(),this._speechRecognizer=x.createTransfer("modelNameExample"),this._speechRecognizer.words=b.wordLabels,this._speechRecognizer.model=this._localModel}self.postMessage({type:"load_model",payload:{}})}break;case"LOAD_AUDIO_MODEL":this._localModel&&this._localModel.dispose();const{modelJSON:r,weightBuffers:a,weightInfo:o,audioAlphabeticalWords:i}=e.data,l=new fo;yield l.ensureModelLoaded(),this._speechRecognizer=l.createTransfer("modelNameExample");const c=a.map((y,b)=>{const w=o[b].shape,x=o[b].dtype;return Bt(y,w,x)});this._speechRecognizer.words=i,this._speechRecognizer.createTransferModelFromBaseModel(c),self.postMessage({type:"load_model",payload:{}});break;case"LOAD_MODEL":this._localModel&&this._localModel.dispose();const{modelJSON:u,weightBuffers:h,weightInfo:d}=e.data;console.debug("MLModelWorker: Loading MM model...");const p=JSON.parse(u);console.debug("MLModelWorker: Loading model...",e.data);const f=yield vF(p);console.log("model",f);const m=h.map((y,b)=>{const w=d[b].shape,x=d[b].dtype;return Bt(y,w,x)});console.debug("MLModelWorker: Setting weights...",m),f.setWeights(m),this._localModel=f,console.debug("MLModelWorker: Model loaded!"),self.postMessage({type:"load_model",payload:{}});break;case"SET_TRAINING_DATA":this._trainingData=e.data.trainingData;break;case"STREAM_AUDIO":const{streamedAudioData:g}=e.data;this._speechRecognizer&&this._speechRecognizer.getAudioStream(g);break;default:console.log("MLModelWorker: Unknown command. Event: "+JSON.stringify(e))}})}imgToTensor(e,t){return P(()=>{let s=EA(e);return t&&(s=Pn.resizeBilinear(s,t)),this.cropImage(s).expandDims(0).toFloat().div(fe(127)).sub(fe(1))})}cropImage(e){const t=Math.min(e.shape[0],e.shape[1]),r=e.shape[0]/2-t/2,o=e.shape[1]/2-t/2;return e.slice([r,o,0],[t,t,3])}convertUtilsPixelsToTensor(e){const{pixelsBase64:t,height:s,width:r,depth:a}=e,o=Uint8Array.from(atob(t),u=>u.charCodeAt(0)),i=Ua(o,[s,r,a],"int32"),c=Pn.resizeBilinear(i,[uS,cS],!0).div(255);return this._mobileNetModel.predict(c.expandDims())}trainAudio(e){return O(this,null,function*(){console.log("event",e);const t=e.data.trainingData;if(!t)return console.log("MLModelWorker: No audio training data!"),!1;const s=t;this._trainingData=s;const r=new fo;this._localIsTraining=!0;const a=new pl;a.isTraining=!0,yield r.ensureModelLoaded(),console.log("MLModelWorker: Speech model loaded!"),console.log("baseRecognizer parameters:",r.params()),console.log("baseRecognizer input shape:",r.modelInputShape());const o=r.createTransfer("modelNameExample");yield o.addExamples(s.classes),console.log(o.countExamples());const i=e.data.trainIterations,l=performance.now(),c=[[]],u=[[]];return yield o.train({epochs:i,callback:{onEpochEnd:(h,d)=>O(this,null,function*(){const f=(performance.now()-l)/1e3,m=d.loss,g=d.acc;c[0].push({x:h,y:m}),u[0].push({x:h,y:g}),a.i=h,a.iterations=i,a.modelFitTime=f,a.lossValues=c,a.accuracyValues=u,self.postMessage({type:"status",payload:a})}),onTrainEnd:h=>O(this,null,function*(){this._localIsTraining=!1,this._resetModelRequested?this.resetModel():(a.isTraining=!1,a.isTrained=!0,self.postMessage({type:"status",payload:a})),this._speechRecognizer=o,this._localModel=o.model})}}),!0})}train(e){return O(this,null,function*(){if(!this._mobileNetModel)if(this._mobileNetModel=yield this.loadTruncatedMobileNet(),this._mobileNetModel)console.log("MLModelWorker: mobilenet loaded");else return console.log("MLModelWorker: MobileNet not loaded!"),!1;if(!e.data.trainingData)return console.log("MLModelWorker: No training data!"),!1;const t=e.data.trainingData;if(t.classes.length===0)return console.log("MLModelWorker: No classes to train!"),!1;if(t.classes.some(w=>w.samples.length===0))return console.log("MLModelWorker: At least one class has no samples!"),!1;this._localIsTraining=!0;const s=new pl;s.isTraining=!0,this._trainingData=t;const r=t.classes.flatMap(w=>w.samples.map(({tensorData:x,tensorShape:v})=>{const $=Bt(x,v),C=this._mobileNetModel.predict($),k=C.dataSync();return $.dispose(),C.dispose(),k})),a=t.classes.flatMap((w,x)=>w.samples.map(()=>x));go(r,a);const o=ft(a,"int32"),i=Vr(o,t.classes.length),l=Wt(r),c=this._mobileNetModel.outputs[0].shape.slice(1),u=U(c),h=E_({});this._localModel=ji(),this._localModel.add(om({inputShape:[u],units:100,activation:"relu",kernelInitializer:h,useBias:!0})),this._localModel.add(om({kernelInitializer:h,useBias:!1,activation:"softmax",units:t.classes.length}));const d=.001,p=qr.adam(d);this._localModel.compile({optimizer:p,loss:"categoricalCrossentropy",metrics:["accuracy"]});const{trainIterations:f,batchSize:m}=e.data,g=[[]],y=[[]];for(let w=0;w<f&&!this._stopTrainingRequested;++w){const x=performance.now(),v=yield this._localModel.fit(l,i,{epochs:1,batchSize:m,yieldEvery:"epoch"}),C=(performance.now()-x)/1e3,k=v.history.loss[0],T=v.history.acc[0];g[0].push({x:w,y:k}),y[0].push({x:w,y:T}),s.i=w,s.iterations=f,s.modelFitTime=C,s.lossValues=g,s.accuracyValues=y,self.postMessage({type:"status",payload:s})}o.dispose(),i.dispose(),l.dispose(),this._localIsTraining=!1,this._resetModelRequested?this.resetModel():(s.isTraining=!1,s.isTrained=!0,self.postMessage({type:"status",payload:s}));const b=ji();return b.add(this._mobileNetModel),b.add(this._localModel),this._localModel=b,p.dispose(),!0})}predict(e){return O(this,null,function*(){const{imagePixels:t}=e.data;if(this._mobileNetModel===null)return console.log("MLModelWorker: MobileNet v3 not loaded!"),!1;if(this._localModel===null)return console.log("MLModelWorker: Local model not loaded!"),!1;if(this._trainingData===null)return console.log("MLModelWorker: Training data not loaded!"),!1;if(!t)return console.log("MLModelWorker: No image data!"),!1;const s=performance.now(),r=t.tensorShape,a=Bt(t.tensorData,r),o=P(()=>this._localModel.predict(a)),i=yield o.data();let l=-1,c=-1;const u=new sg;for(let f=0;f<i.length;f++)i[f]>c&&(u.output.push({label:this._trainingData.classes[f].name,confidence:i[f]}),l=f);u.predictionIdx=l;const d=(performance.now()-s)/1e3,p=new pl;p.isTrained=!0,p.isPrediction=!0,p.modelPredictTime=d,p.predictions=u,be(o),self.postMessage({type:"status",payload:p})})}};ce(fa,"_instance",null);let rg=fa;self.onmessage=n=>O(this,null,function*(){rg.getInstance().onmessage(n)})})();
|
|
22174
|
+
*/const Qm="tfjs-speech-commands-saved-model-metadata",a8="indexeddb://tfjs-speech-commands-model/";let eg={localStorage:typeof window=="undefined"?null:window.localStorage};function o8(n){return n.split(".").slice(0,2).join(".")}const yS="0.5.4",uh=.25,mo=class mo{constructor(e,t,s){ce(this,"MODEL_URL_PREFIX",`https://storage.googleapis.com/tfjs-models/tfjs/speech-commands/v${o8(yS)}/browser_fft`);ce(this,"SAMPLE_RATE_HZ",44100);ce(this,"FFT_SIZE",1024);ce(this,"DEFAULT_SUPPRESSION_TIME_MILLIS",0);ce(this,"model");ce(this,"modelWithEmbeddingOutput");ce(this,"vocabulary");ce(this,"parameters");ce(this,"words");ce(this,"streaming",!1);ce(this,"nonBatchInputShape");ce(this,"elementsPerExample");ce(this,"audioDataExtractor");ce(this,"transferRecognizers",{});ce(this,"modelArtifactsOrURL");ce(this,"metadataOrURL");ce(this,"secondLastBaseDenseLayer");S(t==null&&s==null||t!=null&&s!=null,()=>"modelURL and metadataURL must be both provided or both not provided."),t==null?(e==null?e=mo.DEFAULT_VOCABULARY_NAME:S(mo.VALID_VOCABULARY_NAMES.indexOf(e)!==-1,()=>`Invalid vocabulary name: '${e}'`),this.vocabulary=e,this.modelArtifactsOrURL=`${this.MODEL_URL_PREFIX}/${this.vocabulary}/model.json`,this.metadataOrURL=`${this.MODEL_URL_PREFIX}/${this.vocabulary}/metadata.json`):(S(e==null,()=>"vocabulary name must be null or undefined when modelURL is provided"),this.modelArtifactsOrURL=t,this.metadataOrURL=s),this.parameters={sampleRateHz:this.SAMPLE_RATE_HZ,fftSize:this.FFT_SIZE}}listen(e,t){return O(this,null,function*(){if(this.streaming)throw new Error("Cannot start streaming again when streaming is ongoing.");console.log("in listen: ensuring model is loaded"),yield this.ensureModelLoaded(),t==null&&(t={});let s=t.probabilityThreshold==null?0:t.probabilityThreshold;if(t.includeEmbedding&&(s=0),S(s>=0&&s<=1,()=>`Invalid probabilityThreshold value: ${s}`),t.invokeCallbackOnNoiseAndUnknown==null||t.invokeCallbackOnNoiseAndUnknown,t.includeEmbedding,t.suppressionTimeMillis<0)throw new Error(`suppressionTimeMillis is expected to be >= 0, but got ${t.suppressionTimeMillis}`);const r=t.overlapFactor==null?.5:t.overlapFactor;S(r>=0&&r<1,()=>`Expected overlapFactor to be >= 0 and < 1, but got ${r}`);const a=(i,l)=>O(this,null,function*(){const c=hl(i);let u,h;t.includeEmbedding?(yield this.ensureModelWithEmbeddingOutputCreated(),[u,h]=this.modelWithEmbeddingOutput.predict(c)):u=this.model.predict(c);const d=yield u.data(),p=u.argMax(-1);(yield p.data())[0];const f=Math.max(...d);if(be([u,p,c]),f<s)return!1;{let m;t.includeSpectrogram&&(m={data:yield i.data(),frameSize:this.nonBatchInputShape[1]});let g=!0;return e({scores:d,spectrogram:m,embedding:h}),g}}),o=t.suppressionTimeMillis==null?this.DEFAULT_SUPPRESSION_TIME_MILLIS:t.suppressionTimeMillis;this.audioDataExtractor=new Jm({sampleRateHz:this.parameters.sampleRateHz,numFramesPerSpectrogram:this.nonBatchInputShape[0],columnTruncateLength:this.nonBatchInputShape[1],suppressionTimeMillis:o,spectrogramCallback:a,overlapFactor:r}),yield this.audioDataExtractor.startStreamedInput(),this.streaming=!0})}ensureModelLoaded(){return O(this,null,function*(){if(this.model!=null)return;yield this.ensureMetadataLoaded();let e;if(typeof this.modelArtifactsOrURL=="string"?e=yield Hi(this.modelArtifactsOrURL):e=yield Hi(Pb(this.modelArtifactsOrURL.modelTopology||{},this.modelArtifactsOrURL.weightSpecs,this.modelArtifactsOrURL.weightData)),e.inputs.length!==1)throw new Error(`Expected model to have 1 input, but got a model with ${e.inputs.length} inputs`);if(e.inputs[0].shape.length!==4)throw new Error(`Expected model to have an input shape of rank 4, but got an input shape of rank ${e.inputs[0].shape.length}`);if(e.inputs[0].shape[3]!==1)throw new Error(`Expected model to have an input shape with 1 as the last dimension, but got input shape${JSON.stringify(e.inputs[0].shape[3])}}`);const t=e.outputShape;if(t.length!==2)throw new Error(`Expected loaded model to have an output shape of rank 2,but received shape ${JSON.stringify(t)}`);if(t[1]!==this.words.length)throw new Error(`Mismatch between the last dimension of model's output shape (${t[1]}) and number of words (${this.words.length}).`);this.model=e,this.freezeModel(),this.nonBatchInputShape=e.inputs[0].shape.slice(1),this.elementsPerExample=1,e.inputs[0].shape.slice(1).forEach(a=>this.elementsPerExample*=a),this.warmUpModel();const s=this.parameters.fftSize/this.parameters.sampleRateHz*1e3,r=e.inputs[0].shape[1];this.parameters.spectrogramDurationMillis=r*s})}ensureModelWithEmbeddingOutputCreated(){return O(this,null,function*(){if(this.modelWithEmbeddingOutput!=null)return;yield this.ensureModelLoaded();let e;for(let t=this.model.layers.length-2;t>=0;--t)if(this.model.layers[t].getClassName()==="Dense"){e=this.model.layers[t];break}if(e==null)throw new Error("Failed to find second last dense layer in the original model.");this.modelWithEmbeddingOutput=Su({inputs:this.model.inputs,outputs:[this.model.outputs[0],e.output]})})}warmUpModel(){P(()=>{const e=ct([1].concat(this.nonBatchInputShape));for(let t=0;t<3;++t)this.model.predict(e)})}ensureMetadataLoaded(){return O(this,null,function*(){if(this.words!=null)return;const e=typeof this.metadataOrURL=="string"?yield G5(this.metadataOrURL):this.metadataOrURL;if(e.wordLabels==null){const t=e.words;if(t==null)throw new Error('Cannot find field "words" or "wordLabels" in metadata JSON file');this.words=t}else this.words=e.wordLabels})}stopListening(){return O(this,null,function*(){yield this.audioDataExtractor.stop(),this.streaming=!1})}isListening(){return this.streaming}wordLabels(){return this.words}params(){return this.parameters}modelInputShape(){if(this.model==null)throw new Error("Model has not been loaded yet. Load model by calling ensureModelLoaded(), recognize(), or listen().");return this.model.inputs[0].shape}recognize(e,t){return O(this,null,function*(){t==null&&(t={}),yield this.ensureModelLoaded(),e==null&&(e=(yield this.recognizeOnline()).data);let s,r,a;if(e instanceof Oe)this.checkInputTensorShape(e),r=e,s=e.shape[0];else{if(e.length%this.elementsPerExample)throw new Error(`The length of the input Float32Array ${e.length} is not divisible by the number of tensor elements per per example expected by the model ${this.elementsPerExample}.`);s=e.length/this.elementsPerExample,r=ab(e,[s].concat(this.nonBatchInputShape))}const o={scores:[]};if(t.includeEmbedding){yield this.ensureModelWithEmbeddingOutputCreated();const i=this.modelWithEmbeddingOutput.predict(r);a=i[0],o.embedding=i[1]}else a=this.model.predict(r);if(s===1)o.scores=yield a.data();else{const i=Cn(a),l=i.map(c=>c.data());o.scores=yield Promise.all(l),be(i)}return t.includeSpectrogram&&(o.spectrogram={data:e instanceof Oe?yield e.data():e,frameSize:this.nonBatchInputShape[1]}),be(a),o})}recognizeOnline(){return O(this,null,function*(){return new Promise((e,t)=>{const s=r=>O(this,null,function*(){const a=hl(r);return yield this.audioDataExtractor.stop(),e({data:yield a.data(),frameSize:this.nonBatchInputShape[1]}),a.dispose(),!1});this.audioDataExtractor=new Jm({sampleRateHz:this.parameters.sampleRateHz,numFramesPerSpectrogram:this.nonBatchInputShape[0],columnTruncateLength:this.nonBatchInputShape[1],suppressionTimeMillis:0,spectrogramCallback:s,overlapFactor:0})})})}createTransfer(e){if(this.model==null)throw new Error("Model has not been loaded yet. Load model by calling ensureModelLoaded(), recognizer(), or listen().");S(e!=null&&typeof e=="string"&&e.length>1,()=>`Expected the name for a transfer-learning recognized to be a non-empty string, but got ${JSON.stringify(e)}`),S(this.transferRecognizers[e]==null,()=>`There is already a transfer-learning model named '${e}'`);const t=new i8(e,this.parameters,this.model);return this.transferRecognizers[e]=t,t}freezeModel(){for(const e of this.model.layers)e.trainable=!1}checkInputTensorShape(e){const t=this.model.inputs[0].shape.length;if(e.shape.length!==t)throw new Error(`Expected input Tensor to have rank ${t}, but got rank ${e.shape.length} that differs `);const s=e.shape.slice(1),r=this.model.inputs[0].shape.slice(1);if(!Ee(s,r))throw new Error(`Expected input to have shape [null,${r}], but got shape [null,${s}]`)}};ce(mo,"VALID_VOCABULARY_NAMES",["18w","directional4w"]),ce(mo,"DEFAULT_VOCABULARY_NAME","18w");let fo=mo;class i8 extends fo{constructor(t,s,r){super();ce(this,"dataset");ce(this,"transferHead");this.name=t,this.parameters=s,this.baseModel=r,S(t!=null&&typeof t=="string"&&t.length>0,()=>`The name of a transfer model must be a non-empty string, but got ${JSON.stringify(t)}`),this.nonBatchInputShape=this.baseModel.inputs[0].shape.slice(1),this.words=null,this.dataset=new gS}addExamples(t){return O(this,null,function*(){for(const s of t)for(const r of s.samples)console.log(`class ${s.name}, sample ${r}, has timeDataQueue length ${r.timeDataQueue.length}, and freqDataQueue length ${r.freqDataQueue.length}`),yield this.collectExample(s.name,r,{durationMultiplier:1,includeRawAudio:!0})})}collectExample(t,s,r){return O(this,null,function*(){if(S(!this.streaming,()=>"Cannot start collection of transfer-learning example because a streaming recognition or transfer-learning example collection is ongoing"),S(t!=null&&typeof t=="string"&&t.length>0,()=>"Must provide a non-empty string when collecting transfer-learning example"),r==null&&(r={}),r.durationMultiplier!=null&&r.durationSec!=null)throw new Error("durationMultiplier and durationSec are mutually exclusive, but are both specified.");let a;if(r.durationSec!=null){S(r.durationSec>0,()=>`Expected durationSec to be > 0, but got ${r.durationSec}`);const l=this.parameters.fftSize/this.parameters.sampleRateHz;a=Math.ceil(r.durationSec/l)}else r.durationMultiplier!=null?(S(r.durationMultiplier>=1,()=>`Expected duration multiplier to be >= 1, but got ${r.durationMultiplier}`),a=Math.round(this.nonBatchInputShape[0]*r.durationMultiplier)):a=this.nonBatchInputShape[0];r.snippetDurationSec!=null&&(S(r.snippetDurationSec>0,()=>`snippetDurationSec is expected to be > 0, but got ${r.snippetDurationSec}`),S(r.onSnippet!=null,()=>"onSnippet must be provided if snippetDurationSec is provided.")),r.onSnippet!=null&&S(r.snippetDurationSec!=null,()=>"snippetDurationSec must be provided if onSnippet is provided.");const i=this.parameters.fftSize/this.parameters.sampleRateHz*a;return this.streaming=!0,new Promise(l=>{const c=r.snippetDurationSec==null?1:r.snippetDurationSec/i,u=1-c,h=Math.round(1/c);let d=0,p=-1;const f=[],m=(g,y)=>O(this,null,function*(){if(r.onSnippet==null){const b=hl(g);this.dataset.addExample({label:t,spectrogram:{data:yield b.data(),frameSize:this.nonBatchInputShape[1]},rawAudio:r.includeRawAudio?{data:yield y.data(),sampleRateHz:this.audioDataExtractor.sampleRateHz}:void 0}),b.dispose(),yield this.audioDataExtractor.stop(),this.streaming=!1,this.collateTransferWords(),l({data:yield g.data(),frameSize:this.nonBatchInputShape[1]})}else{const b=yield g.data();p===-1&&(p=b.length);let w=p-1;for(;b[w]!==0&&w>=0;)w--;const x=p-w-1;p=w+1;const v=b.slice(b.length-x,b.length);if(f.push(v),r.onSnippet!=null&&r.onSnippet({data:v,frameSize:this.nonBatchInputShape[1]}),d++===h){yield this.audioDataExtractor.stop(),this.streaming=!1,this.collateTransferWords();const C={data:H5(j5(f)),frameSize:this.nonBatchInputShape[1]};this.dataset.addExample({label:t,spectrogram:C,rawAudio:r.includeRawAudio?{data:yield y.data(),sampleRateHz:this.audioDataExtractor.sampleRateHz}:void 0}),l(C)}}return!1});this.audioDataExtractor=new Jm({sampleRateHz:this.parameters.sampleRateHz,numFramesPerSpectrogram:a,columnTruncateLength:this.nonBatchInputShape[1],suppressionTimeMillis:0,spectrogramCallback:m,overlapFactor:u,includeRawAudio:r.includeRawAudio}),this.audioDataExtractor.start(s)})})}clearExamples(){S(this.words!=null&&this.words.length>0&&!this.dataset.empty(),()=>`No transfer learning examples exist for model name ${this.name}`),this.dataset.clear(),this.words=null}countExamples(){if(this.dataset.empty())throw new Error(`No examples have been collected for transfer-learning model named '${this.name}' yet.`);return this.dataset.getExampleCounts()}getExamples(t){return this.dataset.getExamples(t)}setExampleKeyFrameIndex(t,s){this.dataset.setExampleKeyFrameIndex(t,s)}removeExample(t){this.dataset.removeExample(t),this.collateTransferWords()}isDatasetEmpty(){return this.dataset.empty()}loadExamples(t,s=!1){const r=new gS(t);s&&this.clearExamples();const a=r.getVocabulary();for(const o of a){const i=r.getExamples(o);for(const l of i)this.dataset.addExample(l.example)}this.collateTransferWords()}serializeExamples(t){return this.dataset.serialize(t)}collateTransferWords(){this.words=this.dataset.getVocabulary()}collectTransferDataAsTensors(t,s){const r=this.nonBatchInputShape[0];t=t||uh;const a=Math.round(t*r),o=this.dataset.getData(void 0,cg({numFrames:r,hopFrames:a},s));return{xs:o.xs,ys:o.ys}}collectTransferDataAsTfDataset(t,s=.15,r=32,a){const o=this.nonBatchInputShape[0];t=t||uh;const i=Math.round(t*o);return this.dataset.getData(void 0,cg({numFrames:o,hopFrames:i,getDataset:!0,datasetBatchSize:r,datasetValidationSplit:s},a))}train(t){return O(this,null,function*(){S(this.words!=null&&this.words.length>0,()=>`Cannot train transfer-learning model '${this.name}' because no transfer learning example has been collected.`),S(this.words.length>1,()=>`Cannot train transfer-learning model '${this.name}' because only 1 word label ('${JSON.stringify(this.words)}') has been collected for transfer learning. Requires at least 2.`),t.fineTuningEpochs!=null&&S(t.fineTuningEpochs>=0&&Number.isInteger(t.fineTuningEpochs),()=>`If specified, fineTuningEpochs must be a non-negative integer, but received ${t.fineTuningEpochs}`),t==null&&(t={}),this.model==null&&this.createTransferModelFromBaseModel(),this.secondLastBaseDenseLayer.trainable=!1,this.model.compile({loss:"categoricalCrossentropy",optimizer:t.optimizer||"sgd",metrics:["acc"]});const s=t.fitDatasetDurationMillisThreshold==null?6e4:t.fitDatasetDurationMillisThreshold;return this.dataset.durationMillis()>s?(console.log(`Detected large dataset: total duration = ${this.dataset.durationMillis()} ms > ${s} ms. Training transfer model using fitDataset() instead of fit()`),this.trainOnDataset(t)):this.trainOnTensors(t)})}trainOnDataset(t){return O(this,null,function*(){S(t.epochs>0,()=>"Invalid config!.epochs");const s=t.batchSize==null?32:t.batchSize,r=t.windowHopRatio||uh,[a,o]=this.collectTransferDataAsTfDataset(r,t.validationSplit,s,{augmentByMixingNoiseRatio:t.augmentByMixingNoiseRatio}),i=dt(),l=yield this.model.fitDataset(a,{epochs:t.epochs,validationData:t.validationSplit>0?o:void 0,callbacks:t.callback==null?void 0:[t.callback]});if(console.log(`fitDataset() took ${(dt()-i).toFixed(2)} ms`),t.fineTuningEpochs!=null&&t.fineTuningEpochs>0){const c=dt(),u=yield this.fineTuningUsingTfDatasets(t,a,o);return console.log(`fitDataset() (fine-tuning) took ${(dt()-c).toFixed(2)} ms`),[l,u]}else return l})}trainOnTensors(t){return O(this,null,function*(){const s=t.windowHopRatio||uh,{xs:r,ys:a}=this.collectTransferDataAsTensors(s,{augmentByMixingNoiseRatio:t.augmentByMixingNoiseRatio});console.log(`Training data: xs.shape = ${r.shape}, ys.shape = ${a.shape}`);let o,i,l=null;try{if(t.validationSplit!=null){const u=X5(r,a,t.validationSplit);o=u.trainXs,i=u.trainYs,l=[u.valXs,u.valYs]}else o=r,i=a;const c=yield this.model.fit(o,i,{epochs:t.epochs==null?20:t.epochs,validationData:l,batchSize:t.batchSize,callbacks:t.callback==null?void 0:[t.callback]});if(t.fineTuningEpochs!=null&&t.fineTuningEpochs>0){const u=yield this.fineTuningUsingTensors(t,o,i,l);return[c,u]}else return c}finally{be([r,a,o,i,l])}})}fineTuningUsingTfDatasets(t,s,r){return O(this,null,function*(){const a=this.secondLastBaseDenseLayer.trainable;this.secondLastBaseDenseLayer.trainable=!0;const o=t.fineTuningOptimizer==null?"sgd":t.fineTuningOptimizer;this.model.compile({loss:"categoricalCrossentropy",optimizer:o,metrics:["acc"]});const i=yield this.model.fitDataset(s,{epochs:t.fineTuningEpochs,validationData:r,callbacks:t.callback==null?void 0:[t.callback]});return this.secondLastBaseDenseLayer.trainable=a,i})}fineTuningUsingTensors(t,s,r,a){return O(this,null,function*(){const o=this.secondLastBaseDenseLayer.trainable;this.secondLastBaseDenseLayer.trainable=!0;const i=t.fineTuningOptimizer==null?"sgd":t.fineTuningOptimizer;this.model.compile({loss:"categoricalCrossentropy",optimizer:i,metrics:["acc"]});const l=yield this.model.fit(s,r,{epochs:t.fineTuningEpochs,validationData:a,batchSize:t.batchSize,callbacks:t.fineTuningCallback==null?void 0:[t.fineTuningCallback]});return this.secondLastBaseDenseLayer.trainable=o,l})}evaluate(t){return O(this,null,function*(){S(t.wordProbThresholds!=null&&t.wordProbThresholds.length>0,()=>"Received null or empty wordProbThresholds");const s=0;return S(this.words[s]===dl,()=>`Cannot perform evaluation when the first tag is not ${dl}`),P(()=>{const r=[];let a=0;const{xs:o,ys:i}=this.collectTransferDataAsTensors(t.windowHopRatio),l=i.argMax(-1).dataSync(),c=this.model.predict(o),u=Sn(Ae(c,[0,1],[c.shape[0],c.shape[1]-1]),-1),h=c.shape[0];for(let d=0;d<t.wordProbThresholds.length;++d){const p=t.wordProbThresholds[d],f=u.greater(pe(p)).dataSync();let m=0,g=0,y=0,b=0;for(let v=0;v<h;++v)l[v]===s?(m++,f[v]&&y++):(g++,f[v]&&b++);const w=y/m,x=b/g;r.push({probThreshold:p,fpr:w,tpr:x}),console.log(`ROC thresh=${p}: fpr=${w.toFixed(4)}, tpr=${x.toFixed(4)}`),d>0&&(a+=Math.abs(r[d-1].fpr-r[d].fpr)*(r[d-1].tpr+r[d].tpr)/2)}return{rocCurve:r,auc:a}})})}createTransferModelFromBaseModel(t){S(this.words!=null,()=>"No word example is available for tranfer-learning model of name "+this.name);const s=this.baseModel.layers;let r=s.length-2;for(;r>=0&&s[r].getClassName().toLowerCase()!=="dense";)r--;if(r<0)throw new Error("Cannot find a hidden dense layer in the base model.");this.secondLastBaseDenseLayer=s[r];const a=this.secondLastBaseDenseLayer.output;this.transferHead=ji(),this.transferHead.add(om({units:this.words.length,activation:"softmax",inputShape:a.shape.slice(1),name:"NewHeadDense"})),t&&this.transferHead.setWeights(t);const o=this.transferHead.apply(a);this.model=Su({inputs:this.baseModel.inputs,outputs:o})}modelInputShape(){return this.baseModel.inputs[0].shape}getMetadata(){return{tfjsSpeechCommandsVersion:yS,modelName:this.name,timeStamp:new Date().toISOString(),wordLabels:this.wordLabels()}}getAudioStream(t){t.timeData&&this.audioDataExtractor&&(this.audioDataExtractor.streamedTimeData=t.timeData),t.freqData&&this.audioDataExtractor&&(this.audioDataExtractor.streamedFreqData=t.freqData)}save(t){return O(this,null,function*(){const s=t!=null;if(t=t||bS(this.name),!s){const r=eg.localStorage.getItem(Qm),a=r==null?{}:JSON.parse(r);a[this.name]=this.getMetadata(),eg.localStorage.setItem(Qm,JSON.stringify(a))}return console.log(`Saving model to ${t}`),this.model.save(t)})}load(t){return O(this,null,function*(){const s=t!=null;if(t=t||bS(this.name),!s){const r=JSON.parse(eg.localStorage.getItem(Qm)||"");if(r==null||r[this.name]==null)throw new Error(`Cannot find metadata for transfer model named ${this.name}"`);this.words=r[this.name].wordLabels,console.log(`Loaded word list for model named ${this.name}: ${this.words}`)}this.model=yield Hi(t),console.log(`Loaded model from ${t}:`),this.model.summary()})}createTransfer(t){throw new Error("Creating transfer-learned recognizer from a transfer-learned recognizer is not supported.")}}function bS(n){return`${a8}${n}`}console.log("running MLModelWorker.ts");const l8=1,c8="conv_pw_13_relu",u8="out_relu",tg=.25,ng=.35,xS=224;class sg{constructor(){ce(this,"output",[]);ce(this,"predictionIdx",-1)}}class pl{constructor(){ce(this,"isTraining",!1);ce(this,"isTrained",!1);ce(this,"isPrediction",!1);ce(this,"i",0);ce(this,"iterations",0);ce(this,"modelFitTime",0);ce(this,"lossValues",[]);ce(this,"accuracyValues",[]);ce(this,"accuracy",0);ce(this,"isCorrect",[]);ce(this,"examples",[]);ce(this,"predictions",new sg);ce(this,"modelPredictTime",0)}}const wS=(n,e)=>{if(n===1){if(e!==.25&&e!==.5&&e!==.75&&e!==1)return console.warn("Invalid alpha. Options are: 0.25, 0.50, 0.75 or 1.00."),console.log("Loading model with alpha: ",tg.toFixed(2)),tg}else if(e!==.35&&e!==.5&&e!==.75&&e!==1)return console.warn("Invalid alpha. Options are: 0.35, 0.50, 0.75 or 1.00."),console.log("Loading model with alpha: ",ng.toFixed(2)),ng;return e},h8=n=>{if(n=n||{},n.checkpointUrl&&n.trainingLayer)return(n.alpha||n.version)&&console.warn("Checkpoint URL passed to modelOptions, alpha options are ignored"),[n.checkpointUrl,n.trainingLayer];if(n.version=n.version||l8,n.version===1){n.alpha=n.alpha||tg,n.alpha=wS(n.version,n.alpha),console.log(`Loading mobilenet ${n.version} and alpha ${n.alpha}`);let e=n.alpha.toFixed(2);return e==="1.00"&&(e="1.0"),[`https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_${e}_${xS}/model.json`,c8]}else{if(n.version===2)return n.alpha=n.alpha||ng,n.alpha=wS(n.version,n.alpha),console.log(`Loading mobilenet ${n.version} and alpha ${n.alpha}`),[`https://storage.googleapis.com/teachable-machine-models/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_${n.alpha.toFixed(2)}_${xS}_no_top/model.json`,u8];throw new Error(`MobileNet V${n.version} doesn't exist`)}},fa=class fa{constructor(){ce(this,"_mobileNetModel",null);ce(this,"_stopTrainingRequested",!1);ce(this,"_resetModelRequested",!1);ce(this,"_localIsTraining",!1);ce(this,"_localModel",null);ce(this,"_speechRecognizer",null);ce(this,"_trainingData",null);ce(this,"seed")}static getInstance(){return fa._instance||(fa._instance=new fa),fa._instance}loadTruncatedMobileNet(e){return O(this,null,function*(){const[t,s]=h8(e),r=yield Hi(t);if(e&&e.version===1){const a=r.getLayer(s),o=Su({inputs:r.inputs,outputs:a.output}),i=ji();return i.add(o),i.add(WF()),i}else{const a=r.getLayer(s),o=Su({inputs:r.inputs,outputs:a.output}),i=ji();return i.add(o),i.add(UF({})),i}})}loadMobilenet(){return O(this,null,function*(){const t=yield ZO("https://tfhub.dev/google/tfjs-model/imagenet/mobilenet_v3_small_100_224/feature_vector/5/default/1",{fromTFHub:!0});return t!==void 0&&P(()=>{t.predict(ct([1,cS,uS,3]))}),t})}resetModel(){this._mobileNetModel=null,this._stopTrainingRequested=!1,this._resetModelRequested=!1,this._localIsTraining=!1,this._localModel=null,this._trainingData=null;const e=new pl;self.postMessage({type:"status",payload:e})}onmessage(e){return O(this,null,function*(){switch(e.data.command){case"PREPARE_ENGINE":console.log("MLModelWorker: prepare engine"),this._mobileNetModel||(this._mobileNetModel=yield this.loadTruncatedMobileNet());break;case"AUDIO_START_LISTENING":this._speechRecognizer&&this._speechRecognizer.listen(y=>O(this,null,function*(){let b=-1,w=-1;const x=new sg;for(let $=0;$<y.scores.length;$++){const C=y.scores[$];C>w&&(x.output.push({label:this._trainingData.classes.sort((k,T)=>k.name<T.name?-1:k.name>T.name?1:0)[$].name,confidence:C}),b=$)}x.predictionIdx=b;const v=new pl;v.isTrained=!0,v.isPrediction=!0,v.predictions=x,self.postMessage({type:"status",payload:v})}));break;case"STOP_AUDIO_LISTENING":this._speechRecognizer&&this._speechRecognizer.stopListening();break;case"TRAIN":this._stopTrainingRequested=!1,this._resetModelRequested=!1;const t=e.data.modelType;t==="image-device"?yield this.train(e):t==="audio"&&(yield this.trainAudio(e));break;case"PREDICT":yield this.predict(e);break;case"STOP_TRAINING":this._localIsTraining&&(this._stopTrainingRequested=!0);break;case"RESET_MODEL":this._localIsTraining?(this._stopTrainingRequested=!0,this._resetModelRequested=!0):this.resetModel();break;case"SAVE_MODEL":if(this._localModel){let y=null,b=null,w=null;if(e.data.modelType==="audio"){const x=this._localModel.layers[this._localModel.layers.length-1];b=x.weights.map(C=>C.read().dataSync()),w=x.weights;const $={config:x.getConfig(),weights:b};y=JSON.stringify($)}else e.data.modelType==="image-device"&&(this._localModel=this._localModel,y=this._localModel.toJSON(),b=this._localModel.weights.map(x=>x.read().dataSync()),w=this._localModel.weights);self.postMessage({type:"save_model",payload:{modelJSON:y,weights:{weightBuffers:b,weightInfo:w}}})}break;case"LOAD_TM_MODEL":const{tmModelUrl:s}=e.data;if(this._localModel&&this._localModel.dispose(),s){console.debug("MLModelWorker: Loading TM model...");const b=yield(yield fetch(s.replace("model.json","metadata.json"))).json();this._localModel=yield Hi(s),console.log("modelMetadataJson",b),console.log("modelMetadataJson.labels",b.labels),console.log("modelMetadataJson.wordLabels",b.wordLabels);const w=b.labels?"image-device":"audio";if(console.log("modelType",w),w==="audio"){const x=new fo;yield x.ensureModelLoaded(),this._speechRecognizer=x.createTransfer("modelNameExample"),this._speechRecognizer.words=b.wordLabels,this._speechRecognizer.model=this._localModel}self.postMessage({type:"load_model",payload:{}})}break;case"LOAD_AUDIO_MODEL":this._localModel&&this._localModel.dispose();const{modelJSON:r,weightBuffers:a,weightInfo:o,audioAlphabeticalWords:i}=e.data,l=new fo;yield l.ensureModelLoaded(),this._speechRecognizer=l.createTransfer("modelNameExample");const c=a.map((y,b)=>{const w=o[b].shape,x=o[b].dtype;return Bt(y,w,x)});this._speechRecognizer.words=i,this._speechRecognizer.createTransferModelFromBaseModel(c),self.postMessage({type:"load_model",payload:{}});break;case"LOAD_MODEL":this._localModel&&this._localModel.dispose();const{modelJSON:u,weightBuffers:h,weightInfo:d}=e.data;console.debug("MLModelWorker: Loading MM model...");const p=JSON.parse(u);console.debug("MLModelWorker: Loading model...",e.data);const f=yield vF(p);console.log("model",f);const m=h.map((y,b)=>{const w=d[b].shape,x=d[b].dtype;return Bt(y,w,x)});console.debug("MLModelWorker: Setting weights...",m),f.setWeights(m),this._localModel=f,console.debug("MLModelWorker: Model loaded!"),self.postMessage({type:"load_model",payload:{}});break;case"SET_TRAINING_DATA":this._trainingData=e.data.trainingData;break;case"STREAM_AUDIO":const{streamedAudioData:g}=e.data;this._speechRecognizer&&this._speechRecognizer.getAudioStream(g);break}})}imgToTensor(e,t){return P(()=>{let s=EA(e);return t&&(s=Pn.resizeBilinear(s,t)),this.cropImage(s).expandDims(0).toFloat().div(pe(127)).sub(pe(1))})}cropImage(e){const t=Math.min(e.shape[0],e.shape[1]),r=e.shape[0]/2-t/2,o=e.shape[1]/2-t/2;return e.slice([r,o,0],[t,t,3])}convertUtilsPixelsToTensor(e){const{pixelsBase64:t,height:s,width:r,depth:a}=e,o=Uint8Array.from(atob(t),u=>u.charCodeAt(0)),i=Ua(o,[s,r,a],"int32"),c=Pn.resizeBilinear(i,[uS,cS],!0).div(255);return this._mobileNetModel.predict(c.expandDims())}trainAudio(e){return O(this,null,function*(){console.log("event",e);const t=e.data.trainingData;if(!t)return console.log("MLModelWorker: No audio training data!"),!1;const s=t;this._trainingData=s;const r=new fo;this._localIsTraining=!0;const a=new pl;a.isTraining=!0,yield r.ensureModelLoaded(),console.log("MLModelWorker: Speech model loaded!"),console.log("baseRecognizer parameters:",r.params()),console.log("baseRecognizer input shape:",r.modelInputShape());const o=r.createTransfer("modelNameExample");yield o.addExamples(s.classes),console.log(o.countExamples());const i=e.data.trainIterations,l=performance.now(),c=[[]],u=[[]];return yield o.train({epochs:i,callback:{onEpochEnd:(h,d)=>O(this,null,function*(){const f=(performance.now()-l)/1e3,m=d.loss,g=d.acc;c[0].push({x:h,y:m}),u[0].push({x:h,y:g}),a.i=h,a.iterations=i,a.modelFitTime=f,a.lossValues=c,a.accuracyValues=u,self.postMessage({type:"status",payload:a})}),onTrainEnd:h=>O(this,null,function*(){this._localIsTraining=!1,this._resetModelRequested?this.resetModel():(a.isTraining=!1,a.isTrained=!0,self.postMessage({type:"status",payload:a})),this._speechRecognizer=o,this._localModel=o.model})}}),!0})}train(e){return O(this,null,function*(){if(!this._mobileNetModel)if(this._mobileNetModel=yield this.loadTruncatedMobileNet(),this._mobileNetModel)console.log("MLModelWorker: mobilenet loaded");else return console.log("MLModelWorker: MobileNet not loaded!"),!1;if(!e.data.trainingData)return console.log("MLModelWorker: No training data!"),!1;const t=e.data.trainingData;if(t.classes.length===0)return console.log("MLModelWorker: No classes to train!"),!1;if(t.classes.some(w=>w.samples.length===0))return console.log("MLModelWorker: At least one class has no samples!"),!1;this._localIsTraining=!0;const s=new pl;s.isTraining=!0,this._trainingData=t;const r=t.classes.flatMap(w=>w.samples.map(({tensorData:x,tensorShape:v,tensorDtype:$})=>{const C=Bt(new Float32Array(x),v,$),k=this._mobileNetModel.predict(C),T=k.dataSync();return C.dispose(),k.dispose(),T})),a=t.classes.flatMap((w,x)=>w.samples.map(()=>x));go(r,a);const o=ft(a,"int32"),i=Vr(o,t.classes.length),l=Wt(r),c=this._mobileNetModel.outputs[0].shape.slice(1),u=U(c),h=E_({});this._localModel=ji(),this._localModel.add(om({inputShape:[u],units:100,activation:"relu",kernelInitializer:h,useBias:!0})),this._localModel.add(om({kernelInitializer:h,useBias:!1,activation:"softmax",units:t.classes.length}));const d=.001,p=qr.adam(d);this._localModel.compile({optimizer:p,loss:"categoricalCrossentropy",metrics:["accuracy"]});const{trainIterations:f,batchSize:m}=e.data,g=[[]],y=[[]];for(let w=0;w<f&&!this._stopTrainingRequested;++w){const x=performance.now(),v=yield this._localModel.fit(l,i,{epochs:1,batchSize:m,yieldEvery:"epoch"}),C=(performance.now()-x)/1e3,k=v.history.loss[0],T=v.history.acc[0];g[0].push({x:w,y:k}),y[0].push({x:w,y:T}),s.i=w,s.iterations=f,s.modelFitTime=C,s.lossValues=g,s.accuracyValues=y,self.postMessage({type:"status",payload:s})}o.dispose(),i.dispose(),l.dispose(),this._localIsTraining=!1,this._resetModelRequested?this.resetModel():(s.isTraining=!1,s.isTrained=!0,self.postMessage({type:"status",payload:s}));const b=ji();return b.add(this._mobileNetModel),b.add(this._localModel),this._localModel=b,p.dispose(),!0})}predict(e){return O(this,null,function*(){const{imagePixels:t}=e.data;if(this._mobileNetModel===null)return console.log("MLModelWorker: MobileNet v3 not loaded!"),!1;if(this._localModel===null)return console.log("MLModelWorker: Local model not loaded!"),!1;if(this._trainingData===null)return console.log("MLModelWorker: Training data not loaded!"),!1;if(!t)return console.log("MLModelWorker: No image data!"),!1;const s=performance.now(),r=Bt(new Float32Array(t.tensorData),t.tensorShape,t.tensorDtype),a=P(()=>this._localModel.predict(r)),o=yield a.data();let i=-1,l=-1;const c=new sg;for(let p=0;p<o.length;p++)o[p]>l&&(c.output.push({label:this._trainingData.classes[p].name,confidence:o[p]}),i=p);c.predictionIdx=i;const h=(performance.now()-s)/1e3,d=new pl;d.isTrained=!0,d.isPrediction=!0,d.modelPredictTime=h,d.predictions=c,be(a),self.postMessage({type:"status",payload:d})})}};ce(fa,"_instance",null);let rg=fa;self.onmessage=n=>O(this,null,function*(){rg.getInstance().onmessage(n)})})();
|