Tensorflow:关于如何保存经过训练的模型的差异
Tensorflow: differences on how to save a trained model
我注意到在 Python 中有几种保存训练模型的方法
但是我看不出它们之间的真正区别。
检查点
saver = tf.train.Saver()
saver.save(session, output_path)
冻结
from tensorflow.python.framework import graph_util
input_graph_def = graph.as_graph_def()
output_graph_def = graph_util.convert_variables_to_constants(
session, input_graph_def, output_nodes_names)
with tf.gfile.GFile(output_graph, "wb") as output_graph_file:
output_graph_file.write(output_graph_def.SerializeToString())
SavedModelBuilder
builder = tf.saved_model.builder.SavedModelBuilder(output_path)
builder.add_meta_graph_and_variables(
session,
[tf.saved_model.tag_constants.SERVING],
clear_devices=True)
builder.save()
让我们考虑不同的场景:evaluation/inference、微调、服务 API、导出到其他框架。
为每种情况保存模型的最佳方法是什么?是否有关于何时使用一种方法或另一种方法的规则?
谢谢
这不是一个详尽的答案,但对于现代(2018 年年中)TensorFlow,您可能只需要检查点和保存模型。
正如
中所指出的
https://www.tensorflow.org/get_started/checkpoints
"Checkpoints - a format dependent on the code that created the model"
"SavedModel - a format independent of the code that created the model"
"Freezing" 很大程度上被折叠到 SavedModel 中并被 SavedModel 取代。
在你的训练代码中,虽然你仍然想保留继续 training/fine-tuning 的能力,但检查点是可行的方法,因为所有相关的 code/state 不仅可以训练,还可以监控培训在检查点和您的代码之间进行。
当您转到 "serving" 端(即消费)时,您添加使用模型所需的所有元数据,去除不需要的训练元素并转到 SavedModel。
我个人没有尝试从 TensorFlow 导出 到 其他框架,只是导出到它,所以我无法就哪种情况最适合提供好的意见。
我注意到在 Python 中有几种保存训练模型的方法 但是我看不出它们之间的真正区别。
检查点
saver = tf.train.Saver() saver.save(session, output_path)
冻结
from tensorflow.python.framework import graph_util input_graph_def = graph.as_graph_def() output_graph_def = graph_util.convert_variables_to_constants( session, input_graph_def, output_nodes_names) with tf.gfile.GFile(output_graph, "wb") as output_graph_file: output_graph_file.write(output_graph_def.SerializeToString())
SavedModelBuilder
builder = tf.saved_model.builder.SavedModelBuilder(output_path) builder.add_meta_graph_and_variables( session, [tf.saved_model.tag_constants.SERVING], clear_devices=True) builder.save()
让我们考虑不同的场景:evaluation/inference、微调、服务 API、导出到其他框架。 为每种情况保存模型的最佳方法是什么?是否有关于何时使用一种方法或另一种方法的规则?
谢谢
这不是一个详尽的答案,但对于现代(2018 年年中)TensorFlow,您可能只需要检查点和保存模型。
正如
中所指出的https://www.tensorflow.org/get_started/checkpoints
"Checkpoints - a format dependent on the code that created the model"
"SavedModel - a format independent of the code that created the model"
"Freezing" 很大程度上被折叠到 SavedModel 中并被 SavedModel 取代。
在你的训练代码中,虽然你仍然想保留继续 training/fine-tuning 的能力,但检查点是可行的方法,因为所有相关的 code/state 不仅可以训练,还可以监控培训在检查点和您的代码之间进行。
当您转到 "serving" 端(即消费)时,您添加使用模型所需的所有元数据,去除不需要的训练元素并转到 SavedModel。
我个人没有尝试从 TensorFlow 导出 到 其他框架,只是导出到它,所以我无法就哪种情况最适合提供好的意见。