1. 前提
利用Transformer模型進(jìn)行O3濃度的反演
2. 問題
2.1 速度慢
一開始模型是在CPU上面跑的,為了加快速度,我改成了在GPU上跑
方法如下:
1、驗(yàn)證pytorch是否存在GPU版本
在Pycharm命令行輸入
import torch
print(torch.cuda.is_available)
# 若輸出為True,則存在GPU版本
# 若輸出為False,則不存在GPU版本
我的輸出為True,說明pytorch是存在GPU版本的
2、將模型從CPU版本轉(zhuǎn)換到GPU版本
- 聲明使用GPU(指定具體的卡)
PS:torch.device()
是裝torch.Tensor
的一個(gè)空間。
device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# 'cuda' 這里如果沒有指定具體的卡號(hào),系統(tǒng)默認(rèn)cuda:0
device = torch.device('cuda:2') # 使用2號(hào)卡
- 將模型(model)加載到GPU上
model = Transformer() #例子中,采用Transformer模型
model.to(device)
- 將數(shù)據(jù)和標(biāo)簽放到GPU上【注意!什么數(shù)據(jù)可以被放入GPU-Tensor類型的數(shù)據(jù)】
# 只有Tensor類型的數(shù)據(jù)可以放入GPU中
# 可以一個(gè)個(gè)【batch_size】進(jìn)行轉(zhuǎn)換
inputs = inputs.to(device)
labels = labels.to(device)
如果結(jié)果還是顯示你是在CPU上進(jìn)行訓(xùn)練,要不就是模型沒有加進(jìn)去,要不就是數(shù)據(jù)沒有加進(jìn)去
2.2 內(nèi)存不足
- 在使用CPU時(shí),出現(xiàn)了內(nèi)存不足的情況
RuntimeError: [enforce fail at C:\cb\pytorch_1000000000000\work\c10\core\impl\alloc_cpu.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 280410627200 bytes.
- 在使用GPU時(shí),出現(xiàn)了內(nèi)存不足的情況
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 261.15 GiB (GPU 0; 8.00 GiB total capacity; 487.30 MiB already allocated; 5.71 GiB free; 506.00 MiB reserved in total by PyTorch) If reserved memory is >> allocated memory try sett
我的模型在訓(xùn)練的時(shí)候沒有問題,在進(jìn)行預(yù)測(cè)的時(shí)候,總是出現(xiàn)內(nèi)存不足
(1)一開始我以為是batch_size
大小的問題,在從128更改到4后,發(fā)現(xiàn)依舊存在問題,這說明不是batch_size
大小的問題。
(2)然后,我猜測(cè)是反演過程的問題
我在進(jìn)行模型反演的過程中,直接將全部數(shù)據(jù)輸入到模型model
中(大概有10萬行),為了驗(yàn)證這個(gè)問題,我添加了一個(gè)for循環(huán),一個(gè)一個(gè)數(shù)據(jù)的反演
問題解決!文章來源:http://www.zghlxwxcb.cn/news/detail-414652.html
學(xué)習(xí)鏈接:文章來源地址http://www.zghlxwxcb.cn/news/detail-414652.html
- pytorch 中判斷和指定模型和數(shù)據(jù)在GPU或CPU上–有用
- Pytorch | GPU | 將代碼加載到GPU上運(yùn)行
- pytorch中cuda out of memory問題
到了這里,關(guān)于Pytorch運(yùn)行過程中解決出現(xiàn)內(nèi)存不足的問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!