Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5956263
  • 博文数量: 680
  • 博客积分: 10821
  • 博客等级: 上将
  • 技术积分: 11839
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-02 10:41
个人简介

大数据、ML、AI、云计算openstack、Linux、SpringCloud。

文章分类

全部博文(680)

分类: 大数据

2017-07-17 14:26:45

一、算法介绍
在应用机器学习算法时,我们通常采用梯度下降法来对采用的算法进行训练。其实,常用的梯度下降法还具体包含有三种不同的形式,它们也各自有着不同的优缺点。

  下面我们以线性回归算法来对三种梯度下降法进行比较。

  一般线性回归函数的假设函数为:

hθ=nj=0θjxj

  对应的能量函数(损失函数)形式为:

Jtrain(θ)=1/(2m)mi=1(hθ(x(i))?y(i))2

  下图为一个二维参数(θ0θ1)组对应能量函数的可视化图:


1. 批量梯度下降法BGD

   批量梯度下降法(Batch Gradient Descent,简称BGD)是梯度下降法最原始的形式,它的具体思路是在更新每一参数时都使用所有的样本来进行更新,其数学形式如下:

  (1) 对上述的能量函数求偏导:

  (2) 由于是最小化风险函数,所以按照每个参数θ的梯度负方向来更新每个θ

  具体的伪代码形式为:

  repeat{    

      

        (for every j=0, ... , n)

  }

  从上面公式可以注意到,它得到的是一个全局最优解,但是每迭代一步,都要用到训练集所有的数据,如果样本数目m很大,那么可想而知这种方法的迭代速度!所以,这就引入了另外一种方法,随机梯度下降。

  优点:全局最优解;易于并行实现;

  缺点:当样本数目很多时,训练过程会很慢。

  从迭代的次数上来看,BGD迭代的次数相对较少。其迭代的收敛曲线示意图可以表示如下:


2. 随机梯度下降法SGD

  由于批量梯度下降法在更新每一个参数时,都需要所有的训练样本,所以训练过程会随着样本数量的加大而变得异常的缓慢。随机梯度下降法(Stochastic Gradient Descent,简称SGD)正是为了解决批量梯度下降法这一弊端而提出的。

  将上面的能量函数写为如下形式:

  利用每个样本的损失函数对θ求偏导得到对应的梯度,来更新θ

  具体的伪代码形式为:

  1. Randomly shuffle dataset;

  2. repeat{

    for i=1, ... , m{

      

       (for j=0, ... , n)

    }

  }

  随机梯度下降是通过每个样本来迭代更新一次,如果样本量很大的情况(例如几十 万),那么可能只用其中几万条或者几千条的样本,就已经将theta迭代到最优解了,对比上面的批量梯度下降,迭代一次需要用到十几万训练样本,一次迭代 不可能最优,如果迭代10次的话就需要遍历训练样本10次。但是,SGD伴随的一个问题是噪音较BGD要多,使得SGD并不是每次迭代都向着整体最优化方 向。

  优点:训练速度快;

  缺点:准确度下降,并不是全局最优;不易于并行实现。

  从迭代的次数上来看,SGD迭代的次数较多,在解空间的搜索过程看起来很盲目。其迭代的收敛曲线示意图可以表示如下:


3. 小批量梯度下降法MBGD

  有上述的两种梯度下降法可以看出,其各自均有优缺点,那么能不能在两种方法的 性能之间取得一个折衷呢?即,算法的训练过程比较快,而且也要保证最终参数训练的准确率,而这正是小批量梯度下降法(Mini-batch Gradient Descent,简称MBGD)的初衷。

  MBGD在每次更新参数时使用b个样本(b一般为10),其具体的伪代码形式为:

  Say b=10, m=1000.

  Repeat{

    for i=1, 11, 21, 31, ... , 991{

    

    (for every j=0, ... , n)

    }

  }


4. 总结

  Batch gradient descent: Use all examples in each iteration;

  Stochastic gradient descent: Use 1 example in each iteration;

  Mini-batch gradient descent: Use b examples in each iteration.

二、spark实现
   
在spark2.2.0中:org.apache.spark.mllib.classification包下:

