def get_multi_fidelity_history_str(history):
    """ Prints the history of the multifidelity optimisation. """
    fidel_dim = len(history.query_fidels[0])
    print_fidels = np.array(
        [get_rounded_list(qf) for qf in history.query_fidels]).T
    # First the function values
    val_title_str = ', '.join(
        ['fidel(dim=%d)' % (fidel_dim), 'val', 'curr_true_opt_val'])
    val_title_str = val_title_str + '\n' + '-' * len(val_title_str)
    print_vals = get_rounded_list(history.query_vals)
    print_curr_opt_vals = get_rounded_list(history.curr_true_opt_vals)
    val_history_mat_str_T = np.vstack(
        (print_fidels, print_vals, print_curr_opt_vals))
    val_history_mat_list = [
        get_list_of_floats_as_str(row) for row in val_history_mat_str_T.T
    ]
    val_history_mat_str = '\n'.join(val_history_mat_list)
    # NOw the fidels
    fidel_title_str = ', '.join([
        'fidel(%d)' % (fidel_dim), 'eval_times', 'fidel_costs',
        'cum(fidel_costs)', 'receive_times'
    ])
    fidel_title_str = fidel_title_str + '\n' + '-' * len(fidel_title_str)
    fidel_history_mat_str_T = np.vstack(
        (print_fidels, history.query_eval_times, history.query_cost_at_fidels,
         np.cumsum(history.query_cost_at_fidels), history.query_receive_times))
    fidel_history_mat_list = [
        get_list_of_floats_as_str(row) for row in fidel_history_mat_str_T.T
    ]
    fidel_history_mat_str = '\n'.join(fidel_history_mat_list)
    return val_title_str + '\n' + val_history_mat_str + '\n\n' + \
           fidel_title_str + '\n' + fidel_history_mat_str + '\n'
Ejemplo n.º 2
0
 def _get_scales_str(self):
     """ Returns a description of the alphas. """
     if 'alphas' in self.kernel.hyperparams.keys():
         return 'alphas=' + get_list_of_floats_as_str(
             self.kernel.hyperparams['alphas'])
     else:
         return 'scale=' + str(round(self.kernel.hyperparams['scale'], 4))
Ejemplo n.º 3
0
 def _test_dist_comp_for_multiple_coeffs(self, nns, dist_types,
                                         mislabel_coeffs, struct_coeffs,
                                         tp_comp):
     """ Tests distance computation for a single connectivity coefficient. """
     num_nns = len(nns)
     for i in range(num_nns):
         for j in range(i, num_nns):
             nn_i = nns[i]
             nn_j = nns[j]
             dists = {}
             for dt in dist_types:
                 dists[dt] = tp_comp(nn_i,
                                     nn_j,
                                     dist_type=dt,
                                     mislabel_coeffs=mislabel_coeffs,
                                     struct_coeffs=struct_coeffs)
                 num_dists = len(dt.split('-')) * len(mislabel_coeffs)
                 assert len(dists[dt]) == num_dists
             res_str = ' '.join([
                 '%s=%s' % (self._get_dist_type_abbr(key),
                            get_list_of_floats_as_str(val))
                 for key, val in six.iteritems(dists)
             ])
             self.report('(i,j)=(%d,%d) %s' % (i, j, res_str),
                         'test_result')
Ejemplo n.º 4
0
def return_kernel_hyperparams_as_str(kernel_hyperparams):
    """ Returns the kernel hyper-params as a string. """
    if 'scale' in kernel_hyperparams:
        scale_str = 'scale=%0.5f' % (kernel_hyperparams['scale'])
    else:
        scale_str = 'alphas=%s' % (get_list_of_floats_as_str(
            kernel_hyperparams['alphas'], 5))
    betas_str = 'betas=' + str(kernel_hyperparams['betas'])
    return scale_str + ', ' + betas_str
