#NEWLINE# def sag( X , y , step_size , alpha , n_iter = 1 , dloss = None , sparse = False , sample_weight = None , fit_intercept = True ) : #NEWLINE# #INDENT# ( n_samples , n_features ) = ( X . shape [ 0 ] , X . shape [ 1 ] ) #NEWLINE# weights = np . zeros ( X . shape [ 1 ] ) #NEWLINE# sum_gradient = np . zeros ( X . shape [ 1 ] ) #NEWLINE# gradient_memory = np . zeros ( ( n_samples , step_size ) ) #NEWLINE# intercept = 0.0 #NEWLINE# intercept_sum_gradient = 0.0 #NEWLINE# intercept_gradient_memory = np . zeros ( n_samples ) #NEWLINE# rng = np . random . RandomState ( 77 ) #NEWLINE# decay = 1.0 #NEWLINE# seen = set ( ) #NEWLINE# if sparse : #NEWLINE# #INDENT# decay = 0.01 #NEWLINE# #UNINDENT# for epoch in range ( n_iter ) : #NEWLINE# #INDENT# for k in range ( n_samples ) : #NEWLINE# #INDENT# idx = int ( ( rng . rand ( 1 ) * n_samples ) ) #NEWLINE# entry = X [ idx ] #NEWLINE# seen . add ( idx ) #NEWLINE# p = ( np . dot ( entry , weights ) + intercept ) #NEWLINE# gradient = dloss ( p , y [ idx ] ) #NEWLINE# if ( sample_weight is not None ) : #NEWLINE# #INDENT# gradient *= sample_weight [ idx ] #NEWLINE# #UNINDENT# update = ( ( entry * gradient ) + ( alpha * weights ) ) #NEWLINE# sum_gradient += ( update - gradient_memory [ idx ] ) #NEWLINE# gradient_memory [ idx ] = update #NEWLINE# if fit_intercept : #NEWLINE# #INDENT# intercept_sum_gradient += ( gradient - intercept_gradient_memory [ idx ] ) #NEWLINE# intercept_gradient_memory [ idx ] = gradient #NEWLINE# intercept -= ( ( ( step_size * intercept_sum_gradient ) / len ( seen ) ) * decay ) #NEWLINE# #UNINDENT# weights -= ( ( step_size * sum_gradient ) / len ( seen ) ) #NEWLINE# #UNINDENT# #UNINDENT# return ( weights , intercept )