22
22
23
23
24
24
def _sparse_encode (X , dictionary , gram = None , cov = None , algorithm = 'lasso_lars' ,
25
- n_nonzero_coefs = None , alpha = None , copy_gram = True ,
26
- copy_cov = True , init = None , max_iter = 1000 ):
25
+ n_nonzero_coefs = None , alpha = None , copy_cov = True ,
26
+ init = None , max_iter = 1000 ):
27
27
"""Generic sparse coding
28
28
29
29
Each column of the result is the solution to a Lasso problem.
@@ -74,10 +74,6 @@ def _sparse_encode(X, dictionary, gram=None, cov=None, algorithm='lasso_lars',
74
74
max_iter: int, 1000 by default
75
75
Maximum number of iterations to perform if `algorithm='lasso_cd'`.
76
76
77
- copy_gram: boolean, optional
78
- Whether to copy the precomputed Gram matrix; if False, it may be
79
- overwritten.
80
-
81
77
copy_cov: boolean, optional
82
78
Whether to copy the precomputed covariance matrix; if False, it may be
83
79
overwritten.
@@ -135,7 +131,7 @@ def _sparse_encode(X, dictionary, gram=None, cov=None, algorithm='lasso_lars',
135
131
alpha /= n_features # account for scaling
136
132
new_code = np .empty ((n_samples , n_atoms ))
137
133
clf = Lasso (alpha = alpha , fit_intercept = False , precompute = gram ,
138
- max_iter = 1000 )
134
+ max_iter = max_iter )
139
135
for k in xrange (n_samples ):
140
136
# A huge amount of time is spent in this loop. It needs to be
141
137
# tight
@@ -180,7 +176,7 @@ def _sparse_encode(X, dictionary, gram=None, cov=None, algorithm='lasso_lars',
180
176
181
177
182
178
def sparse_encode (X , dictionary , gram = None , cov = None , algorithm = 'lasso_lars' ,
183
- n_nonzero_coefs = None , alpha = None , copy_gram = True ,
179
+ n_nonzero_coefs = None , alpha = None , copy_gram = None ,
184
180
copy_cov = True , init = None , max_iter = 1000 , n_jobs = 1 ):
185
181
"""Sparse coding
186
182
@@ -236,10 +232,6 @@ def sparse_encode(X, dictionary, gram=None, cov=None, algorithm='lasso_lars',
236
232
max_iter: int, 1000 by default
237
233
Maximum number of iterations to perform if `algorithm='lasso_cd'`.
238
234
239
- copy_gram: boolean, optional
240
- Whether to copy the precomputed Gram matrix; if False, it may be
241
- overwritten.
242
-
243
235
copy_cov: boolean, optional
244
236
Whether to copy the precomputed covariance matrix; if False, it may be
245
237
overwritten.
@@ -262,29 +254,34 @@ def sparse_encode(X, dictionary, gram=None, cov=None, algorithm='lasso_lars',
262
254
warnings .warn ("Please note: the interface of sparse_encode has changed: "
263
255
"It now follows the dictionary learning API and it also "
264
256
"handles parallelization. Please read the docstring for "
265
- "more information." )
257
+ "more information." , stacklevel = 2 )
258
+ if copy_gram is not None :
259
+ warnings .warn ("copy_gram in sparse_encode is deprecated: it"
260
+ "lead to errors." , DeprecationWarning , stacklevel = 2 )
266
261
dictionary = np .asarray (dictionary )
267
262
X = np .asarray (X )
268
263
n_samples , n_features = X .shape
269
264
n_atoms = dictionary .shape [0 ]
270
265
if gram is None :
271
- copy_gram = False
272
266
gram = np .dot (dictionary , dictionary .T )
273
267
if cov is None and algorithm != 'lasso_cd' :
274
268
copy_cov = False
275
269
cov = np .dot (dictionary , X .T )
276
270
if n_jobs == 1 or algorithm == 'threshold' :
277
- return _sparse_encode (X , dictionary , gram , cov , algorithm ,
278
- n_nonzero_coefs , alpha , copy_gram , copy_cov , init )
271
+ return _sparse_encode (X , dictionary , gram = gram , cov = cov ,
272
+ algorithm = algorithm , n_nonzero_coefs = n_nonzero_coefs ,
273
+ alpha = alpha , copy_cov = copy_cov ,
274
+ init = init , max_iter = max_iter )
279
275
code = np .empty ((n_samples , n_atoms ))
280
276
slices = list (gen_even_slices (n_samples , n_jobs ))
281
277
code_views = Parallel (n_jobs = n_jobs )(
282
278
delayed (sparse_encode )(X [this_slice ], dictionary , gram ,
283
279
cov [:, this_slice ], algorithm ,
284
280
n_nonzero_coefs , alpha ,
285
- copy_gram , copy_cov ,
281
+ copy_cov = copy_cov ,
286
282
init = init [this_slice ] if init is not
287
- None else None )
283
+ None else None ,
284
+ max_iter = max_iter )
288
285
for this_slice in slices )
289
286
for this_slice , this_view in zip (slices , code_views ):
290
287
code [this_slice ] = this_view
0 commit comments