python 梯度下降

机器学习 , 编程 (Machine learning, Programming)

介绍 (Introduction)

Regression is a kind of supervised learning algorithm within machine learning. It is an approach to model the relationship between the dependent variable (or target, responses), y, and explanatory variables (or inputs, predictors), X. Its objective is to predict a quantity of the target variable, for example; predicting the stock price, which differs from classification problem, where we want to predict the label of target, for example; predicting the direction of stock (up or down).

回归是机器学习中的一种监督学习算法。 这是一种对因变量(或目标,响应) y和解释变量(或输入,预测变量) X之间的关系建模的方法。 例如,其目的是预测目标变量的数量。 预测股票价格,这与分类问题不同,例如,我们要预测目标标签; 预测库存的方向(向上或向下)。

Moreover, a regression can be used to answer whether and how several variables are related, or influence each other, for example; determine if and to what extent the work experience or age impacts salaries.

而且,例如,可以使用回归来回答几个变量是否相关以及如何相互影响,或者相互影响。 确定工作经验或年龄是否以及在多大程度上影响工资。

In this article, I will focus mainly on linear regression and its approaches.

在本文中,我将主要关注线性回归及其方法。

线性回归的不同方法 (Different approaches to Linear Regression)

OLS (Ordinary least squares) goal is to find the best-fitting line (hyperplane) that minimizes the vertical offsets, which can be mean squared error (MSE) or other error metrics (MAE, RMSE) between the target variable and the predicted output.

OLS(普通最小二乘法)的目标是找到使垂直偏移最小的最佳拟合线(超平面),垂直偏移可以是目标变量和预测输出之间的均方误差(MSE)或其他误差度量(MAE,RMSE) 。

We can implement a linear regression model using the following approaches:

我们可以使用以下方法实现线性回归模型:

  1. Solving model parameters (closed-form equations)求解模型参数(闭式方程)
  2. Using optimization algorithm (gradient descent, stochastic gradient, etc.)使用优化算法(梯度下降,随机梯度等)

Please note that OLS regression estimates are the best linear unbiased estimator (BLUE, in short). Regression in other forms, the parameter estimates may be biased, for example; ridge regression is sometimes used to reduce the variance of estimates when there is collinearity in the data. However, the discussion of bias and variance is not in the scope of this article (please refer to this great article related to bias and variance).

请注意,OLS回归估计是最佳线性无偏估计器 (简称BLUE)。 例如,以其他形式回归时,参数估计可能有偏差; 当数据存在共线性时,脊回归有时用于减少估计的方差。 但是,对偏差和方差的讨论不在本文的讨论范围内(请参阅这篇有关偏差和方差的出色文章 )。

闭式方程 (Closed-form equation)

Let’s assume we have inputs of X size n and a target variable, we can write the following equation to represent the linear regression model.

假设我们有X个大小为n的输入和一个目标变量,我们可以编写以下方程式来表示线性回归模型。

Simple form of linear regression (where i = 1, 2, …, n)
线性回归的简单形式(其中i = 1、2,…,n)

The equation is assumed we have the intercept X0 = 1. There is also a model without intercept, where B0 = 0, but this is based on some hypothesis that it will always undergo through the origin (there’s a lot of discussion on this topic which you can read more here and here).

假设该方程式我们有截距X0 =1。还有一个没有截距的模型,其中B0 = 0,但这是基于某些假设,即它将始终通过原点进行(关于该主题有很多讨论,您可以在此处和此处内容)。

From the equation above, we can compute the regression parameters based on the below computation.

从上面的方程式,我们可以根据下面的计算来计算回归参数。

Matrix formulation of the multiple regression model
多元回归模型的矩阵表述

Now, let’s implement this in Python, there are three ways we can do this; manual matrix multiplication, statsmodels library, and sklearn library.

现在,让我们在Python中实现此功能,可以通过三种方式实现此功能: 手动矩阵乘法, statsmodels库和sklearn库。

The model weights
模型权重
Example of single linear model, one input (left) and the model prediction (right)
单个线性模型,一个输入(左)和模型预测(右)的示例

You can see that all three solutions give out the same results, we can then use the output to write the model equation (Y =0.7914715+1.38594198X).

您可以看到所有三个解决方案都给出了相同的结果,然后我们可以使用输出编写模型方程式(Y = 0.7914715 + 1.38594198X )。

This approach offers a better solution for smaller data, easy, and quick explainable model.

这种方法为较小的数据,简单且快速的可解释模型提供了更好的解决方案。

梯度下降 (Gradient Descent)

Why we need gradient descent if the closed-form equation can solve the regression problem. There will be some situations which are;

如果封闭形式的方程可以解决回归问题,为什么我们需要梯度下降。 会有一些情况;

  • There is no closed-form solution for most nonlinear regression problems.对于大多数非线性回归问题,没有封闭形式的解决方案。
  • Even in linear regression, there may be some cases where it is impractical to use the formula. An example is when X is a very large, sparse matrix. The solution will be too expensive to compute.即使在线性回归中,在某些情况下使用该公式也不可行。 一个示例是X是一个非常大的稀疏矩阵。 该解决方案将太昂贵而无法计算。

Gradient descent is a computationally cheaper (faster) option to find the solution.

梯度下降是找到解决方案的计算便宜(更快)的选择。

Gradient descent is an optimization algorithm used to minimize some cost function by repetitively moving in the direction of steepest descent. Hence, the model weights are updated after each epoch.

梯度下降是一种优化算法,用于通过在最陡下降方向反复移动来最小化某些成本函数。 因此,在每个时期之后更新模型权重。

Basic visualization of gradient descent — ideally gradient descent tries to converge toward global minimum
梯度下降的基本可视化-理想情况下,梯度下降试图向全局最小值收敛

There are three primary types of gradient descent used in machine learning algorithm;

机器学习算法中使用了三种主要的梯度下降类型:

  1. Batch gradient descent批次梯度下降
  2. Stochastic gradient descent随机梯度下降
  3. Mini-batch gradient descent小批量梯度下降

Let us go through each type in more detail and implementation.

让我们更详细地介绍每种类型和实现。

批次梯度下降 (Batch Gradient Descent)

This approach is the most straightforward. It calculates the error for each observation within the training set. It will update the model parameters after all training observations are evaluated. This process can be called a training epoch.

这种方法是最简单的。 它为训练集中的每个观测值计算误差。 在评估所有训练观察结果后,它将更新模型参数 。 这个过程可以称为训练时期

The main advantages of this approach are computationally efficient and producing a stable error gradient and stable convergence, however, it requires the entire training set in memory, also the stable error gradient can sometimes result in the not-the-best model (converge to local minimum trap, instead attempt to find the best global minimum).

这种方法的主要优点是计算效率高,并产生稳定的误差梯度和稳定的收敛性,但是,它需要将整个训练集存储在内存中,而且稳定的误差梯度有时会导致生成非最佳模型(收敛到局部最小陷阱,而是尝试找到最佳的全局最小值)。

Let’s observe the python implementation of the regression problem.

让我们观察一下回归问题的python实现。

The cost function of linear regression
线性回归的成本函数
Batch gradient descent — cost and MSE per epoch
批次梯度下降-每个时期的成本和MSE

As we can see, the cost is reducing stably and reach a minimum of around 150–200 epochs.

正如我们所看到的,成本正在稳定下降,并至少达到150-200个时代。

During the computation, we also use vectorization for better performance. However, if the training set is very large, the performance will be slower.

在计算过程中,我们还使用矢量化以获得更好的性能。 但是,如果训练集很大,则性能会变慢。

随机梯度下降 (Stochastic Gradient Descent)

In stochastic gradient descent, SGD (or sometimes referred to as iterative or online GD). The name “stochastic” and “online GD” come from the fact that the gradient-based on single training observation is a “stochastic approximation” of the true cost gradient. However, because of this, the path towards the global cost minimum is not direct and may go up-and-down before converging to the global cost minimum.

