ubuntu 源码安装tensorflow

 

敲黑板,画重点:能不用源码安装就不要用源码安装,若非必要请不要作死,若非必要请不要作死,若非必要请不要作死

tensorflow模型量化功能需要源码安装tensorflow,如若已安装,请自行卸载

安装编译工具bazel

传送门:https://docs.bazel.build/versions/master/install.html

 

下载tensorflow源码

git clone https://github.com/tensorflow/tensorflow 

源码下载完成之后,转到tensorflow的根目录下

cd tensorflow
git checkout Branch # where Branch is the desired branch

查看分支

git branch -a

例如,要使用 r1.8 版本而不是主版本,可发出以下命令:

git checkout r1.8

安装依赖库

python2

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

 

编译

这里只安装cpu版本,gpu请移步https://tensorflow.google.cn/install/install_sources

在tensorflow的根目录下,终端输入

注:由于配置没有保存,以下配置是官网复制粘贴来的,本人只选了一个y和一个默认选项,其他全选n

Do you wish to use jemalloc as the malloc implementation? [Y/n]y
jemalloc enabled
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:

 

./configure
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7
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/lib/python2.7/dist-packages]

Using python library path: /usr/local/lib/python2.7/dist-packages
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 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] Y
CUDA support will be enabled for TensorFlow
Do you want to use clang as CUDA compiler? [y/N]
nvcc will be used as CUDA compiler
Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 9.0
Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7
Please specify the location where cuDNN 7 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.

Do you wish to build TensorFlow with MPI support? [y/N]
MPI support will not be enabled for TensorFlow
Configuration finished

 

编译pip包

bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

这里耗时久,请耐心等候。编译过程很可能会出错,本人就是一直卡在这里。

出现以上问题,是swap空间不够,可执行如下代码

# 生成swap镜像文件,空间大小是bs*count 即 1M*1024
sudo dd if=/dev/zero of=/mnt/1024Mb.swap bs=1M count=1024 
# 对该镜像文件格式化
sudo mkswap /mnt/1024Mb.swap
# 挂载该镜像文件 
sudo swapon /mnt/1024Mb.swap

参考链接:https://blog.csdn.net/dengheCSDN/article/details/78195573

参考链接:https://stackoverflow.com/questions/45796177/bazel-tensorflow-inception-v3-retraining-attributeerror-int-object-attrib

ERROR: /home/lgq1/tensorflow/tensorflow/tools/api/generator/BUILD:27:1: Executing genrule //tensorflow/tools/api/generator:python_api_gen failed (Exit 1)
Traceback (most recent call last):
  File "/home/lgq1/.cache/bazel/_bazel_lgq1/76d7b150de91c5a394b669718cf07178/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/tools/api/generator/create_python_api.runfiles/org_tensorflow/tensorflow/tools/api/generator/create_python_api.py", line 26, in <module>
    from tensorflow.python.util import tf_decorator
  File "/home/lgq1/.cache/bazel/_bazel_lgq1/76d7b150de91c5a394b669718cf07178/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/tools/api/generator/create_python_api.runfiles/org_tensorflow/tensorflow/python/__init__.py", line 121, in <module>
    from tensorflow.python.platform import test
  File "/home/lgq1/.cache/bazel/_bazel_lgq1/76d7b150de91c5a394b669718cf07178/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/tools/api/generator/create_python_api.runfiles/org_tensorflow/tensorflow/python/platform/test.py", line 61, in <module>
    import mock                # pylint: disable=g-import-not-at-top,unused-import
ImportError: No module named mock
Target //tensorflow/tools/pip_package:build_pip_package failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 5440.488s, Critical Path: 198.20s
INFO: 5965 processes: 5965 local.
FAILED: Build did NOT complete successfully

出现这样的错误,直接安装相应的包就可以了

pip install mock

 

出现以上错误,修改代码,修改tensorflow/tensorflow/tools/api/generator目录的create_python_api.py 第161行代码。修改如下

编译成功之后,执行以下代码,生成TensorFlow的安装包,在/tmp/tensorflow_pkg目录下

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
sudo pip install /tmp/tensorflow_pkg/tensorflow-1.8.0-cp35-cp35m-linux_x86_64.whl

等待安装成功。

测试

python 
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  2 2016, 17:53:06) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('hello')
>>> sess = tf.Session()
>>> print(sess.run(hello))
b'hello'

至此,安装完成。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值