diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/algorithms.py | 9 | ||||
| -rw-r--r-- | src/cascade_creation.py | 2 | ||||
| -rw-r--r-- | src/convex_optimization.py | 39 | ||||
| -rw-r--r-- | src/make_plots.py | 11 |
4 files changed, 53 insertions, 8 deletions
diff --git a/src/algorithms.py b/src/algorithms.py index c880a7b..29de661 100644 --- a/src/algorithms.py +++ b/src/algorithms.py @@ -39,6 +39,11 @@ def recovery_l1obj_l2constraint(G, cascades, floor_cstt, passed_function, """ G_hat = cascade_creation.InfluenceGraph(max_proba=None) G_hat.add_nodes_from(G.nodes()) + + #### + print(G.nodes()) + #### + for node in G_hat.nodes(): print(node) try: @@ -85,10 +90,10 @@ def test(): unit test """ G = cascade_creation.InfluenceGraph(max_proba = .8) - G.erdos_init(n = 100, p = .05) + G.erdos_init(n = 10, p = .2) import time t0 = time.time() - A = cascade_creation.generate_cascades(G, .2, 10000) + A = cascade_creation.generate_cascades(G, .2, 1000) if 1: G_hat = greedy_prediction(G, A) if 0: diff --git a/src/cascade_creation.py b/src/cascade_creation.py index 4c47d8b..93ce662 100644 --- a/src/cascade_creation.py +++ b/src/cascade_creation.py @@ -52,7 +52,7 @@ class Cascade(list): Returns lists of infections times for node i in cascade """ infected_times = [] - for t, infected_set in zip(range(len(self)), self): + for t, infected_set in enumerate(self): if infected_set[node]: infected_times.append(t) if not infected_times: diff --git a/src/convex_optimization.py b/src/convex_optimization.py index 02787d3..1d84db2 100644 --- a/src/convex_optimization.py +++ b/src/convex_optimization.py @@ -6,11 +6,46 @@ import timeout import cvxopt - @timeout.timeout(20) def sparse_recovery(M_val, w_val, lbda): """ Solves: + min lbda * |theta|_1 - sum b^i log(exp(-<M[i], theta_>) -1) - M*theta + s.t theta_j <= 0 + """ + assert len(M_val) == len(w_val) + + if M_val.dtype == bool: + M_val = M_val.astype('float32') + + if type(lbda) == int: + lbda = np.array(lbda) + + theta = tensor.row().T + theta_ = theta.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_tmp = lbda * (theta_).norm(1) - tensor.sum(tensor.dot(M, theta_ + )) + w.dot(tensor.log(tensor.exp(-M.dot(theta_))-1)) + y_diff = tensor.grad(y_tmp, theta_) + y_hess = theano.gradient.hessian(y_tmp, theta_) + f = function([theta_], y_hess) + + print(f(-1*np.random.rand(M_val.shape[1]).astype("float32"))) + + y = lbda * (theta_).norm(1) - tensor.sum(tensor.dot(M, theta_ + )) - w.dot(tensor.log(tensor.exp(-M.dot(theta_))-1)) + + return diff_and_opt(theta, theta_, M, M_val, w, lbda, y) + + +@timeout.timeout(20) +def type_lasso(M_val, w_val, lbda): + """ + Solves: min - sum_j theta_j + lbda*|e^{M*theta} - (1 - w)|_2 s.t theta_j <= 0 """ @@ -63,7 +98,7 @@ def diff_and_opt(theta, theta_, M, M_val, w, lbda, y): G = cvxopt.spdiag([1 for i in range(n)]) h = cvxopt.matrix(0.0, (n,1)) - cvxopt.solvers.options['show_progress'] = False + cvxopt.solvers.options['show_progress'] = True try: theta = cvxopt.solvers.cp(F, G, h)['x'] except ArithmeticError: diff --git a/src/make_plots.py b/src/make_plots.py index 5aab683..18d0874 100644 --- a/src/make_plots.py +++ b/src/make_plots.py @@ -22,7 +22,7 @@ def compare_greedy_and_lagrange_cs284r(): #Lagrange Objective G_hat = algorithms.recovery_l1obj_l2constraint(G, A, - passed_function=convex_optimization.sparse_recovery, + passed_function=convex_optimization.type_lasso, floor_cstt=.1, lbda=10) algorithms.correctness_measure(G, G_hat, print_values=True) @@ -31,8 +31,13 @@ def test(): """ unit test """ - if 1: - compare_greedy_and_lagrange_cs284r() + G = cascade_creation.InfluenceGraph(max_proba=.3) + G.erdos_init(n=10, p=.2) + A = cascade_creation.generate_cascades(G, p_init=.2, n_cascades=100) + G_hat = algorithms.recovery_l1obj_l2constraint(G, A, + passed_function=convex_optimization.sparse_recovery, + floor_cstt=.1, lbda=10) + algorithms.correctness_measure(G, G_hat, print_values=True) if __name__=="__main__": test() |
