我已经在MNIST数据集上训练了TensorFlow(TF)CNN模型,并在训练后使用 tf.train.Saver()存储了模型。现在,我想恢复这个模型并在重新训练之前在两个层之间添加一个操作,例如,在第一个卷积层的输出(名为'conv1')之间添加 tf.stop_gradient()和第一个汇集层(名为'pool1')。那可能吗?如果是这样,我怎么能这样做?
这是我的代码:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/home/frr/MNIST_data", one_hot=True)
learning_rate = 0.001
training_iters = 100000
batch_size = 64
display_step = 20
ImVecDim = 784
NumOfClasses = 10
dropout = 0.8
g = tf.get_default_graph()
with tf.Session() as sess:
LoadMod = tf.train.import_meta_graph('simple_mnist.ckpt.meta')
LoadMod.restore(sess, tf.train.latest_checkpoint('./'))
#############################################################################
# Adding a tf.stop_gradient() operation between 'conv1' and 'pool1' #
#############################################################################
x = g.get_tensor_by_name('ImageIn:0')
y = g.get_tensor_by_name('LabelIn:0')
keep_prob = g.get_tensor_by_name('KeepProb:0')
accuracy = g.get_tensor_by_name('NetAccuracy:0')
optimizer = g.get_operation_by_name('Adam')
step = 1
while step * batch_size < training_iters:
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_prob: dropout})
if step % display_step == 0:
acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.})
print("Iter " + str(step*batch_size) + ", Training Accuracy= " + "{:.5f}".format(acc))
step += 1
print("Optimization Finished!")
print("Testing Accuracy:",
sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.}))