12

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

Jonas
  • 7,089
  • 15
  • 49
  • 110
Schotsl
  • 187
  • 1
  • 8
  • 29
  • Could you show us the bit of your code where you tried to fix the issue using `tfjs-node`, because I understand it this should be the solution of your problem. – Sebastian Speitel Sep 16 '18 at 16:27
  • @SebastianSpeitel I've added the `tfjs-node` code to my post. – Schotsl Sep 17 '18 at 14:51
  • I have the same issue. Have you found anything helpful yet? – Jonas Sep 21 '18 at 18:02
  • @Jonas No sadly enough I haven't found anything helpful, It's too bad there aren't many examples around on how to save. It might just be a bug? Please do tell me if you find anything! – Schotsl Sep 21 '18 at 20:04

5 Answers5

14

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');
Jonas
  • 7,089
  • 15
  • 49
  • 110
  • Hmm, I though I had tried this before.. But I'll double check / try it when I get home! – Schotsl Sep 24 '18 at 13:04
  • Yes I tried that and it didn't work for me neither. But when I created a completely new project with adding only the node dependency worked. It seems like installing the node package over the normal package caused my problems. So maybe just try my code with a new project. That should work. – Jonas Sep 24 '18 at 20:40
  • 1
    It works! After creating a new cloud environment, updating to the most recent Node version and only installing the node dependencies it works! I think your right about installing the normal version and then the node version – Schotsl Sep 25 '18 at 08:56
  • Nice, glad I could help :D – Jonas Sep 25 '18 at 09:35
  • 1
    Hi, I deleted my npm_modules and then ran `npm i @tensorflow/tfjs-node` and when running my `.js` script I get the "Cannot find module '@tensorflow/tfjs" error. Wasn't the solution to JUST do that one npm install? – Diego Aguado Jan 17 '19 at 03:02
1

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();
serraosays
  • 7,163
  • 3
  • 35
  • 60
  • Thank you, after updating to Node.js version 10 in other to use the import function, but it returns this error: `import * as tf from '@tensorflow/tfjs'` and `SyntaxError: Unexpected token *` – Schotsl Sep 16 '18 at 17:51
  • If you want to make sense of imports, this S.O. answer is pretty good: https://stackoverflow.com/questions/31386631/difference-between-import-x-and-import-as-x-in-node-js-es6-babel – serraosays Sep 16 '18 at 18:00
  • Thanks! After installing switching back to Node 9 and using the experimental flag I can use, the import function but now it returns `TypeError: tf.sequential is not a function`. But shouldn't it still work even when not using experimental flag and instead using require? – Schotsl Sep 17 '18 at 12:36
  • `.sequential()` was mentioned in the TF documentation, you might not even need it for what you're doing. But it should be available when you do the import. I'd double check some of their examples, you might see one that matches what you're trying to do more closely than my example. – serraosays Sep 17 '18 at 14:04
  • I've been looking around but It appears there are no whole TensorFlow Node save examples, only snippets. Which aren't working :( – Schotsl Sep 17 '18 at 14:50
  • Ok I will open an Issue on Github. I will let you know when I found something helpful. – Jonas Sep 21 '18 at 20:06
0

I had this same problem "Cannot find any save handlers for URL" when trying to use model.save(). The solution to my problem was slightly different.

I finally managed to get it installed correctly by removing the previous packages and doing a local install (npm install package_name) instead of a global install (npm install -g package_name). I then copied the local node_modules to the place they go when doing a global install:

rm -rf /usr/local/lib/node_modules/@tensorflow
cp -ax node_modules/* /usr/local/lib/node_modules/

The Following DOES NOT install @tensorflow/tfjs nor any of the other 29 related packages (adm-zip...yallist):

npm install -g @tensorflow/tfjs-node

Yes, I issued the command as the root user if you're wondering about priviledges.

I was using npm v6.4.1:

npm list -g --depth=0
/usr/local/lib
├── @tensorflow/tfjs-node@0.1.21
├── express@4.16.4
├── npm@6.4.1
└── socket.io@2.1.1
0

I used tfjs-node-save from https://www.npmjs.com/package/tfjs-node-save, and succeeded.

process:

npm i @tensorflow/tfjs

npm i tfjs-node-save

code: mostly same as @Jonas

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');
greentec
  • 2,130
  • 1
  • 19
  • 16
0

Had encountered the same error when using bun on ubuntu: bun run mytensor.ts. When change the importing queue (first should be tfjs-node):

import "@tensorflow/tfjs-node"
import * as tf from '@tensorflow/tfjs';

Then use npm and node mytensor.js. It works now. Apparently, Typescript and bun node are not accepted

Bitfinicon
  • 1,045
  • 1
  • 8
  • 22