scikit-learn 中的批量梯度下降

Batch gradient descent in scikit-learn

我们如何为sklearn.linear_model.SGDRegressor设置参数使其执行批量梯度下降?

我想使用批量梯度下降法解决线性回归问题。我需要让 SGD 像批量梯度下降一样工作,这应该(我认为)通过让它在一个纪元结束时修改模型来完成。它可以以某种方式参数化以表现得像那样吗?

I need to make SGD act like batch gradient descent, and this should be done (I think) by making it modify the model at the end of an epoch.

你不能那样做;从 documentation 可以清楚地看出:

the gradient of the loss is estimated each sample at a time and the model is updated along the way

虽然在 SGDClassifier 文档中提到

SGD allows minibatch (online/out-of-core) learning

大概也适用于SGDRegressor,实际意思是可以使用partial_fit的方式分批提供数据;然而,计算(和更新)总是按样本执行。

如果您确实需要使用 GD 执行线性回归,您可以在 Keras 或 Tensorflow 中轻松完成,组装 LR 模型并使用等于整个训练样本的批量大小。