tensorflow安装

原创 2018-04-22 01:34 阅读(289)次

TensorFlow 是目前最好的深度学习框架吧,是开源的人工智能框架。

谷歌开源的。好吧,这个名气够大了。

有两种安装方式:从源码安装和基于python包管理器的各种安装方式。

tensorflow主要依赖是两个包,Protocol Buffer 和  Bazel。 

Protocol Buffer 是用来序列化和反序列化的,主要是数据传递用。他序列化后得到的是二进制,反序列化的时候需要用描述数据格式的schema,好处是数据序列化后非常小,而且反序列化很快速,比 xml和json要高效很多。

Bazel是一个构建工具,类似ant,maven。tensorflow本身也是用他编译的。从源码安装需要使用到Bazel。


用docker安装是最简单的吧

步骤

安装docker,再获取镜像,谷歌已经提供了镜像,然后运行起来就可以了····,第一次运行起来会自动下载镜像。

 docker run -it gcr.io/tensorflow/tensorflow bash

这是启动了tensorflow的shell模式。

国内可能会有timeout问题,除了通用手法,可以用国内的镜像

docker run -it tensorflow/tensorflow bash

验证是否安装完成:

启动python

python
在python shell中输入
# Python
import tensorflow as tf
hello = tf.constant('hello tensorflow')
sess = tf.Session()
print(sess.run(hello))
最后输出hello tensorflow 说明成功。




linux上安装   (centos7和6.6),ubuntu在官网有安装步骤,自行查阅。

首先安装python3.4,一般系统自带的是python2.7

yum install python34
安装完成后用

python3 --version
查看是否完整正确。

之后安装setuptools

yum install python34-setuptools
安装完后,可以使用easy_install-3.4。

easy_install-3.4 pip
安装完pip3,用

pip3 -V
检查是否完成。

开始安装tensorflow

pip3 install tensorflow
注意:此命令安装的是不带GPU版本的tensorflow。

注意:   卸载tensorflow方法   pip3 uninstall tensorflow


验证:python3进入python命令行,执行

import tensorflow as tf

我遇到了一个错误

>>> import tensorflow as tf
Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/lib64/python3.4/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
ImportError: /lib64/libc.so.6: version `GLIBC_2.17' not found (required by /usr/lib/python3.4/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *  # pylint: disable=redefined-builtin
  File "/usr/lib/python3.4/site-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/lib64/python3.4/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
ImportError: /lib64/libc.so.6: version `GLIBC_2.17' not found (required by /usr/lib/python3.4/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)


Failed to load the native TensorFlow runtime.
原因:看起来需要glibc 2.17版本,但用rpm -qa|grep glibc查询发现

[root@server1 ~]# rpm -qa|grep glibc
glibc-devel-2.12-1.149.el6.x86_64
glibc-common-2.12-1.149.el6.x86_64
glibc-2.12-1.149.el6.x86_64
MySQL-server-5.6.22-1.linux_glibc2.5.x86_64
glibc-headers-2.12-1.149.el6.x86_64
MySQL-client-5.6.22-1.linux_glibc2.5.x86_64

发现只有glibc 2.12

再查看glibc支持的版本

[root@server1 build]# 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_PRIVATE


需要安装glibc

解决方法:

下载 glibc2.17     地址 http://ftp.gnu.org/gnu/glibc/glibc-2.17.tar.gz

把文件放到服务器上

tar xvf glibc-2.17.tar.gz
cd /home/apps/glibc-2.17
mkdir build
cd ./build
../configure --prefix=/home/apps/glibc-2.17

注意,网上其他地方查到的都是类似上面红字的命令,无非是改成-prefix=/opt/glibc-2.17,然后让你到/lib64目录下去修改软链接,这些方法我试了是不行的,会造成系统的命令失效。基本所有文章都是这样,抄来抄去,却总是抄了错误。我很遗憾,也浪费了我好多时间。请用下面的命令

../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin  

configure过程可能出现这个错误,

checking LD_LIBRARY_PATH variable... contains current directory
configure: error: 
*** LD_LIBRARY_PATH shouldn't contain the current directory when
*** building glibc. Please change the environment variable
*** and run configure again.
请检查LD_LIBRARY_PATH 是否以“:”结尾或者有连续的两个“:”,  请去掉

export LD_LIBRARY_PATH=xxx

make

make install

再次检查glibc

strings /lib64/libc.so.6|grep GLIBC
得到glibc2.17升级完成的

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
再次进入python3,import tensorflow,得到另外一个错误


Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/lib64/python3.4/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.19' not found (required by /usr/lib/python3.4/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *  # pylint: disable=redefined-builtin
  File "/usr/lib/python3.4/site-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/lib64/python3.4/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.19' not found (required by /usr/lib/python3.4/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)

看来得继续升级glibcxx到3.4.19

http://101.96.10.19/ftp.de.debian.org/debian/pool/main/g/gcc-4.8/libstdc++6-4.8-dbg_4.8.4-1_amd64.deb

解压

ar -x libstdc++6-4.8-dbg_4.8.4-1_amd64.deb 
tar xvf data.tar.xz
得到一个/usr目录

cd  usr/lib/x86_64-linux-gnu/debug
[root@server1 debug]# ll
总用量 21920
-rw-r--r-- 1 root root 17997220 12月 20 2014 libstdc++.a
lrwxrwxrwx 1 root root       19 4月  29 00:44 libstdc++.so -> libstdc++.so.6.0.19
lrwxrwxrwx 1 root root       19 4月  29 00:44 libstdc++.so.6 -> libstdc++.so.6.0.19
-rw-r--r-- 1 root root  4444464 12月 20 2014 libstdc++.so.6.0.19

mv /usr/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so.6.bak
cp libstdc++.so /usr/lib64
cd /usr/lib64/
chmod +x libstdc++.so.6.0.19
ln -s libstdc++.so.6.0.19 libstdc++.so.6
检查是否升级成功

[root@server1 debug]# 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_DEBUG_MESSAGE_LENGTH
可以看到glibcxx3.4.19 安装完成。

再次运行python3


>>> import tensorflow as tf
Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/lib64/python3.4/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
ImportError: /lib64/libc.so.6: version `GLIBC_2.18' not found (required by libstdc++.so.6)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *  # pylint: disable=redefined-builtin
  File "/usr/lib/python3.4/site-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "/usr/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "/usr/lib64/python3.4/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
ImportError: /lib64/libc.so.6: version `GLIBC_2.18' not found (required by libstdc++.so.6)

逗我,又说要glibc2.18(上面刚装了2.17), 哎, centos6一些环境过旧确实很烦恼。

用上面编译安装glibc2.17的方式再编译安装一次2.18 ,步骤是一样的。

下载地址:http://ftp.gnu.org/pub/gnu/glibc/glibc-2.18.tar.xz 

亲测可用。



从源码安装方式

从源代码安装TensorFlow 的过程主要就是将TensorFlow 源代码编译成pip 安装包的过程。将Te nsorFlow 的源代码编译为pip 所使用的wheel 文件之后,用pip3 install 的方法就可以完成安装。在编译TensorFlow 源代码之前需要先安装TensorFlo w所依赖的其他工具包。官网有介绍,不过我不建议不熟练的用户从源码安装。

本文完。

本站作品的版权皆为作品作者所有。

本站文字和内容为本站编辑或翻译,部分内容属本站原创,所以转载前务必通知本站并以超链接形式注明内容来自本站,否则以免带来不必要的麻烦。

本站内容欢迎分享,但拒绝有商业目的的转载!