2016-10-08?陳偉才?人工智能學(xué)堂
一、Tensorflow簡介
Tensorflow ( https://www.tensorflow.org/ ) 是google大腦團隊打造的一款開源軟件庫,用于人工智能、機器學(xué)習(xí)以及深度學(xué)習(xí)等領(lǐng)域。Tensorflow主要包括Tersor和Flow兩個主要的概念,tensor表示多維數(shù)組multi-dimensional arrays,flow表示數(shù)據(jù)流圖data flow graph。Tensorflow (https://github.com/tensorflow/tensorflow )從star數(shù)、fork數(shù)來看,可以說是目前最火熱的機器學(xué)習(xí)開源框架,值得我們深入學(xué)習(xí)。
二、Ubuntu/Linux和MAC OS X簡單安裝過程
Tensorflow可以通過二進制或者源碼來進行安裝。本文通過tensorflow的二進制包進行Ubuntu和MAC平臺下安裝演示。
1. Ubuntu安裝
以Ubuntu 14.04.4 LTS (GNU/Linux 3.13.0-86-generic x86_64)為例,安裝過程如下藍色字體:
#apt-get update//獲取ubuntu最新的軟件包
#sudo apt-get install python-pip python-dev python-virtualenv
#virtualenv --system-site-packages ~/tensorflow
#source ~/tensorflow/bin/activate
#pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0-cp27-none-linux_x86_64.whl//我們安裝CPU版本為例,如果需要支持GPU Card,則安裝對應(yīng)的tensorflow GPU版本
#deactivate
至此tensorflow已經(jīng)安裝完畢,我們檢測一下是否安裝成功了,
#~/tensorflow/bin/python2.7
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>import tensorflow as tf
>>>hello = tf.constant('Hello, TensorFlow!')
>>>sess = tf.Session()
>>>print sess.run(hello)
Hello, TensorFlow!
>>>
2. MAC OS X安裝
MAC OS X需要OSX 10.11 EL Capitan版本才能支持Tensorflow,所以安裝Tensorflow之前,請自行升級MAX OS系統(tǒng)。升級完MAX OS X系統(tǒng)成功后,MAX默認是開啟rootless,所以還需要關(guān)系rootless。
MAC只支持CPU版本,目前還不支持GPU Card,MAC安裝過程同樣也很簡單,如下:
#sudo easy_install pip
#sudo pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl
三、簡單入門示例MNIST
TensorFlow是一個非常強大的用來做大規(guī)模數(shù)值計算的庫。其所擅長的任務(wù)之一就是實現(xiàn)以及訓(xùn)練深度神經(jīng)網(wǎng)絡(luò)。Tensorflow官網(wǎng)入門示例MNIST,https://www.tensorflow.org/versions/r0.11/tutorials/mnist/beginners/index.html#softmax-regressions,是采用softmax regression進行機器學(xué)習(xí)的入門例子。
MNIST是一個入門級的計算機視覺數(shù)據(jù)集,它包含各種手寫數(shù)字圖:
Softmax回歸就是推廣版本的邏輯回歸。 只不過邏輯回歸是個2分類問題,而Softmax是多分類問題,僅此而已。有關(guān)softmax regression算法,請參考http://deeplearning.stanford.edu/wiki/index.php/Softmax_Regression,本文不重點描述和推導(dǎo)。
程序代碼github地址是https://github.com/chenweicai/tensorflow-study/blob/master/tf_softmax_mnist.py,內(nèi)容如下:
# Softmax Regression using tensorflow.
import tensorflow as tf
# Download the mnist data.
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/MNIST_data", one_hot=True)
# Input placeholder, 2-D tensor of floating-point nunbers.
# here None means that a dimension can be of any length.
x = tf.placeholder(tf.float32, [None, 784])
# Initialize both W and b as tensors full of zeros.
# Since we are going to learn W and b, it doesn't
# matter very much what they initial are.
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
# Maichine Learning Model.
y = tf.nn.softmax(tf.matmul(x, W) + b)
# New placeholder to input the correct answers.
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), \
reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
# Training 1000 times, 100 for each loop.
for i 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))
# Testing accuracy using test images.
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
執(zhí)行,輸出如下:
# ~/tensorflow/bin/python2.7 tf_softmax_mnist.py
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting /tmp/MNIST_data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting /tmp/MNIST_data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting /tmp/MNIST_data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting /tmp/MNIST_data/t10k-labels-idx1-ubyte.gz
0.9168
上述紅色0.9168,即為softmax學(xué)習(xí)mnist的準確率。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?長按二維碼關(guān)注公眾號人工智能學(xué)堂