How to convert Pytorch autograd.Variable to Numpy?

后端 未结 2 1859
甜味超标
甜味超标 2020-12-14 10:09

The title says it all. I want to convert a PyTorch autograd.Variable to its equivalent numpy array. In their official documentation they advocated

2条回答
  •  抹茶落季
    2020-12-14 10:30

    Two possible case

    • Using GPU: If you try to convert a cuda float-tensor directly to numpy like shown below,it will throw an error.

      x.data.numpy()

      RuntimeError: numpy conversion for FloatTensor is not supported

      So, you cant covert a cuda float-tensor directly to numpy, instead you have to convert it into a cpu float-tensor first, and try converting into numpy, like shown below.

      x.data.cpu().numpy()

    • Using CPU: Converting a CPU tensor is straight forward.

      x.data.numpy()

提交回复
热议问题