TensorFlow安装手册

  


 

1 TensorFlow简介

TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理。Tensor(张量)意味着N维数组,Flow(流)意味着基于数据流图的计算,TensorFlow为张量从流图的一端流动到另一端计算过程。TensorFlow是将复杂的数据结构传输至人工智能神经网中进行分析和处理过程的系统。

 

2 准备

2.1 环境说明

1  Tensorflow安装版本是0.8.0

2  Python版本是2.7.6,由默认的版本2.6.6升级而来。

3  Tensorflow安装相关参考资源:

 

序号

标题

地址

1

Centos 6.5将python 2.6 升级到 2.7


 

2


CentOS6.5安装Python2.7和Pip

 



3

configure: error: no acceptable C compiler found in $PATH

http://blog.sina.com.cn/s/blog_476c7f7201017pw1.html

 

4

Centos 6.4安装Python 2.7 python-pip


 

5

使用pip安装tensorflow 0.80,python 使用tensorflow 0.80遇到的问题及处理方法


 

 

2.1.1虚拟机里Linux系统版本

[root@localhost ranger-0.5.0-usersync]# cat /etc/issue | grep Linux

Red Hat Enterprise Linux Server release 6.5 (Santiago)

 

2.1.2 Python版本

[root@localhost native]# python  -V

Python 2.6.6

 

 

3

3.1 配置系统源为阿里源

首先确保当前环境可以上外网。这里系统是redhat,如果你是CentOS,需要先备份之前的

cd /etc/yum.repos.d

mv CentOS-Base.repoCentOS-Base.repo.backup

 

#下载阿里的源

wget-O /etc/yum.repos.d/CentOS-Base.repo    http://mirrors.aliyun.com/repo/Centos-6.repo

 

#替换源里的默认配置,更新源

sed-i  's/$releasever/6/g'/etc/yum.repos.d/CentOS-Base.repo    

yumclean all

yumlist

3.2升级Python至2.7.6


#安装Development Tools

yum groupinstall -y'development tools'


#安装所有需要的SSL、bz2、zlib

yum install -y zlib-develbzip2-devel openssl-devel xz-libs zlib


# 下载Python-2.7.6.taz并解压后进入

cd /root/shenl

wget https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tgz

tar -zxvf Python-2.7.6.tgz

cd Python-2.7.6


# 安装

./configure  

make all             

make install  

make clean  

make distclean

 

# 查看Python版本

/usr/local/bin/python2.7 -V

Python 2.7.6

 

# 建立软连接,使系统默认的 python指向 python2.7

mv /usr/bin/python /usr/bin/python2.6.6  

ln -s /usr/local/bin/python2.7 /usr/bin/python



#重新检验Python 版本

python -V

Python 2.7.6

 

Python 软链接指向 Python2.7 版本后,yum是不兼容 Python 2.7的,所以yum不能正常工作,我们需要指定 yum 的Python版本

vim /usr/bin/yum

修改头部的内容

#!/usr/bin/python

为:

#!/usr/bin/python2.6.6

 

相关问题:

1、configure: error: no acceptable C compiler found in $PATH

这个错误是因为gcc没安装,这里可以通过

yum install gcc

 

 

 

3.3 安装setuptools

#下载setuptools-25.1.6.tar.gz


wget --no-check-certificate https://pypi.python.org/packages/1f/7a/6b239a65d452b04ad8068193ae313b386e6fc745b92cd4584fccebecebf0/setuptools-25.1.6.tar.gz


 

 

#解压setuptools-25.1.6

tar -xvfsetuptools-25.1.6.tar.gz 

cd setuptools-25.1.6

 

#安装 setuptools

python2.7 setup.py install

 

注:

安装时报:

  File"/opt/oracle/sor/install/setuptools-0.6c11/setuptools/command/bdist_egg.py",line 236, in run 

   dry_run=self.dry_run, mode=self.gen_header()) 

  File"/opt/oracle/sor/install/setuptools-0.6c11/setuptools/command/bdist_egg.py",line 527, in make_zipfile 

    z =zipfile.ZipFile(zip_filename, mode, compression=compression) 

  File"/usr/local/lib/python2.7/zipfile.py", line 681, in __init__ 

"Compression requires the (missing) zlibmodule"

这里是缺少zlib包,需要通过执行如下命令解决:

yum install -y zlib-devel zlib

如果按照上述步骤已经安装依赖,不会出现此错误。

 

3.4 安装pip

#安装pip

curlhttps://bootstrap.pypa.io/get-pip.py | python2.7 -


3.5 安装tensorflow依赖

#安装python开发包

yum install python-devel

3.6 安装tensorflow

#安装tensorflow


pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl


 

注:

如果下载速度较低,则可以在浏览器自行下载whl再pip install方式离线安装。以numpy为例:

1)   浏览器里访问https://pypi.python.org/pypi

2)   在搜索框里输入numpy

3)   点开numpy 1.12.1

