[Boostcamp 2주차] PyTorch 기본

yoonene·2022년 1월 24일
0

Boostcamp AI Tech

목록 보기
7/27

PyTorch Operations

📌 핵심 정리

  • tensornumpy와 굉장히 비슷하다.
  • view와 reshape 중에 view를 써라.
  • squeeze는 dimention 축소, unsqueeze는 추가
  • 행렬곱셈 연산은 torch.mm
  • PyTorch는 AutoGrad(자동미분)을 지원한다.

Tensor

  • 다차원 Arrays를 표현하는 pyTorch 클래스
  • numpy의 ndarray와 사실상 동일
  • list나 ndarray를 사용해 생성 가능
# data -> tensor
data = [[0,1],[3,2]]
x = torch.tensor(data)

# array -> tensor
nd_array = np.array(data)
x = torch.from_numpy(nd_array)
  • tensor가 가질 수 있는 기본적인 데이터 타입은 numpy와 역시 동일.
  • 인덱싱 등 대부분의 numpy 사용법이 적용됨.
# tensor 생성
data = [[0,1,2],[3,4,5],[6,7,8]]
tensor_x = torch.tensor(data)

tensor_ex[1:]
# 결과 -> tensor([[3,4,5],[6,7,8]])
tensor_x[:2, 1:]
# tensor([[1,2],[4,5]])
tensor_x.flatten()
# tensor([1,2,3,4,5,6,7,8])
torch.ones_like(tensor_ex)
# tensor([[0,0,0],[0,0,0],[0,0,0]])
torch_x.numpy()
# array([[0,1,2],[3,4,5],[6,7,8]], dtype=int64)
torch_x.shape
# torch.Size([3,3])
torch_x.dtype
# torch.int64
  • PyTorch의 tensor를 GPU에 올리는 방법
# 현재 어디에 올라와있는지 확인
tensor_x.device

# GPU 사용 가능하면 GPU에 사용하기
if torch.cuda.is_available():
	tensor_x_cuda = tensor_x.to('cuda')

Tensor 조정하기

  • view : reshape와 동일, tensor의 shape 반환
# view
a = torch.zeros(4,2)
b = a.view(2,4)
a.fill_(1)
# b도 출력 시 1로 채워져있음.

# reshape
a = torch.zeros(4,2)
b = a.t().reshape(6)
a.fill_(1)
# b는 1로 채워지지 않고 그대로 0
  • squeeze : 차원의 개수가 1인 차원 압축 / unsqueeze : 차원의 개수가 1인 차원 추가
tensor_x = torch.rand(size=(3,3))
tensor_x.unsqueeze(0).shape
# torch.Size([1,3,3])
tensor_x.unsqueeze(1).shape
# torch.Size([3,1,3])
tensor_x.unsqueeze(2).shape
# torch.Size([3,3,1])

tensor_x = torch.rand(size=(3,1,3))
tensor_x.squeeze()
# torch.Size([3,3])

Tensor operations

  • 이또한 기본적으로 numpy와 동일
  • numpy는 행렬곱셈과 내적 모두 dot 함수 사용
    tensor는 내적은 dot, 행렬곱셈은 mm 함수 사용
    * matmul이라는 함수도 있는데, matmul은 broadcasting 지원하고 mm은 안함
  • nn.functional 모듈을 통해 softmax, argmax, one_hot 등 다양한 수식 지원

AutoGrad (자동미분)

  • PyTorch의 핵심
  • backward 함수 사용
w = torch.tensor(1.0, required_grad=True)	# 미분 대상에 required_grad=True로
y = w**2
z = 20*y + 1
z.backward()
w.grad
# 결과 -> 40
# 연쇄법칙

# 2차원 이상
a = torch.tensor([1, 2], requires_grad=True)
b = torch.tensor([4, 2], requires_grad=True)
Q = 2*a**3 - b**2
external_grad = torch.tensor([1, 1])
Q.backward(gradient=external_grad)
a.grad	# 6*a**2
# 결과 -> tensor([6,24])
b.grad	# -2*b
# tensor([-8., -4])
profile
NLP Researcher / Information Retrieval / Search

1개의 댓글

comment-user-thumbnail
2022년 1월 24일

pytorch grad에 대해서 더 학습하고 싶으시다면 cs224n 강의의 assignment2 의 Q1문제를 풀어보시는 것을 추천합니다!
https://github.com/cs231n/cs231n.github.io/tree/master/assignments/2020

답글 달기