Pytorch tensor to numpy array

前端 未结 6 1304
名媛妹妹
名媛妹妹 2021-02-01 01:31

I have a pytorch Tensor of size torch.Size([4, 3, 966, 1296])

I want to convert it to numpy array using the following code:

<
6条回答
  •  悲&欢浪女
    2021-02-01 01:49

    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:

    # this is just my embedding matrix which is a Torch tensor object
    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 below is a numpy array
    tensor_array.cpu().detach().numpy()
    

提交回复
热议问题