模拟实现梯度下降法
import numpy as npimport matplotlib.pyplot as plt
生成数据
plot_x = np.linspace(-1,6,141)print(plot_x)plot_y = (plot_x - 2.5) ** 2 -1
[-1. -0.95 -0.9 -0.85 -0.8 -0.75 -0.7 -0.65 -0.6 -0.55 -0.5 -0.45-0.4 -0.35 -0.3 -0.25 -0.2 -0.15 -0.1 -0.05 0. 0.05 0.1 0.150.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.750.8 0.85 0.9 0.95 1. 1.05 1.1 1.15 1.2 1.25 1.3 1.351.4 1.45 1.5 1.55 1.6 1.65 1.7 1.75 1.8 1.85 1.9 1.952. 2.05 2.1 2.15 2.2 2.25 2.3 2.35 2.4 2.45 2.5 2.552.6 2.65 2.7 2.75 2.8 2.85 2.9 2.95 3. 3.05 3.1 3.153.2 3.25 3.3 3.35 3.4 3.45 3.5 3.55 3.6 3.65 3.7 3.753.8 3.85 3.9 3.95 4. 4.05 4.1 4.15 4.2 4.25 4.3 4.354.4 4.45 4.5 4.55 4.6 4.65 4.7 4.75 4.8 4.85 4.9 4.955. 5.05 5.1 5.15 5.2 5.25 5.3 5.35 5.4 5.45 5.5 5.555.6 5.65 5.7 5.75 5.8 5.85 5.9 5.95 6. ]
plt.plot(plot_x,plot_y)
求导数
#求导def dJ(thata):return 2 * (thata - 2.5)
函数值
def J(theta):return (theta - 2.5) ** 2 -1
eta 学习率,一般设置为0.01就可以epsion精度设置为1e-8theta刚开始的’x’,超参数,某些情况下需要随机多次取值,以便达到全局最优解theta_history = [theta],把theta轨迹绘制出来
eta = 0.1 #学习率epsilon = 1e-8theta = 0.0theta_history = [theta]while True:gradient = dJ(theta)last_theta = thetatheta = theta - eta * gradienttheta_history.append(theta)if (np.abs(J(theta) - J(last_theta)) < epsilon):breakprint(theta)print(J(theta))print(theta_history)print(len(theta_history))
2.499891109642585-0.99999998814289[0.0, 0.5, 0.9, 1.2200000000000002, 1.4760000000000002, 1.6808, 1.84464, 1.9757120000000001, 2.0805696, 2.16445568, 2.2315645440000003, 2.2852516352000003, 2.3280816, 2.362561046528, 2.3900488372224, 2.41203906977792, 2.429631255822336, 2.4437050046578688, 2.454964003726295, 2.4639712029810363, 2.471176962384829, 2.476941569907863, 2.4815532559262907, 2.4852426047410328, 2.488194083792826, 2.490555267034261, 2.492444213627409, 2.493955370901927, 2.4951642967215415, 2.496131437377233, 2.4969051499017865, 2.497524119921429, 2.4980192959371434, 2.4984154367497147, 2.4987323493997717, 2.498985879519817, 2.4991887036158538, 2.499350962892683, 2.4994807703141464, 2.499584616251317, 2.4996676930010535, 2.499734154400843, 2.4997873235206742, 2.4998298588165393, 2.4998638870532313, 2.499891109642585]46
绘制出来
plt.plot(plot_x,J(plot_x))plt.plot(np.array(theta_history),J(np.array(theta_history)),color = 'r',marker = '*')
封装两个函数
n_iers控制轮数
imitial_theta控制初始’x’
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 (np.abs(J(theta) - J(last_theta)) < epsilon):breaki_iter += 1def plot_theta_history():plt.plot(plot_x,J(plot_x))plt.plot(np.array(theta_history),J(np.array(theta_history)),color = 'r',marker = '*')
把学习率降低到0.01
eta = 0.01theta_history = []gradient_descent(0.0,eta)plot_theta_history()
学习率0.8
eta = 0.1
eta = 1.1#eta = 0.01一般就可以theta_history = []gradient_descent(0.0,eta,n_iters=10)plot_theta_history()