You can just do what I told you.
Option 1:
loss_avg+=loss.data

Option 2:
with torch.no_grad():

     loss_avg+=loss


Take a look at the autograd documentation to see what does the torch tensor class stores