TensorFlow JS - 加载使用 Python 生成的模型

TensorFlow JS - Load A Model Generated Using Python

我按照 this tutorial 中的步骤转换使用 Python 生成的经过训练的 TensorFlow 模型。现在我想用它在 TensorFlow JS 中重新创建模型。我像这样传递了 model.js 的路径:

const model = tf.loadLayersModel(pathToModelsDotJSON).then(() => {
    // Do stuff
})

我正在使用 Vue.js,当我 运行 代码时,它抛出此错误:

Uncaught (in promise) Error: The first layer in a Sequential model must get an `inputShape` or `batchInputShape` argument.
    at new t (tf-layers.esm.js?271e:17)
    at t.add (tf-layers.esm.js?271e:17)
    at t.fromConfig (tf-layers.esm.js?271e:17)
    at deserializeKerasObject (tf-layers.esm.js?271e:17)
    at deserialize (tf-layers.esm.js?271e:17)
    at eval (tf-layers.esm.js?271e:17)
    at eval (tf-layers.esm.js?271e:17)
    at Object.eval [as next] (tf-layers.esm.js?271e:17)
    at o (tf-layers.esm.js?271e:17)

我的问题是:为什么会这样?以及如何解决?

1 - 您使用了错误的函数 modelFromJSON 来导入模型。根据教程,这里是要使用的函数:loadLayersModel.

2-

The first layer in a Sequential model must get an inputShape or batchInputShape argument

您需要将 batchInputShape 或 InputShape 添加到 Python

中定义的模型