torch.from_numpy¶
- torch.from_numpy(ndarray) Tensor¶
從
numpy.ndarray建立一個Tensor。返回的張量和
ndarray共享相同的記憶體。對張量的修改會反映到ndarray中,反之亦然。返回的張量不可調整大小。它目前接受 dtype 為
numpy.float64,numpy.float32,numpy.float16,numpy.complex64,numpy.complex128,numpy.int64,numpy.int32,numpy.int16,numpy.int8,numpy.uint8和bool的ndarray。警告
寫入從只讀 NumPy 陣列建立的張量不受支援,並將導致未定義行為。
示例
>>> a = numpy.array([1, 2, 3]) >>> t = torch.from_numpy(a) >>> t tensor([ 1, 2, 3]) >>> t[0] = -1 >>> a array([-1, 2, 3])