Skip to content

Instantly share code, notes, and snippets.

@angadsinghsandhu
Created December 1, 2020 04:30
Show Gist options
  • Save angadsinghsandhu/2325b2bdbec8fc41701aea80dac08b0e to your computer and use it in GitHub Desktop.
Save angadsinghsandhu/2325b2bdbec8fc41701aea80dac08b0e to your computer and use it in GitHub Desktop.
Neural Network From Scratch
# imports
import numpy as np
# nn class
class NeuralNetwork:
# initializing variables
def __init__(self, x, y, learning_rate=0.06, num_layers=2):
# input array
self.input = x
# learning rate of gradient descent value
self.alpha = learning_rate
# number of layers of NN (not including input layer)
self.num_layers = num_layers
# creating array that hold the number of nodes in each layer
self.num_nodes = np.random.randint(
2, high=10, size=num_layers-1).tolist()
self.num_nodes.insert(0, 1)
self.num_nodes.append(1)
# setting weights of all layers
for i in range(len(self.num_nodes)-1):
# dynamically creating weights
cmd = "self.w{} = np.random.randn(self.num_nodes[i+1], self.num_nodes[i])".format(i+1)
exec(cmd)
# setting biases of all layers
for i in range(len(self.num_nodes)-1):
# dynamically creating biases
cmd = "self.b{} = np.random.randn(self.num_nodes[i+1], 1)".format(i+1)
exec(cmd)
# output array and it's shape
self.y = y
self.y_hat = np.random.rand(y.shape[0], y.shape[1])
self.loss = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment