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信息表示配置成功!!!

相關文章
相關標籤/搜索