Pytorch テンソルから NumPy 配列へ 質問する

Pytorch テンソルから NumPy 配列へ 質問する

Tensor私は形状 のPyTorch を持っています。次のコードを使用して[4, 3, 966, 1296]それを配列に変換したいと思います。numpy

imgs = imgs.numpy()[:, ::-1, :, :]

そのコードはどのように機能しますか?

ベストアンサー1

も使用する必要があると思います.detach()。私は、CUDA と GPU を使用する Colab で Tensor を numpy 配列に変換する必要がありました。次のようにしました。

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

おすすめ記事