1. Ref
2. Clone TensorFlow repository
- Clone TensorFlow repo with protobuf sub-modules
$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow
- checkout需要的tag
$ cd tensorflow
$ git tag # 查看有哪些tag
$ git checkout Branch # where Branch is the desired branch
$ git checkout v1.7.0 # checkout v1.7.0
$ git status # 查看当前代码的状态
3. 环境安装 (CPU)
3.1 安装 bazel
- 安装JDK8
$ 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 -
- 安装并升级bazel
$ sudo apt-get update && sudo apt-get install bazel
$ sudo apt-get upgrade bazel
3.2 安装TensorFlow Python依赖包
- numpy, which is a numerical processing package that TensorFlow requires.
- dev, which enables adding extensions to Python.
- pip, which enables you to install and manage certain Python packages.
wheel, which enables you to manage Python compressed packages in the wheel (.whl) format.
对于Python2.7,需要执行
$ sudo apt-get install python-numpy python-dev python-pip python-wheel
- 对于Python3.n, 需要执行
sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel
- 对于Pythoh2.7或Python3.n,在执行TensorFlow的configure时,需要输入对应的路径
4. 配置并编译TensorFlow
4.1 配置TensorFlow
$ ./configure
- 在重新执行configure之前,需要执行bazel clean
- gcc版本要求:gcc 5 or later
4.2 编译pip包
- 仅支持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
- The bazel build command builds a script named build_pip_package
- Running this script as follows will build a .whl file within the /tmp/tensorflow_pkg directory
- 创建.whl文件
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
安装pip包
- 包名依赖于平台和TensorFlow版本叼,对于TensorFlow 1.7.0 on Linux,安装命令如下
$ sudo pip3 install --upgrade pip # 升级pip3
$ sudo pip3 install /tmp/tensorflow_pkg/tensorflow-1.7.0-py2-none-any.whl
5. 验证
ai@aivm:~/tools$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> a = tf.constant(3)
>>> b = tf.constant(5)
>>> c = a + b
>>> sess = tf.Session()
>>> print(sess.run(c))
8
>>>
6. 安装jupyter notebook
IPython是一个 Python 的一个交互式 shell,它提供了很多内建的函数。Jupyter Notebook是IPython的一个Web接口,其实它也支持其它语言;允许您创建和共享包含实时代码,方程,可视化和说明文本的文档。 用途包括:数据清理和转换,数值模拟,统计建模,机器学习等等。
安装Jupyter Notebook
$ sudo pip3 install jupyter
7. 安装Virtualenv
- 安装Virtualenv
$ sudo apt-get install python3-pip python3-dev python-virtualenv # for Python 3.n
- 创建Virtualenv环境
$ virtualenv --system-site-packages -p python3 targetDirectory # for Python 3.n
$ virtualenv --system-site-packages -p python3 ~/tensorflow # for Python 3.n例子
- 激活Virtualenv环境
$ source ~/tensorflow/bin/activate # bash, sh, ksh, or zsh
- 确认pip ≥8.1
(tensorflow)$ easy_install -U pip
- 在激活的Virtualenv环境中安装TensorFlow
(tensorflow)$ pip install --upgrade tensorflow # for Python 2.7
(tensorflow)$ pip3 install --upgrade tensorflow # for Python 3.n
(tensorflow)$ pip install --upgrade tensorflow-gpu # for Python 2.7 and GPU
(tensorflow)$ pip3 install --upgrade tensorflow-gpu # for Python 3.n and GPU
- 退出Virtualenv
(tensorflow)$ deactivate
- 卸载TensorFlow
$ rm -r targetDirectory
$ rm -r ~/tensorflow