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
|
import theano
import cascade_creation
from theano import tensor, function
import numpy as np
import timeout
import cvxopt
import scipy
@timeout.timeout(20)
def sparse_recovery(lbda, n_cascades):
"""
Solves:
min lbda * |theta|_1 - 1/n_cascades*(
sum w log(e^[M*theta]) + (1-w) log(1-e^[M*theta])
)
s.t theta_j <= 0
The objective must be re-normalized:
-> log(e^x - 1) = (1-k)x + log(e^[kx] - 1)
with k chosen as : n_nodes*n_cascades
"""
if type(lbda) == int or type(lbda) == float:
lbda = np.array(lbda)
lbda = theano.shared(lbda.astype(theano.config.floatX))
theta = tensor.row().T
theta_ = theta.flatten()
M = tensor.matrix(name='M', dtype=theano.config.floatX)
w = tensor.vector(name='w', dtype=theano.config.floatX)
y = lbda * theta_.norm(1) - 1./n_cascades*(
(w).dot(tensor.log(1-tensor.exp(M.dot(theta_))))
+ (1-w).dot(tensor.dot(M, theta_))
)
z = tensor.row().T
z_ = z.flatten()
y_diff = tensor.grad(y, theta_)
y_hess = z[0] * theano.gradient.hessian(y, theta_)
f_x = theano.function([theta, M, w], [y, y_diff],
allow_input_downcast=True)
f_xz = theano.function([theta, z, M, w], [y, y_diff, y_hess],
allow_input_downcast=True)
return f_x, f_xz
def type_lasso(lbda, n_cascades):
"""
Solves:
min - sum_j theta_j + lbda*|e^{M*theta} - (1 - w)|_2
s.t theta_j <= 0
"""
if type(lbda) == int or type(lbda) == float:
lbda = np.array(lbda)
lbda = theano.shared(lbda.astype(theano.config.floatX))
theta = tensor.row().T
theta_ = theta.flatten()
M = tensor.matrix(name='M', dtype=theano.config.floatX)
w = tensor.vector(name='w', dtype=theano.config.floatX)
y = lbda * (theta_).norm(1) + (
tensor.exp(M.dot(theta_)) - (1 - w)).norm(2)
z = tensor.row().T
z_ = z.flatten()
y_diff = tensor.grad(y, theta_)
y_hess = z[0] * theano.gradient.hessian(y, theta_)
f_x = theano.function([theta, M, w], [y, y_diff],
allow_input_downcast=True)
f_xz = theano.function([theta, z, M, w], [y, y_diff, y_hess],
allow_input_downcast=True)
return f_x, f_xz
@timeout.timeout(70)
def diff_and_opt(M_val, w_val, f_x, f_xz):
if M_val.dtype == bool:
M_val = M_val.astype(theano.config.floatX)
m, n = M_val.shape
def F(x=None, z=None):
if x is None:
return 0, cvxopt.matrix(-.1, (n,1))
elif z is None:
y, y_diff = f_x(x, M_val, w_val)
return cvxopt.matrix(float(y), (1, 1)),\
cvxopt.matrix(y_diff.astype("float64")).T
else:
y, y_diff, y_hess = f_xz(x, z, M_val, w_val)
return cvxopt.matrix(float(y), (1, 1)), \
cvxopt.matrix(y_diff.astype("float64")).T, \
cvxopt.matrix(y_hess.astype("float64"))
G = cvxopt.spdiag([1 for i in range(n)])
h = cvxopt.matrix(0.0, (n,1))
#Relaxing precision constraints
# cvxopt.solvers.options['feastol'] = 2e-5
# cvxopt.solvers.options['abstol'] = 2e-5
# cvxopt.solvers.options['maxiters'] = 50
cvxopt.solvers.options['show_progress'] = False
try:
theta = cvxopt.solvers.cp(F, G, h)['x']
except ArithmeticError:
print("ArithmeticError thrown, change initial point"
" given to the solver")
except ValueError:
print("Domain Error, skipping to next node")
theta = np.zeros(M_val.shape[1])
if cvxopt.solvers.options['show_progress']:
print(1 - np.exp(theta))
return 1 - np.exp(theta), theta
def restrictedEigenvalue(M_val, w_val, theta_val, gramMatrix=False):
"""
returns the smallest restricted eigenvalue of the matrix on the set S
gramMatrix (bool) : If true, use the gram matrix instead of the Hessian
theta_val : true vector of parameters
"""
m, n = M_val.shape
if gramMatrix:
H = 1./m * np.dot(M_val.T,M_val)
else:
#compute the Hessian
#theta = tensor.row().T
#theta_ = theta.flatten()
#M = tensor.matrix(name='M', dtype=theano.config.floatX)
#w = tensor.vector(name='w', dtype=theano.config.floatX)
#y = - 1./n*( (w).dot(tensor.log(1-tensor.exp(M.dot(theta_)))) +
#(1-w).dot(tensor.dot(M, theta_)))
#f = theano.function([theta, M, w], [theano.gradient.hessian(y,
#theta_)], allow_input_downcast=True)
#print(theta_val)
#H = f(np.atleast_2d(theta_val).T, M_val.astype('float64'), w_val)
#print(H)
#evals_small, evecs_small = scipy.sparse.linalg.eigsh(H.astype('float64'), 1,
#which="LM")
#print(evals_small)
theta = tensor.vector()
M = tensor.matrix(name='M', dtype=theano.config.floatX)
w = tensor.vector(name='w', dtype=theano.config.floatX)
y = - 1./n*((w).dot(tensor.log(1-tensor.exp(M.dot(theta)))) +
(1-w).dot(tensor.dot(M, theta)))
f = theano.function([theta, M, w], [theano.gradient.hessian(y,
theta)], allow_input_downcast=True)
H = f(theta_val, M_val.astype('float32'), w_val)
print(H)
evals_small, evecs_small = scipy.sparse.linalg.eigsh(H, 1,
which="LM")
print(evals_small)
def test():
"""
unit test
"""
lbda = .0001
G = cascade_creation.InfluenceGraph(max_proba=.9, min_proba=.2)
G.erdos_init(n=20, p = .3)
A = cascade_creation.generate_cascades(G, .1, 500)
M_val, w_val = cascade_creation.icc_matrixvector_for_node(A, 0)
print(M_val.shape)
print(w_val.shape)
#Type lasso
if 0:
f_x, f_xz = type_lasso(lbda, 500)
p_vec, _ = diff_and_opt(M_val, w_val, f_x, f_xz)
print(G.mat[2])
#Sparse recovery
if 0:
f_x, f_xz = sparse_recovery(lbda, 500)
p_vec, _ = diff_and_opt(M_val, w_val, f_x, f_xz)
print(G.mat[2])
#Restricted Eigenvalue
if 2:
node = 1
parents = np.nonzero(G.mat[:, node])[0]
theta_val = G.logmat[:,node]
restrictedEigenvalue(M_val, w_val, theta_val)
if __name__=="__main__":
test()
|