7

操作系统: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”文件的方式。

4

1 回答 1

3

我在使用我系统 GPU 的 ipython 笔记本上使用 theano。此配置似乎在我的系统上运行良好。(带有 GTX 750M 的 Macbook Pro)

我的 ~/.theanorc 文件:

[global]
cnmem = True
floatX = float32
device = gpu0

各种环境变量(我使用虚拟环境(macvnev):

echo $LD_LIBRARY_PATH
/opt/local/lib:

echo $PATH
/Developer/NVIDIA/CUDA-7.5/bin:/opt/local/bin:/opt/local/sbin:/Developer/NVIDIA/CUDA-7.0/bin:/Users/Ramana/projects/macvnev/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

echo $DYLD_LIBRARY_PATH
/Developer/NVIDIA/CUDA-7.5/lib:/Developer/NVIDIA/CUDA-7.0/lib:

我如何运行 ipython notebook(对我来说,设备是 gpu0):

$THEANO_FLAGS=mode=FAST_RUN,device=gpu0,floatX=float32 ipython notebook

输出$nvcc -V

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2015 NVIDIA Corporation
Built on Thu_Sep_24_00:26:39_CDT_2015
Cuda compilation tools, release 7.5, V7.5.19

从您的帖子中,您可能设置了错误的 $PATH 变量。

于 2016-02-10T12:19:33.220 回答