I implemented the code like below:
for layer in self.model.features:
    if isinstance(layer, torch.nn.Conv2d):
        kernel_vec = layer.weight.reshape(layer.out_channels, -1)
        kernel_norm = torch.norm(kernel_vec, p=1, dim=1)
        for i in range(kernel_norm.shape[0]):
            kernel_vec[i] = kernel_vec[i] / kernel_norm[i]
        inner_prod = torch.mm(kernel_vec, kernel_vec.t())
        inner_prod = inner_prod - torch.diag(inner_prod)
        inner_loss = torch.sum(torch.abs(inner_prod))
        loss += inner_loss

optimizer.zero_grad()
loss.backward()
optimizer.step()

But it doesn’t work. I have the error code like this:

one of the variables needed for gradient computation has been modified by an inplace operation

I found this error occurred by the in-place operation. But I don’t know how fix the code…
Could you help me?