tensorflow 源码编译安装以及遇到的一些错误

来源:互联网 发布:梦幻西游数据错误 编辑:程序博客网 时间:2024/05/01 15:31

tensorflow 源码编译在ubuntu 自带的Python上安装比较方便,下面一些地方就可以默认了(我是在自带Python上安装的)

其他配置就不累述了,直说我想说的

bazel 安装0.5.2,还是不要安装最新的

sudo ./configure   除了cuda,cudnn相关选项是自己选择外(可能每个人安装cuda的地方不一样),全是是默认按enter就对了

这是我的

saners@saners:~/tensorflow$ sudo ./configure
[sudo] password for saners: 
WARNING: Running Bazel server needs to be killed, because the startup options are different.
You have bazel 0.5.2 installed.
Please specify the location of python. [Default is /usr/bin/python]: 
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
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]: 
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 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]: y
CUDA support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 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 default to cuDNN 6.0]: 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: 5.0]
Do you want to use clang as CUDA compiler? [y/N]: 
nvcc will be used as CUDA compiler.


Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: 
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 -march=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 before build.
Configuration finished

说明:记得cudnn要写自己的版本,我的用的是5.1,所以我这个问题写了5

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 default to cuDNN 6.0]: 5

上面的没问题就执行下面的命令

bazel build -c opt --config=cuda  //tensorflow/tools/pip_package:build_pip_package
  • 1
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorlflow_pkg

(以上三个命令都是在tensorflow根目录编译执行的)

cd //tmp/tensorflow_pkg(打开终端就可以执行)

sudo pip install tensorflow_xxx.whl(替换为你自己生成的whl文件名)

执行完以后打开Python import tensorflow 测试

遇到的第一个问题:

ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory
Failed to load the native TensorFlow runtime.

解决方法:打开.bashrc 末尾添加export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64/

如果还是不行,那就试试打开终端 sudo vi /etc/environment 也就是把environment文件打开输入

LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/include:/usr/local/cuda/include:

$LD_LIBRARY_PATH

然后命令行:source ~/.bashrc (我的tensorflow放在home目录下的,ctrl+h就可以把隐藏的.bashrc打开,

或者命令行sudo vi .bashrc打开编辑,这比较麻烦)。

遇到的第二个问题:ImportError: No module named enum

解决方法:sudo pip install enum

遇到的第三个问题:ImportError: cannot import name pywrap_tensorflow 或者说是“__doc__” can read-only 什么的我记不清了

这两个问题是在我输入了import tensorflow as tf 和 import tensorflow 分别出现的,所以就算是一个了。

遇到这个问题真的是运气好到不行。。。我试了无数次各种方法,被认可最多的方法是不在tensorflow目录下打开python而是换一个目录

以及还有一个就是重装six,但是都无法解决这个问题。

我误打误撞安装了jupyter,没想到居然可以了,sudo pip install jupyter

安装好后可以现在命令行输入jupyter notebook 在jupyter工具中试一下import tensorflow

我猜是安装jupyter的一些依赖包也正好是我这个问题缺少的,希望这个解决方式可以给走投无路的朋友帮助!

原创粉丝点击