-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneural_network.py
More file actions
126 lines (103 loc) · 3.54 KB
/
Copy pathneural_network.py
File metadata and controls
126 lines (103 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
class NNet:
z = [] # Inputs Matrix
a = [] # Activation Matrix, a = g(z)
lr = 0.1 # Learning Rate
def __init__(self, **kwargs): # Initializing inputs, thetas and biases
self.__dict__.update(kwargs)
# Activation Function:
def sigmoid(self, z):
return 1/(1 + np.exp(-z))
# Derivative Sigmoid:
def d_sigmoid(self, a):
return a*(1-a) # g(x) = a
# Forward Propagation Function:
def feedforward(self, x):
NNet.a, NNet.z = [], []
for b, w in zip(self.biases, self.weights):
temp = np.dot(w, x) + b # theta1*x1 + theta2 * x2 + b1
NNet.z.append(temp)
x = self.sigmoid(temp)
NNet.a.append(x)
return x
# Prediction Function:
def predict(self, x):
for b, w in zip(self.biases, self.weights):
x = self.sigmoid(np.dot(w, x) + b)
return x
# Backpropagation Algorithm:
def backprop(self, x, y):
l = self.layer
for i in range(-1, -l, -1):
if i==-1:
delta = y - self.feedforward(x)
corr = (NNet.lr)*delta*self.d_sigmoid(NNet.a[i])
corrections = np.dot(corr, NNet.a[i-1].T)
self.weights[i] += corrections
self.biases[i] += corr
elif i==-l+1:
delta = np.dot(self.weights[i+1].T, delta)
corr = (NNet.lr)*delta*self.d_sigmoid(NNet.a[i])
corrections = np.dot(corr, x.T)
self.weights[i] += corrections
self.biases[i] += corr
else:
delta = np.dot(self.weights[i+1].T, delta)
corr = (NNet.lr)*delta*self.d_sigmoid(NNet.a[i])
corrections = np.dot(corr, NNet.a[i-1].T)
self.weights[i] += corrections
self.biases[i] += corr
# Training:
def train(self, itr, Y, m):
#print("Before:")
#self.show_stat()
for _ in range(itr):
for i in range(m):
self.feedforward(self.input[i].reshape(-1, 1))
self.backprop(self.input[i].reshape(-1, 1), y[i])
#print("After:")
#self.show_stat()
def show_stat(self):
print("W:")
print(self.weights)
print("B:")
print(self.biases)
# Loading Data:
data = np.genfromtxt('week_5.csv', delimiter=',')
x = data[:, 0:2]
y = data[:, 2]
# Data Scaling:
x[:,0] = x[:,0]/max(x[:,0])
x[:,1] = x[:,1]/max(x[:,1])
# Constants:
m = len(data)
# Initializing The Weights:
theta12 = np.random.rand(3, 2)
theta23 = np.random.rand(3, 3)
theta34 = np.random.rand(1, 3)
theta = np.array([theta12, theta23, theta34], dtype=object)
# Initializing The Biases:
b12 = np.random.rand(3, 1)
b23 = np.random.rand(3, 1)
b34 = np.random.rand(1, 1)
b = np.array([b12, b23, b34], dtype=object)
# Feeding Data Into The Network:
nn = NNet(input=x, weights=theta, biases=b, layer=4)
# Training The Network:
nn.train(1000, y, m)
# Making Predictions:
pt = 0
for i in range(m):
#print("Prediction:", nn.predict(x[i].reshape(-1, 1)), "Actual:", y[i])
check = nn.predict(x[i].reshape(-1, 1))[0][0]
if check>=0.5 and y[i]==1:
pt+=1
if check<0.5 and y[i]==0:
pt+=1
print("Accuracy:", (pt/m)*100)
x1 = float(input("Enter x1: "))
x2 = float(input("Enter x2: "))
xx = np.array([x1, x2])
result = nn.predict(xx.reshape(-1, 1))
print("Prediction:", result)
nn.show_stat()