pytorch torch.from_numpy接口

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

你可能感兴趣的:(PYTHON学习)