딥러닝 개요
인공지능은 크게 세 가지 발전 단계를 거쳤습니다. 초기에는 심볼릭 AI가 전문가 시스템으로 주류를 이루었으며, 이후 통계적 방법론이 발전했습니다. 2010년대 이후 신경망과 딥러닝이 부상하며 현재는 대규모 사전 학습 모델 시대에 접어들었습니다.
주요 딥러닝 프레임워크
TensorFlow, PyTorch, PaddlePaddle 등 다양한 프레임워크가 존재합니다. PyTorch는 Python 기반으로 동적 그래프를 지원하며 연구 커뮤니티에서 널리 사용됩니다. 직관적인 API와 풍부한 문서화로 학습 및 프로토타이핑에 적합합니다.
PyTorch 환경 설정
가상 환경 생성 및 패키지 설치 방법:
conda create -n dl_env python=3.9
conda activate dl_env
pip install torch torchvision torchaudio
CUDA 지원 GPU가 있는 경우:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
텐서 기본 연산
텐서는 PyTorch의 핵심 데이터 구조로, 다양한 차원을 가질 수 있습니다:
import torch
# 다양한 차원의 텐서 생성
scalar = torch.tensor(7) # 0차원
vector = torch.tensor([1, 2, 3]) # 1차원
matrix = torch.tensor([[1, 2], [3, 4]]) # 2차원
텐서 생성 및 변환:
# 특정 값으로 채운 텐서
ones_tensor = torch.ones(2, 3)
zeros_tensor = torch.zeros(2, 3)
full_tensor = torch.full((2, 3), 5)
# 데이터 타입 변환
float_tensor = full_tensor.float()
int_tensor = full_tensor.int()
텐서 연산
기본 산술 연산:
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
add_result = a + b # 덧셈
mul_result = a * b # 요소별 곱셈
matmul_result = a @ b # 행렬 곱셈
형상 조작:
data = torch.randn(2, 3, 4)
reshaped = data.reshape(6, 4) # 형태 변경
transposed = data.transpose(1, 2) # 차원 교환
squeezed = data.squeeze() # 단일 차원 제거
자동 미분
PyTorch의 autograd 시스템은 역전파를 자동으로 처리합니다:
x = torch.tensor(2.0, requires_grad=True)
y = x ** 2 + 3 * x + 1
y.backward()
print(x.grad) # dy/dx = 2x + 3 → 7
NumPy 호환성
PyTorch 텐서와 NumPy 배열 간 변환:
import numpy as np
numpy_array = np.array([1, 2, 3])
torch_tensor = torch.from_numpy(numpy_array)
back_to_numpy = torch_tensor.numpy()