【问题标题】:Tensorflow: How to save the model in the memory while trainingTensorflow:如何在训练时将模型保存在内存中
【发布时间】:2017-04-27 09:14:01
【问题描述】:

我在 tensorflow 中的训练过程涉及在两个模型之间切换。 而使用 tf.saver 并从硬盘恢复模型确实很耗时(在我的代码中,切换很频繁),因此,我想知道有没有办法将模型参数存储在内存中并恢复它们从记忆里。我的模型很小,绝对可以存储在 RAM 中。 stackoverflow 有一个答案。 Storing tensorflow models in memory 但是,我不太明白这是如何工作的。有谁知道如何实现这个目标?谢谢。

【问题讨论】:

  • 您在理解方面具体需要哪些帮助?您链接的问题确实有答案。
  • 我不明白如何使用它。 Comp1 和 Comp2 究竟是什么意思?

标签: python model tensorflow tensorflow-serving


【解决方案1】:

你应该像这样使用两个单独的图表:

g1 = tf.Graph()
g2 = tf.Graph()

with g1.as_default():
  # build your 1st model
  sess1 = tf.Session(graph=g1)
  # do some work with sess1 on g1
  sess1.run(...)

with g2.as_default():
  # build your 2nd model
  sess2 = tf.Session(graph=g2)
  # do some work with sess2 on g2
  sess2.run(...)

with g1.as_default():
  # do some more work with sess1 on g1 
  sess1.run(...)

with g2.as_default():
  # do some more work with sess2 on g2
  sess2.run(...)

sess1.close()
sess2.close()

您实际上并不需要with 语句,一旦您创建了sess1sess2,您就可以使用它们,它们将引用正确的图表,但设置当您仍在习惯 TF 如何处理全局变量时使用该图时的默认图。

【讨论】:

猜你喜欢
  • 2020-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 2021-11-24
  • 2018-02-13
  • 1970-01-01
相关资源
最近更新 更多