ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 파이썬 날코딩: 경사하강법(gradient descent), 선형 회귀
    ML 2023. 1. 21. 21:43
    반응형

    경사하강법은 가설 함수의 기울기(가중치)와 절편(편향)을 찾는 중 하나. 이를 옵티마이저라고 한다. 

    단순 선형 회귀

    순수 파이썬으로.. 

    x = (2, 4, 6, 8)
    y = (81, 93, 91, 97)
    
    a = b = 0
    lr = 0.03
    epochs = 2001
    
    for i in range(epochs):
        pred_y = [a * each_x + b for each_x in x]
        error = [each_y - each_pred_y for each_y, each_pred_y in zip(y, pred_y)]
    
        a_diff = 2 / len(x) * sum(-each_x * each_error for each_x, each_error in zip(x, error))
        b_diff = 2 / len(x) * sum(-each for each in error)
    
        a -= lr * a_diff  # a = a - lr * a_diff
        b -= lr * b_diff  # b = b - lr * b_diff
    
        if i % 100 == 0:
            print(i, a, b)
    
    final_pred_y = [a * each + b for each in x]
    print(final_pred_y)

    넘파이로

    import numpy as np
    
    x = np.array([2, 4, 6, 8])
    y = np.array([81, 93, 91, 97])
    
    a = b = 0
    lr = 0.03
    epochs = 2001
    
    for i in range(epochs):
        pred_y = a * x + b
        error = y - pred_y
    
        a_diff = 2 / len(x) * np.sum(-x * error)
        b_diff = 2 / len(x) * np.sum(-error)
    
        a -= lr * a_diff
        b -= lr * b_diff
    
        if i % 100 == 0:
            print(i, a, b)
    
    final_pred_y = a * x + b
    print(final_pred_y)

    텐서 플로 / 케라스

    import matplotlib.pyplot as plt
    import numpy as np
    from tensorflow import keras
    
    x = np.array([2, 4, 6, 8])
    y = np.array([81, 93, 91, 97])
    
    model = keras.models.Sequential()
    
    model.add(keras.layers.Dense(1, input_dim=1, activation='linear'))
    model.compile(optimizer='sgd', loss='mse')
    model.fit(x, y, epochs=2001)
    
    plt.scatter(x, y)
    plt.plot(x, model.predict(x), 'r')
    plt.show()
    
    print(model.predict([7]))
    반응형
Designed by Tistory.