How to calculate MSE loss with respect to input gradient?

What is the best way to optimize \theta for the the following loss function:

I tried the following but does not work:

optimizer.zero_grad()

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