-
Notifications
You must be signed in to change notification settings - Fork 2
/
nn.py
73 lines (54 loc) · 2.54 KB
/
nn.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
71
72
73
"""DIY Neural Net
Sept. 26, 2019"""
import math
from matrix import Matrix,transpose,multiply
def sigmoid(x):
return 1 / (1 + math.exp(-x))
def dsigmoid(y):
return y * (1 - y)
class NeuralNetwork(object):
def __init__(self, input_nodes,hidden_nodes,output_nodes,
learningrate):
self.input_nodes = input_nodes #int
self.hidden_nodes = hidden_nodes #int
self.output_nodes = output_nodes #int
self.weights_ih = Matrix(self.hidden_nodes,self.input_nodes)
self.weights_ho = Matrix(self.output_nodes,self.hidden_nodes)
self.weights_ih.randomize()
self.weights_ho.randomize()
self.lr = learningrate
def activation_function(self,x):
"""The Sigmoid Function"""
out = [0]*len(x)
for i, element in enumerate(x):
#print("element:",element)
out[i] = sigmoid(x[i][0])
#print(out)
return out
#train the neural network
def train(self,inputs_list,targets_list):
#convert inputs list to 2d array
inputs = inputs_list.transpose()
targets = targets_list.transpose()
#calculate signals into hidden layer
hidden_inputs = multiply(self.weights_ih,inputs)
hidden_outputs = self.activation_function(hidden_inputs)
#calculate signals entering final output layer
final_inputs = multiply(self.weights_ho,hidden_outputs)
#calculate signals exiting final output layer
final_outputs = self.activation_function(final_inputs)
#output layer error is the target - actual
output_errors = targets - final_outputs
#hidden layer error is the output_errors, split by weights,
#recombined at hidden nodes
hidden_errors = multiply(transpose(self.weights_ho),output_errors)
#update the weights for the links between the hidden and output layers
self.weights_ho += self.lr * multiply((output_errors*final_inputs *\
(1.0 - final_outputs)),
transpose(hidden_outputs))
#update the weights for the links between the input and hidden layers
self.weights_ih += self.lr * multiply((hidden_errors * hidden_outputs *\
(1.0 - hidden_outputs)),
transpose(inputs))
def query(self,inputs_list):
#convert inputs list to 2d array