本文介绍如何在 Ubuntu 系统中配置 TensorFlow 的开发环境,需要的 Ubuntu 版本为 14.04 或者更高版本。
确认显卡
-
安装前需要先根据你的电脑是否安装了 NVIDIA 的显卡来确认一下需要安装的 TensorFlow 版本:
- CPU 版本 如果你的电脑没有安装 NVIDIA 显卡,只能安装 CPU 版本,这个版本安装会比较快(大概10分钟左右),即使有 NVIDIA 显卡但只是想体验一下 TensorFlow,也可以安装这个版本。
- GPU 版本 如果你的电脑安装了 NVIDIA 显卡,可以安装 GPU 版本,使用 GPU 进行深度学习的计算会比用 CPU 快很多。选择安装 GPU 版本的 TensorFlow 前先确认如下信息:
-
确认你的显卡是否支持 CUDA Compute Capability 3.0 或更高版本,下面是 GeForce 系列显卡的支持情况:
台式机版 | 笔记本版 | ||
---|---|---|---|
GPU型号 | Compute Capability | GPU型号 | 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 套件及相关驱动,CUDA 是 NVIDIA 推出的基于自家 GPU 的运算平台,下载地址
- 安装 cuDNN v6.0,并在环境变量里配置好
CUDA_HOME
,cuDNN 是基于 CUDA 的深度神经网络库,下载地址 - 安装
libcupti-dev
,这是用于调试 CUDA 的库
$ sudo apt-get install libcupti-dev
安装官方发布版 TensorFlow
官方提供的安装方法有virtualenv、native pip、Docker、Anaconda。如果只是想简单的体验一下 TensorFlow,之后会删除,可以使用virtualenv安装;如果是进行深入学习、开发,最好还是通过pip安装。下面介绍的是使用pip安装 TensorFlow 的方法。
- 首先确认 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
- 安装 TensorFlow
安装 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 版
$ 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
需要根据 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 版本在上面没有找到,那么只能通过下载源码自己编译安装。通过源码编译安装的方法下面会介绍。
编译源码安装 TensorFlow
如果你安装的 Python 版本官方没有提供安装包,或者你想要修改 TensorFlow Core 相关的功能,可以尝试通过 Google 发布的 TensorFlow 源码,自己编译、安装 TensorFlow。目前只有 Ubuntu 和 Mac OS X 系统支持编译 TensorFlow 源码,Window 系统不支持编译 TensorFlow 源码。
- 安装必要的依赖软件
安装 JDK 1.8
$ sudo apt-get install openjdk-8-jdk
安装 Bazel
$ echo "deb [arch=amd64] http://storage.googleapis.com/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,使用这个命令更新到最新版
安装 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
-
如果选择 GPU 版,还需要安装 CUDA 等相关库,参考 NVIDIA's documentation
-
下载 TensorFlow 代码
$ git clone https://github.com/tensorflow/tensorflow # clone 代码
$ cd tensorflow # 进入代码目录
$ git branch -a # 查看所有分支,找到最新版本,我的为 remotes/origin/r1.4
$ git checkout r1.4 # 切到最新版本
- 配置编译参数,运行
configure
文件,一路按回车使用默认值就可以
$ ./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
- 启动编译
编译 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
编译成功会打印下面的信息,我编译用了将近30分钟
INFO: Elapsed time: 1605.225s, Critical Path: 44.86s
INFO: Build completed successfully, 4223 total actions
使用下面的命令将编译结果打包成whl
文件,并放到/tmp/tensorflow_pkg
目录
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
最终编译出来的whl
文件名会根据使用的 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
验证 TensorFlow
安装完成后可以先运行一段代码验证一下 TensorFlow 是否安装成功:
- 启动一个命令终端
- 启动 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))
- 如果运行第一句
import tensorflow as tf
时没有报错ImportError: No module named tensorflow
,就说明 TensorFlow 安装成功了 - 如果执行完最后一句打出
Hello, TensorFlow!
,说明 TensorFlow 可以正常运行
卸载 TensorFlow
如果是通过 pip 或 pip3 安装的 TensorFlow,可以使用下面的命令卸载 TensorFlow
$ sudo pip uninstall tensorflow # for Python 2.7
$ sudo pip3 uninstall tensorflow # for Python 3.n
网友评论