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

Tensorflow1.15.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) ]]

以上是 Tensorflow1.15.0 模型保存与还原的问题 的全部内容, 来源链接: utcz.com/p/937788.html

回到顶部