无法在 tensorflow v0.8 中恢复模型

Unable to restore models in tensorflow v0.8

我正在尝试恢复保存的模型。但它给我返回一个错误。请帮帮我。 保存模型的代码: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.")

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

我怀疑出现此错误是因为在 save_model.py 中您将变量声明为 tf.float32 类型(1.321.33 的隐式类型),而在restore_model.py 您将变量定义为 tf.int32 类型(0 的隐式类型)。

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

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