Ejemplo n.º 5
0
 def _test_variation_along_fidel_dim(self,
                                     func_caller,
                                     unnorm_func_caller,
                                     fidel_dim,
                                     test_point=None):
     """ Tests and prints variation along the a fidelity dim. """
     _grid_size = 10
     _rem_coords_pre = np.ones((_grid_size, fidel_dim))
     _fidel_dim_coords = np.reshape(np.linspace(0, 1, _grid_size),
                                    (_grid_size, 1))
     _rem_coords_post = np.ones(
         (_grid_size, func_caller.fidel_space.dim - fidel_dim - 1))
     fidel_test_grid = np.hstack(
         (_rem_coords_pre, _fidel_dim_coords, _rem_coords_post))
     raw_fidel_test_grid = func_caller.get_raw_fidel_coords(fidel_test_grid)
     test_point = test_point if test_point is not None else \
                  np.random.random((func_caller.domain.dim,))
     raw_test_point = func_caller.get_raw_domain_coords(test_point)
     test_vals_at_grid = [
         func_caller.eval_at_fidel_single(fidel, test_point)[0]
         for fidel in fidel_test_grid
     ]
     ufc_test_vals_at_grid = [
         unnorm_func_caller.eval_at_fidel_single(raw_fidel,
                                                 raw_test_point)[0]
         for raw_fidel in raw_fidel_test_grid
     ]
     test_vals_at_grid_str = get_list_of_floats_as_str(test_vals_at_grid)
     ufc_test_vals_at_grid_str = get_list_of_floats_as_str(
         ufc_test_vals_at_grid)
     test_point_str = get_list_of_floats_as_str(test_point)
     raw_test_point_str = get_list_of_floats_as_str(raw_test_point)
     self.report(
         'fidel values (normalised func_caller) at x=%s: (fidel_dim=%d) %s'
         % (test_point_str, fidel_dim, test_vals_at_grid_str),
         'test_result')
     self.report(
         'fidel values (unnorm_func_caller) at x=%s:  (fidel_dim=%d) %s' %
         (raw_test_point_str, fidel_dim, ufc_test_vals_at_grid_str),
         'test_result')
Ejemplo n.º 6
0
 def _get_kernel_str(cls, kern):
     """ Gets a string format of the kernel depending on whether it is SE/Poly. """
     if isinstance(kern, gp_kernel.ExpDecayKernel):
         ret = 'expd: offs=%0.3f, pow=%s' % (
             kern.hyperparams['offset'],
             get_list_of_floats_as_str(kern.hyperparams['powers']))
     elif isinstance(kern, gp_kernel.SEKernel) or isinstance(
             kern, gp_kernel.MaternKernel):
         hp_name = 'dim_bandwidths'
         kern_name = 'se' if isinstance(kern, gp_kernel.SEKernel) else \
                     'matern(%0.1f)'%(kern.hyperparams['nu'])
         if kern.dim > 4:
             ret = '%0.4f(avg)' % (kern.hyperparams[hp_name].mean())
         else:
             ret = get_list_of_floats_as_str(kern.hyperparams[hp_name])
         ret = kern_name + ': ' + ret
     elif isinstance(kern, gp_kernel.PolyKernel):
         ret = 'poly: %s' % (get_list_of_floats_as_str(
             kern.hyperparams['dim_scalings']))
     else:  # Return an empty string.
         ret = str(kern)
     return ret
 def _test_optimiser_results(self, opt_val, opt_point, history):
     """ Tests optimiser results. """
     assert opt_val == history.curr_opt_vals[-1]
     assert opt_point.shape[0] == self.func_caller.domain.dim
     assert is_nondecreasing(history.curr_opt_vals)
     assert is_nondecreasing(history.curr_true_opt_vals)
     self.report('True opt val sequence: %s.' %
                 (get_list_of_floats_as_str(history.curr_true_opt_vals)))
     saved_in_history = [
         key for key, _ in list(history.__dict__.items())
         if not key.startswith('__')
     ]
     self.report('Stored in history: %s.' % (saved_in_history),
                 'test_result')
Ejemplo n.º 8
0
 def _get_kernel_str(cls, kern):
   """ Gets a string format of the kernel depending on whether it is SE/Poly."""
   if isinstance(kern, SEKernel):
     hp_name = 'dim_bandwidths'
     kern_name = 'se'
   elif isinstance(kern, PolyKernel):
     hp_name = 'dim_scalings'
     kern_name = 'poly'
   if kern.dim > 4:
     ret = '%0.4f(avg)'%(kern.hyperparams[hp_name].mean())
   else:
     ret = get_list_of_floats_as_str(kern.hyperparams[hp_name])
   ret = kern_name + '-' + ret
   return ret