4)   找到numpy-1.12.1-cp27-cp27m-manylinux1_x86_64.whl (md5pgp) 点击下载。

5)   上传到服务器,通过pip install numpy-1.12.1-cp27-cp27m-manylinux1_x86_64.whl离线安装。

 

 

 

4 验证

4.1 测试tensorflow包是否能加载

#启动python,引入tensorflow包验证


python

Python2.7.6 (default, Apr 13 2017, 11:38:40) 
[GCC4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type"help", "copyright", "credits" or"license" for more information.
>>> 
 
# python里输入import tensorflow as tf
>>> importtensorflow as tf
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/__init__.py",line 23, in <module>
     from tensorflow.python import *
   File"/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/__init__.py",line 45, in <module>
     from tensorflow.python import pywrap_tensorflow
   File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py",line 28, in <module>
     _pywrap_tensorflow = swig_import_helper()
   File"/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py",line 24, in swig_import_helper
     _mod = imp.load_module('_pywrap_tensorflow', fp, pathname,description)
 ImportError: /lib64/libc.so.6: version `GLIBC_2.15' not found (required by /usr/local/python27/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so)

 

# 解决方法,安装glibc2.17:

 

cd/root/shenl/
wgethttp://ftp.gnu.org/pub/gnu/glibc/glibc-2.17.tar.xz
 xz -d glibc-2.17.tar.xz
 tar -xvf glibc-2.17.tar
 cd glibc-2.17
 mkdir build
 cd build
 ../configure --prefix=/usr --disable-profile --enable-add-ons--with-headers=/usr/include --with-binutils=/usr/bin  
 make && make install

 

# 验证glibc是否已更新

strings/lib64/libc.so.6|grep GLIBC

# 结果应该是:

GLIBC_2.2.5

GLIBC_2.2.6

GLIBC_2.3

GLIBC_2.3.2

GLIBC_2.3.3

GLIBC_2.3.4

GLIBC_2.4

GLIBC_2.5

GLIBC_2.6

GLIBC_2.7

GLIBC_2.8

GLIBC_2.9

GLIBC_2.10

GLIBC_2.11

GLIBC_2.12

GLIBC_2.13

GLIBC_2.14

GLIBC_2.15

GLIBC_2.16

GLIBC_2.17

GLIBC_PRIVATE

 

再次在python里输入import tensorflow as tf

>>> importtensorflow as tf
>>>import tensorflow as tf
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File"/usr/local/python27/lib/python2.7/site-packages/tensorflow/__init__.py",line 23, in <module>
     from tensorflow.python import *
   File "/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/__init__.py",line 45, in <module>
     from tensorflow.python import pywrap_tensorflow
   File"/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py",line 28, in <module>
     _pywrap_tensorflow = swig_import_helper()
   File"/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py",line 24, in swig_import_helper
     _mod = imp.load_module('_pywrap_tensorflow', fp, pathname,description)
 ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by/usr/local/python27/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so)

 

# 解决方法, 更新libstdc++.so.6.0.20:

下载地址:


 

#应用libstdc++.so.6.0.20,将下载的libstdc++.so.6.0.20放置在/usr/lib64/目录下

 

cd /usr/lib64/

chmod+x libstdc++.so.6.0.20
rm libstdc++.so.6
ln -s libstdc++.so.6.0.20 libstdc++.so.6


#验证GLIBCXX版本

strings/usr/lib64/libstdc++.so.6 | grep GLIBCXX


# 结果应该是:

GLIBCXX_3.4

GLIBCXX_3.4.1

GLIBCXX_3.4.2

GLIBCXX_3.4.3

GLIBCXX_3.4.4

GLIBCXX_3.4.5

GLIBCXX_3.4.6

GLIBCXX_3.4.7

GLIBCXX_3.4.8

GLIBCXX_3.4.9

GLIBCXX_3.4.10

GLIBCXX_3.4.11

GLIBCXX_3.4.12

GLIBCXX_3.4.13

GLIBCXX_3.4.14

GLIBCXX_3.4.15

GLIBCXX_3.4.16

GLIBCXX_3.4.17

GLIBCXX_3.4.18

GLIBCXX_3.4.19

GLIBCXX_3.4.20

GLIBCXX_FORCE_NEW

GLIBCXX_DEBUG_MESSAGE_LENGTH

 

 

#再一次在python里验证

 [root@nn shenl]# python

Python2.7.6 (default, Apr 13 2017, 11:38:40)

[GCC4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2

Type"help", "copyright", "credits" or"license" for more information.

>>>import tensorflow astf

>>>matrix1 =tf.constant([[3., 3.]])

>>>matrix2 = tf.constant([[2.],[2.]])

>>>product =tf.matmul(matrix1, matrix2)

>>>print product

Tensor("MatMul:0",shape=(1, 1), dtype=float32)

>>>sess = tf.Session()

>>>result =sess.run(product)

>>>print result

[[12.]]

5 使用

5.1 Tensorflow基本功能

待补充。

6 总结

仔细跟踪log,具体问题具体分析.