Skip to content

Commit

Permalink
added gradient descent code
Browse files Browse the repository at this point in the history
  • Loading branch information
Luv1881 committed Oct 22, 2024
1 parent 759e057 commit d6c0ae6
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions gradient_descent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np
def gradient_descent(x,y):
w_curr=b_curr=0
iterations=10000
n=len(x)
learning_rate=0.16
for i in range(iterations):
y_predicted=w_curr*x+b_curr
cost=(1/2*n)*sum([val**2 for val in (y_predicted-y)])
dw=(1/n)*sum(x*(y_predicted-y))
db=(1/n)*sum(y_predicted-y)
w_curr-=learning_rate*dw
b_curr-=learning_rate*db
print(f'w:{w_curr} b:{b_curr} cost:{cost} iterations:{i+1}')

x=np.array([1,2,3,4,5])
y=np.array([5,7,9,11,13])
gradient_descent(x,y)

0 comments on commit d6c0ae6

Please sign in to comment.