CentOS 7 搭建Tensorflow环境

CentOS 7 搭建Tensorflow环境_第1张图片

安装

这是GPU版本的r0.12版本安装,其他需求请参考官方指导 Pip installation安装

export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc1-cp27-none-linux_x86_64.whl
sudo pip install --upgrade $TF_BINARY_URL

验证

[zhangxinming@localhost Python-2.7.13]$ ipython
/usr/local/lib/python2.7/site-packages/IPython/core/history.py:228: UserWarning: IPython History requires SQLite, your history will not be saved
  warn("IPython History requires SQLite, your history will not be saved")
Python 2.7.13 (default, May  8 2017, 04:15:39) 
Type "copyright", "credits" or "license" for more information.

IPython 5.4.0.dev -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import tensorflow
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.8.0 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally
I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally

In [2]: 

错误及解决方法

ImportError: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 49, in 
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in 
    _pywrap_tensorflow = swig_import_helper()
  File "/usr/local/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/local/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so: undefined symbol: PyUnicodeUCS4_AsUTF8String

这个是在python编译时没有指定unicode编码防止导致的,参见How to find out if Python is compiled with UCS-2 or UCS-4?
测试编码范围

  • 当你使用 --enable-unicode=ucs4编译安装的python时:
>>> import sys
>>> print sys.maxunicode
1114111
  • 当你使用 --enable-unicode=ucs2编译安装的python时【这是默认选项】
>>> import sys
>>> print sys.maxunicode
65535

如果你的结果是65535,那么重新编译升级pyhton吧,注意在configure执行时添加--enable-unicode=ucs4选项
详细操作移步本人博客 CentOS 7.3 升级Python 2.7.13

操作完毕后再次导入,出现错误

ImportError: 
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.

这是由于之前的旧版本python安装的numpy与新编译的python不兼容的缘故,卸载重新安装即可

pip uninstall numpy
pip install numpy

之后在试试,出现

ImportError: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 61, in 
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in 
    _pywrap_tensorflow = swig_import_helper()
  File "/usr/local/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: libcudart.so.8.0: cannot open shared object file: No such file or directory

虽然你可能安装了,但是可能LD_LIBRARY_PATH没有配置好,执行以下步骤即可

  • 修改环境变量
vi /etc/profile
#添加如下内容
#Base LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/lib:/lib64:/usr/lib:/usr/lib64:/usr/local/lib:/usr/local/lib64
#CUDA
export PATH=/usr/local/cuda-8.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH
  • 更新运行库缓存
source /etc/profile
sudo ldconfig

此时TensorFlow应该没有问题了

你可能感兴趣的:(CentOS 7 搭建Tensorflow环境)