-
Notifications
You must be signed in to change notification settings - Fork 22
/
Helper_Functions.py
39 lines (32 loc) · 1.24 KB
/
Helper_Functions.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
import itertools
import random
def mean(num_list):
return sum(num_list)*1.0/len(num_list)
def n_words_of_length(n,length,alphabet):
if 50*n >= pow(len(alphabet),length):
res = all_words_of_length(length, alphabet)
random.shuffle(res)
return res[:n]
#else if 50*n < total words to be found, i.e. looking for 1/50th of the words or less
res = set()
while len(res)<n:
word = ""
for _ in range(length):
word += random.choice(alphabet)
res.add(word)
return list(res)
def all_words_of_length(length,alphabet):
return [''.join(list(b)) for b in itertools.product(alphabet, repeat=length)]
def compare(network,classifier,length,num_examples=1000,provided_samples=None):
if not None is provided_samples:
words = provided_samples
else:
words = n_words_of_length(num_examples,length,network.alphabet)
disagreeing_words = [w for w in words if not (network.classify_word(w) == classifier.classify_word(w))]
return 1-(len(disagreeing_words)/len(words)), disagreeing_words
def map_nested_dict(d,mapper):
if not isinstance(d,dict):
return mapper(d)
return {k:map_nested_dict(d[k],mapper) for k in d}
class MissingInput(Exception):
pass