nn.MSELoss expects the second input to be the “target”. In your case layer1 is a Variable which requires its gradients to be computed (according to your snippet) and hence the error.
You can compute your loss like  this:
loss = torch.pow(layer2 - layer1, 2).mean()