Pytorch 解決自定義子Module .cuda() tensor失敗的問題
最近在剛從tensorflow轉(zhuǎn)入pytorch,對于自定義的nn.Module 碰到了個問題,即使把模組 modle=Model().cuda(),里面的子Module的parameter都沒有被放入cuda,導(dǎo)致輸入是torch.cuda.FloatTensor,但是比如CNN的weight卻還是torch.FloatTensor
(當(dāng)然最粗暴的方法就是直接在子模組里面都用了 .cuda()
但是問題并不在那,可能是調(diào)用子模組的時候,存在某些錯誤,導(dǎo)致父模組沒有把子模組的parameter注冊到pytorch中。
而我遇到的錯誤就是,使用list來存放子模組的對象,導(dǎo)致list中的parameter沒有注冊。
解決方案就是 使用nn.ModuleList()這個封裝的函數(shù) 來替換list() / []
補(bǔ)充知識:關(guān)于Pytorch框架下報錯CUDA驅(qū)動版本不滿足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版本都有對應(yīng)的cuda版本,可能是在安裝pytorch的時候,選擇的pytorch版本所對應(yīng)的版本cuda版本與本機(jī)所安裝的cuda版本不相符。
check步驟:
#查看pytorch版本
import torch
torch.__version__
#查看pytorch版本對應(yīng)的cuda版本
torch.version.cuda
#查看cuda是否可用
torch.cuda.is_available()
#查看Linux server安裝的cuda版本
#切換到/usr/local/cuda/samples/1_Utilities/deviceQuery然后運行
./deviceQuery
出現(xiàn)類似以下信息:
./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版本是否一致,如果不一致,就需要卸載并安裝與本機(jī)cuda版本相同的pytorch(當(dāng)然應(yīng)該也可以改本機(jī)的cuda版本,只不過相對比較麻煩)
pip3 uninstall pytorch
pip3 install [pytorch-version-link]
打開鏈接,選擇合適版本版本,右鍵復(fù)制鏈接地址,替換上面的pytorch-version-link,執(zhí)行命令就行。
這樣應(yīng)該就可以解決了。
以上這篇Pytorch 解決自定義子Module .cuda() tensor失敗的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python獲取android設(shè)備的GPS信息腳本分享
這篇文章主要介紹了python獲取android設(shè)備的GPS信息腳本分享,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-03-03pygame學(xué)習(xí)筆記之設(shè)置字體及顯示中文
游戲界面中文字也是非常常見的元素之一,pygame專門提供了Font模塊來支持文字的顯示,下面這篇文章主要給大家介紹了關(guān)于pygame學(xué)習(xí)筆記之設(shè)置字體及顯示中文的相關(guān)資料,需要的朋友可以參考下2022-07-07Python + OpenCV 實現(xiàn)LBP特征提取的示例代碼
這篇文章主要介紹了Python + OpenCV 實現(xiàn)LBP特征提取的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Python Web框架Flask下網(wǎng)站開發(fā)入門實例
這篇文章主要介紹了Python Web框架Flask下網(wǎng)站開發(fā)入門實例,本文實現(xiàn)了一個注冊頁面、登錄頁面和上傳頁面,需要的朋友可以參考下2015-02-02