[email protected]
MacOS X 10.12.5
node v9.4.0
Trying to train a model and then save it with:
await model.save('file:///./model');
And I get a message:
(node:2792) UnhandledPromiseRejectionWarning: Error: Cannot find any save handlers for URL 'file:///./model'
How to properly save the trained model?
@ProgrammingLife Did you import tfjs-node using a line such as import '@tensorflow/tfjs-node' or require('@tensorflow/tfjs-node'), in addition importing @tensorflow/tfjs?
This is probably related to the fact you don't have a working Node.js package: https://github.com/tensorflow/tfjs/issues/593#issuecomment-412245609
My source code is:
"use strict";
const tf = require("@tensorflow/tfjs");
async function go() {
const training_data = tf.tensor2d([ [0, 0], [0, 1], [1, 0], [1, 1] ]);
const target_data = tf.tensor2d([ [0], [1], [1], [0] ]);
const model = tf.sequential();
model.add(tf.layers.dense({ inputShape: 2, units: 10, activation: "sigmoid" }));
model.add(tf.layers.dense({ units: 1, activation: "sigmoid" }));
model.compile({ loss: "meanSquaredError", optimizer: "rmsprop" });
for (let i = 0; i < 3; i++) {
var h = await model.fit(training_data, target_data, { epochs: 25 });
console.log("Loss after Epoch " + i + ", loss = " + h.history.loss[0]);
}
model.predict(training_data).print();
await model.save('file:///./model');
}
go();
And an output is:
Loss after Epoch 0, loss = 0.2550252676010132
Loss after Epoch 1, loss = 0.25055640935897827
Loss after Epoch 2, loss = 0.25000935792922974
Tensor
[[0.4813891],
[0.5064855],
[0.4947665],
[0.5187985]]
(node:875) UnhandledPromiseRejectionWarning: Error: Cannot find any save handlers for URL 'file:///./model'
at new ValueError (/Users/user/node-testes/node_modules/@tensorflow/tfjs-layers/dist/errors.js:36:28)
at Sequential.<anonymous> (/Users/user/node-testes/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:1126:39)
at step (/Users/user/node-testes/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:42:23)
at Object.next (/Users/user/node-testes/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:23:53)
at /Users/user/node-testes/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:17:71
at new Promise (<anonymous>)
at __awaiter (/Users/user/node-testes/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:13:12)
at Sequential.Model.save (/Users/user/node-testes/node_modules/@tensorflow/tfjs-layers/dist/engine/training.js:1118:16)
at go (/Users/user/node-testes/tensorflow-test.js:56:8)
at <anonymous>
Sure I have all write permissions to the directory.
@ProgrammingLife So your code doesn't import @tensorflow/tfjs-node. Without importing that package, the machinery for file-system saving and loading won't be there. Another thing to point out here is: if you us relative path, you should do something like 'file://./model' (2 slashes after the colon), instead of "file:///./model" (3 slashes after the colon).
Is any other way to get weights out of model? I wanna save and load it manually without using model.save() and model.load(). Is it possible?
@ProgrammingLife
In your package.json, make sure you have included the dependency such as
"@tensorflow/tfjs-node": "^0.1.11"
In your .js file, do after const tf = require("@tensorflow/tfjs");: require('@tensorflow/tfjs-node');
Let us know if that works.
Wow it worked! Thanks! 馃憤
Most helpful comment
@ProgrammingLife
In your package.json, make sure you have included the dependency such as
"@tensorflow/tfjs-node": "^0.1.11"
In your .js file, do after
const tf = require("@tensorflow/tfjs");:require('@tensorflow/tfjs-node');Let us know if that works.