Save preds as numpy

I am trying to save the predictions from my code as CSV file but getting this error message. Can someone please help? Thanks

  output = model(images)
   preds = output.sum(dim=[1,2,3])
   np.save('output.csv', preds)

I am getting this error message:

RuntimeError: Can’t call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

Hi,

As mentioned in the error, you will need to detach to avoid giving a Tensor that requires_grad=True to numpy: np.save('output.csv', preds.detach())

Thanks! That has got rid of the error message, but the file that is getting saved as csv is empty for some reason. Any idea why please?

Not sure. What is the content of the Tensor you give to it? You can try to convert it to numpy before passing it with pred.detach().numpy() ?

Many Thanks for your help! When i run the training model the file is saving the tensor which consist of 16 values. However the values seem to get over written at each step and the final CSV final only has 3 values. Any idea why I am not getting all the values? Thanks

Hi,

I would guess that np.save() overrides the file every time and that the last batch from the dataloader is of size 3 (because dataset_len % batch_size = 3) and so you get this.
I am not sure if there is an option to append in np.save but a solution is to accumulate all the values in a Tensor and write it at the end.
Or write to the file yourself and make sure to append to it.