在应用机器学习算法时,我们通常采用梯度下降法来对采用的算法进行训练。其实,常用的梯度下降法还具体包含有三种不同的形式,它们也各自有着不同的优缺点。
下面我们以线性回归算法来对三种梯度下降法进行比较。
一般线性回归函数的假设函数为:
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-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
trait ClassificationModel extends Serializable
Annotations @Since( "0.8.0" )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.
class LogisticRegressionModel extends GeneralizedLinearModel with ClassificationModel with Serializable with Saveable with PMMLExportable
Classification model trained using Multinomial/Binary Logistic Regression.
class LogisticRegressionWithLBFGS extends GeneralizedLinearAlgorithm[LogisticRegressionModel] with Serializable
Train a classification model for Multinomial/Binary Logistic Regression usingLimited-memory BFGS.
class LogisticRegressionWithSGD extends GeneralizedLinearAlgorithm[LogisticRegressionModel] with Serializable
Train a classification model for Binary Logistic Regressionusing Stochastic Gradient Descent.
class NaiveBayes extends Serializable with Logging
Trains a Naive Bayes model given an RDD of (label, features) pairs.
class NaiveBayesModel extends ClassificationModel with Serializable with Saveable
Model for Naive Bayes Classifiers.
class SVMModel extends GeneralizedLinearModel with ClassificationModel with Serializable with Saveable with PMMLExportable
Model for Support Vector Machines (SVMs).
class SVMWithSGD extends GeneralizedLinearAlgorithm[SVMModel] with Serializable
Train a Support Vector Machine (SVM) using Stochastic Gradient Descent.
class StreamingLogisticRegressionWithSGD extends StreamingLinearAlgorithm[LogisticRegressionModel, LogisticRegressionWithSGD] with Serializable
Train or predict a logistic regression model on streaming data.
Value Members
object LogisticRegressionModel extends Loader[LogisticRegressionModel] with Serializable
object NaiveBayes extends Serializable
Top-level methods for calling naive Bayes.
object NaiveBayesModel extends Loader[NaiveBayesModel] with Serializable
object SVMModel extends Loader[SVMModel] with Serializable
object SVMWithSGD extends Serializable
Top-level methods for calling SVM.
Deprecated Value Members
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.packageType Members
Experimental class BinaryLogisticRegressionSummary extends LogisticRegressionSummary
Binary Logistic regression results for a given model.
Experimental class BinaryLogisticRegressionTrainingSummary extends BinaryLogisticRegressionSummary with LogisticRegressionTrainingSummary
Logistic regression training results.
Developer API abstract class ClassificationModel[FeaturesType, M ClassificationModel[FeaturesType, M]] extends PredictionModel[FeaturesType, M] with ClassifierParams
Developer API abstract class Classifier[FeaturesType, E Classifier[FeaturesType, E, M], M ClassificationModel[FeaturesType, M]] extends Predictor[FeaturesType, E, M] with ClassifierParams
class DecisionTreeClassificationModel extends ProbabilisticClassificationModel[Vector, DecisionTreeClassificationModel] with DecisionTreeModel with DecisionTreeClassifierParams with MLWritable with Serializable
Decision tree model (http://en.wikipedia.org/wiki/Decision_tree_learning) for classification.
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.
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.
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.
Experimental class LinearSVC extends Classifier[Vector, LinearSVC, LinearSVCModel] with LinearSVCParams with DefaultParamsWritable
Experimental class LinearSVCModel extends ClassificationModel[Vector, LinearSVCModel] with LinearSVCParams with MLWritable
Linear SVM Model trained by LinearSVC
class LogisticRegression extends ProbabilisticClassifier[Vector, LogisticRegression, LogisticRegressionModel] with LogisticRegressionParams with DefaultParamsWritable with Logging
Logistic regression.
class LogisticRegressionModel extends ProbabilisticClassificationModel[Vector, LogisticRegressionModel] with LogisticRegressionParams with MLWritable
Model produced by LogisticRegression.
sealed trait LogisticRegressionSummary extends Serializable
Abstraction for Logistic Regression Results for a given model.
sealed trait LogisticRegressionTrainingSummary extends LogisticRegressionSummary
Abstraction for multinomial Logistic Regression Training results.
class MultilayerPerceptronClassificationModel extends PredictionModel[Vector, MultilayerPerceptronClassificationModel] with Serializable with MLWritable
Classification model based on the Multilayer Perceptron.
class MultilayerPerceptronClassifier extends Predictor[Vector, MultilayerPerceptronClassifier, MultilayerPerceptronClassificationModel] with MultilayerPerceptronParams with DefaultParamsWritable
Classifier trainer based on the Multilayer Perceptron.
class NaiveBayes extends ProbabilisticClassifier[Vector, NaiveBayes, NaiveBayesModel] with NaiveBayesParams with DefaultParamsWritable
Naive Bayes Classifiers.
class NaiveBayesModel extends ProbabilisticClassificationModel[Vector, NaiveBayesModel] with NaiveBayesParams with MLWritable
Model produced by NaiveBayes
final class OneVsRest extends Estimator[OneVsRestModel] with OneVsRestParams with MLWritable
Reduction of Multiclass Classification to Binary Classification.
final class OneVsRestModel extends Model[OneVsRestModel] with OneVsRestParams with MLWritable
Model produced by OneVsRest.
Developer API abstract class ProbabilisticClassificationModel[FeaturesType, M ProbabilisticClassificationModel[FeaturesType, M]] extends ClassificationModel[FeaturesType, M] with ProbabilisticClassifierParams
Developer API abstract class ProbabilisticClassifier[FeaturesType, E ProbabilisticClassifier[FeaturesType, E, M], M ProbabilisticClassificationModel[FeaturesType, M]] extends Classifier[FeaturesType, E, M] with ProbabilisticClassifierParams
class RandomForestClassificationModel extends ProbabilisticClassificationModel[Vector, RandomForestClassificationModel] with RandomForestClassifierParams with TreeEnsembleModel[DecisionTreeClassificationModel] with MLWritable with Serializable
Random Forest model for classification.
class RandomForestClassifier extends ProbabilisticClassifier[Vector, RandomForestClassifier, RandomForestClassificationModel] with RandomForestClassifierParams with DefaultParamsWritable
Random Forest learning algorithm forclassification.
Value Members
object DecisionTreeClassificationModel extends MLReadable[DecisionTreeClassificationModel] with Serializable
object DecisionTreeClassifier extends DefaultParamsReadable[DecisionTreeClassifier] with Serializable
object GBTClassificationModel extends MLReadable[GBTClassificationModel] with Serializable
object GBTClassifier extends DefaultParamsReadable[GBTClassifier] with Serializable
object LinearSVC extends DefaultParamsReadable[LinearSVC] with Serializable
object LinearSVCModel extends MLReadable[LinearSVCModel] with Serializable
object LogisticRegression extends DefaultParamsReadable[LogisticRegression] with Serializable
object LogisticRegressionModel extends MLReadable[LogisticRegressionModel] with Serializable
object MultilayerPerceptronClassificationModel extends MLReadable[MultilayerPerceptronClassificationModel] with Serializable
object MultilayerPerceptronClassifier extends DefaultParamsReadable[MultilayerPerceptronClassifier] with Serializable
object NaiveBayes extends DefaultParamsReadable[NaiveBayes] with Serializable
object NaiveBayesModel extends MLReadable[NaiveBayesModel] with Serializable
object OneVsRest extends MLReadable[OneVsRest] with Serializable
object OneVsRestModel extends MLReadable[OneVsRestModel] with Serializable
object RandomForestClassificationModel extends MLReadable[RandomForestClassificationModel] with Serializable
object RandomForestClassifier extends DefaultParamsReadable[RandomForestClassifier] with Serializable