Pytorch遇到的错误解决方法

1. pytorch运行错误:RuntimeError: cuDNN error: CUDNN_STATUS_INTERNAL_ERROR

解决方法:

代码中添加:

torch.cuda.set_device(0)

2. 训练RNN网络loss出现Nan解决办法

(1) 梯度爆炸的原因可以通过梯度裁决解决

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) 学习率调小一点

3. RuntimeError: Expected object of device type cuda but got device type cpu for argument

在代码中由三个位置需要进行cuda()转换:

  1. 模型是否放到了CUDA上model = model.to(device)
  2. 输入数据是否放到了CUDA上data = data.to(device)
  3. 模型内部新建的张量是否放到了CUDA上p = torch.tensor([1]).to(device)

关于第一条中model = model.to(device)只对model中实例化在__init__()中的函数有效,如果在forward中实例化并直接使用则不会将model放置到cuda中。

下面给出一个错误的代码:

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))

4. RuntimeError: CUDA error: an illegal memory access was encountered

出现上面问题一种情况是某些nn模块下的函数传入了gpu类型的数据,如下错误代码:

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)

5. RuntimeError: CUDA error: device-side assert triggered

分类的类别target与模型输出softmax的值不是一一对应的,如三分类问题:

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)

6. RuntimeError: CUDA error: CUBLAS_STATUS_INTERNAL_ERROR when calling xxxx

https://zhuanlan.zhihu.com/p/140954200

训练时加上以下代码解决

torch.cuda.set_device(1)

  

当出现以下错误时,

RuntimeError: CUDA error: an illegal memory access was encountered error

也可以使用以上方法解决。

不知为何,总能work fine。