python - 使用 GPU 运行 Keras/Theano
问题描述
我正在尝试在 jupyter 笔记本上使用 GPU 运行 keras/theano,我的系统是带有 NVIDIA GeForce GT 330M 的 mac os High Sierra 10.13。
我按照该站点上的说明进行操作:我安装了 cuda 和 cudnn,然后编辑~/.bash_profile
. $ THEANO_FLAGS=mode=FAST_RUN python imdb_cnn.py
因为我使用的是 jupyter notebook,所以我不能使用这个命令。此外,我还编辑了 .theanorc文件,它看起来像这样:
[global]
floatX = float32
device = gpu
force_device = True
optimizer_including=cudnn
[nvcc]
fastmath = True
[cuda]
root=Users/jack/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/jack/miniconda3/lib/python3.6/site-packages/theano/gpuarray/__init__.py", line 227, in <module>
use(config.device)
File "/Users/jack/miniconda3/lib/python3.6/site-packages/theano/gpuarray/__init__.py", line 214, in use
init_dev(device, preallocate=preallocate)
File "/Users/jack/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
我找到了这个线程set DEVICE=cuda0
并在终端上运行它:set GPUARRAY_CUDA_VERSION=80
但我仍然得到同样的错误加上输出说我正在使用 cpu。
EDIT2:我重新安装了 Cuda(使用 .dmg 文件,而不是从终端),我遵循了Invidia 安装指南并应用了以下提示:
- 取消选中系统偏好设置 > 节能器 > 自动图形切换
- 在系统偏好设置>节能器中将计算机睡眠栏拖到从不
现在我得到这个新错误:Segmentation fault: 11
>>> import pygpu
>>> pygpu.test()
pygpu is installed in /Users/jack/miniconda3/lib/python3.6/site-packages/pygpu
NumPy version 1.15.4
NumPy relaxed strides checking option: True
NumPy is installed in /Users/jack/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
解决方案
推荐阅读
- firebase - 旅行计划者的 Firestore 数据建模
- c# - 为什么只在更改控件的可见属性时才抛出跨线程异常?
- php - 如何从 HTML 字符串中获取特殊字符列表
- python - 操作具有很长字符串的列表时出现运行时错误 (Python3)
- ruby-on-rails - 为什么 Rails Postgresql 不允许我启动我的 rails 应用程序?
- javascript - 如何在 Javascript 中添加自定义复制警报?
- three.js - 无法让 Three.js InstancedBufferGeometry 出现在场景中
- javascript - 如何在 HTML 中访问 Angular 的表单数组?
- javascript - 从 SVG 创建文本
的 - docker - Docker 和 Airflow - 初始化容器时出错