Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
codebasics
GitHub Repository: codebasics/deep-learning-keras-tf-tutorial
Path: blob/master/8_sgd_vs_gd/gradient_descent.py
1141 views
1
import numpy as np
2
3
def gradient_descent(x,y,epochs):
4
m_curr = b_curr = 0
5
n = len(x)
6
learning_rate = 0.08
7
8
for i in range(epochs):
9
y_predicted = m_curr * x + b_curr
10
cost = (1/n) * sum([val**2 for val in (y-y_predicted)])
11
md = -(2/n)*sum(x*(y-y_predicted))
12
bd = -(2/n)*sum(y-y_predicted)
13
m_curr = m_curr - learning_rate * md
14
b_curr = b_curr - learning_rate * bd
15
print ("m {}, b {}, cost {} iteration {}".format(m_curr,b_curr,cost, i))
16
17
x = np.array([1,2,3,4,5])
18
y = np.array([5,7,9,11,13])
19
20
gradient_descent(x,y, 500)
21