文章首发及后续更新:https://mwhls.top/3680.html
新的更新内容请到mwhls.top查看。
无图/无目录/格式错误/更多相关请到上方的文章首发页面查看。
stackoverflow热门问题目录
如有翻译问题欢迎评论指出,谢谢。
DukeLover asked:
torch.Size([4, 3, 966, 1296])
的 pytorch Tensor
,numpy
数组:imgs = imgs.numpy()[:, ::-1, :, :]
Answers:
azizbro – vote: 86
我觉得你还得加上 detach()
。我在利用CUDA和GPU进行计算的Colab上用下面的代码来实现张量和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)
tensor_array.cpu().detach().numpy()
Scott – vote: 29
试试这个:
np_arr = torch_tensor.cpu().detach().numpy()
Maaz Bin Musa – vote: 24
因为这个张量里有四个维度。
[:, ::-1, :, :]
:
表示第一、第三、第四维度在转换时保持不变。
::-1
表示第二维度需要翻转。
DukeLover asked:
pytorch
Tensor of size torch.Size([4, 3, 966, 1296])
torch.Size([4, 3, 966, 1296])
的 pytorch Tensor
,numpy
array using the following code:numpy
数组:imgs = imgs.numpy()[:, ::-1, :, :]
Answers:
azizbro – vote: 86
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:
我觉得你还得加上 detach()
。我在利用CUDA和GPU进行计算的Colab上用下面的代码来实现张量和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()
Scott – vote: 29
This worked for me:
试试这个:
np_arr = torch_tensor.cpu().detach().numpy()
Maaz Bin Musa – vote: 24
There are 4 dimensions of the tensor you want to convert.
因为这个张量里有四个维度。
[:, ::-1, :, :]
:
means that the first dimension should be copied as it is and converted, same goes for the third and fourth dimension.:
表示第一、第三、第四维度在转换时保持不变。
::-1
means that for the second axes it reverses the the axes::-1
表示第二维度需要翻转。