ml5js / ml5-library

Friendly machine learning for the web! 🤖
https://ml5js.org
Other
6.5k stars 901 forks source link

Shouldn't the "neuralNetwork" function return a Promise if no callback is passed? #961

Open loic-brtd opened 4 years ago

loic-brtd commented 4 years ago

Dear ml5 community, I'm submitting a new issue. Please see the details below.

Is there a way that I can create a Neural Network without a callback, using the await keyword? I tried the following and it seems to work fine with version 0.4.3 of ml5:

const nn = ml5.neuralNetwork({
    dataUrl: 'data.csv',
    inputs: INPUTS,
    outputs: OUTPUTS,
    task: 'classification'
});
await nn.ready;

Using await nn.ready feels like a hack ^^ And with version 0.5.0, it throws an error:

Uncaught (in promise) TypeError: this.createMetadata is not a function
    at t.<anonymous> (index.js:183)
    at x (runtime.js:62)
    at Generator._invoke (runtime.js:296)
    at Generator.t.<computed> [as next] (runtime.js:114)
    at i (asyncToGenerator.js:17)
    at asyncToGenerator.js:28

I'm using:

joeyklee commented 4 years ago

@lobertrand - thank you for flagging this. @yining1023 and I will be doing a refactor of the NN feature this summer and will definitely try to address this.

For now, if you're keen to use async/await, I'm wondering if you could do the following:

const nn = ml5.neuralNetwork({
    inputs: INPUTS,
    outputs: OUTPUTS,
    task: 'classification'
});
nn.options.dataUrl = 'datata.csv';
await nn.loadDataInternal(nn.options);

It's not ideal, but might do the trick.