import os
import numpy as np
from time import time
import matplotlib.pyplot as plt
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from tensorflow.examples.tutorials.mnist import input_data
#步骤1:读取数据
mnist=input_data.read_data_sets("MNIST_data/",one_hot=True)
#步骤2:搭建多隐藏层神经网络
def fcn_layer(output_dim,input_dim,inputs,activation=None):
W=tf.Variable(tf.random_normal([input_dim,output_dim]))
b=tf.Variable(tf.random_normal([1,output_dim]))
XWb=tf.matmul(inputs,W) b
if activation is None:
outputs=XWb
else:
outputs=activation(XWb)
return outputs
x = tf.placeholder("float",[None,784])
# 隐藏层h1
h1=fcn_layer(output_dim=256,input_dim=784,inputs=x ,activation=tf.nn.relu)
# 隐藏层h2
h2=fcn_layer(output_dim=256,input_dim=256,inputs=h1,activation=tf.nn.relu)
pred=fcn_layer(output_dim=10,input_dim=256,inputs=h2,activation=None)
y=tf.placeholder("float",[None,10])
loss_function=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))#交叉熵
train_epochs=30
batch_size=100
total_batch=int(mnist.train.num_examples/batch_size)
display_step=1
save_step=5
learning_rate=0.001#学习率
epoch_list=[];loss_list=[];accuracy_list=[]
#步骤3:训练模型
startTime=time()
optimizer=tf.train.AdamOptimizer(learning_rate).minimize(loss_function)
correct_prediction=tf.equal(tf.argmax(y, 1),tf.argmax(pred, 1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,"float"))
saver=tf.train.Saver()
sess=tf.Session()#生成会话
sess.run(tf.global_variables_initializer())
for epoch in range(train_epochs):
for batch in range(total_batch):
xs, ys=mnist.train.next_batch(batch_size)
sess.run(optimizer,feed_dict={x: xs,y: ys})
loss,acc=sess.run([loss_function,accuracy],feed_dict={x:mnist.validation.images,y:mnist.validation.labels})
epoch_list.append(epoch)
loss_list.append(loss)
accuracy_list.append(acc)
if (epoch 1)%display_step==0:
print("Train Epoch:",'d'%(epoch 1),"Loss=","{:.9f}".format(loss)," Accuracy=","{:.4f}".format(acc))
duration=time()-startTime
print("Train Finished takes:","{:.2f}".format(duration))
sess.close()
print("Starting another session for prediction")
saver=tf.train.Saver()
sess=tf.Session()
init=tf.global_variables_initializer()
sess.run(init)
#步骤4:测试模型
def plot_images_labels_prediction(images,labels,prediction,idx,num=10):
fig=plt.gcf()
fig.set_size_inches(10,12)
if num>25:num=25
for i in range(0,num):
ax=plt.subplot(5,5,1 i)
ax.imshow(np.reshape(images[idx],(28, 28)),cmap='binary')
title="label=" str(np.argmax(labels[idx]))
if len(prediction)>0:
title =",predict=" str(prediction[idx])
ax.set_title(title,fontsize=10)
ax.set_xticks([])
ax.set_yticks([])
idx =1
plt.show()
prediction_result=sess.run(tf.argmax(pred,1),feed_dict={x: mnist.test.images})
plot_images_labels_prediction(mnist.test.images,mnist.test.labels,prediction_result,10)
count=0
for i in range(500):
if prediction_result[i]!=np.argmax(mnist.test.labels[i]):
print("i=" str(i) "label=",np.argmax(mnist.test.labels[i]),"predict=",prediction_result[i])
count =1
print("count=", count)
Extracting MNIST_data/train-images-idx3-ubyte.gz Extracting MNIST_data/train-labels-idx1-ubyte.gz Extracting MNIST_data/t10k-images-idx3-ubyte.gz Extracting MNIST_data/t10k-labels-idx1-ubyte.gz Train Epoch: 01 Loss= 53.529132843 Accuracy= 0.8624 Train Epoch: 02 Loss= 33.633792877 Accuracy= 0.9002 Train Epoch: 03 Loss= 26.070878983 Accuracy= 0.9166 Train Epoch: 04 Loss= 21.878053665 Accuracy= 0.9256 Train Epoch: 05 Loss= 19.345390320 Accuracy= 0.9310 Train Epoch: 06 Loss= 18.134420395 Accuracy= 0.9348 Train Epoch: 07 Loss= 15.690796852 Accuracy= 0.9414 Train Epoch: 08 Loss= 15.634956360 Accuracy= 0.9372 Train Epoch: 09 Loss= 15.122309685 Accuracy= 0.9414 Train Epoch: 10 Loss= 14.535149574 Accuracy= 0.9426 Train Epoch: 11 Loss= 14.188427925 Accuracy= 0.9450 Train Epoch: 12 Loss= 14.709759712 Accuracy= 0.9426 Train Epoch: 13 Loss= 13.977644920 Accuracy= 0.9448 Train Epoch: 14 Loss= 13.594002724 Accuracy= 0.9466 Train Epoch: 15 Loss= 12.868132591 Accuracy= 0.9492 Train Epoch: 16 Loss= 12.838119507 Accuracy= 0.9506 Train Epoch: 17 Loss= 12.817976952 Accuracy= 0.9496 Train Epoch: 18 Loss= 12.890332222 Accuracy= 0.9506 Train Epoch: 19 Loss= 12.724534988 Accuracy= 0.9502 Train Epoch: 20 Loss= 13.171916008 Accuracy= 0.9494 Train Epoch: 21 Loss= 12.193360329 Accuracy= 0.9558 Train Epoch: 22 Loss= 11.771809578 Accuracy= 0.9516 Train Epoch: 23 Loss= 12.657453537 Accuracy= 0.9532 Train Epoch: 24 Loss= 12.012898445 Accuracy= 0.9552 Train Epoch: 25 Loss= 12.073326111 Accuracy= 0.9542 Train Epoch: 26 Loss= 12.455985069 Accuracy= 0.9556 Train Epoch: 27 Loss= 11.321227074 Accuracy= 0.9564 Train Epoch: 28 Loss= 12.093022346 Accuracy= 0.9568 Train Epoch: 29 Loss= 11.713661194 Accuracy= 0.9580 Train Epoch: 30 Loss= 11.451450348 Accuracy= 0.9588 Train Finished takes: 76.92 Starting another session for prediction
算法:手写体数字识别使用的框架是由多个隐藏层组成的神经网络,增加隐藏层的层数以提高分类准确率。