在随机梯度下降中,SGD(或有时称为迭代在线 GD)。 名称“随机”和“在线GD”来自以下事实:基于单一训练观察的梯度是真实成本梯度的“随机近似”。 但是,由于这个原因,通往全球最低成本的道路并不直接,可能会在达到全球最低成本之前起伏不定。

Hence;

因此;

  • This makes SGD faster than batch GD (in most cases).这使得SGD比批处理GD更快(在大多数情况下)。
  • We can view the insight and rate of improvement of the model in real-time.我们可以实时查看模型的洞察力和改进率。
  • Increased model update frequency can result in faster learning.增加模型更新频率可以加快学习速度。
  • Noisy update of stochastic nature can help to avoid the local minimum.随机性的噪声更新可以帮助避免局部最小值。

However, some disadvantages are;

但是,有一些缺点。

  • Due to update frequency, this can be more computation expensive, which can take a longer time to complete than another approach.由于更新频率的原因,与其他方法相比,这可能会增加计算开销,并且可能需要更长的时间才能完成。
  • The frequent updates will result in a noisy gradient signal, which causes the model parameters and error to jump around, higher variance over training epochs.频繁的更新将导致嘈杂的梯度信号,这会导致模型参数和误差跳来跳去,在训练时期上变化更大。

Let’s look at how we can implement this in Python.

让我们看一下如何在Python中实现它。

Stochastic gradient descent — performance per epoch
随机梯度下降-每个时期的表现

小批量梯度下降 (Mini-Batch Gradient Descent)

Mini-batch gradient descent (MB-GD) is a more preferred method since it compromises between Batch gradient descent and stochastic gradient descent. It separates the training set into small batches and feeds to the algorithm. The model will get updates based on these batches. The model will converge more quickly than batch GD because the weights get updated more frequently.

小批量梯度下降(MB-GD)是一种更优选的方法,因为它会在批量梯度下降和随机梯度下降之间进行折衷。 它将训练集分成小批,并馈给算法。 该模型将基于这些批次获得更新。 该模型将比批处理GD更快收敛,因为权重得到更频繁的更新。

This method combines the efficiency of batch GD and the robustness of stochastic GD. One (small) downside is this method introduces a new parameter “batch size”, which may require the fine-tuning as part of model tuning/optimization.

该方法结合了批处理GD的效率和随机GD的鲁棒性。 一个(小的)缺点是该方法引入了一个新的参数“批大小”,这可能需要进行微调,作为模型调整/优化的一部分。

We can imagine batch size as a slider on the learning process.

我们可以想象批量大小是学习过程中的滑块。

  • Small value gives a learning process converges quickly at the cost of noise in the training process较小的价值使学习过程Swift收敛,但以训练过程中的噪音为代价
  • Large value gives a learning process converges slowly with accurate estimates of the error gradient较大的值可让学习过程缓慢收敛,并准确估计误差梯度

We can reuse the above function but need to specify the batch size to be len(training set) > batch_size > 1.

我们可以重用上面的函数,但是需要将批处理大小指定为len(训练集)> batch_size> 1。

theta, _, mse_ = _sgd_regressor(X_, y, learning_rate=learning_rate, n_epochs=n_epochs, batch_size=50)

theta, _, mse_ = _sgd_regressor(X_, y, learning_rate=learning_rate, n_epochs=n_epochs, batch_size=50)

Mini-batch gradient descent — performance over an epoch
小批量梯度下降—历时性能

We can see that only the first few epoch, the model is able to converge immediately.

我们可以看到,只有前几个时期,模型才能够立即收敛。

SGD回归器(scikit-learn) (SGD Regressor (scikit-learn))

In python, we can implement a gradient descent approach on regression problem by using sklearn.linear_model.SGDRegressor . Please refer to the documentation for more details.

在python中,我们可以使用sklearn.linear_model.SGDRegressor在回归问题上实现梯度下降方法。 请参阅文档以获取更多详细信息。

