6.md 23.1 KB
Newer Older
L
loopyme 已提交
1
# 1.5. 随机梯度下降
W
init  
wizardforcel 已提交
2 3 4 5

校验者:
        [@A](https://github.com/apachecn/scikit-learn-doc-zh)
        [@HelloSilicat](https://github.com/HelloSilicat)
6
        [@Loopy](https://github.com/loopyme)
H
Hanmin Qin 已提交
7
        [@qinhanmin2014](https://github.com/qinhanmin2014)
W
init  
wizardforcel 已提交
8 9 10 11 12 13 14 15 16
翻译者:
        [@L](https://github.com/apachecn/scikit-learn-doc-zh)

**随机梯度下降(SGD)** 是一种简单但又非常高效的方法,主要用于凸损失函数下线性分类器的判别式学习,例如(线性) [支持向量机](https://en.wikipedia.org/wiki/Support_vector_machine)[Logistic 回归](https://en.wikipedia.org/wiki/Logistic_regression) 。 尽管 SGD 在机器学习社区已经存在了很长时间, 但是最近在 large-scale learning (大规模学习)方面 SGD 获得了相当大的关注。

SGD 已成功应用于在文本分类和自然语言处理中经常遇到的大规模和稀疏的机器学习问题。对于稀疏数据,本模块的分类器可以轻易的处理超过 10^5 的训练样本和超过 10^5 的特征。

Stochastic Gradient Descent (随机梯度下降法)的优势:

17 18
 *   高效。
 *   易于实现 (有大量优化代码的机会)。
W
init  
wizardforcel 已提交
19 20 21

Stochastic Gradient Descent (随机梯度下降法)的劣势:

22 23
 *   SGD 需要一些超参数,例如 regularization (正则化)参数和 number of iterations (迭代次数)。
 *   SGD 对 feature scaling (特征缩放)敏感。
W
init  
wizardforcel 已提交
24

L
loopyme 已提交
25
## 1.5.1. 分类
W
init  
wizardforcel 已提交
26

27 28
>**警告**:
>
H
Hanmin Qin 已提交
29
>在拟合模型前,确保你重新排列了(打乱)你的训练数据,或者使用 `shuffle=True` 在每次迭代后打乱训练数据。
W
init  
wizardforcel 已提交
30

H
Hanmin Qin 已提交
31
[`SGDClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier) 类实现了一个简单的随机梯度下降学习例程, 支持分类问题不同的损失函数和正则化方法。
W
init  
wizardforcel 已提交
32

33
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_sgd_separating_hyperplane_0011.png](img/b3206aa7b52a9c0918727730873d1363.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_separating_hyperplane.html)
W
init  
wizardforcel 已提交
34 35 36 37 38 39 40 41 42

作为另一个 classifier (分类器), 拟合 SGD 我们需要两个 array (数组):保存训练样本的 size 为 [n_samples, n_features] 的数组 X 以及保存训练样本目标值(类标签)的 size 为 [n_samples] 的数组 Y

```py
>>> from sklearn.linear_model import SGDClassifier
>>> X = [[0., 0.], [1., 1.]]
>>> y = [0, 1]
>>> clf = SGDClassifier(loss="hinge", penalty="l2")
>>> clf.fit(X, y)
H
Hanmin Qin 已提交
43 44 45 46 47 48
SGDClassifier(alpha=0.0001, average=False, class_weight=None,
              early_stopping=False, epsilon=0.1, eta0=0.0, fit_intercept=True,
              l1_ratio=0.15, learning_rate='optimal', loss='hinge',
              max_iter=1000, n_iter_no_change=5, n_jobs=None, penalty='l2',
              power_t=0.5, random_state=None, shuffle=True, tol=0.001,
              validation_fraction=0.1, verbose=0, warm_start=False)
W
init  
wizardforcel 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

```

拟合后,我们可以用该模型来预测新值:

```py
>>> clf.predict([[2., 2.]])
array([1])

```

SGD 通过训练数据来拟合一个线性模型。成员 `coef_` 保存模型参数:

```py
>>> clf.coef_                                         
array([[ 9.9...,  9.9...]])

```

成员 `intercept_` 保存 intercept(截距) (又称作 offset(偏移)或 bias(偏差)):

```py
>>> clf.intercept_                                    
array([-9.9...])

```

模型是否使用 intercept(截距), 即 a biased hyperplane(一个偏置的超平面), 是由参数 `fit_intercept` 控制的。

78
使用 [`SGDClassifier.decision_function`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier.decision_function "sklearn.linear_model.SGDClassifier.decision_function") 来获得到此超平面的 signed distance (符号距离)
W
init  
wizardforcel 已提交
79 80 81 82 83 84 85

```py
>>> clf.decision_function([[2., 2.]])                 
array([ 29.6...])

```

86
具体的 loss function(损失函数) 可以通过 `loss` 参数来设置。 [`SGDClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier) 支持以下的 loss functions(损失函数):
W
init  
wizardforcel 已提交
87

88 89 90 91
 *   `loss="hinge"`: (soft-margin) linear Support Vector Machine ((软-间隔)线性支持向量机),
 *   `loss="modified_huber"`: smoothed hinge loss (平滑的 hinge 损失),
 *   `loss="log"`: logistic regression (logistic 回归),
 *   and all regression losses below(以及所有的回归损失)。
W
init  
wizardforcel 已提交
92

片刻小哥哥's avatar
片刻小哥哥 已提交
93
前两个 loss functions(损失函数)是懒惰的,只有一个示例违反了 margin constraint(边界约束),它们才更新模型的参数, 这使得训练非常有效率,即使使用了 L2 penalty(惩罚)我们仍然可能得到稀疏的模型结果。
W
init  
wizardforcel 已提交
94 95 96 97 98 99 100 101 102 103 104 105

使用 `loss="log"` 或者 `loss="modified_huber"` 来启用 `predict_proba` 方法, 其给出每个样本 ![x](img/5c82dbae35dc43d2f556f9f284d9d184.jpg) 的概率估计 ![P(y|x)](img/3cca81fd08a4732dc7061cd246b323ed.jpg) 的一个向量:

```py
>>> clf = SGDClassifier(loss="log").fit(X, y)
>>> clf.predict_proba([[1., 1.]])                      
array([[ 0.00...,  0.99...]])

```

具体的惩罚方法可以通过 `penalty` 参数来设定。 SGD 支持以下 penalties(惩罚):

106 107 108
 *   `penalty="l2"`: L2 norm penalty on `coef_`.
 *   `penalty="l1"`: L1 norm penalty on `coef_`.
 *   `penalty="elasticnet"`: Convex combination of L2 and L1(L2 型和 L1 型的凸组合); `(1 - l1_ratio) * L2 + l1_ratio * L1`.
W
init  
wizardforcel 已提交
109 110 111

默认设置为 `penalty="l2"` 。 L1 penalty (惩罚)导致稀疏解,使得大多数系数为零。 Elastic Net(弹性网)解决了在特征高相关时 L1 penalty(惩罚)的一些不足。参数 `l1_ratio` 控制了 L1 和 L2 penalty(惩罚)的 convex combination (凸组合)。

112
[`SGDClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier) 通过利用 “one versus all” (OVA)方法来组合多个二分类器,从而实现多分类。对于每一个 ![K](img/e279b8169ddd6581c5606c868ba52fae.jpg) 类, 可以训练一个二分类器来区分自身和其他 ![K-1](img/7ce09555ac9e490df7f81ef7eb0e58e8.jpg) 个类。在测试阶段,我们计算每个分类器的 confidence score(置信度分数)(也就是与超平面的距离),并选择置信度最高的分类。下图阐释了基于 iris(鸢尾花)数据集上的 OVA 方法。虚线表示三个 OVA 分类器; 不同背景色代表由三个分类器产生的决策面。
W
init  
wizardforcel 已提交
113

114
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_sgd_iris_0011.png](img/7ec998f799acf05b040856bc6b37657f.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_iris.html)
W
init  
wizardforcel 已提交
115 116 117

在 multi-class classification (多类分类)的情况下, `coef_``shape=[n_classes, n_features]` 的一个二维数组, `intercept_``shape=[n_classes]` 的一个一维数组。 `coef_` 的第 i 行保存了第 i 类的 OVA 分类器的权重向量;类以升序索引 (参照属性 `classes_` )。 注意,原则上,由于它们允许创建一个概率模型,所以 `loss="log"``loss="modified_huber"` 更适合于 one-vs-all 分类。

118
[`SGDClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier) 通过拟合参数 `class_weight``sample_weight` 来支持 weighted classes (加权类)和 weighted instances(加权实例)。更多信息请参照下面的示例和 [`SGDClassifier.fit`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier.fit "sklearn.linear_model.SGDClassifier.fit") 的文档。
W
init  
wizardforcel 已提交
119

L
loopyme 已提交
120
> **示例**:
121 122 123 124 125 126
>
>*   [SGD: Maximum margin separating hyperplane](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_separating_hyperplane.html#sphx-glr-auto-examples-linear-model-plot-sgd-separating-hyperplane-py),
>*   [Plot multi-class SGD on the iris dataset](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_iris.html#sphx-glr-auto-examples-linear-model-plot-sgd-iris-py)
>*   [SGD: Weighted samples](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_weighted_samples.html#sphx-glr-auto-examples-linear-model-plot-sgd-weighted-samples-py)
>*   [Comparing various online solvers](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_comparison.html#sphx-glr-auto-examples-linear-model-plot-sgd-comparison-py)
>*   [SVM: Separating hyperplane for unbalanced classes](https://scikit-learn.org/stable/auto_examples/svm/plot_separating_hyperplane_unbalanced.html#sphx-glr-auto-examples-svm-plot-separating-hyperplane-unbalanced-py) (参见 "*注意*")
W
init  
wizardforcel 已提交
127

128
[`SGDClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier) 支持 averaged SGD (ASGD)。Averaging(均值化)可以通过设置 ``average=True`` 来启用。AGSD 工作原理是在普通 SGD 的基础上,对每个样本的每次迭代后的系数取均值。当使用 ASGD 时,学习速率可以更大甚至是恒定,在一些数据集上能够加速训练过程。
W
init  
wizardforcel 已提交
129

130
对于带 logistic loss(logistic 损失)的分类,在 [`LogisticRegression`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression "sklearn.linear_model.LogisticRegression") 中提供了另一个采取 averaging strategy(平均策略)的 SGD 变体,其使用了随机平均梯度 (SAG) 算法。
W
init  
wizardforcel 已提交
131

L
loopyme 已提交
132
## 1.5.2. 回归
W
init  
wizardforcel 已提交
133

134
[`SGDRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDRegressor.html#sklearn.linear_model.SGDRegressor "sklearn.linear_model.SGDRegressor") 类实现了一个简单的随机梯度下降学习例程,它支持用不同的损失函数和惩罚来拟合线性回归模型。 [`SGDRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDRegressor.html#sklearn.linear_model.SGDRegressor "sklearn.linear_model.SGDRegressor") 非常适用于有大量训练样本(>10.000)的回归问题,对于其他问题,我们推荐使用 [`Ridge`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html#sklearn.linear_model.Ridge "sklearn.linear_model.Ridge")[`Lasso`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Lasso.html#sklearn.linear_model.Lasso "sklearn.linear_model.Lasso") ,或 [`ElasticNet`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.ElasticNet.html#sklearn.linear_model.ElasticNet "sklearn.linear_model.ElasticNet")
W
init  
wizardforcel 已提交
135

136
具体的损失函数可以通过 `loss` 参数设置。 [`SGDRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDRegressor.html#sklearn.linear_model.SGDRegressor "sklearn.linear_model.SGDRegressor") 支持以下的损失函数:
W
init  
wizardforcel 已提交
137

138 139 140
*   `loss="squared_loss"`: Ordinary least squares(普通最小二乘法),
*   `loss="huber"`: Huber loss for robust regression(Huber回归),
*   `loss="epsilon_insensitive"`: linear Support Vector Regression(线性支持向量回归).
W
init  
wizardforcel 已提交
141 142 143

Huber 和 epsilon-insensitive 损失函数可用于 robust regression(鲁棒回归)。不敏感区域的宽度必须通过参数 `epsilon` 来设定。这个参数取决于目标变量的规模。

144
[`SGDRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDRegressor.html#sklearn.linear_model.SGDRegressor "sklearn.linear_model.SGDRegressor") 支持 ASGD(平均随机梯度下降) 作为 [`SGDClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier)。 均值化可以通过设置 ``average=True`` 来启用。
W
init  
wizardforcel 已提交
145

146
对于利用了 squared loss(平方损失)和 l2 penalty(l2惩罚)的回归,在 [`Ridge`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.Ridge.html#sklearn.linear_model.Ridge "sklearn.linear_model.Ridge") 中提供了另一个采取 averaging strategy(平均策略)的 SGD 变体,其使用了随机平均梯度 (SAG) 算法。
W
init  
wizardforcel 已提交
147

L
loopyme 已提交
148
## 1.5.3. 稀疏数据的随机梯度下降
W
init  
wizardforcel 已提交
149

150 151 152
> **注意**
>
>由于在截距部分收敛学习速率的差异,稀疏实现与密集实现相比产生的结果略有不同。
W
init  
wizardforcel 已提交
153 154 155

[scipy.sparse](https://docs.scipy.org/doc/scipy/reference/sparse.html) 支持的格式中,任意矩阵都有对稀疏数据的内置支持方法。但是,为了获得最高的效率,请使用 [scipy.sparse.csr_matrix](http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html) 中定义的 CSR 矩阵格式.

L
loopyme 已提交
156
> **示例**:
157 158
>
>*   [Classification of text documents using sparse features](https://scikit-learn.org/stable/auto_examples/text/document_classification_20newsgroups.html#sphx-glr-auto-examples-text-document-classification-20newsgroups-py)
W
init  
wizardforcel 已提交
159

L
loopyme 已提交
160
## 1.5.4. 复杂度
W
init  
wizardforcel 已提交
161 162 163 164

SGD 主要的优势在于它的高效性,对于不同规模的训练样本,处理复杂度基本上是线性的。假如 X 是 size 为 (n, p) 的矩阵,训练成本为 ![O(k n \bar p)](img/d11a3c7c81eef9f2c8ece06f91411e9e.jpg),其中 k 是迭代次数, ![\bar p](img/77e01a63c0620550a5f11a7613001120.jpg) 是每个样本非零特征的平均数。

但是,最近的理论结果表明,得到期望优化精度的运行时间并不会随着训练集规模扩大而增加。
L
loopyme 已提交
165
## 1.5.5. 停止判据
166 167 168
[`SGDClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier)[`SGDRegressor`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDRegressor.html#sklearn.linear_model.SGDRegressor "sklearn.linear_model.SGDRegressor") 类提供了两个判据,当达到给定的收敛水平时停止算法:

- `early_stopping = True`时,输入数据分为训练集和验证集。该模型在训练集拟合,停止判据是基于验证集上的预测分数。改变参数`validation_fraction`可以调整验证集的大小。
W
init  
wizardforcel 已提交
169

170 171 172 173
- `early_stop = False`时,模型对整个输入数据进行拟合,停止判据基于整个输入数据上的目标函数来计算。

在这两种情况下,判据在每个`epoch`计算一次,当判据没有改变的次数超过参数`n_iter_no_change`的值时,算法将停止。改进是用一个容错参数`tol`来评估的,并且算法在最大迭代次数`max_iter`之后停止。

L
loopyme 已提交
174
## 1.5.6. 实用小贴士
175 176 177 178 179 180 181 182 183 184 185 186

*   随机梯度下降法对 feature scaling (特征缩放)很敏感,因此强烈建议您缩放您的数据。例如,将输入向量 X 上的每个特征缩放到 [0,1] 或 [- 1,+1], 或将其标准化,使其均值为 0,方差为 1。请注意,必须将 **相同** 的缩放应用于对应的测试向量中,以获得有意义的结果。使用 `StandardScaler`能很容易做到这一点:
``` py
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train) # Don’t cheat - fit only on training data
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test) # apply same transformation to test data
```   
 假如你的 attributes (属性)有一个固有尺度(例如 word frequencies (词频)或 indicator features(指标特征))就不需要缩放。
*   最好使用 `GridSearchCV` 找到一个合理的 regularization term (正则化项) ![\alpha](img/d8b3d5242d513369a44f8bf0c6112744.jpg) , 它的范围通常在 `10.0**-np.arange(1,7)`
*   经验表明,SGD 在处理约 10^6 训练样本后基本收敛。因此,对于迭代次数第一个合理的猜想是 `n_iter = np.ceil(10**6 / n)`,其中 `n` 是训练集的大小。  
H
Hanmin Qin 已提交
187
*   假如将 SGD 应用于使用 PCA 提取的特征,我们发现通过某个常数 c 来缩放特征值是明智的,比如使训练数据的 L2 norm 平均值为 1。
188 189
*   我们发现,当特征很多或 eta0 很大时, ASGD(平均随机梯度下降) 效果更好。

L
loopyme 已提交
190
> **参考资料**:
191 192
>*   [“Efficient BackProp”](http://yann.lecun.com/exdb/publis/pdf/lecun-98b.pdf) Y. LeCun, L. Bottou, G. Orr, K. Müller - In Neural Networks: Tricks of the Trade 1998.

L
loopyme 已提交
193
## 1.5.7. 数学描述
W
init  
wizardforcel 已提交
194 195 196 197 198 199 200 201 202

给定一组训练样本 ![(x_1, y_1), \ldots, (x_n, y_n)](img/5421b26a31de754ee8d186d038006fa3.jpg) ,其中 ![x_i \in \mathbf{R}^m](img/2736dbaab8f81e4cb2d0e388f2b0c6b2.jpg) , ![y_i \in \{-1,1\}](img/e24edaeb407b6a696ddb188697f0934d.jpg), 我们的目标是一个线性 scoring function(评价函数) ![f(x) = w^T x + b](img/75fd7809a46f43dcd922f39ff8f91026.jpg) ,其中模型参数 ![w \in \mathbf{R}^m](img/99d4804dc3d2ef82e10d91de99d0142a.jpg) ,截距 ![b \in \mathbf{R}](img/f40416aceb254b77100eb361321c1804.jpg)。为了做预测, 我们只需要看 ![f(x)](img/da2ce2d49bbab0c389600d1c82fccf9b.jpg) 的符号。找到模型参数的一般选择是通过最小化由以下式子给出的正则化训练误差

![E(w,b) = \frac{1}{n}\sum_{i=1}^{n} L(y_i, f(x_i)) + \alpha R(w)](img/580270908cf4e5ba3907b7267fcfbb44.jpg)

其中 ![L](img/639e82f3829a0ad677110cc33a028c98.jpg) 衡量模型(mis)拟合程度的损失函数,![R](img/0fccbdc535b0a4d8003725e8ad606561.jpg) 是惩罚模型复杂度的正则化项(也叫作惩罚); ![\alpha > 0](img/11cde057716cf1a820780a60c8ffa8e4.jpg) 是一个非负超平面。

![L](img/639e82f3829a0ad677110cc33a028c98.jpg) 的不同选择产生不同的分类器,例如

203 204 205 206
*   Hinge: (软-间隔) 支持向量机。
*   Log: Logistic 回归。
*   Least-Squares: 岭回归。
*   Epsilon-Insensitive: (软-间隔) 支持向量回归。
W
init  
wizardforcel 已提交
207 208 209

所有上述损失函数可以看作是错误分类误差的上限(0 - 1损失),如下图所示。

210
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_sgd_loss_functions_0011.png](img/42edb18b0951c4f7ab739e5c24bf9ba3.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_loss_functions.html)
W
init  
wizardforcel 已提交
211 212 213

比较流行的正则化项 ![R](img/0fccbdc535b0a4d8003725e8ad606561.jpg) 包括:

214 215 216
*   L2 norm: ![R(w) := \frac{1}{2} \sum_{i=1}^{n} w_i^2](img/2e2461d59015f9759fa0612965e2425e.jpg),
*   L1 norm: ![R(w) := \sum_{i=1}^{n} |w_i|](img/61a540d6591602c8f513910fd2f33b40.jpg), 这导致了稀疏解。
*   Elastic Net: ![R(w) := \frac{\rho}{2} \sum_{i=1}^{n} w_i^2 + (1-\rho) \sum_{i=1}^{n} |w_i|](img/f9d2fc91f381e1772999a738d3c8c32b.jpg), l2和l1的凸组合, 其中 ![\rho](img/b91e4507d9fd7068b02f689d697f8714.jpg)`1 - l1_ratio`给出.
W
init  
wizardforcel 已提交
217 218 219

下图显示当 ![R(w) = 1](img/55f44df097de0ddde791d3084a69a1bf.jpg) 时参数空间中不同正则项的轮廓。

220
[![http://sklearn.apachecn.org/cn/0.19.0/_images/sphx_glr_plot_sgd_penalties_0011.png](img/7c228fb3025521b5d9c2aef929547d1d.jpg)](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_penalties.html)
W
init  
wizardforcel 已提交
221

L
loopyme 已提交
222
### 1.5.7.1. SGD
W
init  
wizardforcel 已提交
223 224 225

随机梯度下降法是一种无约束优化问题的优化方法。与(批量)梯度下降法相反,SGD 通过一次只考虑单个训练样本来近似 ![E(w,b)](img/343401666d8fc0aeeea395495b9dc570.jpg) 真实的梯度。

226
[`SGDClassifier`](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.SGDClassifier.html#sklearn.linear_model.SGDClassifier) 类实现了一个 first-order SGD learning routine (一阶 SGD 学习程序)。 算法在训练样本上遍历,并且对每个样本根据由以下式子给出的更新规则来更新模型参数。
W
init  
wizardforcel 已提交
227

228
![w \leftarrow w - \eta (\alpha \frac{\partial R(w)}{\partial w}+ \frac{\partial L(w^T x_i + b, y_i)}{\partial w})](img/74f4ea0e25b673d30d56ab4269f03f3b.jpg)
W
init  
wizardforcel 已提交
229 230 231 232 233 234 235

其中 ![\eta](img/fe1d79339349f9b6263e123094ffce7b.jpg) 是在参数空间中控制步长的 learning rate (学习速率)。 intercept(截距) ![b](img/6ae91fb0f3221b92d2dd4e22204d8008.jpg) 的更新类似但不需要正则化。

学习率 ![\eta](img/fe1d79339349f9b6263e123094ffce7b.jpg) 可以恒定或者逐渐减小。对于分类来说, 默认的学习率设定方案 (`learning_rate='optimal'`)由下式给出。

![\eta^{(t)} = \frac {1}{\alpha  (t_0 + t)}](img/938ee5c0c620fd2298ea16abe621e7bb.jpg)

236
其中 ![t](img/12b2c1da1f9041738fa7153efc651372.jpg) 是时间步长(总共有 (n_samples * n_iter)时间步长), ![t_0](img/56c751b0714a570fdcef0caf63f81580.jpg) 是由 Léon Bottou 提出的启发式算法决定的,比如预期的初始更新可以设定为权重的期望大小(假设训练样本的范数近似1)。在 `BaseSGD` 中的 `_init_t` 中可以找到确切的定义。
W
init  
wizardforcel 已提交
237 238 239 240 241 242 243 244 245 246 247

对于回归来说,默认的学习率是反向缩放 (`learning_rate='invscaling'`),由下式给出

![\eta^{(t)} = \frac{eta_0}{t^{power\_t}}](img/114b22cba4861a82ce7df1eab3219a0d.jpg)

其中 ![eta_0](img/0e8aa67015918fa2807e6ddf7192c32f.jpg) 和 ![power\_t](img/7ece088a96a66cb9675fde4610d67980.jpg) 是用户通过 `eta0``power_t` 分别选择的超参数。

使用固定的学习速率则设置 `learning_rate='constant'` ,或者设置 `eta0` 来指定学习速率。

模型参数可以通过成员 `coef_``intercept_` 来获得:

248 249
*   成员 `coef_` holds the weights(控制权重) ![w](img/8a58e8df6a985a3273e39bac7dd72b1f.jpg)
*   成员 `intercept_` holds ![b](img/6ae91fb0f3221b92d2dd4e22204d8008.jpg)
W
init  
wizardforcel 已提交
250

L
loopyme 已提交
251
> **参考资料**:
252 253 254
>*   [“Solving large scale linear prediction problems using stochastic gradient descent algorithms”](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.58.7377) T. Zhang - In Proceedings of ICML ‘04.
>*   [“Regularization and variable selection via the elastic net”](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.124.4696) H. Zou, T. Hastie - Journal of the Royal Statistical Society Series B, 67 (2), 301-320.
>*   [“Towards Optimal One Pass Large Scale Learning with Averaged Stochastic Gradient Descent”](http://arxiv.org/pdf/1107.2490v2.pdf) Xu, Wei
W
init  
wizardforcel 已提交
255

L
loopyme 已提交
256
## 1.5.8. 实现细节
W
init  
wizardforcel 已提交
257 258 259

SGD 的实现受到了 Léon Bottou [Stochastic Gradient SVM](http://leon.bottou.org/projects/sgd) 的影响。类似于 SvmSGD,权重向量表达为一个标量和一个向量的内积,这样保证在使用L2正则项时可以高效更新权重。 在 sparse feature vectors (稀疏特征向量)的情况下, intercept (截距)是以更小的学习率(乘以 0.01)更新的,这导致了它的更新更加频繁。训练样本按顺序选取并且每处理一个样本就要降低学习速率。我们采用了 Shalev-Shwartz 等人2007年提出的的学习速率设定方案。 对于多类分类,我们使用了 “one versus all” 方法。 我们在 L1 正则化(和 Elastic Net )中使用 Tsuruoka 等人2009年提出的 truncated gradient algorithm (截断梯度算法)。代码是用 Cython 编写的。

L
loopyme 已提交
260
> **参考资料**:
261 262 263 264
>*   [“Stochastic Gradient Descent”](http://leon.bottou.org/projects/sgd) L. Bottou - Website, 2010.
>*   [“The Tradeoffs of Large Scale Machine Learning”](http://leon.bottou.org/slides/largescale/lstut.pdf) L. Bottou - Website, 2011.
>*   [“Pegasos: Primal estimated sub-gradient solver for svm”](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.74.8513) S. Shalev-Shwartz, Y. Singer, N. Srebro - In Proceedings of ICML ‘07.
>*   [“Stochastic gradient descent training for l1-regularized log-linear models with cumulative penalty”](http://www.aclweb.org/anthology/P/P09/P09-1054.pdf) Y. Tsuruoka, J. Tsujii, S. Ananiadou - In Proceedings of the AFNLP/ACL ‘09.