Ejemplo n.º 1
0
def get_placeholder_values(
        sess,
        criterion,
        crit_params,
        required_placeholders,
        intermediate_tensors,
        ls,
        sigmas,
        sigma0s,
        X_np,
        Y_np,
        candidate_xs,
        init_random,
        init_npoint,
        init_seed=None,
        previous_opt_meanf_maximizer=None,  # (1,xdim)
        iteration=1,
        dtype=tf.float32,
        is_debug_mode=False):

    xdim = crit_params['xdim']
    nmax = crit_params['nmax']
    ntrain = crit_params['ntrain']
    nfeature = crit_params['nfeature']
    xmin = crit_params['xmin']
    xmax = crit_params['xmax']

    values = {'iteration': iteration, 'query_x': None}

    if 'max_observed_y' in intermediate_tensors:
        max_observed_y_np = sess.run(
            intermediate_tensors['max_observed_y'],
            feed_dict={required_placeholders['Y']: Y_np})

        values['max_observed_y'] = max_observed_y_np

    if 'invKs' in intermediate_tensors:
        invKs_np = sess.run(intermediate_tensors['invKs'],
                            feed_dict={required_placeholders['X']: X_np})
        values['invKs'] = invKs_np

    if previous_opt_meanf_maximizer is None:
        previous_opt_meanf_maximizer = functions.get_initializers(
            xdim, xmin, xmax, name="random", random=True, npoint=1, seed=None)

    # Optimize for marginal_bes_mpt guess
    if candidate_xs['opt_meanf'] is None:
        opt_meanf_candidate_xs_np = functions.get_initializers(
            xdim,
            xmin,
            xmax,
            name="opt_meanf",
            random=init_random,
            npoint=init_npoint,
            seed=init_seed)

        opt_meanf_candidate_xs_np = np.concatenate(
            [opt_meanf_candidate_xs_np, previous_opt_meanf_maximizer], axis=0)
    else:
        opt_meanf_candidate_xs_np = candidate_xs['opt_meanf']

    sess.run(intermediate_tensors['opt_meanf_assign'],
             feed_dict={
                 required_placeholders['opt_meanf_candidate_xs']:
                 opt_meanf_candidate_xs_np,
                 required_placeholders['X']: X_np,
                 required_placeholders['Y']: Y_np,
                 required_placeholders['invKs']: values['invKs']
             })

    for _ in range(crit_params['ntrain']):
        sess.run(intermediate_tensors['opt_meanf_train'],
                 feed_dict={
                     required_placeholders['X']: X_np,
                     required_placeholders['Y']: Y_np,
                     required_placeholders['invKs']: values['invKs']
                 })

    opt_meanf_maximizer_np, opt_meanf_maximum_np \
        = sess.run([
                intermediate_tensors['opt_meanf_maximizer'],
                intermediate_tensors['opt_meanf_maximum'] ],
            feed_dict = {
                required_placeholders['X']: X_np,
                required_placeholders['Y']: Y_np,
                required_placeholders['invKs']: values['invKs']
            })

    values['opt_meanf_maximizer'] = opt_meanf_maximizer_np
    values['opt_meanf_maximum'] = opt_meanf_maximum_np

    if 'opt_fsample_maximizer' in required_placeholders or 'opt_fsample_maxima' in required_placeholders:

        if use_GPU_for_sample_functions:
            # sample functions
            # move the computation of thetas, Ws, bs
            # to numpy instead of tensorflow
            # to avoid numerical error (return None of tf.linalg.eigh)
            print("use GPU to sample functions.")
            thetas_np, Ws_np, bs_np = sess.run([
                intermediate_tensors['thetas'], intermediate_tensors['Ws'],
                intermediate_tensors['bs']
            ],
                                               feed_dict={
                                                   required_placeholders['X']:
                                                   X_np,
                                                   required_placeholders['Y']:
                                                   Y_np
                                               })

        else:
            print("use CPU to sample functions.")
            thetas_np = np.zeros([nhyp, nmax, nfeature, 1])
            Ws_np = np.zeros([nhyp, nmax, nfeature, xdim])
            bs_np = np.zeros([nhyp, nmax, nfeature, 1])

            for hyp_idx in range(nhyp):
                thetas_np[hyp_idx,...], Ws_np[hyp_idx,...], bs_np[hyp_idx,...] \
                    = optfunc.draw_random_init_weights_features_np(
                        xdim, nmax, nfeature,
                        X_np, Y_np,
                        ls[hyp_idx], sigmas[hyp_idx], sigma0s[hyp_idx])

        # optimize functions
        # assign initial values

        if candidate_xs['opt_fsample'] is None:
            opt_fsample_candidate_xs_np = functions.get_initializers(
                xdim,
                xmin,
                xmax,
                name="opt_fsample",
                random=init_random,
                npoint=init_npoint,
                seed=init_seed + 123)

            opt_fsample_candidate_xs_np = np.concatenate(
                [opt_fsample_candidate_xs_np, opt_meanf_maximizer_np], axis=0)
        else:
            opt_fsample_candidate_xs_np = candidate_xs['opt_fsample']

        sess.run(intermediate_tensors['opt_fsample_assigns'],
                 feed_dict={
                     required_placeholders['thetas']:
                     thetas_np,
                     required_placeholders['Ws']:
                     Ws_np,
                     required_placeholders['bs']:
                     bs_np,
                     required_placeholders['opt_fsample_candidate_xs']:
                     opt_fsample_candidate_xs_np
                 })

        for xx in range(ntrain):
            sess.run(intermediate_tensors['opt_fsample_trains'],
                     feed_dict={
                         required_placeholders['thetas']: thetas_np,
                         required_placeholders['Ws']: Ws_np,
                         required_placeholders['bs']: bs_np
                     })

        opt_fsample_maximizers_np, opt_fsample_maxima_np \
            = sess.run([
                    intermediate_tensors['opt_fsample_maximizers'],
                    intermediate_tensors['opt_fsample_maxima'] ],
                feed_dict = {
                        required_placeholders['thetas']: thetas_np,
                        required_placeholders['Ws']: Ws_np,
                        required_placeholders['bs']: bs_np
                })

        values['opt_fsample_maximizers'] = opt_fsample_maximizers_np
        values['opt_fsample_maxima'] = opt_fsample_maxima_np

    # optimize acquisition function
    if criterion in ['pes']:
        print("Implement for known GP hyperparameter only!")

        unique_opt_fsample_maximizers_np = utils.remove_duplicates_np(
            opt_fsample_maximizers_np[0, ...], resolution=duplicate_resolution)

        if unique_opt_fsample_maximizers_np.shape[0] == 1 and criterion in [
                'ftl', 'sftl'
        ]:
            values['query_x'] = unique_opt_fsample_maximizers_np
            return values

        if criterion == 'pes':
            invKmaxsams_np = sess.run(
                intermediate_tensors['invKmaxsams'],
                feed_dict={
                    required_placeholders['X']:
                    X_np,
                    required_placeholders['Y']:
                    Y_np,
                    required_placeholders['opt_fsample_maximizers']:
                    opt_fsample_maximizers_np
                })
            values['invKmaxsams'] = invKmaxsams_np

    return values
