-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
113 lines (95 loc) · 2.64 KB
/
util.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# Imports
import autograd.numpy as np
from scipy.stats import truncnorm
from autograd import grad
import pickle as pk
import matplotlib.pyplot as plt
__author__ = 'Haoping Bai'
__copyright__ = 'Copyright (c) 2018, Haoping Bai'
__email__ = '[email protected]'
__license__ = 'MIT'
def load_pickle(filename):
try:
p = open(filename, 'rb')
except IOError:
print "Pickle file cannot be opened."
return None
try:
picklelicious = pk.load(p)
except ValueError:
print 'load_pickle failed once, trying again'
p.close()
p = open(filename, 'rb')
picklelicious = pk.load(p)
p.close()
return picklelicious
def save_pickle(data_object, filename):
pickle_file = open(filename, 'wb')
pk.dump(data_object, pickle_file)
pickle_file.close()
# Utils: Numerically Stable, Vectorized implementations of util functions
def nprn(*size):
return truncnorm.rvs(-2, 2, size=size)
def l2(x):
"""
stable l2-norm
"""
return np.sqrt(np.sum(np.multiply(x, x)) + 1.e-20)
def oneplus(x, limit=30.):
"""
Numerically stable implementation: | log(1+exp(30)) - 30 | < 1e-10
Constraint to [1, inf)
"""
# limit = 30
# x[x < limit] = np.log(1. + np.exp(x[x < limit]))
x = np.log(1. + np.exp(x))
return 1. + x
def sigmoid(x):
"""
Constraint to [0, 1]
"""
return 1. / (1. + np.exp(-x))
def softmax(x): # row-wise softmax
res = np.exp(x - np.max(x, axis=1, keepdims=True))
res /= np.sum(res, axis=1, keepdims=True)
return res
def shift_cumprod(x):
# solve dimension problem
c = np.squeeze(x)
slices = [1.]
for i in range(1,len(c)):
slices.append(np.prod(c[:i]))
return np.array([slices])
def cos_sim(u, v):
"""
Cosine similarity between u,v
"""
n = np.dot(u,v)
d = np.sqrt(np.dot(u,u) * np.dot(v,v))
d += 1.e-20 # prevent undefined cos similarity at 0 from breaking the code
return n / d
def d_tanh(x):
"""
Derivative for tanh used for gradient calculation
"""
y = np.tanh(x)
return 1. - y * y
def d_sigmoid(x):
"""
Derivative for sigmoid used for gradient calculation
"""
y = sigmoid(x)
return y * (1. - y)
def d_cos_sim(u, v):
"""
Differentiate Cos Similarity with regard to u, switch order to calculate for v
"""
n = np.dot(u, v)
u_inner_prod = np.dot(u*u)
d = np.sqrt(u_inner_prod * np.dot(v*v)) + 1.e-20 # deal with undefined cos sim for zero vec
return v / d - (n / d) * (u / u_inner_prod)
def display(arr):
plt.imshow(arr, cmap='Greys')
# plt.yticks([])
# plt.xticks([])
plt.show()