#NEWLINE# def estimate_u_multiple_tries( sumLogPi = None , nDoc = 0 , gamma = 1.0 , alpha0 = 1.0 , initu = None , initU = None , approx_grad = False , fList = [ 10000000.0 , 100000000.0 , 10000000000.0 ] , **kwargs ) : #NEWLINE# #INDENT# ' Estimate 2K-vector "u" via gradient descent,\n        gracefully using multiple restarts with progressively weaker tolerances\n        until one succeeds\n\n      Returns\n      --------\n      u : 2K-vector of positive parameters\n      fofu : scalar value of minimization objective\n      Info : dict\n\n      Raises\n      --------\n      ValueError with FAILURE in message if all restarts fail\n  ' #NEWLINE# K = ( sumLogPi . size - 1 ) #NEWLINE# if ( initU is not None ) : #NEWLINE# #INDENT# initu = initU #NEWLINE# #UNINDENT# if ( ( initu is not None ) and ( not np . allclose ( initu [ ( - K ) : ] , alpha0 ) ) ) : #NEWLINE# #INDENT# uList = [ initu , None ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# uList = [ None ] #NEWLINE# #UNINDENT# nOverflow = 0 #NEWLINE# u = None #NEWLINE# Info = dict ( ) #NEWLINE# msg = '' #NEWLINE# for ( trial , myTuple ) in enumerate ( itertools . product ( uList , fList ) ) : #NEWLINE# #INDENT# ( initu , factr ) = myTuple #NEWLINE# try : #NEWLINE# #INDENT# ( u , fofu , Info ) = estimate_u ( sumLogPi , nDoc , gamma , alpha0 , initu = initu , factr = factr , approx_grad = approx_grad ) #NEWLINE# Info [ 'nRestarts' ] = trial #NEWLINE# Info [ 'factr' ] = factr #NEWLINE# Info [ 'msg' ] = Info [ 'task' ] #NEWLINE# del Info [ 'grad' ] #NEWLINE# del Info [ 'task' ] #NEWLINE# break #NEWLINE# #UNINDENT# except ValueError as err : #NEWLINE# #INDENT# if ( str ( err ) . count ( 'FAILURE' ) == 0 ) : #NEWLINE# #INDENT# raise err #NEWLINE# #UNINDENT# msg = str ( err ) #NEWLINE# if ( str ( err ) . count ( 'overflow' ) > 0 ) : #NEWLINE# #INDENT# nOverflow += 1 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( u is None ) : #NEWLINE# #INDENT# raise ValueError ( ( 'FAILURE! ' + msg ) ) #NEWLINE# #UNINDENT# Info [ 'nOverflow' ] = nOverflow #NEWLINE# return ( u , fofu , Info )