Stochastic Gradient Descent Technique with Example

Stochastic Gradient Descent Technique with Example

General idea

In previous post, we talked about gradient descent optimization technique. Read full article here.

In this post we will discuss about incremental/online version of gradient descent optimization algorithm

Batch strategies, for example, restricted memory BFGS, which utilize the full preparing set to figure the following refresh to parameters at every emphasis will in general meet exceptionally well to nearby optima. They are likewise straight forward to get working gave a decent off the rack execution (for example minFunc) on the grounds that they have not very many hyper-parameters to tune.

atch strategies, for example, restricted memory BFGS, which utilize the full preparing set to figure the following refresh to parameters at every emphasis will in general meet exceptionally well to nearby optima. They are likewise straight forward to get working gave a decent off the rack execution (for example minFunc) on the grounds that they have not very many hyper-parameters to tune.

Nonetheless, regularly by and by figuring the expense and gradient for the whole preparing set can be moderate and once in a while recalcitrant on a solitary machine if the dataset is too huge to fit in primary memory. Another issue with batch advancement techniques is that they don’t give a simple method to fuse new information in a ‘web based’ setting. Stochastic

gradient descent
gradient descent

Gradient Descent (SGD) addresses both of these issues by following the negative gradient of the target in the wake of seeing just a solitary or a couple of preparing models. The utilization of SGD In the neural network setting is roused by the staggering expense of running back spread over the full preparing set. SGD can conquer this expense and still lead to quick assembly.

Few things to note,

a) In SGD, you should arbitrarily shamble the training samples and apply for looping

b) SGD is uses only one sample at a time, its track to the minima is noisier (more random) than that of the batch gradient. As long as training time is less and minima is achieved , we can ignore this thing.  

c) Mini-batch gradient descent algorithm practices n number of data instances (as an alternative of single sample as in SGD) at every repetition.

Stochastic Gradient Descent

The standard gradient descent technique apprises the parameters θθ of the objective function J(θ)J(θ) as given below,

θ=θ−α∇θE[J(θ)]θ=θ−α∇θE[J(θ)]

Here, the expectation is approximated by computing the cost and gradient over the complete training set.

Stochastic Gradient Descent (SGD) does away with the expectation in the update and evaluates the gradient of the parameters using single or a few training samples. The updated equation is given by following equation,

θ=θ−α∇θJ(θ;x(i),y(i))θ=θ−α∇θJ(θ;x(i),y(i))

with a couple (x(i),y(i))(x(i),y(i)) from the preparation set.

By and large every parameter refresh in SGD is figured w.r.t a couple of preparing models or a minibatch instead of a solitary precedent. The explanation behind this is twofold: first this lessens the fluctuation in the parameter refresh and can prompt progressively stable combination, second this enables the calculation to exploit exceedingly streamlined lattice activities that ought to be utilized in a very much vectorized calculation of the expense and gradient. A run of the mill minibatch estimate is 256, despite the fact that the ideal size of the minibatch can change for various applications and models.

In SGD, the learning rate αα is naturally lesser than a conforming learning rate in batch gradient descent since there is more variance in the update. Selecting the correct learning rate and schedule (i.e. altering the value of the learning rate as learning growths) can be little tough. One normal process that works fine in practice is to use a less constant learning rate that stretches stable convergence in the early epoch (full pass over the training set) or couple of training and then halve the value of the learning rate as convergence reduces.

An even better approach is to evaluate a held out set after each epoch and anneal the learning rate when the change in objective between epochs is below a small threshold. This tends to give good convergence to a local optima. Another commonly used schedule is to anneal the learning rate at each iteration tt as ab+tab+t where aa and bb dictate the initial learning rate and when the annealing begins respectively. More sophisticated methods include using a backtracking line search to find the optimal update.

One final but important point regarding SGD is the order in which we present the data to the algorithm. If the data is given in some meaningful order, this can bias the gradient and lead to poor convergence. Generally a good method to avoid this is to randomly shuffle the data prior to each epoch of training.

Example and usage guidelines

  • SGD is optimization technique. It can be used for both classification and regression. You can improve output quality of linear regression using SGD.
  • In Logistic Regression implementation of sklearn, it provides a parameter called ‘solver’ where you can choose which optimization algorithm it will use. You should choose ‘sag’ as it means
    ‘Stochastic Average Gradient descent solver’.
  • SKLearn python library has implementation API as SGD Classifier and SGDRegressor
  • In SGDClassifier implementation of sklearn, if you want to obtain linear regression you select loss to be L2 and penalty to be none (linear regression). Choose loss value as L2 for Ridge regression.
  • Refer sample code for SGDRegressor implementation, given below:

>>>import pandas as pd
>>>from math import sqrt
>>>from sklearn.metrics import mean_squared_error
>>>from sklearn import linear_model

>>>dataSet = pd.read_csv(‘dataSet.csv’)
>>>trainSet = dataSet.X.values.reshape(-1,1)
>>>testSet = dataset.y.values.reshape(-1,1).ravel()
>>>sgdrModel= linear_model.SGDRegressor(alpha = 0.0001, shuffle=True, max_iter = 100000)
>>>sgdrModel.fit(trainSet ,testSet )
>>>testSetPredicted = sgdrModel.predict((trainSet )
>>>mse = mean_squared_error(testSet,testSetPredicted)
>>>print(“Root Mean Squared Error: “, sqrt(mse))

Applications

  • Stochastic gradient descent is a standard algorithm for training an extensive variety of models in machine learning, comprising (linear) support vector machines, logistic and graphical models.
  • When joined with the backpropagation algorithm, it is the de facto standard algorithm for training artificial neural networks.
  • Its usage has been conveyed in the Geophysics community, precisely to solicitations of Full Waveform Inversion.
  • Stochastic gradient descent races with the L-BFGS system, which is also broadly cast-off.
  • Alternative prevalent stochastic gradient descent system is the least mean squares (LMS) adaptive filter.
Please follow and like us:

4 thoughts on “Stochastic Gradient Descent Technique with Example

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)