温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么解决Pytorch 自定义子Module .cuda() tensor失败问题

发布时间:2020-06-23 13:46:58 来源:亿速云 阅读:361 作者:清晨 栏目:开发技术

不懂怎么解决Pytorch 自定义子Module .cuda() tensor失败问题?其实想解决这个问题也不难,下面让小编带着大家一起学习怎么去解决,希望大家阅读完这篇文章后大所收获。

在刚从tensorflow转入pytorch,对于自定义的nn.Module 碰到了个问题,即使把模组 modle=Model().cuda(),里面的子Module的parameter都没有被放入cuda,导致输入是torch.cuda.FloatTensor,但是比如CNN的weight却还是torch.FloatTensor

(当然最粗暴的方法就是直接在子模组里面都用了 .cuda()

但是问题并不在那,可能是调用子模组的时候,存在某些错误,导致父模组没有把子模组的parameter注册到pytorch中。

而我遇到的错误就是,使用list来存放子模组的对象,导致list中的parameter没有注册。

解决方案就是 使用nn.ModuleList()这个封装的函数 来替换list() / []

参考这里

补充知识:关于Pytorch框架下报错CUDA驱动版本不满足CUDA运行版本——一种可能的原因及解决办法

运行Pytorch代码的时候遇到:

RuntimeError: cuda runtime error (35) : CUDA driver version is insufficient for CUDA runtime version at /pytorch/aten/src/THC/THCGeneral.cpp:74

可能原因:每一个pytorch版本都有对应的cuda版本,可能是在安装pytorch的时候,选择的pytorch版本所对应的版本cuda版本与本机所安装的cuda版本不相符。

怎么解决Pytorch 自定义子Module .cuda() tensor失败问题

check步骤:

#查看pytorch版本

import torch

torch.__version__

#查看pytorch版本对应的cuda版本

torch.version.cuda

#查看cuda是否可用

torch.cuda.is_available()

#查看Linux server安装的cuda版本

#切换到/usr/local/cuda/samples/1_Utilities/deviceQuery然后运行

./deviceQuery

出现类似以下信息:

./deviceQuery Starting...
 CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)

Device 0: "GeForce GTX TITAN Black"
 CUDA Driver Version / Runtime Version  8.0 / 8.0
 CUDA Capability Major/Minor version number: 3.5
 Total amount of global memory:   6080 MBytes (6375407616 bytes)
 (15) Multiprocessors, (192) CUDA Cores/MP: 2880 CUDA Cores
 GPU Max Clock rate:    1072 MHz (1.07 GHz)
 Memory Clock rate:    3500 Mhz
 Memory Bus Width:    384-bit
 L2 Cache Size:     1572864 bytes
 Maximum Texture Dimension Size (x,y,z)  1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
 Maximum Layered 1D Texture Size, (num) layers 1D=(16384), 2048 layers
 Maximum Layered 2D Texture Size, (num) layers 2D=(16384, 16384), 2048 layers
 Total amount of constant memory:  65536 bytes
 Total amount of shared memory per block: 49152 bytes
 Total number of registers available per block: 65536
 Warp size:     32
 Maximum number of threads per multiprocessor: 2048
 Maximum number of threads per block:  1024
 Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
 Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
 Maximum memory pitch:    2147483647 bytes
 Texture alignment:    512 bytes
 Concurrent copy and kernel execution:  Yes with 1 copy engine(s)
 Run time limit on kernels:   No
 Integrated GPU sharing Host Memory:  No
 Support host page-locked memory mapping: Yes
 Alignment requirement for Surfaces:  Yes
 Device has ECC support:   Disabled
 Device supports Unified Addressing (UVA): Yes
 Device PCI Domain ID / Bus ID / location ID: 0 / 1 / 0
 Compute Mode:
 < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GTX TITAN Black
Result = PASS

比对前后两个cuda版本是否一致,如果不一致,就需要卸载并安装与本机cuda版本相同的pytorch(当然应该也可以改本机的cuda版本,只不过相对比较麻烦)

pip3 uninstall pytorch
pip3 install [pytorch-version-link]

打开链接,选择合适版本版本,右键复制链接地址,替换上面的pytorch-version-link,执行命令就行。

这样应该就可以解决了。

感谢你能够认真阅读完这篇文章,希望小编分享怎么解决Pytorch 自定义子Module .cuda() tensor失败问题内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI