风险提示:理性看待区块链,提高风险意识!
在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中的实现。现在,您对这个算法有了直观的理解,可以准备将其应用于现实世界的问题。

上一篇: 基于哈希函数的签名
下一篇: 比特币Bitcoin(BTC)是什么
推荐专栏
web3首席知识博主
一位相信价值投资的币圈KOL。稳定盈利的缠论野生交易员 #BTC行情分析师 #价值投资 #链上数据分析
爱Web 3,爱生活,爱科技,爱炒币的老韭菜
热门币种
更多
币种
价格
24H涨跌幅
BTC比特币
¥264,723.74
37,091.22 USDT
+0.1%
ETH以太坊
¥14,416.22
2,019.90 USDT
-0.12%
USDT泰达币
¥7.20
1.01 USDT
0%
BNB币安币
¥1,625.40
227.74 USDT
+0.36%
XRP瑞波币
¥4.32
0.60460 USDT
+0.37%
USDC
¥7.14
1.00 USDT
+0.03%
SOLSolana
¥398.85
55.89 USDT
+1.54%
OKBOK币
¥398.61
55.85 USDT
-1.64%
ADA艾达币
¥2.68
0.37580 USDT
-1.16%
DOGE狗狗币
¥0.55160
0.07730 USDT
-1.52%
热搜币种
更多
币种
价格
24H涨跌幅
Terra Classic
¥0.00
9.402E-5 USDT
-18.95%
Gala
¥0.18
0.025374 USDT
-4.66%
dYdX
¥22.58
3.1918 USDT
-0.91%
比特股
¥0.05
0.006964 USDT
+4.28%
PancakeSwap
¥15.52
2.1936 USDT
-2.74%
Conflux
¥1.08
0.1524 USDT
-2.87%
Filecoin
¥31.45
4.4454 USDT
-0.69%
FTX Token
¥29.82
4.2155 USDT
+16.96%
Yield Guild Games
¥2.55
0.3608 USDT
-0.52%
Shiba Inu
¥0.00
8.14E-6 USDT
-2.51%
比特币
¥262,381.44
37091.22 USDT
+0.1%
比原链
¥0.07
0.010011 USDT
-4.38%
最新快讯
更多
汇丰、恒生、渣打、富邦华一四家外资银行入围首批“数字人民币”业务试点名单
2023-11-28 19:06:57
摩根大通和Apollo计划建立代币化“企业主网”
2023-11-28 19:03:57
Nansen2公测版本上线,新增链上数据异动、智能搜索等功能
2023-11-28 18:59:52
西班牙公民需在明年3月底前申报其海外平台上加密货币持仓
2023-11-28 18:53:43
Nansen2已公开测试
2023-11-28 18:53:38
dYdX基金会:主网启动以来超过1645万DYDX被质押
2023-11-28 18:52:07
NicCarter等比特币倡导者发文:比特币挖矿是清洁能源和平衡电网的关键工具
2023-11-28 18:47:58
下载币界网APP