logo

Difference between torch.from_numpy() and torch.tensor() 📂Machine Learning

Difference between torch.from_numpy() and torch.tensor()

Explanation

To convert a NumPy array to a torch tensor, use torch.from_numpy() or torch.tensor(). First, from_numpy() shares memory with the NumPy array. In other words, it does not copy the data, so modifying the NumPy array will change the torch tensor, and vice versa.

>>> 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)

By contrast, tensor() copies the NumPy array into new memory. Changing the values of the NumPy array and the torch tensor will not affect each other.

# 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)