-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathutil.py
More file actions
146 lines (121 loc) · 3.2 KB
/
util.py
File metadata and controls
146 lines (121 loc) · 3.2 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
from random import random
from math import sqrt, log, floor
import json
# Random Number Utilities
return_v = False
v_val = 0.0
def gaussRandom():
global return_v, v_val
if return_v:
return_v = False
return v_val
u = 2 * random() - 1
v = 2 * random() - 1
r = u * u + v * v
if r == 0 or r > 1:
return gaussRandom()
c = sqrt(-2.0 * log(r) / r)
v_val = v * c #cache this
return_v = True
return u * c
def randf(a, b):
return random() * (b - a) + a
def randi(a, b):
return int(floor(random() * (b - a) + a))
def randn(mu, std):
return mu + gaussRandom() * std
# Array Utilities
import numpy
def zeros(n=None):
if not n:
return []
else:
return [0.0] * int(n)
def arrContains(arr, elt):
for elem in arr:
if elem == elt:
return True
return False
def arrUnique(arr):
b = set(arr)
return list(b)
# Return max and min of a given non-empty list
def maxmin(w):
if len(w) == 0:
return {}
maxv = max(w)
maxi = w.index(maxv)
minv = min(w)
mini = w.index(minv)
return {
'maxi': maxi,
'maxv': maxv,
'mini': mini,
'minv': minv,
'dv' : maxv - minv
}
# Create random permutations of numbers, in range [0 ... n-1]
def randperm(n):
i, j, temp = n - 1, 0, None
array = range(n)
while i:
j = int(floor(random() * (i + 1)))
temp = array[i]
array[i] = array[j]
array[j] = temp
i -= 1
return array
# Sample for list 'lst' according to probabilities in list probs
# the two lists are of same size, and probs adds up to 1
# or lst is a dictionary with keys and probabilities for values
def weightedSample(lst=None, prob=None):
if not (prob or lst):
return
if type(lst) != list:
try:
prob = lst.values()
lst = lst.keys()
except:
return
p = randf(0, 1.0)
cumprob = 0.0
for k in xrange(len(lst)):
cumprob += prob[k]
if p < cumprob:
return lst[k]
# Syntactic sugar function for getting default parameter values
def getopt(opt, field_name, default_value):
return opt.get(field_name, default_value)
# Utilities for saving/loading json to/from a file
def saveJSON(filename, data):
print 'Saving to: {}'.format(filename)
with open(filename, 'w') as outfile:
json.dump(data, outfile)
def loadJSON(path):
with open(path, 'r') as infile:
return json.load(path)
class Window(object):
"""
a window stores _size_ number of values
and returns averages. Useful for keeping running
track of validation or training accuracy during SGD
"""
def __init__(self, size=100, minsize=20):
self.v = []
self.size = size
self.minsize = minsize
self.sum = 0
def add(self, x):
self.v.append(x)
self.sum += x
if len(self.v) > self.size:
xold = self.v.pop(0)
self.sum -= xold
def get_average(self):
if len(self.v) < self.minsize:
return -1
else:
return 1.0 * self.sum / len(self.v)
def reset(self):
self.v = []
self.sum = 0