Ejemplo n.º 2
0
                else:

                    feed_dict = {
                        required_placeholders['X']: Xsamples_np,
                        required_placeholders['Y']: Ysamples_np - mean_f_const
                    }

                    for key in required_placeholder_keys[criterion]:
                        if key not in ['X', 'Y']:
                            feed_dict[required_placeholders[
                                key]] = placeholder_values[key]

                    rand_xs_np = functions.get_initializers(xdim,
                                                            xmin,
                                                            xmax,
                                                            name="opt_crit",
                                                            random=True,
                                                            npoint=50,
                                                            seed=seed + nr +
                                                            nq)

                    if candidate_xs['opt_crit'] is None:
                        if criterion in ['avg_bes_mp', 'pes']:
                            print(
                                "Init optimize crit with fsample_maximizers, opt_meanf_maximizer and meanf_maximizer and 50 random inputs"
                            )

                            opt_crit_candidate_xs_np = np.concatenate([
                                placeholder_values['opt_fsample_maximizers'][
                                    0, ...],
                                placeholder_values['opt_meanf_maximizer']
                            ],
Ejemplo n.º 3
0
def get_placeholder_values(
        sess,
        criterion,
        crit_params,
        required_placeholders,
        intermediate_tensors,
        ls,
        sigmas,
        sigma0s,
        X_np,
        Y_np,
        candidate_xs,
        # for computing log loss
        validation_xs,  # (m,xdim) np array
        validation_fs,  # (m,1) np array
        level,  # scalar
        # # #
    init_random,
        init_npoint,
        init_seed=None,
        previous_opt_meanf_maximizer=None,  # (1,xdim)
        iteration=1,
        dtype=tf.float32,
        is_debug_mode=False):

    xdim = crit_params['xdim']
    nmax = crit_params['nmax']
    ntrain = crit_params['ntrain']
    nfeature = crit_params['nfeature']
    xmin = crit_params['xmin']
    xmax = crit_params['xmax']

    values = {'iteration': iteration, 'query_x': None}

    if 'max_observed_y' in intermediate_tensors:
        max_observed_y_np = sess.run(
            intermediate_tensors['max_observed_y'],
            feed_dict={required_placeholders['Y']: Y_np})

        values['max_observed_y'] = max_observed_y_np

    if 'invKs' in intermediate_tensors:
        invKs_np = sess.run(intermediate_tensors['invKs'],
                            feed_dict={required_placeholders['X']: X_np})
        values['invKs'] = invKs_np

    if previous_opt_meanf_maximizer is None:
        previous_opt_meanf_maximizer = functions.get_initializers(
            xdim, xmin, xmax, name="random", random=True, npoint=1, seed=None)

    # Optimize for marginal_bes_mpt guess
    if candidate_xs['opt_meanf'] is None:
        opt_meanf_candidate_xs_np = functions.get_initializers(
            xdim,
            xmin,
            xmax,
            name="opt_meanf",
            random=init_random,
            npoint=init_npoint,
            seed=init_seed)

        opt_meanf_candidate_xs_np = np.concatenate(
            [opt_meanf_candidate_xs_np, previous_opt_meanf_maximizer], axis=0)
    else:
        opt_meanf_candidate_xs_np = candidate_xs['opt_meanf']

    sess.run(intermediate_tensors['opt_meanf_assign'],
             feed_dict={
                 required_placeholders['opt_meanf_candidate_xs']:
                 opt_meanf_candidate_xs_np,
                 required_placeholders['X']: X_np,
                 required_placeholders['Y']: Y_np,
                 required_placeholders['invKs']: values['invKs']
             })

    for _ in range(crit_params['ntrain']):
        sess.run(intermediate_tensors['opt_meanf_train'],
                 feed_dict={
                     required_placeholders['X']: X_np,
                     required_placeholders['Y']: Y_np,
                     required_placeholders['invKs']: values['invKs']
                 })

    opt_meanf_maximizer_np, opt_meanf_maximum_np \
        = sess.run([
                intermediate_tensors['opt_meanf_maximizer'],
                intermediate_tensors['opt_meanf_maximum'] ],
            feed_dict = {
                required_placeholders['X']: X_np,
                required_placeholders['Y']: Y_np,
                required_placeholders['invKs']: values['invKs']
            })

    values['opt_meanf_maximizer'] = opt_meanf_maximizer_np
    values['opt_meanf_maximum'] = opt_meanf_maximum_np

    if 'opt_fsample_maximizer' in required_placeholders or 'opt_fsample_maxima' in required_placeholders:

        if use_GPU_for_sample_functions:
            # sample functions
            # move the computation of thetas, Ws, bs
            # to numpy instead of tensorflow
            # to avoid numerical error (return None of tf.linalg.eigh)
            print("use GPU to sample functions.")
            thetas_np, Ws_np, bs_np = sess.run([
                intermediate_tensors['thetas'], intermediate_tensors['Ws'],
                intermediate_tensors['bs']
            ],
                                               feed_dict={
                                                   required_placeholders['X']:
                                                   X_np,
                                                   required_placeholders['Y']:
                                                   Y_np
                                               })

        else:
            print("use CPU to sample functions.")
            thetas_np = np.zeros([nhyp, nmax, nfeature, 1])
            Ws_np = np.zeros([nhyp, nmax, nfeature, xdim])
            bs_np = np.zeros([nhyp, nmax, nfeature, 1])

            for hyp_idx in range(nhyp):
                thetas_np[hyp_idx,...], Ws_np[hyp_idx,...], bs_np[hyp_idx,...] \
                    = optfunc.draw_random_init_weights_features_np(
                        xdim, nmax, nfeature,
                        X_np, Y_np,
                        ls[hyp_idx], sigmas[hyp_idx], sigma0s[hyp_idx])

        # optimize functions
        # assign initial values

        if candidate_xs['opt_fsample'] is None:
            opt_fsample_candidate_xs_np = functions.get_initializers(
                xdim,
                xmin,
                xmax,
                name="opt_fsample",
                random=init_random,
                npoint=init_npoint,
                seed=init_seed + 123)

            opt_fsample_candidate_xs_np = np.concatenate(
                [opt_fsample_candidate_xs_np, opt_meanf_maximizer_np], axis=0)
        else:
            opt_fsample_candidate_xs_np = candidate_xs['opt_fsample']

        sess.run(intermediate_tensors['opt_fsample_assigns'],
                 feed_dict={
                     required_placeholders['thetas']:
                     thetas_np,
                     required_placeholders['Ws']:
                     Ws_np,
                     required_placeholders['bs']:
                     bs_np,
                     required_placeholders['opt_fsample_candidate_xs']:
                     opt_fsample_candidate_xs_np
                 })

        for xx in range(ntrain):
            sess.run(intermediate_tensors['opt_fsample_trains'],
                     feed_dict={
                         required_placeholders['thetas']: thetas_np,
                         required_placeholders['Ws']: Ws_np,
                         required_placeholders['bs']: bs_np
                     })

        opt_fsample_maximizers_np, opt_fsample_maxima_np \
            = sess.run([
                    intermediate_tensors['opt_fsample_maximizers'],
                    intermediate_tensors['opt_fsample_maxima'] ],
                feed_dict = {
                        required_placeholders['thetas']: thetas_np,
                        required_placeholders['Ws']: Ws_np,
                        required_placeholders['bs']: bs_np
                })

        values['opt_fsample_maximizers'] = opt_fsample_maximizers_np
        values['opt_fsample_maxima'] = opt_fsample_maxima_np

    # evaluating the log loss performance metric
    """
        we don't know the true level, use the max mean as an estimate of the max-value
        then predict the level
    """
    estimated_levels = (values['opt_fsample_maxima'][0, :] - alpha).reshape(
        1, -1)
    # (1,nmax)

    # - E_x log E_{estimated_level} p(correct_label(x) | estimated_level)
    meanf_val, stdf_val = sess.run(
        [intermediate_tensors['meanf'], intermediate_tensors['stdf']],
        feed_dict={
            required_placeholders['X']: X_np,
            required_placeholders['Y']: Y_np,
            required_placeholders['validation_X']: validation_xs
        })

    sign = (level - validation_fs) / np.abs(validation_fs - level)

    sign = sign.reshape(-1, 1)  # (n,1)
    meanf_val = meanf_val.reshape(-1, 1)  # (n,1)
    stdf_val = stdf_val.reshape(-1, 1)  # (n,1)

    logloss = -np.mean(
        sp.misc.logsumexp(  # expectation over estimated_levels
            spst.norm.logcdf(sign *
                             (estimated_levels - meanf_val) / stdf_val) -
            np.log(estimated_levels.shape[1]),
            axis=1))

    values['logloss'] = logloss

    return values
Ejemplo n.º 4
0
xdim = f_info['xdim']

validation_random = False
validation_npoints = 7001

if xdim == 2:
    validation_random = False
    validation_npoints = 81
elif xdim >= 3:
    validation_random = True
    validation_npoints = 7001

validation_xs = functions.get_initializers(xdim,
                                           xmin,
                                           xmax,
                                           name="validation",
                                           random=validation_random,
                                           npoint=validation_npoints,
                                           seed=123)
validation_fs = f(validation_xs)

true_l = f_info['RBF.lengthscale']
true_sigma = f_info['RBF.variance']
true_sigma0 = f_info['noise.variance']
true_sigma0 = args.noisevar
print("Override true_sigma0 = {}".format(true_sigma0))

ls_np = true_l.reshape(-1, xdim).astype(nptype)
sigmas_np = np.array([true_sigma], dtype=nptype)
sigma0s_np = np.array([true_sigma0], dtype=nptype)
Ejemplo n.º 5
0
print("____________________________________________")

validation_random = False
validation_npoints = 7001

if xdim == 2:
    validation_random = False
    validation_npoints = 81
elif xdim >= 3:
    validation_random = True
    validation_npoints = 7001

validation_xs = functions.get_initializers(xdim,
                                           xmin,
                                           xmax,
                                           name="validation",
                                           random=validation_random,
                                           npoint=validation_npoints,
                                           seed=123)
validation_fs = f(validation_xs)
true_level = f_info['maximum'] - alpha

ls_toload = tf.get_variable(dtype=dtype, shape=(nhyp, xdim), name='ls')
sigmas_toload = tf.get_variable(dtype=dtype, shape=(nhyp, ), name='sigmas')
sigma0s_toload = tf.get_variable(dtype=dtype, shape=(nhyp, ), name='sigma0s')

crit_params = {
    'nhyp': nhyp,
    'xdim': xdim,
    'nmax': nmax,
    'nfeature': nfeature,