第6章 梯度下降法
梯度下降法:(Gradient?Descent),梯度法用來求一個函數(shù)的最優(yōu)值
,?如果第一個點導(dǎo)數(shù)不為零的話,那么這點肯定不在極值點上!
有時候一上來η的取值不是很準確,需要通過調(diào)參的方式來找到適合的η值
?
?
,??
,??
import numpy as np import matplotlib.pyplot as plt plot_x=np.linspace(-1,6,141) plot_x plot_y=(plot_x-2.5)**2-1 plt.plot(plot_x,plot_y) plt.show()def dJ(theta):return 2*(theta-2.5) def J(theta):return (theta-2.5)**2-1 def gradient_descent(initial_theta,eta,n_iters = 1e4,epsilon=1e-8):theta = initial_thetatheta_history.append(initial_theta)i_iter=0while i_iter<n_iters:gradient=dJ(theta)last_theta=thetatheta=theta-eta*gradienttheta_history.append(theta)if (abs(J(theta)-J(last_theta))<epsilon):breaki_iter+=1 def plot_theta_history():plt.plot(plot_x,J(plot_x))plt.plot(np.array(theta_history),J(np.array(theta_history)),color='r',marker='+')plt.show()eta=1.1 theta_history=[] gradient_descent(0,eta,n_iters=10) plot_theta_history() 梯度下降法模擬?
,??
?
?
,?
,?,?線性回歸中梯度下降法的向量化
假設(shè):X0恒等于1
?
, ??
?
,?
?
?
,??取值a=5,b=50
?
?
,??
?
,??
,??
,??
總結(jié)
- 上一篇: 第5章 线性回归算法
- 下一篇: 第五章--预处理理论