tensorflow源码安装教程

本文详细介绍了如何从源码安装TensorFlow的过程,包括获取源文件、安装依赖(如Bazel、Python库及NVIDIA组件)、配置环境、编译pip包及安装验证等步骤。

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前需要的依赖:

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

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

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

    • 添加Bazel的分布URI作为源:

      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 -

    • 依赖:安装之前可能需要jdk8依赖,可以选择:

      • google-jdk

      • java8-jdk

      • java8-sdk

      • 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

转载于:https://my.oschina.net/u/1046919/blog/1154302

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值