日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

基于Pytorch使用GPU運(yùn)行模型方法及可能出現(xiàn)的問題解決方法

作者:rothschildlhl 更新時(shí)間: 2022-10-25 編程語言

基于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 02.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

欄目分類
最近更新