신경망 기본 이해 단계에서는 퍼셉트론 → 다층 퍼셉트론 → 활성화 함수 → 손실 함수 → 역전파로 이어지는 흐름을 다루면 됩니다. 이것이 딥러닝의 뼈대 구조입니다.
퍼셉트론(Perceptron): 단순 신경망
→ 입력에 가중치를 곱하고 합산 후, 임계값을 넘으면 1, 아니면 0을 출력합니다.
👉 예: 전등 스위치처럼 AND/OR 논리 게이트 구현 가능
다층 퍼셉트론(MLP: Multi-Layer Perceptron): 층을 여러 개 쌓은 신경망
→ 입력층(Input), 은닉층(Hidden), 출력층(Output)으로 구성됩니다.
👉 예: 손글씨 숫자 인식 (0~9)
활성화 함수(Activation Function): 비선형성을 넣는 장치
→ 단순 직선 모델이 아닌, 복잡한 패턴도 학습할 수 있게 만듭니다.
👉 예: ReLU(음수는 0, 양수는 그대로), Sigmoid(0~1 사이 값), Tanh(-1~1 사이 값)
손실 함수(Loss Function): 모델의 오차 측정 도구
→ 예측값과 실제값 차이를 수치화합니다.
👉 예: MSE(연속값 예측), Cross-Entropy(분류 문제)
역전파(Backpropagation): 학습 메커니즘
→ 오차를 뒤로 전달해 가중치를 수정하는 과정입니다.
👉 경사하강법(Gradient Descent)을 사용해 오차가 줄어드는 방향으로 학습
import numpy as np
from tensorflow.keras import models, layers
# XOR 데이터셋
X = np.array([[0,0],[0,1],[1,0],[1,1]])
y = np.array([0,1,1,0]) # XOR 결과
# 모델 생성
model = models.Sequential([
layers.Dense(4, input_dim=2, activation='relu'), # 은닉층
layers.Dense(1, activation='sigmoid') # 출력층
])
# 모델 컴파일
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# 학습
model.fit(X, y, epochs=100, verbose=0)
# 평가
print("정확도:", model.evaluate(X, y, verbose=0)[1])
# 예측
print("예측 결과:", model.predict(X).round())
graph LR
subgraph "입력층"
X1["x1 (float)"]
X2["x2 (float)"]
end
subgraph "은닉층(ReLU, 4)"
H1["뉴런1"]
H2["뉴런2"]
H3["뉴런3"]
H4["뉴런4"]
end
subgraph "출력층(Sigmoid, 1)"
Yhat["ŷ (0~1 확률)"]
end
X1 --> H1
X1 --> H2
X1 --> H3
X1 --> H4
X2 --> H1
X2 --> H2
X2 --> H3
X2 --> H4
H1 --> Yhat
H2 --> Yhat
H3 --> Yhat
H4 --> Yhat
style X1 fill:#e1f5ff,stroke:#0288d1,stroke-width:2px,color:#000
style X2 fill:#e1f5ff,stroke:#0288d1,stroke-width:2px,color:#000
style H1 fill:#fff8e1,stroke:#f9a825,stroke-width:2px,color:#000
style H2 fill:#fff8e1,stroke:#f9a825,stroke-width:2px,color:#000
style H3 fill:#fff8e1,stroke:#f9a825,stroke-width:2px,color:#000
style H4 fill:#fff8e1,stroke:#f9a825,stroke-width:2px,color:#000
style Yhat fill:#e8f5e9,stroke:#43a047,stroke-width:2px,color:#000