用户登陆
正在加载
在Python实现梯度下降优化算法
互联网 · 2019-03-03 10:00:01

每一个机器学习工程师都在寻求改进他们的模型的性能。优化是机器学习中最重要的领域之一。优化使我们能够为我们的问题案例选择与机器学习算法或方法找到相关的最佳参数。优化算法有几种类型。也许最流行的是梯度下降优化算法。许多机器学习工程师第一次遇到梯度下降法是在他们对python网络的介绍中。在本教程中,我们将教你如何在python中从头实现梯度下降。首先,梯度下降法到底是什么?

什么是梯度下降法?

梯度下降法是一种通过重复步骤使机器学习模型收敛到最小值的优化算法。从本质上讲,梯度下降法是通过求出函数输出最小的值来最小化函数的。通常,这个函数通常是一个损失函数。损失函数衡量的是我们的模型与实际情况相比的糟糕程度。因此,我们只有减少这种损失才有意义。一个简单的梯度下降算法如下:·获得使F(x)最小化的函数·初始化一个值x,从该值开始下降就进行优化·指定一个学习率,该学习率将确定要下降多少步,或收敛到最小值的速度有多快·得到x的导数(下降)·继续下降,这个值的导数乘以学习速率·不断更新x的值·检查您的停止状态,看是否要停止·如果条件满足,停止。如果没有,则使用新的x值继续第4步,并保持重复算法

在Python中实现梯度下降

在这里,我们将使用python实现梯度下降的简单表示。我们将创建一个任意的损失函数,并试图找到该函数的局部最小值。我函数:- f(x)= x³5 x²+ 7

我们将首先使用一组从-1到3的值(为了确保陡峭的曲线,任意进行选择)来视化这个函数

import numpy as npimport matplotlib.pyplot as plt%matplotlib inlinecreating the function and plotting it function = lambda x: (x ** 3)-(3 *(x ** 2))+7#Get 1000 evenly spaced numbers between -1 and 3 (arbitratil chosen to ensure steep curve)x = np.linspace(-1,3,500)#Plot the curveplt.plot(x, function(x))plt.show()

结果如下:

然后,我们将继续为梯度下降创建两个函数:第一个是导数函数:这个函数接受x的值并根据我们指定的初始函数返回它的导数。如下图所示:第二个是阶跃函数:这是实际梯度下降发生的函数。该函数接受x的初始值或先前值,根据通过学习率采取的步骤对其进行更新,并输出达到停止条件的x的最小值。对于停止条件,我们将使用精确停止。这意味着,当旧x和更新x之间的绝对差大于一个值时,算法应该停止。函数还会输出x的最小值,以及达到该值所需的步骤或下降次数。该函数如下图所示:

def deriv(x): ''' Description: This function takes in a value of x and returns its derivative based on the initial function we specified. Arguments: x - a numerical value of x Returns: x_deriv - a numerical value of the derivative of x ''' x_deriv = 3* (x**2) - (6 * (x)) return x_derivdef step(x_new, x_prev, precision, l_r): ''' Description: This function takes in an initial or previous value for x, updates it based on steps taken via the learning rate and outputs the most minimum value of x that reaches the precision satisfaction. Arguments: x_new - a starting value of x that will get updated based on the learning rate x_prev - the previous value of x that is getting updated to the new one precision - a precision that determines the stop of the stepwise descent l_r - the learning rate (size of each descent step) Output: 1. Prints out the latest new value of x which equates to the minimum we are looking for 2. Prints out the the number of x values which equates to the number of gradient descent steps 3. Plots a first graph of the function with the gradient descent path 4. Plots a second graph of the function with a zoomed in gradient descent path in the important area ''' # create empty lists where the updated values of x and y wil be appended during each iteration x_list, y_list = [x_new], [function(x_new)] # keep looping until your desired precision while abs(x_new - x_prev) > precision: # change the value of x x_prev = x_new # get the derivation of the old value of x d_x = - deriv(x_prev) # get your new value of x by adding the previous, the multiplication of the derivative and the learning rate x_new = x_prev + (l_r * d_x) # append the new value of x to a list of all x-s for later visualization of path x_list.append(x_new) # append the new value of y to a list of all y-s for later visualization of path y_list.append(function(x_new)) print ("Local minimum occurs at: "+ str(x_new)) print ("Number of steps: " + str(len(x_list))) plt.subplot(1,2,2) plt.scatter(x_list,y_list,c="g") plt.plot(x_list,y_list,c="g") plt.plot(x,function(x), c="r") plt.title("Gradient descent") plt.show() plt.subplot(1,2,1) plt.scatter(x_list,y_list,c="g") plt.plot(x_list,y_list,c="g") plt.plot(x,function(x), c="r") plt.xlim([1.0,2.1]) plt.title("Zoomed in Gradient descent to Key Area") plt.show()

接下来,我们继续绘制梯度下降路径,如下图所示:

#Implement gradient descent (all the arguments are arbitrarily chosen)step(0.5, 0, 0.001, 0.05)

梯度下降在机器学习中的重要性是在你的机器学习过程中都会遇到的。这就是为什么您必须了解这个算法的内部工作原理。本教程向您介绍了梯度下降算法的最简单形式及其在python中的实现。现在,您对这个算法有了直观的理解,可以准备将其应用于现实世界的问题。

免责声明:
本网站所提供的所有信息仅供参考,不构成任何投资建议。用户在使用本网站的信息时应自行判断和承担风险。币界网不对用户因使用本网站信息而导致的任何损失负责。用户在进行任何投资活动前应自行进行调查和研究,并谨慎决策。币界网不对用户基于本网站信息做出的任何投资决策负责。用户在本网站发布的任何内容均由其个人负责,与币界网无关。
免责声明:本网站、超链接、相关应用程序、论坛、博客等媒体账户以及其他平台和用户发布的所有内容均来源于第三方平台及平台用户。币界网对于网站及其内容不作任何类型的保证,网站所有区块链相关数据以及其他内容资料仅供用户学习及研究之用,不构成任何投资、法律等其他领域的建议和依据。币界网用户以及其他第三方平台在本网站发布的任何内容均由其个人负责,与币界网无关。币界网不对任何因使用本网站信息而导致的任何损失负责。您需谨慎使用相关数据及内容,并自行承担所带来的一切风险。强烈建议您独自对内容进行研究、审查、分析和验证。
s_logo
App内打开