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
|
import theano
from theano import tensor, function
import numpy as np
import cvxopt
def l1obj_l2constraint(M_val, w_val):
"""
Solves:
min - sum_j theta_j
s.t theta_j <= 0
|e^{M*theta} - (1 - w)|_2 <= 1
"""
m, n = M_val.shape
c = cvxopt.matrix(-1.0, (n,1))
theta = tensor.row().T
z = tensor.row().T
theta_ = theta.flatten()
z_ = z.flatten()
M = theano.shared(M_val.astype(theano.config.floatX))
w = theano.shared(w_val.astype(theano.config.floatX))
y = (tensor.exp(M.dot(theta_)) - (1 - w)).norm(2) - 1
y_diff = tensor.grad(y, theta_)
y_hess = z[0] * theano.gradient.hessian(y, theta_)
f_x = theano.function([theta], [y, y_diff], allow_input_downcast=True)
f_xz = theano.function([theta, z], [y, y_diff, y_hess], allow_input_downcast=True)
def F(x=None, z=None):
if x is None:
return 1, cvxopt.matrix(1.0, (n,1))
elif z is None:
y, y_diff = f_x(x)
return cvxopt.matrix(float(y), (1, 1)),\
cvxopt.matrix(y_diff.astype("float64")).T
else:
y, y_diff, y_hess = f_xz(x, z)
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 xrange(n)])
h = cvxopt.matrix(0.0, (n,1))
return cvxopt.solvers.cpl(c,F, G, h)['x']
def l1obj_l2regl(M_val, w_val, lbda):
"""
Solves:
min - sum_j theta_j + lbda * |e^{M*theta} - (1 - w)|_2
s.t theta_j <= 0
"""
#TODO!!!!!!!
m, n = M_val.shape
c = cvxopt.matrix(-1.0, (n,1))
theta = tensor.row().T
z = tensor.row().T
theta_ = theta.flatten()
z_ = z.flatten()
M = theano.shared(M_val.astype(theano.config.floatX))
w = theano.shared(w_val.astype(theano.config.floatX))
y = (tensor.exp(M.dot(theta_)) - (1 - w)).norm(2) - 1
y_diff = tensor.grad(y, theta_)
y_hess = z[0] * theano.gradient.hessian(y, theta_)
f_x = theano.function([theta], [y, y_diff], allow_input_downcast=True)
f_xz = theano.function([theta, z], [y, y_diff, y_hess], allow_input_downcast=True)
def F(x=None, z=None):
if x is None:
return 1, cvxopt.matrix(1.0, (n,1))
elif z is None:
y, y_diff = f_x(x)
return cvxopt.matrix(float(y), (1, 1)),\
cvxopt.matrix(y_diff.astype("float64")).T
else:
y, y_diff, y_hess = f_xz(x, z)
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 xrange(n)])
h = cvxopt.matrix(0.0, (n,1))
return cvxopt.solvers.cpl(c,F, G, h)['x']
def test():
"""
unit test
"""
M_val = np.random.rand(100, 20)
w_val = np.random.rand(100)
print l1obj_l2regl(M_val, w_val, 1)
if __name__=="__main__":
test()
|