pytorch 튜토리얼 [1]

·2023년 7월 3일
0

Pytorch 튜토리얼

목록 보기
1/4

tensor는 배열(array)이나 행렬(matrix)와 매우 유사한 특수한 자료구조

pytorch는 tensor 사용해 모델 input, output, 그리고 모델 매개변수들의 부호화(encode)한다.

tensor는 GPU, 다른 하드웨어 가속기에서 실행할 수 있는 점 제외하면
numpy의 ndarray와 유사
실제로 텐서와 numpy 배열은 종종 동일한내부 메모리 공유할 수 있어 데이터 복사할 필요 없다.
tensor는 자동 미분에 최적화되어 있다.

tensor 초기화

tensor 여러 방법으로 초기화 가능

data로부터 직접 생성

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

numpy 배열로부터 생성하기

tensor는 numpy 배열로 생성할 수 있다 ( 그 반대도 가능하다 )

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

다른 tensor로부터 생성하기

명시적으로 재정의(override)하지 않는다면, 인자로 주어진 tensor의 속성
(모양(shape), 자료형(datatype)) 을 유지한다.

x_ones = torch.ones_like(x_data) # x_data의 속성을 유지합니다.
print(f"Ones Tensor: \n {x_ones} \n")

x_rand = torch.rand_like(x_data, dtype=torch.float) # x_data의 속성을 덮어씁니다.
print(f"Random Tensor: \n {x_rand} \n")

Out:

Ones Tensor:
 tensor([[1, 1],
        [1, 1]])

Random Tensor:
 tensor([[0.9920, 0.3355],
        [0.8188, 0.6083]])

random 또는 constant 값 사용

shape는 tensor의 차원(dimension)을 나타내는 tuple로,
아래 함수들에서 출력 tensor의 차원을 결정한다.

shape = (2,3,)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)

print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor}")

Out:

Random Tensor:
 tensor([[0.2851, 0.4862, 0.0144],
        [0.4129, 0.0634, 0.8218]])

Ones Tensor:
 tensor([[1., 1., 1.],
        [1., 1., 1.]])

Zeros Tensor:
 tensor([[0., 0., 0.],
        [0., 0., 0.]])

tensor의 속성 (Attribute)

tensor의 속성은 tensor의 모양, 자료형 및 어느 장치에 저장되는지 나타난다.

tensor = torch.rand(3,4)

print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")

Out:

Shape of tensor: torch.Size([3, 4])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu

tensor 연산 (Operation)

전치(transposing), 인덱싱, 슬라이싱, 수학 계산, 선형 대수, 임의 샘플링 등
100가지 이상의 tensor 연산들 가능
기본적으로 cpu에 있지만 gpu에서도 실행 가능하다

# GPU가 존재하면 텐서를 이동합니다
if torch.cuda.is_available():
    tensor = tensor.to("cuda")

numpy식의 표준 인덱싱과 슬라이싱

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

Out:

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)

# 두 텐서 간의 행렬 곱(matrix multiplication)을 계산합니다. y1, y2, y3은 모두 같은 값을 갖습니다.
# ``tensor.T`` 는 텐서의 전치(transpose)를 반환합니다.
y1 = tensor @ tensor.T
y2 = tensor.matmul(tensor.T)

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


# 요소별 곱(element-wise product)을 계산합니다. z1, z2, z3는 모두 같은 값을 갖습니다.
z1 = tensor * tensor
z2 = tensor.mul(tensor)

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

Out:

tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]])

단일-요소(single-element) tensor

tensor의 모든 값을 하나로 집계(aggregate)해 요소가 하나인 tensor 경우
item() 사용해 Python 숫자 값으로 변환할 수 있다.

agg = tensor.sum()
agg_item = agg.item()
print(agg_item, type(agg_item))

Out:

12.0<class 'float'>

바꿔치기(in-place) 연산

연산 결과 피연산자에 저장하는 연산을 바꿔치기 연산이라고 부르고
접미사를 가진다.
예를 들면 x.copy
(y) 나 x.t_() 는 x를 변경한다

print(f"{tensor} \n")
tensor.add_(5)
print(tensor)

Out:

tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]])

tensor([[6., 5., 6., 6.],
        [6., 5., 6., 6.],
        [6., 5., 6., 6.],
        [6., 5., 6., 6.]])
  • 바꿔치기 연산 메모리 일부 절약하지만, 기록 즉시 삭제되 도함수 계산에 문제 발생할 수 있어
    사용 권장하지 않는다.

numpy 변환(Bridge)

cpu상 tensor와 numpy 배열 메모리 공간 공유하기 때문에, 하나를 변경하면 다른 하나도 변경됨

tensor를 numpy 배열로 변환

t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")

Out:

t: tensor([1., 1., 1., 1., 1.])
n: [1. 1. 1. 1. 1.]

tensor의 변경 사항이 numpy 배열에 반영된다.

t.add_(1)
print(f"t: {t}")
print(f"n: {n}")

Out:

t: tensor([2., 2., 2., 2., 2.])
n: [2. 2. 2. 2. 2.]

numpy 배열을 tensor로 변환하기

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

numpy 배열의 변경 사항이 tensor에 반영된다.

np.add(n, 1, out=n)
print(f"t: {t}")
print(f"n: {n}")

Out:

t: tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
n: [2. 2. 2. 2. 2.]

출처
https://tutorials.pytorch.kr/beginner/basics/tensorqs_tutorial.html

0개의 댓글