Created
October 22, 2021 15:14
-
-
Save AndyM10/2597b244354938a7cbdb828e82497199 to your computer and use it in GitHub Desktop.
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
import numpy as np | |
def sigmoid(num): | |
return 1 / (1 + np.exp(-num)) | |
def deriv_sigmoid(z): | |
return z * (1 - z) | |
class ANN: | |
def __init__(self, x, y): | |
self.input = x | |
self.output = np.zeros(y.shape) | |
self.y = y | |
self.weight1 = np.random.uniform(low = -0.5, high = 0.5 , size = (self.input.shape[1],500)) | |
# self.b1 = np.random.uniform(low = -0.5, high = 0.5 , size = (1,100)) | |
self.weight2 = np.random.uniform(low = -0.5, high = 0.5 , size = (500,500)) | |
# self.b2 = np.random.uniform(low = -0.5, high = 0.5 , size = (1,100)) | |
self.weight3 = np.random.uniform(low = -0.5, high = 0.5 , size = (500,1)) | |
# self.b3 = np.random.uniform(low = -0.5, high = 0.5 , size = (1,1)) | |
self.learningRate = 0.0001 | |
def forward(self): | |
self.layer1 = sigmoid(np.dot(self.input, self.weight1) ) | |
self.layer2 = sigmoid(np.dot(self.layer1, self.weight2) ) | |
self.output = sigmoid(np.dot(self.layer2,self.weight3) ) | |
def back(self): | |
deriv_weights3 = np.dot(self.layer2.T, (2*(self.y - self.output) * deriv_sigmoid(self.output))) | |
deriv_weights2 = np.dot(self.layer1.T, (np.dot(2*(self.y - self.output) * deriv_sigmoid(self.output), self.weight3.T) * deriv_sigmoid(self.layer2))) | |
deriv_weights1 = np.dot(self.input.T, (np.dot(2*(self.y - self.output) * deriv_sigmoid(self.layer2), self.weight2.T) * deriv_sigmoid(self.layer1))) | |
self.weight1 += deriv_weights1 * self.learningRate | |
self.weight2 += deriv_weights2 * self.learningRate | |
self.weight3 += deriv_weights3 * self.learningRate | |
# self.b1 += deriv_weights1.sum() * self.learningRate | |
# self.b2 += deriv_weights2.sum() * self.learningRate | |
# self.b3 += deriv_weights3.sum() * self.learningRate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment