def checkgrad(self, target_param=None, verbose=False, step=1e-6, tolerance=1e-3, block_indices=None, plot=False, super_plot=False):
        """
        Gradient checker that just checks each hessian individually

        super_plot will plot the hessian wrt every parameter, plot will just do the first one
        """
        try:
            import numdifftools as nd
        except:
            raise ImportError("Don't have numdifftools package installed, it is not a GPy dependency as of yet, it is only used for hessian tests")

        if target_param:
            raise NotImplementedError('Only basic functionality is provided with this gradchecker')

        #Repeat for each parameter, not the nicest but shouldn't be many cases where there are many
        #variables
        current_index = 0
        for name, n_shape in zip(self.names, self.shapes):
            current_size = numpy.prod(n_shape)
            x = self.optimizer_array.copy()
            #x = self._get_params_transformed().copy()
            x = x[current_index:current_index + current_size].reshape(n_shape)

            # Check gradients
            #Actually the third derivative
            analytic_hess = self._ddf(x)

            #Can only calculate jacobian for one variable at a time
            #From the docs:
            #x0 : vector location
            #at which to differentiate fun
            #If x0 is an N x M array, then fun is assumed to be a function
            #of N*M variables., thus we must have it flat, not (N,1), but just (N,)
            #numeric_hess_partial = nd.Hessian(self._f, vectorized=False)
            #Actually _df is already the hessian
            numeric_hess_partial = nd.Jacobian(self._df, vectorized=True)
            numeric_hess = numeric_hess_partial(x)

            print("Done making numerical hessian")
            if analytic_hess.dtype is np.dtype('object'):
                #Blockify numeric_hess aswell
                blocksizes, pagesizes = get_block_shapes_3d(analytic_hess)
                #HACK
                real_block_size = np.sum(blocksizes)
                numeric_hess = numeric_hess.reshape(real_block_size, real_block_size, pagesizes)
                #numeric_hess = get_blocks_3d(numeric_hess, blocksizes)#, pagesizes)
            else:
                numeric_hess = numeric_hess.reshape(*analytic_hess.shape)

            #Check every block individually (for ease)
            check_passed = [False]*numeric_hess.shape[2]
            for block_ind in range(numeric_hess.shape[2]):
                #Unless super_plot is set, just plot the first one
                p = True if (plot and block_ind == numeric_hess.shape[2]-1) or super_plot else False
                if verbose:
                    print("Checking derivative of hessian wrt parameter number {}".format(block_ind))
                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)

            current_index += current_size
        return np.all(check_passed)
def checkgrad ( self , target_param = None , verbose = False , step = 1e-6 , tolerance = 1e-3 , block_indices = None , plot = False , super_plot = False ) : try : import numdifftools as nd except : raise ImportError ( "Don't have numdifftools package installed, it is not a GPy dependency as of yet, it is only used for hessian tests" ) if target_param : raise NotImplementedError ( 'Only basic functionality is provided with this gradchecker' ) current_index = 0 for name , n_shape in zip ( self . names , self . shapes ) : current_size = numpy . prod ( n_shape ) x = self . optimizer_array . copy ( ) x = x [ current_index : current_index + current_size ] . reshape ( n_shape ) analytic_hess = self . _ddf ( x ) numeric_hess_partial = nd . Jacobian ( self . _df , vectorized = True ) numeric_hess = numeric_hess_partial ( x ) print ( "Done making numerical hessian" ) if analytic_hess . dtype is np . dtype ( 'object' ) : blocksizes , pagesizes = get_block_shapes_3d ( analytic_hess ) real_block_size = np . sum ( blocksizes ) numeric_hess = numeric_hess . reshape ( real_block_size , real_block_size , pagesizes ) else : numeric_hess = numeric_hess . reshape ( * analytic_hess . shape ) check_passed = [ False ] * numeric_hess . shape [ 2 ] for block_ind in range ( numeric_hess . shape [ 2 ] ) : p = True if ( plot and block_ind == numeric_hess . shape [ 2 ] - 1 ) or super_plot else False if verbose : print ( "Checking derivative of hessian wrt parameter number {}" . format ( block_ind ) ) 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 ) current_index += current_size return np . all ( check_passed )
Gradient checker that just checks each hessian individually

        super_plot will plot the hessian wrt every parameter, plot will just do the first one