设置神经网络结构(向前传播)
#coding:utf-8
import tensorflow as tf
IMAGE_SIZE = 28
NUM_CHANNELS = 1
CONV1_SIZE = 5
CONV1_KERNEL_NUM = 32
CONV2_SIZE = 5
CONV2_KERNEL_NUM = 64
FC_SIZE = 512
OUTPUT_NODE = 10
# 初始化神经元
def get_weight(shape, regularizer):
"""
:param shape: 输入形状
:param regularizer: 正则化
:return:
"""
w = tf.Variable(tf.truncated_normal(shape,stddev=0.1))
if regularizer != None: tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w))
return w
# 添加偏置
def get_bias(shape):
b = tf.Variable(tf.zeros(shape))
return b
# 卷积操作
def conv2d(x,w):
# 全零填充,步长为1,第一个第四个参数为1,第二第三为步长
return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
# 池化ksize表示池化尺寸2*2,步长为2
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# 搭建卷积神经网络体系
def forward(x, train, regularizer):
# 创建第一层卷积层
conv1_w = get_weight([CONV1_SIZE, CONV1_SIZE, NUM_CHANNELS, CONV1_KERNEL_NUM], regularizer)
# 创建第一次偏置层
conv1_b = get_bias([CONV1_KERNEL_NUM])
# 卷积操作
conv1 = conv2d(x, conv1_w)
# 偏置操作,并去relu
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_b))
# 池化操作
pool1 = max_pool_2x2(relu1)
conv2_w = get_weight([CONV2_SIZE, CONV2_SIZE, CONV1_KERNEL_NUM, CONV2_KERNEL_NUM],regularizer)
conv2_b = get_bias([CONV2_KERNEL_NUM])
conv2 = conv2d(pool1, conv2_w)
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_b))
pool2 = max_pool_2x2(relu2)
# 获取经过第二层池化后的张量形状
pool_shape = pool2.get_shape().as_list()
nodes = pool_shape[1] * pool_shape[2] * pool_shape[3]
# 改变张量形状,变为一维张量
reshaped = tf.reshape(pool2, [pool_shape[0], nodes])
# 经过第一层全连接层,正则化
fc1_w = get_weight([nodes, FC_SIZE], regularizer)
fc1_b = get_bias([FC_SIZE])
fc1 = tf.nn.relu(tf.matmul(reshaped, fc1_w) + fc1_b)
# 经过第二层全连接层使用0.5的dropout避免过拟合
if train: fc1 = tf.nn.dropout(fc1, 0.5)
# 经过
fc2_w = get_weight([FC_SIZE, OUTPUT_NODE], regularizer)
fc2_b = get_bias([OUTPUT_NODE])
y = tf.matmul(fc1, fc2_w) + fc2_b
return y
训练(向后传播)
# coding:utf-8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_lenet5_forward
import os
import numpy as np
BATCH_SIZE = 100 # 100张图片为一批次
LEARNING_RATE_BASE = 0.005 # 学习率
LEARNING_RATE_DECAY = 0.99 # 学习衰减率
REGULARIZER = 0.0001 # 正则化参数
STEPS = 50000 # 训练步长
MOVING_AVERAGE_DECAY = 0.99 # 滑动平均衰减率
MODEL_SAVE_PATH = "./model/" # 模型保存路径
MODEL_NAME = "mnist_model" # 模型保存名字
def backward(mnist):
# 创建输入数据占位符
x = tf.placeholder(tf.float32, [
BATCH_SIZE,
mnist_lenet5_forward.IMAGE_SIZE,
mnist_lenet5_forward.IMAGE_SIZE,
mnist_lenet5_forward.NUM_CHANNELS])
# 输出数据占位符
y_ = tf.placeholder(tf.float32, [None, mnist_lenet5_forward.OUTPUT_NODE])
# 用向前传播,计算输出数据
y = mnist_lenet5_forward.forward(x, True, REGULARIZER)
# 记录步长
global_step = tf.Variable(0, trainable=False)
# 使用交叉熵代价函数
ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1)) #注意ce为向量
# 取平均得到数
cem = tf.reduce_mean(ce)
# 加入正则化系数
loss = cem + tf.add_n(tf.get_collection('losses'))
# 指数衰减学习率,计算公式为 learning_rate = learning_rate * learning_rate_decay^(global_step/decay_step)
learning_rate = tf.train.exponential_decay(
LEARNING_RATE_BASE,
global_step,
mnist.train.num_examples / BATCH_SIZE,
LEARNING_RATE_DECAY,
staircase=True)
# 梯度下降优化
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
# 下面是一个滑动平均模型
ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
ema_op = ema.apply(tf.trainable_variables())
with tf.control_dependencies([train_step, ema_op]):
train_op = tf.no_op(name='train')
# 保存模型
saver = tf.train.Saver()
# 启动会话
with tf.Session() as sess:
# 初始化
init_op = tf.global_variables_initializer()
sess.run(init_op)
# 通过checkpoint来定位模型
ckpt = tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
# 如果已经训练过模型,加载训练好的模型
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
# 开始训练
for i in range(STEPS):
# 每次取一批次的数据和标注
xs, ys = mnist.train.next_batch(BATCH_SIZE)
reshaped_xs = np.reshape(xs, (
BATCH_SIZE,
mnist_lenet5_forward.IMAGE_SIZE,
mnist_lenet5_forward.IMAGE_SIZE,
mnist_lenet5_forward.NUM_CHANNELS))
_, loss_value, step = sess.run([train_op, loss, global_step], feed_dict={x: reshaped_xs, y_: ys})
# 每100次输出一次,并保存模型
if i % 100 == 0:
print("After %d training step(s), loss on training batch is %g." % (step, loss_value))
saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
def main():
# 读取模型,one_hot代表向量话
mnist = input_data.read_data_sets("./data/", one_hot=True)
backward(mnist)
if __name__ == '__main__':
main()
预测
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_lenet5_forward
import mnist_lenet5_backward
import numpy as np
TEST_INTERVAL_SECS = 5
# 测试函数
def test(mnist):
# 创建一个图
with tf.Graph().as_default() as g:
x = tf.placeholder(tf.float32,
[mnist.test.num_examples,
mnist_lenet5_forward.IMAGE_SIZE,
mnist_lenet5_forward.IMAGE_SIZE,
mnist_lenet5_forward.NUM_CHANNELS,
]
)
y_ = tf.placeholder(tf.float32, [None, mnist_lenet5_forward.OUTPUT_NODE])
# 使用向前传播计算结果,第二参数代表训练过程,第三个参数代表不使用正则化
y = mnist_lenet5_forward.forward(x, False, None)
# 使用滑动平均模型
ema = tf.train.ExponentialMovingAverage(mnist_lenet5_backward.MOVING_AVERAGE_DECAY)
ema_restore = ema.variables_to_restore()
# 保存模型
saver = tf.train.Saver(ema_restore)
# 计算训练率
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) # 比较预测值与实际值是否相等,为逻辑向量
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 求和取平均得到最终结果
while True:
with tf.Session() as sess:
# 查找check_point
ckpt = tf.train.get_checkpoint_state(mnist_lenet5_backward.MODEL_SAVE_PATH)
# 找到了,加载这个模型
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
# 加载对应的迭代步长
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
# 将图片用四维张量的形式传入
reshaped_x = np.reshape(mnist.test.images, (
mnist.test.num_examples,
mnist_lenet5_forward.IMAGE_SIZE,
mnist_lenet5_forward.IMAGE_SIZE,
mnist_lenet5_forward.NUM_CHANNELS))
# 计算准确率
accuracy_score = sess.run(accuracy, feed_dict={x: reshaped_x, y_: mnist.test.labels})
print("After %s training step(s), test accuracy = %g" % (global_step, accuracy_score))
else:
print('No checkpoint file found')
return
# 每个5秒搜索新的模型
time.sleep(TEST_INTERVAL_SECS)
def main():
mnist = input_data.read_data_sets("./data/", one_hot=True)
test(mnist)
if __name__ == '__main__':
main()