Path: blob/master/8_sgd_vs_gd/mini_batch_gd.ipynb
1141 views
Kernel: Python 3
Implementation of mini batch grandient descent in python
We will use very simple home prices data set to implement mini batch gradient descent in python.
Batch gradient descent uses all training samples in forward pass to calculate cumulitive error and than we adjust weights using derivaties
Stochastic GD: we randomly pick one training sample, perform forward pass, compute the error and immidiately adjust weights
Mini batch GD: we use a batch of m samples where 0 < m < n (where n is total number of training samples)
In [1]:
Load the dataset in pandas dataframe
In [2]:
Out[2]:
Preprocessing/Scaling: Since our columns are on different sacle it is important to perform scaling on them
In [3]:
Out[3]:
array([[0.08827586, 0.25 ],
[0.62068966, 0.75 ],
[0.22068966, 0.5 ],
[0.24862069, 0.5 ],
[0.13793103, 0.25 ],
[0.12758621, 0.25 ],
[0.6662069 , 0.75 ],
[0.86206897, 0.75 ],
[0.17586207, 0.5 ],
[1. , 1. ],
[0.34482759, 0.5 ],
[0.68448276, 0.75 ],
[0.06896552, 0.25 ],
[0.10344828, 0.25 ],
[0.5 , 0.5 ],
[0.12931034, 0.25 ],
[0.13103448, 0.5 ],
[0.25517241, 0.5 ],
[0.67931034, 0.5 ],
[0. , 0. ]])
In [392]:
Out[392]:
array([[0.05237037],
[0.65185185],
[0.22222222],
[0.31851852],
[0.14074074],
[0.04444444],
[0.76296296],
[0.91111111],
[0.13333333],
[1. ],
[0.37037037],
[0.8 ],
[0.04444444],
[0.05925926],
[0.51111111],
[0.07407407],
[0.11851852],
[0.20740741],
[0.51851852],
[0. ]])
We should convert target column (i.e. price) into one dimensional array. It has become 2D due to scaling that we did above but now we should change to 1D
In [4]:
Out[4]:
array([0.05237037, 0.65185185, 0.22222222, 0.31851852, 0.14074074,
0.04444444, 0.76296296, 0.91111111, 0.13333333, 1. ,
0.37037037, 0.8 , 0.04444444, 0.05925926, 0.51111111,
0.07407407, 0.11851852, 0.20740741, 0.51851852, 0. ])
Gradient descent allows you to find weights (w1,w2,w3) and bias in following linear equation for housing price prediction
Now is the time to implement batch gradient descent.
(1) Batch Gradient Descent Implementation
In [13]:
Out[13]:
array([17, 13, 9, 6, 16, 1, 18, 2, 5, 0, 3, 10, 4, 7, 19, 12, 8,
14, 11, 15])
In [41]:
Out[41]:
(array([0.71002416, 0.67805002]), -0.2334439172048776, 0.003226091705359379)
In [ ]:
Check price equation above. In that equation we were trying to find values of w1,w2,w3 and bias. Here we got these values for each of them,
w1 = 0.50381807 w2 = 0.85506386 w3 = 0.34167275 bias = -0.3223
Now plot epoch vs cost graph to see how cost reduces as number of epoch increases
In [42]:
Out[42]:
[<matplotlib.lines.Line2D at 0x2254b1a1100>]
Lets do some predictions now.
In [43]:
Out[43]:
128.63276359101357
In [44]:
Out[44]:
29.979829091937678
In [45]:
Out[45]:
69.39044167400473