pytorch torch.from_numpy
功能说明:从numpy数组创建一个张量,数组和张量共享相同内存.
torch.from_numpy(ndarray) → Tensor
Creates a Tensor from a numpy.ndarray.
从numpy.ndarray创建一个张量。
The returned tensor and ndarray share the same memory. Modifications to the tensor will be reflected in the ndarray and vice versa. The returned tensor is not resizable.
返回的张量和ndarray共享相同的内存。
对张量的修改将反映在ndarray中,反之亦然。 返回的张量不可调整大小。
Example:
>>> a = numpy.array([1, 2, 3])
>>> t = torch.from_numpy(a)
>>> t
tensor([ 1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, 2, 3])
尊重原创:
https://blog.csdn.net/Dontla/article/details/105844900
本文详细介绍了如何使用PyTorch的torch.from_numpy()函数从NumPy数组创建张量,强调了数据共享机制及操作限制。通过示例展示了修改张量如何影响原始NumPy数组。

2471

被折叠的 条评论
为什么被折叠?



