i am working on a small app in which I am passing some data from main class to the web worker to train model using RandomForestClassifer method from 'ml-random-forest' module.
I tried to import the above method into the web worker but i get the following error: Uncaught ReferenceError: ml_random_forest__WEBPACK_IMPORTED_MODULE_0__ is not defined at onmessage
worker.js
import { RandomForestClassifier as RFClassifier } from 'ml-random-forest';
export default ()=>{
onmessage=(e)=>{
console.log('in worker')
const classifier = new RFClassifier(e.data[0]);
console.log('classifer'+ classifier)
classifier.train(e.data[1],e.data[2])
var result = classifier.predict(e.data[3]);
var accuracy = calculateAccuracy(result, e.data[4]);
console.log(accuracy)
postMessage(accuracy)
}
const calculateAccuracy = (result, test_labels) => {
let hits = 0;
for(let i = 0; i<result.length; i++){
if(result[i] == test_labels[i]){
hits++;
}
}
let rcl = hits / result.length;
return rcl;
}
}
main.js
const myworker = new webWorker(worker);
//some extra code inbetween
setTimeout(async () => {
// training_labels = transformLabelsIntoEnums(training_labels);
// test_labels = transformLabelsIntoEnums(test_labels);
const options = {
seed: 132456,
maxFeatures: props.state.rf_config.rf_maxFeatures,
replacement: false,
nEstimators: props.state.rf_config.rf_nEstimators
};
const classifier = new RFClassifier(options);
// await sleep(1000000);
console.log('classifier '+classifier)
myworker.onmessage = function (event){
accuracy=event.data;
console.log(event.data);
}
console.log('calling worker')
myworker.postMessage([options,training_dataset,training_labels,test_dataset,test_labels]);
}, 100);