I would like to convert a Pytorch tensor to numpy array using cuda:
this is the code line while not using cuda:
A = self.tensor.weight.data.numpy()
How can I do the same operation using cuda? According to this: https://discuss.pytorch.org/t/how-to-transform-variable-into-numpy/104/3 it seems:
A = self.tensor.weight.data.cpu().numpy()
I believe you also have to use .detach(). I had to convert my Tensor to a numpy array on Colab which uses CUDA and GPU. I did it like the following:
embedding = learn.model.u_weight
embedding_list = list(range(0, 64382))
input = torch.cuda.LongTensor(embedding_list)
tensor_array = embedding(input)
# the output of the line bwlow is a numpy array
tensor_array.cpu().detach().numpy()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With