實現隨機梯度下降算法遇到的問題

在學習Andrew Ng的課程的隨機梯度下降算法時,想着去實現一下,就自定了一個一次函數來進行嘗試:

y=1+2x

使用python3生成隨機數:

import pandas as pd
import numpy as np
import random
import matplotlib.pyplot as plt
X = np.random.randint(1,10,5)
Y = 1 + 2*X
data = pd.DataFrame([X,Y])
data = data.T
data.columns = ['x', 'y']
data['intercept'] = 1
X = data[['x','intercept']]
y = data[['y']]
X = X.as_matrix()
y = y.as_matrix()

本來自己寫了代碼,總是出現誤差無限擴大的情況,因此查找資料發現別人也是這麼寫的,參考:https://blog.csdn.net/kwame211/article/details/80364079完成後終於找到了誤差無限大的原因,參數初始值的設定,對結果極其重要。

1.theta初始值

2.alpha學習率

theta = [0,1]
loss = 10
alpha = 0.001
eps = 0.1
max_iters = 1000
error = 0
iter_count = 0
error_list = []
while (loss > eps and iter_count < max_iters):
    loss = 0
    i = random.randint(0,4)
    pred = theta[0]*X[i,0] + theta[1]*X[i][1]
    theta[0] = theta[0] - alpha*(pred - y[i])*X[i][0]
    theta[1] = theta[1] - alpha*(pred - y[i])*X[i][1]
    for i in range(3):
        pred = theta[0]*X[i][0] + theta[1]*X[i][1]
        error = 0.5 * (pred - y[i])**2
        loss = loss + error
    error_list.append(loss)
    iter_count += 1
    print('iter_count:',iter_count)
print('theta:',theta)
print('final_loss:',loss)
print('iters:',iter_count)

得到結果如下:

theta: [array([1.92490034]), array([1.28644738])]
final_loss: [0.09858994]
iters: 123

1.theta初始值的影響:

2.alpha初始值的影響,對於theta=[1,1]

可以發現受alpha的影響巨大,當設置爲0.05時就已經達不到設定的收斂條件了。當然最大迭代次數和設定的收斂條件eps同樣也影響着結果。

那麼梯度下降的學習率如何設定可以參照https://lumingdong.cn/setting-strategy-of-gradient-descent-learning-rate.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章