最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Tensorflow.js save model using node - Stack Overflow

programmeradmin3浏览0评论

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 (), 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 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

Share Improve this question edited Sep 21, 2018 at 21:59 Jonas 7,87516 gold badges55 silver badges122 bronze badges asked Sep 16, 2018 at 15:36 SchotslSchotsl 2271 gold badge10 silver badges32 bronze badges 4
  • 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 Commented Sep 16, 2018 at 16:27
  • @SebastianSpeitel I've added the tfjs-node code to my post. – Schotsl Commented Sep 17, 2018 at 14:51
  • I have the same issue. Have you found anything helpful yet? – Jonas Commented Sep 21, 2018 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 Commented Sep 21, 2018 at 20:04
Add a comment  | 

5 Answers 5

Reset to default 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');

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

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/[email protected]
├── [email protected]
├── [email protected]
└── [email protected]

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

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

发布评论

评论列表(0)

  1. 暂无评论