크크루쿠쿠

DL02. Gradient Descent 본문

DeepLearning/공부

DL02. Gradient Descent

JH_KIM 2021. 1. 6. 23:21

What is the learning?

 

 

loss(MSE)를 최소화하는 w를 찾는것!

 

그렇담 어떻게 찾아야할까?

 

Gradient Descent algorithm

 

편미분을 사용한다!

편미분을 사용해서 w값을 점점더 loss의 minimum값으로 이동시켜줌

 

이때 편미분값을 이용하여 한번 이동시 얼만큼 이동하느냐? 에 사용되는 parameter인 알파 즉 learning rate가 사용된다.

 

 

Code

 

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
31
32
33
34
35
36
37
38
39
40
41
# Training Data
x_data = [1.02.03.0]
y_data = [2.04.06.0]
 
= 1.0  # a random guess: random value
 
 
# our model forward pass
def forward(x):
    return x * w
 
 
# Loss function
def loss(x, y):
    y_pred = forward(x)
    return (y_pred - y) * (y_pred - y)
 
 
# compute gradient
def gradient(x, y):  # d_loss/d_w
    return 2 * x * (x * w - y)
 
 
# Before training
print("Prediction (before training)",  4, forward(4))
 
# Training loop
for epoch in range(10):
    for x_val, y_val in zip(x_data, y_data):
        # Compute derivative w.r.t to the learned weights
        # Update the weights
        # Compute the loss and print progress
        grad = gradient(x_val, y_val)
        w = w - 0.01 * grad
        print("\tgrad: ", x_val, y_val, round(grad, 2))
        l = loss(x_val, y_val)
    print("progress:", epoch, "w=", round(w, 2), "loss=", round(l, 2))
 
# After training
print("Predicted score (after training)",  "4 hours of studying: ", forward(4))
 
cs

결과값

 

 

 

 

 

'DeepLearning > 공부' 카테고리의 다른 글

Prompt Engineering Guide 1. Introduction  (0) 2023.03.27
NLP 01.Text Classification  (0) 2021.01.21
DL04. Linear Regression in the PyTorch way  (0) 2021.01.13
DL03. Back-propagation 역전파  (0) 2021.01.09
DL01.Linear Model  (0) 2021.01.06
Comments