【发布时间】:2017-11-06 18:45:06
【问题描述】:
我完全迷失了 tensorflow saver 方法。
我正在尝试学习基本的 tensorflow 深度神经网络模型教程。我想弄清楚如何训练网络进行几次迭代,然后在另一个会话中加载模型。
with tf.Session() as sess:
graph = tf.Graph()
x = tf.placeholder(tf.float32,shape=[None,784])
y_ = tf.placeholder(tf.float32, shape=[None,10])
sess.run(global_variables_initializer())
#Define the Network
#(This part is all copied from the tutorial - not copied for brevity)
#See here: https://www.tensorflow.org/versions/r0.12/tutorials/mnist/pros/
跳过训练。
#Train the Network
train_step = tf.train.AdamOptimizer(1e-4).minimize(
cross_entropy,global_step=global_step)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
saver = tf.train.Saver()
for i in range(101):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict=
{x:batch[0],y_:batch[1]})
print 'Step %d, training accuracy %g'%(i,train_accuracy)
train_step.run(feed_dict={x:batch[0], y_: batch[1]})
if i%100 == 0:
print 'Test accuracy %g'%accuracy.eval(feed_dict={x:
mnist.test.images, y_: mnist.test.labels})
saver.save(sess,'./mnist_model')
控制台打印出来:
第 0 步,训练精度 0.16
测试精度 0.0719
Step 100,训练精度 0.88
测试精度 0.8734
接下来我要加载模型
with tf.Session() as sess:
saver = tf.train.import_meta_graph('mnist_model.meta')
saver.restore(sess,tf.train.latest_checkpoint('./'))
sess.run(tf.global_variables_initializer())
现在我想重新测试一下是否加载了模型
print 'Test accuracy %g'%accuracy.eval(feed_dict={x:
mnist.test.images, y_: mnist.test.labels})
控制台打印出来:
测试精度 0.1151
模型似乎没有保存任何数据?我做错了什么?
【问题讨论】:
-
你不应该在恢复权重后运行
sess.run(tf.global_variables_initializer())。这将重置您的所有权重
标签: python-2.7 tensorflow deep-learning