Cloud-ml 无法使用 tables_initializer 部署模型
Cloud-ml unable to deploy model with tables_initializer
我希望将经过训练的模型部署到 ml-engine。我可以在本地 运行 我的代码,例如:
with tf.Session() as sess:
sess.run([tf.global_variables_initializer(), tf.tables_initializer()])
example_result = sess.run(
my_model,
feed_dict=###snip###
)
我一直在尝试导出:
export_builder.add_meta_graph_and_variables(
sess,
[tf.saved_model.tag_constants.SERVING],
signature_def_map={
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: sig
})
然后我可以将其部署到 ml-engine。但是,当我调用模型时出现错误,因为 table 未初始化。
如何部署 table 初始化为 automatically/by 默认值的模型?
我尝试过的东西:
- 在
my_model
中的任何操作之前添加 tf.tables_initializer()
作为依赖项
这适用于 第一次 我调用我部署的模型但每次后续调用都失败并显示一条错误消息告诉我 table 已经初始化
- 将
tf.tables_initializer()
作为 legacy_init_op
参数传递给 add_meta_graph_and_variables
方法。
这根本无法部署到 ml-engine,并显示错误消息
Create Version failed. Bad model detected with error: "Failed to load
model: a bytes-like object is required, not 'str' (Error code: 0)"
- 将
tf.tables_initializer()
或 tf.saved_model.main_op.main_op()
作为 main_op
参数传递给 add_meta_graph_and_variables
方法。
在每种情况下,它仍然无法部署模型,并出现与 (2.) 中相同的错误消息:
Create Version failed. Bad model detected with error: "Failed to load
model: a bytes-like object is required, not 'str' (Error code: 0)"
我们必须将表初始化操作作为 main_op
传递,而且至关重要的是,我们需要在对 add_meta_graph_and_variables
:
的调用中包含 assets_collection
init_op = tf.tables_initializer()
with tf.Session() as sess:
sess.run([tf.global_variables_initializer(), init_op])
example_result = sess.run(
my_model,
feed_dict=###snip###
)
然后
export_builder.add_meta_graph_and_variables(
sess,
[tf.saved_model.tag_constants.SERVING],
<b>main_op = init_op,</b>
<b>assets_collection=tf.get_collection(tf.GraphKeys.ASSET_FILEPATHS),</b>
signature_def_map={
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: sig
}
)
作为(可以说更简单的)替代方案,我们可以使用 simple_save
方法来保存我们的模型
我希望将经过训练的模型部署到 ml-engine。我可以在本地 运行 我的代码,例如:
with tf.Session() as sess:
sess.run([tf.global_variables_initializer(), tf.tables_initializer()])
example_result = sess.run(
my_model,
feed_dict=###snip###
)
我一直在尝试导出:
export_builder.add_meta_graph_and_variables(
sess,
[tf.saved_model.tag_constants.SERVING],
signature_def_map={
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: sig
})
然后我可以将其部署到 ml-engine。但是,当我调用模型时出现错误,因为 table 未初始化。
如何部署 table 初始化为 automatically/by 默认值的模型?
我尝试过的东西:
- 在
my_model
中的任何操作之前添加
tf.tables_initializer()
作为依赖项
这适用于 第一次 我调用我部署的模型但每次后续调用都失败并显示一条错误消息告诉我 table 已经初始化
- 将
tf.tables_initializer()
作为legacy_init_op
参数传递给add_meta_graph_and_variables
方法。
这根本无法部署到 ml-engine,并显示错误消息
Create Version failed. Bad model detected with error: "Failed to load model: a bytes-like object is required, not 'str' (Error code: 0)"
- 将
tf.tables_initializer()
或tf.saved_model.main_op.main_op()
作为main_op
参数传递给add_meta_graph_and_variables
方法。
在每种情况下,它仍然无法部署模型,并出现与 (2.) 中相同的错误消息:
Create Version failed. Bad model detected with error: "Failed to load model: a bytes-like object is required, not 'str' (Error code: 0)"
我们必须将表初始化操作作为 main_op
传递,而且至关重要的是,我们需要在对 add_meta_graph_and_variables
:
assets_collection
init_op = tf.tables_initializer()
with tf.Session() as sess:
sess.run([tf.global_variables_initializer(), init_op])
example_result = sess.run(
my_model,
feed_dict=###snip###
)
然后
export_builder.add_meta_graph_and_variables(
sess,
[tf.saved_model.tag_constants.SERVING],
<b>main_op = init_op,</b>
<b>assets_collection=tf.get_collection(tf.GraphKeys.ASSET_FILEPATHS),</b>
signature_def_map={
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: sig
}
)
作为(可以说更简单的)替代方案,我们可以使用 simple_save
方法来保存我们的模型