操作系统:Ubuntu 14.04LTS
语言:Python Anaconda 2.7 (keras, theano)
GPU:GTX980Ti CUDA:CUDA 7.5
我想使用我的 GPU(GTX980Ti)在 IPython Notebook 上运行 keras python 代码,
但我找不到它。
我想测试下面的代码。当我在 Ubuntu 终端上运行它时,我的命令如下(它很好地使用了 GPU。它没有任何问题)
首先我设置如下路径
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
其次我运行代码如下
THEANO_FLAGS='floatX=float32,device=gpu0,nvcc.fastmath=True' python myscript.py
它运行良好。
但是当我在 pycharm(python IDE) 上运行代码或在 Ipython Notebook 上运行代码时,它不使用 gpu。它只使用CPU
myscript.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 %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')
为了解决它,我强制代码使用 gpu 如下 (在 myscript.py 上再插入两行)
import theano.sandbox.cuda
theano.sandbox.cuda.use("gpu0")
然后它会产生如下错误
ERROR (theano.sandbox.cuda): nvcc compiler not found on $PATH. Check your nvcc installation and try again.
怎么做???我花了两天时间..
而且我确实做了在主目录中使用“.theanorc”文件的方式。