-
Notifications
You must be signed in to change notification settings - Fork 4
/
Dictionary_learning_v2.py
338 lines (302 loc) · 16.1 KB
/
Dictionary_learning_v2.py
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 05 23:31:51 2017
@author: Rehan Ahmad
Back Tracking Line Search taken from:
http://users.ece.utexas.edu/~cmcaram/EE381V_2012F/Lecture_4_Scribe_Notes.final.pdf
"""
import numpy as np
from sklearn import preprocessing
import matplotlib.pylab as plt
from copy import deepcopy
import time
from omp import omp
from KSVD import KSVD
from FindDistanceBetweenDictionaries import FindDistanceBetweenDictionaries
from DictUpdate03 import DictUpdate03
import pdb
def awgn(x,snr_db):
L = len(x)
Es = np.sum(np.abs(x)**2)/L
snr_lin = 10**(snr_db/10.0)
noise = np.sqrt(Es/snr_lin)*np.random.randn(L)
y = x + noise
return y
if __name__ == "__main__":
tic = time.time()
FlagPGD = True; FlagPGDMom = True; FlagMOD = False; FlagKSVD = True;
FlagRSimCo = True; FlagPSimCo = True; FlagGDBTLS = True; FlagRGDBTLS = True
drows = 16 #20 #16
dcols = 32 #50 #32
ycols = 78 #1500 #78
alpha = 0.005
iterations = 1000
SNR = 20
epochs = 1
sparsity = 4
count_success = np.ndarray((iterations,epochs))
count_success_momen = np.ndarray((iterations,epochs))
count_success_MOD = np.ndarray((iterations,epochs))
count_success_KSVD = np.ndarray((iterations,epochs))
count_success_RSimCo = np.ndarray((iterations,epochs))
count_success_PSimCo = np.ndarray((iterations,epochs))
count_success_GDBTLS = np.ndarray((iterations,epochs))
count_success_RGDBTLS = np.ndarray((iterations,epochs))
e = np.ndarray((iterations,epochs))
e_momen = np.ndarray((iterations,epochs))
e_GDBTLS = np.ndarray((iterations,epochs))
e_MOD = np.ndarray((iterations,epochs))
e_KSVD = np.ndarray((iterations,epochs))
e_RSimCo = np.ndarray((iterations,epochs))
e_PSimCo = np.ndarray((iterations,epochs))
e_RGDBTLS = np.ndarray((iterations,epochs))
for epoch in range(epochs):
alpha = 0.005
# np.random.seed(epoch)
################# make initial dictionary #############################
# Pn=ceil(sqrt(K));
# DCT=zeros(bb,Pn);
# for k=0:1:Pn-1,
# V=cos([0:1:bb-1]'*k*pi/Pn);
# if k>0, V=V-mean(V); end;
# DCT(:,k+1)=V/norm(V);
# end;
# DCT=kron(DCT,DCT);
######################################################################
# Creating dictionary from uniform iid random distribution
# and normalizing atoms by l2-norm
D = np.random.rand(drows,dcols)
D = preprocessing.normalize(D,norm='l2',axis=0)
# Creating data Y by linear combinations of randomly selected
# atoms and iid uniform coefficients
Y = np.ndarray((drows,ycols))
for i in range(ycols):
PermIndx = np.random.permutation(dcols)
Y[:,i] = np.random.rand()*D[:,PermIndx[0]] + \
np.random.rand()*D[:,PermIndx[1]] + \
np.random.rand()*D[:,PermIndx[2]] + \
np.random.rand()*D[:,PermIndx[3]]
# Add awgn noise in data Y
# for i in range(ycols):
# Y[:,i] = awgn(Y[:,i],SNR)
Dhat = np.ndarray((drows,dcols))
Dhat = deepcopy(Y[:,np.random.permutation(ycols)[0:dcols]])
Dhat = preprocessing.normalize(Dhat,norm='l2',axis=0)
Dhat_momen = deepcopy(Dhat)
Dhat_MOD = deepcopy(Dhat)
Dhat_KSVD = deepcopy(Dhat)
Dhat_RSimCo = deepcopy(Dhat)
Dhat_PSimCo = deepcopy(Dhat)
Dhat_GDBTLS = deepcopy(Dhat)
Dhat_RGDBTLS = deepcopy(Dhat)
########################################################
# Applying Projected Gradient Descent without momentum #
########################################################
if(FlagPGD==True):
X = omp(D,Y,sparsity)
for j in range(iterations):
# X = omp(Dhat,Y,sparsity)
# for i in range(dcols):
# R = Y-np.dot(Dhat,X)
# Dhat[:,i] = Dhat[:,i] + alpha*np.dot(R,X[i,:])
Dhat = Dhat + alpha*np.dot(Y-np.dot(Dhat,X),X.T) #Parallel dictionary update...
Dhat = preprocessing.normalize(Dhat,norm='l2',axis=0)
e[j,epoch] = np.linalg.norm(Y-np.dot(Dhat,X),'fro')**2
count = FindDistanceBetweenDictionaries(D,Dhat)
count_success[j,epoch] = count
#####################################################
# Applying Projected Gradient Descent with momentum #
#####################################################
if(FlagPGDMom==True):
v = np.zeros((drows,dcols))
gamma = 0.5
X = omp(D,Y,sparsity)
for j in range(iterations):
# X = omp(Dhat_momen,Y,sparsity)
# for i in range(dcols):
# R = Y-np.dot(Dhat_momen,X)
# v[:,i] = gamma*v[:,i] + alpha*np.dot(R,X[i,:])
# Dhat_momen[:,i] = Dhat_momen[:,i] + v[:,i]
v = gamma*v - alpha*np.dot(Y-np.dot(Dhat_momen,X),X.T)
Dhat_momen = Dhat_momen - v
Dhat_momen = preprocessing.normalize(Dhat_momen,norm='l2',axis=0)
e_momen[j,epoch] = np.linalg.norm(Y-np.dot(Dhat_momen,X),'fro')**2
count_momen = FindDistanceBetweenDictionaries(D,Dhat_momen)
count_success_momen[j,epoch] = count_momen
#####################################################
# Applying Gradient Descent with back tracking line #
# search algorithm #
#####################################################
if(FlagGDBTLS==True):
alpha = 1
beta = np.random.rand()
eta = np.random.rand()*0.5
Grad = np.zeros((drows,dcols))
X = omp(D,Y,sparsity)
for j in range(iterations):
alpha = 1
# X = omp(Dhat_GDBTLS,Y,sparsity)
Dhat_GDtemp = deepcopy(Dhat_GDBTLS)
#################################################################
# Back Tracking line search Algorithm (BTLS) to find optimal #
# value of alpha #
#################################################################
Grad = -np.dot(Y-np.dot(Dhat_GDBTLS,X),X.T)
oldfunc = np.linalg.norm(Y-np.dot(Dhat_GDBTLS,X),'fro')**2
newfunc = np.linalg.norm(Y-np.dot(Dhat_GDtemp,X),'fro')**2
while(~(newfunc <= oldfunc-eta*alpha*np.sum(Grad**2))):
alpha = beta*alpha
Dhat_GDtemp = deepcopy(Dhat_GDBTLS)
Dhat_GDtemp = Dhat_GDtemp + alpha*np.dot(Y-np.dot(Dhat_GDtemp,X),X.T)
Dhat_GDtemp = preprocessing.normalize(Dhat_GDtemp,norm='l2',axis=0)
newfunc = np.linalg.norm(Y-np.dot(Dhat_GDtemp,X),'fro')**2
if(alpha < 1e-9):
break
#################################################################
#################################################################
Dhat_GDBTLS = Dhat_GDBTLS + alpha*np.dot(Y-np.dot(Dhat_GDBTLS,X),X.T)
Dhat_GDBTLS = preprocessing.normalize(Dhat_GDBTLS,norm='l2',axis=0)
e_GDBTLS[j,epoch] = np.linalg.norm(Y-np.dot(Dhat_GDBTLS,X),'fro')**2
count_GDBTLS = FindDistanceBetweenDictionaries(D,Dhat_GDBTLS)
count_success_GDBTLS[j,epoch] = count_GDBTLS
#####################################################
# Applying Gradient Descent with back tracking line #
# search algorithm with regularization on X #
#####################################################
if(FlagRGDBTLS==True):
alpha = 1
mu = 0.01
# beta = np.random.rand()
# eta = np.random.rand()*0.5
# Grad = np.zeros((drows,dcols))
# mu = 0.01
X = omp(D,Y,sparsity)
for j in range(iterations):
alpha = 1
# X = omp(Dhat_RGDBTLS,Y,sparsity)
Dhat_RGDtemp = deepcopy(Dhat_RGDBTLS)
#################################################################
# Back Tracking line search Algorithm (BTLS) to find optimal #
# value of alpha #
#################################################################
Grad = -np.dot(Y-np.dot(Dhat_RGDBTLS,X),X.T)
oldfunc = np.linalg.norm(Y-np.dot(Dhat_RGDBTLS,X),'fro')**2 + mu*np.linalg.norm(X,'fro')**2
newfunc = np.linalg.norm(Y-np.dot(Dhat_RGDtemp,X),'fro')**2 + mu*np.linalg.norm(X,'fro')**2
while(~(newfunc <= oldfunc-eta*alpha*np.sum(Grad**2))):
alpha = beta*alpha
Dhat_RGDtemp = deepcopy(Dhat_RGDBTLS)
Dhat_RGDtemp = Dhat_RGDtemp + alpha*np.dot(Y-np.dot(Dhat_RGDtemp,X),X.T)
Dhat_RGDtemp = preprocessing.normalize(Dhat_RGDtemp,norm='l2',axis=0)
newfunc = np.linalg.norm(Y-np.dot(Dhat_RGDtemp,X),'fro')**2 + mu*np.linalg.norm(X,'fro')**2
if(alpha < 1e-9):
break
#################################################################
#################################################################
Dhat_RGDBTLS = Dhat_RGDBTLS + alpha*np.dot(Y-np.dot(Dhat_RGDBTLS,X),X.T)
Dhat_RGDBTLS = preprocessing.normalize(Dhat_RGDBTLS,norm='l2',axis=0)
########## Update X Considering same sparsity pattern############
Omega = X!=0
ColUpdate = np.sum(Omega,axis=0)!=0
YI = deepcopy(Y[:,ColUpdate])
DI = deepcopy(Dhat_RGDBTLS)
XI = deepcopy(X[:,ColUpdate])
OmegaI = deepcopy(Omega[:,ColUpdate])
OmegaL = np.sum(Omega,axis=0)
mu_sqrt = np.sqrt(mu)
for cn in range(ycols):
L = deepcopy(OmegaL[cn])
X[OmegaI[:,cn],cn] = np.linalg.lstsq(np.append(DI[:,OmegaI[:,cn]],\
np.diag(mu_sqrt*np.ones((L,))),axis=0),\
np.append(YI[:,cn],np.zeros((L,)),axis=0))[0]
#################################################################
e_RGDBTLS[j,epoch] = np.linalg.norm(Y-np.dot(Dhat_RGDBTLS,X),'fro')**2
count_RGDBTLS = FindDistanceBetweenDictionaries(D,Dhat_RGDBTLS)
count_success_RGDBTLS[j,epoch] = count_RGDBTLS
############################################
# Applying MOD Algorithm #
############################################
if(FlagMOD==True):
X = omp(D,Y,sparsity)
for j in range(iterations):
# X = omp(Dhat_MOD,Y,sparsity)
Dhat_MOD = np.dot(Y,np.linalg.pinv(X))
Dhat_MOD = preprocessing.normalize(Dhat_MOD,norm='l2',axis=0)
count_MOD = FindDistanceBetweenDictionaries(D,Dhat_MOD)
count_success_MOD[j,epoch] = count_MOD
e_MOD[j,epoch] = np.linalg.norm(Y-np.dot(Dhat_MOD,X),'fro')**2
############################################
# Applying KSVD Algorithm #
############################################
if(FlagKSVD==True):
X = omp(D,Y,sparsity)
for j in range(iterations):
# X = omp(Dhat_KSVD,Y,sparsity)
Dhat_KSVD,X = KSVD(Y,Dhat_KSVD,X)
count_KSVD = FindDistanceBetweenDictionaries(D,Dhat_KSVD)
count_success_KSVD[j,epoch] = count_KSVD
e_KSVD[j,epoch] = np.linalg.norm(Y-np.dot(Dhat_KSVD,X),'fro')**2
#############################################
# Applying Regularized SimCo Algorithm #
#############################################
if(FlagRSimCo==True):
class IPara():
pass
IPara = IPara()
IPara.I = range(D.shape[1])
IPara.mu = 0.01
IPara.dispN = 20
IPara.DebugFlag = 0
IPara.itN = 1
IPara.gmin = 1e-5; # the minimum value of gradient
IPara.Lmin = 1e-6; # t4-t1 should be larger than Lmin
IPara.t4 = 1e-2; # the initial value of t4
IPara.rNmax = 3; # the number of iterative refinement in Part B in DictLineSearch03.m
X = omp(D,Y,sparsity)
for j in range(iterations):
# X = omp(Dhat_RSimCo,Y,sparsity)
Dhat_RSimCo,X,_ = DictUpdate03(Y,Dhat_RSimCo,X,IPara)
count_RSimCo = FindDistanceBetweenDictionaries(D,Dhat_RSimCo)
count_success_RSimCo[j,epoch] = count_RSimCo
e_RSimCo[j,epoch] = np.linalg.norm(Y-np.dot(Dhat_RSimCo,X),'fro')**2
#############################################
# Applying Primitive SimCo Algorithm #
#############################################
if(FlagPSimCo==True):
IPara.mu = 0
X = omp(D,Y,sparsity)
for j in range(iterations):
# X = omp(Dhat_PSimCo,Y,sparsity)
Dhat_PSimCo,X,_ = DictUpdate03(Y,Dhat_PSimCo,X,IPara)
count_PSimCo = FindDistanceBetweenDictionaries(D,Dhat_PSimCo)
count_success_PSimCo[j,epoch] = count_PSimCo
e_PSimCo[j,epoch] = np.linalg.norm(Y-np.dot(Dhat_PSimCo,X),'fro')**2
#############################################
#############################################
print 'epoch: ',epoch,'completed'
plt.close('all')
if FlagPGD==True: plt.plot(np.sum(count_success,axis=1)/epochs,'b',label = 'PGD')
if FlagPGDMom==True: plt.plot(np.sum(count_success_momen,axis=1)/epochs,'r',label = 'PGD_Momentum')
if FlagMOD==True: plt.plot(np.sum(count_success_MOD,axis=1)/epochs,'g',label = 'MOD')
if FlagKSVD==True: plt.plot(np.sum(count_success_KSVD,axis=1)/epochs,'y',label = 'KSVD')
if FlagRSimCo==True: plt.plot(np.sum(count_success_RSimCo,axis=1)/epochs,'m',label = 'RSimCo')
if FlagPSimCo==True: plt.plot(np.sum(count_success_PSimCo,axis=1)/epochs,'c',label = 'PSimCo')
if FlagGDBTLS==True: plt.plot(np.sum(count_success_GDBTLS,axis=1)/epochs,':',label = 'GDBTLS')
if FlagRGDBTLS==True: plt.plot(np.sum(count_success_RGDBTLS,axis=1)/epochs,'--',label = 'R_GDBTLS')
plt.legend()
plt.xlabel('iteration number')
plt.ylabel('Success Counts in iteration')
plt.title('Dictionary Learning Algorithms applied on Syhthetic data')
plt.figure()
if FlagPGD==True: plt.plot(np.sum(e,axis=1)/epochs,'b',label = 'PGD')
if FlagPGDMom==True: plt.plot(np.sum(e_momen,axis=1)/epochs,'r',label = 'PGD_Momentum')
if FlagMOD==True: plt.plot(np.sum(e_MOD,axis=1)/epochs,'g',label = 'MOD')
if FlagKSVD==True: plt.plot(np.sum(e_KSVD,axis=1)/epochs,'y',label = 'KSVD')
if FlagRSimCo==True: plt.plot(np.sum(e_RSimCo,axis=1)/epochs,'m',label = 'RSimCo')
if FlagPSimCo==True: plt.plot(np.sum(e_PSimCo,axis=1)/epochs,'c',label = 'PSimCo')
if FlagGDBTLS==True: plt.plot(np.sum(e_GDBTLS,axis=1)/epochs,':',label = 'GDBTLS')
if FlagRGDBTLS==True: plt.plot(np.sum(e_RGDBTLS,axis=1)/epochs,'--',label = 'R_GDBTLS')
plt.legend()
plt.xlabel('iteration number')
plt.ylabel('Error: Sum of squares')
toc = time.time()
print 'Total Time Taken by code: ','%.2f' %((toc-tic)/60.0),'min'