torch.from_numpy#
- torch.from_numpy(ndarray) Tensor#
從
numpy.ndarray建立一個Tensor。返回的 tensor 和
ndarray共享同一塊記憶體。對 tensor 的修改會反映在ndarray中,反之亦然。返回的 tensor 不能調整大小。目前支援的
ndarray的 dtypes 包括:numpy.float64、numpy.float32、numpy.float16、numpy.complex64、numpy.complex128、numpy.int64、numpy.int32、numpy.int16、numpy.int8、numpy.uint8和bool。警告
從只讀 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])