torch.from_numpy¶
- torch.from_numpy(ndarray) Tensor ¶
从
Tensor
从numpy.ndarray
创建。返回的 tensor 和
ndarray
共享相同的内存。对 tensor 的修改将反映在ndarray
中,反之亦然。返回的 tensor 不可调整大小。目前它接受 dtype 为
numpy.float64
,numpy.float32
,numpy.float16
,numpy.complex64
,numpy.complex128
,numpy.int64
,numpy.int32
,numpy.int16
,numpy.int8
,numpy.uint8
, 和bool
的ndarray
。警告
不支持写入从只读 NumPy 数组创建的 tensor,这将导致未定义的行为。
示例
>>> a = numpy.array([1, 2, 3]) >>> t = torch.from_numpy(a) >>> t tensor([ 1, 2, 3]) >>> t[0] = -1 >>> a array([-1, 2, 3])