#NEWLINE# def optimize( self , inputs ) : #NEWLINE# #INDENT# inputs = tuple ( inputs ) #NEWLINE# try_penalty = np . clip ( self . _penalty , self . _min_penalty , self . _max_penalty ) #NEWLINE# penalty_scale_factor = None #NEWLINE# f_opt = self . _opt_fun [ 'f_opt' ] #NEWLINE# f_penalized_loss = self . _opt_fun [ 'f_penalized_loss' ] #NEWLINE# def gen_f_opt( penalty ) : #NEWLINE# #INDENT# def f( flat_params ) : #NEWLINE# #INDENT# self . _target . set_param_values ( flat_params , trainable = True ) #NEWLINE# return f_opt ( * ( inputs + ( penalty , ) ) ) #NEWLINE# #UNINDENT# return f #NEWLINE# #UNINDENT# cur_params = self . _target . get_param_values ( trainable = True ) . astype ( 'float64' ) #NEWLINE# opt_params = cur_params #NEWLINE# for penalty_itr in range ( self . _max_penalty_itr ) : #NEWLINE# #INDENT# logger . log ( ( 'trying penalty=%.3f...' % try_penalty ) ) #NEWLINE# ( itr_opt_params , _ , _ ) = scipy . optimize . fmin_l_bfgs_b ( func = gen_f_opt ( try_penalty ) , x0 = cur_params , maxiter = self . _max_opt_itr ) #NEWLINE# ( _ , try_loss , try_constraint_val ) = f_penalized_loss ( * ( inputs + ( try_penalty , ) ) ) #NEWLINE# logger . log ( ( 'penalty %f => loss %f, %s %f' % ( try_penalty , try_loss , self . _constraint_name , try_constraint_val ) ) ) #NEWLINE# if ( ( try_constraint_val < self . _max_constraint_val ) or ( ( penalty_itr == ( self . _max_penalty_itr - 1 ) ) and ( opt_params is None ) ) ) : #NEWLINE# #INDENT# opt_params = itr_opt_params #NEWLINE# #UNINDENT# if ( not self . _adapt_penalty ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# if ( ( penalty_scale_factor is None ) or np . isnan ( try_constraint_val ) ) : #NEWLINE# #INDENT# if ( ( try_constraint_val > self . _max_constraint_val ) or np . isnan ( try_constraint_val ) ) : #NEWLINE# #INDENT# penalty_scale_factor = self . _increase_penalty_factor #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# penalty_scale_factor = self . _decrease_penalty_factor #NEWLINE# opt_params = itr_opt_params #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( penalty_scale_factor > 1 ) and ( try_constraint_val <= self . _max_constraint_val ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( penalty_scale_factor < 1 ) and ( try_constraint_val >= self . _max_constraint_val ) ) : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# try_penalty *= penalty_scale_factor #NEWLINE# try_penalty = np . clip ( try_penalty , self . _min_penalty , self . _max_penalty ) #NEWLINE# self . _penalty = try_penalty #NEWLINE# #UNINDENT# self . _target . set_param_values ( opt_params , trainable = True )