Skimming through your code I couldn’t find anything obviously wrong.
I assume you are using nn.BCELoss as the criterion.
Which optimizer are you using and how did you set the hyperparamters (learning rate etc.)?
Also, why do you have to reshape the target in your validation loop, while the training loop seems to be alright?
Could there be some kind of shape mismatch?
As a small side note: Variables are deprecated since Pytorch 0.4.0. You don’t have to wrap tensors anymore into it. If you are using an older version, you’ll find the install instructions here 11.