pytorch運行錯誤:RuntimeError: cuDNN error: CUDNN_STATUS_INTERNAL_ERROR
解決方法:網絡
代碼中添加: app
torch.cuda.set_device(0)
訓練RNN網絡loss出現Nan解決辦法
(1). 梯度爆炸的緣由能夠經過梯度裁決解決ide
GRAD_CLIP = 5 loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), GRAD_CLIP) optimizer.step()
(2)testModel和evaluate中須要使用函數
with torch.no_grad():
(3) 學習率調小一點學習
RuntimeError: Expected object of device type cuda but got device type cpu for argument #1 'self' in call to _th_addmm
在代碼中由三個位置須要進行cuda()轉換:lua
- 模型是否放到了CUDA上
model = model.to(device)
- 輸入數據是否放到了CUDA上
data = data.to(device)
- 模型內部新建的張量是否放到了CUDA上
p = torch.tensor([1]).to(device)
關於第一條中model = model.to(device)只對model中實例化在__init__()中的函數有效,若是在forward中實例化並直接使用則不會將model放置到cuda中。spa
下面給出一個錯誤的代碼:code
import torch import torch.nn as nn data = torch.rand(1, 10).cuda() class TestMoule(nn.Module): def __init__(self): super(TestMoule, self).__init__() # self.linear = torch.nn.Linear(10, 2) def forward(self, x): # return self.linear(x) return torch.nn.Linear(10, 2)(x) model = TestMoule() model = model.cuda() print(model(data))
RuntimeError: CUDA error: an illegal memory access was encountered
出現上面問題一種狀況是某些nn模塊下的函數傳入了gpu類型的數據,以下錯誤代碼:orm
import torch data = torch.randn(1, 10).cuda() layernorm = torch.nn.LayerNorm(10) # layernorm = torch.nn.LayerNorm(10).cuda() re_data = layernorm(data) print(re_data)
RuntimeError: CUDA error: device-side assert triggered
分類的類別target與模型輸出softmax的值不是一一對應的,如三分類問題:ip
targets 爲 1-3的值,可是softmax計算的值是0-2,所以提示上面的錯誤。
df = pd.read_csv('data/reviews.csv') def to_sentiment(score): score = int(score) if score <= 2: return 0 elif score == 3: return 1 else: return 2 df['sentiment'] = df.score.apply(to_sentiment)