Below is how we can implement a stochastic and mini-batch gradient descent method.

以下是我们如何实现随机和小批量梯度下降方法。

scikit-learn SGD model detail and performance
scikit-learn SGD模型的详细信息和性能
scikit-learn SGD mini-batch model detail and performance
scikit-learn SGD微型批次模型的详细信息和性能

尾注 (EndNote)

In this post, I have provided the explanation of linear regression on both closed-form equation and optimization algorithm, gradient descent, by implementing them from scratch and using a built-in library.

在这篇文章中,我通过从头开始实现并使用内置库,对闭式方程式和优化算法(梯度下降)进行了线性回归的解释。

其他阅读和Github存储库: (Additional reading and Github repository:)

翻译自: https://medium.com/towards-artificial-intelligence/closed-form-and-gradient-descent-regression-explained-with-python-1627c9eeb60e

python 梯度下降


http://www.taodudu.cc/news/show-863748.html

相关文章:

  • 内容管理系统_内容
  • opencv图像深度-1_OpenCV空间AI竞赛之旅(第1部分-初始设置+深度)
  • 概率编程编程_概率编程语言的温和介绍
  • TensorFlow 2.X中的动手NLP深度学习模型准备
  • 时间序列 线性回归 区别_时间序列分析的完整介绍(带R)::线性过程I
  • 深度学习学习7步骤_如何通过4个简单步骤为深度学习标记音频
  • 邮件伪造_伪造品背后的数学
  • 图像匹配与OpenCV模板匹配
  • 边缘计算边缘计算edge_Edge AI-边缘上的计算机视觉推理
  • arduino 入门套件_计算机视觉入门套件
  • 了解LSTM和GRU
  • 使用TensorFlow 2.0+和Keras实现AlexNet CNN架构
  • power bi_如何将Power BI模型的尺寸减少90%!
  • 使用Optuna的XGBoost模型的高效超参数优化
  • latex 表格中虚线_如何识别和修复表格识别中的虚线
  • 构建强化学习_如何构建强化学习项目(第1部分)
  • sam服务器是什么_使用SAM CLI将机器学习模型部署到无服务器后端
  • pca 主成分分析_六分钟的主成分分析(PCA)的直观说明。
  • seaborn 教程_使用Seaborn进行数据可视化教程
  • alexnet 结构_AlexNet的体系结构和实现
  • python做作业没头绪_使用Python做作业
  • 使用Python构建推荐系统的机器学习
  • DeepR —训练TensorFlow模型进行生产
  • 通化红灯_我们如何构建廉价,可扩展的架构来对世界进行卡通化!
  • 机器学习学习吴恩达逻辑回归_机器学习基础:逻辑回归
  • 软件测试 测试停止标准_停止正常测试
  • cloud 部署_使用Google Cloud AI平台开发,训练和部署TensorFlow模型
  • 机器学习多元线性回归_过度简化的机器学习(1):多元回归
  • 机器学习与分布式机器学习_机器学习的歧义
  • 单词嵌入_单词嵌入与单词袋:推荐系统的奇怪案例

