Last active
June 25, 2021 21:45
-
-
Save acl21/703ecc8949a01f472d17db3359d56985 to your computer and use it in GitHub Desktop.
Gradient Descent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
X = [0.5, 2.5] | |
Y = [0.2, 0.9] | |
def f(w, b, x): | |
return 1.0/(1.0 + np.exp(-(w*x + b))) | |
def error(w, b): | |
err = 0.0 | |
for x,y in data: | |
fx = f(w,b,x) | |
err += 0.5 * (fx - y) ** 2 | |
return err | |
def grad_b(w, b, x, y): | |
fx = f(w,b,x) | |
return (fx-y) * fx * (1 - fx) | |
def grad_w(w, b, x, y): | |
fx = f(w,b,x) | |
return (fx-y) * fx * (1 - fx) * x | |
def do_gradient_descent(): | |
w, b, eta, max_epochs = 1, 1, 0.01, 100 | |
for i in range(max_epochs): | |
dw, db = 0, 0 | |
for x,y in zip(X, Y): | |
dw += grad_w(w, b, x, y) | |
db += grad_b(w, b, x, y) | |
w = w - eta * dw | |
b = b - eta * db | |
print(error(w,b)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Intially i got the error at line 11
I changed data to zip(x,y)
but i am not getting any results.
Not getting values of error. Please help me
Please also include the code to plot the graph which you have explain in your blog