logo

torch.from_numpy()とtorch.tensor()の違い 📂機械学習

torch.from_numpy()とtorch.tensor()の違い

説明

NumPy配列をtorchテンソルに変換する方法は torch.from_numpy()torch.tensor() を使う。まず from_numpy() はNumPy配列とメモリを共有する。つまりコピーしないのでNumPy配列を変えるとtorchテンソルも変わり、その逆も同様だ。

>>> import torch
>>> import numpy as np

# from_numpy()로 생성한 텐서와 넘파이 배열의 메모리 주소가 같음
>>> a = np.random.randn(6)
>>> hex(a.ctypes.data)
'0x1e1487700b0'
>>> b = torch.from_numpy(a)
>>> hex(b.data_ptr())
'0x1e1487700b0'

# a를 바꾸면 b로 바뀜
>>> a[0:3] = 0
>>> b
tensor([ 0.0000,  0.0000,  0.0000, -0.7667,  1.0462,  1.0083],
       dtype=torch.float64)

一方で tensor() はNumPy配列をコピーして新しいメモリに割り当てる。NumPy配列とtorchテンソルそれぞれの値を変更しても互いに影響しない。

# tensor()로 생성한 텐서는 넘파이 배열과 주소가 다름
>>> a = np.random.randn(6)
>>> hex(a.ctypes.data)
'0x1e148770070'
>>> c = torch.tensor(a)
>>> hex(c.data_ptr())
'0x4ad40420480'

# a를 바꿔도 c가 바뀌지 않음
>>> a[0:3] = 0
>>> c
tensor([-0.2374,  1.5200, -0.5507, -2.3992,  1.2971, -0.6234],
       dtype=torch.float64)