pytorch:在执行loss.backward()时out of memory报错 您所在的位置:网站首页 内存不足无法打开worksheet pytorch:在执行loss.backward()时out of memory报错

pytorch:在执行loss.backward()时out of memory报错

2023-08-01 21:15| 来源: 网络整理| 查看: 265

在自己编写SurfNet网络的过程中,出现了这个问题,查阅资料后,将得到的解决方法汇总如下 可试用的方法:

reduce batch size, all the way down to 1remove everything to CPU leaving only the network on the GPUremove validation code, and only executing the training codereduce the size of the network (I reduced it significantly: details below)I tried scaling the magnitude of the loss that is backpropagating as well to a much smaller value在训练时,在每一个step后面加上: torch.cuda.empty_cache() 在每一个验证时的step之后加上代码: with torch.no_grad() 不要在循环训练中累积历史记录 total_loss = 0 for i in range(10000): optimizer.zero_grad() output = model(input) loss = criterion(output) loss.backward() optimizer.step() total_loss += loss

total_loss在循环中进行了累计,因为loss是一个具有autograd历史的可微变量。你可以通过编写total_loss += float(loss)来解决这个问题。

本人遇到这个问题的原因是,自己构建的模型输入到全连接层中的特征图拉伸为1维向量时太大导致的,加入pool层或者其他方法将最后的卷积层输出的特征图尺寸减小即可。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有