在Ubuntu 16.04上,如何将Theano与GPU结合使用?

在Ubuntu 16.04上,如何将Theano与GPU结合使用?,ubuntu,theano,theano-cuda,Ubuntu,Theano,Theano Cuda,我使用以下脚本测试GPU是否正常工作: #!/usr/bin/env python 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(2

我使用以下脚本测试GPU是否正常工作:

#!/usr/bin/env python
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')
当我运行它时,我得到:

有趣的部分在结尾:

ERROR (theano.sandbox.cuda): Failed to compile cuda_ndarray.cu: ('nvcc return status', 1, 'for cmd', 'nvcc -shared -O3 -m64 -Xcompiler -DCUDA_NDARRAY_CUH=c72d035fdf91890f3b36710688069b2e,-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION,-fPIC,-fvisibility=hidden -Xlinker -rpath,/home/moose/.theano/compiledir_Linux-4.4--generic-x86_64-with-Ubuntu-16.04-xenial-x86_64-2.7.11+-64/cuda_ndarray -I/home/moose/.local/lib/python2.7/site-packages/theano/sandbox/cuda -I/usr/lib/python2.7/dist-packages/numpy/core/include -I/usr/include/python2.7 -I/home/moose/.local/lib/python2.7/site-packages/theano/gof -o /home/moose/.theano/compiledir_Linux-4.4--generic-x86_64-with-Ubuntu-16.04-xenial-x86_64-2.7.11+-64/cuda_ndarray/cuda_ndarray.so mod.cu -L/usr/lib -lcublas -lpython2.7 -lcudart')
WARNING (theano.sandbox.cuda): CUDA is installed, but device gpu is not available  (error: cuda unavailable)
我的系统
  • 我使用Ubuntu 16.04
  • 我已经通过标准repos(
    V7.5.17
    )安装了CUDA<代码>nvcc--版本有效
  • 我已经通过pip安装了Theano
  • 我有CuDNN 4(与TensorFlow合作)
  • 我设置了
    CUDA\u ROOT=/usr/bin/
    LD\u LIBRARY\u PATH=/usr/lib/x86\u 64-linux-gnu/
    (我不确定这是否正确)
我的
~/。NORC

[global]
exception_verbosity=high
device=gpu
floatX=float32

[cuda]
root=/usr/bin/
路径 我认为标准repos的安装可能与手动安装有所不同。以下是一些可能会发现一些问题的路径:

/usr/bin/nvcc
/usr/lib/x86_64-linux-gnu/libcuda.so
/usr/lib/x86_64-linux-gnu/libcudart.so
/usr/lib/nvidia-cuda-toolkit
/usr/include/cudnn.h
问题:
我怎样才能让它工作呢?

我不确定是什么解决了这个问题,但有以下一个或两个问题()


我在这里写一个更一般的答案,以防其他人发现自己处于类似的情况。 首先,如前所述,确保安装了无依赖项。您应该按照说明安装nvidia驱动程序,使用
sudo ubuntu驱动程序设备
确定推荐的驱动程序,并使用
sudo-apt-get-install-nvidia-xxx
(此时xxx=375)安装它。然后,通过打开“附加驱动程序”窗口(从终端<代码>软件属性GTK-OpenTabab=4 ),查看英伟达驱动程序的使用。按如下方式设置~/.theanoc文本文件:

[global]
exception_verbosity=high
device=gpu
floatX=float32

[cuda]
root=/usr/bin/

[nvcc]
flags=-D_FORCE_INLINES

[lib]
cnmem = 1

[lib]部分不是必需的,但在我的笔记本电脑上,将其添加到.theanoc时,性能提高了约2倍

一种可能的方法是:阅读并仔细遵循它,包括其中包含的验证步骤,然后再转到“否”。@RobertCrovella是的,我知道。但我确实有一个可以工作的CUDA安装(我知道它可以工作,因为它使用TensorFlow)。我真的很想手动安装它,但可能无法让它同时适用于Theano和TensorFlow。好的,那只是Theano的安装问题。因为您列出的“验证”步骤(例如,
nvcc--version
)没有进行太多验证。这似乎没有提供问题的答案。一旦你有足够的钱,你就可以在任何岗位上工作。也检查一下这个。
[global]
exception_verbosity=high
device=gpu
floatX=float32

[cuda]
root=/usr/bin/

[nvcc]
flags=-D_FORCE_INLINES

[lib]
cnmem = 1