RuntimeError: a leaf Variable that requires grad has been used in an in-place operation

Traceback (most recent call last):
File “E:\迅雷下载\向量\000、代码+数据+课件\YOLO5\yolov5-master\train.py”, line 466, in
train(hyp, opt, device, tb_writer)
File “E:\迅雷下载\向量\000、代码+数据+课件\YOLO5\yolov5-master\train.py”, line 79, in train
model = Model(opt.cfg, ch=3, nc=nc).to(device) # create 灏辨槸鍜变滑涔嬪墠璁茬殑鍒涘缓妯″瀷閭e潡
File “E:\迅雷下载\向量\000、代码+数据+课件\YOLO5\yolov5-master\models\yolo.py”, line 89, in init
self._initialize_biases() # only run once
File “E:\迅雷下载\向量\000、代码+数据+课件\YOLO5\yolov5-master\models\yolo.py”, line 149, in _initialize_biases
b[:, 4] =b[:, 4]+ math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.

解决办法:
叶子节点不能执行in-place(原地)操作,因为在进行前向传播的时候得到的是叶子结点的地址,再进行反向传播的时候这个地址不变才不会报错,地址改变了就会出错

要将

a += torch.ones((1, ))

改为

a = a + torch.ones((1, ))

原代码:

for mi, s in zip(m.m, m.stride):  #  from
            b = mi.bias.view(m.na, -1)  # conv.bias(255) to (3,85)
            b[:, 4]+= math.log(8 / (640 / s) ** 2)  # obj (8 objects per 640 image)
            b[:, 5:]+= math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum())  # cls
            mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)

本错误的解决办法:

 for mi, s in zip(m.m, m.stride):  #  from
            b = mi.bias.view(m.na, -1)  # conv.bias(255) to (3,85)
            bb = b[:, 4]
            bb = bb + math.log(8 / (640 / s) ** 2)  # obj (8 objects per 640 image)
            bbb = b[:, 5:]
            bbb = bbb + math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum())  # cls
            mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
  • 10
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值