The code looks generally alright. Some minor issues:


Variables are deprecated since PyTorch 0.4, so you can use tensors now
most likely you don’t need gradients in the input data and target, so you could skip the requires_grad=True attribute during training
you don’t need to reset the requires_grad attribute for all parameters in each iteration
don’t use the .data attribute, as it might have unwanted side effects.

Did you run the check by swapping the training set for the validation set?