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

时间:2019-07-13 17:28:16

标签: javascript python json tensorflow tensorflow.js

我按照this tutorial中的步骤转换了使用TensorFlow生成的经过训练的Python模型。现在,我想用它在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 个答案:

答案 0 :(得分:1)

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

2-

  

顺序模型的第一层必须获取inputShapebatchInputShape参数

您需要将batchInputShape或InputShape添加到Python中定义的模型中