译(五十七)-Pytorch避免’CUDA out of memory’

文章首发及后续更新:https://mwhls.top/3789.html,无图/无目录/格式错误/更多相关请至首发页查看。
新的更新内容请到mwhls.top查看。
欢迎提出任何疑问及批评,非常感谢!

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。

如何避免PyTorch的’CUDA out of memory’
  • voilalex asked:

    • 下面的报错对于缺少 GPU 资源的 PyTorch 用户们很常见:

    • RuntimeError: CUDA out of memory. Tried to allocate 😊 MiB (GPU 😊; 😊 GiB total capacity; 😊 GiB already allocated; 😊 MiB free; 😊 cached)
    • 我试过先把图片用 GPU 传给每一层网络,然后再把网络丢回 GPU:

    • for m in self.children():
      m.cuda()
      x = m(x)
      m.cpu()
      torch.cuda.empty_cache()
    • 但效果不佳。有没什么小技巧可以用一个小 GPU 训练一个大网络?

  • Answers:

    • SHAGUN SHARMA – vote: 57

    • 即便

    • import torch
      torch.cuda.empty_cache()
    • 提供了一个不错的方式来清理没用的 CUDA 内存,以及手动清理不再使用的变量,

    • import gc
      del variables
      gc.collect()
    • 但清理后依然报错,因为 PyTorch 实际上并不清理内存,而是清理变量的内存引用。所以找到一个更合适的 batch_size 可能是个更好的选择(虽然有时很不方便)。

    • 另一个详细获得 GPU 内存信息的方式:

    • torch.cuda.memory_summary(device=None, abbreviated=False)
    • 这里的所有参数都是可选的,它可以给出一个清晰的内存分配总结,方便我们调整代码避免再次出现 CUDA out of memory

    • 还可以试着迭代数据,改变网络大小或是分解网络,都挺有效的(有的模型会占据较多内存,比如迁移学习)。

    • Rahul – vote: 32

    • 减少 batch_size。我训练的时候有这个错:

    CUDA out of memory. Tried to allocate 20.00 MiB (GPU 0; 10.76 GiBtotal capacity; 4.29 GiB already allocated; 10.12 MiB free; 4.46 GiBreserved in total by PyTorch)

    • 、我把 batch_size 从 32 调到 15 的时候就正常了。

    • Nicolas Gervais – vote: 21

    • 迭代的训练数据,或者用小的 batch_size。不要一次性把所有数据都塞给 CUDA,像这样分开塞会更好:

    • for e in range(epochs):
        for images, labels in train_loader:   
            if torch.cuda.is_available():
                images, labels = images.cuda(), labels.cuda()   
            # blablabla  
    • 改变 dtypes 降低点精度会使用更低内存。例如 torch.float16torch.half 都不错。


