小编Ant*_*ena的帖子

PyTorch CUDA 错误:遇到非法内存访问

使用 CUDA 相对较新。经过一段看似随机的时间后,我不断收到以下错误: RuntimeError: CUDA error: an invalid memory access was Been应付

我见过人们提出诸如使用cuda.set_device()而不是cuda.device()设置之类的建议torch.backends.cudnn.benchmark = False

但我似乎无法让错误消失。这是我的一些代码: torch.cuda.set_device(torch.device('cuda:0')) torch.backends.cudnn.benchmark = False

class LSTM(nn.Module):
    def __init__(self, input_dim, hidden_dim, num_layers, output_dim):
        super(LSTM, self).__init__()
        self.hidden_dim = hidden_dim
        self.num_layers = num_layers

        self.lstm = nn.LSTM(input_dim, hidden_dim, num_layers, batch_first=True, dropout=0.2)

        self.fc = nn.Linear(hidden_dim, output_dim)

    def forward(self, x):
        h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).requires_grad_().cuda()
        c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_dim).requires_grad_().cuda()

        out, (hn, cn) = self.lstm(x, (h0.detach(), c0.detach()))

        out = self.fc(out[:, -1, :]) 
        
        return out

    def …
Run Code Online (Sandbox Code Playgroud)

python pytorch

15
推荐指数
2
解决办法
5万
查看次数

标签 统计

python ×1

pytorch ×1