首页 > 解决方案 > 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

标签: javascriptpythontensorflow.js

解决方案


问题似乎来自这里[[tf.zeros(500)]]model.predicts在模型有多个输入条目的情况下,将张量或张量数组作为参数。

根据模型的 InputShape,tf.zeros(500)应该足够或者可能 tf.zeros(500).reshape(appropriateShape)


推荐阅读