本文介紹如何在 Ubuntu 系統(tǒng)中配置 TensorFlow 的開發(fā)環(huán)境,需要的 Ubuntu 版本為 14.04 或者更高版本。
確認(rèn)顯卡
臺(tái)式機(jī)版 |
|
筆記本版 |
|
GPU型號(hào) |
Compute Capability |
GPU型號(hào) |
Compute Capability |
NVIDIA TITAN Xp |
6.1 |
GeForce GTX 1080 |
6.1 |
NVIDIA TITAN X |
6.1 |
GeForce GTX 1070 |
6.1 |
GeForce GTX 1080 Ti |
6.1 |
GeForce GTX 1060 |
6.1 |
GeForce GTX 1080 |
6.1 |
GeForce GTX 980 |
5.2 |
GeForce GTX 1070 |
6.1 |
GeForce GTX 980M |
5.2 |
GeForce GTX 1060 |
6.1 |
GeForce GTX 970M |
5.2 |
GeForce GTX 1050 |
6.1 |
GeForce GTX 965M |
5.2 |
GeForce GTX TITAN X |
5.2 |
GeForce GTX 960M |
5.0 |
GeForce GTX TITAN Z |
3.5 |
GeForce GTX 950M |
5.0 |
GeForce GTX TITAN Black |
3.5 |
GeForce 940M |
5.0 |
GeForce GTX TITAN |
3.5 |
GeForce 930M |
5.0 |
GeForce GTX 980 Ti |
5.2 |
GeForce 920M |
3.5 |
GeForce GTX 980 |
5.2 |
GeForce 910M |
5.2 |
GeForce GTX 970 |
5.2 |
GeForce GTX 880M |
3.0 |
GeForce GTX 960 |
5.2 |
GeForce GTX 870M |
3.0 |
GeForce GTX 950 |
5.2 |
GeForce GTX 860M |
3.0/5.0 |
GeForce GTX 780 Ti |
3.5 |
GeForce GTX 850M |
5.0 |
GeForce GTX 780 |
3.5 |
GeForce 840M |
5.0 |
GeForce GTX 770 |
3.0 |
GeForce 830M |
5.0 |
GeForce GTX 760 |
3.0 |
GeForce GTX 780M |
3.0 |
GeForce GTX 750 Ti |
5.0 |
GeForce GTX 770M |
3.0 |
GeForce GTX 750 |
5.0 |
GeForce GTX 765M |
3.0 |
GeForce GTX 690 |
3.0 |
GeForce GTX 760M |
3.0 |
GeForce GTX 680 |
3.0 |
GeForce GTX 680MX |
3.0 |
GeForce GTX 670 |
3.0 |
GeForce GTX 680M |
3.0 |
GeForce GTX 660 Ti |
3.0 |
GeForce GTX 675MX |
3.0 |
GeForce GTX 660 |
3.0 |
GeForce GTX 670MX |
3.0 |
GeForce GTX 650 Ti BOOST |
3.0 |
GeForce GTX 660M |
3.0 |
GeForce GTX 650 Ti |
3.0 |
GeForce GT 750M |
3.0 |
GeForce GTX 650 |
3.0 |
GeForce GT 650M |
3.0 |
GeForce GT 740 |
3.0 |
GeForce GT 745M |
3.0 |
GeForce GT 730 |
3.5 |
GeForce GT 645M |
3.0 |
GeForce GT 720 |
3.5 |
GeForce GT 740M |
3.0 |
GeForce GT 705 |
3.5 |
GeForce GT 730M |
3.0 |
GeForce GT 640 (GDDR5) |
3.5 |
GeForce GT 640M |
3.0 |
- |
- |
GeForce GT 640M LE |
3.0 |
- |
- |
GeForce GT 735M |
3.0 |
- |
- |
GeForce GT 730M |
3.0 |
* 安裝 CUDA Toolkit 8.0 套件及相關(guān)驅(qū)動(dòng),CUDA 是 NVIDIA 推出的基于自家 GPU 的運(yùn)算平臺(tái),下載地址
* 安裝 cuDNN v6.0,并在環(huán)境變量里配置好 CUDA_HOME ,cuDNN 是基于 CUDA 的深度神經(jīng)網(wǎng)絡(luò)庫,下載地址
* 安裝libcupti-dev ,這是用于調(diào)試 CUDA 的庫
$ sudo apt-get install libcupti-dev
安裝官方發(fā)布版 TensorFlow
官方提供的安裝方法有virtualenv、native pip、Docker、Anaconda。如果只是想簡單的體驗(yàn)一下 TensorFlow,之后會(huì)刪除,可以使用virtualenv安裝;如果是進(jìn)行深入學(xué)習(xí)、開發(fā),最好還是通過pip安裝。下面介紹的是使用pip安裝 TensorFlow 的方法。
- 首先確認(rèn) pip 或 pip3 是否安裝
$ sudo apt-get install python-pip python-dev # for Python 2.7
$ sudo apt-get install python3-pip python3-dev # for Python 3.n
安裝 CPU 版
$ sudo pip install tensorflow # Python 2.7; CPU support (no GPU support)
$ sudo pip3 install tensorflow # Python 3.n; CPU support (no GPU support)
安裝 GPU 版
bash
$ sudo pip install tensorflow-gpu # Python 2.7; GPU support
$ sudo pip3 install tensorflow-gpu # Python 3.n; GPU support
如果使用上面的命令安裝失敗,可以嘗試使用
$ sudo pip install --upgrade <tfBinaryURL> # Python 2.7
$ sudo pip3 install --upgrade <tfBinaryURL> # Python 3.n
tfBinaryURL 需要根據(jù) Python 版本以及是否支持 NAVIDIA 顯卡確定:
Python 2.7:
# CPU 版
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp27-none-linux_x86_64.whl
# GPU 版
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.4.0-cp27-none-linux_x86_64.whl
Python 3.4:
# CPU 版
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp34-cp34m-linux_x86_64.whl
# GPU 版
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.4.0-cp34-cp34m-linux_x86_64.whl
Python 3.5:
# CPU 版
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp35-cp35m-linux_x86_64.whl
# GPU 版
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.4.0-cp35-cp35m-linux_x86_64.whl
Python 3.6:
# CPU 版
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp36-cp36m-linux_x86_64.whl
# GPU 版
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.4.0-cp36-cp36m-linux_x86_64.whl
如果你使用的 Python 版本在上面沒有找到,那么只能通過下載源碼自己編譯安裝。通過源碼編譯安裝的方法下面會(huì)介紹。
編譯源碼安裝 TensorFlow
如果你安裝的 Python 版本官方?jīng)]有提供安裝包,或者你想要修改 TensorFlow Core 相關(guān)的功能,可以嘗試通過 Google 發(fā)布的 TensorFlow 源碼,自己編譯、安裝 TensorFlow。目前只有 Ubuntu 和 Mac OS X 系統(tǒng)支持編譯 TensorFlow 源碼,Window 系統(tǒng)不支持編譯 TensorFlow 源碼。
安裝 JDK 1.8
$ sudo apt-get install openjdk-8-jdk
安裝 Bazel
$ echo "deb [arch=amd64] http://storage./bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
$ curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install bazel
$ sudo apt-get upgrade bazel # 如果之前安裝過 Bazel,使用這個(gè)命令更新到最新版
安裝 Python,可以選擇安裝 Python 2.7 或者 Python 3 其一
安裝 Python 2.7:
$ sudo apt-get install python-numpy python-dev python-pip python-wheel
安裝 Python 3.n:
$ sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel
$ git clone https://github.com/tensorflow/tensorflow # clone 代碼
$ cd tensorflow # 進(jìn)入代碼目錄
$ git branch -a # 查看所有分支,找到最新版本,我的為 remotes/origin/r1.4
$ git checkout r1.4 # 切到最新版本
- 配置編譯參數(shù),運(yùn)行
configure 文件,一路按回車使用默認(rèn)值就可以
$ ./configure
You have bazel 0.8.1 installed.
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3
Found possible Python library paths:
/usr/local/lib/python3.5/dist-packages
/usr/lib/python3/dist-packages
Please input the desired Python library path to use. Default is [/usr/local/lib/python3.5/dist-packages]
Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]:
jemalloc as malloc support will be enabled for TensorFlow.
Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]:
Google Cloud Platform support will be enabled for TensorFlow.
Do you wish to build TensorFlow with Hadoop File System support? [Y/n]:
Hadoop File System support will be enabled for TensorFlow.
Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]:
Amazon S3 File System support will be enabled for TensorFlow.
Do you wish to build TensorFlow with XLA JIT support? [y/N]:
No XLA JIT support will be enabled for TensorFlow.
Do you wish to build TensorFlow with GDR support? [y/N]:
No GDR support will be enabled for TensorFlow.
Do you wish to build TensorFlow with VERBS support? [y/N]:
No VERBS support will be enabled for TensorFlow.
Do you wish to build TensorFlow with OpenCL support? [y/N]:
No OpenCL support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]:
No CUDA support will be enabled for TensorFlow.
Do you wish to build TensorFlow with MPI support? [y/N]:
No MPI support will be enabled for TensorFlow.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is ch=native]:
Add "--config=mkl" to your bazel command to build with MKL support.
Please note that MKL on MacOS or windows is still not supported.
If you would like to use a local MKL instead of downloading, please set the environment variable "TF_MKL_ROOT" every time fore build.
Configuration finished
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
編譯 CPU 版本使用下面的命令:
$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
編譯 GPU 版本使用下面的命令:
$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
編譯成功會(huì)打印下面的信息,我編譯用了將近30分鐘
INFO: Elapsed time: 1605.225s, Critical Path: 44.86s
INFO: Build completed successfully, 4223 total actions
使用下面的命令將編譯結(jié)果打包成whl 文件,并放到/tmp/tensorflow_pkg 目錄
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
最終編譯出來的whl 文件名會(huì)根據(jù)使用的 Python 版本,以及電腦配置而不同,比如我的電腦使用 Python 3.5 編譯出來的就是tensorflow-1.4.1-cp35-cp35m-linux_x86_64.whl
使用 pip 或者 pip3 安裝前面編譯出來的whl 文件
$ sudo pip3 install /tmp/tensorflow_pkg/tensorflow-1.4.1-cp35-cp35m-linux_x86_64.whl
驗(yàn)證 TensorFlow
安裝完成后可以先運(yùn)行一段代碼驗(yàn)證一下 TensorFlow 是否安裝成功:
- 啟動(dòng)一個(gè)命令終端
- 啟動(dòng) Python
$ python # for Python 2.7
$ python3 # for Python 3.n
# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
- 如果運(yùn)行第一句
import tensorflow as tf 時(shí)沒有報(bào)錯(cuò)ImportError: No module named tensorflow ,就說明 TensorFlow 安裝成功了 - 如果執(zhí)行完最后一句打出
Hello, TensorFlow! ,說明 TensorFlow 可以正常運(yùn)行
卸載 TensorFlow
如果是通過 pip 或 pip3 安裝的 TensorFlow,可以使用下面的命令卸載 TensorFlow
$ sudo pip uninstall tensorflow # for Python 2.7
$ sudo pip3 uninstall tensorflow # for Python 3.n
|