Conversation
|
Hi @ziyuan-linn, maybe we can meet to discuss these! I would very much prefer to keep the synchronous calls for neural network especially for the neuroevolution examples. I originally made asychrnous versions of these examples for teaching the class and the students really struggled with them along managing the I realize it's not typical to use synchronous calls for ML models, but since these are tiny neural networks running on CPU for these p5.js examples I think it's worth making an exception and hacking our way around it! Later, when p5.js adopts promises I think we could reconsider! |
|
I stumbled across this, as @ziyuan-linn mentioned, that explanation was nice and clear, @shiffman, thanks! Out of curiosity, is the adoption of promises in p5 in the works / planned? |
Looks like it is! https://github.com/processing/p5.js/blob/dev-2.0/rfc_p5js_2.md#async |
|
Awesome, thanks @ziyuan-linn ! |
This PR adds
webgpusupport for neuralNetwork so it can be used without calledml5.setBackend.Changes:
readSyncas @lindapaiste suggested.await tf.ready()to theinitfunction ofDiyNeuralNetwork@lindapaiste suggested.copyandcrossoverto async, support both promise and callback interface.The
ml5.neuralNetworkfunction is technically an async operation like with other models. It should be called with a callback function or placed within p5preloadfunction. Our examples, however, have been treatingml5.neuralNetworklike a synchronous function since it sometimes (when not loading data) behaves synchronously. The changes from this PR addsawait tf.ready()thus making it definitively asynchronous.The functions
copyandcrossoveralso instantiate the neuralNetwork object, which is why they have to be async.I updated all example sketches, removing
ml5.setBackendand moving theml5.nerualNetworkfunction topreloadfor non neuroevolution examples.The neuroevolution examples are kept on
cpubackend for performance. Adapting for asynccopyandcrossoverslightly complicates those examples but I think they are still fairly readable. Theml5.neuralNetworkcalls in neuroevolution examples are still synchronous, but they seem to not cause any error for now. I will try to come up with a clean way to adapt them to async.