1. 程式人生 > >python利用梯度下降求多元線性迴歸

python利用梯度下降求多元線性迴歸

之前一直看Ng的課程,以為掌握了,結果自己動手實現發現問題很多。

多元線性迴歸
Y=WX
y=w0x0+w1x1+...+wnxn
:W:w0,w1,...wn
J(w0,w1,...wn)=12mmi=0(wixiyi)

# 批量創造一些資料
# y = w0*x0+w1*x1+w2*x2+w3*x3...wn*xn
# 我們要回歸的w先提前隨機生成
# 然後給出一些x,和w相乘後得到一些y,再加一些高斯噪聲
# 即 y=wx+noise
# data_num生成的資料量,weight_num w的維度
def loadDataSet
(data_num,weight_num):
x = np.random.random((data_num,weight_num)) w = np.random.random((weight_num,1)) mu, sigma = 0, 0.1 # 均值與標準差 noise = np.random.normal(mu, sigma, (data_num, 1)) y = x.dot(w)+ noise print 'groundtruth_weight:' print w return x, y

1. 批梯度下降

優化的代價函式是關於所有資料樣本的loss,計算整個樣本的loss後才更新權值,推導參考我的部落格

梯度下降法
W=W1m(XT(WXY)lr
wj=wj1mmi=0(wixiyi)xilr
m是所有樣本的數量和

# x:(data_num,weight_num)
# init_w:(weight_num,1)
# y:(data_num,1)
def bgd(x,init_w,y,iter_size,lr):
    start_time = timeit.default_timer()
    w = init_w
    m = x.shape[0]
    for i in range(iter_size):
        predict = x.dot(w) # predict:(data_num,1)
# x:(data_num,weight_num) x.T:(weight_num,data_num) y-predit:(data_num,1) # grad:(weight_num,1) grad = x.T.dot((predcit - y)) / m * lr w -= grad print w end_time = timeit.default_timer() print 'the time of cost: ',end_time - start_time

2. 隨機梯度下降

優化的代價函式是單個數據樣本的loss,計算每個樣本的loss後就立即更新權值
W=W1m(XT(WXY)lr
wj=wj1mmi=0(wixiyi)xilr
只是:m=1

# 隨機梯度下降
# x:(data_num,weight_num)
# init_w:(weight_num,1)
# y:(data_num,1)
def sgd(x, y, init_w, iter_size, lr):
    start_time = timeit.default_timer()
    w = init_w
    for i in range(iter_size):
        for j in range(x.shape[0]):
            temp_x = x[np.newaxis,j]
            temp_y = y[np.newaxis, j]
            predict = temp_x.dot(w)
            # x:(1,weight_num) x.T:(weight_num,1) y-predit:(1,1)
            # grad:(weight_num,1)
            grad = temp_x.T.dot((predict - temp_y)) / 1 * lr 
            w -= grad
    print w
    end_time = timeit.default_timer()
    print 'the time of cost: ', end_time - start_time

3. 小批量隨機梯度下降

優化的代價函式是每塊(batch_size)資料樣本的loss,計算每快樣本的loss後就更新權值
W=W1m(XT(WXY)lr
wj=wj1mmi=0(wixi