基於TensorFlow簡單實現手寫體數字識別


本案例采用的是MNIST數據集[1],是一個入門級的計算機視覺數據集。

MNIST數據集已經被嵌入到TensorFlow中,可以直接下載和安裝。

1 from tensorflow.examples.tutorials.mnist import input_data 2 mnist=input_data.read_data_sets("MNIST_data/",one_hot=True)

此時,文件名為MNIST_data的數據集就下載下來了,其中one_hot=True為將樣本標簽轉化為one_hot編碼。

接下來將MNIST的信息打印出來。

3 print('輸入數據:',mnist.train.images) 4 print('輸入數據的尺寸:',mnist.train.images.shape) 5 import pylab 6 im=mnist.train.images[0]  #第一張圖片
7 im=im.reshape(-1,28) 8 pylab.imshow(im) 9 pylab.show()

輸出為:

輸入數據: [[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]
輸入數據的尺寸: (55000, 784)

MNIST的圖片尺寸為28*28,數據集的存儲把所有的圖片存在一個矩陣中,將一張圖片鋪開存為一個行向量,從輸出信息我們可以知道訓練集包含55000張圖片。

MNIST中還包括測試集和驗證集,大小分別為10000和5000。

10 print("測試集大小:",mnist.test.images.shape) 11 print("驗證集大小:",mnist.validation.images.shape)

測試集用於訓練過程中評估模型的准確度,驗證集用於最終評估模型的准確度。

接下來就可以進行識別了,采用最簡單的單層神經網絡的方法,大致順序就是定義輸入-學習參數-學習參數和輸入計算-計算損失-定義優化函數-迭代優化

 

 1 import tensorflow as tf  2 tf.reset_default_graph()   #清除默認圖形堆棧並重置全局默認圖形
 3 #定義占位符
 4 x=tf.placeholder(tf.float32,[None,784])   #圖像28*28=784
 5 y=tf.placeholder(tf.float32,[None,10])    #標簽10類
 6 #定義學習參數
 7 w=tf.Variable(tf.random_normal([784,10])) #權值,初始化為正太隨機值
 8 b=tf.Variable(tf.zeros([10]))             #偏置,初始化為0
 9 #定義輸出
10 pred=tf.nn.softmax(tf.matmul(x,w)+b)      #相當於單層神經網絡,激活函數為softmax
11 #損失函數
12 cost=tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),reduction_indices=1))  #reduction_indices指定計算維度
13 #優化函數
14 optimizer=tf.train.GradientDescentOptimizer(0.01).minimize(cost) 15 #定義訓練參數
16 training_epochs=25   #訓練次數
17 batch_size=100       #每次訓練圖像數量
18 display_step=1       #打印訓練信息周期
19 #保存模型
20 saver=tf.train.Saver() 21 model_path="log/521model.ckpt"
22 #開始訓練
23 with tf.Session() as sess : 24     sess.run(tf.global_variables_initializer())   #初始化所有參數
25     for epoch in range(training_epochs) : 26         avg_cost=0.                               #平均損失
27         total_batch=int(mnist.train.num_examples/batch_size)   #計算總的訓練批次
28         for i in range(total_batch) : 29             batch_xs, batch_ys=mnist.train.next_batch(batch_size)  #抽取數據
30             _, c=sess.run([optimizer,cost], feed_dict={x:batch_xs, y:batch_ys})  #運行
31             avg_cost+=c/total_batch 32         if (epoch+1) % display_step == 0 : 33             print("Epoch:",'%04d'%(epoch+1),"cost=","{:.9f}".format(avg_cost)) 34     print("Finished!") 35     #測試集測試准確度
36     correct_prediction=tf.equal(tf.argmax(pred,1),tf.argmax(y,1)) 37     accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) 38     print("Accuracy:",accuracy.eval({x:mnist.test.images,y:mnist.test.labels})) 39     #保存模型
40     save_path=saver.save(sess,model_path) 41     print("Model saved in file: %s" % save_path)

 

運行得到的結果:

Epoch: 0001 cost= 7.973125283
...
Epoch: 0025 cost= 0.898346810
Finished!

可以看出,損失降低了很多,得到的結果還不錯,這只是簡單的模型,使用復雜的模型可以得到更好的結果,將在以后給出。

讀取保存的模型,測試模型。

 

 1 print("Starting 2nd session...")  2 with tf.Session() as sess :  3  sess.run(tf.global_variables_initializer())  4     #恢復模型及參數
 5  saver.restore(sess,model_path)  6     
 7     #測試
 8     correct_prediction=tf.equal(tf.argmax(pred,1),tf.arg_max(y,1))  9     #計算准確度
10     accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) 11     print("Accuracy: ",accuracy.eval({x:mnist.test.images,y:mnist.test.labels})) 12     #計算輸出
13     output=tf.argmax(pred,1) 14     batch_xs,batch_yx=mnist.train.next_batch(2) 15     outputval,predv=sess.run([output,pred],feed_dict={x:batch_xs}) 16     print(outputval,predv,batch_ys) 17     #顯示圖片1
18     im=batch_xs[0] 19     im=im.reshape(-1,28) 20  pylab.imshow(im) 21  pylab.show() 22     #顯示圖片2
23     im=batch_xs[1] 24     im=im.reshape(-1,28) 25  pylab.imshow(im) 26     pylab.show()

 

[1] http://yann.lecun.com/exdb/mnist/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM