溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

用python實(shí)現(xiàn)梯度下降算法的方式是什么

發(fā)布時(shí)間:2020-08-11 09:13:01 來(lái)源:億速云 閱讀:132 作者:小新 欄目:編程語(yǔ)言

用python實(shí)現(xiàn)梯度下降算法的方式是什么?這個(gè)問(wèn)題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過(guò)這個(gè)問(wèn)題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!

python版本選擇

這里選的python版本是2.7,因?yàn)槲抑坝胮ython3試了幾次,發(fā)現(xiàn)在畫3d圖的時(shí)候會(huì)報(bào)錯(cuò),所以改用了2.7。

(推薦教程:Python入門教程)

數(shù)據(jù)集選擇

數(shù)據(jù)集我選了一個(gè)包含兩個(gè)變量,三個(gè)參數(shù)的數(shù)據(jù)集,這樣可以畫出3d圖形對(duì)結(jié)果進(jìn)行驗(yàn)證。

部分函數(shù)總結(jié)

symbols()函數(shù):首先要安裝sympy庫(kù)才可以使用。用法:

>>> x1 = symbols('x2')
>>> x1 + 1
x2 + 1

在這個(gè)例子中,x1和x2是不一樣的,x2代表的是一個(gè)函數(shù)的變量,而x1代表的是python中的一個(gè)變量,它可以表示函數(shù)的變量,也可以表示其他的任何量,它替代x2進(jìn)行函數(shù)的計(jì)算。實(shí)際使用的時(shí)候我們可以將x1,x2都命名為x,但是我們要知道他們倆的區(qū)別。
再看看這個(gè)例子:

>>> x = symbols('x')
>>> expr = x + 1
>>> x = 2
>>> print(expr)
x + 1

作為python變量的x被2這個(gè)數(shù)值覆蓋了,所以它現(xiàn)在不再表示函數(shù)變量x,而expr依然是函數(shù)變量x+1的別名,所以結(jié)果依然是x+1。
subs()函數(shù):既然普通的方法無(wú)法為函數(shù)變量賦值,那就肯定有函數(shù)來(lái)實(shí)現(xiàn)這個(gè)功能,用法:

>>> (1 + x*y).subs(x, pi)#一個(gè)參數(shù)時(shí)的用法
pi*y + 1
>>> (1 + x*y).subs({x:pi, y:2})#多個(gè)參數(shù)時(shí)的用法
1 + 2*pi

diff()函數(shù):求偏導(dǎo)數(shù),用法:result=diff(fun,x),這個(gè)就是求fun函數(shù)對(duì)x變量的偏導(dǎo)數(shù),結(jié)果result也是一個(gè)變量,需要賦值才能得到準(zhǔn)確結(jié)果。

代碼實(shí)現(xiàn):

from __future__ import division
from sympy import symbols, diff, expand
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

data = {'x1': [100, 50, 100, 100, 50, 80, 75, 65, 90, 90],
        'x2': [4, 3, 4, 2, 2, 2, 3, 4, 3, 2],
        'y': [9.3, 4.8, 8.9, 6.5, 4.2, 6.2, 7.4, 6.0, 7.6, 6.1]}#初始化數(shù)據(jù)集
theta0, theta1, theta2 = symbols('theta0 theta1 theta2', real=True)  # y=theta0+theta1*x1+theta2*x2,定義參數(shù)
costfuc = 0 * theta0
for i in range(10):
    costfuc += (theta0 + theta1 * data['x1'][i] + theta2 * data['x2'][i] - data['y'][i]) ** 2
costfuc /= 20#初始化代價(jià)函數(shù)
dtheta0 = diff(costfuc, theta0)
dtheta1 = diff(costfuc, theta1)
dtheta2 = diff(costfuc, theta2)

rtheta0 = 1
rtheta1 = 1
rtheta2 = 1#為參數(shù)賦初始值

costvalue = costfuc.subs({theta0: rtheta0, theta1: rtheta1, theta2: rtheta2})
newcostvalue = 0#用cost的值的變化程度來(lái)判斷是否已經(jīng)到最小值了
count = 0
alpha = 0.0001#設(shè)置學(xué)習(xí)率,一定要設(shè)置的比較小,否則無(wú)法到達(dá)最小值
while (costvalue - newcostvalue > 0.00001 or newcostvalue - costvalue > 0.00001) and count < 1000:
    count += 1
    costvalue = newcostvalue
    rtheta0 = rtheta0 - alpha * dtheta0.subs({theta0: rtheta0, theta1: rtheta1, theta2: rtheta2})
    rtheta1 = rtheta1 - alpha * dtheta1.subs({theta0: rtheta0, theta1: rtheta1, theta2: rtheta2})
    rtheta2 = rtheta2 - alpha * dtheta2.subs({theta0: rtheta0, theta1: rtheta1, theta2: rtheta2})
    newcostvalue = costfuc.subs({theta0: rtheta0, theta1: rtheta1, theta2: rtheta2})
rtheta0 = round(rtheta0, 4)
rtheta1 = round(rtheta1, 4)
rtheta2 = round(rtheta2, 4)#給結(jié)果保留4位小數(shù),防止數(shù)值溢出
print(rtheta0, rtheta1, rtheta2)

fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(data['x1'], data['x2'], data['y'])  # 繪制散點(diǎn)圖
xx = np.arange(20, 100, 1)
yy = np.arange(1, 5, 0.05)
X, Y = np.meshgrid(xx, yy)
Z = X * rtheta1 + Y * rtheta2 + rtheta0
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'))

plt.show()#繪制3d圖進(jìn)行驗(yàn)證

結(jié)果:

用python實(shí)現(xiàn)梯度下降算法的方式是什么

用python實(shí)現(xiàn)梯度下降算法的方式是什么

感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)用python實(shí)現(xiàn)梯度下降算法的方式是什么大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI