import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_DATA',one_hot=True)
def weights_variable(shape):
return tf.Variable(tf.truncated_normal(shape,stddev=0.1))
def bias_variable(shape):
return tf.constant(0.1,shape=shape)
def conv2d(x, Kernel):
return tf.nn.conv2d(x, Kernel, strides=[1,1,1,1],padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
xs = tf.placeholder(tf.float32, [None, 784])
ys = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(xs, [-1, 28, 28, 1])
# Build net
with tf.name_scope('Conv1'):
w_conv1 = weights_variable([5,5,1,32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
with tf.name_scope('Conv2'):
w_conv2 = weights_variable([5,5,32,64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
with tf.name_scope('Full1'):
fc_input = tf.reshape(h_pool2, [-1, 7*7*64])
w_fc1 = weights_variable([7*7*64, 1024])
b_fc1 = bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(fc_input, w_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob=keep_prob)
with tf.name_scope('Full2'):
w_fc2 = weights_variable([1024, 10])
b_fc2 = bias_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, w_fc2) + b_fc2)
with tf.name_scope('Loss'):
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction)))
tf.summary.scalar('Cross_entropy', cross_entropy)
with tf.name_scope('Accuracy'):
correct = tf.equal(tf.argmax(ys, 1), tf.argmax(prediction, 1))
accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
tf.summary.scalar('Accuracy',accuracy)
with tf.name_scope('Train'):
train = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, 'model/cnn_saver.ckpt')
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter('log/',sess.graph)
sess.run(init)
for step in range(1,1001):
batch_xs, batch_ys = mnist.train.next_batch(64)
sess.run(train,feed_dict={xs: batch_xs, ys:batch_ys, keep_prob:0.5})
if step % 10 == 0:
print('Step: ',step,' || Accuracy: ',sess.run(accuracy,feed_dict={xs: batch_xs,ys: batch_ys,keep_prob:1}))
writer.add_summary(sess.run(merged, feed_dict={xs: batch_xs,ys: batch_ys,keep_prob:0.5}) ,step)
saver.save(sess, 'model/cnn_saver.ckpt')
以cnn分类mnist为测试。首先构建一个tf.train.Saver()
对象,然后使用save()
保存网络参数,使用restore()
加载网络参数。测试如下:
存储
代码
import tensorflow as tf
data = tf.Variable(tf.random_normal([2,2]))
saver = tf.train.Saver()
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(sess.run(data))
saver.save(sess, 'model/test.ckpt')
输出
[[ 1.20965123 -1.97269511]
[-0.66441923 -1.37852442]]
加载
代码
import tensorflow as tf
data = tf.Variable(tf.random_normal([2,2]))
saver = tf.train.Saver()
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
saver.restore(sess, 'model/test.ckpt')
print(sess.run(data))
输出
[[ 1.20965123 -1.97269511]
[-0.66441923 -1.37852442]]