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

this is related. Basically you cannot introduce non-differentiable operation.