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

Usually these error can happen, if you are detaching some tensors from the computation graph as described here. Could you check, if this might be the case here?