模型建的不好,最终只有85%左右的准确率,后面继续改进吧
#卷积神经网络API
卷积层:tf.nn.conv2d(input, #输入张量,具有[batch, height, width, channel]
filter, #过滤器大小[filter_height, filter_width, in_channels,out_channels]
strides,#步长,一般为1
padding #SAME VALID,same为填充0,输出结论与输入结论的长宽一样
)
池化层:tf.nn.max_pool(value, #[batch, height, width,channels]
ksize, #池化窗口大小
strides, #步长大小
padding #填充类型,SAME or VALID
)
#卷积神经网络识别mnist
import tensorflow as tf
import os
from tensorflow.examples.tutorials.mnist import input_data
def init_weight(shape):
"""
初始化权重参数
"""
w = tf.Variable(tf.random_normal(shape = shape, mean=0.0, stddev=1.0))
return w
def init_bias(shape):
"""
初始化偏置参数
"""
b = tf.Variable(tf.random_normal(shape = shape, mean=0.0, stddev=1.0))
return b
def modol():
"""
卷积神经网络模型
"""
# 1、准备数据占位符 x[None, 784], y[None, 10]
with tf.variable_scope("Input_data"):
x = tf.placeholder(tf.float32, [None, 784])
y_true = tf.placeholder(tf.int32, [None, 10])
# 2、卷积层1
with tf.variable_scope("Conv1"):
# 随机初始化参数
w1 = init_weight([5,5,1,32])
b1 = init_bias([32])
# 改变x从[None,784] -> [None,28, 28, 1]
x_reshape = tf.reshape(x, [-1, 28, 28, 1])
# 进行卷积操作,x_relu[None,28,28,32]
x1_relu = tf.nn.relu(tf.nn.conv2d(x_reshape, w1, strides = [1,1,1,1], padding="SAME") + b1)
#池化层
x1_pool = tf.nn.max_pool(x1_relu, ksize=[1,2,2,1], strides = [1,2,2,1], padding="SAME")
# 3、卷积层2:输入为:[None,14,14,32]
# 随机初始化参数
w2 = init_weight([5,5,32,64])
b2 = init_bias([64])
# 进行卷积操作,x_relu[None,14,14,64]
x2_relu = tf.nn.relu(tf.nn.conv2d(x1_pool, w2, strides = [1,1,1,1], padding="SAME") + b2)
#池化层[None,14,14,64] -> [None,7,7,64]
x2_pool = tf.nn.max_pool(x2_relu, ksize=[1,2,2,1], strides = [1,2,2,1], padding="SAME")
# 4、全连接层,输入为[None,7,7,64] * [7*7*64,10] = [None,10]
#随机初始化权重和偏置
w_fc = init_weight([7*7*64,10])
b_fc = init_bias([10])
#修改矩阵大小[None,7,7,64] -> [None,7*7*64]
fc_reshape = tf.reshape(x2_pool, [-1,7*7*64])
#进行矩阵运算[None, 10]
y_predict = tf.matmul(fc_reshape, w_fc) + b_fc
return x, y_true, y_predict
def conv_fc():
mnist = input_data.read_data_sets("./mnist/input_data/", one_hot=True)
x, y_true, y_predict = modol()
#计算交叉熵损失
with tf.variable_scope("soft_cross"):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_true, logits = y_predict))
with tf.variable_scope("opitmizer"):
train_op = tf.train.GradientDescentOptimizer(0.0001).minimize(loss)
with tf.variable_scope("accuracy"):
equal_list = tf.equal(tf.argmax(y_true, 1), tf.argmax(y_predict,1))
accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))
init_op = tf.global_variables_initializer()
print("asd")
with tf.Session() as sess:
sess.run(init_op)
#循环训练
for i in range(5000):
# API获取一个训练batch
mnixt_x, mnist_y = mnist.train.next_batch(50)
# 运行op训练
sess.run(train_op, feed_dict = {x: mnixt_x, y_true: mnist_y})
print("after %d times of trainings, accuracy is %f" %
(i, sess.run(accuracy, feed_dict = {x: mnixt_x, y_true: mnist_y})))
return None
if __name__ == '__main__':
conv_fc()