-
Notifications
You must be signed in to change notification settings - Fork 2
/
utilities.py
94 lines (71 loc) · 2.51 KB
/
utilities.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
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
from __future__ import division # floating point division
import math
import numpy as np
def mean(numbers):
return sum(numbers)/float(len(numbers))
def stdev(numbers):
avg = mean(numbers)
variance = sum([pow(x-avg,2) for x in numbers])/float(len(numbers)-1)
return math.sqrt(variance)
def calculateprob(x, mean, stdev):
if stdev < 1e-3:
if math.fabs(x-mean) < 1e-2:
return 1.0
else:
return 0
exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(stdev,2))))
return (1 / (math.sqrt(2*math.pi) * stdev)) * exponent
def sigmoid(xvec1):
""" Compute the sigmoid function """
# Cap -xvec, to avoid overflow
# Undeflow is okay, since it get set to zero
#Somehow the passing nparray was in read mode and wasn't allowed to change its values
xvec=xvec1.copy()
xvec[xvec < -100] = -100
vecsig = 1.0 / (1.0 + np.exp(np.negative(xvec)))
return vecsig
def dsigmoid(xvec):
""" Gradient of standard sigmoid 1/(1+e^-x) """
vecsig = sigmoid(xvec)
return vecsig * (1 - vecsig)
def l2(vec):
""" l2 norm on a vector """
return np.linalg.norm(vec)
def dl2(vec):
""" Gradient of l2 norm on a vector """
return vec
def l1(vec):
""" l1 norm on a vector """
return np.linalg.norm(vec, ord=1)
def dl1(vec):
""" Subgradient of l1 norm on a vector """
grad = np.sign(vec)
grad[abs(vec) < 1e-4] = 0.0
return grad
def threshold_probs(probs):
""" Converts probabilities to hard classification """
classes = np.ones(len(probs),)
classes[probs < 0.5] = 0
return classes
def logsumexp(a):
"""
Compute the log of the sum of exponentials of input elements.
Modified scipys logsumpexp implemenation for this specific situation
"""
awithzero = np.hstack((a, np.zeros((len(a),1))))
maxvals = np.amax(awithzero, axis=1)
aminusmax = np.exp((awithzero.transpose() - maxvals).transpose())
# suppress warnings about log of zero
with np.errstate(divide='ignore'):
out = np.log(np.sum(aminusmax, axis=1))
out = np.add(out,maxvals)
return out
def update_dictionary_items(dict1, dict2):
""" Replace any common dictionary items in dict1 with the values in dict2
There are more complicated and efficient ways to perform this task,
but we will always have small dictionaries, so for our use case, this simple
implementation is acceptable.
"""
for k in dict1:
if k in dict2:
dict1[k]=dict2[k]