天天看點

win7下Anaconda 的theano安裝記錄

    鹵煮已經在windows平台安裝了niuzhiheng&&happynear的caffe,用起來甚爽,真心比Linux直覺,誰叫咱們用了這麼久的windows呢。當然ubuntu用起來也是很爽的……跑題了又……

    打算重新過一遍深度學習的代碼,熟悉下算法so ,不得不提的http://deeplearning.net/tutorial/确實是一個比較好的撸代碼的選擇,于是乎我需要重新安裝一下這萬人景仰的theano,由于已經安裝過了Anaconda這個python包,當然是2.7的,so,針對官方的安裝辦法需要按照自己的情況進行選擇。于是跑到http://deeplearning.net/software/theano/撸一下user guide。發現什麼cuda,numpy各種雲雲,已經安裝好了……于是我的安裝步驟分為以下:

1 更新Anaconda

跳過,發現一句cmd下的指令

conda install mingw libpython      

于是執行,發現它幫我安裝了mingw,并且更新了python的一衆工具包。

2 下載下傳安裝并配置 theano 0.7

    1〉下載下傳見https://pypi.python.org/pypi/Theano

     2〉進入下載下傳後解壓的目錄 然後執行 python setup.py install

     3> 之後同樣在該目錄下

python setup.py develop      

至此,強大的theano已經安裝完畢,可以打開Anaconda的 spyder編輯器粘貼下面代碼

import numpy as np
import time
import theano
A = np.random.rand(1000,10000).astype(theano.config.floatX)
B = np.random.rand(10000,1000).astype(theano.config.floatX)
np_start = time.time()
AB = A.dot(B)
np_end = time.time()
X,Y = theano.tensor.matrices('XY')
mf = theano.function([X,Y],X.dot(Y))
t_start = time.time()
tAB = mf(A,B)
t_end = time.time()
print("NP time: %f[s], theano time: %f[s] (times should be close when run on CPU!)" %(
                                           np_end-np_start, t_end-t_start))
print("Result difference: %f" % (np.abs(AB-tAB).max(), ))      

正常情況下 console會輸出

NP time: 0.129000[s], theano time: 0.092000[s] (times should be close when run on CPU!)

Result difference: 0.000000

3 安裝cuda支援并配置theano的gpu mode

看到支援GPU,忍不住裝下,cuda已經裝好,不在複述。

這裡主要講下 如何配置theano

閑話不說,直接在你登陸使用者的目錄下建立檔案

.theanorc.txt注意這裡不需要檔案名

粘貼以下内容到建立的檔案下,注意裡邊不需要空格。 

[global]

openmp=False

device=gpu

floatX=float32

allow_input_downcast=True

[blas]

ldflags =

[gcc]

cxxflags=-IE:\Anaconda2\pkgs\mingw-4.7-1\MinGW\x86_64-w64-mingw32\include  #你的 MinG目錄

[nvcc]

flags=-LE:\Anaconda2\libs #你的python目錄

compiler_bindir=E:\vstudio12\VC\bin#你的vs.net 的vc目錄

fastmath=True

flags=-arch=sm_50#按照gpu的能力來設定nvcc裡邊可以測到自己的gpu支援什麼

搞定了之後,測試,粘貼如下代碼

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 range(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')	      
配置正常情況下會輸出:      
Looping 1000 times took 1.221000 seconds
Result is [ 1.23178029  1.61879349  1.52278066 ...,  2.20771813  2.29967761
  1.62323296]
Used the gpu 至此,你的theano已經安裝完畢,可以開始你的deep learning的旅程了!
      

繼續閱讀