机器学习之梯度下降法(Gradient Descent)

1.梯度下降法

首先梯度下降法不是一个机器学习算法,而是一种基于搜索的最优化方法。它的作用是最小化一个损失函数。梯度上升法则是最大化一个效用函数。

导数可以代表J 增大的方向,如上图一点导数为负值,说明J增大的方向是沿着X轴负方向。

代表移动的步长,一直移动到导数为0.

 

 

模拟梯度下降法:

"""模拟梯度下降法"""
import numpy as np
import matplotlib.pyplot as plt

plot_x = np.linspace(-1, 6, 141)
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


theta = 0.0
theta_history = [theta]

def gradient_descent(initial_theta,eta,epsilon=1e-8):
    """模拟梯度下降法"""
    theta=initial_theta
    theta_history.append(i
  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值