1. 程式人生 > >深度學習平臺TensorFlow環境搭建【GPU版】

深度學習平臺TensorFlow環境搭建【GPU版】

系統 Ubuntu14.04.4 LTS x64

GPU NVIDIA GeForce GTX 750Ti

TensorFlow GPU版本首先需要安裝NVIDIA顯示卡驅動,並且需要CUDA以及cuDNN支援,這裡採用的顯示卡驅動版本為375.39,CUDA版本為8.0,cuDNN版本為5.1。具體安裝過程請見深度學習平臺Caffe環境搭建【GPU版】

安裝完成之後,開始搭建TensorFlow平臺。

首先安裝libcupti-dev library,官方給出的解釋為NVIDIA CUDA Profile Tools Interface。其實就是NVIDIA的一個庫。執行命令:

sudo apt-get install libcupti-dev

官方給出了4種安裝方法,因為電腦裡面已經裝有Caffe,為了避免產生衝突,這裡選擇使用virtualenv安裝,virtualenv是一個python管理工具,它可以建立一個獨立的python環境,程式執行在這裡面可以不受其他python library版本的影響。使用python 2.7,執行命令:
sudo apt-get install python-pip python-dev python-virtualenv

建立一個名為tensorflow的virtualenv環境
virtualenv --system-site-packages ~/tensorflow

啟用virtualenv環境
source ~/tensorflow/bin/activate

此時在命令列的最前面會出現(tensorflow),表示環境啟用成功。

接下來安裝TensorFlow GPU版本。

pip install --upgrade tensorflow-gpu

可能出現問題: Downloading/unpacking tensorflow-gpu Could not find any downloads that satisfy the requirement tensorflow-gpu Cleaning up... No distributions at all found for tensorflow-gpu Storing debug log for failure in /tmp/tmpmUPZAM

這是因為pip的版本過低,執行命令:

pip install -U pip

升級pip之後,再次執行TensorFlow GPU版本安裝命令。

安裝完成之後,執行deactivate關閉環境。

為了以後啟用tensorflow環境更加簡單,執行以下命令將tensorflow啟用命令寫入bash

sudo printf '\nalias tensorflow="source ~/tensorflow/bin/activate"' >>~/.bashrc

重新整理bash之後,鍵入tensorflow即可啟用tensorflow環境。

啟用tensorflow環境後開始測試。

進入python, 執行以下命令:

>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
>>> print(sess.run(hello))
Hello, TensorFlow!
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42

出現上述結果說明安裝成功。

執行測試demo

# encoding: utf-8
import tensorflow as tf
import numpy
import matplotlib.pyplot as plt
rng = numpy.random

learning_rate = 0.01
training_epochs = 1000
display_step = 50

# 資料集x
train_X = numpy.asarray([3.3,4.4,5.5,7.997,5.654,.71,6.93,4.168,9.779,6.182,7.59,2.167,
                         7.042,10.791,5.313,9.27,3.1])
# 資料集y
train_Y = numpy.asarray([1.7,2.76,3.366,2.596,2.53,1.221,1.694,1.573,3.465,1.65,2.09,
                         2.827,3.19,2.904,2.42,2.94,1.3])

n_samples = train_X.shape[0]
X = tf.placeholder("float")
Y = tf.placeholder("float")

W = tf.Variable(rng.randn(), name="weight")
b = tf.Variable(rng.randn(), name="bias")

pred = tf.add(tf.multiply(X, W), b)

cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)

optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

init = tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(init)

    # 訓練資料
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})

    print "優化完成!"
    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
    print "Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n'

    #視覺化顯示
    plt.plot(train_X, train_Y, 'ro', label='Original data')
    plt.plot(train_X, sess.run(W) * train_X + sess.run(b), label='Fitted line')
    plt.legend()
    plt.show()

出現結果

至此tensorflow安裝結束,可以開始構建網路了。