版权声明:本文为King_HAW原创文章,未经King_HAW允许不得转载。 https://blog.csdn.net/King_HAW/article/details/78411469
系统 Ubuntu14.04.4 LTS x64
GPU NVIDIA GeForce GTX 1060 6GB
TensorFlow GPU版本首先需要安装NVIDIA显卡驱动,并且需要CUDA以及cuDNN支持,这里采用的显卡驱动版本为375.39,CUDA版本为8.0,cuDNN版本为5.1。具体安装过程请见深度学习平台Caffe环境搭建【GPU版】
安装Theano的过程和安装Tensorflow大致差不多,为了避免产生冲突,这里选择使用virtualenv安装。
创建一个名为theano的virtualenv环境
virtualenv --system-site-packages ~/theano
激活virtualenv环境
source ~/theano/bin/activate
此时在命令行的最前面会出现(theano),表示环境激活成功
接下来安装Theano
sudo pip install Theano
安装完成之后,执行deactivate关闭环境
为了以后激活Theano环境更加简单,执行以下命令将theano激活命令写入bash
sudo printf '\nalias theano="source ~/theano/bin/activate"' >>~/.bashrc
刷新bash之后,键入theano即可激活theano环境
激活theano之后,开始测试
python -c "import theano;theano.test()"
可能出现的问题:
ImportError:No module named nose-parameterized
解决,执行命令
sudo pip install nose_parameterized
此时Theano安装完成,但是如果直接使用的话,Theano默认调用CPU,需要配置一下.theanorc文件
在主目录下新建.theanorc文件
cd
sudo gedit .theanorc
将以下内容写入.theanorc文件
[global]
floatX=float32
device=gpu
optimizer=fast_compile
optimizer_including=cudnn
[blas]
ldflas=-lopenblas
[nvcc]
fastmath=True
flags=-D_FORCE_INLINES
[cuda]
root=/usr/local/cuda-8.0/
CUDA默认安装在/usr/local/cuda-8.0位置,如果你的cuda不是安装在该位置,需要将root指定为你的安装目录
执行测试文件
将下列代码保存为test_theano.py文件
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' % iters, t1 - t0, 'seconds'
print 'Result is', 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'
执行test_theano.py文件
python test.py
如果以上安装步骤没有错误的话,会得到以下结果,执行完毕所需时间大约在1s左右
至此Theano GPU版安装完毕