TensorFlow的安装

来源:互联网 发布:淘宝网安徽食品药品 编辑:程序博客网 时间:2024/06/09 20:35

1、官网介绍TensorFlow的安装,可以先了解一下。
https://www.tensorflow.org/versions/r0.12/get_started/os_setup#pip_installation
2、下面我们选择pip安装方法。
安装好以后导入tensorflow的模块
如果报下面的错误一:

[root@localhost ~]pythonPython 2.7.13 (default, Jul  5 2017, 15:14:54)[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import tensorflowTraceback (most recent call last):  File "<stdin>", line 1, in <module>  File "/opt/Python-2.7.13/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>    from tensorflow.python import *  File "/opt/Python-2.7.13/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 49, in <module>    from tensorflow.python import pywrap_tensorflow  File "/opt/Python-2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 52, in <module>    raise ImportError(msg)ImportError: Traceback (most recent call last):  File "/opt/Python-2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 41, in <module>    from tensorflow.python.pywrap_tensorflow_internal import *  File "/opt/Python-2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>    _pywrap_tensorflow_internal = swig_import_helper()  File "/opt/Python-2.7.13/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)ImportError: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /opt/Python-2.7.13/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)Failed to load the native TensorFlow runtime.See https://www.tensorflow.org/install/install_sources#common_installation_problems
[root@localhost ~]# strings /lib64/libc.so.6 |grep GLIBC_GLIBC_2.2.5GLIBC_2.2.6GLIBC_2.3GLIBC_2.3.2GLIBC_2.3.3GLIBC_2.3.4GLIBC_2.4GLIBC_2.5GLIBC_2.6GLIBC_2.7GLIBC_2.8GLIBC_2.9GLIBC_2.10GLIBC_2.11GLIBC_2.12GLIBC_PRIVATE

glibc版本需glibc2.14(升级到2.17),如下升级:

[root@localhost soft]# wget http://ftp.gnu.org/gnu/glibc/glibc-2.17.tar.gz[root@localhost soft]# tar xf glibc-2.17.tar.gz[root@localhost soft]# cd glibc-2.17[root@localhost glibc-2.17]# mkdir build; cd build[root@localhost build]# ../configure --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin

这里可能会报错,错误信息如下:

LD_LIBRARY_PATH shouldn't contain the current directory when building glibc. Please change the environment variable 

解决方法:
打印出你的echo $LD_LIBRARY_PATH的值,你会发现结尾会有一个“”:“”号,该值的解释这里引用其他人写的博客,我也是按照这个方法的去做的,我的操作是将$LD_LIBRARY_PATH值的末尾的冒号去掉了.
http://blog.csdn.net/ldl22847/article/details/18702645
http://blog.csdn.net/anda0109/article/details/39229597

接下来就是编译安装了,然后我们看看glibc升级到2.17这个版本了。

[root@localhost build]# make[root@localhost build]# make install[root@localhost build]# strings /lib64/libc.so.6 | grep GLIBCGLIBC_2.2.5GLIBC_2.2.6GLIBC_2.3GLIBC_2.3.2GLIBC_2.3.3GLIBC_2.3.4GLIBC_2.4GLIBC_2.5GLIBC_2.6GLIBC_2.7GLIBC_2.8GLIBC_2.9GLIBC_2.10GLIBC_2.11GLIBC_2.12GLIBC_2.13GLIBC_2.14GLIBC_2.15GLIBC_2.16GLIBC_2.17GLIBC_PRIVATE

import tensorflow 模块可能出现的错误二:

Python 2.7.13 (default, Jul 14 2017, 16:05:15)[GCC 4.8.1] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import tensorflow as tfTraceback (most recent call last):  File "<stdin>", line 1, in <module>  File "/opt/python/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>    from tensorflow.python import *  File "/opt/python/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 72, in <module>    raise ImportError(msg)ImportError: Traceback (most recent call last):  File "/opt/python/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 61, in <module>    from tensorflow.python import pywrap_tensorflow  File "/opt/python/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>    _pywrap_tensorflow = swig_import_helper()  File "/opt/python/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.19' not found (required by /opt/python/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so)Failed to load the native TensorFlow runtime.See https://github.com/tensorflow/tensorflow/blob/master/tensorflow/g3doc/get_started/os_setup.md#import_error

这里指出gcc的版本太低导致的,至于如何升级gcc的版本,之前写过一个gcc的文档。gcc的离线安装

运行以下命令检查动态库:

[root@localhost common]# strings /usr/lib64/libstdc++.so.6 | grep GLIBCGLIBCXX_3.4GLIBCXX_3.4.1GLIBCXX_3.4.2GLIBCXX_3.4.3GLIBCXX_3.4.4GLIBCXX_3.4.5GLIBCXX_3.4.6GLIBCXX_3.4.7GLIBCXX_3.4.8GLIBCXX_3.4.9GLIBCXX_3.4.10GLIBCXX_3.4.11GLIBCXX_3.4.12GLIBCXX_3.4.13GLIBC_2.2.5GLIBC_2.3GLIBC_2.4GLIBC_2.3.2GLIBCXX_FORCE_NEWGLIBCXX_DEBUG_MESSAGE_LENGTH

解决了这么多问题,当我们在引入这个模块的时候,还是有错,这可能是TensorFlow依赖其他的包,没办法继续pip install。
tensorflow依赖的python库安装:

pip install ipython numpy scipy pandas jupyter gensim scikit-learn polyglot

这里最好一个一个去安装。

感觉应该差不多了,导入该模块,发现尼玛又出错了,找了半天,原来是编码问题。
如果报 undefined symbol: PyUnicodeUCS4_AsASIIString 错误,是因为python和某个你用的库编译时指定的UCS编码不对导致,编译python时,可以通过指定–enable-unicode[=ucs[24]]来选择使用UCS2或者UCS4,如下:

解决方法:

[root@localhost Python-2.7.13]# ./configure --enable-shared --enable-unicode=ucs4[root@localhost Python-2.7.13]# make[root@localhost Python-2.7.13]# make install

这里相当于重新编译了python,可能会与之前安装的有所冲突,如果冲突了,重新修改环境变零或者创建软链接。