在pytorch結合cuda下,直接使用
output.data.numpy()
會報標題錯誤:
TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
解決辦法:
output.data.cpu().numpy()
把CUDA tensor格式的數據改成numpy時,需要先將其轉換成cpu float-tensor隨后再轉到numpy格式。 numpy不能讀取CUDA tensor 需要將它轉化為 CPU tensor。