【问题标题】:Tensorflow.js - TypeError: e.forEach is not a functionTensorflow.js - TypeError:e.forEach 不是函数
【发布时间】:2021-05-05 01:20:05
【问题描述】:

我是 tensorflow.js 的新手,我之前使用过 Python 版本,现在我正在将我之前训练的模型转换为 web。

我关注了这个codelabs tutorial

代码很简单,我有index.html:

<!DOCTYPE html>

<html>

    <head>
        <title>Testing the JS Model</title>
        <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@3.6.0/dist/tf.min.js"></script>
        <script src="scripttest.js"></script>
    </head>

    <body>

    </body>

</html>

在 scripttest.js 我有:

const MODEL_URL = "../model/model.json"

async function run() {
    // Load the model from the CDN.
    const model = await tf.loadLayersModel(MODEL_URL, strict=false);
    
    // Print out the architecture of the loaded model.
    // This is useful to see that it matches what we built in Python.
    console.log(model.summary());

    // Create a 1 dimensional tensor with our test value.
    const input = tf.tensor1d([TEST_VALUE]);

    // Actually make the prediction.
    const result = model.predict(input);
}

run();

但是当我运行它时,我得到了这个错误:

Uncaught (in promise) TypeError: e.forEach is not a function
    at bg (util_base.js:681)
    at Mw (tensor_ops_util.js:44)
    at Lw (tensor.js:56)
    at Ww (io_utils.js:225)
    at RM (models.js:334)
    at models.js:316
    at c (runtime.js:63)
    at Generator._invoke (runtime.js:293)
    at Generator.next (runtime.js:118)
    at bv (runtime.js:747)

如果您想看它自己运行,我已将其放入我的website

你能告诉我这里有什么问题吗?是我转换后的模型有问题还是可能是一些错误?

由于它在console.log(model.summary()) 中没有输出任何内容,我怀疑它是由await tf.loadLayersModel(MODEL_URL, strict=false); 行引起的

【问题讨论】:

    标签: javascript tensorflow tensorflow.js


    【解决方案1】:

    您忘记为TEST_VALUE 输入任何值

    const MODEL_URL = "../model/model.json"
    
    async function run() {
        // Load the model from the CDN.
        const model = await tf.loadLayersModel(MODEL_URL);
    
        const TEST_VALUE = 950.0
        
        // Print out the architecture of the loaded model.
        // This is useful to see that it matches what we built in Python.
        console.log(model.summary());
    
        // Create a 1 dimensional tensor with our test value.
        const input = tf.tensor1d([TEST_VALUE]);
    
        // Actually make the prediction.
        const result = model.predict(input);
    }
    
    run();
    

    【讨论】:

    • 感谢您的回答,我只是输入了这个值,但我仍然遇到同样的错误。它与const input = tf.tensor1d([TEST_VALUE]); 行无关。在打印console.log(model.summary()); 之前抛出此错误,这让我认为这发生在const model = await tf.loadLayersModel(MODEL_URL, strict=false); 行。最奇怪的是,在错误消息中出现的那些文件中,没有一个有任何e.forEach 函数。
    • 尝试删除strict=False
    • 没用,我仍然收到相同的错误消息
    • 您可以尝试升级 Tensorflow.js 吗?
    • 我已经在使用我认为的最新版本,我已经从cdn.jsdelivr.net/npm/@tensorflow/tfjs@3.6.0/dist/tf.min.js导入了3.6.0版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多