#NEWLINE# def construct_loss_function( m , loss , f_scale ) : #NEWLINE# #INDENT# if ( loss == 'linear' ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# if ( not callable ( loss_function ) ) : #NEWLINE# #INDENT# loss = IMPLEMENTED_LOSSES [ loss ] #NEWLINE# rho = np . empty ( ( 3 , m ) ) #NEWLINE# def loss_function( f , cost_only = False ) : #NEWLINE# #INDENT# z = ( ( f / f_scale ) ** 2 ) #NEWLINE# loss ( z , rho , cost_only = cost_only ) #NEWLINE# if cost_only : #NEWLINE# #INDENT# return ( ( 0.5 * ( f_scale ** 2 ) ) * np . sum ( rho [ 0 ] ) ) #NEWLINE# #UNINDENT# rho [ 0 ] *= ( f_scale ** 2 ) #NEWLINE# rho [ 2 ] /= ( f_scale ** 2 ) #NEWLINE# return rho #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# def loss_function( f , cost_only = False ) : #NEWLINE# #INDENT# z = ( ( f / f_scale ) ** 2 ) #NEWLINE# rho = loss ( z ) #NEWLINE# if cost_only : #NEWLINE# #INDENT# return ( ( 0.5 * ( f_scale ** 2 ) ) * np . sum ( rho [ 0 ] ) ) #NEWLINE# #UNINDENT# rho [ 0 ] *= ( f_scale ** 2 ) #NEWLINE# rho [ 2 ] /= ( f_scale ** 2 ) #NEWLINE# return rho #NEWLINE# #UNINDENT# #UNINDENT# return loss_function