深度学习【29】pytorch 自定义损失函数

构建一个加权函数:
loss=w1loss1+w2loss2 l o s s = w 1 ∗ l o s s 1 + w 2 ∗ l o s s 2

class myloss(nn.Module):
    def __init__(self,w1,w2):
        super(myloss,self).__init__()
        self.w1 = w1
        self.w2 = w2
        return
    def forward(self,output1,target1,output2,target2,mse):#mse:最小平方误差函数
        loss = self.w1*mse(output1,target1)+self.w2*mse(output2,target2)

        return loss

你可能感兴趣的:(深度学习)