python 梯度下降_Python解释的闭合形式和梯度下降回归相关推荐

  1. python 分位数 位置_Python解释数学系列——分位数Quantile

    1. 分位数计算案例与Python代码 案例1 Ex1: Given a data = [6, 47, 49, 15, 42, 41, 7, 39, 43, 40, 36],求Q1, Q2, Q3, ...

  2. 关闭python解释器快捷键_Python解释器之Spyde和PyCharm中的一些快捷键

    熟练spyder中的一些快捷键后,能极大提升code效率 Anaconda中有两个Python解释器,一个是Spyde,另一个是Jupyter notebook,个人习惯是喜欢用Spyde Spyde ...

  3. python赋值语句格式_Python中变量和变量赋值的几种形式

    动态类型的语言 python是动态类型的语言,不需要声明变量的类型. 实际上,python中的变量仅仅只是用来保存一个数据对象的地址.无论是什么数据对象,在内存中创建好数据对象之后,都只是把它的地址保 ...

  4. python迭代算法_Python实现简单的梯度下降法

    Python 实现简单的梯度下降法 机器学习算法常常可以归结为求解一个最优化问题,而梯度下降法就是求解最优化问题的一个方法. 梯度下降法(gradient descent)或最速下降法(steepes ...

  5. 梯度下降法的三种形式批量梯度下降法、随机梯度下降以及小批量梯度下降法

    梯度下降法的三种形式BGD.SGD以及MBGD 梯度下降法的三种形式BGD.SGD以及MBGD 阅读目录 1. 批量梯度下降法BGD 2. 随机梯度下降法SGD 3. 小批量梯度下降法MBGD 4. ...

  6. python采用解释方式执行_Python解释执行原理

    谈到了Python语句的两种执行方式,实际上,这两种运行方式在本质 上是相同的,它们都是由解释器来解释执行我们提供的Python语句. 这里所说的解释执行是相对于编译执行而言的.我们知道,使用诸如 C ...

  7. 梯度下降法的三种形式BGD(批量梯度下降)、SGD(随机梯度下降)以及MBGD(小批量梯度下降)

    在应用机器学习算法时,我们通常采用梯度下降法来对采用的算法进行训练.其实,常用的梯度下降法还具体包含有三种不同的形式,它们也各自有着不同的优缺点. 下面我们以线性回归算法来对三种梯度下降法进行比较. ...

  8. python是一种解释型、面向什么的计算机程序设计语言_python语言是一种什么类型...

    python语言是一种什么类型,是一种,类型,语言,是在,程序 python语言是一种什么类型 易采站长站,站长之家为您整理了python语言是一种什么类型的相关内容. python语言是一种什么类型 ...

  9. python是一种面向____的高级语言_Python 基础教程Python是一种解释型、面向对象、动...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 Python 基础教程 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python由Guido van Rossum于1989年底 ...

最新文章

  1. 商汤及联合实验室入选论文重点解读 | ECCV 2018
  2. 《JavaScript高级程序设计》心得笔记-----第四篇章
  3. chosen组件实现下拉框
  4. Flink 能够改写成 FlinkSQL的理论依据:命令式代码 vs 声明式代码
  5. 编译log4cplus-2.0.x备忘录
  6. 三角形分类(洛谷P5717题题解,Java语言描述)
  7. JavaScript进阶部分笔记
  8. JS:1.5,日期(Date)对象
  9. ddos发包php文件,简单防范PHPDDOS对外发UDP包消耗流量
  10. 教你如何修改树莓派的时区和网络对时
  11. 用Gitosis搭建Git服务器(经典资料)
  12. 矩形排样程序matlab,矩形优化排样
  13. 高性能JSON框架之FastJson的简单使用
  14. 微信公众号信息推送实现业务解耦
  15. 查看yum安装软件的目录
  16. 备份Ubuntu 并制作成iso安装文件
  17. 2020年东北三省数学建模联赛赛题
  18. Python初级学习教程—从入门开始学习(函数、组合数据类型、文件操作、异常、模块)
  19. Word安全警告 宏已被禁用解决
  20. 2019互联网公司100强

热门文章

  1. 微信支付 企业转账 小程序发红包 提现 发红包 企业支付等遇到的问题
  2. 成立仅8个月的个人网站,月收入几十万美金
  3. 【虚拟化实战】Cluster设计之一资源池
  4. 关于 Repeater 控件嵌套的使用。在嵌套中添加 其它控件(如:按钮),并影响其它控件的方法,很重要哦,测试通过。...
  5. SQL中的存储过程中的事务处理。备忘
  6. 实现Profile购物车的匿名用户迁移
  7. keil的主要功能和作用_组合式空调机组各功能段的主要作用
  8. 基于UML的面向对象分析与设计
  9. 出现画面抖动_解析液晶拼接大屏在使用中出现的常见问题及解决方案
  10. 数据库 记录php 全屏编辑,Thinkphp5数据库操作源码