简体   繁体   English

如何将pytorch张量转换为numpy数组?

[英]How to convert a pytorch tensor into a numpy array?

I have a torch tensor我有一个火炬张量

a = torch.randn(1, 2, 3, 4, 5)

How can I get it in numpy?我怎样才能在 numpy 中得到它?

Something like就像是

b = a.tonumpy()

output should be the same as if I did输出应该和我一样

b = np.random.randn(1, 2, 3, 4, 5)

copied from pytorch doc :复制自pytorch 文档

a = torch.ones(5)
print(a)

tensor([1., 1., 1., 1., 1.])张量([1., 1., 1., 1., 1.])

b = a.numpy()
print(b)

[1. [1. 1. 1. 1. 1.] 1. 1. 1. 1.]


Following from the below discussion with @John:以下与@John 的讨论:

In case the tensor is (or can be) on GPU, or in case it (or it can) require grad, one can use如果张量在(或可以)在 GPU 上,或者如果它(或可以)需要 grad,可以使用

t.detach().cpu().numpy()

I recommend to uglify your code only as much as required.我建议仅根据需要丑化您的代码。

You can try following ways您可以尝试以下方法

1. torch.Tensor().numpy()
2. torch.Tensor().cpu().data.numpy()
3. torch.Tensor().cpu().detach().numpy()

Another useful way :另一种有用的方法:

a = torch(0.1, device='cuda')

a.cpu().data.numpy()

Answer回答

array(0.1, dtype=float32)数组(0.1,dtype=float32)

This is a function from fastai core :这是fastai core 的一个函数:

def to_np(x):
    "Convert a tensor to a numpy array."
    return apply(lambda o: o.data.cpu().numpy(), x)

Possible using a function from prospective PyTorch library is a nice choice.可能使用来自预期 PyTorch 库的函数是一个不错的选择。

If you look inside PyTorch Transformers you will find this code :如果您查看PyTorch Transformers内部,您会发现以下代码

preds = logits.detach().cpu().numpy()

So you may ask why the detach() method is needed?所以你可能会问为什么需要detach()方法? It is needed when we would like to detach the tensor from AD computational graph.当我们想从 AD 计算图中分离张量时需要它。

Still note that the CPU tensor and numpy array are connected .还是要注意,CPU张量和numpy数组是相连的 They share the same storage:它们共享相同的存储:

import torch
tensor = torch.zeros(2)
numpy_array = tensor.numpy()
print('Before edit:')
print(tensor)
print(numpy_array)

tensor[0] = 10

print()
print('After edit:')
print('Tensor:', tensor)
print('Numpy array:', numpy_array)

Output:输出:

Before edit:
tensor([0., 0.])
[0. 0.]

After edit:
Tensor: tensor([10.,  0.])
Numpy array: [10.  0.]

The value of the first element is shared by the tensor and the numpy array.第一个元素的值由张量和 numpy 数组共享。 Changing it to 10 in the tensor changed it in the numpy array as well.在张量中将其更改为 10 也会在 numpy 数组中更改它。

This is why we need to be careful, since altering the numpy array my alter the CPU tensor as well.这就是为什么我们需要小心,因为改变 numpy 数组也会改变 CPU 张量。

You may find the following two functions useful.您可能会发现以下两个函数很有用。

  1. torch.Tensor.numpy() 火炬.Tensor.numpy()
  2. torch.from_numpy() torch.from_numpy()

Sometimes if there's "applied" gradient, you'll first have to put .detach() function before the .numpy() function.有时候,如果有“应用”渐变,你首先得把.detach()的函数之前.numpy()函数。

loss = loss_fn(preds, labels)
print(loss.detach().numpy())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM