天天看點

《TensorFlow實戰》學習1——softmax regression

作為書中第一個實戰例子,“Tensorflow實作Softmax Regression識别手寫數字”中使用的網絡很簡單,因為沒有隐含層,都算不上神經網絡。我也簡單的記錄一下這個執行個體中比較有價值的點吧。

一. 資料集

本書中很多執行個體都是跑mnist資料集,此資料集很小隻有55000張訓練圖檔,10000張測試圖檔,5000張驗證圖檔,圖檔的内容是0-9手寫數字。圖檔是28*28的灰階圖,空白像素點的值為0,有顔色的像素值全為1。資料集有兩種輸入格式:1*784或28*28.

二.訓練網絡

《TensorFlow實戰》學習1——softmax regression

訓練有3個過程:

1.      提取特征

《TensorFlow實戰》學習1——softmax regression

2.      Softmax

《TensorFlow實戰》學習1——softmax regression

3.      交叉熵和梯度下降優化

《TensorFlow實戰》學習1——softmax regression

   梯度下降調用tf内部的操作即可。

三.測試網絡

《TensorFlow實戰》學習1——softmax regression

測試網絡通過10000張驗證圖檔求accuracy。

四.程式分析

# coding:utf-8

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

import tensorflow as tf

#建立新的會話,圖的執行和儲存一般是在會話中完成,但這裡sess貌似沒有用到,這點不太明白。
sess = tf.InteractiveSession()
#placeholder:占位符,提供輸入資料的地方,不是tensor.一般隻有x,y輸入才需要用到。
#[None, 784]:占位符的shape,也是輸入tensor的shape,取決于輸入資料的shape。784表示列數,None幾乎等于輸入行數任意。
x = tf.placeholder(tf.float32, [None, 784])

#Variable:持久化儲存tensor。tensor本身一用完就會消失,像w,b這種一直疊代的參數需要持久化存在。
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

#tensorflow的nn中有大量神經網絡元件,要用某種操作,首先到nn、train這樣的模組種找。
#matmul矩陣乘法中的一個ops,最常用的ops有array、矩陣等檔案下的ops,可在../tenserflow/python/ops下xx_ops.py中找。
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32,[None, 10])

#計算交叉熵,也就是求loss
#reduce_mean:對每個batch資料結果求均值
#reduce_sum:求和
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

#GradientDescentOptimizer:随機梯度下降(SGD),0.5為其學習率,minimize的參數為最小化的目标tensor.
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

#初始化圖中變量。
tf.global_variables_initializer().run()

#開始按Batch訓練。
for i in range(1000):
    #讀入100張圖和對應的100個标簽。
    batch_xs, batch_ys = mnist.train.next_batch(100)
    #執行優化
    #{x:batch_xs, y_:batch_ys}将資料輸入到對應的placeholder中。
    train_step.run({x:batch_xs, y_:batch_ys})

#equal:判斷兩個參數是否相等,預測是否準确。
#argmax:求各預測數字中機率最大的一個。
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

#計算搜尋測試的平均準确率。
#cast:轉換類型,将第一個參數的類型轉化為第二參數指定的類型。
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print accuracy.eval({x:mnist.test.images, y_:mnist.test.labels})
           

繼續閱讀