개발/AI

[Python] GBL : Gradient-based Learning

jykim23 2023. 11. 13. 18:59

GBL 학습을 위해 파이썬으로 구현한다.

그리고 update 횟수, Learning rate, gradient(기울기) 등 변경하면서 그려지는 그래프를 보며 이해하면 도움이 된다.

 

 

변수가 하나일 경우

import numpy as np


def f(x, grad): return grad * ((x)**2)
def df_dx(x, grad): return 2 * grad *(x)

gradient = [1/10, 1/8, 1/6] # 일반적인 기울기
gradient = [1/10, 1.1, 2] # 발산하는 케이스
ITERATIONS = 20 # 총 n번의 update를 할 것
lr = 0.3 # learning rate

import matplotlib.pyplot as plt
fig, axes = plt.subplots(len(gradient)*2, 1, figsize=(10,20))


for idx, grade in enumerate(gradient):
    x = 3 # 초깃값 x를 n으로 설정
    x_track, y_track = [x], [f(x, grade)] # 업데이트되는 x, y를 저장하기 위한 list
    print(f'Initial x: {x}')
    for iter in range(ITERATIONS):
        dy_dx = df_dx(x, grade)
        x = x - lr * dy_dx # learning rate 적용

        x_track.append(x)
        y_track.append(f(x, grade))
        # print(f'{iter + 1}-th  x: {x:.4f}')

    function_x = np.linspace(-5, 5, 100)
    function_y = f(function_x, grade)

    axes[idx*2].plot(function_x, function_y)
    axes[idx*2].scatter(x_track, y_track, c=range(ITERATIONS + 1), cmap='rainbow')
    axes[idx*2].set_xlabel('x', fontsize=15)
    axes[idx*2].set_ylabel('y', fontsize=15)

    axes[idx*2+1].plot(x_track, marker='o')
    axes[idx*2+1].set_xlabel('Iteration', fontsize=15)
    axes[idx*2+1].set_ylabel('x', fontsize=15)


fig.tight_layout()

scatter 변화를 주시하자

 

 

 

 

변수가 두개일 경우

 

import numpy as np
import matplotlib.pyplot as plt
def f(x1, x2, grad): return ((grad*(x1))**2) + ((grad*(x2))**2)
def df_dx(x, grad): return grad * 2 * x

ITERATIONS = 20 # 총 n번의 update를 할 것
x1, x2 = -3.0, 1.0 # init x1, x2
grad = 1/6 # 계수 coefficient
lr = 0.3 # learning rate

x1_track, x2_track, y_track = [x1], [x2], [f(x1, x2, grad)]
print(f'Initial x: {x1}, {x2}')
for iter in range(ITERATIONS):
    dy_dx1 = df_dx(x1, grad)
    dy_dx2 = df_dx(x2, grad)
    x1 = x1 - lr * dy_dx1 # learning rate 적용
    x2 = x2 - lr * dy_dx2 # learning rate 적용

    x1_track.append(x1)
    x2_track.append(x2)
    y_track.append(f(x1, x2, grad))
    print(f'{iter + 1}-th  x1:x2 = {x1:.4f} : {x2:.4f}')

function_x1 = np.linspace(-5, 5, 100)
function_x2 = np.linspace(-5, 5, 100)
function_X1, function_X2 = np.meshgrid(function_x1, function_x2)
function_Y = np.log(f(function_X1, function_X2, grad))
# function_Y = (f(function_X1, function_X2))

fig, ax = plt.subplots(figsize=(7,7))
ax.contour(function_X1, function_X2, function_Y, levels=50, cmap='Reds_r')
ax.set_xlabel('x1', fontsize=15)
ax.set_ylabel('x2', fontsize=15)
ax.tick_params(labelsize=15)

ax.scatter(x1_track, x2_track)
fig.tight_layout()

등고선 사용

 

'개발 > AI' 카테고리의 다른 글

[Python] ANN : XOR - 2 layer 구현하기  (0) 2023.11.16
[Python] Affine, Sigmoid, BCELoss 구현  (0) 2023.11.14
[Python] Backpropagation  (1) 2023.11.13
[Python] Logic Gate 클래스 만들기  (0) 2023.11.07
[Python] Logic Gate 함수 구현 연습  (1) 2023.11.06