-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnn.py
More file actions
executable file
·164 lines (123 loc) · 3.89 KB
/
nn.py
File metadata and controls
executable file
·164 lines (123 loc) · 3.89 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# Neural Network
# -- nn.py
#
# @package NeuralNetwork
import Queue
import random
import math
import pickle
import zmq
import time
import threading
NUM_INPUTS = 3
NUM_HIDDEN = 3
NUM_OUTPUTS = 3
OUTPUTS = []
test_input = [ .0, .3, .6, .2, .8 ]
class Node:
def __init__(self):
self.connected_edges = []
def sigmoid(self, num):
return math.tanh(num)
class InputNode(Node):
def __init__(self):
self.input = Queue.Queue()
class HiddenNode(Node):
def __init__(self):
self.values = []
self.final = 0
self.last_input = None
def activate(self):
sum = 0
for value in self.values:
sum += value
for value in self.values:
sum += value
return self.sigmoid(sum)
class OutputNode(Node):
def __init__(self):
self.values = []
def checkThreshold(self):
sum = 0
for value in self.values:
sum += value
fin = self.sigmoid(sum)
if fin < 0.5:
return 0
else:
return 1
def initEdgeWeights(nodes):
random.seed()
for node in nodes:
node.connected_edges = [ random.uniform(-1.0, 1.0) for x in range(NUM_INPUTS) ]
def recvInputVector(input, input_nodes):
for i in range(NUM_INPUTS):
input_nodes[i].input.put(input[i])
def derivSig(num):
return 1 - num**2
def run(inputs, hidden, outputs):
for input in inputs:
val = input.input.get()
for i in range(NUM_HIDDEN):
hidden[i].values.append(input.connected_edges[i] * val)
hidden[i].last_input = val
for node in hidden:
node.final = node.activate()
for i in range(NUM_OUTPUTS):
outputs[i].values.append(node.connected_edges[i] * node.final)
for out in outputs:
OUTPUTS.append(out.checkThreshold())
def backPropagate(targets, inputs, hidden):
out_deltas = []
for i in range(NUM_OUTPUTS):
error = targets[i] - OUTPUTS[i]
out_deltas.append(error * derivSig(OUTPUTS[i]))
for i in range(NUM_HIDDEN):
for j in range(NUM_OUTPUTS):
delta = out_deltas[j] * hidden[i].final
hidden[i].connected_edges[j] += .5 * delta
hidden_deltas = []
for i in range(NUM_HIDDEN):
error = 0
for j in range(NUM_OUTPUTS):
error += out_deltas[j] * hidden[i].connected_edges[j]
hidden_deltas.append(error * derivSig(hidden[i].final))
for i in range(NUM_INPUTS):
for j in range(NUM_HIDDEN):
delta = hidden_deltas[j] * hidden[i].last_input
inputs[i].connected_edges[j] += .5 * delta
error = 0
for i in range(len(targets)):
error += .5 * (targets[i] - OUTPUTS[i])**2
return error
def main():
# initialize all node objects
input_nodes = [ InputNode() for x in range(NUM_INPUTS) ]
hidden_nodes = [ HiddenNode() for x in range(NUM_HIDDEN) ]
output_nodes = [ OutputNode() for x in range(NUM_OUTPUTS) ]
# create the weights
initEdgeWeights(input_nodes)
initEdgeWeights(hidden_nodes)
# Start sending/receiving with the player (ANN)
# create the server and the client for communication with Simulation.
# server for receiving input from the simulation.
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind('tcp://127.0.0.1:1235')
socket.connect('tcp://127.0.0.1:1234')
while True:
global OUTPUTS
new_inputs = socket.recv()
try:
new_inputs = pickle.loads(new_inputs)
except:
socket.send("SENT BAD DATA")
# initialize input nodes with newest data
recvInputVector(new_inputs, input_nodes)
run(input_nodes, hidden_nodes, output_nodes)
backPropagate([1, 0, 1], input_nodes, hidden_nodes)
#print OUTPUTS
socket.send(pickle.dumps(OUTPUTS))
OUTPUTS = []
if __name__ == "__main__":
main()