简体   繁体   中英

When I try to make a prediction using a tensorflow.js model I get the error: Uncaught TypeError: Cannot read property 'length' of undefined

I'm trying to create a webpage that uses a deep learning model to perform live sentiment analysis.

When I use the model.predict() function I get the error: Uncaught TypeError: Cannot read property 'length' of undefined.

I'm currently using version 1.2.3 of tensorflow.js and I've tried downgrading the version but it didn't help.

Here's my code:

<script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/1.2.3/tf.js"></script>
<script type="text/javascript">
    async function init()   {
       model =  await tf.loadLayersModel('http://127.0.0.1:8000/model.json');
       model.predict([[tf.zeros(500)]]);
    };
    init();
</script>

I get the following error:

training.ts:320 Uncaught TypeError: Cannot read property 'length' of undefined
    at checkInputData (training.ts:320)
    at t.predict (training.ts:1101)
    at t.predict (models.ts:780)
    at <anonymous>:1:7

The issue seems to come from here [[tf.zeros(500)]] . model.predicts takes as paremeter either a tensor or an array of tensors in the case the model has several inputs entries.

Depending on the InputShape of the model, tf.zeros(500) should be enough or maybe tf.zeros(500).reshape(appropriateShape)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM