diff options
| author | jeanpouget-abadie <jean.pougetabadie@gmail.com> | 2015-02-01 13:43:12 -0500 |
|---|---|---|
| committer | jeanpouget-abadie <jean.pougetabadie@gmail.com> | 2015-02-01 13:43:12 -0500 |
| commit | 0991a13214af4023259465f132f09e6c66f3895c (patch) | |
| tree | 4c3f89721285eb07a004e8efae9375f3dbd4919e /src/convex_optimization.py | |
| parent | 6446b4bc986be1e4ff4bd1914efc4a2e6d0b7d12 (diff) | |
| download | cascades-0991a13214af4023259465f132f09e6c66f3895c.tar.gz | |
remodeling results section
Diffstat (limited to 'src/convex_optimization.py')
| -rw-r--r-- | src/convex_optimization.py | 72 |
1 files changed, 13 insertions, 59 deletions
diff --git a/src/convex_optimization.py b/src/convex_optimization.py index 96dc26f..2661e25 100644 --- a/src/convex_optimization.py +++ b/src/convex_optimization.py @@ -6,63 +6,9 @@ import timeout import cvxopt -@timeout.timeout(20) -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 - """ - assert len(M_val) == len(w_val) - - if M_val.dtype == bool: - M_val = M_val.astype('float32') - - 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(.0001, (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)) - - cvxopt.solvers.options['show_progress'] = False - try: - theta = cvxopt.solvers.cpl(c,F, G, h)['x'] - except ArithmeticError: - print "ArithmeticError thrown, change initial point"+\ - " given to the solver" - - return 1 - np.exp(theta), theta - @timeout.timeout(20) -def l1obj_l2penalization(M_val, w_val, lbda): +def sparse_recovery(M_val, w_val, lbda): """ Solves: min - sum_j theta_j + lbda*|e^{M*theta} - (1 - w)|_2 @@ -76,17 +22,25 @@ def l1obj_l2penalization(M_val, w_val, lbda): if type(lbda) == int: lbda = np.array(lbda) - m, n = M_val.shape - 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)) lbda = theano.shared(lbda.astype(theano.config.floatX)) + y = (theta_).norm(1) + lbda * ( tensor.exp(M.dot(theta_)) - (1 - w)).norm(2) + + return diff_and_opt(theta, theta_, M, w, lbda, y) + + +def diff_and_opt(theta, theta_, M, w, lbda, y): + z = tensor.row().T + z_ = z.flatten() + + m, n = M_val.shape + 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) |
