Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Pytorch Tensor to Numpy Array using Cuda

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()

like image 427
Noa Yehezkel Avatar asked Nov 25 '18 12:11

Noa Yehezkel


1 Answers

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() 
like image 65
azizbro Avatar answered Sep 21 '22 04:09

azizbro