Ejemplo n.º 9
0
 def test_instantiation_and_computation(self):
     """ Testing instantiation. """
     self.report(
         'Testing instantiation of DistSumNNKernelTestCase and computation '
         + 'for specific networks.')
     dist_type_vals = ['lp', 'emd', 'lp-emd']
     all_kernels = []
     for dist_type in dist_type_vals:
         if dist_type == 'lp':
             betas = self.lp_betas
             scales = [1]
         elif dist_type == 'emd':
             betas = self.emd_betas
             scales = [1]
         else:
             betas = [
                 j for i in zip(self.lp_betas, self.emd_betas) for j in i
             ]
             scales = [1, 1]
         tp_kernel = nn_comparators.generate_otmann_kernel_from_params(
             'sum',
             self.all_layer_labels,
             self.label_mismatch_penalty,
             self.non_assignment_penalty,
             nn_comparators.MLP_STRUCTURAL_PENALTY_GROUPS,
             nn_comparators.PATH_LENGTH_TYPES,
             self.mislabel_coeffs,
             self.struct_coeffs,
             betas,
             scales,
             dist_type=dist_type)
         nn_K = tp_kernel(self.mlps)
         nn_eig_vals, _ = np.linalg.eig(nn_K)
         self.report(
             'dist-type: %s, eigvals: %s.' %
             (dist_type, get_list_of_floats_as_str(sorted(nn_eig_vals))))
         assert nn_K.shape == (len(self.mlps), len(self.mlps))
         self.report(
             '%s transport kernel:\n%s' %
             (dist_type, str(np.round(nn_K, 3))), 'test_result')
         assert np.all(np.diag(nn_K) == sum(scales))
         all_kernels.append(nn_K)
     # Check if it is in fact the sum
     if 'lp' in dist_type_vals and 'emd' in dist_type_vals and 'lp-emd' in dist_type_vals:
         lp_kernel = all_kernels[dist_type_vals.index('lp')]
         emd_kernel = all_kernels[dist_type_vals.index('emd')]
         lpemd_kernel = all_kernels[dist_type_vals.index('lp-emd')]
         assert np.linalg.norm(lpemd_kernel - lp_kernel - emd_kernel) < _TOL
Ejemplo n.º 10
0
 def _test_dist_comp_for_single_conn_coeff(self, nns, dist_types, tp_comp):
     """ Tests distance computation for a single connectivity coefficient. """
     num_nns = len(nns)
     for i in range(num_nns):
         for j in range(i, num_nns):
             nn_i = nns[i]
             nn_j = nns[j]
             dists = {}
             for dt in dist_types:
                 dists[dt] = tp_comp(nn_i, nn_j, dist_type=dt)
             res_str = ' '.join([
                 '%s=%s' % (self._get_dist_type_abbr(key),
                            get_list_of_floats_as_str(val))
                 for key, val in dists.items()
             ])
             self.report('(i,j)=(%d,%d) %s' % (i, j, res_str),
                         'test_result')
Ejemplo n.º 11
0
 def _get_kernel_str(cls, kern):
     """ Gets a string format of the kernel depending on whether it is SE/Poly."""
     if isinstance(kern, gp_kernel.AdditiveKernel):
         return str(kern)
     if isinstance(kern, gp_kernel.SEKernel) or isinstance(
             kern, gp_kernel.MaternKernel):
         hp_name = 'dim_bandwidths'
         kern_name = 'se' if isinstance(kern, gp_kernel.SEKernel) else \
           'matern(%0.1f)' % (kern.hyperparams['nu'])
     elif isinstance(kern, gp_kernel.PolyKernel):
         hp_name = 'dim_scalings'
         kern_name = 'poly'
     else:  # Return an empty string.
         return ''
     if kern.dim > 6:
         ret = '%0.4f(avg)' % (kern.hyperparams[hp_name].mean())
     else:
         ret = get_list_of_floats_as_str(kern.hyperparams[hp_name])
     ret = kern_name + '-' + ret
     return ret
Ejemplo n.º 12
0
 def _child_str(self):
     """ Description of the child GP. """
     scales_str = self._get_scales_str()
     betas_str = 'betas=' + get_list_of_floats_as_str(
         self.kernel.hyperparams['betas'])
     return self.kernel.dist_type + ':: ' + scales_str + ' ' + betas_str
Ejemplo n.º 13
0
 def __str__(self):
     """ Return a string representation. """
     return 'Hamming: wts=%s' % (get_list_of_floats_as_str(
         self.hyperparams['dim_weights']))
Ejemplo n.º 14
0
 def __str__(self):
     """ Return a string representation. """
     return 'ExpDec: sc=%0.3f, offset=%0.3f, pow=%s'%(self.hyperparams['scale'], \
         self.hyperparams['offset'], get_list_of_floats_as_str(self.hyperparams['powers']))