基于卷积神经网络的Tensorflow实现MNIST识别
import keras
import tensorflow as tf
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation
from keras.optimizers import RMSprop
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets('MNIST_data',one_hot=True)
print("训练集图像大小:{}".format(mnist.train.images.shape))
print("训练集标签大小:{}".format(mnist.train.labels.shape))
print("验证集图像大小:{}".format(mnist.validation.images.shape))
print("验证集标签大小:{}".format(mnist.validation.labels.shape))
print("测试集图像大小:{}".format(mnist.test.images.shape))
print("测试集标签大小:{}".format(mnist.test.labels.shape))
x_train,y_train=mnist.train.images,mnist.train.labels
x_valid,y_valid=mnist.validation.images,mnist.validation.labels
x_test,y_test=mnist.test.images,mnist.test.labels
img_size=28*28
num_classes=10
learning_rate=1e-4
epochs=10
batch_size=50
x=tf.placeholder(tf.float32,shape=[None,img_size])
x_shaped=tf.reshape(x,[-1,28,28,1])
y=tf.placeholder(tf.float32,shape=[None,num_classes])
def create_conv2d(input_data,num_input_channels,num_filters,filter_shape,pool_shape,name):
conv_filter_shape=[filter_shape[0],filter_shape[1],num_input_channels,num_filters]
weights=tf.Variable(tf.truncated_normal(conv_filter_shape,stddev=0.03),name=name+'_W')
bias=tf.Variable(tf.truncated_normal([num_filters]),name=name+'_b')
out_layer=tf.nn.conv2d(input_data,weights,(1,1,1,1),padding='SAME')
out_layer+=bias
out_layer=tf.nn.relu(out_layer)
out_layer=tf.nn.max_pool(out_layer,ksize=(1,pool_shape[0],pool_shape[1],1),strides=(1,2,2,1),padding='SAME')
return out_layer
layer1=create_conv2d(x_shaped,1,32,(5,5),(2,2),name='layer1')
layer2=create_conv2d(layer1,32,64,(5,5),(2,2),name='layer2')
flattened=tf.reshape(layer2,(-1,7*7*64))
wd1=tf.Variable(tf.truncated_normal((7*7*64,1000),stddev=0.03),name='wd1')
bd1=tf.Variable(tf.truncated_normal([1000],stddev=0.01),name='bd1')
dense_layer1=tf.add(tf.matmul(flattened,wd1),bd1)
dense_layer1=tf.nn.relu(dense_layer1)
wd2=tf.Variable(tf.truncated_normal((1000,num_classes),stddev=0.03),
name='wd2')
bd2=tf.Variable(tf.truncated_normal([num_classes],stddev=0.01),name='bd2')
dense_layer2=tf.add(tf.matmul(dense_layer1,wd2),bd2)
y_=tf.nn.softmax(dense_layer2)
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_,labels=y))
optimizer=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
import math
iteration=0
saver=tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
batch_count=int(math.ceil(mnist.train.labels.shape[0]/128.0))
for e in range(epochs):
for batch_i in range(batch_count):
batch_start=batch_i*batch_size
batch_x=mnist.train.images[batch_start:batch_start+batch_size]
batch_y=mnist.train.labels[batch_start:batch_start+batch_size]
loss,_=sess.run([cost,optimizer],feed_dict={x:batch_x,y:batch_y})
if batch_i%20==0:
print("Epoch:{}/{}".format(e+1,epochs),
"Iteartion:{}".format(iteration),
"Training loss:{}".format(loss))
iteration +=1
if iteration % batch_size==0:
valid_acc=sess.run(accuracy,feed_dict={x:x_valid,y:y_valid})
print("Epoch:{}/{}".format(e, epochs),
"Iteartion:{}".format(iteration),
"Training loss:{}".format(valid_acc))
saver.save(sess,'checkpoints/mnist_cnn_tf.ckpt')
saver=tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess,tf.train.latest_checkpoint('checkpoints/'))
test_acc=sess.run(accuracy,feed_dict={x:x_test,y:y_test})
print("test accuracy:{}".format(test_acc))
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9dce34b80e0d258c643abac4a51033ea.png)