How to avoid CUDA out of memory in PyTorch
  • voilalex asked:

    • I think it\’s a pretty common message for PyTorch users with low GPU memory:
      下面的报错对于缺少 GPU 资源的 PyTorch 用户们很常见:

    • RuntimeError: CUDA out of memory. Tried to allocate 😊 MiB (GPU 😊; 😊 GiB total capacity; 😊 GiB already allocated; 😊 MiB free; 😊 cached)
    • I tried to process an image by loading each layer to GPU and then loading it back:
      我试过先把图片用 GPU 传给每一层网络,然后再把网络丢回 GPU:

    • for m in self.children():
      m.cuda()
      x = m(x)
      m.cpu()
      torch.cuda.empty_cache()
    • But it doesn\’t seem to be very effective. I\’m wondering is there any tips and tricks to train large deep learning models while using little GPU memory.
      但效果不佳。有没什么小技巧可以用一个小 GPU 训练一个大网络?

  • Answers:

    • SHAGUN SHARMA – vote: 57

    • Although
      即便

    • import torch
      torch.cuda.empty_cache()
    • provides a good alternative for clearing the occupied cuda memory and we can also manually clear the not in use variables by using,
      提供了一个不错的方式来清理没用的 CUDA 内存,以及手动清理不再使用的变量,

    • import gc
      del variables
      gc.collect()
    • But still after using these commands, the error might appear again because pytorch doesn\’t actually clears the memory instead clears the reference to the memory occupied by the variables.So reducing the batch_size after restarting the kernel and finding the optimum batch_size is the best possible option (but sometimes not a very feasible one).
      但清理后依然报错,因为 PyTorch 实际上并不清理内存,而是清理变量的内存引用。所以找到一个更合适的 batch_size 可能是个更好的选择(虽然有时很不方便)。

    • Another way to get a deeper insight into the alloaction of memory in gpu is to use:
      另一个详细获得 GPU 内存信息的方式:

    • torch.cuda.memory_summary(device=None, abbreviated=False)
    • wherein, both the arguments are optional. This gives a readable summary of memory allocation and allows you to figure the reason of CUDA running out of memory and restart the kernel to avoid the error from happening again (Just like I did in my case).
      这里的所有参数都是可选的,它可以给出一个清晰的内存分配总结,方便我们调整代码避免再次出现 CUDA out of memory

    • Passing the data iteratively might help but changing the size of layers of your network or breaking them down would also prove effective (as sometimes the model also occupies a significant memory for example, while doing transfer learning).
      还可以试着迭代数据,改变网络大小或是分解网络,都挺有效的(有的模型会占据较多内存,比如迁移学习)。

    • Rahul – vote: 32

    • Just reduce the batch size, and it will work.While I was training, it gave following error:
      减少 batch_size。我训练的时候有这个错:

    CUDA out of memory. Tried to allocate 20.00 MiB (GPU 0; 10.76 GiBtotal capacity; 4.29 GiB already allocated; 10.12 MiB free; 4.46 GiBreserved in total by PyTorch)

    • And I was using batch size of 32. So I just changed it to 15 and it worked for me.
      我把 batch_size 从 32 调到 15 的时候就正常了。

    • Nicolas Gervais – vote: 21

    • Send the batches to CUDA iteratively, and make small batch sizes. Don\’t send all your data to CUDA at once in the beginning. Rather, do it as follows:
      迭代的训练数据,或者用小的 batch_size。不要一次性把所有数据都塞给 CUDA,像这样分开塞会更好:

    • for e in range(epochs):
        for images, labels in train_loader:   
            if torch.cuda.is_available():
                images, labels = images.cuda(), labels.cuda()   
            # blablabla  
    • You can also use dtypes that use less memory. For instance, torch.float16 or torch.half.
      改变 dtypes 降低点精度会使用更低内存。例如 torch.float16torch.half 都不错。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: PyTorch CUDA 内存不足 这个错误通常是由于您的 GPU 内存不足而导致的。您可以尝试以下方法来解决这个问题: 1. 减少批量大小:减少每个批次中的样本数量,以减少 GPU 内存的使用。 2. 减少模型大小:减少模型的参数数量,以减少 GPU 内存的使用。 3. 使用更大的 GPU:如果您的 GPU 内存不足,您可以尝试使用更大的 GPU。 4. 使用分布式训练:使用多个 GPU 进行分布式训练,以减少每个 GPU 的内存使用。 5. 使用梯度累积:将多个小批次的梯度累积到一个大批次中,以减少每个批次的内存使用。 希望这些方法能够帮助您解决 PyTorch CUDA 内存不足的问题。 ### 回答2: PyTorch是一种广泛使用的深度学习工具,尤其是在GPU上的加速,然而,当在进行深度学习训练时,开发者可能会遇到'CUDA out of memory'的报错。这个报错意味着GPU存储区已经无法容纳更多的数据了,导致无法继续进行深度学习训练。那么,如何解决这个问题呢? 一些常见的避免OutOfMemory问题的方法如下: 1. 降低batch size:降低batch size是最常用的方法来解决OutOfMemory问题。减少batch size能够减少GPU内存使用量。但这也会导致训练时间变慢,可能会减少准确性。 2. 将数据集分割成更小的块:如果无法在GPU上同时处理整个数据集,则可以将数据集分割成更小的块,每个块都小于GPU的总内存。这样做可以避免OutOfMemory错误并加速训练。 3. 对数据进行规范化:对数据进行规范化可以降低GPU存储区的使用率。例如,可以通过减去均值再除以标准差来标准化输入数据。 4. 使用更少的参数和层:使用较少的参数和层可以减少模型的复杂性和存储需求。但这也可能会对准确性产生影响。 5. 使用更大的GPU:如果您的GPU内存不足以容纳整个模型,则可以考虑使用更大的GPU。 6. 缓存的清除:在深度学习训练过程中,Python和机器学习库会存储大量的缓存信息。在GPU内存不足时,这些缓存将会占据GPU内存,因此在训练过程中及时清理缓存,可以释放更多的GPU内存,从而解决OutOfMemory问题。 总之,OutOfMemory错误在PyTorch中是一个常见的问题,但是通过采用上面的方法,您可以很容易地解决这个问题,确保深度学习训练顺利进行。 ### 回答3: Pytorch是一个非常强大的深度学习库,能够进行各种各样的深度学习算法和模型的创建和训练。在使用Pytorch进行深度学习任务时,很可能会遇到一个名为“cuda out of memory”的错误。 这个错误的意思是,GPU显存不足。在Pytorch中,当你使用GPU来训练你的模型时,你需要把模型和数据都放到GPU中。也就是说,Pytorch会在GPU显存中存储你的模型和数据,然后进行计算。如果你的GPU显存不足,计算就会失败,导致“cuda out of memory”错误。 如果你遇到了这个错误,有几种办法可以解决它: 1. 减少batch_size:通过减少每个batch中图像的数量来减小GPU显存的使用量,可以通过在DataLoader中设置batch_size参数实现。 2. 减少网络层数:如果你的网络非常复杂,导致GPU显存不足,可以尝试减少网络层数,或者调整一些网络结构参数来降低GPU显存使用量。 3. 使用更大显存的GPU:如果你有条件升级你的GPU,可以选择一款显存更大的GPU,这样就有更多的显存来存储你的模型和数据,从而避免cuda out of memory”错误。 总之,遇到“cuda out of memory”错误并不意味着你的代码有问题,它只是表示你需要采取一些措施来优化你的训练过程,以确保GPU显存的充足。同时,Pytorch还提供了一些工具和API来帮助你查看GPU显存的使用情况,这也可以帮助你更好地理解你的代码和模型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值