說明:系統是unbuntu14.04LTS,32位的操做系統,之前安裝了python3.4,如今想要安裝theano和keras。步驟以下:python
1,安裝pipjson
sudo apt-get install python3-setuptools sudo easy_install3 pip
2,安裝g++vim
sudo apt-get install g++
採用上述命令安裝g++,安裝完成後可用g++ -version查看是否安裝完成。注意,若是沒有安裝g++,在import theano時會出現如下錯誤:dom
WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.
搜了一下是由於theano採用g++編譯的話速度比較快,在網上找到的大部分解決方案都是基於Anaconda安裝的,解決方法是:測試
conda install mingw libpython
3,安裝theanothis
sudo pip3 install theano
該命令會自動下載theano所須要的依賴,包括numpy,scipy等等。spa
4,安裝keras操作系統
sudo pip3 install keras
最後須要注意的是,keras默認的backend是tensorflow,咱們須要的是theano,因此須要修改下設置。(並且tensorflow用pip3安裝,在32位系統上沒有對應的版本!用源文件安裝又很複雜)code
vim ~/.keras/keras.json { "image_dim_ordering":"tf", "epsilon":1e-07, "floatx":"float32", "backend":"theano" }
5,測試theanoorm
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(), ))