gpt4 book ai didi

tensorflow - 如何更改 SavedModel 的签名而不重新训练模型?

转载 作者:行者123 更新时间:2023-12-02 20:49:30 25 4
gpt4 key购买 nike

我刚刚完成模型的训练,却发现我导出的服务模型存在签名问题。我如何更新它们?

(一个常见问题是为 CloudML Engine 设置错误的形状)。

最佳答案

别担心——您不需要重新训练您的模型。也就是说,还有一些工作要做。您将创建一个新的(已更正的)服务图表,将检查点加载到该图表中,然后导出该图表。

例如,假设您添加了一个占位符,但没有设置形状,即使您打算这样做(例如,在 CloudML 上运行)。在这种情况下,您的图表可能如下所示:

x = tf.placeholder(tf.float32)
y = foo(x)
...

要纠正这个问题:

# Create the *correct* graph
with tf.Graph().as_default() as new_graph:
x = tf.placeholder(tf.float32, shape=[None])
y = foo(x)
saver = tf.train.Saver()

# (Re-)define the inputs and the outputs.
inputs = {"x": tf.saved_model.utils.build_tensor_info(x)}
outputs = {"y": tf.saved_model.utils.build_tensor_info(y)}
signature = tf.saved_model.signature_def_utils.build_signature_def(
inputs=inputs,
outputs=outputs,
method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
)

with tf.Session(graph=new_graph) as session:
# Restore the variables
vars_path = os.path.join(old_export_dir, 'variables', 'variables')
saver.restore(session, vars_path)

# Save out the corrected model
b = builder.SavedModelBuilder(new_export_dir)
b.add_meta_graph_and_variables(session, ['serving_default'], signature)
b.save()

关于tensorflow - 如何更改 SavedModel 的签名而不重新训练模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42801551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com