nikcleju@21
|
1 # -*- coding: utf-8 -*-
|
nikcleju@21
|
2 """
|
nikcleju@21
|
3 Created on Thu Oct 13 14:05:22 2011
|
nikcleju@21
|
4
|
nikcleju@21
|
5 @author: ncleju
|
nikcleju@21
|
6 """
|
nikcleju@21
|
7
|
nikcleju@21
|
8 #from numpy import *
|
nikcleju@21
|
9 #from scipy import *
|
nikcleju@27
|
10 import numpy
|
nikcleju@27
|
11 import numpy.linalg
|
nikcleju@21
|
12 import scipy as sp
|
nikcleju@27
|
13 #import scipy.stats #from scipy import stats
|
nikcleju@27
|
14 #import scipy.linalg #from scipy import lnalg
|
nikcleju@21
|
15 import math
|
nikcleju@21
|
16
|
nikcleju@21
|
17 from numpy.random import RandomState
|
nikcleju@21
|
18 rng = RandomState()
|
nikcleju@21
|
19
|
nikcleju@21
|
20 def Generate_Analysis_Operator(d, p):
|
nikcleju@21
|
21 # generate random tight frame with equal column norms
|
nikcleju@21
|
22 if p == d:
|
nikcleju@21
|
23 T = rng.randn(d,d);
|
nikcleju@27
|
24 [Omega, discard] = numpy.qr(T);
|
nikcleju@21
|
25 else:
|
nikcleju@21
|
26 Omega = rng.randn(p, d);
|
nikcleju@27
|
27 T = numpy.zeros((p, d));
|
nikcleju@21
|
28 tol = 1e-8;
|
nikcleju@21
|
29 max_j = 200;
|
nikcleju@21
|
30 j = 1;
|
nikcleju@27
|
31 while (sum(sum(abs(T-Omega))) > numpy.dot(tol,numpy.dot(p,d)) and j < max_j):
|
nikcleju@21
|
32 j = j + 1;
|
nikcleju@21
|
33 T = Omega;
|
nikcleju@27
|
34 [U, S, Vh] = numpy.linalg.svd(Omega);
|
nikcleju@21
|
35 V = Vh.T
|
nikcleju@21
|
36 #Omega = U * [eye(d); zeros(p-d,d)] * V';
|
nikcleju@27
|
37 Omega2 = numpy.dot(numpy.dot(U, numpy.concatenate((numpy.eye(d), numpy.zeros((p-d,d))))), V.transpose())
|
nikcleju@21
|
38 #Omega = diag(1./sqrt(diag(Omega*Omega')))*Omega;
|
nikcleju@27
|
39 Omega = numpy.dot(numpy.diag(1.0 / numpy.sqrt(numpy.diag(numpy.dot(Omega2,Omega2.transpose())))), Omega2)
|
nikcleju@21
|
40 #end
|
nikcleju@21
|
41 ##disp(j);
|
nikcleju@21
|
42 #end
|
nikcleju@21
|
43 return Omega
|
nikcleju@21
|
44
|
nikcleju@21
|
45
|
nikcleju@21
|
46 def Generate_Data_Known_Omega(Omega, d,p,m,k,noiselevel, numvectors, normstr):
|
nikcleju@21
|
47 #function [x0,y,M,LambdaMat] = Generate_Data_Known_Omega(Omega, d,p,m,k,noiselevel, numvectors, normstr)
|
nikcleju@21
|
48
|
nikcleju@21
|
49 # Building an analysis problem, which includes the ingredients:
|
nikcleju@21
|
50 # - Omega - the analysis operator of size p*d
|
nikcleju@21
|
51 # - M is anunderdetermined measurement matrix of size m*d (m<d)
|
nikcleju@21
|
52 # - x0 is a vector of length d that satisfies ||Omega*x0||=p-k
|
nikcleju@21
|
53 # - Lambda is the true location of these k zeros in Omega*x0
|
nikcleju@21
|
54 # - a measurement vector y0=Mx0 is computed
|
nikcleju@21
|
55 # - noise contaminated measurement vector y is obtained by
|
nikcleju@21
|
56 # y = y0 + n where n is an additive gaussian noise with norm(n,2)/norm(y0,2) = noiselevel
|
nikcleju@21
|
57 # Added by Nic:
|
nikcleju@21
|
58 # - Omega = analysis operator
|
nikcleju@21
|
59 # - normstr: if 'l0', generate l0 sparse vector (unchanged). If 'l1',
|
nikcleju@21
|
60 # generate a vector of Laplacian random variables (gamma) and
|
nikcleju@21
|
61 # pseudoinvert to find x
|
nikcleju@21
|
62
|
nikcleju@21
|
63 # Omega is known as input parameter
|
nikcleju@21
|
64 #Omega=Generate_Analysis_Operator(d, p);
|
nikcleju@21
|
65 # Omega = randn(p,d);
|
nikcleju@21
|
66 # for i = 1:size(Omega,1)
|
nikcleju@21
|
67 # Omega(i,:) = Omega(i,:) / norm(Omega(i,:));
|
nikcleju@21
|
68 # end
|
nikcleju@21
|
69
|
nikcleju@21
|
70 #Init
|
nikcleju@27
|
71 LambdaMat = numpy.zeros((k,numvectors))
|
nikcleju@27
|
72 x0 = numpy.zeros((d,numvectors))
|
nikcleju@27
|
73 y = numpy.zeros((m,numvectors))
|
nikcleju@27
|
74 realnoise = numpy.zeros((m,numvectors))
|
nikcleju@21
|
75
|
nikcleju@21
|
76 M = rng.randn(m,d);
|
nikcleju@21
|
77
|
nikcleju@21
|
78 #for i=1:numvectors
|
nikcleju@21
|
79 for i in range(0,numvectors):
|
nikcleju@21
|
80
|
nikcleju@21
|
81 # Generate signals
|
nikcleju@21
|
82 #if strcmp(normstr,'l0')
|
nikcleju@21
|
83 if normstr == 'l0':
|
nikcleju@21
|
84 # Unchanged
|
nikcleju@21
|
85
|
nikcleju@21
|
86 #Lambda=randperm(p);
|
nikcleju@21
|
87 Lambda = rng.permutation(int(p));
|
nikcleju@27
|
88 Lambda = numpy.sort(Lambda[0:k]);
|
nikcleju@21
|
89 LambdaMat[:,i] = Lambda; # store for output
|
nikcleju@21
|
90
|
nikcleju@21
|
91 # The signal is drawn at random from the null-space defined by the rows
|
nikcleju@21
|
92 # of the matreix Omega(Lambda,:)
|
nikcleju@27
|
93 [U,D,Vh] = numpy.linalg.svd(Omega[Lambda,:]);
|
nikcleju@21
|
94 V = Vh.T
|
nikcleju@21
|
95 NullSpace = V[:,k:];
|
nikcleju@27
|
96 #print numpy.dot(NullSpace, rng.randn(d-k,1)).shape
|
nikcleju@21
|
97 #print x0[:,i].shape
|
nikcleju@27
|
98 x0[:,i] = numpy.squeeze(numpy.dot(NullSpace, rng.randn(d-k,1)));
|
nikcleju@21
|
99 # Nic: add orthogonality noise
|
nikcleju@21
|
100 # orthonoiseSNRdb = 6;
|
nikcleju@21
|
101 # n = randn(p,1);
|
nikcleju@21
|
102 # #x0(:,i) = x0(:,i) + n / norm(n)^2 * norm(x0(:,i))^2 / 10^(orthonoiseSNRdb/10);
|
nikcleju@21
|
103 # n = n / norm(n)^2 * norm(Omega * x0(:,i))^2 / 10^(orthonoiseSNRdb/10);
|
nikcleju@21
|
104 # x0(:,i) = pinv(Omega) * (Omega * x0(:,i) + n);
|
nikcleju@21
|
105
|
nikcleju@21
|
106 #elseif strcmp(normstr, 'l1')
|
nikcleju@21
|
107 elif normstr == 'l1':
|
nikcleju@21
|
108 print('Nic says: not implemented yet')
|
nikcleju@21
|
109 raise Exception('Nic says: not implemented yet')
|
nikcleju@21
|
110 #gamma = laprnd(p,1,0,1);
|
nikcleju@21
|
111 #x0(:,i) = Omega \ gamma;
|
nikcleju@21
|
112 else:
|
nikcleju@21
|
113 #error('normstr must be l0 or l1!');
|
nikcleju@21
|
114 print('Nic says: not implemented yet')
|
nikcleju@21
|
115 raise Exception('Nic says: not implemented yet')
|
nikcleju@21
|
116 #end
|
nikcleju@21
|
117
|
nikcleju@21
|
118 # Acquire measurements
|
nikcleju@27
|
119 y[:,i] = numpy.dot(M, x0[:,i])
|
nikcleju@21
|
120
|
nikcleju@21
|
121 # Add noise
|
nikcleju@27
|
122 t_norm = numpy.linalg.norm(y[:,i],2);
|
nikcleju@27
|
123 n = numpy.squeeze(rng.randn(m, 1));
|
nikcleju@27
|
124 y[:,i] = y[:,i] + noiselevel * t_norm * n / numpy.linalg.norm(n, 2);
|
nikcleju@27
|
125 realnoise[:,i] = noiselevel * t_norm * n / numpy.linalg.norm(n, 2)
|
nikcleju@21
|
126 #end
|
nikcleju@21
|
127
|
nikcleju@21
|
128 return x0,y,M,LambdaMat,realnoise
|
nikcleju@21
|
129
|
nikcleju@21
|
130 #####################
|
nikcleju@21
|
131
|
nikcleju@21
|
132 #function [xhat, arepr, lagmult] = ArgminOperL2Constrained(y, M, MH, Omega, OmegaH, Lambdahat, xinit, ilagmult, params)
|
nikcleju@21
|
133 def ArgminOperL2Constrained(y, M, MH, Omega, OmegaH, Lambdahat, xinit, ilagmult, params):
|
nikcleju@21
|
134
|
nikcleju@21
|
135 #
|
nikcleju@21
|
136 # This function aims to compute
|
nikcleju@21
|
137 # xhat = argmin || Omega(Lambdahat, :) * x ||_2 subject to || y - M*x ||_2 <= epsilon.
|
nikcleju@21
|
138 # arepr is the analysis representation corresponding to Lambdahat, i.e.,
|
nikcleju@21
|
139 # arepr = Omega(Lambdahat, :) * xhat.
|
nikcleju@21
|
140 # The function also returns the lagrange multiplier in the process used to compute xhat.
|
nikcleju@21
|
141 #
|
nikcleju@21
|
142 # Inputs:
|
nikcleju@21
|
143 # y : observation/measurements of an unknown vector x0. It is equal to M*x0 + noise.
|
nikcleju@21
|
144 # M : Measurement matrix
|
nikcleju@21
|
145 # MH : M', the conjugate transpose of M
|
nikcleju@21
|
146 # Omega : analysis operator
|
nikcleju@21
|
147 # OmegaH : Omega', the conjugate transpose of Omega. Also, synthesis operator.
|
nikcleju@21
|
148 # Lambdahat : an index set indicating some rows of Omega.
|
nikcleju@21
|
149 # xinit : initial estimate that will be used for the conjugate gradient algorithm.
|
nikcleju@21
|
150 # ilagmult : initial lagrange multiplier to be used in
|
nikcleju@21
|
151 # params : parameters
|
nikcleju@21
|
152 # params.noise_level : this corresponds to epsilon above.
|
nikcleju@21
|
153 # params.max_inner_iteration : `maximum' number of iterations in conjugate gradient method.
|
nikcleju@21
|
154 # params.l2_accurary : the l2 accuracy parameter used in conjugate gradient method
|
nikcleju@21
|
155 # params.l2solver : if the value is 'pseudoinverse', then direct matrix computation (not conjugate gradient method) is used. Otherwise, conjugate gradient method is used.
|
nikcleju@21
|
156 #
|
nikcleju@21
|
157
|
nikcleju@21
|
158 #d = length(xinit)
|
nikcleju@21
|
159 d = xinit.size
|
nikcleju@21
|
160 lagmultmax = 1e5;
|
nikcleju@21
|
161 lagmultmin = 1e-4;
|
nikcleju@21
|
162 lagmultfactor = 2.0;
|
nikcleju@21
|
163 accuracy_adjustment_exponent = 4/5.;
|
nikcleju@21
|
164 lagmult = max(min(ilagmult, lagmultmax), lagmultmin);
|
nikcleju@21
|
165 was_infeasible = 0;
|
nikcleju@21
|
166 was_feasible = 0;
|
nikcleju@21
|
167
|
nikcleju@21
|
168 #######################################################################
|
nikcleju@21
|
169 ## Computation done using direct matrix computation from matlab. (no conjugate gradient method.)
|
nikcleju@21
|
170 #######################################################################
|
nikcleju@21
|
171 #if strcmp(params.l2solver, 'pseudoinverse')
|
nikcleju@21
|
172 if params['l2solver'] == 'pseudoinverse':
|
nikcleju@21
|
173 #if strcmp(class(M), 'double') && strcmp(class(Omega), 'double')
|
nikcleju@21
|
174 if M.dtype == 'float64' and Omega.dtype == 'double':
|
nikcleju@21
|
175 while True:
|
nikcleju@21
|
176 alpha = math.sqrt(lagmult);
|
nikcleju@27
|
177 xhat = numpy.linalg.lstsq(numpy.concatenate((M, alpha*Omega[Lambdahat,:])), numpy.concatenate((y, numpy.zeros(Lambdahat.size))))[0]
|
nikcleju@27
|
178 temp = numpy.linalg.norm(y - numpy.dot(M,xhat), 2);
|
nikcleju@21
|
179 #disp(['fidelity error=', num2str(temp), ' lagmult=', num2str(lagmult)]);
|
nikcleju@21
|
180 if temp <= params['noise_level']:
|
nikcleju@21
|
181 was_feasible = True;
|
nikcleju@21
|
182 if was_infeasible:
|
nikcleju@21
|
183 break;
|
nikcleju@21
|
184 else:
|
nikcleju@21
|
185 lagmult = lagmult*lagmultfactor;
|
nikcleju@21
|
186 elif temp > params['noise_level']:
|
nikcleju@21
|
187 was_infeasible = True;
|
nikcleju@21
|
188 if was_feasible:
|
nikcleju@21
|
189 xhat = xprev.copy();
|
nikcleju@21
|
190 break;
|
nikcleju@21
|
191 lagmult = lagmult/lagmultfactor;
|
nikcleju@21
|
192 if lagmult < lagmultmin or lagmult > lagmultmax:
|
nikcleju@21
|
193 break;
|
nikcleju@21
|
194 xprev = xhat.copy();
|
nikcleju@27
|
195 arepr = numpy.dot(Omega[Lambdahat, :], xhat);
|
nikcleju@21
|
196 return xhat,arepr,lagmult;
|
nikcleju@21
|
197
|
nikcleju@21
|
198
|
nikcleju@21
|
199 ########################################################################
|
nikcleju@21
|
200 ## Computation using conjugate gradient method.
|
nikcleju@21
|
201 ########################################################################
|
nikcleju@21
|
202 #if strcmp(class(MH),'function_handle')
|
nikcleju@21
|
203 if hasattr(MH, '__call__'):
|
nikcleju@21
|
204 b = MH(y);
|
nikcleju@21
|
205 else:
|
nikcleju@27
|
206 b = numpy.dot(MH, y);
|
nikcleju@21
|
207
|
nikcleju@27
|
208 norm_b = numpy.linalg.norm(b, 2);
|
nikcleju@21
|
209 xhat = xinit.copy();
|
nikcleju@21
|
210 xprev = xinit.copy();
|
nikcleju@21
|
211 residual = TheHermitianMatrix(xhat, M, MH, Omega, OmegaH, Lambdahat, lagmult) - b;
|
nikcleju@21
|
212 direction = -residual;
|
nikcleju@21
|
213 iter = 0;
|
nikcleju@21
|
214
|
nikcleju@21
|
215 while iter < params.max_inner_iteration:
|
nikcleju@21
|
216 iter = iter + 1;
|
nikcleju@27
|
217 alpha = numpy.linalg.norm(residual,2)**2 / numpy.dot(direction.T, TheHermitianMatrix(direction, M, MH, Omega, OmegaH, Lambdahat, lagmult));
|
nikcleju@21
|
218 xhat = xhat + alpha*direction;
|
nikcleju@21
|
219 prev_residual = residual.copy();
|
nikcleju@21
|
220 residual = TheHermitianMatrix(xhat, M, MH, Omega, OmegaH, Lambdahat, lagmult) - b;
|
nikcleju@27
|
221 beta = numpy.linalg.norm(residual,2)**2 / numpy.linalg.norm(prev_residual,2)**2;
|
nikcleju@21
|
222 direction = -residual + beta*direction;
|
nikcleju@21
|
223
|
nikcleju@27
|
224 if numpy.linalg.norm(residual,2)/norm_b < params['l2_accuracy']*(lagmult**(accuracy_adjustment_exponent)) or iter == params['max_inner_iteration']:
|
nikcleju@21
|
225 #if strcmp(class(M), 'function_handle')
|
nikcleju@21
|
226 if hasattr(M, '__call__'):
|
nikcleju@27
|
227 temp = numpy.linalg.norm(y-M(xhat), 2);
|
nikcleju@21
|
228 else:
|
nikcleju@27
|
229 temp = numpy.linalg.norm(y-numpy.dot(M,xhat), 2);
|
nikcleju@21
|
230
|
nikcleju@21
|
231 #if strcmp(class(Omega), 'function_handle')
|
nikcleju@21
|
232 if hasattr(Omega, '__call__'):
|
nikcleju@21
|
233 u = Omega(xhat);
|
nikcleju@27
|
234 u = math.sqrt(lagmult)*numpy.linalg.norm(u(Lambdahat), 2);
|
nikcleju@21
|
235 else:
|
nikcleju@27
|
236 u = math.sqrt(lagmult)*numpy.linalg.norm(Omega[Lambdahat,:]*xhat, 2);
|
nikcleju@21
|
237
|
nikcleju@21
|
238
|
nikcleju@21
|
239 #disp(['residual=', num2str(norm(residual,2)), ' norm_b=', num2str(norm_b), ' omegapart=', num2str(u), ' fidelity error=', num2str(temp), ' lagmult=', num2str(lagmult), ' iter=', num2str(iter)]);
|
nikcleju@21
|
240
|
nikcleju@21
|
241 if temp <= params['noise_level']:
|
nikcleju@21
|
242 was_feasible = True;
|
nikcleju@21
|
243 if was_infeasible:
|
nikcleju@21
|
244 break;
|
nikcleju@21
|
245 else:
|
nikcleju@21
|
246 lagmult = lagmultfactor*lagmult;
|
nikcleju@21
|
247 residual = TheHermitianMatrix(xhat, M, MH, Omega, OmegaH, Lambdahat, lagmult) - b;
|
nikcleju@21
|
248 direction = -residual;
|
nikcleju@21
|
249 iter = 0;
|
nikcleju@21
|
250 elif temp > params['noise_level']:
|
nikcleju@21
|
251 lagmult = lagmult/lagmultfactor;
|
nikcleju@21
|
252 if was_feasible:
|
nikcleju@21
|
253 xhat = xprev.copy();
|
nikcleju@21
|
254 break;
|
nikcleju@21
|
255 was_infeasible = True;
|
nikcleju@21
|
256 residual = TheHermitianMatrix(xhat, M, MH, Omega, OmegaH, Lambdahat, lagmult) - b;
|
nikcleju@21
|
257 direction = -residual;
|
nikcleju@21
|
258 iter = 0;
|
nikcleju@21
|
259 if lagmult > lagmultmax or lagmult < lagmultmin:
|
nikcleju@21
|
260 break;
|
nikcleju@21
|
261 xprev = xhat.copy();
|
nikcleju@21
|
262 #elseif norm(xprev-xhat)/norm(xhat) < 1e-2
|
nikcleju@21
|
263 # disp(['rel_change=', num2str(norm(xprev-xhat)/norm(xhat))]);
|
nikcleju@21
|
264 # if strcmp(class(M), 'function_handle')
|
nikcleju@21
|
265 # temp = norm(y-M(xhat), 2);
|
nikcleju@21
|
266 # else
|
nikcleju@21
|
267 # temp = norm(y-M*xhat, 2);
|
nikcleju@21
|
268 # end
|
nikcleju@21
|
269 #
|
nikcleju@21
|
270 # if temp > 1.2*params.noise_level
|
nikcleju@21
|
271 # was_infeasible = 1;
|
nikcleju@21
|
272 # lagmult = lagmult/lagmultfactor;
|
nikcleju@21
|
273 # xprev = xhat;
|
nikcleju@21
|
274 # end
|
nikcleju@21
|
275
|
nikcleju@21
|
276 #disp(['fidelity_error=', num2str(temp)]);
|
nikcleju@21
|
277 print 'fidelity_error=',temp
|
nikcleju@21
|
278 #if iter == params['max_inner_iteration']:
|
nikcleju@21
|
279 #disp('max_inner_iteration reached. l2_accuracy not achieved.');
|
nikcleju@21
|
280
|
nikcleju@21
|
281 ##
|
nikcleju@21
|
282 # Compute analysis representation for xhat
|
nikcleju@21
|
283 ##
|
nikcleju@21
|
284 #if strcmp(class(Omega),'function_handle')
|
nikcleju@21
|
285 if hasattr(Omega, '__call__'):
|
nikcleju@21
|
286 temp = Omega(xhat);
|
nikcleju@21
|
287 arepr = temp(Lambdahat);
|
nikcleju@21
|
288 else: ## here Omega is assumed to be a matrix
|
nikcleju@27
|
289 arepr = numpy.dot(Omega[Lambdahat, :], xhat);
|
nikcleju@21
|
290
|
nikcleju@21
|
291 return xhat,arepr,lagmult
|
nikcleju@21
|
292
|
nikcleju@21
|
293
|
nikcleju@21
|
294 ##
|
nikcleju@21
|
295 # This function computes (M'*M + lm*Omega(L,:)'*Omega(L,:)) * x.
|
nikcleju@21
|
296 ##
|
nikcleju@21
|
297 #function w = TheHermitianMatrix(x, M, MH, Omega, OmegaH, L, lm)
|
nikcleju@21
|
298 def TheHermitianMatrix(x, M, MH, Omega, OmegaH, L, lm):
|
nikcleju@21
|
299 #if strcmp(class(M), 'function_handle')
|
nikcleju@21
|
300 if hasattr(M, '__call__'):
|
nikcleju@21
|
301 w = MH(M(x));
|
nikcleju@21
|
302 else: ## M and MH are matrices
|
nikcleju@27
|
303 w = numpy.dot(numpy.dot(MH, M), x);
|
nikcleju@21
|
304
|
nikcleju@21
|
305 if hasattr(Omega, '__call__'):
|
nikcleju@21
|
306 v = Omega(x);
|
nikcleju@27
|
307 vt = numpy.zeros(v.size);
|
nikcleju@21
|
308 vt[L] = v[L].copy();
|
nikcleju@21
|
309 w = w + lm*OmegaH(vt);
|
nikcleju@21
|
310 else: ## Omega is assumed to be a matrix and OmegaH is its conjugate transpose
|
nikcleju@27
|
311 w = w + lm*numpy.dot(numpy.dot(OmegaH[:, L],Omega[L, :]),x);
|
nikcleju@21
|
312
|
nikcleju@21
|
313 return w
|
nikcleju@21
|
314
|
nikcleju@21
|
315 def GAP(y, M, MH, Omega, OmegaH, params, xinit):
|
nikcleju@21
|
316 #function [xhat, Lambdahat] = GAP(y, M, MH, Omega, OmegaH, params, xinit)
|
nikcleju@21
|
317
|
nikcleju@21
|
318 ##
|
nikcleju@21
|
319 # [xhat, Lambdahat] = GAP(y, M, MH, Omega, OmegaH, params, xinit)
|
nikcleju@21
|
320 #
|
nikcleju@21
|
321 # Greedy Analysis Pursuit Algorithm
|
nikcleju@21
|
322 # This aims to find an approximate (sometimes exact) solution of
|
nikcleju@21
|
323 # xhat = argmin || Omega * x ||_0 subject to || y - M * x ||_2 <= epsilon.
|
nikcleju@21
|
324 #
|
nikcleju@21
|
325 # Outputs:
|
nikcleju@21
|
326 # xhat : estimate of the target cosparse vector x0.
|
nikcleju@21
|
327 # Lambdahat : estimate of the cosupport of x0.
|
nikcleju@21
|
328 #
|
nikcleju@21
|
329 # Inputs:
|
nikcleju@21
|
330 # y : observation/measurement vector of a target cosparse solution x0,
|
nikcleju@21
|
331 # given by relation y = M * x0 + noise.
|
nikcleju@21
|
332 # M : measurement matrix. This should be given either as a matrix or as a function handle
|
nikcleju@21
|
333 # which implements linear transformation.
|
nikcleju@21
|
334 # MH : conjugate transpose of M.
|
nikcleju@21
|
335 # Omega : analysis operator. Like M, this should be given either as a matrix or as a function
|
nikcleju@21
|
336 # handle which implements linear transformation.
|
nikcleju@21
|
337 # OmegaH : conjugate transpose of OmegaH.
|
nikcleju@21
|
338 # params : parameters that govern the behavior of the algorithm (mostly).
|
nikcleju@21
|
339 # params.num_iteration : GAP performs this number of iterations.
|
nikcleju@21
|
340 # params.greedy_level : determines how many rows of Omega GAP eliminates at each iteration.
|
nikcleju@21
|
341 # if the value is < 1, then the rows to be eliminated are determined by
|
nikcleju@21
|
342 # j : |omega_j * xhat| > greedy_level * max_i |omega_i * xhat|.
|
nikcleju@21
|
343 # if the value is >= 1, then greedy_level is the number of rows to be
|
nikcleju@21
|
344 # eliminated at each iteration.
|
nikcleju@21
|
345 # params.stopping_coefficient_size : when the maximum analysis coefficient is smaller than
|
nikcleju@21
|
346 # this, GAP terminates.
|
nikcleju@21
|
347 # params.l2solver : legitimate values are 'pseudoinverse' or 'cg'. determines which method
|
nikcleju@21
|
348 # is used to compute
|
nikcleju@21
|
349 # argmin || Omega_Lambdahat * x ||_2 subject to || y - M * x ||_2 <= epsilon.
|
nikcleju@21
|
350 # params.l2_accuracy : when l2solver is 'cg', this determines how accurately the above
|
nikcleju@21
|
351 # problem is solved.
|
nikcleju@21
|
352 # params.noise_level : this corresponds to epsilon above.
|
nikcleju@21
|
353 # xinit : initial estimate of x0 that GAP will start with. can be zeros(d, 1).
|
nikcleju@21
|
354 #
|
nikcleju@21
|
355 # Examples:
|
nikcleju@21
|
356 #
|
nikcleju@21
|
357 # Not particularly interesting:
|
nikcleju@21
|
358 # >> d = 100; p = 110; m = 60;
|
nikcleju@21
|
359 # >> M = randn(m, d);
|
nikcleju@21
|
360 # >> Omega = randn(p, d);
|
nikcleju@21
|
361 # >> y = M * x0 + noise;
|
nikcleju@21
|
362 # >> params.num_iteration = 40;
|
nikcleju@21
|
363 # >> params.greedy_level = 0.9;
|
nikcleju@21
|
364 # >> params.stopping_coefficient_size = 1e-4;
|
nikcleju@21
|
365 # >> params.l2solver = 'pseudoinverse';
|
nikcleju@21
|
366 # >> [xhat, Lambdahat] = GAP(y, M, M', Omega, Omega', params, zeros(d, 1));
|
nikcleju@21
|
367 #
|
nikcleju@21
|
368 # Assuming that FourierSampling.m, FourierSamplingH.m, FDAnalysis.m, etc. exist:
|
nikcleju@21
|
369 # >> n = 128;
|
nikcleju@21
|
370 # >> M = @(t) FourierSampling(t, n);
|
nikcleju@21
|
371 # >> MH = @(u) FourierSamplingH(u, n);
|
nikcleju@21
|
372 # >> Omega = @(t) FDAnalysis(t, n);
|
nikcleju@21
|
373 # >> OmegaH = @(u) FDSynthesis(t, n);
|
nikcleju@21
|
374 # >> params.num_iteration = 1000;
|
nikcleju@21
|
375 # >> params.greedy_level = 50;
|
nikcleju@21
|
376 # >> params.stopping_coefficient_size = 1e-5;
|
nikcleju@21
|
377 # >> params.l2solver = 'cg'; # in fact, 'pseudoinverse' does not even make sense.
|
nikcleju@21
|
378 # >> [xhat, Lambdahat] = GAP(y, M, MH, Omega, OmegaH, params, zeros(d, 1));
|
nikcleju@21
|
379 #
|
nikcleju@21
|
380 # Above: FourierSampling and FourierSamplingH are conjugate transpose of each other.
|
nikcleju@21
|
381 # FDAnalysis and FDSynthesis are conjugate transpose of each other.
|
nikcleju@21
|
382 # These routines are problem specific and need to be implemented by the user.
|
nikcleju@21
|
383
|
nikcleju@21
|
384 #d = length(xinit(:));
|
nikcleju@21
|
385 d = xinit.size
|
nikcleju@21
|
386
|
nikcleju@21
|
387 #if strcmp(class(Omega), 'function_handle')
|
nikcleju@21
|
388 # p = length(Omega(zeros(d,1)));
|
nikcleju@21
|
389 #else ## Omega is a matrix
|
nikcleju@21
|
390 # p = size(Omega, 1);
|
nikcleju@21
|
391 #end
|
nikcleju@21
|
392 if hasattr(Omega, '__call__'):
|
nikcleju@27
|
393 p = Omega(numpy.zeros((d,1)))
|
nikcleju@21
|
394 else:
|
nikcleju@21
|
395 p = Omega.shape[0]
|
nikcleju@21
|
396
|
nikcleju@21
|
397
|
nikcleju@21
|
398 iter = 0
|
nikcleju@21
|
399 lagmult = 1e-4
|
nikcleju@21
|
400 #Lambdahat = 1:p;
|
nikcleju@27
|
401 Lambdahat = numpy.arange(p)
|
nikcleju@21
|
402 #while iter < params.num_iteration
|
nikcleju@21
|
403 while iter < params["num_iteration"]:
|
nikcleju@21
|
404 iter = iter + 1
|
nikcleju@21
|
405 #[xhat, analysis_repr, lagmult] = ArgminOperL2Constrained(y, M, MH, Omega, OmegaH, Lambdahat, xinit, lagmult, params);
|
nikcleju@21
|
406 xhat,analysis_repr,lagmult = ArgminOperL2Constrained(y, M, MH, Omega, OmegaH, Lambdahat, xinit, lagmult, params)
|
nikcleju@21
|
407 #[to_be_removed, maxcoef] = FindRowsToRemove(analysis_repr, params.greedy_level);
|
nikcleju@27
|
408 to_be_removed,maxcoef = FindRowsToRemove(analysis_repr, params["greedy_level"])
|
nikcleju@21
|
409 #disp(['** maxcoef=', num2str(maxcoef), ' target=', num2str(params.stopping_coefficient_size), ' rows_remaining=', num2str(length(Lambdahat)), ' lagmult=', num2str(lagmult)]);
|
nikcleju@21
|
410 #print '** maxcoef=',maxcoef,' target=',params['stopping_coefficient_size'],' rows_remaining=',Lambdahat.size,' lagmult=',lagmult
|
nikcleju@21
|
411 if check_stopping_criteria(xhat, xinit, maxcoef, lagmult, Lambdahat, params):
|
nikcleju@21
|
412 break
|
nikcleju@21
|
413
|
nikcleju@21
|
414 xinit = xhat.copy()
|
nikcleju@21
|
415 #Lambdahat[to_be_removed] = []
|
nikcleju@27
|
416 Lambdahat = numpy.delete(Lambdahat.squeeze(),to_be_removed)
|
nikcleju@21
|
417
|
nikcleju@21
|
418 #n = sqrt(d);
|
nikcleju@21
|
419 #figure(9);
|
nikcleju@21
|
420 #RR = zeros(2*n, n-1);
|
nikcleju@21
|
421 #RR(Lambdahat) = 1;
|
nikcleju@21
|
422 #XD = ones(n, n);
|
nikcleju@21
|
423 #XD(:, 2:end) = XD(:, 2:end) .* RR(1:n, :);
|
nikcleju@21
|
424 #XD(:, 1:(end-1)) = XD(:, 1:(end-1)) .* RR(1:n, :);
|
nikcleju@21
|
425 #XD(2:end, :) = XD(2:end, :) .* RR((n+1):(2*n), :)';
|
nikcleju@21
|
426 #XD(1:(end-1), :) = XD(1:(end-1), :) .* RR((n+1):(2*n), :)';
|
nikcleju@21
|
427 #XD = FD2DiagnosisPlot(n, Lambdahat);
|
nikcleju@21
|
428 #imshow(XD);
|
nikcleju@21
|
429 #figure(10);
|
nikcleju@21
|
430 #imshow(reshape(real(xhat), n, n));
|
nikcleju@21
|
431
|
nikcleju@21
|
432 #return;
|
nikcleju@27
|
433 return xhat,Lambdahat
|
nikcleju@21
|
434
|
nikcleju@21
|
435 def FindRowsToRemove(analysis_repr, greedy_level):
|
nikcleju@21
|
436 #function [to_be_removed, maxcoef] = FindRowsToRemove(analysis_repr, greedy_level)
|
nikcleju@21
|
437
|
nikcleju@21
|
438 #abscoef = abs(analysis_repr(:));
|
nikcleju@27
|
439 abscoef = numpy.abs(analysis_repr)
|
nikcleju@21
|
440 #n = length(abscoef);
|
nikcleju@21
|
441 n = abscoef.size
|
nikcleju@21
|
442 #maxcoef = max(abscoef);
|
nikcleju@21
|
443 maxcoef = abscoef.max()
|
nikcleju@21
|
444 if greedy_level >= 1:
|
nikcleju@21
|
445 #qq = quantile(abscoef, 1.0-greedy_level/n);
|
nikcleju@21
|
446 qq = sp.stats.mstats.mquantile(abscoef, 1.0-greedy_level/n, 0.5, 0.5)
|
nikcleju@21
|
447 else:
|
nikcleju@21
|
448 qq = maxcoef*greedy_level
|
nikcleju@21
|
449
|
nikcleju@21
|
450 #to_be_removed = find(abscoef >= qq);
|
nikcleju@27
|
451 # [0] needed because nonzero() returns a tuple of arrays!
|
nikcleju@27
|
452 to_be_removed = numpy.nonzero(abscoef >= qq)[0]
|
nikcleju@21
|
453 #return;
|
nikcleju@27
|
454 return to_be_removed,maxcoef
|
nikcleju@21
|
455
|
nikcleju@21
|
456 def check_stopping_criteria(xhat, xinit, maxcoef, lagmult, Lambdahat, params):
|
nikcleju@21
|
457 #function r = check_stopping_criteria(xhat, xinit, maxcoef, lagmult, Lambdahat, params)
|
nikcleju@21
|
458
|
nikcleju@21
|
459 #if isfield(params, 'stopping_coefficient_size') && maxcoef < params.stopping_coefficient_size
|
nikcleju@21
|
460 if ('stopping_coefficient_size' in params) and maxcoef < params['stopping_coefficient_size']:
|
nikcleju@21
|
461 return 1
|
nikcleju@21
|
462
|
nikcleju@21
|
463 #if isfield(params, 'stopping_lagrange_multiplier_size') && lagmult > params.stopping_lagrange_multiplier_size
|
nikcleju@21
|
464 if ('stopping_lagrange_multiplier_size' in params) and lagmult > params['stopping_lagrange_multiplier_size']:
|
nikcleju@21
|
465 return 1
|
nikcleju@21
|
466
|
nikcleju@21
|
467 #if isfield(params, 'stopping_relative_solution_change') && norm(xhat-xinit)/norm(xhat) < params.stopping_relative_solution_change
|
nikcleju@27
|
468 if ('stopping_relative_solution_change' in params) and numpy.linalg.norm(xhat-xinit)/numpy.linalg.norm(xhat) < params['stopping_relative_solution_change']:
|
nikcleju@21
|
469 return 1
|
nikcleju@21
|
470
|
nikcleju@21
|
471 #if isfield(params, 'stopping_cosparsity') && length(Lambdahat) < params.stopping_cosparsity
|
nikcleju@21
|
472 if ('stopping_cosparsity' in params) and Lambdahat.size() < params['stopping_cosparsity']:
|
nikcleju@21
|
473 return 1
|
nikcleju@21
|
474
|
nikcleju@21
|
475 return 0 |