ubuntu 安装 theano

参考博客: http://www.cnblogs.com/anyview/p/5025704.html html

1. 安装gfortran, numpy, scipy, sklearn, blas, atlas等包python

# 安装gfortran,后面编译过程当中会用到
sudo apt-get install gfortran
# 安装blas,Ubuntu下对应的是libopenblas,其它操做系统可能须要安装其它版本的blas——这是个OS相关的。
sudo apt-get install libopenblas-dev
# 安装lapack,Ubuntu下对应的是liblapack-dev,和OS相关。
sudo apt-get install liblapack-dev
# 安装atlas,Ubuntu下对应的是libatlas-base-dev,和OS相关。
sudo apt-get install libatlas-base-dev
# 安装pip
sudo apt-get install python-pip
sudo apt-get install python-dev
sudo apt-get install python-nose
sudo apt-get install g++
sudo apt-get install gitgit

 

2. 安装numpy和scipydom

  • 安装这两个python库有点问题,若是使用apt-get安装,后面的test不能经过。若是使用pip安装,有得考虑各类依赖关系。oop

  • 因此,先使用apt-get安装,而后再卸载,最后再使用pip安装。这样,既能不考虑依赖关系,又能经过后面的test()测试。测试

#安装numpy和scipy
sudo apt-get install python-numpy
sudo apt-get install python-scipy
sudo apt-get install python-sklearn
#卸载numpy和scipy
sudo apt-get remove python-numpy
sudo apt-get remove python-scipy
# 安装numpy
sudo pip install numpy
# 测试numpy#,若是没有安装python-nose,测试会出错!python -c "
import numpy
numpy.test()
# 安装scipy
sudo pip install scipy
# 测试scipypython -c "
import scipy
scipy.test()"spa

 

3. 安装Theano操作系统

  • 前面的操做若是没有出现错误,就能够开始安装Theano了。命令以下所示。.net

# 安装Theano
sudo pip install Theano
# 测试Theano
import theano;
theano.test()

4.安装pyCUDA
  • 测试Theano时,提示PyCUDA import错误,所以须要安装pyCUDA。而PyCUDA须要以Boost为基础,因此应该先安装Boost。
  • 使用pip安装pyCUDA。code

安装boost
sudo apt-get install libboost-all-dev

若是使用pip安装pyCUDA出错,使用下面安装方式。参考文章:《Ubuntu Theano CUDA》

 sudo ldconfig /usr/local/cuda-7.0/lib64

 

5. 解决cuda_ndarray.cu错误

  • 若是出现错误:ERROR (theano.sandbox.cuda): Failed to compile cuda_ndarray.cu: libcublas.so.6.5 cannot open shared object file: No such file or directory,须要运行如下命令:

    sudo ldconfig /usr/local/cuda-7.0/lib64

6.配置GUP

个人电脑帐号名字yf

在主目录下面/home/yf/新建 .theanorc目录,写人以下内容:

[global] 
device=gpu
floatX=float32 
root=/usr/local/cuda-7.5
[nvcc] 
fastmath=True 
[blas]
ldflags=-lopenblas
[cuda]
root=/usr/local/cuda-7.5

 

7.测试Theano是否在使用GPU

  • 将下列python代码复制到useGPU.py,并运行。

  • from theano import function, config, shared, sandbox
    import theano.tensor as T
    import numpy
    import time
     
    vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
    iters = 1000
     
    rng = numpy.random.RandomState(22)
    x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
    f = function([], T.exp(x))
    print(f.maker.fgraph.toposort())
    t0 = time.time()
    for i in xrange(iters):
        r = f()
    t1 = time.time()
    print("Looping %d times took %f seconds" % (iters, t1 - t0))
    print("Result is %s" % (r,))
    if numpy.any([isinstance(x.op, T.Elemwise) for x in f.maker.fgraph.toposort()]):
        print('Used the cpu')
    else:
        print('Used the gpu')
  •  

    假定上面已经设置文件.theanorc,运行命令以下所示:.theanorc
  • python useGPU.py
  • 若是出现下面的错误信息,请运行命令sudo ldconfig /usr/local/cuda-7.0/lib64参考 

    #错误信息ERROR (theano.sandbox.cuda): Failed to compile cuda_ndarray.cu: libcublas.so.7.0: cannot open shared object file: No such file or directory

 

而后把调用GPU的测试程序copy一下,在http://deeplearning.net/software/theano/tutorial/using_gpu.html#testing-theano-with-gpu 这里。

终端显示GPU信息表示配置成功!!!