基础:已经掌握深度学习的基本概念,需要配置GPU
主要深度学习框架Keras
:A minimalist, highly modular neural networks library, written in Python and capable of running on top of either TensorFlow or Theano.
一、Anaconda安装
anaconda集合了theano所需的基础环境。anaconda2用python2编译,能够支持pydot(theano所需要的画图的包),而anaconda3用python3编译,不能支持pydot。
二、安装theano、keras
(一)theano
方法一: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
1. cuda安装
(1)官网下载cuda sdb安装包
安装:
1. `sudo dpkg -i cuda-repo-ubuntu1404-7-5-local_7.5-18_amd64.deb`2. `sudo apt-get update`3. `sudo apt-get install cuda`
(
2)
永久环境变量(设置到 ~/.bashrc 不行,提示找不到nvcc):cd 到 /etc,用sudo gedit profile打开profile这个文件,在最后(也就是某些帖子说的“适当位置”)添加
export PATH=$PATH:/usr/local/cuda-7.5/binexport LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:/lib
(3)在用户目录下新建.theanorc配置文件,设置采用GPU替代CPU进行运算:
新建配置文件sudo vi ~/.theanorc
添加如下内容:
[global]floatX=float32device=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')
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
2). copy文件至CUDA安装目录:解压后,在你的目录下生成一个“cuda”文件夹。使用如下命令copy,注意第二个有个 -a 参数,否则,拷贝过去的文件失去了
链接。
# 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