반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

경사하강법 본문

[AI]/python.Neural_Network

경사하강법

givemebro 2020. 5. 8. 09:21
반응형
import numpy as np
from sklearn.datasets import load_iris

# data 준비
iris=load_iris()
x = iris.data[:,2]
y = iris.data[:,3]

# 초기화
w = np.random.randn()/10
b = np.random.randn()/10

# learning rate 설정
lr = 0.05

pred_y = x*w + b
mse = np.mean((y-pred_y)**2)
costs = [mse]

for i in range(3000):
    pred_y = x*w + b
    dw = lr*2/len(y)*np.sum((y-pred_y)*x)
    db = lr*2/len(y)*np.sum(y-pred_y)
    
    w_new = w + dw
    b_new = b + db
    mse_new = np.mean((y-(x*w_new + b_new))**2)
    
    # 종료점
    if mse_new>=mse: break
        
    w = w_new
    b = b_new
    mse = mse_new
    costs.append(mse)
    
print(i, w, b, mse)
881 0.4164191208275526 -0.3665139932459091 0.04228994631948473

 

plt.plot(costs[:100])

(array([0.41641913]), -0.3665140452167275)
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x.reshape(-1,1), y)
model.coef_, model.intercept_
반응형

'[AI] > python.Neural_Network' 카테고리의 다른 글

다중 분류 원리  (0) 2020.05.08
중간층 만들기(분류)  (0) 2020.05.07
중간층 만들기(값예측)  (0) 2020.05.07
activation function(활성 함수)  (0) 2020.05.07
neural_network.logisticRegression.원리알기  (0) 2020.05.06
Comments