For my project in machine learning supervised, I have to simplify a training-data and I have to use this technique at page 5 of the document.
Pseudocode algorithm
My code (numbers are the steps):
import numpy as np
import math
def GAD(X):
# 1. Initialize
l=0
D=[]
I=[]
R = np.copy(X)
# 2. repeat l=<N (look algorithm)
while l<X.shape[0]:
# 3. Find residual column (l1 to l2 norm ratio)
min_eps = float('inf') # to search min initialize to infinite
j_min = -1
for j in range(R.shape[1]):
norma1 = norma2 = 0
for i in range(R.shape[0]):
norma1 += abs(R[i][j])
norma2 += (R[i][j])**2
norma2 = math.sqrt(norma2)
eps = norma1/norma2 # sparsity index
if min_eps > eps and j not in I: #excludes if already inserted
min_eps=eps
j_min = j
# 4. Set the l-th atom equal to normalized
norma2 = np.sqrt(np.sum(R[:, j_min]**2, axis=0))
atomo = R[:, j_min]/norma2
# 5. Add to the dictionary
if len(D) == 0:
D = np.asarray(atomo)
else:
D = np.vstack((D, atomo.T))
I.append(j_min)
# 6. Compute the new residual
for j in range(R.shape[1]):
R[:, j] = R[:, j]-atomo*(atomo.T*R[:, j])
l = l+1
# 7. Termination (read page 6 of the document)
return D,I
I have some doubts:
- Is it a correct implementation (observe mostly steps 5 and 6 in the pseudocode algorithm)?