Tensorflow1.15.0 模型保存与还原的问题

发布于 2022-09-12 01:45:59 字数 2471 浏览 28 评论 0

保存了一个训练好的模型,但无法正常恢复

MINIST手写数字识别单隐藏层的保存完成后,利用saver.restore()恢复训练好的模型时报错

相关代码

`
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data 

mnist=input_data.read_data_sets("E:\TF1.9\data",one_hot=True)
x=tf.placeholder(tf.float32,[None,784],name='X')
y=tf.placeholder(tf.float32,[None,10],name='Y')

#构建隐藏层
H1_NN=256
W1=tf.Variable(tf.random_normal([784,H1_NN]))
b1=tf.Variable(tf.zeros([H1_NN]))
Y1=tf.nn.relu(tf.matmul(x,W1)+b1)

#构建输出层
W2=tf.Variable(tf.random_normal([H1_NN,10]))
b2=tf.Variable(tf.zeros([10]))

forward=tf.matmul(Y1,W2)+b2
pred=tf.nn.softmax(forward)

#交叉熵
loss_function=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=forward,
labels=y))

train_epochs=40
batch_size=50
total_batch=int(mnist.train.num_examples/batch_size)
display_step=1
learning_rate=0.01

ckpt_dir='./save_step'

optimizer=tf.train.AdamOptimizer(learning_rate).minimize(loss_function)

correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(pred,1))

accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))


saver = tf.train.Saver()

sess=tf.Session()
sess.run(tf.global_variables_initializer())

ckpt=tf.train.get_checkpoint_state(ckpt_dir)#选取一份最新的存盘文件
print(ckpt.model_checkpoint_path)

if ckpt and ckpt.model_checkpoint_path:
    print('dir exists')
    saver.restore(sess,ckpt.model_checkpoint_path)
    print("Restore model from"+ckpt.model_checkpoint_path)

`

实际看到的错误信息又是什么?

NotFoundError: Key Variable/Adam not found in checkpoint
     [[{{node save/RestoreV2}}]]

During handling of the above exception, another exception occurred:

...
...

NotFoundError: Key Variable/Adam not found in checkpoint
     [[node save/RestoreV2 (defined at C:\Users\reza\.conda\envs\TF1.9\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]
...     
...
NotFoundError: Key _CHECKPOINTABLE_OBJECT_GRAPH not found in checkpoint

During handling of the above exception, another exception occurred:
...   
...


NotFoundError: Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Key Variable/Adam not found in checkpoint
     [[node save/RestoreV2 (defined at C:\Users\reza\.conda\envs\TF1.9\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]

  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文