基础:已经掌握深度学习的基本概念,需要配置GPU



主要深度学习框架Keras :A minimalist, highly modular neural networks library, written in Python and capable of running on top of either TensorFlow or Theano.

Documents:   http://keras.io/ , 

Packages:   https://github.com/fchollet/keras





一、Anaconda安装



anaconda集合了theano所需的基础环境。anaconda2用python2编译,能够支持pydot(theano所需要的画图的包),而anaconda3用python3编译,不能支持pydot。




二、安装theano、keras



(一)theano


Github Page:   https://github.com/Theano/Theano

Documentation:   http://deeplearning.net/software/theano/

Tutorial:   http://deeplearning.net/tutorial/contents.html

方法一:After installing Anaconda, in a terminal execute this command to install the latest Theano release:



$ pip install Theano







方法二:If you want the bleeding edge version instead execute this command(先安装git再安装Theano):



$ pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
 
  
$ python setup.py install 
 
  



$ python setup.py develop







(二)Keras

方法一:




$ pip install keras



方法二:

$ git clone  
   https://github.com/fchollet/keras.git 
  
 
  
$ python setup.py install



检验:在python中输入import theano ,import keras 如果什么也不显示,那就对了。如果import theano报错,尝试安装bleeding edge version(最新但不是稳定版本的)。



三、GPU运算

1. cuda安装



(1)官网下载cuda sdb安装包



安装:



`sudo dpkg -i cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb`
 
  
`sudo apt-get update`
 
  
`sudo apt-get install cuda`






( 2) 永久环境变量(设置到 ~/.bashrc 不行,提示找不到nvcc):cd 到 /etc,用sudo gedit profile打开profile这个文件,在最后(也就是某些帖子说的“适当位置”)添加



export PATH=$PATH:/usr/local/cuda-7.5/bin
 
  
export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:/lib






(3)在用户目录下新建.theanorc配置文件,设置采用GPU替代CPU进行运算:



新建配置文件sudo vi ~/.theanorc



添加如下内容:



[global]

 
  

floatX=float32

 
  

device=gpu

 
  

[nvcc]

 
  


fastmath = True








(4)测试代码:



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')




如果输出显示 used the gpu ,则证明一切正常。






2.  cudnn 安装

cudnn 是nvdia开发的为gpu计算加速的组件,安装简单。 



注意:直接pip安装的theano 是0.7版本,而最新cudnn 为7.5版本,cudnn还是用7.0的先,否则后面会有warning提示cudnn版本高于theano) 


    1).    解压 cudnn: tar -xzvf cudnn-7.0



链接。 


    # copy the library files into CUDA's include and lib folders 


    sudo cp cuda/include/cudnn.h /usr/local/cuda/include 


    sudo cp -a cuda/lib64/libcudnn* /usr/local/cuda/lib64 







测试: python 进入后,输入import theano ,如果显示using gpu decice 0 : GeForce GTX 745 (CNMem is disabled ,CuDnn 4007)则一切正常。