Search code examples
machine-learningscikit-learnregressionlinear-regressiongradient-descent

Batch gradient descent in scikit-learn


How do we set parameters for sklearn.linear_model.SGDRegressor to make it perform Batch gradient descent?

I want to solve a linear-regression problem using Batch gradient descent. 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. Can it be somehow parameterized to behave like that?


Solution

  • 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.

    You cannot do that; it is clear from the documentation that:

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

    And although in the SGDClassifier docs it is mentioned that

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

    which presumably holds also for SGDRegressor, what is actually meant is that you can use the partial_fit method for providing the data in different batches; the computations (and updates), however, are always performed per sample.

    If you really need to perform linear regression with GD, you could do it easily in Keras or Tensorflow, assembling an LR model and using a batch size equal to the whole of your training samples.