If you care for a bit of unsolicited advice: Note that X.reqires_grad = True will silently not do the expected while X.reqires_grad_() will tell you about your spelling. For that reason (and for saying y = x.detach().requires_grad_() and similar more compactly) I have moved to use the function exclusively.
Best regards
Thomas