如何使用saved_model API定期保存tensorflow模型? [英] How to periodically save tensorflow model using saved_model API?

查看:23
本文介绍了如何使用saved_model API定期保存tensorflow模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此出于各种原因(例如其语言独立性),我想使用 tensorflow 的 saved_model 用于保存/加载模型的 API.我可以在训练结束时调用 builder.add_meta_graph_and_variables() 来保存所有内容(并成功恢复),但我没有看到任何定期保存的方法.这方面的 Tensorflow 文档非常稀少,他们提供的模板代码(here) 对我没有帮助:

So for various reasons (such as its language-independence) I want to use tensorflow's saved_model API for saving/loading models. I can save everything (and restore it successfully) with a call to builder.add_meta_graph_and_variables() at the end of training, but I don't see any way to save periodically. Tensorflow docs on this are very sparse, and the template code they provide (here) doesn't help me:

...
builder = tf.saved_model.builder.SavedModelBuilder(export_dir)

with tf.Session(graph=tf.Graph()) as sess:
  ...
  builder.add_meta_graph_and_variables(sess,
                                  ["foo-tag"],
                                  signature_def_map=foo_signatures,
                                  assets_collection=foo_assets)
...

with tf.Session(graph=tf.Graph()) as sess:
  ...
  builder.add_meta_graph(["bar-tag", "baz-tag"])
...

builder.save()

调用 builder.save() 不会将新变量保存到模型中.它只是更新模型protobuf.

Calling builder.save() does not save the new variables into the model. It just updates the model protobuf.

我错过了什么?之后如何保存,例如使用 saved_model 的第 n 个 epoch?

What am I missing? How do I save after e.g. the nth epoch using saved_model?

推荐答案

好吧,看完 tensorflow 代码后 这里 和其他地方,看起来答案是你不能".SavedModelBuilder 实际上只是为训练阶段之外的模型设计的,它允许您添加元图并选择要加载/保存的变量集(即 TRAINING 与 SERVING),仅此而已.例如,SavedModelBuilder.add_meta_graph_and_variables 可以被调用一次,并且没有 SavedModelBuilder.update_variables 或类似的东西.另一方面,在训练时,您需要使用 Saver 类并保存检查点和那些相关文件.为什么没有一个统一的系统,我不知道,但显然就是这样.

Well, after looking through the tensorflow code here and elsewhere, it looks like the answer is "you can't". SavedModelBuilder is really just designed for models outside of the training phase, and it allows you to add metagraphs and choose which sets of variables to load/save (i.e. TRAINING vs. SERVING) but that's it. SavedModelBuilder.add_meta_graph_and_variables, for example, can be called exactly once, and there is no SavedModelBuilder.update_variables or anything like that. While training, on the other hand, you need to use the Saver class and save checkpoints and those associated files. Why there isn't a unified system for this I have no idea but apparently that's the way it is.

这篇关于如何使用saved_model API定期保存tensorflow模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