
- 학습률 (Learning rate)
- 기울기에 따라 이동할 step의 크기. 경사하강법 알고리즘에서 지정해야하는 하이퍼 파라미터이다.
- 학습률을 너무 작게 잡으면 최소값에 수렴하기 위해 많은 반복을 진행해야해 시간이 오래걸린다.
- 학습률을 너무 크게 잡으면 왔다 갔다 하다가 오히려 더 큰 값으로 발산하여 최소값에 수렴하지 못하게 된다.
# 가상의 loss 함수 def loss(weight): return (weight-1)**2 + 2 # 위의 loss함수의 도함수 def derived_loss(weight): return 2*(weight-1) # 기울기 print('w=1, 오차:', loss(1)) print('w=1, 기울기:', derived_loss(1))w=1, 오차: 2
w=1, 기울기: 0loss(5-8*0.01), derived_loss(5-8*0.01)(17.3664, 7.84)
#초기 weight weight = 5 # 학습율 lr = 0.1 new_weight = weight - lr*derived_loss(weight) new_weight반복문을 이용해 gradient가 0이 되는 지점의 weight 찾기
import numpy as np np.random.seed(0) # learning_rate = 0.001 # learning_rate = 10 learning_rate=0.4 # 죄적의 weight를 찾기 위한 최대 반복횟수. max_iter = 100 # 첫번째(시작) weight => random하게 잡는다. weight = np.random.randint(-2, 3) weight_list = [weight] # 새로 계산된 weight들을 저장할 리스트 iter_cnt = 0 # 반복회수를 저장할 변수 while True : # loss함수에 대한 미분값(기울기)을 구해서 0(최소지점)이면 반복을 멈춘다. if derived_loss(weight) == 0 : break if iter_cnt == max_iter : # 현재 반복수가 max_iter이면 멈춘다. break # 새로운 weight값을 계산 weight = weight - learning_rate * derived_loss(weight) weight_list.append(weight) iter_cnt += 1iter_cnt23
weight1.0
loss(weight)2.0
weight_list[2,
1.2,
1.04,
1.008,
1.0016,
1.00032,
1.000064,
1.0000128,
1.00000256,
1.000000512,
1.0000001024,
1.00000002048,
1.000000004096,
1.0000000008192,
1.00000000016384,
1.000000000032768,
1.0000000000065536,
1.0000000000013107,
1.0000000000002622,
1.0000000000000524,
1.0000000000000104,
1.000000000000002,
1.0000000000000004,
1.0]