人工智能 - TensorFlow 框架初探

歡迎Follow我的GitHub

本文地址:http://blog.csdn.net/caroline_wendy/article/details/77639394

框架:Python + TensorFlow
知識:工程配置 + HelloWorld + MNIST

本文源碼的GitHub地址

TensorFlow


準備

Fork TensorFlow的工程,並下載,轉換遠端Git地址

git remote set-url origin https://github.com/SpikeKing/tensorflow.git

創建Python工程MachineLearningTutorial,使用 virtualenv 創建虛擬環境

pip install virtualenv
virtualenv MLT_ENV

激活或關閉的命令

source MLT_ENV/bin/activate
deactivate

安裝TensorFlow的庫,使用阿里雲的源

pip install TensorFlow -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

導出庫的版本,及全部安裝

pip freeze>requirements.txt
pip install -r requirements.txt

Hello World

切換Python的解釋器(Interpreter)

Interpreter

HelloWorld

import tensorflow as tf

hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print sess.run(hello)

a = tf.constant(10)
b = tf.constant(32)
print sess.run(a + b)

輸出

Hello, TensorFlow!
42

避免cpu_feature_guard警告

import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'

MNIST

路徑:tensorflow/examples/tutorials/mnist/mnist_softmax.py

加載MNIST數據,默認存放於tmp文件夾,標籤使用one-hot模式

mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)

one-hot的值,如下:[ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.],表示標籤“7”,將類別標籤轉換爲向量,避免標籤的數值關係。

創建變量,placeholder表示輸入數據、Variable表示可變參數,最終公式是y = x * W + b,y_表示真實標籤(Ground Truth)

x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, W) + b 
y_ = tf.placeholder(tf.float32, [None, 10])

計算交叉熵,即損失函數,labels表示真實標籤,logits表示預估標籤

cross_entropy = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))

等價於

sc2 = tf.reduce_sum(-1 * (labels_ * tf.log(tf.nn.softmax(labels))), reduction_indices=[1])

梯度下降的方式,優化交叉熵

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

創建交互會話,初始化全部可變參數

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

每次取出100張圖片,進行訓練,在Session中執行train_step公式,feed_dict輸入參數(placeholder),按批次(batch)訓練

for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

輸出一維列表最大值的索引(argmax),進行比較(equal),再講Bool值轉爲Float(cast),全部求平均(reduce_mean),就是準確率的計算公式

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

在Session中執行accuracy公式,feed_dict輸入參數(placeholder),數據源是測試集

print(sess.run(accuracy, feed_dict={x: mnist.test.images,
                                    y_: mnist.test.labels}))

腳本參數是data_dir,在main函數中,則是FLAGS.data_dir,默認存放於臨時目錄(tmp),在 tf.app.run()中執行,FLAGS表示指定的參數,如--learning_rate 20,unparsed表示未指定的參數,隨意輸入的參數。

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data',
                        help='Directory for storing input data')
    FLAGS, unparsed = parser.parse_known_args()
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

Mac系統中,tmp存放隱藏文件,在終端的home目錄中,輸入open /tmp,即可打開

tmp

完整的MNIST代碼,及註釋

FLAGS = None  # 全局變量


def main(_):
    mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)  # 加載數據源

    x = tf.placeholder(tf.float32, [None, 784])  # 數據輸入
    W = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    y = tf.matmul(x, W) + b

    y_ = tf.placeholder(tf.float32, [None, 10])  # 標籤輸入

    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))  # 損失函數
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)  # 優化器

    sess = tf.InteractiveSession()  # 交互會話
    tf.global_variables_initializer().run()  # 初始化變量

    # 訓練模型
    for _ in range(1000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

    # 驗證模型
    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    print(sess.run(accuracy, feed_dict={x: mnist.test.images,
                                        y_: mnist.test.labels}))


if __name__ == '__main__':
    parser = argparse.ArgumentParser()  # 設置參數data_dir
    parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data',
                        help='Directory for storing input data')
    FLAGS, unparsed = parser.parse_known_args()
    tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

工程配置 + HelloWorld + MNIST

OK! That’s all! Enjoy it!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章