MXNet在GPU上的计算与各种错误处理

        安装GPU版本的MXNet,可能会遇到很多的错误需要处理,下面是本人遇到的一些问题,其实对于环境的搭建这块,大家要多练习,这样也是可以在错误处理中得到很多的收获。

环境搭建

如果安装的是CPU版本的,可以先卸载,再安装
pip uninstall mxnet

或者创建一个新的虚拟环境
conda create -n pygpu python=3.7
激活
activate pygpu

使用-i 镜像地址,下载快(命令行)
pip install --pre mxnet-cu101 -i https://pypi.douban.com/simple

在jupyter notebook中显示虚拟环境:
python -m ipykernel install --user --name=pygpu  --display-name pygpu

如果报错,缺少ipykernel这个模块,先进行安装即可
python.exe: No module named ipykernel
conda install ipykernel

版本环境,GPU计算

import sys
print(sys.version)
3.7.12 | packaged by conda-forge | (default, Oct 26 2021, 05:35:01) [MSC v.1916 64 bit (AMD64)]
import mxnet as mx
from mxnet import nd
from mxnet.gluon import nn

mx.test_utils.list_gpus()
range(0, 1)#本人只有一块GPU,如果有多块GPU,使用gpu(i)(i从0开始)指明第i块GPU

#平时没有指定的情况,NDArray都是存在内存上
x=nd.random.uniform(shape=(3,4))
x.context
cpu(0)

#指定设备
y=nd.random.uniform(shape=(3,4),ctx=mx.gpu())
y.context
gpu(0)

#设备之间也可以传输数据
z=x.copyto(mx.gpu())#把变量x复制到gpu(0)
z2=x.as_in_context(mx.gpu())

效果一样,copyto和as_in_context的区别是什么呢?
如果源变量和目标变量的context一致,那么as_in_context函数,使得目标变量和源变量共享源变量的内存或显存,而copyto函数总会为目标变量开辟新的内存或显存
y.as_in_context(mx.gpu()) is y    #True
y.copyto(mx.gpu()) is y    #False

        特别注意的是,所有输入数据要求都在内存里面或者都在同一块的显存上,这样的设计,主要考虑CPU和GPU之间的数据交互通常比较耗时,所以在MXNet都需确切地指明计算的输入数据都在内存或同一块显存里。

在Gluon的模型中,也是通过ctx参数来指定设备
net=nn.Sequential()
net.add(nn.Dense(1))
net.initialize(ctx=mx.gpu())
net(y)

'''
[[-4.9759010e-03]
 [ 7.2911382e-05]
 [-5.4539666e-02]]

'''
net[0].weight.data()

'''
[[-0.00873779 -0.02834515  0.05484822 -0.06206018]]

'''

可以看出所有计算都是在gpu(0)上进行。

错误处理

jupyter kernelspec list
Available kernels:
  myd2l      C:\Users\Tony\AppData\Roaming\jupyter\kernels\myd2l
  pygpu      C:\Users\Tony\AppData\Roaming\jupyter\kernels\pygpu
  python3    c:\users\tony\appdata\local\programs\python\python36\share\jupyter\kernels\python3

ImportError: DLL load failed: 找不到指定的模块。 

  File "D:\Anaconda3\envs\pygpu\lib\site-packages\zmq\backend\cython\__init__.py", line 6, in
    from . import (
The kernel appears to have died. It will restart automatically.
The kernel has died, and the automatic restart has failed. It is possible the kernel cannot be restarted. If you are not able to restart the kernel, you will still be able to save the notebook, but running code will no longer work until the notebook is reopened.
命令行的显示是
ImportError: DLL load failed: 找不到指定的模块。

可以看出也是模块缺失或者版本问题,重装pyzmq模块即可

pip uninstall pyzmq
pip install pyzmq 

OSError: [WinError 126] 找不到指定的模块

(pygpu) C:\Users\Tony>python
Python 3.7.12 | packaged by conda-forge | (default, Oct 26 2021, 05:35:01) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import mxnet
Traceback (most recent call last):
  File "", line 1, in 
  File "D:\Anaconda3\envs\pygpu\lib\site-packages\mxnet\__init__.py", line 24, in 
    from .context import Context, current_context, cpu, gpu, cpu_pinned
  File "D:\Anaconda3\envs\pygpu\lib\site-packages\mxnet\context.py", line 24, in 
    from .base import classproperty, with_metaclass, _MXClassPropertyMetaClass
  File "D:\Anaconda3\envs\pygpu\lib\site-packages\mxnet\base.py", line 213, in 
    _LIB = _load_lib()
  File "D:\Anaconda3\envs\pygpu\lib\site-packages\mxnet\base.py", line 204, in _load_lib
    lib = ctypes.CDLL(lib_path[0], ctypes.RTLD_LOCAL)
  File "D:\Anaconda3\envs\pygpu\lib\ctypes\__init__.py", line 364, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: [WinError 126] 找不到指定的模块。

版本问题,本人换成了mxnet-cu101可以了,低版本的就报错,匹配11.3的又没有,于是退而求其次的版本

import mxnet as mx
mx.__version__
OSError                                   Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_11904\2780807244.py in 
----> 1 import mxnet as mx
      2 mx.__version__

D:\Anaconda3\envs\pygpu\lib\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
OSError: [WinError 126] 找不到指定的模块。

        这个错误是jupyter notebook中出现,在命令行中是正常显示,很大可能是python版本的问题,因为本人的conda的python是3.6,而平时安装的都是3.7的版本。有两种解决方案保持一致,重新安装的Anaconda版本,或者新建虚拟环境指定3.6版本,保持一致!留给后期本人有时间再确认下。

IPKernelApp] ERROR 

[IPKernelApp] ERROR | No such comm target registered: jupyter.widget.version

conda install ipywidgets
conda install widgetsnbextension
jupyter nbextension enable --py widgetsnbextension

你可能感兴趣的:(深度学习框架(MXNet),mxnet,ctx=mx.gpu,as_in_context)