javascript - 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
解决方案
问题似乎来自这里[[tf.zeros(500)]]
。model.predicts
在模型有多个输入条目的情况下,将张量或张量数组作为参数。
根据模型的 InputShape,tf.zeros(500)
应该足够或者可能 tf.zeros(500).reshape(appropriateShape)
推荐阅读
- sql - SQL 根据其他值分组除以选择
- swift - 无法将场景编辑器中的 SKSpriteNode 引用到代码中
- c# - Asp.net 在 PostBack 后丢失所有信息
- php - 如何测试添加的图像是否适用于 WooCommerce 产品?
- python - 如何更新熊猫数据框字符串列中的子字符串
- javascript - 桌面通知 JS 在 Firefox 上有效,但在 Chrome 上无效?
- c# - 如何将页面上的数字放入PDF
- ios - iOS - AVSpeechSynthesizer 暂停和 continueSpeaking 问题
- java - 未从 Firebase 调用 setValue onComplete
- r - 贴现未来现金流