I want to save a trained model from node.js using this function
async function tfModelExperiment(model) {
try {
let tsModelTraining = await model.save('file:///tmp/my-model-1');
}
catch (error) {
console.log(error);
}
}
but when saving the model it returns
(node:23756) UnhandledPromiseRejectionWarning: Error: Cannot find any save handlers for URL 'file:///tmp/my-model-1'
I found another person struggeling with this problem but it was fixed by including
const tf = require('@tensorflow/tfjs');
Which I already had, I've tried changing the directory to my home directory but that doesn't solve the problem, neither does running it as sudo, what could I be doing wrong?
Software I'm using Ubuntu Ubuntu 18.04.1 LTS with the most recent TensorFlow.js package (0.13.0) installed with npm
EDIT:
It should be noted that I tried
import * as tf from '@tensorflow/tfjs';
import '@tensorflow/tfjs-node';
As provided here (https://github.com/caisq/tfjs-node), which returns
TypeError: tf.sequential is not a function at file:///home/sjors/node.mjs:7:18 at ModuleJob.run (internal/loader/ModuleJob.js:94:14) at
And I've tried:
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
Which returns the same UnhandledPromiseRejectionWarning
error as before
I got it working now with the help of the guys from tfjs over at github.
Basically you need to install only the the tfjs-node dependency:
npm i @tensorflow/tfjs-node
Then you can just require tfjs and it should work.
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.save('file://./model-1a');
Your async function reads the await
line inside of it and executes a JS promise
. A promise is the JS compiler executing a remote piece of code and assuring the async function that a value will be delivered to it in the future (hence the name promise).
So in your case, Node is looking at model.save('file:///tmp/my-model-1')
and not finding any .save
method that can handle the response from the promise. That's why your errors are talking about unhandled responses/promises.
The last part of this issue is saying you don't have any error handlers either. Using async/await
JS pattern, you typically wrap your call await
calls in a try
and your error handlers in a catch
.
Finally, you mention the require
code fixing the issue. What require
is doing is giving your JS file access to the tensorflow library, which would fix the model.save() error. But in the newer versions of JS (called ES6/7/8), require
has been replaced by import
- they accomplish the same thing but look a little different.
Taken together, your JS code will look something like this:
// Do the TS import
import * as tf from '@tensorflow/tfjs';
// Set up TS model
const model = tf.sequential();
async function tfModelExperiment() {
try {
let tsModelTraining = await model.save();
// Missing code where you would handle `tsModelTraining`
}
catch (error) {
// Handle the error in here
console.log(error);
}
}
tfModelExperiment();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With