RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead

Hi All I have data from a dataloader which I pass to my model for prediction. I am trying to normalize my prediction within a certain range using T.tonumpy_denormalize, however I get this error RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead. I have tried to use .detach().numpy() on my prediction tensor but I get this error afterwards AttributeError: 'numpy.ndarray' object has no attribute 'cpu'. Initially I wasn’t getting any of these error but I changed my code structure and started getting this error, even though its the same code as before. Below is my sample code for demonstration:

val = "./data.txt"
val_data = torch.load(val)
dataloader = torch.utils.data.DataLoader(val_data, batch_size=1, shuffle=True)

def evaluate(model):
      label_min = 1500
      label_max = 4500
      model.eval()
      data, label = iter(dataloader).next()
      pred = model(data)
      pred_np = T.tonumpy_denormalize(pred, label_min, label_max, exp=False)   # I get the error on this line. #
# previously wasn't getting this error, I just changed the code structure by moving some parts upwards and got this error. #

# full error is below #
pred_np = T.tonumpy_denormalize(pred, label_min, label_max, exp=False)
  File "/home/pi/Desktop/exp/fcnvmb/transforms.py", line 97, in tonumpy_denormalize
    vid = minmax_denormalize(vid.cpu().numpy(), vmin, vmax, scale)
RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.

You are trying to call numpy() on a tensor, which is still attached to a computation graph, and which is disallowed without calling detach() explicitly. This error is used to make sure the users are aware that this tensor is being detached (and thus they have to use the detach() call explicitly) in the numpy() operation.
I don’t know why the error wasn’t raised previously, but pred should have been attached to a computation graph before as well.

Okay I figured. I just used pred.detach() and it worked. Thanks.

I try to use a neural network to estimate parameters. When I loop my program 10000 times, and it runs without error. But the result is rather bad, Then I try to loop the program 100,000,000 times, it shows this error, who knows the reason? I feel very wired.