关于权重的pytorch自定义损失

pytorch custom loss with regards to weights

我有两点损失:

  1. MSE 损失
  2. 基于网络权重的自定义损失项。

我有这个代码:

net = CustomNet()
mse_loss = torch.nn.MSELoss()
def custom_loss(output, target):
    weights = net.linear_layer.weight.data
    return mse_loss(output, target) + torch.linalg.norm(weights @ weights.T -
                                                        torch.eye(weights.shape[0]))

当我尝试移除 MSE 损失时(因此我的损失仅基于权重):

def custom_loss(output, target):
    weights = net.linear_layer.weight.data
    return torch.linalg.norm(weights @ weights.T -
                             torch.eye(weights.shape[0])) 

我收到错误:

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

我注意到 mse 损失有 grad_fn=<MseLossBackward object at 0x14908c450>

我做错了什么?为什么我不能只使用第二个损失?

您不能单独使用第二项,因为它不像第一项那样具有 grad_fn 功能。这意味着如果你有两个术语,它只会在第一个术语(MSE 损失)上反向传播,而不会考虑第二个术语。没有 grad_fn 意味着它被视为常量 w.r.t。输入或参数,对梯度没有影响。

您用来计算第二项的张量不需要梯度。更具体地说,您使用 data 属性获得的任何张量都不需要梯度。在你的情况下 net.linear_layer.weight.data.

相反,您应该直接通过以下方式访问张量:

>>> weights = net.linear_layer.weight