Ubuntu16使用theano出错

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jeryjeryjery/article/details/79545514

  最近看了一篇paper,在复现论文代码时,需要用到theano这个机器学习工具(感觉我看了好多个工具,但却都只是了解,好悲伤!!!),然后再本地的Ubuntu16虚拟机里面装这个工具,用一下指令:

sudo pip3 install theano

因为之前我装过TensorFlow和Pytorch,所以一些依赖包基本已经包括了。我安装的theano版本是1.0.1,python是3.6。

  提示安装成功之后,开始跑代码,但是却出现一下错误:

You can find the C code in this temporary file: /tmp/theano_compilation_error_jj1i_fkk
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/theano/gof/lazylinker_c.py", line 75, in <module>
    raise ImportError()
ImportError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/theano/gof/lazylinker_c.py", line 92, in <module>
    raise ImportError()
ImportError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "filter_noise_embs.py", line 3, in <module>
    import theano
  File "/usr/local/lib/python3.6/site-packages/theano/__init__.py", line 110, in <module>
    from theano.compile import (
  File "/usr/local/lib/python3.6/site-packages/theano/compile/__init__.py", line 12, in <module>
    from theano.compile.mode import *
  File "/usr/local/lib/python3.6/site-packages/theano/compile/mode.py", line 11, in <module>
    import theano.gof.vm
  File "/usr/local/lib/python3.6/site-packages/theano/gof/vm.py", line 673, in <module>
    from . import lazylinker_c
  File "/usr/local/lib/python3.6/site-packages/theano/gof/lazylinker_c.py", line 127, in <module>
    preargs=args)
  File "/usr/local/lib/python3.6/site-packages/theano/gof/cmodule.py", line 2359, in compile_str
    (status, compile_stderr.replace('\n', '. ')))
Exception: Compilation failed (return status=1): /usr/bin/ld: 
/usr/local/lib/libpython3.6m.a(abstract.o): 
relocation R_X86_64_32S against `_Py_NotImplementedStruct' can not be used
when making a shared object; recompile with -fPIC. /usr/local/lib/libpython3.6m.a: 
无法添加符号: 错误的值. collect2: error: ld returned 1 exit status. 

这种提示错误一般是一层一层抛出,所以只需要看最里面那行错误:

Exception: Compilation failed (return status=1): /usr/bin/ld: 
/usr/local/lib/libpython3.6m.a(abstract.o): 
relocation R_X86_64_32S against `_Py_NotImplementedStruct' can not be used 
when making a shared object; recompile with -fPIC. 
/usr/local/lib/libpython3.6m.a: 无法添加符号: 错误的值. collect2: error: ld returned 
1 exit status. 

大致意思是编译出错,有个东西无法使用,需要用-fPIC模式进行编译。看完之后,查了一下资料,发现是之前编译python3的时候选项没有选正确,出现了无法支持当前的编译,需要对Python重新编译。

  查阅资料,发现应该按以下方式对Python3进行编译安装:

./configure --enable-shared CFLAGS=-fPIC
make
sudo make install

对于./configure后面的选项,可以通过./configure -help来进行了解。上面的编译带上了可以共享选项。

  安装完之后,试一试效果,发现连python3都会出错了(有点小崩溃),出现以下错误:

python3: error while loading shared libraries: libpython3.6m.so.1.0: cannot open shared 
object file: No such file or directory

说找不到libpython3.6m.so.1.0文件。

运行ldd /usr/local/bin/python3,看看python3运行所需要的库,结果显示如下:

linux-vdso.so.1 =>  (0x00007fff70da5000)
libpython3.6m.so.1.0 => not found
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f1496ff1000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1496c27000)
/lib64/ld-linux-x86-64.so.2 (0x00007f149720e000)

发现那个依赖文件的确没有找到.。

查找一下文件的位置:

find /-name 'libpython3.3m.so.1.0'

文件位置为:

/usr/local/lib/libpython3.6m.so.1.0

之后在目录/etc/ld.so.conf.d下,建立python3.conf文件

cd /etc
cd ld.so.conf.d
sudo touch python3.conf

然后编辑该文件,加入libpython3.6m.so.1.0文件所在的目录

sudo gedit python3.conf                   #编辑文件

/usr/local/lib/                           #加入文件所在的目录

最后运行:sudo ldconfig
再运行python3,问题解决.

再运行theano时,仅仅提示:

WARNING (theano.tensor.blas): Using NumPy C-API based implementation 
for BLAS functions.

并没有出现错误,问题解决!!!

猜你喜欢

转载自blog.csdn.net/jeryjeryjery/article/details/79545514