-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleNN.py
More file actions
293 lines (246 loc) · 9.34 KB
/
Copy pathSimpleNN.py
File metadata and controls
293 lines (246 loc) · 9.34 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import copy
from collections import Iterable
import json_wrapper
__author__ = 'Jian Xun'
class Neural:
# a map from input id to input value
__inputs = None
# current output
__output = None
# a map from input id to input weight
__weights = None
__learn_rate = None
__theta = None
__default_weight = None
def __init__(self, learn_rate, ids, default_weight):
self.__inputs = {}
self.__output = None
self.__weights = {}
self.__learn_rate = learn_rate
self.__theta = default_weight
self.__default_weight = default_weight
for id in ids:
self.__weights[id] = default_weight
def add_link(self, id):
self.__weights[id] = self.__default_weight
def receive(self, inputs):
for id in self.__weights:
self.__inputs[id] = inputs[id]
def output(self):
a = -self.__theta
for id in self.__weights:
a += self.__weights[id] * self.__inputs[id]
self.__output = a if a > 0 else 0
return self.__output
def back_propagate(self, target):
deri = 0 if self.__output == 0 else 1
error = deri * (target - self.__output)
derror = self.__learn_rate * error
propagation = {}
for id in self.__inputs:
propagation[id] = self.__weights[id] * error
self.__weights[id] += self.__inputs[id] * derror
self.__theta -= derror
return propagation
def get_weights(self):
return copy.deepcopy(self.__weights)
def set_weights(self, weights):
self.__weights = copy.deepcopy(weights)
def get_theta(self):
return self.__theta
def set_theta(self, theta):
self.__theta = theta
class Configuration:
__learn_rate = None
__default_weight = None
__input_nodes = None
# records the input id of each node
# __relations[a] = [b, c] means a receives inputs from b and c
__relations = None
__output_nodes = None
# stores weights and theta of each internal/output nodes
__weights = None
__thetas = None
def __init__(self, path=None):
if path is None:
self.__learn_rate = 0.01
self.__default_weight = 0.5
self.__input_nodes = []
self.__relations = {}
self.__output_nodes = []
self.__weights = {}
self.__thetas = {}
else:
file = open(path, 'r')
structure = json_wrapper.loads(file.readline(), 'utf-8')
file.close()
self.__learn_rate = structure['learn_rate']
self.__default_weight = structure['default_weight']
self.__input_nodes = structure['input_nodes']
self.__output_nodes = structure['output_nodes']
self.__relations = structure['relations']
self.__weights = structure['weights']
self.__thetas = structure['thetas']
def set_learn_rate(self, learn_rate):
self.__learn_rate = learn_rate
def set_default_weight(self, weight):
self.__default_weight = weight
def add_input_node(self, ids):
if isinstance(ids, list):
for id in ids:
self.__input_nodes.append(str(id))
if isinstance(ids, str):
self.__input_nodes.append(ids)
def add_output_node(self, ids):
if isinstance(ids, list):
for id in ids:
self.__output_nodes.append(str(id))
if isinstance(ids, str):
self.__output_nodes.append(ids)
def add_weight(self, from_id, to_id, weight):
if to_id not in self.__weights:
self.__weights[to_id] = {}
self.__weights[to_id][from_id] = weight
def add_theta(self, id, theta):
self.__thetas[id] = theta
def add_relation(self, from_id, to_id):
if from_id in self.__relations and to_id in self.__relations[from_id]:
raise LoopException('loop relation found between ' + from_id + ' and ' + to_id)
if to_id not in self.__relations:
self.__relations[to_id] = []
if from_id not in self.__relations[to_id]:
self.__relations[to_id].append(from_id)
def dump_to_file(self, path):
file = open(path, 'w')
structure = {
'learn_rate': self.__learn_rate,
'default_weight': self.__default_weight,
'input_nodes': self.__input_nodes,
'output_nodes': self.__output_nodes,
'relations': self.__relations,
'weights': self.__weights,
'thetas': self.__thetas
}
file.write(json_wrapper.dumps(structure))
file.close()
def to_string(self):
structure = {
'learn_rate': self.__learn_rate,
'default_weight': self.__default_weight,
'input_nodes': self.__input_nodes,
'output_nodes': self.__output_nodes,
'relations': self.__relations,
'weights': self.__weights,
'thetas': self.__thetas
}
return str(structure)
def get_learn_rate(self):
return self.__learn_rate
def get_default_weight(self):
return self.__default_weight
def get_input_nodes(self):
return self.__input_nodes
def get_output_nodes(self):
return self.__output_nodes
def get_inputs(self, id):
return self.__relations[id]
def get_relations(self):
return copy.deepcopy(self.__relations)
def get_weights(self, id):
return None if id not in self.__weights else copy.deepcopy(self.__weights[id])
def get_theta(self, id):
return None if id not in self.__thetas else self.__thetas[id]
class Network:
__nodes = None
__input_nodes = None
__output_nodes = None
# stores the order of nodes to calculate result
__topology = None
__learn_rate = None
__default_weight = None
def __init__(self, configuration):
self.__learn_rate = configuration.get_learn_rate()
self.__default_weight = configuration.get_default_weight()
self.__input_nodes = []
self.__input_nodes.extend(configuration.get_input_nodes())
self.__output_nodes = []
self.__output_nodes.extend(configuration.get_output_nodes())
self.__nodes = {}
relations = configuration.get_relations()
for id in relations:
self.__nodes[id] = Neural(self.__learn_rate, configuration.get_inputs(id), self.__default_weight)
weights = configuration.get_weights(id)
if weights is not None:
self.__nodes[id].set_weights(weights)
theta = configuration.get_theta(id)
if theta is not None:
self.__nodes[id].set_theta(theta)
self.__topology = []
temp_output = []
for id in self.__input_nodes:
temp_output.append(id)
while len(self.__topology) < len(relations):
for nid in relations:
if nid not in temp_output:
while len(relations[nid]) > 0 and relations[nid][0] in temp_output:
relations[nid].pop(0)
if len(relations[nid]) == 0:
self.__topology.append(nid)
temp_output.append(nid)
def train(self, inputs, expect_outputs):
for id in self.__input_nodes:
if id not in inputs:
raise MissException('input node not found : ' + id)
for id in self.__output_nodes:
if id not in expect_outputs:
raise MissException('output node not found : ' + id)
self.predict(inputs)
# then do back propagation
internal_propagate = {}
for id in expect_outputs:
internal_propagate[id] = expect_outputs[id]
for idx in xrange(len(self.__topology) - 1, -1, -1):
id = self.__topology[idx]
temp = self.__nodes[id].back_propagate(internal_propagate[id])
for tid in temp:
if tid in internal_propagate:
internal_propagate[tid] += temp[tid]
else:
internal_propagate[tid] = temp[tid]
def predict(self, inputs):
internal_result = {}
for id in inputs:
internal_result[id] = inputs[id]
# calculate output
for id in self.__topology:
self.__nodes[id].receive(internal_result)
internal_result[id] = self.__nodes[id].output()
return internal_result
def dump(self):
conf = Configuration()
conf.set_learn_rate(self.__learn_rate)
conf.set_default_weight(self.__default_weight)
for id in self.__input_nodes:
conf.add_input_node(id)
for id in self.__output_nodes:
conf.add_output_node(id)
for id in self.__nodes:
neural = self.__nodes[id]
weights = neural.get_weights()
for fid in weights:
conf.add_relation(fid, id)
conf.add_weight(fid, id, weights[fid])
conf.add_theta(id, neural.get_theta())
return conf
class LoopException(Exception):
message = None
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
class MissException(Exception):
message = None
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)