深度学习环境搭建 - keras+GPU

一、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:

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

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

  • (二) pydot 和graphviz

  • 1.在mac 下必须用这个命令安装pydot 和graphviz,否则即使pip install pydot 或 conda install pydot 安装完后,还会报错找不到pydot。
    在centos下估计也可以。

conda install --channel https://conda.anaconda.org/RMG graphviz
conda install --channel https://conda.anaconda.org/RMG pydot
  • 2.在ubuntu 下:
    1中的命令执行完毕,然后再执行以下命令,就可以了
sudo apt-get install python-pydot
  • (三)keras
pip install keras

三、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 ,则证明一切正常。
四、鸣谢
特别感谢龙哥的无私奉献!

你可能感兴趣的:(深度学习环境搭建 - keras+GPU)