tensorflow源码安装教程

1. 获取原文件

$ git clone https://github.com/tensorflow/tensorflow
$ cd tensorflow
$ git checkout Branch # where Branch is the desired branch
$ git checkout r1.0 #r1.0 can be replaced by other version

2. 依赖

编译tensorflow前须要的依赖:html

    1. bazel
    1. tensorflow的python依赖
    1. 可选: nvidia对tensorflow的GPU支持

2.1 bazel

bazel官网地址 有三种安装方式:java

  • 1.使用APT安装 (建议)python

    • 添加Bazel的分布URI做为源:linux

      echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.listgit

      curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -github

    • 依赖:安装以前可能须要jdk8依赖,能够选择:web

      • google-jdkapi

      • java8-jdkbash

      • java8-sdkoracle

      • oracle-java8-installer

      随便安装一个就能够了

    • 安装bazel:sudo apt-get update && sudo apt-get install bazel

    • 更新到最新的bazel:sudo apt-get upgrade bazel

  • 2.使用二进制文件安装

    • 安装依赖包:sudo apt-get install pkg-config zip g++ zlib1g-dev unzip

    • 下载bazel包,地址。下载bazel-0.5.0-installer-linux-x86_64.sh。

    • 运行安装:

      chmod +x bazel-0.5.0-installer-linux-x86_64.sh

      ./bazel-0.5.0-installer-linux-x86_64.sh --user

    • 设置环境,在~/.bashrc(使用zsh的在~/.zshrc)文件中加入:export PATH="$PATH:$HOME/bin"

    • 最后再更新下bazel:sudo apt-get upgrade bazel

  • 3.编译bazel源文件

    • 确保系统中安装了OpenJDK8,使用命令:sudo apt-get install openjdk-8-jdk

    • 从bazel的release页面下载源文件

    • 解压,而后执行:bash ./compile.sh

2.2 python

须要使用到的python依赖有:

  • numpy
  • dev
  • pip
  • wheel

为python2.7安装这些依赖: $ sudo apt-get install python-numpy python-dev python-pip python-wheel

为python3安装这些依赖: $ sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel

2.3 nvidia依赖

若是不使用GPU就能够跳过这节。

  • nvidia硬件:
    • GPU的cuda计算能力必须大于等于3。能够经过NVIDIA官网查看NVIDIA官方文档
  • nvidia软件:
    • NVIDIA's Cuda Toolkit (>= 7.0)。建议使用版本8.0的。
    • NVIDIA的驱动必须有NVIDIA's Cuda Toolkit
    • cuDNN (>= v3)。建议使用版本5.1的。

最后必须安装libcupti-dev: $ sudo apt-get install libcupti-dev

Mac OS依赖安装页面看这里

3. 开始安装

tensorflow的文件夹中有一个configure的脚本。这个脚本会向你确认tensorflow依赖的路径和一些特别要编译的东西。你必须先运行prior这个脚原本建立pip package和安装tensorflow。

若是你要使用GPU,那么在运行configure的时候写上cuda和cuDNN的版本。若是有多个版本的cuda,那么就选择你想要的版本,而不是系统默认的版本。

运行configure脚本会出现一下相似输出:

$ cd tensorflow  # cd to the top-level directory created
$ ./configure
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N]
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]
No XLA JIT support will be enabled for TensorFlow
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]
Using python library path: /usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with OpenCL support? [y/N] N
No OpenCL support will be enabled for TensorFlow
Do you wish to build TensorFlow with CUDA support? [y/N] Y
CUDA support will be enabled for TensorFlow
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 8.0
Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify the cuDNN version you want to use. [Leave empty to use system default]: 5
Please specify the location where cuDNN 5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size.
[Default is: "3.5,5.2"]: 3.0
Setting up Cuda include
Setting up Cuda lib
Setting up Cuda bin
Setting up Cuda nvvm
Setting up CUPTI include
Setting up CUPTI lib64
Configuration finished

若是你使用GPU,那么configure脚本会在你的系统上建立符号连接到cuda。因此每次改变cuda,在执行bazel build以前就从新运行configure

4. 编译pip package

使用CPU-only,使用:$ 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

注意: gcc => 5: the binary pip packages available on the TensorFlow website are built with gcc 4, which uses the older ABI. To make your build compatible with the older ABI, you need to add --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" to your bazel build command. ABI compatibility allows custom ops built against the TensorFlow pip package to continue to work against your built package.

Tip: 这种须要不少的RAM,若是你的RAM没有吗么多,能够限制RAM的使用:--local_resources 2048,.5,1.0

执行bazel build会生成一个build_pip_package的脚本。运行这个脚本会生成一个.whl的文件在/tmp/tensorflow_pkg文件夹:

$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

5. 安装pip package

执行pip install来安装, 会生成一个.whl的文件。例如在linux上的Tensorlow 1.2.0:$ sudo pip install /tmp/tensorflow_pkg/tensorflow-1.2.0-py2-none-any.whl

6. 验证你的安装是否成功

使用terminal进入python,输入

# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

若是输出Hello, TensorFlow!就表示成功了!

开始使用tensorflow

一些问题

问题

  • 若是碰到AttributeError: 'module' object has no attribute 'Default',那么执行sudo pip install --upgrade protobuf
相关文章
相关标签/搜索