學習筆記TF024:TensorFlow實現Softmax Regression(回歸)辨識手寫數字

PHP中文网
發布: 2017-07-10 18:13:29
原創
1339 人瀏覽過

TensorFlow實作Softmax Regression(回歸)辨識手寫數字。 MNIST(Mixed National Institute of Standards and Technology database),簡單機器視覺資料集,28X28像素手寫數字,只有灰階值信息,空白部分為0,筆跡根據顏色深淺取[0, 1], 784維,丟棄二維空間訊息,目標分0~9共10類。資料加載,data.read_data_sets, 55000個樣本,測試集10000樣本,驗證集5000樣本。樣本標註訊息,label,10維向量,10種類one-hot編碼。訓練集訓練模型,驗證集檢驗效果,測試集評測模型(準確率、召回率、F1-score)。

演算法設計,Softmax Regression訓練手寫數字辨識分類模型,估算類別機率,取機率最大數字作模型輸出結果。類別特徵相加,判定類別機率。模型學習訓練調整權值。 softmax,各類別特徵計算exp函數,標準化(所有類別輸出機率值為1)。 y = softmax(Wx+b)。

NumPy使用C、fortran,呼叫openblas、mkl矩陣運算函式庫。 TensorFlow密集複雜運算在Python外執行。定義計算圖,運算操作不需要每次把運算完的資料傳回Python,全部都在Python外面運作。

import tensor flow as tf,載入TensorFlow函式庫。 less = tf.InteractiveSession(),建立InteractiveSession,註冊為預設session。不同session的資料、運算,相互獨立。 x = tf.placeholder(tf.float32, [None,784]),建立Placeholder 接收輸入數據,第一參數資料類型,第二參數代表tensor shape 資料尺寸。 None不限條數輸入,每個輸入為784維向量。

tensor儲存數據,一旦使用掉就會消失。 Variable在模型訓練迭代中持久化,長期存在,每輪迭代更新。 Softmax Regression模型的Variable物件weights、biases 初始化為0。模型訓練自動學習合適值。複雜網絡,初始化方法重要。 w = tf.Variable(tf.zeros([784, 10])),784特徵維數,10類。 Label,one-hot編碼後10維向量。

Softmax Regression演算法,y = tf.nn.softmax(tf.matmul(x, W) + b)。 tf.nn包含大量神經網路組件。 tf.matmul,矩陣乘法函數。 TensorFlow將forward、backward內容自動實現,只要定義好loss,訓練自動求導梯度下降,完成Softmax Regression模型參數自動學習。

定義loss function描述問題模型分類精確度。 Loss越小,模型分類結果與真實值越小,越精確。模型初始參數全零,產生初始loss。訓練目標是縮小loss,找出全局最優或局部最優解。 cross-entropy,分類問題常用loss function。 y預測機率分佈,y'真實機率分佈(Label one-hot編碼),判斷模型對真實機率分佈預測準確度。 cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))。定義placeholder,輸入真實label。 tf.reduce_sum求和,tf.reduce_mean每個batch資料結果求均值。

定義最佳化演算法,隨機梯度下降SGD(Stochastic Gradient Descent)。根據計算圖自動求導,根據反向傳播(Back Propagation)演算法訓練,每輪迭代更新參數減少loss。提供封裝優化器,每輪迭代feed數據,TensorFlow在後台自動補充運算操作(Operation)實現反向傳播和梯度下降。 train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)。呼叫tf.train.GradientDescentOptimizer,設定學習速度0.5,設定優化目標cross-entropy,得到訓練操作train_step。

tf.global_variables_initializer().run()。 TensorFlow全域參數初始化器tf.golbal_variables_initializer。

batch_xs,batch_ys = mnist.train.next_batch(100)。訓練操作train_step。每次隨機從訓練集抽取100個樣本構成mini-batch,feed給 placeholder,呼叫train_step訓練樣本。使用小部分樣本訓練,隨機梯度下降,收斂速度更快。每次訓練全部樣本,計算量大,不容易跳出局部最優。

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmzx(y_,1)),驗證模型準確率。 tf.argmax從tensor找出最大值序號,tf.argmax(y,1)求預測數字機率最大,tf.argmax(y_,1)找樣本真實數字類別。 tf.equal判斷預測數字類別是否正確,返回計算分類運算是否正確。

accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)),統計全部樣本預測正確度。 tf.cast轉換correct_prediction輸出值類型。

print(accuracy.eval({x: mnist.test.images,y_: mnist.test.labels}))。測試資料特徵、Label輸入評測流程,計算模型測試集準確率。 Softmax Regression MNIST資料分類識別,測試集平均準確率約92%。

TensorFlow 實作簡單機器演算法步驟:
1、定義演算法公式,神經網路forward計算。
2、定義loss,選定優化器,指定優化器優化loss。
3、迭代訓練資料。
4、測試集、驗證集評測準確率。

定義公式只是Computation Graph,只有呼叫run方法,feed數據,計算才執行。

    <span style="color: #0000ff">from</span> tensorflow.examples.tutorials.mnist <span style="color: #0000ff">import</span><span style="color: #000000"> input_data
    mnist </span>= input_data.read_data_sets(<span style="color: #800000">"</span><span style="color: #800000">MNIST_data/</span><span style="color: #800000">"</span>, one_hot=<span style="color: #000000">True)
    </span><span style="color: #0000ff">print</span><span style="color: #000000">(mnist.train.images.shape, mnist.train.labels.shape)
    </span><span style="color: #0000ff">print</span><span style="color: #000000">(mnist.test.images.shape, mnist.test.labels.shape)
    </span><span style="color: #0000ff">print</span><span style="color: #000000">(mnist.validation.images.shape, mnist.validation.labels.shape)
    </span><span style="color: #0000ff">import</span><span style="color: #000000"> tensorflow as tf
    sess </span>=<span style="color: #000000"> tf.InteractiveSession()
    x </span>= tf.placeholder(tf.float32, [None, 784<span style="color: #000000">])
    W </span>= tf.Variable(tf.zeros([784, 10<span style="color: #000000">]))
    b </span>= tf.Variable(tf.zeros([10<span style="color: #000000">]))
    y </span>= tf.nn.softmax(tf.matmul(x, W) +<span style="color: #000000"> b)
    y_ </span>= tf.placeholder(tf.float32, [None, 10<span style="color: #000000">])
    cross_entropy </span>= tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1<span style="color: #000000">]))
    train_step </span>= tf.train.GradientDescentOptimizer(0.5<span style="color: #000000">).minimize(cross_entropy)
    tf.global_variables_initializer().run()
    </span><span style="color: #0000ff">for</span> i <span style="color: #0000ff">in</span> range(1000<span style="color: #000000">):
        batch_xs, batch_ys </span>= mnist.train.next_batch(100<span style="color: #000000">)
        train_step.run({x: batch_xs, y_: batch_ys})
    correct_prediction </span>= tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1<span style="color: #000000">))
    accuracy </span>=<span style="color: #000000"> tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    </span><span style="color: #0000ff">print</span>(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))
登入後複製

 


參考資料:
《TensorFlow實作》

歡迎付費諮詢(150元每小時),我的微信:qingxingfengzi

以上是學習筆記TF024:TensorFlow實現Softmax Regression(回歸)辨識手寫數字的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!