python – Pytorch tensor to numpy array

python – Pytorch tensor to numpy array

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:

n

# this is just my embedding matrix which is a Torch tensor objectnembedding = learn.model.u_weightnnembedding_list = list(range(0, 64382))nninput = torch.cuda.LongTensor(embedding_list)ntensor_array = embedding(input)n# the output of the line below is a numpy arrayntensor_array.cpu().detach().numpy()n

This worked for me:

n

np_arr = torch_tensor.cpu().detach().numpy()n

python – Pytorch tensor to numpy array

There are 4 dimensions of the tensor you want to convert.

n

[:, ::-1, :, :] n

n

: means that the first dimension should be copied as it is and converted, same goes for the third and fourth dimension.

n

::-1 means that for the second axes it reverses the the axes

Leave a Reply

Your email address will not be published.