-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutilities.py
More file actions
278 lines (205 loc) · 6.4 KB
/
Copy pathutilities.py
File metadata and controls
278 lines (205 loc) · 6.4 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
import math
import sys
import types
from random import gauss, random, choice, uniform, random
def write_pretty_params(f, config, params):
param_names = [p.name for p in params]
longest_name = max(len(name) for name in param_names)
param_names.sort()
params = dict((p.name, p) for p in params)
for name in param_names:
p = params[name]
f.write('{} = {}\n'.format(p.name.ljust(longest_name), p.format(getattr(config, p.name))))
def mean(values):
values = list(values)
return sum(map(float, values)) / len(values)
def median(values):
values = list(values)
values.sort()
return values[len(values) // 2]
def median2(values):
values = list(values)
n = len(values)
if n <= 2:
return mean(values)
values.sort()
if (n % 2) == 1:
return values[n//2]
i = n//2
return (values[i - 1] + values[i])/2.0
def variance(values):
values = list(values)
m = mean(values)
return sum((v - m) ** 2 for v in values) / len(values)
def stdev(values):
return math.sqrt(variance(values))
stat_functions = {'min': min, 'max': max, 'mean': mean, 'median': median,
'median2': median2}
def creates_cycle(connections, test):
i, o = test
if i == o:
return True
visited = {o}
while True:
num_added = 0
for a, b in connections:
if a in visited and b not in visited:
if b == i:
return True
visited.add(b)
num_added += 1
if num_added == 0:
return False
def required_for_output(inputs, outputs, connections):
required = set(outputs)
s = set(outputs)
while 1:
# Find nodes not in S whose output is consumed by a node in s.
t = set(a for (a, b) in connections if b in s and a not in s)
if not t:
break
layer_nodes = set(x for x in t if x not in inputs)
if not layer_nodes:
break
required = required.union(layer_nodes)
s = s.union(t)
return required
def feed_forward_layers(inputs, outputs, connections):
required = required_for_output(inputs, outputs, connections)
layers = []
s = set(inputs)
while 1:
# Find candidate nodes c for the next layer. These nodes should connect
# a node in s to a node not in s.
c = set(b for (a, b) in connections if a in s and b not in s)
# Keep only the used nodes whose entire input set is contained in s.
t = set()
for n in c:
if n in required and all(a in s for (a, b) in connections if b == n):
t.add(n)
if not t:
break
layers.append(t)
s = s.union(t)
return layers
if sys.version_info[0] == 3:
def iterkeys(d, **kw):
return iter(d.keys(**kw))
def iteritems(d, **kw):
return iter(d.items(**kw))
def itervalues(d, **kw):
return iter(d.values(**kw))
else:
def iterkeys(d, **kw):
return iter(d.iterkeys(**kw))
def iteritems(d, **kw):
return iter(d.iteritems(**kw))
def itervalues(d, **kw):
return iter(d.itervalues(**kw))
if sys.version_info[0] > 2:
from functools import reduce
def product_aggregation(x): # note: `x` is a list or other iterable
return reduce(mul, x, 1.0)
def sum_aggregation(x):
return sum(x)
def max_aggregation(x):
return max(x)
def min_aggregation(x):
return min(x)
def maxabs_aggregation(x):
return max(x, key=abs)
def median_aggregation(x):
return median2(x)
def mean_aggregation(x):
return mean(x)
class AggregationFunctionSet(object):
def __init__(self):
self.functions = {}
self.add('product', product_aggregation)
self.add('sum', sum_aggregation)
self.add('max', max_aggregation)
self.add('min', min_aggregation)
self.add('maxabs', maxabs_aggregation)
self.add('median', median_aggregation)
self.add('mean', mean_aggregation)
def add(self, name, function):
self.functions[name] = function
def get(self, name):
f = self.functions.get(name)
return f
def __getitem__(self, index):
return self.get(index)
def is_valid(self, name):
return name in self.functions
def sigmoid_activation(z):
#z = max(-60.0, min(60.0, 5.0 * z))
#z = max(0.0, min(1.0, z))
try:
tmp = 1.0 / (1.0 + math.exp(-z))
except OverflowError:
tmp = 1.0
return tmp
def tanh_activation(z):
z = max(-60.0, min(60.0, 2.5 * z))
return math.tanh(z)
def sin_activation(z):
z = max(-60.0, min(60.0, 5.0 * z))
return math.sin(z)
def gauss_activation(z):
z = max(-3.4, min(3.4, z))
return math.exp(-5.0 * z**2)
def relu_activation(z):
return z if z > 0.0 else 0.0
def softplus_activation(z):
z = max(-60.0, min(60.0, 5.0 * z))
return 0.2 * math.log(1 + math.exp(z))
def identity_activation(z):
return z
def clamped_activation(z):
return max(-1.0, min(1.0, z))
def inv_activation(z):
try:
z = 1.0 / z
except ArithmeticError: # handle overflows
return 0.0
else:
return z
def log_activation(z):
z = max(1e-7, z)
return math.log(z)
def exp_activation(z):
z = max(-60.0, min(60.0, z))
return math.exp(z)
def abs_activation(z):
return abs(z)
def hat_activation(z):
return max(0.0, 1 - abs(z))
def square_activation(z):
return z ** 2
def cube_activation(z):
return z ** 3
class ActivationFunctionSet(object):
def __init__(self):
self.functions = {}
self.add('sigmoid', sigmoid_activation)
self.add('tanh', tanh_activation)
self.add('sin', sin_activation)
self.add('gauss', gauss_activation)
self.add('relu', relu_activation)
self.add('softplus', softplus_activation)
self.add('identity', identity_activation)
self.add('clamped', clamped_activation)
self.add('inv', inv_activation)
self.add('log', log_activation)
self.add('exp', exp_activation)
self.add('abs', abs_activation)
self.add('hat', hat_activation)
self.add('square', square_activation)
self.add('cube', cube_activation)
def add(self, name, function):
self.functions[name] = function
def get(self, name):
f = self.functions.get(name)
return f
def is_valid(self, name):
return name in self.functions