问题描述
我正在尝试恢复已保存的模型.但它返回给我一个错误.请帮帮我.保存模型的代码:save_model.py
I am trying to restore a saved model . But it is returning me an error. Please help me out.code to save the model : 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.")
我已将两个文件保存在同一目录中.
I have saved both the files in the same directory.
推荐答案
我怀疑错误是因为在 save_model.py
中你声明变量为 tf.float32
类型code>(1.32
和 1.33
的隐式类型),而在 restore_model.py
中,您将变量定义为具有 tf.int32
(0
的隐式类型).
I suspect the error is raised because in save_model.py
you declare the variables as having type tf.float32
(the implicit type of 1.32
and 1.33
), whereas in restore_model.py
you define the variables as having type tf.int32
(the implicit type of 0
).
最简单的解决方案是修改 restore_model.py
以将变量声明为 tf.float32
.例如,您可以执行以下操作:
The easiest solution would be to modify restore_model.py
to declare the variables as tf.float32
. For example, you could do the following:
v1 = tf.Variable(0.0, name="v1")
v2 = tf.Variable(0.0, name="v2")
这篇关于无法在 tensorflow v0.8 中恢复模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!