cnn+pooling, save and restore, 第一次结构比较完整的代码
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
#hyper parameters
save_path = 'model'
max_globel_step = 1000
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
def add_layer(input, in_size, out_size, name='layer', activation=None):
with tf.variable_scope(name):
Weight = tf.get_variable(name='Weight', shape=(in_size, out_size), dtype=tf.float32, initializer=tf.random_normal_initializer())
bias = tf.get_variable(name='bias', shape=(out_size), dtype=tf.float32, initializer=tf.constant_initializer(0.1))
Wx_b = tf.matmul(input, Weight) + bias
if activation != None:
output = activation(Wx_b)
else:
output = Wx_b
return output
def weight_variable(x):
init = tf.truncated_normal(shape=x, stddev=0.1)
w = tf.Variable(init)
return w
def bias_variable(x):
init = tf.constant(0.1, shape=x)
return tf.Variable(init)
def conv2d(x, w):
return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')
def max_pooling_2X2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#network structure
class nn(object):
def __init__(self, x, y_label, trainning=True, reuse=False):
with tf.variable_scope('nn', reuse=reuse):
self.global_step = tf.Variable(0, trainable=False, dtype=tf.int32, name='global_step')
with tf.variable_scope('predict'):
#x (batch, 784) -> (batch, 28, 28, 1)
x_pic = tf.reshape(x, shape=(-1, 28, 28, 1))
#1st conv+pooling [-1, 28, 28 , 1] -> [-1, 14, 14, 32] patch: 5*5
'''用tf.nn的conv莫名其妙的无法训练
weight_conv1 = weight_variable([5, 5, 1, 32])
bias_conv1 = bias_variable([32])
conv_conv1 = tf.nn.relu(conv2d(x_pic, weight_conv1) + bias_conv1)
pooling_conv1 = max_pooling_2X2(conv_conv1)
'''
conv_conv1 = tf.nn.relu(tf.layers.conv2d(x_pic, filters=32, kernel_size=[5, 5],
strides=[1, 1], padding='SAME'))
pooling_conv1 = max_pooling_2X2(conv_conv1)
# 2st conv+pooling [-1, 14, 14 , 32] -> [-1, 7, 7, 64] patch: 5*5
conv_conv2 = tf.nn.relu(tf.layers.conv2d(pooling_conv1, filters=64, kernel_size=[5, 5],
strides=[1, 1], padding='SAME'))
pooling_conv2 = max_pooling_2X2(conv_conv2)
#nn fc (-1, 7, 7, 64) -> (-1, 7*7*64) -> (-1, 1024)
pl_conv2 = tf.reshape(pooling_conv1, shape=(-1, 14*14*32))
# pl_conv2 = tf.reshape(pooling_conv2, shape=(-1, 7*7*64))
y = add_layer(pl_conv2, 14*14*32, 1024, activation=tf.nn.relu)
# y = add_layer(pl_conv2, 7*7*64, 1024, activation=tf.nn.relu)
y = tf.layers.dropout(y, rate=0, training=trainning)
#nn fc (-1, 1024) -> (-1, 10)
y = add_layer(y, 1024, 10, name='layer2', activation=tf.nn.softmax)
with tf.variable_scope('loss'):
self.cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_label * tf.log(y + 0.000001),
reduction_indices=[1])) # loss
with tf.variable_scope('accuracy'):
correct = tf.equal(tf.arg_max(y, 1), tf.arg_max(y_label, 1))
self.acuracy = tf.reduce_mean(tf.cast(correct, 'float'))
def get_summary(self):
return tf.summary.scalar('loss', self.cross_entropy)
#placehoder
x = tf.placeholder(name='x', shape=(None, 784), dtype=tf.float32)
y_label = tf.placeholder(name='y_label', shape=(None, 10), dtype=tf.float32)
#model
train_model = nn(x, y_label, trainning=True, reuse=False)
dev_model = nn(x, y_label, trainning=False, reuse=True)
#optimize
with tf.variable_scope('optimizer'):
train_opt = tf.train.AdamOptimizer(1e-4).minimize(train_model.cross_entropy, global_step=train_model.global_step)
#saver
saver = tf.train.Saver()
#run sess
with tf.Session() as sess:
# summary
train_model.get_summary()
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter('logs/train', graph=sess.graph)
test_writer = tf.summary.FileWriter('logs/test', graph=sess.graph)
#restore or initailize
ckpt = tf.train.get_checkpoint_state(save_path)
if ckpt:
ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
saver.restore(sess, os.path.join(save_path, ckpt_name))
else:
sess.run(tf.global_variables_initializer())
global_step_val = sess.run(train_model.global_step)
while global_step_val < max_globel_step:
batch_x, batch_y = mnist.train.next_batch(100)
sess.run(train_opt, feed_dict={x:batch_x, y_label:batch_y})
global_step_val += 1
if global_step_val % 50 == 0:
train_summary = sess.run(merged, feed_dict={x:mnist.train.images[:1000], y_label:mnist.train.labels[:1000]})
test_summary = sess.run(merged, feed_dict={x:mnist.test.images[:1000], y_label:mnist.test.labels[:1000]})
train_writer.add_summary(train_summary, global_step_val)
test_writer.add_summary(test_summary, global_step_val)
print(global_step_val)
print('train')
print(sess.run(dev_model.acuracy, feed_dict={x:mnist.train.images[:1000], y_label:mnist.train.labels[:1000]}))
print('test')
print(sess.run(dev_model.acuracy, feed_dict={x:mnist.test.images[:1000], y_label:mnist.test.labels[:1000]}))
if global_step_val % 100 == 0:
print('save')
saver.save(sess, os.path.join(save_path, 'mnist.ckpt'), global_step_val)