各有两种转换方式,一种是把pytorch.tensor 和对应 numpy放在同一个内存空间,改变tensor时numpy也会改变(The Torch Tensor and NumPy array will share their underlying memory locations (if the Torch Tensor is on CPU), and changing one will change the other.),另一种是单纯的copy,改变一个另一个不会发生变化。

1.同内存转换

1.1 tensor->numpy

我们可以看到改变numpy的值了以后,对应的tensor值也会改变。

>>>x = torch.randn(2,3)
>>>x
Out[