Last active
April 29, 2016 17:24
-
-
Save nicofarr/d277fb0c350849e0c3333767b8a1fb2b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.decomposition import DictionaryLearning | |
nfeat = 20 | |
X = np.random.random(nfeat) | |
X = X.reshape(1,-1)# otherwise sklearn complains | |
alpha = 1 | |
n_comp = 4 | |
dictlearn = DictionaryLearning(n_components=n_comp,alpha=alpha,verbose=2) | |
sk_code = dictlearn.fit_transform(X) | |
sk_dictionary = dictlearn.components_ | |
sk_error = dictlearn.error_[-1] | |
my_estim = np.dot(sk_code,sk_dictionary)-X | |
my_residuals = np.linalg.norm(my_estim,2)**2 | |
my_error = 0.5*my_residuals + (alpha)*np.sum(np.abs(sk_code)) | |
print "Error : my estimation : %f" % my_error | |
print "Error : sklearn estimation : %f" % sk_error | |
print "Difference : %f" % np.abs(my_error - sk_error) | |
print "1 / (2*(alpha**2)) = %f" % (1./2*(alpha**2)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now trying over by directly using the low level function - it seems to work better as I directly control the outputs.
`import numpy as np
from sklearn.decomposition import DictionaryLearning
from sklearn.decomposition import dict_learning
nfeat = 10
X = np.random.random(nfeat)
X = X.reshape(nfeat/10,10)# otherwise sklearn complains
alpha = 0.01
n_comp = 8
sk_code,sk_dictionary,sk_allerrors = dict_learning(X,n_comp,alpha,verbose=1)
sk_error = sk_allerrors[-1]
my_estim = np.dot(sk_code,sk_dictionary)-X
my_residuals = np.linalg.norm(my_estim,2)**2
my_error = 0.5_my_residuals + (alpha)_np.sum(np.abs(sk_code))
print "Error : my estimation : %f" % my_error
print "Error : sklearn estimation : %f" % sk_error
print "Difference : %f" % np.abs(my_error - sk_error)`