在gluon环境中,try_gpu()还是会使用cpu,怎么办

学习5.5卷积神经网络章节的时候遇到的问题。
在try_gpu()这里会选择cpu运行。用 nvidia-smi -L找到服务器上有GPU 0这样一个设备。

首先尝试了在python代码中指定GPU:
import os
os.environ[“CUDA_VISIBLE_DEVICES”]=“0”
没有改变。
尝试直接ctx=mx.gpu(0),只输出ctx可以指向GPU 0这个设备,但在后面运行的时候报错了,出现“MXNetError: [10:30:24] src/ndarray/ndarray.cc:1285: GPU is not enabled”。一头雾水不知道为什么。

然后看到论坛里的一个帖子,在gluon环境中,运行了:
source activate gluon
pip install -U --pre mxnet-cu80
这次是连mxnet和d2lzh都不能再用了…

在这里贴一下我的报错哈。

代码是:
import d2lzh as d2l
import mxnet_cu80 as mx
from mxnet_cu80 import autograd,gluon,init,nd
from mxnet_cu80.gluon import loss as gloss,nn
import time

net=nn.Sequential()
net.add(nn.Conv2D(channels=6,kernel_size=5,activation=‘sigmoid’),
nn.MaxPool2D(pool_size=2,strides=2),
nn.Conv2D(channels=16,kernel_size=5,activation=‘sigmoid’),
nn.MaxPool2D(pool_size=2,strides=2),
nn.Dense(120,activation=‘sigmoid’),
nn.Dense(84,activation=‘sigmoid’),
nn.Dense(10))

报错是:
OSError Traceback (most recent call last)
in
----> 1 import d2lzh as d2l
2 import mxnet_cu80 as mx
3 from mxnet_cu80 import autograd,gluon,init,nd
4 from mxnet_cu80.gluon import loss as gloss,nn
5 import time

~/selfstudy/d2l-zh/d2lzh/init.py in
----> 1 from .utils import *
2
3 version = ‘1.0.0’

~/selfstudy/d2l-zh/d2lzh/utils.py in
10 from IPython import display
11 from matplotlib import pyplot as plt
—> 12 import mxnet as mx
13 from mxnet import autograd, gluon, image, init, nd
14 from mxnet.contrib import text

~/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/init.py in
22 from future import absolute_import
23
—> 24 from .context import Context, current_context, cpu, gpu, cpu_pinned
25 from . import engine
26 from .base import MXNetError

~/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/context.py in
22 import warnings
23 import ctypes
—> 24 from .base import classproperty, with_metaclass, _MXClassPropertyMetaClass
25 from .base import _LIB
26 from .base import check_call

~/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/base.py in
211 version = libinfo.version
212 # library instance of mxnet
–> 213 _LIB = _load_lib()
214
215 # type definitions

~/anaconda3/envs/gluon/lib/python3.6/site-packages/mxnet/base.py in _load_lib()
202 “”“Load library by searching possible path.”""
203 lib_path = libinfo.find_lib_path()
–> 204 lib = ctypes.CDLL(lib_path[0], ctypes.RTLD_LOCAL)
205 # DMatrix functions
206 lib.MXGetLastError.restype = ctypes.c_char_p

~/anaconda3/envs/gluon/lib/python3.6/ctypes/init.py in init(self, name, mode, handle, use_errno, use_last_error)
346
347 if handle is None:
–> 348 self._handle = _dlopen(self._name, mode)
349 else:
350 self._handle = handle

OSError: libcudart.so.8.0: cannot open shared object file: No such file or directory

友友们,我仔细看了看报错,发现在和d2lzh相关的时候它调用到了mxnet这个包。我conda list看了下自己的环境,现在把原先的mxnet删掉了。原先的报错也解决了。 :grinning:
但是出现了新的意外…

具体就是:’autograd‘不能被导入了。
ImportError Traceback (most recent call last)
in
----> 1 import d2lzh as d2l
2 import mxnet_cu80 as mx
3 from mxnet_cu80 import autograd,gluon,init,nd
4 from mxnet_cu80.gluon import loss as gloss,nn
5 import time

~/selfstudy/d2l-zh/d2lzh/init.py in
----> 1 from .utils import *
2
3 version = ‘1.0.0’

~/selfstudy/d2l-zh/d2lzh/utils.py in
11 from matplotlib import pyplot as plt
12 import mxnet as mx
—> 13 from mxnet import autograd, gluon, image, init, nd
14 from mxnet.contrib import text
15 from mxnet.gluon import data as gdata, loss as gloss, nn, utils as gutils

ImportError: cannot import name ‘autograd’

大家好,这个问题我自己搞定了。昨天走了很多弯路,晚上睡一觉醒来重新搞。实现成功的方法就是:卸载cpu运行的mxnet,安装gpu运行的mxnet()也就是how-to-use里面的安装gpu的方法。
关键在于本机cuda和mxnet-gpu之间的匹配。

1赞