隨着近兩年tensorflow越來越火,在一台新win10系統上裝tensorflow並記錄安裝過程。華碩最近的 Geforce 940mx的機子。
TensorFlow是一個采用數據流圖(data flow graphs),用於數值計算的開源軟件庫。節點(Nodes)在圖中表示數學操作,圖中的線(edges)則表示在節點間相互聯系的多維數據數組,即張量(tensor)。它靈活的架構讓你可以在多種平台上展開計算,例如台式計算機中的一個或多個CPU(或GPU),服務器,移動設備等等。TensorFlow 最初由Google大腦小組(隸屬於Google機器智能研究機構)的研究員和工程師們開發出來,用於機器學習和深度神經網絡方面的研究,但這個系統的通用性使其也可廣泛用於其他計算領域。
tensorflow用起來就是更加方便,高效;一般情況是這樣的,如果你用過其它的框架,比如caffe,那tf的感覺就如同fly一般,上手飛快。
tf有CPU和GPU兩個版本,GPU的安裝需要cuda和cudnn,安裝過程十分簡潔。
因為都要安裝python,pip等包,所以直接安裝Anaconda,但是一定要安裝3.0版本的,地址如下:https://www.anaconda.com/download/#windows
本人安裝的是python3.6版本Anaconda,直接下載根據提示安裝。
- 安裝CPU版本。
CPU版本安裝非常簡潔,只需要在cmd輸入如下指令即可:
pip install --upgrade --ignore-installed tensorflow
- 安裝GPU版本。
同樣,在cmd輸入如下指令:
pip install --upgrade --ignore-installed tensorflow-gpu
GPU版本就安裝好了,但是如果你import tensorflow的話,提示錯誤如下:
ImportError: Could not find 'cudart64_90.dll'. TensorFlow requires 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
顯然,這個時候第二步需要安裝cuda9.0,cuda的安裝也是十分方便,下載地址:https://developer.nvidia.com/cuda-90-download-archive
下載的本地安裝的exe文件即可。百度雲鏈接: https://pan.baidu.com/s/1vbfYVMHLreMlk4yrJqp11A 密碼: ddha
安裝的過程中,cuda需要VS,所以請先安裝VS2015或2013等VS之后,再安裝Cuda,否則,以后你用VS開發項目的話會找不到cuda,也沒有cuda的開發環境,意味着你成功把這兩個給隔離了。
******插入廣告中******
很無奈,雖然大多數安裝tensorflow的同學,都已經安裝了VS和cuda,但是為了追求完美,我還是成功演示一遍。
登陸VS官網,記得要登陸微軟賬戶訂閱上,才能下載,本人下載的是免費的2015社區版。有需要的同學,可看百度雲鏈接:鏈接: https://pan.baidu.com/s/1jJoP6JhdwgXigfKa5FTylw 密碼: nse8
接下來安裝就好啦。
******廣告已結束******
第三步是安裝cudnn,同理登陸英偉達的官網下載cudnn7.0 for cuda9.0。百度雲鏈接如下:
鏈接: https://pan.baidu.com/s/1wP5wix8GLKSi2TrCUJzQmQ 密碼: gq2f
接下來就是將下載的cudnn文件夾的bin、include、lib文件夾下的文件copy到cuda9.0對應的文件夾里即可。
最終,完成了整個tensorflow GPU版本的安裝。
下面就是驗證tensorflow安裝是否真正好了。以下面的代碼為例:
import tensorflow as tf import numpy as np # 使用 NumPy 生成假數據(phony data), 總共 100 個點. x_data = np.float32(np.random.rand(2, 100)) # 隨機輸入 y_data = np.dot([0.100, 0.200], x_data) + 0.300 # 構造一個線性模型 b = tf.Variable(tf.zeros([1])) W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0)) y = tf.matmul(W, x_data) + b # 最小化方差 loss = tf.reduce_mean(tf.square(y - y_data)) optimizer = tf.train.GradientDescentOptimizer(0.5) train = optimizer.minimize(loss) # 初始化變量 init = tf.initialize_all_variables() # 啟動圖 (graph) sess = tf.Session() sess.run(init) # 擬合平面 for step in xrange(0, 201): sess.run(train) if step % 20 == 0: print step, sess.run(W), sess.run(b)
運行后報錯:
SyntaxError: invalid syntax
這是因為python3.0版本對print進行了改進,print變成了函數,因此最后一句應該修改為:
print (step, sess.run(W), sess.run(b))
繼續運行,又報錯:
C:\Users\91612\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`. from ._conv import register_converters as _register_converters WARNING:tensorflow:From C:\Users\91612\Anaconda3\lib\site-packages\tensorflow\python\util\tf_should_use.py:118: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02. Instructions for updating: Use `tf.global_variables_initializer` instead. 2018-04-12 10:34:11.748103: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 2018-04-12 10:34:12.473410: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1344] Found device 0 with properties: name: GeForce 940MX major: 5 minor: 0 memoryClockRate(GHz): 1.2415 pciBusID: 0000:01:00.0 totalMemory: 2.00GiB freeMemory: 1.66GiB 2018-04-12 10:34:12.473725: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1423] Adding visible gpu devices: 0 2018-04-12 10:36:24.849943: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:911] Device interconnect StreamExecutor with strength 1 edge matrix: 2018-04-12 10:36:24.850068: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:917] 0 2018-04-12 10:36:24.850685: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:930] 0: N 2018-04-12 10:36:24.852802: I T:\src\github\tensorflow\tensorflow\core\common_runtime\gpu\gpu_device.cc:1041] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1429 MB memory) -> physical GPU (device: 0, name: GeForce 940MX, pci bus id: 0000:01:00.0, compute capability: 5.0) Traceback (most recent call last): File "example.py", line 27, in <module> for step in xrange(0, 201): NameError: name 'xrange' is not defined
主要是兩個錯誤,initialize_all_variables應該修改為最新的global_variables_initializer;然后python3.0將xrange取消了,只有range函數,因此需要把xrange改為range。
另外,我們還看到一個warning,這主要是因為numpy和h5py之間的版本沖突,主要的解決方法有三個:
- 對numpy版本進行降級,查看本機安裝的numpy版本直接import numpy之后 print (numpy.version.version),本機是1.14.2,降級到穩定的1.13版本;
#自動安裝1.13.0,卸載已安裝的1.14.2 pip install numpy==1.13.0
- 將tensorflow版本降低到1.5.0以下,直接tf.__version__查看本機安裝的是1.7.0,因為1.5.0以下是不存在這個沖突的,這個可在安裝的時候指定版本;
- 不用管這個warning,事實上這個警告沒啥影響,可以眼不見心不煩。例如:
import warnings with warnings.catch_warnings(): warnings.filterwarnings("ignore",category=FutureWarning)
就這樣,完成了tensorflow的完整安裝,事實上這很快就可以完成了,於是乎,我們運行上面的栗子,可以看到:
0 [[-0.11355552 0.47500253]] [ 0.4552581] 20 [[ 0.03362741 0.25130805]] [ 0.30422428] 40 [[ 0.08393362 0.21267535]] [ 0.30089444] 60 [[ 0.09610623 0.20312524]] [ 0.30019006] 80 [[ 0.09905535 0.20076929]] [ 0.30004054] 100 [[ 0.09977061 0.2001891 ]] [ 0.30000868] 120 [[ 0.09994424 0.20004643]] [ 0.30000189] 140 [[ 0.09998644 0.2000114 ]] [ 0.3000004] 160 [[ 0.09999672 0.2000028 ]] [ 0.30000007] 180 [[ 0.09999919 0.20000066]] [ 0.30000004] 200 [[ 0.09999977 0.20000014]] [ 0.30000004]
很完美的呈現出了答案。