코딩/PyTorch

[Pytorch] Tensors

guungyul 2025. 1. 9. 23:22

Tensor란 array나 matrix와 매우 유사한 데이터 구조이다. Pytorch에서는 이 tensor들을 이용해 모델 input과 output 뿐만 아니라 parameters들도 encode 한다.

 

Tensor는 NumPy의 ndarrays와 매우 유사하다. 한 가지 다른 점은 tensor는 GPU나 하드웨어 가속기에서 돌아갈 수 있다는 것이다.

import torch
import numpy as np

 

Initializing a Tensor

From data

data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)

From NumPy array

np_array = np.array(data)
x_np = torch.from_numpy(np_array)

From another tensor

명시되지 않은 이상 기존 tensor의 특징을 그대로 가져간다 (shape, datatype).

x_ones = torch.ones_like(x_data)
# tensor([[1, 1], [1, 1]])

x_rand = torch.rand_like(x_data, dtype=torch.float)
# tensor([[0.8823, 0.9150], [0.2324, 0.9384]])

With random or constant values

shape tuple은 tensor의 차원을 정의하게 된다.

shape = (2, 3, )
rand_tensor = torch.rand(shape)
# tensor([[0.1343, 0.9382, 0.7732], [0.3872, 0.3847, 0.2239]])

ones_tensor = torch.ones(shape)
# tensor([[1., 1., 1.], [1., 1., 1.]])

zeros_tensor = torch.zeros(shape)
# tensor([[0., 0., 0.], [0., 0., 0.]])

 

Attributes of a Tensor

tensor = torch.rand(3,4)

print(f"Shape of tensor: {tensor.shape}")
# torch.Size([3, 4])

print(f"Datatype of tensor: {tensor.dtype}")
# torch.float32

print(f"Device tensor is stored on: {tensor.device}")k
# cpu

 

Operations on Tensors

기본적으로 tensor는 CPU에 생성된다.

Tensor를 GPU로 옮기기 위해서는 .to 함수를 사용해야 한다.

큰 tensor들을 옮기는 작업은 시간과 메모리 측면에서 매우 costy한 작업이다.

# GPU가 존재한다면 tensor를 이동
if torch.cuda.is_available():
    tensor = tensor.to("cuda")

Standard numpy-like indexing and slicing

tensor = torch.ones(4, 4)
print(f"First row: {tensor[0]}")
# tensor([1., 1., 1., 1.])

print(f"First column: {tensor[:, 0]}")
# tensor([1., 1., 1., 1.])

print(f"Last column: {tensor[..., -1]}")
# tensor([1., 1., 1., 1.])

tensor[:,1] = 0
print(tensor)
# tensor([[1., 0., 1., 1.],
#         [1., 0., 1., 1.],
#         [1., 0., 1., 1.],
#         [1., 0., 1., 1.]])

Joining tensors

torch.cat 함수를 사용해 주어진 차원으로 tensor를 합칠 수 있다.

torch.stack 함수는 tensor들을 새로운 차원으로 합친다.

t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)
# tensor([[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
#         [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
#         [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
#         [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.]])

Arithmetic operations

# Tensor들 간 matrix multiplication을 계산한다. y1, y2, y3은 모두 같은 값을 가지게 된다.
# ``tensor.T`` 는 tensor의 transpose된 값을 반환한다.
y1 = tensor @ tensor.T
y2 = tensor.matmul(tensor.T)

y3 = torch.rand_like(y1)
torch.matmul(tensor, tensor.T, out=y3)


# Tensor들 간 element-wise product를 계산한다. z1, z2, z3은 모두 같은 값을 가지게 된다.
z1 = tensor * tensor
z2 = tensor.mul(tensor)

z3 = torch.rand_like(tensor)
torch.mul(tensor, tensor, out=z3)

Single-element tensors

One-element tensor는 item() 함수를 사용해 Python 숫자로 바꿀 수 있다.

agg = tensor.sum()
agg_item = agg.item()
print(agg_item, type(agg_item))
# 12.0 <class 'float'>

In-place operations

"_"를 추가함으로써 in-place로 함수를 실행할 수 있다.

In-place로 함수를 실행시키면 메모리 사용량을 절약할 수 있다. 하지만 미분값을 구할때는 추천되지 않는다.

print(f"{tensor} \n")
# tensor([[1., 0., 1., 1.],
#         [1., 0., 1., 1.],
#         [1., 0., 1., 1.],
#         [1., 0., 1., 1.]])
tensor.add_(5)
print(tensor)
# tensor([[6., 5., 6., 6.],
#         [6., 5., 6., 6.],
#         [6., 5., 6., 6.],
#         [6., 5., 6., 6.]])

 

Bridge with NumPy

CPU에 있는 tensor들과 NumPy array들은 같은 메모리 위치를 공유할 수 있다.

따라서 하나를 변경하면 다른 하나도 같이 변경된다.

Tensor to NumPy array

t = torch.ones(5)
print(f"t: {t}")
# t: tensor([1., 1., 1., 1., 1.])

n = t.numpy()
print(f"n: {n}")
# n: [1. 1. 1. 1. 1.]

Tensor 값을 바꾸면 NumPy array 값도 바뀌는 것을 확인할 수 있다.

t.add_(1)
print(f"t: {t}")
# t: tensor([2., 2., 2., 2., 2.])

print(f"n: {n}")
# n: [2. 2. 2. 2. 2.]

NumPy array to Tensor

반대로 NumPy array 값을 바꾸어도 tensor 값이 바뀌는 것을 확인할 수 있다.

n = np.ones(5)
t = torch.from_numpy(n)

np.add(n, 1, out=n)
print(f"t: {t}")
# t: tensor([2., 2., 2., 2., 2.], dtype=torch.float64)

print(f"n: {n}")
# n: [2. 2. 2. 2. 2.]