转: TensorFlow学习一:源码安装

来源:互联网 发布:md204l软件下载 编辑:程序博客网 时间:2024/05/22 04:31

地址:

http://blog.csdn.net/u013832707/article/details/73161071


费劲那么长时间,终于把它安装好了,不知参考了多少安装tensorflow的网站, 最后是在这个网站安装成功的。


获取源码

在安装目录下运行:

git clone --recurse-submodules https://github.com/tensorflow/tensorflow
  • 1
  • 1

其中–recurse-submodules 参数是必须的, 用于获取 TesorFlow 依赖的 protobuf 库.

安装Bazel

首先依照教程安装 Bazel 的依赖.

sudo apt-get install pkg-config zip g++ zlib1g-dev unzip
  • 1
  • 1

安装openjdk8:

sudo add-apt-repository ppa:openjdk-r/ppasudo apt-get updatesudo apt-get install openjdk-8-jdk
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

配置openjdk 8为默认Java环境:

sudo update-alternatives --config javasudo update-alternatives --config javac
  • 1
  • 2
  • 1
  • 2

然后下载 Bazel 的源码,解压之后在根目录运行:

./compile.shsudo cp output/bazel /usr/local/bin
  • 1
  • 2
  • 1
  • 2

安装其他依赖

sudo apt-get install python-numpy swig python-devsudo apt-get install python-numpy python-dev python-pip python-wheel
  • 1
  • 2
  • 1
  • 2

其中通过apt-get install 安装的pip太老了,需要升级

sudo pip install --upgrade pip
  • 1
  • 1

安装CUDA以及CUDNN

参考http://blog.csdn.net/u013832707/article/details/53157976

安装MKL

去https://registrationcenter.intel.com/en/products/postregistration/?sn=33RM-MPPRMLGB&EmailID=gphsmail%40163.com&Sequence=2038178下载MKL 
解压,运行./install.sh即可,完成后添加环境变量:

export LD_LIBRARY_PATH=/opt/intel/mkl/lib/intel64:$LD_LIBRARY_PATH
  • 1
  • 1

配置安装tensorflow

从源码树的根路径执行:

./configure 
进行一些配置,不知道怎么选的就直接回车默认就好,如下:

gph@gph-pc:~/tensorflow-master/tensorflow $ sudo ./configure You have bazel 0.5.1- 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-packagesPlease 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-packagesDo you wish to build TensorFlow with MKL support? [y/N] yMKL support will be enabled for TensorFlowDo you wish to download MKL LIB from the web? [Y/n] nPlease specify the location where MKL is installed. [Default is /opt/intel/mklml]: /opt/intel/mklPlease 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 enabledDo you wish to build TensorFlow with Google Cloud Platform support? [y/N] No Google Cloud Platform support will be enabled for TensorFlowDo you wish to build TensorFlow with Hadoop File System support? [y/N] No Hadoop File System support will be enabled for TensorFlowDo you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] No XLA support will be enabled for TensorFlowDo you wish to build TensorFlow with VERBS support? [y/N] No VERBS support will be enabled for TensorFlowDo you wish to build TensorFlow with OpenCL support? [y/N] No OpenCL support will be enabled for TensorFlowDo you wish to build TensorFlow with CUDA support? [y/N] yCUDA support will be enabled for TensorFlowDo you want to use clang as CUDA compiler? [y/N] nvcc will be used as CUDA compilerPlease 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  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 6.0]: 5.1Please specify the location where cuDNN 5.1 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: Invalid path to cuDNN  toolkit. Neither of the following two files can be found:/usr/local/cuda-8.0/lib64/libcudnn.so.5.1/usr/local/cuda-8.0/libcudnn.so.5.1.5.1Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 5.1.10Please specify the location where cuDNN 5.1.10 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"]: 6.1Do you wish to build TensorFlow with MPI support? [y/N] MPI support will not be enabled for TensorFlowConfiguration finished
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

编译目标程序, 开启 GPU 支持:

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

输出如下:

At global scope: 
cc1plus: warning: unrecognized command line option “-Wno-self-assign” [enabled by default] 
Target //tensorflow/tools/pip_package:build_pip_package up-to-date: 
bazel-bin/tensorflow/tools/pip_package/build_pip_package 
INFO: Elapsed time: 1737.072s, Critical Path: 175.18s

bazel编译命令建立了一个名为build_pip_package的脚本。运行如下的命令将会在 /tmp/tensorflow_pkg路径下生成一个.whl文件:

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
  • 1
  • 1

输出如下:

~/tensorflow-master/tensorflow 2017年 06月 13日 星期二 20:14:17 CST : === 
Output wheel file is in: /tmp/tensorflow_pkg

安装pip包

使用如下命令安装生成的pip包。注意.whl的名字与你的平台有关:

sudo pip install /tmp/tensorflow_pkg/tensorflow-1.1.0-py2-none-any.whl
  • 1
  • 1

验证你的安装

打开任意一个新的终端,注意不要在tensorflow的安装路径下,运行

python
  • 1
  • 1

输入一下代码

>>> import tensorflow as tf>>> hello = tf.constant('Hello, TensorFlow!')>>> sess = tf.Session()>>> print(sess.run(hello))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

得到输出:

Hello, TensorFlow!

错误记录

一遇到如下错误:

Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "tensorflow/__init__.py", line 24, in <module>    from tensorflow.python import *  File "tensorflow/python/__init__.py", line 49, in <module>    from tensorflow.python import pywrap_tensorflow  File "tensorflow/python/pywrap_tensorflow.py", line 52, in <module>    raise ImportError(msg)ImportError: Traceback (most recent call last):  File "tensorflow/python/pywrap_tensorflow.py", line 41, in <module>    from tensorflow.python.pywrap_tensorflow_internal import *ImportError: No module named pywrap_tensorflow_internalFailed to load the native TensorFlow runtime.See https://www.tensorflow.org/install/install_sources#common_installation_problemsfor some common reasons and solutions.  Include the entire stack traceabove this error message when asking for help.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
Traceback (most recent call last):  File "<stdin>", line 1, in <module>ImportError: No module named tensorflow
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

解决办法: 
如果你是按照中文社区的源码编译方式,那么这就是因为教程太旧了的原因,请按照上文的方法安装。

二遇到如下错误:

Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "/usr/local/lib/python2.7/dist-packages/tensorflow/__init__.py", line 23, in <module>    from tensorflow.python import *  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/__init__.py", line 53, in <module>    from tensorflow.core.framework.graph_pb2 import *  File "/usr/local/lib/python2.7/dist-packages/tensorflow/core/framework/graph_pb2.py", line 9, in <module>    from google.protobuf import symbol_database as _symbol_database  File "/usr/local/lib/python2.7/dist-packages/google/protobuf/symbol_database.py", line 164, in <module>    _DEFAULT = SymbolDatabase(pool=descriptor_pool.Default())AttributeError: 'module' object has no attribute 'Default'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

请升级你的protobuf

sudo pip install --upgrade protobuf

原创粉丝点击