-
Notifications
You must be signed in to change notification settings - Fork 0
/
SirajNuralNet.py
70 lines (49 loc) · 2.53 KB
/
SirajNuralNet.py
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
from numpy import exp, array, random, dot
class NeuralNetwork():
def __init__(self):
# Seed the random number generator so it generates
# the same numbers every time the program runs
random.seed(1)
# We want a single neuron with 3 input and 1 output connection
# we assign random weights to a 3 x 1 matrix with values in the
# range -1 to 1 and a mean of 0
self.synaptic_weights = 2 * random.random((3,1)) - 1
# The sigmoid function, which describes an S shaped curve
# we pass the weighted sum of the inputs through this function
# to normalize them between 0 and 1
# I think this is the activation function
def __sigmoid(self, x):
return 1/(1 + exp(-x))
#gradient of the sigmoid curve
def __sigmoid_derivative(self,x):
return x * (1-x)
def train(self, training_set_inputs, training_set_outputs, number_of_training_iterations):
for iteration in range(number_of_training_iterations):
# pass the training set through our neural net
output = self.predict(training_set_inputs)
# calculate the error
error = training_set_outputs - output
# multiply the error by the input ad again by the gradient of the sigmoid curve
adjustment = dot(training_set_inputs.T, error * self.__sigmoid_derivative(output))
# adjust the weights
self.synaptic_weights += adjustment
def predict(self, inputs):
# pass inputs through our neural network (single neuron)
return self.__sigmoid(dot(inputs, self.synaptic_weights))
if __name__ == '__main__':
#initialize a single neuron neural network
neural_network = NeuralNetwork()
print ('Random starting synaptic weights:')
print (neural_network.synaptic_weights)
#The training set. We have 4 examples, each consisting of 3 input
# and 1 output value. the .T modifier transposes the array
training_set_inputs = array([[0,0,1],[1,1,1],[1,0,1],[0,1,1]])
training_set_outputs = array([[0,1,1,0]]).T
#train the neural network using a training set.
#Do it 10,000 times and make small adjustments each time
neural_network.train(training_set_inputs, training_set_outputs, 10000)
print ('new synaptic weights after training: ')
print (neural_network.synaptic_weights)
#Test the neural network
print ('Considering new situation [0,1,1] -> ?')
print (neural_network.predict(array([0,1,1])))