本文對TensorFlow官方示例-手寫數字識别教程的讀書筆記。
教程分為3個部分:
- 資料準備
- 模型建立
- 模型評價
1.資料準備
教程使用的是公開的MNIST手寫數字圖檔記,并且對資料的通路進行了封裝,是以在實際編碼過程中很友善的就可以将資料準備好。
這裡需要注意的是,資料集中儲存的并不是真正的一張張真實的圖檔,而是用矩陣對資料進行了展示。比如,訓練記中的資料表示為:

它是一個55000×784維的張量,表示有55000張圖檔,每張圖檔用784維的一維向量表示。
2.模型建立
本教程使用的模型可以直覺的表示為:
x 是向量形式的圖檔,w表示每一個像素的權重, b 表示偏置值。yi表示為第i個數字的置信度。
用公式表示為:
y=softmax(Wx+b)
損失函數定義為:
Hy′(y)=−∑iy′ilog(yi)
其中 y′ 是真實值, y 是預測值。訓練的目标就是使得Hy′(y)最小。
下面結合代碼進行解釋:
from tensorflow.examples.tutorials.mnist import input_data
print('資料加載...')
mnist=input_data.read_data_sets('./data/mnist',one_hot=True)
# 可以看到傳回的是Datasets類型,包含了訓練集、驗證集、測試集
#return base.Datasets(train=train, validation=validation, test=test)
print('圖檔表示示例:')
print(mnist[].images[])
print('标簽表示示例:')
print(mnist[].labels[])
img_count_train=len(mnist[].images)
img_array_train=len(mnist[].images[])
img_label_train=len(mnist[].labels[])
print('訓練集有%s張圖檔,每張圖檔表示為%s維數組,标簽以one-hot方式編碼為%s維數組。'%(img_count_train,img_array_train,img_label_train))
img_count_validation=len(mnist[].images)
img_array_validation=len(mnist[].images[])
img_label_validation=len(mnist[].labels[])
print('驗證集有%s張圖檔,每張圖檔表示為%s維數組,标簽以one-hot方式編碼為%s維數組。'%(img_count_validation,img_array_validation,img_label_validation))
img_count_test=len(mnist[].images)
img_array_test=len(mnist[].images[])
img_label_test=len(mnist[].labels[])
print('測試集有%s張圖檔,每張圖檔表示為%s維數組,标簽以one-hot方式編碼為%s維數組。'%(img_count_test,img_array_test,img_label_test))
print('資料加載done...')
print('-----------------------------------------------------------------------')
print('開始建構softmax回歸模型...')
# softmax函數:y=softmax(wx+b) loss=-y*logy' 其中y'表示測試值 y表示真實值 訓練方法随機梯度下降,學習速率:0.05,mini_batch:50 訓練10000次
train_nums=
import tensorflow as tf
W=tf.Variable(tf.zeros(shape=[,]))#x方向上是784維的圖檔,y方向上是0-9的置信度,是以是10維
b=tf.Variable(tf.zeros(shape=[]))#偏置值是一個x方向上1維,y方向上10維
x=tf.placeholder(tf.float32,[None,])#這裡寫成這樣是為了友善做矩陣的乘法
#激勵函數
y_test=tf.nn.softmax(tf.matmul(x,W)+b)
y_real=tf.placeholder(tf.float32,shape=[None,])
#損失函數 loss=-sum(y_*logy)
loss=tf.reduce_mean(-tf.reduce_sum(y_real*tf.log(y_test),reduction_indices=[]))
train_setp=tf.train.GradientDescentOptimizer().minimize(loss)
print('完成對softmax模型的建構.....')
print('開始訓練...')
sess=tf.InteractiveSession()#構造session
tf.global_variables_initializer().run()#全局變量的初始化
for _i in range(train_nums):
train_data,train_result=mnist.train.next_batch()
sess.run(train_setp,feed_dict={x: train_data , y_real: train_result})
print('訓練完畢...')
print('-----------------------------------------------------------------------')
print('下面計算模型的準确率:')
corrent_prediction=tf.equal(tf.arg_max(y_test, ), tf.arg_max(y_real, ))#預測的結果對比
print('預測值和實際值進行比對結果:', sess.run(corrent_prediction,feed_dict={x:mnist.test.images,y_real:mnist.test.labels}))
accuracy = tf.reduce_mean(tf.cast(corrent_prediction, tf.float32))
print('模型的準确率為:',sess.run(accuracy,feed_dict={x:mnist.test.images,y_real:mnist.test.labels}))
計算結果
計算完畢之後可以看到如下輸出:
資料加載...
Extracting ./data/mnist/train-images-idx3-ubyte.gz
Extracting ./data/mnist/train-labels-idx1-ubyte.gz
Extracting ./data/mnist/t10k-images-idx3-ubyte.gz
Extracting ./data/mnist/t10k-labels-idx1-ubyte.gz
圖檔表示示例:
[
]
标簽表示示例:
[ ]
訓練集有張圖檔,每張圖檔表示為維數組,标簽以one-hot方式編碼為維數組。
驗證集有張圖檔,每張圖檔表示為維數組,标簽以one-hot方式編碼為維數組。
測試集有張圖檔,每張圖檔表示為維數組,标簽以one-hot方式編碼為維數組。
資料加載done...
-----------------------------------------------------------------------
開始建構softmax回歸模型...
完成對softmax模型的建構.....
開始訓練...
-- ::: W tensorflow/core/platform/cpu_feature_guard.cc:] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-19 00:47:26.928953: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
-- ::: W tensorflow/core/platform/cpu_feature_guard.cc:] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-08-19 00:47:26.928959: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
-- ::: W tensorflow/core/platform/cpu_feature_guard.cc:] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
訓練完畢...
-----------------------------------------------------------------------
下面計算模型的準确率:
預測值和實際值進行比對結果: [ True True True ..., True True True]
模型的準确率為: 0.9214