梯度下降法

时间:2019-11-20
本文章向大家介绍梯度下降法,主要包括梯度下降法使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

简介

梯度下降法是迭代法的一种,可以用于求解最小二乘问题(线性和非线性都可以),在求解机器学习算法的模型参数,梯度下降是最常采用的方法之一,在求解损失函数的最小值时,可以通过梯度下降法来一步步的迭代求解

  • 不是一个机器学习算法
  • 是一种基于搜索的最优化方法
  • 最小化损失函数
  • 最大化一个效用函数(梯度上升法)

模型

$J=\theta ^{2}+b$

  定义了一个损失函数以后,参数 $\theta $ 对应的损失函数 $J$ 的值对应的示例图,需要找到使得损失函数值 $J$ 取得最小值对应的 $\theta $

  • 首先随机取一个 $\theta $,对 $\theta $求导乘 $\eta $,得到一个导数gradient
  • 将之前的 $\theta $ 存为last_theta
  • 将 $\theta $减去$\eta $*gradient得到的值存入 $\theta $
  • 将 $\theta $与last_theta分别代入公式后得到两个函数值相减,如果小于指定的一个极小值,则说明已找到了最小的 $\theta $,否则重复第1个步聚,对 $\theta $ 求导,依次完成,直到差值小于极小值

$\eta $ 超参数的作用

  • $\eta $ 称为学习率也称为步长
  • $\eta $ 的取值影响获得最优解的速度
  • $\eta $ 取值不合适,可能得不到最优解
  • $\eta $ 是梯度下降的一个超参数

η太小,会减慢收敛学习速度

η太大,会导致不收敛

局部最优与全局最优

最优化问题

  就是在复杂环境中遇到的许多可能的决策中,挑选“最好”的决策的科学机器学习中选择最小的参数满足分类与预测的要求

  全局最优:若一项决策和所有解决该问题的决策相比是最优的,对目标函数去最大值还是最小值,损失函数只有一个最优解

  局部最优:不要求在所有决策中是最好的

解决方案

  • 多次运行,随机化初始点
  • 梯度下降法的初始点也是一个超参数

目标

  使 $\sum_{i=1}^{m}(y^{(i)}-\widehat{y}^{(i)})^{2}$ 尽可能小

代码实现

python代码实现

mport numpy as np
import matplotlib.pyplot as plt 
plot_x = np.linspace(-1.0,6.0,141)
plot_y = (plot_x-2.5)**2+1
plt.plot(plot_x,plot_y,c='b')
plt.show() 
#定一个极小值
epsilon = 1e-8
eta = 0.1 
def J(thata):
    return (thata-2.5)**2+1.0 
def DJ(thata):
    return 2*(thata-2.5)
thata = 0.0
while True:
    g = DJ(thata)
    last_thata = thata
    thata = thata-g
    if(abs(J(thata)-J(last_thata))<epsilon):
        #注意这里不能小于0,如果两个损失函数的值相减小于一个极值,说明已找到
        break;
print(thata)
print(J(thata))#最后一次的值

查看学习率

theta = 0.0
theta_history = [theta]
while True:
    gradient = dJ(theta)
    last_theta = theta
    theta = theta - eta * gradient
    theta_history.append(theta)

    if(abs(J(theta) - J(last_theta)) < epsilon):
        break

plt.plot(plot_x, J(plot_x))
plt.plot(np.array(theta_history), J(np.array(theta_history)), color="r", marker='+')
plt.show()

函数封装

def gradient_descent(inital_theta,eta,epslion=1e-8):
    theta = inital_theta
    theta_history.append(theta)
    while True:
        g = DJ(theta)
        last_thata = theta
        theta = theta - eta * g
        theta_history.append(theta)
        if (abs(J(theta) - J(last_thata)) < epsilon):
            # 注意这里不能小于0,如果两个损失函数的值相减小于一个极值,说明已找到
            break;
def plot_theta_history():
    plt.plot(plot_x, J(plot_x), c='b')  # 将x数据传入J函数取得y的值
    plt.plot(np.array(theta_history), J(np.array(theta_history)), c='r', marker='+')
    plt.show() 
gradient_descent(0.0,eta)
plot_theta_history()

调整学习参数

eta = 0.001
theta_history = []
gradient_descent(0, eta)
plot_theta_history()


eta = 0.8
theta_history = []
gradient_descent(0, eta)
plot_theta_history()


eta = 1.1
theta_history = []
gradient_descent(0, eta)

迭代次数的调整

def gradient_descent(initial_theta, eta, n_iters = 1e4, epsilon=1e-8):

    theta = initial_theta
    i_iter = 0
    theta_history.append(initial_theta)

    while i_iter < n_iters:
        gradient = dJ(theta)
        last_theta = theta
        theta = theta - eta * gradient
        theta_history.append(theta)

        if(abs(J(theta) - J(last_theta)) < epsilon):
            break
        i_iter += 1
    return

多元线性回归中的梯度下降法

原文地址:https://www.cnblogs.com/zry-yt/p/11900542.html