<s> import numpy <EOL> np = numpy <EOL> from . . core . parameterization import Param <EOL> from GPy . core . model import Model <EOL> from . . util . block_matrices import get_blocks , get_block_shapes , unblock , get_blocks_3d , get_block_shapes_3d <EOL> def get_shape ( x ) : <EOL> if isinstance ( x , numpy . ndarray ) : <EOL> return x . shape <EOL> return ( ) <EOL> def at_least_one_element ( x ) : <EOL> if isinstance ( x , ( list , tuple ) ) : <EOL> return x <EOL> return [ x ] <EOL> def flatten_if_needed ( x ) : <EOL> return numpy . atleast_1d ( x ) . flatten ( ) <EOL> class GradientChecker ( Model ) : <EOL> def __init__ ( self , f , df , x0 , names = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( GradientChecker , self ) . __init__ ( name = '<STR_LIT>' ) <EOL> if isinstance ( x0 , ( list , tuple ) ) and names is None : <EOL> self . shapes = [ get_shape ( xi ) for xi in x0 ] <EOL> self . names = [ '<STR_LIT>' . format ( i = i ) for i in range ( len ( x0 ) ) ] <EOL> elif isinstance ( x0 , ( list , tuple ) ) and names is not None : <EOL> self . shapes = [ get_shape ( xi ) for xi in x0 ] <EOL> self . names = names <EOL> elif names is None : <EOL> self . names = [ '<STR_LIT:X>' ] <EOL> self . shapes = [ get_shape ( x0 ) ] <EOL> else : <EOL> self . names = names <EOL> self . shapes = [ get_shape ( x0 ) ] <EOL> for name , xi in zip ( self . names , at_least_one_element ( x0 ) ) : <EOL> self . __setattr__ ( name , Param ( name , xi ) ) <EOL> self . link_parameter ( self . __getattribute__ ( name ) ) <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . f = f <EOL> self . df = df <EOL> def _get_x ( self ) : <EOL> if len ( self . names ) > <NUM_LIT:1> : <EOL> return [ self . __getattribute__ ( name ) for name in self . names ] + list ( self . args ) <EOL> return [ self . __getattribute__ ( self . names [ <NUM_LIT:0> ] ) ] + list ( self . args ) <EOL> def log_likelihood ( self ) : <EOL> return float ( numpy . sum ( self . f ( * self . _get_x ( ) , ** self . kwargs ) ) ) <EOL> def _log_likelihood_gradients ( self ) : <EOL> return numpy . atleast_1d ( self . df ( * self . _get_x ( ) , ** self . kwargs ) ) . flatten ( ) <EOL> class HessianChecker ( GradientChecker ) : <EOL> def __init__ ( self , f , df , ddf , x0 , names = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( HessianChecker , self ) . __init__ ( df , ddf , x0 , names = names , * args , ** kwargs ) <EOL> self . _f = f <EOL> self . _df = df <EOL> self . _ddf = ddf <EOL> def checkgrad ( self , target_param = None , verbose = False , step = <NUM_LIT> , tolerance = <NUM_LIT> , block_indices = None , plot = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import numdifftools as nd <EOL> except : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> if target_param : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> current_index = <NUM_LIT:0> <EOL> for name , shape in zip ( self . names , self . shapes ) : <EOL> current_size = numpy . prod ( shape ) <EOL> x = self . optimizer_array . copy ( ) <EOL> x = x [ current_index : current_index + current_size ] . reshape ( shape ) <EOL> analytic_hess = self . _ddf ( x ) <EOL> if analytic_hess . shape [ <NUM_LIT:1> ] == <NUM_LIT:1> : <EOL> analytic_hess = numpy . diagflat ( analytic_hess ) <EOL> numeric_hess_partial = nd . Jacobian ( self . _df , vectorized = False ) <EOL> numeric_hess = numeric_hess_partial ( x ) <EOL> check_passed = self . checkgrad_block ( analytic_hess , numeric_hess , verbose = verbose , step = step , tolerance = tolerance , block_indices = block_indices , plot = plot ) <EOL> current_index += current_size <EOL> return check_passed <EOL> def checkgrad_block ( self , analytic_hess , numeric_hess , verbose = False , step = <NUM_LIT> , tolerance = <NUM_LIT> , block_indices = None , plot = False ) : <EOL> """<STR_LIT>""" <EOL> if analytic_hess . dtype is np . dtype ( '<STR_LIT:object>' ) : <EOL> real_size = get_block_shapes ( analytic_hess ) <EOL> num_elements = np . sum ( real_size ) <EOL> if ( num_elements , num_elements ) == numeric_hess . shape : <EOL> numeric_hess = get_blocks ( numeric_hess , real_size ) <EOL> else : <EOL> tmp_numeric_hess = get_blocks ( np . zeros ( ( num_elements , num_elements ) ) , real_size ) <EOL> tmp_numeric_hess [ block_indices ] = numeric_hess . copy ( ) <EOL> numeric_hess = tmp_numeric_hess <EOL> if block_indices is not None : <EOL> analytic_hess = analytic_hess [ block_indices ] <EOL> numeric_hess = numeric_hess [ block_indices ] <EOL> else : <EOL> if analytic_hess . dtype is np . dtype ( '<STR_LIT:object>' ) : <EOL> analytic_hess = unblock ( analytic_hess ) <EOL> numeric_hess = unblock ( numeric_hess ) <EOL> ratio = numeric_hess / ( numpy . where ( analytic_hess == <NUM_LIT:0> , <NUM_LIT> , analytic_hess ) ) <EOL> difference = numpy . abs ( analytic_hess - numeric_hess ) <EOL> check_passed = numpy . all ( ( numpy . abs ( <NUM_LIT:1> - ratio ) ) < tolerance ) or numpy . allclose ( numeric_hess , analytic_hess , atol = tolerance ) <EOL> if verbose : <EOL> if block_indices : <EOL> print ( "<STR_LIT>" . format ( block_indices ) ) <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> header = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> header_string = map ( lambda x : '<STR_LIT>' . join ( header ) , [ header ] ) <EOL> separator = '<STR_LIT:->' * len ( header_string [ <NUM_LIT:0> ] ) <EOL> print ( '<STR_LIT:\n>' . join ( [ header_string [ <NUM_LIT:0> ] , separator ] ) ) <EOL> min_r = '<STR_LIT>' % float ( numpy . min ( ratio ) ) <EOL> max_r = '<STR_LIT>' % float ( numpy . max ( ratio ) ) <EOL> max_d = '<STR_LIT>' % float ( numpy . max ( difference ) ) <EOL> min_d = '<STR_LIT>' % float ( numpy . min ( difference ) ) <EOL> cols = [ max_r , min_r , min_d , max_d ] <EOL> if check_passed : <EOL> checked = "<STR_LIT>" <EOL> else : <EOL> checked = "<STR_LIT>" <EOL> grad_string = "<STR_LIT>" . format ( checked , cols [ <NUM_LIT:0> ] , cols [ <NUM_LIT:1> ] , cols [ <NUM_LIT:2> ] , cols [ <NUM_LIT:3> ] ) <EOL> print ( grad_string ) <EOL> if plot : <EOL> from matplotlib import pyplot as pb <EOL> fig , axes = pb . subplots ( <NUM_LIT:2> , <NUM_LIT:2> ) <EOL> max_lim = numpy . max ( numpy . vstack ( ( analytic_hess , numeric_hess ) ) ) <EOL> min_lim = numpy . min ( numpy . vstack ( ( analytic_hess , numeric_hess ) ) ) <EOL> msa = axes [ <NUM_LIT:0> , <NUM_LIT:0> ] . matshow ( analytic_hess , vmin = min_lim , vmax = max_lim ) <EOL> axes [ <NUM_LIT:0> , <NUM_LIT:0> ] . set_title ( '<STR_LIT>' ) <EOL> axes [ <NUM_LIT:0> , <NUM_LIT:0> ] . xaxis . set_ticklabels ( [ None ] ) <EOL> axes [ <NUM_LIT:0> , <NUM_LIT:0> ] . yaxis . set_ticklabels ( [ None ] ) <EOL> axes [ <NUM_LIT:0> , <NUM_LIT:0> ] . xaxis . set_ticks ( [ None ] ) <EOL> axes [ <NUM_LIT:0> , <NUM_LIT:0> ] . yaxis . set_ticks ( [ None ] ) <EOL> msn = axes [ <NUM_LIT:0> , <NUM_LIT:1> ] . matshow ( numeric_hess , vmin = min_lim , vmax = max_lim ) <EOL> pb . colorbar ( msn , ax = axes [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> axes [ <NUM_LIT:0> , <NUM_LIT:1> ] . set_title ( '<STR_LIT>' ) <EOL> axes [ <NUM_LIT:0> , <NUM_LIT:1> ] . xaxis . set_ticklabels ( [ None ] ) <EOL> axes [ <NUM_LIT:0> , <NUM_LIT:1> ] . yaxis . set_ticklabels ( [ None ] ) <EOL> axes [ <NUM_LIT:0> , <NUM_LIT:1> ] . xaxis . set_ticks ( [ None ] ) <EOL> axes [ <NUM_LIT:0> , <NUM_LIT:1> ] . yaxis . set_ticks ( [ None ] ) <EOL> msr = axes [ <NUM_LIT:1> , <NUM_LIT:0> ] . matshow ( ratio ) <EOL> pb . colorbar ( msr , ax = axes [ <NUM_LIT:1> , <NUM_LIT:0> ] ) <EOL> axes [ <NUM_LIT:1> , <NUM_LIT:0> ] . set_title ( '<STR_LIT>' ) <EOL> axes [ <NUM_LIT:1> , <NUM_LIT:0> ] . xaxis . set_ticklabels ( [ None ] ) <EOL> axes [ <NUM_LIT:1> , <NUM_LIT:0> ] . yaxis . set_ticklabels ( [ None ] ) <EOL> axes [ <NUM_LIT:1> , <NUM_LIT:0> ] . xaxis . set_ticks ( [ None ] ) <EOL> axes [ <NUM_LIT:1> , <NUM_LIT:0> ] . yaxis . set_ticks ( [ None ] ) <EOL> msd = axes [ <NUM_LIT:1> , <NUM_LIT:1> ] . matshow ( difference ) <EOL> pb . colorbar ( msd , ax = axes [ <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> axes [ <NUM_LIT:1> , <NUM_LIT:1> ] . set_title ( '<STR_LIT>' ) <EOL> axes [ <NUM_LIT:1> , <NUM_LIT:1> ] . xaxis . set_ticklabels ( [ None ] ) <EOL> axes [ <NUM_LIT:1> , <NUM_LIT:1> ] . yaxis . set_ticklabels ( [ None ] ) <EOL> axes [ <NUM_LIT:1> , <NUM_LIT:1> ] . xaxis . set_ticks ( [ None ] ) <EOL> axes [ <NUM_LIT:1> , <NUM_LIT:1> ] . yaxis . set_ticks ( [ None ] ) <EOL> if block_indices : <EOL> fig . suptitle ( "<STR_LIT>" . format ( block_indices ) ) <EOL> pb . show ( ) <EOL> return check_passed <EOL> class SkewChecker ( HessianChecker ) : <EOL> def __init__ ( self , df , ddf , dddf , x0 , names = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( SkewChecker , self ) . __init__ ( df , ddf , dddf , x0 , names = names , * args , ** kwargs ) <EOL> def checkgrad ( self , target_param = None , verbose = False , step = <NUM_LIT> , tolerance = <NUM_LIT> , block_indices = None , plot = False , super_plot = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> import numdifftools as nd <EOL> except : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> if target_param : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> current_index = <NUM_LIT:0> <EOL> for name , n_shape in zip ( self . names , self . shapes ) : <EOL> current_size = numpy . prod ( n_shape ) <EOL> x = self . optimizer_array . copy ( ) <EOL> x = x [ current_index : current_index + current_size ] . reshape ( n_shape ) <EOL> analytic_hess = self . _ddf ( x ) <EOL> numeric_hess_partial = nd . Jacobian ( self . _df , vectorized = True ) <EOL> numeric_hess = numeric_hess_partial ( x ) <EOL> print ( "<STR_LIT>" ) <EOL> if analytic_hess . dtype is np . dtype ( '<STR_LIT:object>' ) : <EOL> blocksizes , pagesizes = get_block_shapes_3d ( analytic_hess ) <EOL> real_block_size = np . sum ( blocksizes ) <EOL> numeric_hess = numeric_hess . reshape ( real_block_size , real_block_size , pagesizes ) <EOL> else : <EOL> numeric_hess = numeric_hess . reshape ( * analytic_hess . shape ) <EOL> check_passed = [ False ] * numeric_hess . shape [ <NUM_LIT:2> ] <EOL> for block_ind in range ( numeric_hess . shape [ <NUM_LIT:2> ] ) : <EOL> p = True if ( plot and block_ind == numeric_hess . shape [ <NUM_LIT:2> ] - <NUM_LIT:1> ) or super_plot else False <EOL> if verbose : <EOL> print ( "<STR_LIT>" . format ( block_ind ) ) <EOL> check_passed [ block_ind ] = self . checkgrad_block ( analytic_hess [ : , : , block_ind ] , numeric_hess [ : , : , block_ind ] , verbose = verbose , step = step , tolerance = tolerance , block_indices = block_indices , plot = p ) <EOL> current_index += current_size <EOL> return np . all ( check_passed ) </s>
