I’m attempting to calculate the gradient w.r.t. an input using the formula
(self.gamma / 2.0) * (torch.norm(grad(output.mean(), inpt)[0]) ** 2)

where grad is the torch.autograd function, and both output and inpt require gradients. In some runs, it works fine; however, it often comes up with the error
RuntimeError: grad can be implicitly created only for scalar outputs

I’ve tried fixing the issue based on past discussions (i.e. Loss.backward() raises error 'grad can be implicitly created only for scalar outputs' 1 and Error: grad can be implicitly created only for scalar outputs 1) but to no avail. Any help would be greatly appreciated.