@@ -159,7 +159,7 @@ def _initialize_nmf(X, n_components, variant=None, eps=1e-6,
159
159
return W , H
160
160
161
161
162
- def _nls_subproblem (V , W , H_init , tol , max_iter , sigma = 0.01 , beta = 0.1 ):
162
+ def _nls_subproblem (V , W , H , tol , max_iter , sigma = 0.01 , beta = 0.1 ):
163
163
"""Non-negative least square solver
164
164
165
165
Solves a non-negative least squares subproblem using the
@@ -171,7 +171,7 @@ def _nls_subproblem(V, W, H_init, tol, max_iter, sigma=0.01, beta=0.1):
171
171
V, W : array-like
172
172
Constant matrices.
173
173
174
- H_init : array-like
174
+ H : array-like
175
175
Initial guess for the solution.
176
176
177
177
tol : float
@@ -214,11 +214,7 @@ def _nls_subproblem(V, W, H_init, tol, max_iter, sigma=0.01, beta=0.1):
214
214
http://www.csie.ntu.edu.tw/~cjlin/nmf/
215
215
216
216
"""
217
- if (H_init < 0 ).any ():
218
- raise ValueError ("Negative values in H_init passed to NLS solver." )
219
-
220
- H = H_init
221
- WtV = safe_sparse_dot (W .T , V , dense_output = True )
217
+ WtV = safe_sparse_dot (W .T , V )
222
218
WtW = np .dot (W .T , W )
223
219
224
220
# values justified in the paper
@@ -228,13 +224,12 @@ def _nls_subproblem(V, W, H_init, tol, max_iter, sigma=0.01, beta=0.1):
228
224
229
225
# The following multiplication with a boolean array is more than twice
230
226
# as fast as indexing into grad.
231
- proj_gradient = norm (grad * np .logical_or (grad < 0 , H > 0 ))
232
- if proj_gradient < tol :
227
+ if norm (grad * np .logical_or (grad < 0 , H > 0 )) < tol :
233
228
break
234
229
235
230
Hp = H
236
231
237
- for inner_iter in range (1 , 20 ):
232
+ for inner_iter in range (19 ):
238
233
# Gradient step.
239
234
Hn = H - alpha * grad
240
235
# Projection step.
@@ -243,7 +238,7 @@ def _nls_subproblem(V, W, H_init, tol, max_iter, sigma=0.01, beta=0.1):
243
238
gradd = np .dot (grad .ravel (), d .ravel ())
244
239
dQd = np .dot (np .dot (WtW , d ).ravel (), d .ravel ())
245
240
suff_decr = (1 - sigma ) * gradd + 0.5 * dQd < 0
246
- if inner_iter == 1 :
241
+ if inner_iter == 0 :
247
242
decr_alpha = not suff_decr
248
243
249
244
if decr_alpha :
@@ -581,6 +576,8 @@ def transform(self, X):
581
576
"""
582
577
X , = check_arrays (X , sparse_format = 'csc' )
583
578
Wt = np .zeros ((self .n_components_ , X .shape [0 ]))
579
+ check_non_negative (X , "ProjectedGradientNMF.transform" )
580
+
584
581
if sp .issparse (X ):
585
582
Wt , _ , _ = _nls_subproblem (X .T , self .components_ .T , Wt ,
586
583
tol = self .tol ,
0 commit comments