one of the variables needed for gradient computation has been modified by an inplace operation
Here is my LossFunction, when I use this function, it will make this error.
And I have tested that using the nn.L1Loss() instead of my LossFunction, and the network is ok.
what should I do? Thanks for your help!

class LossV1(nn.Module):
    def __init__(self,weight=1,pos_weight=1,scale_factor=2.5):
        super(LossV1,self).__init__()
        self.weight = weight
        self.pos_weight = pos_weight
        self.scale_factor = scale_factor

    def forward(self,pred,truth):
        objmask = torch.tensor(truth[:,:6,:,:],dtype=torch.float32,requires_grad=False)
        #没有物体的Boxes，置信度损失*0.4
        objmask[objmask&lt;0.65] = 0.4
        #辅助Boxes，系数0.8
        objmask[(objmask&gt;0.649)*(objmask&lt;0.949)] = 0.8
        objLoss = torch.sum(objmask*self.myBCEWithLogitsLoss(pred[:,:6,:,:],truth[:,:6,:,:]))
        #没有物体的Boxes，只计算置信度损失
        objmask[objmask&lt;0.41] = 0
        personLoss = torch.sum(objmask*self.myBCEWithLogitsLoss(pred[:,6:12,:,:],truth[:,6:12,:,:]))
        carLoss = torch.sum(objmask*self.myBCEWithLogitsLoss(pred[:,12:18,:,:],truth[:,12:18,:,:]))
        wLoss = torch.sum(objmask*self.myL2Loss(pred[:,18:24,:,:],truth[:,18:24,:,:]))
        hLoss = torch.sum(objmask*self.myL2Loss(pred[:,24:,:,:],truth[:,24:,:,:]))
        return objLoss+personLoss+carLoss+wLoss+hLoss

    def myBCEWithLogitsLoss(self,x,y):
        #pos_weight&gt;1增加召回,pos_weight&lt;1提高精度
        return -self.weight*(self.pos_weight*y*torch.log(torch.sigmoid(x))+(1-y)*torch.log(1-torch.sigmoid(x)))

    def myL2Loss(self,x,y):
        return torch.pow(self.scale_factor*torch.sigmoid(x/self.scale_factor) - y,2)


I just remove the objmask,and compute it in my generation function,then pass it on to LossFunction with truth label,and the network works.
I can't understand I have already done that making the requires_grad=False,why pytroch still compute the gradient.
