基于MNIST设计神经网络识别手写数字(version 2 CNN结构)
这是基于卷积神经网络设计的,他的结构是两个卷积层加上两个全连接层。用tf.train.AdamOptimizer()
作为优化器进行优化。准确度比 version1.1(链接)要高。附上主要的代码。
1.所有文件
2.主要代码
1)设计神经网络模块(network_cnn.py)
import random
import numpy as np
from PIL import Image, ImageFilter
import tensorflow as tf
import matplotlib.pyplot as plt
class Network(object):
def add_layer(self, inputs, in_size, out_size, activation_function=None):
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size]))
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs
def weight_variable(self, shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(self, shape):
initial = tf.constant(0.1, shape=shape)
return initial
def conv2d(self, x, W):
return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME')
def max_pool_2_2(self, x):
return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
def compute_accurary(self, test_data):
x1,y1 = test_data[0],test_data[1]
print("x:{0},y:{1}".format(np.shape(x1), np.shape(y1)))
def scanimage(self, image):
#sess = tf.Session()
predict = self.add_layer(image, 784, 10, activation_function=tf.nn.softmax)
y_pre = sess.run(tf.argmax(predict, 0))
result = sess.run(y_pre)
return result
def start(self, training_inputs, training_results, epochs, batch_size, testimage):
print(np.shape(training_inputs))
print(np.shape(training_results))
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])
# conv1 layer
W_conv1 = self.weight_variable([5,5,1,32])
b_conv1 = self.bias_variable([32])
h_conv1 = tf.nn.relu(self.conv2d(x_image, W_conv1) + b_conv1) #28*28*32
h_pool1 = self.max_pool_2_2(h_conv1) #14*14*32
# conv2 layer
W_conv2 = self.weight_variable([5,5,32,64])
b_conv2 = self.bias_variable([64])
h_conv2 = tf.nn.relu(self.conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = self.max_pool_2_2(h_conv2)
# fc1 layer
h_pool2_flat = tf.reshape(h_pool2, [-1,7*7*64])
W_fc1 = self.weight_variable([7*7*64, 1024])
b_fc1 = self.bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# fc2
W_fc2 = self.weight_variable([1024, 10])
b_fc2 = self.bias_variable([10])
predition = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
# self.predition = self.add_layer(xs, 784, 10, activation_function=tf.nn.softmax)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(predition),reduction_indices=[1]))
# train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
# tpm2 = 0
for j in range(epochs):
j = j % 500
if j % 50 == 0:
print("epoch",(j % 50) + 1)
mini_data = training_inputs[j*batch_size:(j+1)*batch_size]
tmp0 = np.reshape(mini_data, [-1,784])
mini_results = training_results[j*batch_size:(j+1)*batch_size]
tmp1 = np.reshape(mini_results, [-1,10])
sess.run(train_step, feed_dict={xs: tmp0, ys:tmp1, keep_prob:0.5})
testimage0 = testimage.reshape([1,784])
test_y = sess.run(predition, feed_dict={xs:testimage0, keep_prob:1})
print(test_y)
result = sess.run(tf.argmax(test_y, 1))
return result
3.测试
测试结果:
4.总结
用CNN设计的神经网络,他的准确度大约为99%。CNN网络结构还有很多可以玩的,我会慢慢试。