์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- ๊ตํํ์
- anaconda ๊ฐ์ํ๊ฒฝ
- Machine learning
- Python
- ํน๋ณ ๋ฉ์๋
- fatigue fracture
- ๋ฅ๋ฌ๋
- special method
- fluent python
- ์ ๋ฝ ๊ตํํ์
- set add
- set method
- ์ค์คํธ๋ฆฌ์
- Linear Regression
- ์ฒญ์ถ ํ์ดํ
- gradient descent
- ๋ฏธ๋์์ ํด์ธ๊ตํ
- cost function
- m1 anaconda ์ค์น
- ์๊ทน์ฌ
- ์ ํํ๊ท
- ์ด์ฐจ์ ์ง
- ๋์23์ด
- Deeplearning
- electrochemical models
- li-ion
- 2022๋
- ๋ฏธ๋์์ ์ฅํ์
- ์ ๋ฝ
- Andrew ng
- Today
- Total
Done is Better Than Perfect
[๋ฅ๋ฌ๋] 2. Backpropagation์ ํ์ต ๋ณธ๋ฌธ
๋ฅ๋ฌ๋ ๋ชจ๋ธ์์์ ํ์ต : loss function์ ์ต์ํํ๊ธฐ ์ํด ์ต์ ํ ์๊ณ ๋ฆฌ์ฆ (gradient descent ๋ฑ) ์ฌ์ฉ
- ๋ฅ๋ฌ๋์์๋ ์ญ์ ํ(backpropagation)์ ํตํด ๊ฐ ๊ฐ์ค์น๋ค์ ๊ธฐ์ธ๊ธฐ๋ฅผ ๊ตฌํ ์ ์์
- backpropagation ์ ์ : ๋ชฉํ target ๊ฐ๊ณผ ์ค์ ๋ชจ๋ธ์ด ์์ธกํ output ๊ฐ์ด ์ผ๋ง๋ ์ฐจ์ด๋๋์ง ๊ตฌํ ํ, ์ค์ฐจ ๊ฐ์ ๋ค์ ๋ค๋ก ์ ํํด๊ฐ๋ฉฐ ๋ณ์๋ค์ ๊ฐฑ์ ํ๋ ์๊ณ ๋ฆฌ์ฆ
1. Backpropagation์ ํ์ต ๋จ๊ณ
backpropataion์ ์ฒด์ธ ๋ฃฐ(chain rule)์ ์ฌ์ฉํ์ฌ ์์ค ํจ์์ ๊ธฐ์ธ๊ธฐ๋ฅผ ๊ณ์ฐํ๊ณ , ์ด๋ฅผ ํตํด ๊ฐ์ค์น๋ฅผ ์ ๋ฐ์ดํธ
1. Forward Propagation
- ์ ๋ ฅ ๋ฐ์ดํฐ๊ฐ ์ ๊ฒฝ๋ง์ ํต๊ณผํ๋ฉด์ ๊ฐ ์ธต์์์ ์ถ๋ ฅ ๊ณ์ฐ
- ๊ฐ ๋ด๋ฐ์ ์ถ๋ ฅ์ ๋ค์๊ณผ ๊ฐ์ด ๊ณ์ฐ๋จ :
$$ z=W⋅x+b $$
$$ a=σ(z) $$
์ฌ๊ธฐ์ $ W $๋ ๊ฐ์ค์น, $ x $๋ ์ ๋ ฅ, $ b $๋ ๋ฐ์ด์ด์ค, $ \sigma $๋ ํ์ฑํ ํจ์.
2. Loss Calculation (์์ค ํจ์ ๊ณ์ฐ)
- ์ถ๋ ฅ์ธต์์ ๋์จ ์์ธก๊ฐ๊ณผ ์ค์ ๋ ์ด๋ธ์ ๋น๊ตํ์ฌ ์์ค ํจ์ ๊ณ์ฐ
- ์๋ฅผ ๋ค์ด, ํ๊ท ์ ๊ณฑ ์ค์ฐจ(MSE)์ ๊ฒฝ์ฐ :
$$ L=\frac{1}{n}(y−\hat{y})^2 $$
์ฌ๊ธฐ์ $ y $๋ ์ค์ ๊ฐ, $ \hat{y} $๋ ์์ธก๊ฐ.
3. ์ถ๋ ฅ์ธต์์์ ๊ธฐ์ธ๊ธฐ ๊ณ์ฐ
- ์์ค ํจ์ $ L $์ ๋ํ ์ถ๋ ฅ์ธต์ ๊ฐ์ค์น $ W $์ ๊ธฐ์ธ๊ธฐ ๊ณ์ฐ
- ์ถ๋ ฅ์ธต์ ํ์ฑํ ํจ์๊ฐ $ \sigma $์ผ ๋, ์ถ๋ ฅ์ธต์ ์์ค ํจ์์ ๋ํ ๊ธฐ์ธ๊ธฐ๋ ๋ค์๊ณผ ๊ฐ์ด ๊ณ์ฐ:
$$ \frac{\partial L}{\partial \hat{y}} = \hat{y} - y $$
$$ \frac{\partial \hat{y}}{\partial z} = \sigma'(z) $$
$$ \frac{\partial z}{\partial W} = a_{\text{previous}} $$
์ฌ๊ธฐ์ $ a_{\text{previous}} $๋ ์ด์ ์ธต์ ํ์ฑํ ๊ฐ.
4. ์ญ์ ํ(Backpropagation) ๋จ๊ณ
- ์ถ๋ ฅ์ธต์์ ๊ณ์ฐ๋ ๊ธฐ์ธ๊ธฐ๋ฅผ ์ด์ ์ธต์ผ๋ก ์ ํ
- ์ฒด์ธ ๋ฃฐ์ ์ฌ์ฉํ์ฌ ์ด์ ์ธต์ ๊ฐ์ค์น์ ๋ํ ๊ธฐ์ธ๊ธฐ๋ฅผ ๊ณ์ฐํจ:
$$ \frac{\partial L}{\partial W} = \frac{\partial L}{\partial \hat{y}} \cdot \frac{\partial \hat{y}}{\partial z} \cdot \frac{\partial z}{\partial W} $$
- ์ด๋ฅผ ๋ฐ๋ณตํ์ฌ ๊ฐ ์ธต์ ๊ฐ์ค์น์ ๋ํ ๊ธฐ์ธ๊ธฐ ๊ณ์ฐ. ๊ฐ ์ค๊ฐ ์ธต $ l $์์ ๊ธฐ์ธ๊ธฐ๋ ๋ค์๊ณผ ๊ฐ์ด ๊ณ์ฐ๋จ:
$$ \delta^l = (\delta^{l+1} \cdot W^{l+1}) \odot \sigma'(z^l) $$
์ฌ๊ธฐ์ $ \delta^l $ ๋ ์ธต $ l $์์์ ์ค์ฐจ, $ \delta^{l+1} $ ๋ ๋ค์ ์ธต์ ์ค์ฐจ, $ W^{l+1} $๋ ๋ค์ ์ธต์ ๊ฐ์ค์น ํ๋ ฌ, $ \odot $ ๋ ์์๋ณ ๊ณฑ(element-wise product).
5. ๊ฐ์ค์น ์ ๋ฐ์ดํธ:
- ๊ฐ ์ธต์ ๊ฐ์ค์น์ ๋ฐ์ด์ด์ค๋ฅผ ๊ธฐ์ธ๊ธฐ์ ํ์ต๋ฅ $ \eta $๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋ฐ์ดํธํจ:
$$ W := W - \eta \frac{\partial L}{\partial W} $$
$$b := b - \eta \frac{\partial L}{\partial b} $$
์ด ๊ณผ์ ์ ๋ชจ๋ ๋ฐ์ดํฐ์ ๋ํด ๋ฐ๋ณตํ์ฌ ๊ฐ์ค์น์ ๋ฐ์ด์ด์ค๋ฅผ ์ ์ง์ ์ผ๋ก ์กฐ์ ํจ์ผ๋ก์จ ๋ชจ๋ธ์ ์ฑ๋ฅ์ ํฅ์์ํด.
๋ฅ๋ฌ๋ ํ๋ ์์ํฌ๋ ์ด ๋ณต์กํ ๊ณผ์ ์ ์๋์ผ๋ก ์ฒ๋ฆฌํด์ฃผ๊ธฐ ๋๋ฌธ์, ์๋์ ์ผ๋ก ์ฝ๊ฒ ๋ชจ๋ธ์ ํ์ตํ ์ ์์.
์ ์ backpropagation ๊ณผ์ ์ ์ฝ๋๋ก ํํํ๋ฉด,
(์์ ๋ 2์ธต ์ ๊ฒฝ๋ง(1๊ฐ์ ์๋์ธต๊ณผ 1๊ฐ์ ์ถ๋ ฅ์ธต)์ผ๋ก ๊ตฌ์ฑ, ํ์ฑํ ํจ์๋ก ์๊ทธ๋ชจ์ด๋ ํจ์๋ฅผ ์ฌ์ฉํ๊ณ , ์์ค ํจ์๋ก ํ๊ท ์ ๊ณฑ ์ค์ฐจ(MSE)๋ฅผ ์ฌ์ฉํจ)
* backward ํจ์์ ๊ฐ์ค์น์ ๋ฐ์ด์ด์ค ์ ๋ฐ์ดํธ ๋ถ๋ถ ์ค์ ์ ์ผ๋ก ๋ณด๊ธฐ!
import numpy as np
# ํ์ฑํ ํจ์(sigmmoid)์ ๊ทธ ๋ฏธ๋ถ ๋ํจ์
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
# ์์ค ํจ์(MSE)์ ๊ทธ ๋ฏธ๋ถ ๋ํจ์
def mean_squared_error(y_true, y_pred):
return np.mean((y_true - y_pred) ** 2)
def mean_squared_error_derivative(y_true, y_pred):
return y_pred - y_true
# ์ ๊ฒฝ๋ง ํด๋์ค ์ ์
class SimpleNeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
# ๊ฐ์ค์น์ ๋ฐ์ด์ด์ค ์ด๊ธฐํ
self.weights_input_hidden = np.random.randn(input_size, hidden_size)
self.bias_hidden = np.zeros((1, hidden_size))
self.weights_hidden_output = np.random.randn(hidden_size, output_size)
self.bias_output = np.zeros((1, output_size))
def forward(self, X):
# ์ ๋ฐฉ ์ ๋ฌ (forward propagation)
self.hidden_input = np.dot(X, self.weights_input_hidden) + self.bias_hidden
self.hidden_output = sigmoid(self.hidden_input)
self.output_input = np.dot(self.hidden_output, self.weights_hidden_output) + self.bias_output
self.output = sigmoid(self.output_input)
return self.output
def backward(self, X, y, output):
# ์ถ๋ ฅ์ธต์ ์ค์ฐจ ๊ณ์ฐ
error = mean_squared_error_derivative(y, output)
d_output = error * sigmoid_derivative(output)
# ์๋์ธต์ ์ค์ฐจ ๊ณ์ฐ
error_hidden = d_output.dot(self.weights_hidden_output.T)
d_hidden = error_hidden * sigmoid_derivative(self.hidden_output)
# ๊ฐ์ค์น์ ๋ฐ์ด์ด์ค ์
๋ฐ์ดํธ
self.weights_hidden_output -= self.hidden_output.T.dot(d_output) * learning_rate
self.bias_output -= np.sum(d_output, axis=0, keepdims=True) * learning_rate
self.weights_input_hidden -= X.T.dot(d_hidden) * learning_rate
self.bias_hidden -= np.sum(d_hidden, axis=0, keepdims=True) * learning_rate
def train(self, X, y, epochs, learning_rate):
for epoch in range(epochs):
output = self.forward(X)
self.backward(X, y, output)
if epoch % 100 == 0:
loss = mean_squared_error(y, output)
print(f'Epoch {epoch}, Loss: {loss}')
# ๋ฐ์ดํฐ ์์ฑ (์: XOR ๊ฒ์ดํธ)
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
# ์ ๊ฒฝ๋ง ์ด๊ธฐํ
input_size = 2
hidden_size = 2
output_size = 1
learning_rate = 0.1
epochs = 10000
nn = SimpleNeuralNetwork(input_size, hidden_size, output_size)
# ์ ๊ฒฝ๋ง ํ์ต
nn.train(X, y, epochs, learning_rate)
# ์์ธก ๊ฒฐ๊ณผ ์ถ๋ ฅ
print("Predictions:")
print(nn.forward(X))