使用GPU运行Keras / Theano

时间:2018-12-13 22:12:14

标签: python keras gpu theano

我正在尝试在jupyter笔记本电脑上使用GPU运行keras / theano,我的系统是Mac OS High Sierra 10.13和NVIDIA GeForce GT 330M。

我遵循说明on this site:我安装了cuda和cudnn,然后编辑~/.bash_profile。由于我使用的是jupyter笔记本,因此无法使用此命令$ THEANO_FLAGS=mode=FAST_RUN python imdb_cnn.py。 此外,我还编辑了 .theanorc 文件,它看起来像这样:

[global]
floatX = float32
device = gpu
force_device = True
optimizer_including=cudnn

[nvcc]
fastmath = True

[cuda]
root=Users/secarbone/cuda 

然后,我尝试运行此代码以检查是否正在使用Gpu:

from theano import function, config, shared, tensor
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([], tensor.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(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, tensor.Elemwise) and
              ('Gpu' not in type(x.op).__name__)
              for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

...而我改用cpu!

要使用GPU,我应该在这段代码的顶部放置什么? 我也尝试过这样:

import os
os.environ['THEANO_FLAGS'] = "device=cuda,force_device=True,floatX=float32"

但是它不起作用,因为我总是得到以下输出:

[Elemwise{exp,no_inplace}(<TensorType(float32, vector)>)] Looping 1000
times took 2.618842 seconds Result is [1.2317803 1.6187934 1.5227807
... 2.2077181 2.2996776 1.6232328] 
Used the cpu

编辑:我尝试使用以下命令在终端上运行之前的“测试”代码       THEANO_FLAGS=mode=FAST_RUN,device=cuda0,floatX=float32 python gpuocpu.py

我收到此错误:

ERROR (theano.gpuarray): Could not initialize pygpu, support disabled
Traceback (most recent call last):
  File "/Users/secarbone/miniconda3/lib/python3.6/site-packages/theano/gpuarray/__init__.py", line 227, in <module>
    use(config.device)
  File "/Users/secarbone/miniconda3/lib/python3.6/site-packages/theano/gpuarray/__init__.py", line 214, in use
    init_dev(device, preallocate=preallocate)
  File "/Users/secarbone/miniconda3/lib/python3.6/site-packages/theano/gpuarray/__init__.py", line 99, in init_dev
    **args)
  File "pygpu/gpuarray.pyx", line 658, in pygpu.gpuarray.init
  File "pygpu/gpuarray.pyx", line 587, in pygpu.gpuarray.pygpu_init

我找到了this thread,并在终端set DEVICE=cuda0set GPUARRAY_CUDA_VERSION=80上运行了该命令,但仍然收到相同的错误以及输出信息,表明我正在使用cpu。


EDIT2 :我重新安装了Cuda(使用.dmg文件,而不是来自终端),我遵循Invidia Installation Guide并应用了以下提示:

  
      
  1. 取消选中系统偏好设置>节能>自动图形开关
  2.   
  3. 在“系统偏好设置”>“节能器”中将计算机睡眠栏拖动到“从不”
  4.   

现在我得到了这个新错误:Segmentation fault: 11

>>> import pygpu
>>> pygpu.test()
pygpu is installed in /Users/secarbone/miniconda3/lib/python3.6/site-packages/pygpu
NumPy version 1.15.4
NumPy relaxed strides checking option: True
NumPy is installed in /Users/secarbone/miniconda3/lib/python3.6/site-packages/numpy
Python version 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 14:01:38) [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
nose version 1.3.7
Segmentation fault: 11

0 个答案:

没有答案