TensorFlow学习一:源码安装

获取源码

在安装目录下运行:

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

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

安装Bazel

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

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

安装openjdk8:

sudo add-apt-repository ppa:openjdk-r/ppa
sudo apt-get update
sudo apt-get install openjdk-8-jdk

配置openjdk 8为默认java环境:

sudo update-alternatives --config java
sudo update-alternatives --config javac

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

./compile.sh
sudo cp output/bazel /usr/local/bin

安装其他依赖

sudo apt-get install python-numpy swig python-dev
sudo apt-get install python-numpy python-dev python-pip python-wheel

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

sudo pip install --upgrade pip

安装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

配置安装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-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 MKL support? [y/N] y
MKL support will be enabled for TensorFlow
Do you wish to download MKL LIB from the web? [Y/n] n
Please specify the location where MKL is installed. [Default is /opt/intel/mklml]: /opt/intel/mkl
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, 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.1
Please 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.1
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 5.1.10
Please 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.1
Do you wish to build TensorFlow with MPI support? [y/N] 
MPI support will not be enabled for TensorFlow
Configuration finished

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

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

输出如下:

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

输出如下:

~/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

验证你的安装

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

python

输入一下代码

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

得到输出:

Hello, TensorFlow!

错误记录

一遇到如下错误:

Traceback (most recent call last):
  File "", 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_internal


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.
Traceback (most recent call last):
  File "", line 1, in <module>
ImportError: No module named tensorflow

解决办法:
如果你是按照中文社区的源码编译方式,那么这就是因为教程太旧了的原因,请按照上文的方法安装。
根据qq_23926575的回答,也有可能是在编译完之后没有退出当前目录导致的,可以尝试回到主目录再运行。

二遇到如下错误:

Traceback (most recent call last):
  File "", 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'

请升级你的protobuf

sudo pip install --upgrade protobuf

你可能感兴趣的:(TensorFlow)