Mercurial > hg > pycsalgos
comparison scripts/ABSapprox.py @ 19:ef0629f859a3
Working approximation script
author | nikcleju |
---|---|
date | Mon, 07 Nov 2011 22:15:13 +0000 |
parents | edb5a287e0bb |
children | 45255b0a6dba |
comparison
equal
deleted
inserted
replaced
18:a8ff9a881d2f | 19:ef0629f859a3 |
---|---|
3 Created on Sat Nov 05 18:08:40 2011 | 3 Created on Sat Nov 05 18:08:40 2011 |
4 | 4 |
5 @author: Nic | 5 @author: Nic |
6 """ | 6 """ |
7 | 7 |
8 import numpy | 8 import numpy as np |
9 import pyCSalgos | 9 import pyCSalgos |
10 import pyCSalgos.GAP.GAP | |
11 import pyCSalgos.SL0.SL0_approx | |
10 | 12 |
13 # Define functions that prepare arguments for each algorithm call | |
11 def gap_paramsetup(y,M,Omega,epsilon,lbd): | 14 def gap_paramsetup(y,M,Omega,epsilon,lbd): |
12 gapparams = dict(num_iteration = 1000, | 15 gapparams = {"num_iteration" : 1000,\ |
13 greedy_level = 0.9, | 16 "greedy_level" : 0.9,\ |
14 stopping_coefficientstopping_coefficient_size = 1e-4, | 17 "stopping_coefficient_size" : 1e-4,\ |
15 l2solver = 'pseudoinverse', | 18 "l2solver" : 'pseudoinverse',\ |
16 noise_level = epsilon) | 19 "noise_level": epsilon} |
17 return y,M,M.T,Omega,Omega.T,gapparams,numpy.zeros(Omega.shape[1]) | 20 return y,M,M.T,Omega,Omega.T,gapparams,np.zeros(Omega.shape[1]) |
21 def sl0_paramsetup(y,M,Omega,epsilon,lbd): | |
22 | |
23 N,n = Omega.shape | |
24 D = np.linalg.pinv(Omega) | |
25 U,S,Vt = np.linalg.svd(D) | |
26 aggDupper = np.dot(M,D) | |
27 aggDlower = Vt[-(N-n):,:] | |
28 aggD = np.concatenate((aggDupper, lbd * aggDlower)) | |
29 aggy = np.concatenate((y, np.zeros(N-n))) | |
30 | |
31 sigmamin = 0.1 | |
32 return aggD,aggy,epsilon,sigmamin | |
18 | 33 |
19 def omp_paramsetup(y,M,Omega,epsilon,lbd): | 34 # Define tuples (algorithm setup function, algorithm function, name) |
20 gapparams = dict(num_iteration = 1000, | 35 gap = (gap_paramsetup, pyCSalgos.GAP.GAP.GAP, 'GAP') |
21 greedy_level = 0.9, | 36 sl0 = (sl0_paramsetup, pyCSalgos.SL0.SL0_approx.SL0_approx, 'SL0_approx') |
22 stopping_coefficientstopping_coefficient_size = 1e-4, | |
23 l2solver = 'pseudoinverse', | |
24 noise_level = epsilon) | |
25 return y,M,M.T,Omega,Omega.T,gapparams,numpy.zeros(Omega.shape[1]) | |
26 | 37 |
27 gap = (pyCSalgos.GAP, gap_paramsetup) | 38 # Main function |
39 def mainrun(): | |
40 | |
41 # Define which algorithms to run | |
42 algos = (gap, sl0) | |
43 numalgos = len(algos) | |
44 | |
45 # Set up experiment parameters | |
46 sigma = 2.0; | |
47 delta = 0.8; | |
48 rho = 0.15; | |
49 numvects = 2; # Number of vectors to generate | |
50 SNRdb = 20; # This is norm(signal)/norm(noise), so power, not energy | |
51 | |
52 # Process parameters | |
53 noiselevel = 1 / (10^(SNRdb/10)); | |
54 d = 50; | |
55 p = round(sigma*d); | |
56 m = round(delta*d); | |
57 l = round(d - rho*m); | |
58 | |
59 # Generate Omega and data based on parameters | |
60 Omega = pyCSalgos.GAP.GAP.Generate_Analysis_Operator(d, p); | |
61 # Optionally make Omega more coherent | |
62 #[U, S, Vt] = np.linalg.svd(Omega); | |
63 #Sdnew = np.diag(S) * (1+np.arange(np.diag(S).size)); % Make D coherent, not Omega! | |
64 #Snew = [diag(Sdnew); zeros(size(S,1) - size(S,2), size(S,2))]; | |
65 #Omega = U * Snew * V'; | |
66 | |
67 # Generate data | |
68 x0,y,M,Lambda,realnoise = pyCSalgos.GAP.GAP.Generate_Data_Known_Omega(Omega, d,p,m,l,noiselevel, numvects,'l0'); | |
69 | |
70 # Values for lambda | |
71 #lambdas = [0 10.^linspace(-5, 4, 10)]; | |
72 lambdas = np.concatenate((np.array([0]), 10**np.linspace(-5, 4, 10))) | |
73 | |
74 xrec = dict() | |
75 err = dict() | |
76 relerr = dict() | |
77 for i,algo in zip(np.arange(numalgos),algos): | |
78 xrec[algo[2]] = np.zeros((lambdas.size, d, y.shape[1])) | |
79 err[algo[2]] = np.zeros((lambdas.size, y.shape[1])) | |
80 relerr[algo[2]] = np.zeros((lambdas.size, y.shape[1])) | |
81 | |
82 for ilbd,lbd in zip(np.arange(lambdas.size),lambdas): | |
83 for iy in np.arange(y.shape[1]): | |
84 for algosetupfunc,algofunc,strname in algos: | |
85 epsilon = 1.1 * np.linalg.norm(realnoise[:,iy]) | |
86 | |
87 inparams = algosetupfunc(y[:,iy],M,Omega,epsilon,lbd) | |
88 xrec[strname][ilbd,:,iy] = algofunc(*inparams)[0] | |
89 | |
90 err[strname][ilbd,iy] = np.linalg.norm(x0[:,iy] - xrec[strname][ilbd,:,iy]) | |
91 relerr[strname][ilbd,iy] = err[strname][ilbd,iy] / np.linalg.norm(x0[:,iy]) | |
92 | |
93 print 'Lambda = ',lbd,' :' | |
94 for strname in relerr: | |
95 print ' ',strname,' : avg relative error = ',np.mean(relerr[strname][ilbd,:]) | |
28 | 96 |
29 | 97 |
30 | 98 |
31 gap = (pyCSalgos.GAP, gap_paramsetup) | 99 # Script main |
32 | 100 if __name__ == "__main__": |
33 | 101 mainrun() |
34 | |
35 | |
36 def mainrun(): | |
37 | |
38 algos = (gap, sl0) | |
39 | |
40 for algofunc,paramsetup in algos: | |
41 xrec = algofunc(algosetup(y, Omega, epsilon, lbd)) |