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

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

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

hθ=nj=0θjxj

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

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

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


1. 批量梯度下降法BGD

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

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

梯度下降法的三种形式BGD、SGD以及MBGD在spark-LMLPHP

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

梯度下降法的三种形式BGD、SGD以及MBGD在spark-LMLPHP

  具体的伪代码形式为:

  repeat{    

      梯度下降法的三种形式BGD、SGD以及MBGD在spark-LMLPHP

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

  }

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

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

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

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


2. 随机梯度下降法SGD

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

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

梯度下降法的三种形式BGD、SGD以及MBGD在spark-LMLPHP

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

梯度下降法的三种形式BGD、SGD以及MBGD在spark-LMLPHP

  具体的伪代码形式为:

  1. Randomly shuffle dataset;

  2. repeat{

    for i=1, ... , m{

      梯度下降法的三种形式BGD、SGD以及MBGD在spark-LMLPHP

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

    }

  }

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

  优点:训练速度快;

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

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


3. 小批量梯度下降法MBGD

  有上述的两种梯度下降法可以看出,其各自均有优缺点,那么能不能在两种方法的性能之间取得一个折衷呢?即,算法的训练过程比较快,而且也要保证最终参数训练的准确率,而这正是小批量梯度下降法(Mini-batchGradient 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包下:
http://spark.apache.org/docs/2.2.0/api/scala/#org.apache.spark.mllib.classification.package

Type Members

  1. trait ClassificationModel extends Serializable

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

    Annotations @Since( "0.8.0" )
  2. class LogisticRegressionModel extends GeneralizedLinearModel with ClassificationModel with Serializable with Saveable with PMMLExportable

    Classification model trained using Multinomial/Binary Logistic Regression.

  3. class LogisticRegressionWithLBFGS extends GeneralizedLinearAlgorithm[LogisticRegressionModel] with Serializable

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

  4. class LogisticRegressionWithSGD extends GeneralizedLinearAlgorithm[LogisticRegressionModel] with Serializable

    Train a classification model for Binary Logistic Regressionusing Stochastic Gradient Descent.

  5. class NaiveBayes extends Serializable with Logging

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

  6. class NaiveBayesModel extends ClassificationModel with Serializable with Saveable

    Model for Naive Bayes Classifiers.

  7. class SVMModel extends GeneralizedLinearModel with ClassificationModel with Serializable with Saveable with PMMLExportable

    Model for Support Vector Machines (SVMs).

  8. class SVMWithSGD extends GeneralizedLinearAlgorithm[SVMModel] with Serializable

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

  9. class StreamingLogisticRegressionWithSGD extends StreamingLinearAlgorithm[LogisticRegressionModel, LogisticRegressionWithSGD] with Serializable

    Train or predict a logistic regression model on streaming data.

Value Members

  1. object LogisticRegressionModel extends Loader[LogisticRegressionModel] with Serializable

  2. object NaiveBayes extends Serializable

    Top-level methods for calling naive Bayes.

  3. object NaiveBayesModel extends Loader[NaiveBayesModel] with Serializable

  4. object SVMModel extends Loader[SVMModel] with Serializable

  5. object SVMWithSGD extends Serializable

    Top-level methods for calling SVM.

Deprecated Value Members

  1. object LogisticRegressionWithSGD extends Serializable

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

    org.apache.spark.ml.classification包下:
    http://spark.apache.org/docs/2.2.0/api/scala/#org.apache.spark.ml.classification.package

    Type Members

    1. Experimental class BinaryLogisticRegressionSummary extends LogisticRegressionSummary

      Binary Logistic regression results for a given model.

    2. Experimental class BinaryLogisticRegressionTrainingSummary extends BinaryLogisticRegressionSummary with LogisticRegressionTrainingSummary

      Logistic regression training results.

    3. Developer API abstract class ClassificationModel[FeaturesType, M ClassificationModel[FeaturesType, M]] extends PredictionModel[FeaturesType, M] with ClassifierParams

    4. Developer API abstract class Classifier[FeaturesType, E Classifier[FeaturesType, E, M], M ClassificationModel[FeaturesType, M]] extends Predictor[FeaturesType, E, M] with ClassifierParams

    5. class DecisionTreeClassificationModel extends ProbabilisticClassificationModel[Vector, DecisionTreeClassificationModel] with DecisionTreeModel with DecisionTreeClassifierParams with MLWritable with Serializable

      梯度下降法的三种形式BGD、SGD以及MBGD在spark-LMLPHP

      Decision tree model (http://en.wikipedia.org/wiki/Decision_tree_learning) for classification.

    6. class DecisionTreeClassifier extends ProbabilisticClassifier[Vector, DecisionTreeClassifier, DecisionTreeClassificationModel] with DecisionTreeClassifierParams with DefaultParamsWritable

      Decision tree learning algorithm (http://en.wikipedia.org/wiki/Decision_tree_learning)for classification.

    7. class GBTClassificationModel extends ProbabilisticClassificationModel[Vector, GBTClassificationModel] with GBTClassifierParams with TreeEnsembleModel[DecisionTreeRegressionModel] with MLWritable with Serializable

      Gradient-Boosted Trees (GBTs) (http://en.wikipedia.org/wiki/Gradient_boosting)model for classification.

    8. class GBTClassifier extends ProbabilisticClassifier[Vector, GBTClassifier, GBTClassificationModel] with GBTClassifierParams with DefaultParamsWritable with Logging

      Gradient-Boosted Trees (GBTs) (http://en.wikipedia.org/wiki/Gradient_boosting)learning algorithm for classification.

    9. Experimental class LinearSVC extends Classifier[Vector, LinearSVC, LinearSVCModel] with LinearSVCParams with DefaultParamsWritable

    10. Experimental class LinearSVCModel extends ClassificationModel[Vector, LinearSVCModel] with LinearSVCParams with MLWritable

      Linear SVM Model trained by LinearSVC

    11. class LogisticRegression extends ProbabilisticClassifier[Vector, LogisticRegression, LogisticRegressionModel] with LogisticRegressionParams with DefaultParamsWritable with Logging

      Logistic regression.

    12. class LogisticRegressionModel extends ProbabilisticClassificationModel[Vector, LogisticRegressionModel] with LogisticRegressionParams with MLWritable

      Model produced by LogisticRegression.

    13. sealed trait LogisticRegressionSummary extends Serializable

      Abstraction for Logistic Regression Results for a given model.

    14. sealed trait LogisticRegressionTrainingSummary extends LogisticRegressionSummary

      Abstraction for multinomial Logistic Regression Training results.

    15. class MultilayerPerceptronClassificationModel extends PredictionModel[Vector, MultilayerPerceptronClassificationModel] with Serializable with MLWritable

      Classification model based on the Multilayer Perceptron.

    16. class MultilayerPerceptronClassifier extends Predictor[Vector, MultilayerPerceptronClassifier, MultilayerPerceptronClassificationModel] with MultilayerPerceptronParams with DefaultParamsWritable

      Classifier trainer based on the Multilayer Perceptron.

    17. class NaiveBayes extends ProbabilisticClassifier[Vector, NaiveBayes, NaiveBayesModel] with NaiveBayesParams with DefaultParamsWritable

      Naive Bayes Classifiers.

    18. class NaiveBayesModel extends ProbabilisticClassificationModel[Vector, NaiveBayesModel] with NaiveBayesParams with MLWritable

      Model produced by NaiveBayes

    19. final class OneVsRest extends Estimator[OneVsRestModel] with OneVsRestParams with MLWritable

      Reduction of Multiclass Classification to Binary Classification.

    20. final class OneVsRestModel extends Model[OneVsRestModel] with OneVsRestParams with MLWritable

      Model produced by OneVsRest.

    21. Developer API abstract class ProbabilisticClassificationModel[FeaturesType, M ProbabilisticClassificationModel[FeaturesType, M]] extends ClassificationModel[FeaturesType, M] with ProbabilisticClassifierParams

    22. Developer API abstract class ProbabilisticClassifier[FeaturesType, E ProbabilisticClassifier[FeaturesType, E, M], M ProbabilisticClassificationModel[FeaturesType, M]] extends Classifier[FeaturesType, E, M] with ProbabilisticClassifierParams

    23. class RandomForestClassificationModel extends ProbabilisticClassificationModel[Vector, RandomForestClassificationModel] with RandomForestClassifierParams with TreeEnsembleModel[DecisionTreeClassificationModel] with MLWritable with Serializable

      Random Forest model for classification.

    24. class RandomForestClassifier extends ProbabilisticClassifier[Vector, RandomForestClassifier, RandomForestClassificationModel] with RandomForestClassifierParams with DefaultParamsWritable

      Random Forest learning algorithm forclassification.

    Value Members

    1. object DecisionTreeClassificationModel extends MLReadable[DecisionTreeClassificationModel] with Serializable

    2. object DecisionTreeClassifier extends DefaultParamsReadable[DecisionTreeClassifier] with Serializable

    3. object GBTClassificationModel extends MLReadable[GBTClassificationModel] with Serializable

    4. object GBTClassifier extends DefaultParamsReadable[GBTClassifier] with Serializable

    5. object LinearSVC extends DefaultParamsReadable[LinearSVC] with Serializable

    6. object LinearSVCModel extends MLReadable[LinearSVCModel] with Serializable

    7. object LogisticRegression extends DefaultParamsReadable[LogisticRegression] with Serializable

    8. object LogisticRegressionModel extends MLReadable[LogisticRegressionModel] with Serializable

    9. object MultilayerPerceptronClassificationModel extends MLReadable[MultilayerPerceptronClassificationModel] with Serializable

    10. object MultilayerPerceptronClassifier extends DefaultParamsReadable[MultilayerPerceptronClassifier] with Serializable

    11. object NaiveBayes extends DefaultParamsReadable[NaiveBayes] with Serializable

    12. object NaiveBayesModel extends MLReadable[NaiveBayesModel] with Serializable

    13. object OneVsRest extends MLReadable[OneVsRest] with Serializable

    14. object OneVsRestModel extends MLReadable[OneVsRestModel] with Serializable

    15. object RandomForestClassificationModel extends MLReadable[RandomForestClassificationModel] with Serializable

    16. object RandomForestClassifier extends DefaultParamsReadable[RandomForestClassifier] with Serializable



10-31 10:27
查看更多