1. 程式人生 > >Windows10 GPU版Tensorflow配置教程+Anaconda3+Jupyter Notebook

Windows10 GPU版Tensorflow配置教程+Anaconda3+Jupyter Notebook

之前配Caffe費了不少周折,詳情參閱 深度學習之caffe入門——caffe環境的配置(CPU ONLY)
如今轉戰Tensorflow,又免不了配環境之苦,摸索半天。終得其法。記錄下來,以備後用。

一、在使用pip安裝package的時候,經常崩掉,換用清華的源就好很多,或者用豆瓣的源也是可以的。
具體怎麼改清華源,請看這篇部落格。Ubuntu使用清華源

或者

pip install 你要裝的包 -i https://pypi.tuna.tsinghua.edu.cn/simple

二、正式開始

先安利兩個網站
Tensorflow官方安裝教程
Tensorflow中文社群


第一個可能要fan牆。
首先下載CUDA和cuDNN
CUDA的所有歷史版本
我用的是CUDA8.0,用起來沒問題,最新版9.1沒試過。
cuDNN5.1對應CUDA8.0
這裡寫圖片描述
先安裝第一個EXE檔案,按照安裝程式走下來就可以。第二個壓縮包,將解壓出來的三個資料夾覆蓋到第一個的安裝目錄下。
這裡寫圖片描述

然後下載Anaconda3

Anaconda的威力不用再多說了,集成了很多package,也有多版本的python管理,用起來很是方便。
首先上官網Anaconda官網
這裡寫圖片描述

因為Windows環境下的Tensorflow支援Python3.5或是Python3.6,所以要下載紅框裡的版本。如果想安裝與Python3.5

相匹配的Anaconda3,有兩個辦法:

  1. 這個網站下載Anaconda3-4.2.0-Windows-x86_64.exe然後安裝即可。
  2. 下載最新版的Anaconda3,然後用python版本管理功能新建一個Python3.5環境即可。下面的步驟會詳細講這個如何操作。

強烈建議大家下載Anaconda3 4.2.0版本,最新版我又試了一下,自帶的python總是3.6版本,裝了幾次Tensorflow都不成功,最後卸了最新版Anaconda,裝回4.2.0,回到了熟悉的python3.5.2就沒有問題了

這個網站下載Anaconda3-4.2.0-Windows-x86_64.exe然後安裝

Anaconda3的安裝截圖
Anaconda3的安裝截圖

按照預設安裝即可。

下面我們利用conda命令建立一個Pyhton3.5的環境,當然,如果有其他需要,也可以建立一個Pyhton2.7 Python3.6等等不同的版本環境,不同的環境之間互不干擾。

  1. 開啟命令列cmd,建議用管理員模式。
  2. 輸入conda create --name python35 python=3.5輸入y,稍等片刻即可建立一個新環境。如果在建立的這個環境裡不成功的話,嘗試建立conda create --name Anaconda3,再走一遍下面的操作。

還是建議大家使用conda create --name Anaconda3,並且檢查一下python版本是不是3.5.2,雖說win版tf支援3.6但是畢竟3.5.2還是最穩定的。

這裡寫圖片描述
這裡寫圖片描述
3. 用activate python35或者activate Anaconda3啟用剛剛配置好的環境。需要退出該環境時,用deactivate python35deactivate Anaconda3
這裡寫圖片描述
C碟符前面的(Anaconda3)說明進入了該環境。
4. 執行python確認3.5版本無誤,ctrl+Z退出python
5. 執行jupyter notebook,瀏覽器自動彈出Jupyter Notebook介面,無誤。
6. 回到conda環境,輸入
pip install tensorflow-gpu==1.2.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
安裝1.2.0版本的tensorflow,主要是如果不加==1.2.0預設安裝的是最新的tensorflow,1.6.0版本,和CUDA8.0不匹配,導致jupyter notebook下無法成功import tensorflow,爆出這個錯誤

1.OSError: [WinError 126] 找不到指定的模組。
2.ImportError: Could not find 'cudart64_90.dll'. TensorFlowrequires that this DLL be installed in a directory that is     named in your %PATH%environment variable. Download and install CUDA 9.0 from this URL:  https://developer.nvidia.com/cuda-toolkit

如果安裝不出問題的話,到此安裝過程就順利結束了。

下面測試一下。
在conda環境下進入python,

import tensorflow as tf
#檢視版本號
tf.__version__
#檢視安裝路徑
tf.__path__

輸入官方教程程式碼

#Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
#Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
#Runs the op.
print (sess.run(c))

輸出
這裡寫圖片描述
這裡寫圖片描述
可見tensorflow在計算時成功呼叫了GPU,並且輸出正確。

jupyter notebook下測試。
我用的是logistic 迴歸的一段訓練程式碼

import tensorflow as tf
x_data = [[1, 2], [2, 3], [3, 1], [4, 3], [5, 3], [6, 2]] 
y_data = [[0], [0], [0], [1], [1], [1]]
# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, 2])
Y = tf.placeholder(tf.float32, shape=[None, 1])

W = tf.Variable(tf.random_normal([2, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
# cost/loss function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

# Accuracy computation
# True if hypothesis>0.5 else False
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

# Launch graph
with tf.Session() as sess:
   # Initialize TensorFlow variables
   sess.run(tf.global_variables_initializer())

   for step in range(10001):
       cost_val, _ = sess.run([cost, train], feed_dict={X: x_data, Y: y_data})
       if step % 200 == 0:
           print(step, cost_val)

   # Accuracy report
   h, c, a = sess.run([hypothesis, predicted, accuracy],
                      feed_dict={X: x_data, Y: y_data})
   print("\nHypothesis: ", h, "\nCorrect (Y): ", c, "\nAccuracy: ", a)

輸出日誌

0 1.4967082
200 0.7839919
400 0.53214276
600 0.43306607
800 0.3880737
1000 0.36270094
1200 0.34548092
1400 0.33213395
1600 0.3208673
1800 0.3108564
2000 0.3016926
2200 0.2931581
2400 0.28512952
2600 0.27753118
2800 0.27031356
3000 0.26344112
3200 0.25688663
3400 0.25062782
3600 0.24464555
3800 0.23892312
4000 0.23344503
4200 0.22819762
4400 0.22316761
4600 0.2183432
4800 0.21371302
5000 0.20926651
5200 0.20499402
5400 0.20088613
5600 0.19693428
5800 0.19313025
6000 0.18946663
6200 0.1859361
6400 0.182532
6600 0.17924798
6800 0.1760783
7000 0.17301725
7200 0.17005971
7400 0.16720062
7600 0.16443534
7800 0.16175961
8000 0.15916926
8200 0.15666038
8400 0.15422934
8600 0.15187272
8800 0.14958727
9000 0.14736985
9200 0.14521751
9400 0.1431275
9600 0.14109737
9800 0.13912444
10000 0.13720647

Hypothesis:  [[0.02574597]
 [0.15147369]
 [0.27998957]
 [0.7929884 ]
 [0.946652  ]
 [0.98256886]] 
Correct (Y):  [[0.]
 [0.]
 [0.]
 [1.]
 [1.]
 [1.]] 
Accuracy:  1.0

結果完美,大功告成!

PS:最後可以安裝一下Keras,pip install keras –i https://pypi.tuna.tsinghua.edu.cn/simple