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
# 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 |
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
# activation function | |
def sigmoid(self, x): | |
return 1 / (1 + np.exp(-x)) |
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
# training neural net | |
def train(self): | |
# dynamically calculating layers and their respective z | |
for i in range(len(self.input)): | |
self.z0 = self.input[i].reshape([-1, 1]) | |
# forward step | |
output = self.forwardprop() |
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
# activation function derrivative | |
def d_sigmoid(self, z): | |
eta = self.sigmoid(z) | |
return eta * (1 - eta) |
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
# Forward Propagation Logic | |
def forwardprop(self): | |
# dynamically calculating first layer | |
exec("self.z1 = np.dot( self.w1, self.z0 ) + self.b1") | |
# dynamically calculating the "a" of layer | |
exec("self.a1 = self.sigmoid(self.z1)") | |
# dynamically calculating all other layers | |
for i in range(2, self.num_layers + 1): |
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
# Backward Propagation Logic | |
def backprop(self, y_hat, y): | |
# using chain rule to chain rule to find derivative of the | |
# loss function with respect to the last layer i.e. z | |
j = self.num_layers | |
# calculating last dz | |
cmd = "self.dz{} = 2 * (y_hat - y) * self.d_sigmoid(self.z{})".format(j, j, j) | |
exec(cmd) |
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
# Calculating Loss | |
def NN_loss(self, y_hat, y): | |
loss = (y_hat - y)**2 | |
loss = loss[0] | |
self.loss += loss |
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
# reset loss after each iteration | |
def resetloss(self): | |
self.loss = 0 |
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
# Predicting input values | |
def predict(self, num): | |
# setting input as the first layer | |
self.z0 = num | |
# forward propagating and returning the result | |
return self.forwardprop()[0] |
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
from Framework.ClassificationFramework import NeuralNetwork | |
from Framework.predict import predict_classification as predict | |
from Framework.normalize import normalize | |
from Data_Creation import odd_even_data | |
def run(): | |
# getting data | |
x_train, y_train = odd_even_data.data() | |
# normalizing data |
OlderNewer