Skip to content

Instantly share code, notes, and snippets.

@ViniTheSwan
Created July 18, 2021 22:28
Show Gist options
  • Save ViniTheSwan/915438dc5861446ca93f5506206f0915 to your computer and use it in GitHub Desktop.
Save ViniTheSwan/915438dc5861446ca93f5506206f0915 to your computer and use it in GitHub Desktop.
class Neuron:
def __init__(self,input_size, learning_rate):
self.w = np.random.random((input_size,1))-0.5 # self.w is a 2 dimensional column vector
self.b = np.random.random(1)-0.5
self.learning_rate = learning_rate
#forward pass
def forward(self,x):
y_hat = x.T.dot(self.w) + self.b
return y_hat
def loss(self,x,y):
y_hat = self.forward(x)
L = (y-y_hat)**2
return L
#backpropagation
def backward(self,x, y, y_hat):
dw = 2.*(y_hat - y) * x.T
db = 2.*(y_hat - y) * 1.
return dw,db
def train(self,x,y):
x = x.reshape(-1,1)
y_hat = self.forward(x)
dw,db = self.backward(x,y,y_hat)
self.w = self.w - self.learning_rate*dw.T
self.b = self.b - self.learning_rate*db.T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment