【问题标题】:Unable to restore models in tensorflow v0.8无法在 tensorflow v0.8 中恢复模型
【发布时间】:2016-09-08 07:52:33
【问题描述】:

我正在尝试恢复已保存的模型。但它给我一个错误。请帮帮我。 保存模型的代码:save_model.py

import tensorflow as tf
v1 = tf.Variable(1.32, name="v1")
v2 = tf.Variable(1.33, name="v2")

init = tf.initialize_all_variables()

saver = tf.train.Saver()

with tf.Session() as sess:
  sess.run(init)
  save_path = saver.save(sess, "model.ckpt")

恢复模型的代码:restore_model.py

import tensorflow as tf
v1 = tf.Variable(0, name="v1")
v2 = tf.Variable(0, name="v2")


saver = tf.train.Saver()

with tf.Session() as sess:
  saver.restore(sess, "model.ckpt")
  print("Model restored.")

我已将这两个文件保存在同一目录中。

【问题讨论】:

  • 请提供您收到的错误。
  • 您好,感谢您的回复。在 restore.py 中将初始值更改为 0.0 后,它可以正常工作:)

标签: tensorflow restore


【解决方案1】:

我怀疑会引发错误,因为在 save_model.py 中,您将变量声明为具有 tf.float32 类型(1.321.33 的隐式类型),而在 restore_model.py 中,您将变量定义为具有类型tf.int320 的隐式类型)。

最简单的解决方案是修改restore_model.py 以将变量声明为tf.float32。例如,您可以执行以下操作:

v1 = tf.Variable(0.0, name="v1")
v2 = tf.Variable(0.0, name="v2")

【讨论】:

  • 嗨,非常感谢!现在可以了。我将值更改为 0.0,它工作正常。但恢复后变量的值仍然是 0.0 而不是 1.32 和 1.33。有什么建议吗?
  • 您能否将完整更新的源代码分享给问题中的restore_model.py?永远不要将变量分配给 0.0(除非有一些额外的代码为它们运行初始化程序)。
  • 嗨,更新的代码。 import tensorflow as tf v1 = tf.Variable(0.0, name="v1") v2 = tf.Variable(0.0, name="v2") saver = tf.train.Saver() init = tf.initialize_all_variables() with tf .Session() as sess: save_path="model.ckpt" saver.restore(sess, save_path) #print v1.eval(sess) print("模型恢复。") print v1.eval()
猜你喜欢
  • 2019-11-25
  • 1970-01-01
  • 2018-09-06
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多