#NEWLINE# def _nls_subproblem( V , W , H , tol , max_iter , alpha = 0.0 , l1_ratio = 0.0 , sigma = 0.01 , beta = 0.1 ) : #NEWLINE# #INDENT# 'Non-negative least square solver\n\n    Solves a non-negative least squares subproblem using the projected\n    gradient descent algorithm.\n\n    Parameters\n    ----------\n    V : array-like, shape (n_samples, n_features)\n        Constant matrix.\n\n    W : array-like, shape (n_samples, n_components)\n        Constant matrix.\n\n    H : array-like, shape (n_components, n_features)\n        Initial guess for the solution.\n\n    tol : float\n        Tolerance of the stopping condition.\n\n    max_iter : int\n        Maximum number of iterations before timing out.\n\n    alpha : double, default: 0.\n        Constant that multiplies the regularization terms. Set it to zero to\n        have no regularization.\n\n    l1_ratio : double, default: 0.\n        The regularization mixing parameter, with 0 <= l1_ratio <= 1.\n        For l1_ratio = 0 the penalty is an L2 penalty.\n        For l1_ratio = 1 it is an L1 penalty.\n        For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2.\n\n    sigma : float\n        Constant used in the sufficient decrease condition checked by the line\n        search.  Smaller values lead to a looser sufficient decrease condition,\n        thus reducing the time taken by the line search, but potentially\n        increasing the number of iterations of the projected gradient\n        procedure. 0.01 is a commonly used value in the optimization\n        literature.\n\n    beta : float\n        Factor by which the step size is decreased (resp. increased) until\n        (resp. as long as) the sufficient decrease condition is satisfied.\n        Larger values allow to find a better step size but lead to longer line\n        search. 0.1 is a commonly used value in the optimization literature.\n\n    Returns\n    -------\n    H : array-like, shape (n_components, n_features)\n        Solution to the non-negative least squares problem.\n\n    grad : array-like, shape (n_components, n_features)\n        The gradient.\n\n    n_iter : int\n        The number of iterations done by the algorithm.\n\n    References\n    ----------\n    C.-J. Lin. Projected gradient methods for non-negative matrix\n    factorization. Neural Computation, 19(2007), 2756-2779.\n    http://www.csie.ntu.edu.tw/~cjlin/nmf/\n    ' #NEWLINE# WtV = safe_sparse_dot ( W . T , V ) #NEWLINE# WtW = fast_dot ( W . T , W ) #NEWLINE# gamma = 1 #NEWLINE# for n_iter in range ( 1 , ( max_iter + 1 ) ) : #NEWLINE# #INDENT# grad = ( np . dot ( WtW , H ) - WtV ) #NEWLINE# if ( ( alpha > 0 ) and ( l1_ratio == 1.0 ) ) : #NEWLINE# #INDENT# grad += alpha #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( alpha > 0 ) : #NEWLINE# #INDENT# grad += ( alpha * ( l1_ratio + ( ( 1 - l1_ratio ) * H ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# if ( norm ( ( grad * np . logical_or ( ( grad < 0 ) , ( H > 0 ) ) ) ) < tol ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# Hp = H #NEWLINE# for inner_iter in range ( 20 ) : #NEWLINE# #INDENT# Hn = ( H - ( gamma * grad ) ) #NEWLINE# Hn *= ( Hn > 0 ) #NEWLINE# d = ( Hn - H ) #NEWLINE# gradd = np . dot ( grad . ravel ( ) , d . ravel ( ) ) #NEWLINE# dQd = np . dot ( np . dot ( WtW , d ) . ravel ( ) , d . ravel ( ) ) #NEWLINE# suff_decr = ( ( ( ( 1 - sigma ) * gradd ) + ( 0.5 * dQd ) ) < 0 ) #NEWLINE# if ( inner_iter == 0 ) : #NEWLINE# #INDENT# decr_gamma = ( not suff_decr ) #NEWLINE# #UNINDENT# if decr_gamma : #NEWLINE# #INDENT# if suff_decr : #NEWLINE# #INDENT# H = Hn #NEWLINE# break #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# gamma *= beta #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( not suff_decr ) or ( Hp == Hn ) . all ( ) ) : #NEWLINE# #INDENT# H = Hp #NEWLINE# break #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# gamma /= beta #NEWLINE# Hp = Hn #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( n_iter == max_iter ) : #NEWLINE# #INDENT# warnings . warn ( 'Iteration limit reached in nls subproblem.' ) #NEWLINE# #UNINDENT# return ( H , grad , n_iter )