1

When I load a saved model like this (please dont mind the fact that the predict function has no input)

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

const model = tf.loadModel('file://./model-1a/model.json').then(() => {
  model.predict();
});

I get this error:

(node:25887) UnhandledPromiseRejectionWarning: TypeError: model.predict is not a function at tf.loadModel.then (/home/ubuntu/workspace/server.js:10:9) at

But when I just create a model instead of loading it works just fine

const model = tf.sequential();
model.add(tf.layers.dense({units: 10, inputShape: [10005]}));
model.add(tf.layers.dense({units: 1, activation: 'linear'}));
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});

The model predict function works just fine? I don't know what could be wrong here and I was hopeing someone could help me out.

1 Answer 1

5

You need to work with promises.

loadModel() returns a promise resolving into the loaded model. So to access it you either need to use the .then() notation or be inside an async function and await it.

.then():

tf.loadModel('file://./model-1a/model.json').then(model => {
  model.predict();
});

async/await:

async function processModel(){
    const model = await tf.loadModel('file://./model-1a/model.json');
    model.predict();
}
processModel();

or in a shorter, more direct way:

(async ()=>{
    const model = await tf.loadModel('file://./model-1a/model.json');
    model.predict();
})()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it appears the problem in my case was that I did not pass in model as a parameter.
I want to load the model once and keep it in memory for an interactive page where multiple prediction calls are made. How do I do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.