安裝 TensorFlow, 並以 40 行 sample code 進行 neural network training, 辨識手寫數字

以下列步驟安裝 TensorFlow, 並以 sample code 進行 neural network training (本文使用的環境: Windows 10):

1. 安裝 Python 3.6.3

2. 安裝 TensorFlow 及相關的 Python packages
pip install --upgrade tensorflow
安裝的時候, 我遇到 error message, 提到 python folder 下的 access right 不足, 透過 explorer 設定好 access right 之後 (原本是 read-only, 改為 read-write), 再安裝一次解決了此問題.
pip install matplotlib
Matplotlib is a Python 2D plotting library.
pip install pandas
pandas is a Python library providing data structures and data analysis tools.
3. 確認 TensorFlow 是否安裝成功
c:\›python

››› import tensorflow as tf
››› hello = tf.constant('Hello, TensorFlow!')
››› sess = tf.Session()
此時可能會出現如下的 warning message:
Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX
它是在提醒安裝的 TensorFlow library 並沒有針對所有的 CPU 延伸指令集做優化, 功能面不會有影響.
››› print(sess.run(hello))
如果出現:
b'Hello, TensorFlow!'
而沒有出現其他錯誤訊息, 表示 TensorFlow 安裝成功了.
4. 安裝 git

5. 取得並執行 tutorial sample code 進行 neural network training, 辨識手寫數字
c:\›git clone https://github.com/martin-gorner/tensorflow-mnist-tutorial

cd tensorflow-mnist-tutorial

c:\tensorflow-mnist-tutorial›python mnist_1.0_softmax.py
此 program 以 40 行 Python code, 1-layer neural network 進行手寫數字的辨識, 進行 2000 個 iteration 後, accuracy 可達 92%:



詳細說明: TensorFlow and deep learning, without a PhD: 6. Lab: let's jump into the code
6. 以 5-layer neural network 提高 accuracy 到 97%
c:\tensorflow-mnist-tutorial›python mnist_2.0_five_layers_sigmoid.py
詳細說明: TensorFlow and deep learning, without a PhD: 7. Lab: adding layers
7. 調整 activation function 及 optimization algorithm, 並進行 learning rate decay, 加快收斂速度及收斂穩定度
把 activation function 由 sigmoid 改為 leru, 並且把 optimization algorithm 由 gradient descent 改為 Adam, 並進行 learning rate decay:
c:\tensorflow-mnist-tutorial›python mnist_2.1_five_layers_relu_lrdecay.py
詳細說明: TensorFlow and deep learning, without a PhD: 8. Lab: special care for deep networks, 9. Lab: learning rate decay
8. 以 dropout 設法降低 overfitting
c:\tensorflow-mnist-tutorial›python mnist_2.2_five_layers_relu_lrdecay_dropout.py
詳細說明: TensorFlow and deep learning, without a PhD: 10. Lab: dropout, overfitting
9. 以 convolutional layers 提升 accuracy 到 98.9%
c:\tensorflow-mnist-tutorial›python mnist_3.0_convolutional.py
詳細說明: TensorFlow and deep learning, without a PhD: 12. Lab: a convolutional network
10. 增加 convolutional layers output channel 數, 並在 fully connected layer 做 dropout, 提升 accuracy 到 99.3%
c:\tensorflow-mnist-tutorial›python mnist_3.1_convolutional_bigger_dropout.py
此 program 以 70 行 Python code, 達到了 99.3% accuracy, 離 MINST 網站公布的世界紀錄 99.7% 只差 0.4%.

詳細說明: TensorFlow and deep learning, without a PhD: 13. Lab: the 99% challenge

延伸閱讀


TensorFlow and deep learning, without a PhD

Getting Started with TensorFlow

[學習筆記] 深度學習概述: 核心概念 (Deep Learning: Core Concepts)