網(wǎng)站首頁 編程語言 正文
基于Pytorch使用GPU運(yùn)行模型方法及注意事項(xiàng)
一、在基于pytorch深度學(xué)習(xí)進(jìn)行模型訓(xùn)練和預(yù)測(cè)的時(shí)候,往往數(shù)據(jù)集比較大,而且模型也可能比較復(fù)雜,但如果直接訓(xùn)練調(diào)用CPU運(yùn)行的話,計(jì)算運(yùn)行速度很慢,因此使用GPU進(jìn)行模型訓(xùn)練和預(yù)測(cè)是非常有必要的,可以大大提高實(shí)驗(yàn)效率。如果還沒有配置好運(yùn)行環(huán)境的博友們可以參考下面博主的文章。
1、點(diǎn)擊打開《基于Windows中學(xué)習(xí)Deep Learning之搭建Anaconda+Cudnn+Cuda+Pytorch+Pycharm工具和配置環(huán)境完整最簡(jiǎn)版》文章
2、點(diǎn)擊打開《基于Pytorch查看本地或者遠(yuǎn)程服務(wù)器GPU及使用方法》文章
二、具體方法分為兩個(gè)大部分(模型和數(shù)據(jù)集)。
- 首先將模型model移動(dòng)到cuda設(shè)備也就是GPU上,注意:此大模型可以內(nèi)含多個(gè)子模型,子模型無需再重復(fù)移動(dòng)到GPU上
model = Net() # 舉例模型
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device) # 移動(dòng)舉例模型到cuda
或者
model = Net() # 舉例模型
device = torch.cuda.current_device() if args.cuda else torch.device('cpu')
model.to(device) # 移動(dòng)舉例模型到cuda
- 將數(shù)據(jù)集(包含訓(xùn)練集和測(cè)試集及所包含的標(biāo)簽數(shù)據(jù)集)移動(dòng)到cuda設(shè)備也就是GPU上,使用數(shù)據(jù)集.cuda()形式完成。
drug_embeddings = drug_embeddings.cuda()
protein_embeddings = protein_embeddings.cuda()
effectives = effectives.cuda()
或者
drug_embeddings = drug_embeddings.to(device)
protein_embeddings = protein_embeddings.to(device)
effectives = effectives.to(device)
三、問題及方法
-
問題 1:torch.FloatTensor和torch.cuda.FloatTensor的差異
Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same or input should be a MKLDNN tensor and weight is a dense tensor
問題中文翻譯成
輸入類型(torch.FloatTensor)和權(quán)重類型(torch.cuda.FloatTensor)應(yīng)該相同,或者輸入應(yīng)該是MKLDNN張量,權(quán)重是密集張量
-
問題 2:數(shù)據(jù)運(yùn)算過程中需交互的情況下不在同一個(gè)設(shè)備上
Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
問題中文翻譯成
預(yù)期所有張量都在同一設(shè)備上,但至少找到了兩個(gè)設(shè)備,cuda:0 和 cpu !
-
問題1和問題2解決方法(相同):首先根據(jù)錯(cuò)誤的提示找到代碼中是哪行數(shù)據(jù)出現(xiàn)的問題,然后針對(duì)性的解決,以數(shù)據(jù)XR出問題舉例,分兩種情況:第一種情況是如果此數(shù)據(jù)XR通過
torch.FloatTensor(數(shù)據(jù)XR)
構(gòu)造,那么改成torch.cuda.FloatTensor(數(shù)據(jù)XR)
即可;第二種情況是并非構(gòu)造數(shù)據(jù)XR,而是將數(shù)據(jù)XR傳輸?shù)搅硪粋€(gè)子模型中,那么直接在數(shù)據(jù)XR的后面加上cuda即可,也就是“數(shù)據(jù)XR.cuda()
”即可。 -
問題 3:代碼運(yùn)行過程中CUDA運(yùn)輸內(nèi)存不夠分配
CUDA out of memory. Tried to allocate 490.00 MiB (GPU 0; 2.00 GiB total capacity; 954.66 MiB already allocated; 62.10 MiB free; 978.00 MiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
問題中文翻譯成
CUDA內(nèi)存不足。嘗試分配490.00 MiB(GPU 0;2.00 GiB總?cè)萘浚?span id="bsd5o550550j" class="token number">954.66 MiB已分配;62.10 MiB可用;PyTorch總共保留978.00 MiB)如果保留內(nèi)存>>已分配內(nèi)存,請(qǐng)嘗試設(shè)置max_split_size_mb以避免碎片。請(qǐng)參閱內(nèi)存管理和PYTORCH_CUDA_ALLOC_CONF的文檔
-
問題3解決方法:運(yùn)算內(nèi)存不夠的情況下有兩種解決方法,第一種方法一般縮小數(shù)據(jù)集批次大小,也就是batch_size縮小,比如:可以將原來batch_size=256那么縮小成batch_size=16;第二種就是去服務(wù)器上跑代碼,也就是更換更好的GPU去運(yùn)行,如果還是出現(xiàn)同樣的問題,那么此兩種方法結(jié)合使用最好。
- 后續(xù)出現(xiàn)新的問題會(huì)繼續(xù)更新,敬請(qǐng)期待!
原文鏈接:https://blog.csdn.net/rothschild666/article/details/127446694
相關(guān)推薦
- 2022-04-16 C語言自定義類型全解析_C 語言
- 2023-07-02 Python利用scikit-learn實(shí)現(xiàn)近鄰算法分類的示例詳解_python
- 2022-10-09 React?Redux使用配置詳解_React
- 2023-01-01 C語言之如何求三次方根_C 語言
- 2022-10-29 tensorflow2數(shù)據(jù)讀取P2: tf.data.Dataset.from_generator通
- 2023-03-26 react組件實(shí)例屬性props實(shí)例詳解_React
- 2023-04-06 C++聚合體初始化aggregate?initialization詳細(xì)介紹_C 語言
- 2022-10-15 C語言中數(shù)據(jù)如何存儲(chǔ)進(jìn)內(nèi)存揭秘_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支