没有调学习率
import numpy as np
import matplotlib.pylab as plt
def _numerical_gradient_no_batch(f, x): #求函数的梯度值
h = 1e-4 # 0.0001
grad = np.zeros_like(x)
for idx in range(x.size):
tmp_val = x[idx]
x[idx] = float(tmp_val) + h
fxh1 = f(x) # f(x+h)
x[idx] = tmp_val - h
fxh2 = f(x) # f(x-h)
grad[idx] = (fxh1 - fxh2) / (2 * h)
x[idx] = tmp_val # 还原值
return grad
def gradient_descent(f, init_x, lr=0.01, step_num=100): #梯度下降法
x = init_x #初始化
x_history = [] #记录寻优过程中的值
for i in range(step_num):
x_history.append( x.copy() )
grad = _numerical_gradient_no_batch(f, x) #计算函数梯度
x -= lr * grad #沿着梯度方向下降,空学习率控制每次下降的多少
return x, np.array(x_history)
def function_2(x):
return (x[0]+1)**2-1
init_x = np.array([-3.0, 4.0]) #从(-3,4)开始搜索
lr = 0.1 #学习率为0.1
step_num = 20 #共搜索20步
x, x_history = gradient_descent(function_2, init_x, lr=lr, step_num=step_num) #梯度下降法
#输出结果
print(x) #结果很接近最小值(0,0)
print(x_history)
#画出历史值
x_=x_history[:,0]
y_=(x_+1)**2-1
print(x_)
print(y_)
plt.plot(x_history[:,0],(x_history[:,0]+1)**2-1 ,'o')
plt.xlim(-4,0)
plt.ylim(-2, 5)
plt.xlabel("X0")
plt.ylabel("y")
plt.show()
结果如下