Last active
March 12, 2019 02:00
-
-
Save Niranjankumar-c/d8f81e3c4436655b6d2b96e75f2b6d71 to your computer and use it in GitHub Desktop.
Contour plot of sigmoid neuron for toy data
This file contains 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
#toy data | |
X = [0.5,2.5] | |
Y = [0.2,0.9] | |
import numpy as np | |
import matplotlib.pyplot as plt | |
w_values = [] | |
b_values = [] | |
loss_values = [] | |
def f(w,b,x): | |
return 1. / (1. + np.exp(-(w*x)+b)) | |
def error(w,b): | |
err = 0.0 | |
for x,y in zip(X,Y): | |
fx = f(w,b,x) | |
err += 0.5*(fx-y)**2 | |
return(err) | |
def grad_w(w,b, x, y): | |
y_pred = f(w,b,x) | |
return (y_pred - y) * y_pred * (1 - y_pred) * x | |
def grad_b(w,b, x, y): | |
y_pred = f(w,b,x) | |
return (y_pred - y) * y_pred * (1 - y_pred) | |
def gradient_descent(): | |
w,b,eta = 0, -8, 1.0 | |
#values for illustration, we can choose randomly | |
for i in range(1000): | |
#iterating for 1000 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 -= eta*dw | |
b -= eta*db | |
w_values.append(w) | |
b_values.append(b) | |
loss_values.append(error(w,b)) | |
#call the gradient descent function to find | |
#the best values for w and b | |
gradient_descent() | |
#plotting the surface plot | |
fig = plt.figure() | |
ax = plt.axes(projection='3d') | |
ax.plot_surface(w_values, b_values,loss_values, cmap='seismic') | |
ax.set_xlabel('w') | |
ax.set_ylabel('b') | |
ax.set_zlabel('loss') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment