这里我们加入学习率来实现一下简单的梯度下降
我们假设输出方程为二次方程y = x**2 +5
def f(x):
return x ** 2 + 5
他是二次方程,当x为0的时候y取得最小值,我们梯度下降就是要得到y的最小值
方程的导数为2*x
def df(x):
"""
Derivative of `f` with respect to `x`.
"""
return 2*x
我们假设当x=2的时候,方程的倒数为4,因此切线方程斜率为4,根据运算得到切线方程为
y = 4*x +1
def gradient_descent_update(x, gradx, learning_rate):
x = x - gradx*learning_rate
return x
x = random.randint(0, 10000)
# TODO: Set the learning rate
learning_rate = 0.1
epochs = 100
for i in range(epochs+1):
cost = f(x)
gradx = df(x)
print("EPOCH {}: Cost = {:.3f}, x = {:.3f}".format(i, cost, gradx))
x = gradient_descent_update(x, gradx, learning_rate)
可以参考一下
https://blog.csdn.net/xf8964/article/details/90151391