最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python+numpy+matplotalib實現(xiàn)梯度下降法

 更新時間:2018年08月31日 08:52:03   作者:Cludy_Sky  
這篇文章主要為大家詳細介紹了python+numpy+matplotalib實現(xiàn)梯度下降法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

這個階段一直在做和梯度一類算法相關(guān)的東西,索性在這兒做個匯總:

一、算法論述

梯度下降法(gradient  descent)別名最速下降法(曾經(jīng)我以為這是兩個不同的算法-.-),是用來求解無約束最優(yōu)化問題的一種常用算法。下面以求解線性回歸為題來敘述:

設(shè):一般的線性回歸方程(擬合函數(shù))為:(其中的值為1)

這一組向量參數(shù)選擇的好與壞就需要一個機制來評估,據(jù)此我們提出了其損失函數(shù)為(選擇均方誤差):

我們現(xiàn)在的目的就是使得損失函數(shù)取得最小值,即目標(biāo)函數(shù)為:

如果的值取到了0,意味著我們構(gòu)造出了極好的擬合函數(shù),也即選擇出了最好的值,但這基本是達不到的,我們只能使得其無限的接近于0,當(dāng)滿足一定精度時停止迭代。

那么問題來了如何調(diào)整使得取得的值越來越小呢?方法很多,此處以梯度下降法為例:

分為兩步:(1)初始化的值。

(2)改變的值,使得按梯度下降的方向減少。

值的更新使用如下的方式來完成:

其中為步長因子,這里我們?nèi)《ㄖ担⒁馊绻?img alt="" src="http://img.jbzj.com/file_images/article/201808/2018083108455312.gif" />取得過小會導(dǎo)致收斂速度過慢,過大則損失函數(shù)可能不會收斂,甚至逐漸變大,可以在下述的代碼中修改的值來進行驗證。后面我會再寫一篇關(guān)于隨機梯度下降法的文章,其實與梯度下降法最大的不同就在于一個求和符號。

二、代碼實現(xiàn)

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib import style
 
 
#構(gòu)造數(shù)據(jù)
def get_data(sample_num=10000):
 """
 擬合函數(shù)為
 y = 5*x1 + 7*x2
 :return:
 """
 x1 = np.linspace(0, 9, sample_num)
 x2 = np.linspace(4, 13, sample_num)
 x = np.concatenate(([x1], [x2]), axis=0).T
 y = np.dot(x, np.array([5, 7]).T) 
 return x, y
#梯度下降法
def GD(samples, y, step_size=0.01, max_iter_count=1000):
 """
 :param samples: 樣本
 :param y: 結(jié)果value
 :param step_size: 每一接迭代的步長
 :param max_iter_count: 最大的迭代次數(shù)
 :param batch_size: 隨機選取的相對于總樣本的大小
 :return:
 """
 #確定樣本數(shù)量以及變量的個數(shù)初始化theta值
 m, var = samples.shape
 theta = np.zeros(2)
 y = y.flatten()
 #進入循環(huán)內(nèi)
 print(samples)
 loss = 1
 iter_count = 0
 iter_list=[]
 loss_list=[]
 theta1=[]
 theta2=[]
 #當(dāng)損失精度大于0.01且迭代此時小于最大迭代次數(shù)時,進行
 while loss > 0.001 and iter_count < max_iter_count:
 loss = 0
 #梯度計算
 theta1.append(theta[0])
 theta2.append(theta[1])
 for i in range(m):
  h = np.dot(theta,samples[i].T) 
 #更新theta的值,需要的參量有:步長,梯度
  for j in range(len(theta)):
  theta[j] = theta[j] - step_size*(1/m)*(h - y[i])*samples[i,j]
 #計算總體的損失精度,等于各個樣本損失精度之和
 for i in range(m):
  h = np.dot(theta.T, samples[i])
  #每組樣本點損失的精度
  every_loss = (1/(var*m))*np.power((h - y[i]), 2)
  loss = loss + every_loss
 
 print("iter_count: ", iter_count, "the loss:", loss)
 
 iter_list.append(iter_count)
 loss_list.append(loss)
 
 iter_count += 1
 plt.plot(iter_list,loss_list)
 plt.xlabel("iter")
 plt.ylabel("loss")
 plt.show()
 return theta1,theta2,theta,loss_list
def painter3D(theta1,theta2,loss):
 style.use('ggplot')
 fig = plt.figure()
 ax1 = fig.add_subplot(111, projection='3d')
 x,y,z = theta1,theta2,loss
 ax1.plot_wireframe(x,y,z, rstride=5, cstride=5)
 ax1.set_xlabel("theta1")
 ax1.set_ylabel("theta2")
 ax1.set_zlabel("loss")
 plt.show()
def predict(x, theta):
 y = np.dot(theta, x.T)
 return y 
if __name__ == '__main__':
 samples, y = get_data()
 theta1,theta2,theta,loss_list = GD(samples, y)
 print(theta) # 會很接近[5, 7] 
 painter3D(theta1,theta2,loss_list)
 predict_y = predict(theta, [7,8])
 print(predict_y)

三、繪制的圖像如下:

迭代次數(shù)與損失精度間的關(guān)系圖如下:步長為0.01

變量與損失函數(shù)loss之間的關(guān)系:(從初始化之后會一步步收斂到loss滿足精度,之后、會變的穩(wěn)定下來)

下面我們來看一副當(dāng)步長因子變大后的圖像:步長因子為0.5(很明顯其收斂速度變緩了)

當(dāng)步長因子設(shè)置為1.8左右時,其損失值已經(jīng)開始震蕩

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

来凤县| 麻阳| 奉新县| 平湖市| 城口县| 京山县| 青川县| 老河口市| 贵港市| 垫江县| 高要市| 岑溪市| 江口县| 华亭县| 东安县| 福建省| 宁河县| 师宗县| 陆川县| 石首市| 和林格尔县| 安多县| 集安市| 四会市| 花莲县| 武汉市| 汉阴县| 锡林浩特市| 黔西县| 松桃| 安义县| 南充市| 宕昌县| 洪洞县| 永新县| 昭觉县| 石泉县| 吴桥县| 南平市| 张家界市| 皋兰县|