3 lines
16 KiB
JavaScript
3 lines
16 KiB
JavaScript
|
|
import{m as d,p,l as y}from"./index-C1-a2ZPT.js";class T{maxSize;inputSize_=0;outputSize_=0;features_=new Float32Array(0);labels_=new Float32Array(0);size_=0;constructor(t=100){if(t<=0)throw new Error("Dataset.maxSize must be > 0");this.maxSize=t}get size(){return this.size_}isEmpty(){return this.size_===0}add(t,e){if(this.size_===0&&(this.inputSize_=t.length,this.outputSize_=e.length,this.features_=new Float32Array(this.maxSize*this.inputSize_),this.labels_=new Float32Array(this.maxSize*this.outputSize_)),t.length!==this.inputSize_||e.length!==this.outputSize_)return!1;this.size_>=this.maxSize&&(this.features_.copyWithin(0,this.inputSize_),this.labels_.copyWithin(0,this.outputSize_),this.size_=this.maxSize-1);const s=this.size_*this.inputSize_,a=this.size_*this.outputSize_;for(let r=0;r<this.inputSize_;++r)this.features_[s+r]=t[r];for(let r=0;r<this.outputSize_;++r)this.labels_[a+r]=e[r];return this.size_++,!0}clear(){this.size_=0}feature(t){if(t<0||t>=this.size_)throw new RangeError(`feature index ${t} out of bounds`);return this.features_.subarray(t*this.inputSize_,(t+1)*this.inputSize_)}label(t){if(t<0||t>=this.size_)throw new RangeError(`label index ${t} out of bounds`);return this.labels_.subarray(t*this.outputSize_,(t+1)*this.outputSize_)}featuresFlat(){return this.features_.subarray(0,this.size_*this.inputSize_)}labelsFlat(){return this.labels_.subarray(0,this.size_*this.outputSize_)}get inputSize(){return this.inputSize_}get outputSize(){return this.outputSize_}computeWeights(t="uniform",e={}){const s=this.size_;if(s===0)return new Float32Array(0);if(s===1)return new Float32Array([1]);const a=new Float32Array(s).fill(1);if(t==="global"||t==="combined"){const i=e.recencyBias??.6;if(i>0){const n=1-.3*i;for(let o=s-2;o>=0;--o)a[o]=a[o+1]*n}}if((t==="local"||t==="combined")&&e.queryInput){const i=e.queryInput,n=e.radius??.15,o=n*n,_=this.inputSize_;for(let u=0;u<s;++u){const S=u*_;let c=0;for(let m=0;m<_;++m){const g=this.features_[S+m]-(i[m]??0);c+=g*g}if(c<o){const m=1-Math.sqrt(c)/n;let g=0;for(let l=u+1;l<s;++l){const h=l*_;let f=0;for(let w=0;w<_;++w){const A=this.features_[S+w]-this.features_[h+w];f+=A*A}f<o&&++g}g>0&&(a[u]*=Math.pow(1-m,g))}}}let r=0;for(let i=0;i<s;++i)r+=a[i];if(r>0)for(let i=0;i<s;++i)a[i]/=r;return a}}class H{worker;nextId=1;pending=new Map;disposed=!1;static async create(){const t=new H;return await t.init_(),t}constructor(){this.worker=new Worker(new URL("/assets/wasm-worker-OATBBHaT.js",import.meta.url),{type:"module"}),this.worker.onmessage=t=>this.onMessage_(t.data),this.worker.onerror=t=>{for(const{reject:e}of this.pending.values())e(t.message??"worker error");this.pending.clear()}}init_(){return new Promise((t,e)=>{const s=r=>{const i=r.data;i.kind==="ready"?(this.worker.removeEventListener("message",s),t()):i.kind==="error"&&(this.worker.removeEventListener("message",s),e(new Error(i.message)))};this.worker.addEventListener("message",s);const a=(Date.now()^Math.floor(Math.random()*4294967295))>>>0;this.worker.postMessage({kind:"init",seed:a})})}train(t){if(this.disposed)return Promise.reject(new Error("WasmTrainer disposed"));const e=this.nextId++;return new Promise((s,a)=>{this.pending.set(e,{resolve:s,reject:a});const r={kind:"train",requestId:e,weights:t.weights,features:t.features,labels:t.labels,sampleWeights:t.sampleWeights,lr:t.lr,maxIter:t.maxIter,minErr:t.minErr,inputSize:t.inputSize,outputSize:t.outputSize};this.worker.postMessage(r,[t.weights.buffer,t.features.buffer,t.labels.buffer,t.sampleWeights.buffer])})}dispose(){if(!this.disposed){this.disposed=!0;try{this.worker.postMessage({kind:"dispose"})}catch{}this.worker.terminate();for(const{reject:t}of this.pending.values())t(new Error("disposed"));this.pending.clear()}}onMessage_(t){if(t.kind==="result"){const e=this.pending.get(t.requestId);e&&(this.pending.delete(t.requestId),e.resolve({loss:t.loss,weights:t.weights,lossHistory:t.lossHistory}))}else if(t.kind==="error"){const e=this.pending.get(t.requestId);e&&(this.pending.delete(t.requestId),e.reject(new Error(t.message)))}}}function M(){return H.create()}const W=t
|
||
|
|
//# sourceMappingURL=wasm-iml-FZSgVMmn.js.map
|