pytorch学习笔记 ---常见问题

local variable ‘val_loss’ referenced before assignment

第一种是变量没有赋值

def test():
    print(a)

这里a没有赋值,应该改成如下形式

def test():
    a = 1
    print(a)

第二种是全局变量没有声明

a = 1
 
def test():
	print(a)

在这里a是全局变量,在定义函数中需要进行变量声明,该为如下形式

a = 1
 
def test():
    global a   # 声明引用的是全局变量
    print(a)

参考博客:local variable referenced before assignment 原因及解决办法

CUDA out of memory 怎么解决?

RuntimeError: CUDA out of memory. Tried to allocate 20.00 MiB (GPU 0; 6.00 GiB total capacity; 192.37 MiB already allocated; 11.46 MiB free; 204.00 MiB reserved in total by PyTorch)

  1. 减小输入的尺寸;
  2. 减少输入的batch size;
  3. 将网络结构改小;
  4. 使用新版pytorch的fp16半精度训练,net.half()就行,理论上可以减少一半显存;
  5. 以上的方法混用,对精度有所影响,谨慎使用
  6. 不计算梯度
    使用with torch.no_grad():
    给出一篇博主写的博客:pytorch运行错误:CUDA out of memory
  7. 释放内存
    在报错代码前加上以下代码,释放无关内存:
if hasattr(torch.cuda, 'empty_cache'):
	torch.cuda.empty_cache()

参考博客:解决:RuntimeError: CUDA out of memory. Tried to allocate 2.00 MiB
pytorch: 四种方法解决RuntimeError: CUDA out of memory. Tried to allocate … MiB

[Errno 32] Broken pipe

源自博文:BrokenPipeError: [Errno 32] Broken pipe 基于pytorch的训练 num_worker参数理解

BrokenPipeError: [Errno 32] Broken pipe 解决
在这里插入图片描述
在这里插入图片描述
原因:
在训练过程中,设置的num_workers过大
在这里插入图片描述
在这里插入图片描述
修改为 num_workers=0 即可。

num_worker参数理解
下面作者讲的很详细,大致就是设置参数大的话,例如参数为10,就可以有10个线程来加载batch到内存。当然参数设置过大,而自己内存不够的情况下,就会出现线程管道破裂,即broken pipe ,所以一般默然设置为0.
当然:如果num_worker设为0,意味着每一轮迭代时,dataloader不再有自主加载数据到RAM这一步骤(因为没有worker了),而是在RAM中找batch,找不到时再加载相应的batch。缺点当然是速度更慢。
具体参考以下链接。
参考:pytorch中num_workers详解

AttributeError: ‘NoneType‘ object has no attribute dim

参考博客:AttributeError: ‘NoneType‘ object has no attribute ‘dim BN层

Target size (torch.Size([4, 1, 256, 256])) must be the same as input size (torch.Size([4, 1, 248, 248])

在这里插入图片描述
原因:output,mask的尺寸不匹配

python报错‘xxx’ object has no attribute ‘xxx’

参考博客:python报错‘StandardScaler’ object has no attribute ‘transfrom’

No module named ‘lmdb’ 的解决办法

conda install -c conda-forge python-lmdb

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值