Tensorflow安裝和入門簡介

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é)堂

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 簡單線性回歸 import tensorflow as tf import numpy # 創(chuàng)造數(shù)據(jù) x_dat...
    CAICAI0閱讀 3,668評論 0 49
  • 一.目的 類似學(xué)習(xí)開發(fā)語言的第一個代碼,Hello World! 機器學(xué)習(xí)中,我們通過MNIST來學(xué)習(xí)手寫輸入法的...
    Coming0524閱讀 6,649評論 2 8
  • 抱怨是自身對事物的一種不滿情緒表達。人生不如意之事十有八九,試問世間誰人不抱怨,誰人不發(fā)發(fā)牢騷。文人以奮筆疾...
    性感有才的郭郭閱讀 183評論 0 0
  • 好久沒咆哮了,昨晚邪惡的怪獸又爬出來嚇人了。。。 昨晚十一點過了,小孩還很精神,而我因為從早忙到晚也沒午休此刻已經(jīng)...
    兮淇哈皮閱讀 205評論 0 3
  • 兩天后,明鏡帶著桂姨從蘇州回來了,阿香也從阿付嫂那里回來了。 明鏡一到家,就看到明臺躺在客廳里的沙發(fā)上看著雜志。明...
    空谷飄零閱讀 3,357評論 0 5

友情鏈接更多精彩內(nèi)容