CentOS 7 搭建Tensorflow環(huán)境

安裝

這是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 <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _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執(zhí)行時添加--enable-unicode=ucs4選項
詳細操作移步本人博客 CentOS 7.3 升級Python 2.7.13

操作完畢后再次導入,出現(xiàn)錯誤

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

之后在試試,出現(xiàn)

ImportError: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 61, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
    _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沒有配置好,執(zhí)行以下步驟即可

  • 修改環(huán)境變量
vi /etc/profile
#添加如下內(nèi)容
#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應該沒有問題了

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容