Tfjs: Saving Model on Node: Cannot find any save handlers

Created on 21 Sep 2018  路  9Comments  路  Source: tensorflow/tfjs

To get help from the community, check out our Google group.

TensorFlow.js version

tfjs: 0.13.0
tfjs-node: 0.1.17
node-version: v8.11.2

Browser version

Run with node, so no browser involved

Describe the problem or feature request

I'm trying to save my model like this: const save = await model.save('file:///tmp/my-model-1'); but I get the error:

UnhandledPromiseRejectionWarning: Error: Cannot find any save handlers for URL 'file:///tmp/my-model-1' at new ValueError (/Users/jonas/RestAPITest/node_modules/@tensorflow/tfjs-layers/dist/errors.js:36:28) at Sequential.<anonymous> (/Users/jonas/RestAPITest/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:1160:39) at step (/Users/jonas/RestAPITest/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:42:23) at Object.next (/Users/jonas/RestAPITest/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:23:53) at /Users/jonas/RestAPITest/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:17:71 at new Promise (<anonymous>) at __awaiter (/Users/jonas/RestAPITest/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:13:12) at Sequential.Model.save (/Users/jonas/RestAPITest/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:1152:16) at train (/Users/jonas/RestAPITest/ml.js:41:30) at <anonymous> (node:43798) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3) (node:43798) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejectionsthat are not handled will terminate the Node.js process with a non-zero exit code.

I followed this tutorial

And this is a stackoverflow post with the same issue but there is no solution.

Code to reproduce the bug / link to feature request

Here is my full code:

const tf = require('@tensorflow/tfjs');
const tfNode = require('@tensorflow/tfjs-node');

const model = tf.sequential();

const hidden = tf.layers.dense({ units: 4, inputShape: 3, activation: 'sigmoid' });
const output = tf.layers.dense({ units: 1, activation: 'sigmoid' });

model.add(hidden);
model.add(output);

const optimzer = tf.train.sgd(0.5);
model.compile({
    optimizer: optimzer,
    loss: tf.losses.meanSquaredError
});

const dataTasks = [];
const dataSolutions = [];
for (let i = 0; i < 90; i++) {
    dataTasks[i] = Math.round(Math.random());
    if (i % 3 == 0) {
        if (dataTasks[i] == 0) {
            dataSolutions.push(0)
        } else if (dataTasks[i] == 1) {
            dataSolutions.push(1)
        }
    } 
}
const tasks = tf.tensor2d(dataTasks, [30, 3]);
const solution = tf.tensor2d(dataSolutions, [30, 1]);

train();

async function train() {
    for (let i = 0; i < 1000; i++) {
        const response = await model.fit(tasks, solution, { shuffle: true})
    }
    console.log('trained');

    const save = await model.save('file:///tmp/my-model-1');
    console.log(save);
}
node.js

Most helpful comment

@JonasJW This minimal code below works for me, maybe you can try it out:

package.json:

{
  "dependencies": {
    "@tensorflow/tfjs-node": "0.1.17"
  }
}

model-save.js:

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');

Run model-save.js with node model-save.js

All 9 comments

@JonasJW My guess is that you are listing both "@tensorflow/tfjs" and "@tensorflow/tfjs-node" as dependencies in your package.json, which leads to a version mismatch because "@tensorflow/tfjs-node" now has " @tensorflow/tfjs" as its dependency.

Can you try removing "@tensorflow/tfjs" from package.json while keeping "@tensorflow/tfjs-node"? The .js file can remain unchanged.

@nkreeger I think we need a way to detect this kind of issues caused by depending on multiple versions of @tensorflow/tfjs in using @tensorflow/tfjs-node.

Yes I was listing "@tensorflow/tfjs" and "@tensorflow/tfjs-node" in my dependency but I thought tfjs-node is just an extension to tfjs. When I remove the "@tensorflow/tfjs" dependency and do npm install, I can no longer require it like this const tf = require('@tensorflow/tfjs');. And when I change it to const tf = require('@tensorflow/tfjs-node'); instead, I'm missing all the functionallity. Then I get the error tf.sequential is not a function

@JonasJW This minimal code below works for me, maybe you can try it out:

package.json:

{
  "dependencies": {
    "@tensorflow/tfjs-node": "0.1.17"
  }
}

model-save.js:

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');

Run model-save.js with node model-save.js

For some reason my @tensorflow package was messed up. I had a the main tfjs api nested inside my tfjs-node folder. I had to delete the package and reinstall it. Now the Code works. Thank you for the great support! You guys are really doing a good job with this project.

Same problem here, I have fixed it by doing

After editing package.json, i've done

rm -rf node_modules
rm package-lock.json
npm install

This problem will occur in many situations :

  • When using npm install --global-style
  • When using npm install --legacy-bundling
  • If package.json contains both @tensorflow/tfjs and @tensorflow/tfjs-node
  • If package.json contains both @tensorflow/tfjs-node-gpu and @tensorflow/tfjs-node on the same project with different versions

I think it would be better to just do

const tf = require('@tensorflow/tfjs-node');

@caisq can you explain the reason behind this 'double-require' design ?

Thank you for this great binding

@piercus Thanks for the feedback. We've thought a bunch about how this all works and we're about to export all the @tensorflow/tfjs symbols through the binding packages themselves. See https://github.com/tensorflow/tfjs-node/pull/187

We'll push that out in the next release.

Is there a workaround for the case where I want to use '@tensorflow/tfjs-node', but one of my other dependencies installs '@tensorflow/tfjs'?

If you use tfjs-node which depends on tfjs, and a dependency of yours depends on tfjs as well, yarn/npm will de-duplicate for you if the version constraints match. Otherwise it will install 2 copies of tfjs with 2 different versions.

Hello!
I have some problems with starting tfjs-examples. (stake-dqn)

  1. I had an error when use 1.3.2 version. So I downgrade it to 1.2.3.
  2. I had an error with saving models: UnhandledPromiseRejectionWarning: Error: Cannot find any save handlers for URL 'file://./trainedModel'
    I solve this with changing path:
    require('@tensorflow/tfjs')
    to absolute:
    require('./node_modules/@tensorflow/tfjs-node/node_modules/@tensorflow/tfjs')
    config: Windows 10 (node 12.13.1, tfjs-node 1.2.3)
Was this page helpful?
0 / 5 - 0 ratings