python實現隨機梯度下降法

2021-08-04 16:58:49 字數 2854 閱讀 8362

一、為什麼要提出隨機梯度下降演算法

也就是說每次更新權值

二、核心思想

對於權值的更新不再通過遍歷全部的資料集,而是選擇其中的乙個樣本即可(對於程式設計師來說你的第一反應一定是:在這裡需要乙個隨機函式來選擇乙個樣本,不是嗎?),一般來說其步長的選擇比梯度下降法的步長要小一點,因為梯度下降法使用的是準確梯度,所以它可以朝著全域性最優解(當問題為凸問題時)較大幅度的迭代下去,但是隨機梯度法不行,因為它使用的是近似梯度,或者對於全域性來說有時候它走的也許根本不是梯度下降的方向,故而它走的比較緩,同樣這樣帶來的好處就是相比於梯度下降法,它不是那麼容易陷入到區域性最優解中去。

三、權值更新方式

(i表示樣本標號下標,j表示樣本維數下標)

四、**實現(大體與梯度下降法相同,不同在於while迴圈中的內容)

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import axes3d

from matplotlib import style

#構造資料

def get_data(sample_num=1000):

"""擬合函式為

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 sgd(samples, y, step_size=2, max_iter_count=1000):

""":param samples: 樣本

:param y: 結果value

:param step_size: 每一接迭代的步長

:param max_iter_count: 最大的迭代次數

:param batch_size: 隨機選取的相對於總樣本的大小

:return:

"""#確定樣本數量以及變數的個數初始化theta值

m, var = samples.shape

theta = np.zeros(2)

y = y.flatten()

#進入迴圈內

loss = 1

iter_count = 0

iter_list=

loss_list=

theta1=

theta2=

#當損失精度大於0.01且迭代此時小於最大迭代次數時,進行

while loss > 0.01 and iter_count < max_iter_count:

loss = 0

#梯度計算

#樣本維數下標

rand1 = np.random.randint(0,m,1)

h = np.dot(theta,samples[rand1].t)

#關鍵點,只需要乙個樣本點來更新權值

for i in range(len(theta)):

theta[i] =theta[i] - step_size*(1/m)*(h - y[rand1])*samples[rand1,i]

#計算總體的損失精度,等於各個樣本損失精度之和

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_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()

if __name__ == '__main__':

samples, y = get_data()

theta1,theta2,theta,loss_list = sgd(samples, y)

print(theta) # 會很接近[5, 7]

painter3d(theta1,theta2,loss_list)

梯度下降法和隨機梯度下降法

批量梯度下降法 batch gradient descent 在更新引數時使用所有的樣本來進行更新 隨機梯度下降法 stochastic gradient descent 求梯度時沒有用所有的m個樣本的資料,而是僅僅選取乙個樣本j來求梯度。小批量梯度下降法 mini batch gradient d...

隨機梯度下降法

剛剛看完史丹福大學機器學習第四講 牛頓法 也對學習過程做一次總結吧。一 誤差準則函式與隨機梯度下降 數學一點將就是,對於給定的乙個點集 x,y 找到一條曲線或者曲面,對其進行擬合之。同時稱x中的變數為特徵 feature y值為 值。如圖 乙個典型的機器學習的過程,首先給出一組輸入資料x,我們的演算...

隨機梯度下降法

自定義虛擬資料集 import numpy as np import matplotlib.pyplot as plt m 100000 m個樣本 x np.random.normal size m x x.reshape 1 1 y 4.x 3 np.random.normal 0,3,size ...