1. 程式人生 > >TensorFlow GPU Anaconda ubuntu安裝

TensorFlow GPU Anaconda ubuntu安裝

1. Anaconda 安裝
官方網站下載安裝包,直接執行即可。

bash Anaconda3-4.3.1-Linux-x86_64.sh  #Python 3.5 版本

查詢安裝資訊

conda info

查詢當前已經安裝的庫

conda list

安裝庫

conda install pkg_name

更新庫

conda update pkg_name
conda update -- all #更新所有包

更新Anaconda

conda update conda

2. Tensorflow 安裝

官方下載更新工具包的速度很慢,所以繼續新增清華大學 TUNA提供的Anaconda倉庫映象

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
conda install numpy   #測試是否新增成功

Tensorflow安裝
新建Anaconda 環境:

conda create -n tensorflow python=3.6

啟用tensorflow虛擬環境(之後的使用每次也都要先啟用虛擬環境才可用):

source activate tensorflow

關閉tensorflow虛擬環境:

source deactivate

命令搜尋當前可用的tensorflow版本

anaconda search -t conda tensorflow

選擇需要的版本:

anaconda show anaconda/tensorflow-gpu

從打印出的命令安裝:

conda install --channel https://conda.anaconda.org/anaconda tensorflow-gpu

測試安裝

source activate tensorflow
python
import tensorflow as tf
tf.__version__
輸:'1.8.0'

遇到問題:
session = tf.session()

E tensorflow/core/common_runtime/direct_session.cc:154] Internal: CUDA runtime implicit initialization on GPU:0 failed. Status: unknown error

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1" #設定使用的GPU,並沒用解決使用GPU的問題。
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
session = tf.Session()
print(sess.run(hello))

輸出:
2018-06-24 20:35:02.462192: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-06-24 20:35:02.470414: E tensorflow/stream_executor/cuda/cuda_driver.cc:406] failed call to cuInit: CUDA_ERROR_NO_DEVICE
2018-06-24 20:35:02.470590: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:158] retrieving CUDA diagnostic information for host: zc-GE62-2QD
2018-06-24 20:35:02.470633: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:165] hostname: zc-GE62-2QD
2018-06-24 20:35:02.470778: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:189] libcuda reported version is: 384.130.0
2018-06-24 20:35:02.470887: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:193] kernel reported version is: 384.130.0
2018-06-24 20:35:02.470925: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:300] kernel version seems to match DSO: 384.130.0

這是由於TensorFlow 使用的是Cuda-9.2的驅動版本,可參照tensorflow官網的介紹:tf安裝指導
並參照其中的CUDA安裝連結:cuda 安裝連結
本人下載了.run的安裝檔案:cuda_9.2.88_396.26_linux.run 和 cuda_9.2.88.1_linux.run, 先安裝前者。
安裝命令:

sudo ./cuda_9.2.88_396.26_linux.run
sudo ./cuda_9.2.88.1_linux.run

注意:安裝CUDA驅動需要ctrl+alt+f1, 進入tty命令列安裝,並關閉x-config:sudo service lightdm stop
另外,安裝的時候需要注意不要安裝open-gl選項,不選擇nvidia-xconfig選項,選擇上述選項會導致重啟後
進入登入介面的無限迴圈,如果不幸進入了迴圈,請參照恢復連結:登入迴圈恢復連結

測試:

(tensorflow) [email protected]2QD:~$ python
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:51:32) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('hello, Tensorflow')
>>> session=tf.Session()
2018-07-28 00:02:35.801458: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2018-07-28 00:02:35.986928: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:898] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2018-07-28 00:02:35.987282: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1356] Found device 0 with properties: 
name: GeForce GTX 960M major: 5 minor: 0 memoryClockRate(GHz): 1.176
pciBusID: 0000:01:00.0
totalMemory: 1.96GiB freeMemory: 1.92GiB
2018-07-28 00:02:35.987302: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0
2018-07-28 00:05:14.965212: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-07-28 00:05:14.965244: I tensorflow/core/common_runtime/gpu/gpu_device.cc:929]      0 
2018-07-28 00:05:14.965253: I tensorflow/core/common_runtime/gpu/gpu_device.cc:942] 0:   N 
2018-07-28 00:05:14.965397: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1687 MB memory) -> physical GPU (device: 0, name: GeForce GTX 960M, pci bus id: 0000:01:00.0, compute capability: 5.0)
>>> print(session.run(hello))
b'hello, Tensorflow'
>>> 

終於配置GPU 成功,接下來的一個部落格將實現CNN 和 RNN的Mnist識別實現程式碼。