-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple-net.py
More file actions
53 lines (38 loc) · 1.55 KB
/
Copy pathsimple-net.py
File metadata and controls
53 lines (38 loc) · 1.55 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
import numpy as np
X = np.array(([0,0,1],[0,1,1],[1,0,1],[1,1,1]),dtype=float)
y = np.array(([0],[1],[1],[0]),dtype=float)
def sigmoid(x):
return (1/(1+np.exp(-x)))
def sigmoid_derivative(p):
return(p*(1-p))
class NeuralNetwork:
def __init__(self,x,y):
self.input = x
#the 4 in the below line means that we
# will have 4 hidden nodes/4 nodes in the hidden layer
self.weights1 = np.random.rand(self.input.shape[1],4)
self.weights2 = np.random.rand(4,1)
self.y = y
self.output = np.zeros(y.shape)
def feedforward(self):
self.layer1 = sigmoid(np.dot(self.input,self.weights1))
self.layer2 = sigmoid(np.dot(self.layer1,self.weights2))
return (self.layer2)
def backprop(self):
d_weights2 = np.dot(self.layer1.T,2*(self.y-self.output)*sigmoid_derivative(self.output))
d_weights1 = np.dot(self.input.T,np.dot(2*(self.y-self.output)*sigmoid_derivative(self.output),self.weights2.T)*sigmoid_derivative(self.layer1))
self.weights1+=d_weights1
self.weights2+=d_weights2
def train(self,X,y):
self.output = self.feedforward()
self.backprop()
NN = NeuralNetwork(X,y)
for i in range(1000): #this network is trained n times
if i%100 == 0:
print("for iteration #"+ str(i)+'\n')
print("Input:\n",str(X))
print("Actual Output:\n" + str(y))
print("Predicted Output:\n" + str(NN.feedforward()))
print("Loss:\n" + str(np.mean(np.square(y-NN.feedforward()))))
print("\n")
NN.train(X,y)