问答

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

作者:admin 2021-09-18 我要评论

保存了一个训练好的模型,但无法正常恢复 MINIST手写数字识别单隐藏层的保存完成后,利用saver.restore()恢复训练好的模型时报错 相关代码 `import tensorflow a...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

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

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) ]]

  

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • Tensorflow1.15.0 模型保存与还原的问

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

  • echart 漏斗图funnel更改图表的颜色为

    echart 漏斗图funnel更改图表的颜色为

  • podman 容器间如何建立联系及端口问题

    podman 容器间如何建立联系及端口问题

  • wavesurfer 在vue中加载网络音频资源跨

    wavesurfer 在vue中加载网络音频资源跨

腾讯云代理商
海外云服务器