Type Members

  1. trait extends Serializable

    Represents a classification model that predicts to which of a set of categories an example belongs. The categories are represented by double values: 0.0, 1.0, 2.0, etc.

    Annotations @Since( "0.8.0" )
  2. class extends with with Serializable with with

    Classification model trained using Multinomial/Binary Logistic Regression.

  3. class extends [] with Serializable

    Train a classification model for Multinomial/Binary Logistic Regression using Limited-memory BFGS.

  4. class extends [] with Serializable

    Train a classification model for Binary Logistic Regression using Stochastic Gradient Descent.

  5. class extends Serializable with

    Trains a Naive Bayes model given an RDD of (label, features) pairs.

  6. class extends with Serializable with

    Model for Naive Bayes Classifiers.

  7. class extends with with Serializable with with

    Model for Support Vector Machines (SVMs).

  8. class extends [] with Serializable

    Train a Support Vector Machine (SVM) using Stochastic Gradient Descent.

  9. class extends [, ] with Serializable

    Train or predict a logistic regression model on streaming data.

Value Members

  1. object extends [] with Serializable

  2. object extends Serializable

    Top-level methods for calling naive Bayes.

  3. object extends [] with Serializable

  4. object extends [] with Serializable

  5. object extends Serializable

    Top-level methods for calling SVM.

Deprecated Value Members

  1. object extends Serializable

    Top-level methods for calling Logistic Regression using Stochastic Gradient Descent.

    org.apache.spark.ml.classification包下:

    Type Members

    1. Experimental class extends

      Binary Logistic regression results for a given model.

    2. Experimental class extends with

      Logistic regression training results.

    3. Developer API abstract class [FeaturesType, M <: [FeaturesType, M]] extends [FeaturesType, M] with ClassifierParams

    4. Developer API abstract class [FeaturesType, E <: [FeaturesType, E, M], M <: [FeaturesType, M]] extends [FeaturesType, E, M] with ClassifierParams

    5. class extends [, ] with DecisionTreeModel with DecisionTreeClassifierParams with with Serializable

      Decision tree model () for classification.

    6. class extends [, , ] with DecisionTreeClassifierParams with

      Decision tree learning algorithm () for classification.

    7. class extends [, ] with GBTClassifierParams with TreeEnsembleModel[] with with Serializable

      Gradient-Boosted Trees (GBTs) () model for classification.

    8. class extends [, , ] with GBTClassifierParams with with

      Gradient-Boosted Trees (GBTs) () learning algorithm for classification.

    9. Experimental class extends [, , ] with LinearSVCParams with

    10. Experimental class extends [, ] with LinearSVCParams with

      Linear SVM Model trained by

    11. class extends [, , ] with LogisticRegressionParams with with

      Logistic regression.

    12. class extends [, ] with LogisticRegressionParams with

      Model produced by .

    13. sealed trait extends Serializable

      Abstraction for Logistic Regression Results for a given model.

    14. sealed trait extends

      Abstraction for multinomial Logistic Regression Training results.

    15. class extends [, ] with Serializable with

      Classification model based on the Multilayer Perceptron.

    16. class extends [, , ] with MultilayerPerceptronParams with

      Classifier trainer based on the Multilayer Perceptron.

    17. class extends [, , ] with NaiveBayesParams with

      Naive Bayes Classifiers.

    18. class extends [, ] with NaiveBayesParams with

      Model produced by

    19. final class extends [] with OneVsRestParams with

      Reduction of Multiclass Classification to Binary Classification.

    20. final class extends [] with OneVsRestParams with

      Model produced by .

    21. Developer API abstract class [FeaturesType, M <: [FeaturesType, M]] extends [FeaturesType, M] with ProbabilisticClassifierParams

    22. Developer API abstract class [FeaturesType, E <: [FeaturesType, E, M], M <: [FeaturesType, M]] extends [FeaturesType, E, M] with ProbabilisticClassifierParams

    23. class extends [, ] with RandomForestClassifierParams with TreeEnsembleModel[] with with Serializable

      model for classification.

    24. class extends [, , ] with RandomForestClassifierParams with

      learning algorithm for classification.

    Value Members

    1. object extends [] with Serializable

    2. object extends [] with Serializable

    3. object extends [] with Serializable

    4. object extends [] with Serializable

    5. object extends [] with Serializable

    6. object extends [] with Serializable

    7. object extends [] with Serializable

    8. object extends [] with Serializable

    9. object extends [] with Serializable

    10. object extends [] with Serializable

    11. object extends [] with Serializable

    12. object extends [] with Serializable

    13. object extends [] with Serializable

    14. object extends [] with Serializable

    15. object extends [] with Serializable

    16. object extends [] with Serializable



阅读(5937) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~