def dare(x, ls, sigmas, sigma0s, X, Y, xdim, n_hyp, ymaxs, invKs, dtype=tf.float32): """ X: n x xdim Y: n x 1 ls: nh x xdim sigmas: nh x 1 signal variances sigma0s: nh x 1 noise variances where nh is the number of hyperparameters invKs: nh x n x n ymaxs: nh x n_maxs """ nx = tf.shape(x)[0] nmax = tf.shape(ymaxs)[1] dare_val = tf.constant(0.0, dtype=dtype) for i in range(n_hyp): l = tf.reshape(ls[i, :], shape=(1, xdim)) sigma = sigmas[i] sigma0 = sigma0s[i] invK = invKs[i, ...] ymax = tf.reshape(ymaxs[i, :], (1, nmax)) # (1,nmax) f_mean, f_var = utils.compute_mean_var_f(x, X, Y, l, sigma, sigma0, invK, dtype=dtype) f_mean = tf.reshape(f_mean, (nx, 1)) f_var = tf.reshape(f_var, (nx, 1)) f_std = tf.sqrt(f_var) # (nx,1) dare_i = tf.abs(f_mean - ymax) / f_std # (nx, nmax) dare_i = -tf.reduce_mean(dare_i, axis=1) # (nx,) # negating as we are maximizing instead of minimizing as in DARE paper dare_val = dare_val + tf.squeeze(dare_i) / tf.constant(n_hyp, dtype=dtype) return dare_val
def ei(x, xdim, n_hyp, Xsamples, Ysamples, ls, sigmas, sigma0s, invKs, fmax, dtype=tf.float32): """ X: n x xdim Y: n x 1 ls: nh x xdim sigmas: nh x 1 signal variances sigma0s: nh x 1 noise variances where nh is the number of hyperparameters invKs: nh x n x n fmax could be the maximum samples (observations) so far or the maximum posterior mean """ norm = tfp.distributions.Normal(loc=tf.constant(0., dtype=dtype), scale=tf.constant(1., dtype=dtype)) ei_val = tf.constant(0.0, dtype=dtype) for i in range(n_hyp): l = tf.reshape(ls[i, :], shape=(1, xdim)) sigma = sigmas[i] sigma0 = sigma0s[i] invK = invKs[i, ...] f_mean, f_var = utils.compute_mean_var_f(x, Xsamples, Ysamples, l, sigma, sigma0, invK, dtype=dtype) f_std = tf.sqrt(f_var) # consider the distribution of f, not of y diff = f_mean - fmax pos_diff = tf.clip_by_value(diff, clip_value_min=0.0, clip_value_max=np.infty) standard_diff = diff / f_std ei_val = ei_val + tf.squeeze(pos_diff + f_std * norm.prob(standard_diff) - tf.abs(diff) * norm.cdf(standard_diff)) ei_val = ei_val / tf.constant(n_hyp, dtype=dtype) return ei_val
def straddle(x, ls, sigmas, sigma0s, X, Y, xdim, n_hyp, ymaxs, invKs, dtype=tf.float32): """ X: n x xdim Y: n x 1 ls: nh x xdim sigmas: nh x 1 signal variances sigma0s: nh x 1 noise variances where nh is the number of hyperparameters invKs: nh x n x n ymaxs: nh x n_maxs """ nx = tf.shape(x)[0] nmax = tf.shape(ymaxs)[1] straddle_val = tf.constant(0.0, dtype=dtype) for i in range(n_hyp): l = tf.reshape(ls[i, :], shape=(1, xdim)) sigma = sigmas[i] sigma0 = sigma0s[i] invK = invKs[i, ...] ymax = tf.reshape(ymaxs[i, :], (1, nmax)) # (1,nmax) f_mean, f_var = utils.compute_mean_var_f(x, X, Y, l, sigma, sigma0, invK, dtype=dtype) f_mean = tf.reshape(f_mean, (nx, 1)) f_var = tf.reshape(f_var, (nx, )) straddle_i = 1.96 * f_var - tf.reduce_mean(tf.abs(f_mean - ymax), axis=1) # (nx,) straddle_val = straddle_val + tf.squeeze(straddle_i) / tf.constant( n_hyp, dtype=dtype) return straddle_val
def ucb(x, xdim, n_hyp, Xsamples, Ysamples, ls, sigmas, sigma0s, invKs, iter, dtype=tf.float32, ntestpoint=1000): """ X: n x xdim Y: n x 1 ls: nh x xdim sigmas: nh x 1 signal variances sigma0s: nh x 1 noise variances where nh is the number of hyperparameters invKs: nh x n x n iter: the current iteration number """ ucb_val = tf.constant(0.0, dtype=dtype) u = 3. + tf.log(tf.square(tf.cast(iter, dtype=dtype)) * np.pi**2 / 6.) for i in range(n_hyp): l = tf.reshape(ls[i,:], shape=(1,xdim)) sigma = sigmas[i] sigma0 = sigma0s[i] invK = invKs[i,...] f_mean, f_var = utils.compute_mean_var_f(x, Xsamples, Ysamples, l, sigma, sigma0, invK, dtype=dtype) f_std = tf.sqrt(f_var) noise_mean = 0.0 post_var = f_var + sigma0 ucb = tf.sqrt(tf.cast(2. * (u + np.log(ntestpoint)), dtype=dtype) * post_var) ucb_val = ucb_val + tf.squeeze(f_mean + ucb) ucb_val = ucb_val / tf.constant(n_hyp, dtype=dtype) return ucb_val
def imposeC2(xdim, n_max, Xsamples, Ysamples, xmaxs, max_observed_y, l, sigma, sigma0, invK, dtype=tf.float32): # C2: fmax is larger than past observations (> max_observed_y) # xmaxs: shape(n_max x xdim) # max_observed_y: scalar # l: shape(1,xdim) # sigma, sigma0: scalars max_observed_y = tf.squeeze(max_observed_y) norm = tfp.distributions.Normal(loc=tf.constant(0., dtype=dtype), scale=tf.constant(1., dtype=dtype)) fmax_mean, fmax_var = utils.compute_mean_var_f(xmaxs, Xsamples, Ysamples, l, sigma, sigma0, invK) tmp = tf.sqrt(tf.constant(1.0, dtype=dtype) + fmax_var / sigma0) z = (fmax_mean - max_observed_y) / tf.sqrt(sigma0) / tmp pdf_over_cdf_z = tf.exp(norm.log_prob(z) - norm.log_cdf(z)) mean_fmax_C2 = fmax_mean + fmax_var * pdf_over_cdf_z / (tf.sqrt(sigma0) * tmp) var_fmax_C2 = tf.clip_by_value( fmax_var - fmax_var * fmax_var * pdf_over_cdf_z / (sigma0 + fmax_var) * (z + pdf_over_cdf_z), clip_value_min=1e-9, clip_value_max=np.infty) return mean_fmax_C2, var_fmax_C2, fmax_mean, fmax_var
def mnes(x, ls, sigmas, sigma0s, X, Y, xdim, n_hyp, ymaxs, invKs, nsamples=100, dtype=tf.float32): """ a more efficient formula than evaluate_interval_rmes.py as it uses simplified formula X: n x xdim Y: n x 1 ls: nh x xdim sigmas: nh x 1 signal variances sigma0s: nh x 1 noise variances where nh is the number of hyperparameters invKs: nh x n x n ymaxs: nh x n_maxs x 2 k+2 elements of ymax are to define the k+1 intervals (including -infty->, and ->infty) nbound = k+2 """ nx = tf.shape(x)[0] # nmax = tf.shape(ymaxs)[1] nbound = tf.shape(ymaxs)[2] # excluding -infty and infty normal_dist = tfd.Normal(loc=tf.zeros(nx, dtype=dtype), scale=tf.ones(nx, dtype=dtype)) one_normal = tfd.Normal(loc=tf.constant(0., dtype=dtype), scale=tf.constant(1., dtype=dtype)) mnes_val = tf.constant(0.0, dtype=dtype) for i in range(n_hyp): l = tf.reshape(ls[i, :], shape=(1, xdim)) sigma = sigmas[i] sigma0 = sigma0s[i] invK = invKs[i, ...] ymax = ymaxs[i, :] # (nmax,nbound) f_mean, f_var = utils.compute_mean_var_f(x, X, Y, l, sigma, sigma0, invK, dtype=dtype) f_mean = tf.reshape(f_mean, (nx, 1)) f_var = tf.reshape(f_var, (nx, 1)) f_std = tf.sqrt(f_var) # f_mean.shape = nx x 1 # f_var.shape = nx x 1 y_var = f_var + sigma0 y_std = tf.sqrt(y_var) ysamples = normal_dist.sample(sample_shape=(nsamples)) # (nsamples, nx) ysamples = tf.transpose(ysamples) * y_std + f_mean # (nx, nsamples) ysamples = tf.expand_dims(ysamples, axis=0) # (1, nx, nsamples) """ (nmax, nx, nsamples, nbound) """ ysamples = tf.expand_dims(ysamples, axis=3) # (1, nx, nsamples, 1) ymax = tf.expand_dims(tf.expand_dims(ymax, axis=1), axis=1) # (nmax, 1, 1, nbound) f_mean = tf.reshape(f_mean, shape=(1, nx, 1, 1)) f_var = tf.reshape(f_var, shape=(1, nx, 1, 1)) f_std = tf.reshape(f_std, shape=(1, nx, 1, 1)) y_var = tf.reshape(y_var, shape=(1, nx, 1, 1)) y_std = tf.reshape(y_std, shape=(1, nx, 1, 1)) # including the 2 ending segments # [-infty,a0], [ak,infty] interval_logprob_given_yD = tf.concat([ one_normal.log_cdf((ymax[:, :, :, 0:1] - f_mean) / f_std), one_normal.log_cdf(-(ymax[:, :, :, 1:] - f_mean) / f_std) ], axis=3) # (nmax, nx, 1, nbound) # including the 2 ending segments # [-infty,a0], [ak,infty] interval_logprob_given_yD_yx = tf.concat([ one_normal.log_cdf( (y_var * ymax[:, :, :, 0:1] - sigma0 * f_mean - f_var * ysamples) / (tf.sqrt(sigma0) * f_std * y_std)), one_normal.log_cdf(-(y_var * ymax[:, :, :, 1:] - sigma0 * f_mean - f_var * ysamples) / (tf.sqrt(sigma0) * f_std * y_std)) ], axis=3) # (nmax, nx, nsamples, nbound) # including the middle segment interval_prob_given_yD_mid = one_normal.cdf( (ymax[:, :, :, 1:] - f_mean) / f_std) - one_normal.cdf( (ymax[:, :, :, 0:1] - f_mean) / f_std) interval_prob_given_yD_mid = tf.clip_by_value( interval_prob_given_yD_mid, clip_value_min=1e-300, clip_value_max=1.0) interval_logprob_given_yD_mid = tf.log(interval_prob_given_yD_mid) interval_prob_given_yD_yx_mid = \ one_normal.cdf( (y_var * ymax[:,:,:,1:] - sigma0 * f_mean - f_var * ysamples) / (tf.sqrt(sigma0) * f_std * y_std) ) - one_normal.cdf( (y_var * ymax[:,:,:,0:1] - sigma0 * f_mean - f_var * ysamples) / (tf.sqrt(sigma0) * f_std * y_std) ) interval_prob_given_yD_yx_mid = tf.clip_by_value( interval_prob_given_yD_yx_mid, clip_value_min=1e-300, clip_value_max=1.0) interval_logprob_given_yD_yx_mid = tf.log( interval_prob_given_yD_yx_mid) interval_logprob_given_yD = tf.concat( [interval_logprob_given_yD, interval_logprob_given_yD_mid], axis=3) interval_logprob_given_yD_yx = tf.concat( [interval_logprob_given_yD_yx, interval_logprob_given_yD_yx_mid], axis=3) log_w = interval_logprob_given_yD_yx - interval_logprob_given_yD # (nmax, nx, nsamples, nbound) # tmp = tf.where( # interval_logprob_given_yD_yx < -1e200, # tf.zeros_like(log_w, dtype=dtype), # tf.exp(interval_logprob_given_yD_yx) * log_w) tmp = tf.exp(interval_logprob_given_yD_yx) * log_w mnes_i = tf.reduce_mean( tf.reduce_mean( tf.reduce_sum(tmp, axis=3), # average over interval index axis=0), # average over nmax axis=1) # average over nsamples # (nx,) mnes_val = mnes_val + tf.squeeze(mnes_i) / tf.constant(n_hyp, dtype=dtype) return mnes_val
def interval_rmes(x, ls, sigmas, sigma0s, X, Y, xdim, n_hyp, ymaxs, invKs, nsamples=100, dtype=tf.float32): """ a more efficient formula than evaluate_interval_rmes.py as it uses simplified formula X: n x xdim Y: n x 1 ls: nh x xdim sigmas: nh x 1 signal variances sigma0s: nh x 1 noise variances where nh is the number of hyperparameters invKs: nh x n x n ymaxs: nh x n_maxs """ nx = tf.shape(x)[0] nmax = tf.shape(ymaxs)[1] normal_dist = tfd.Normal(loc=tf.zeros(nx, dtype=dtype), scale=tf.ones(nx, dtype=dtype)) one_normal = tfd.Normal(loc=tf.constant(0., dtype=dtype), scale=tf.constant(1., dtype=dtype)) rmes = tf.constant(0.0, dtype=dtype) for i in range(n_hyp): l = tf.reshape(ls[i, :], shape=(1, xdim)) sigma = sigmas[i] sigma0 = sigma0s[i] invK = invKs[i, ...] ymax = ymaxs[i, :] # (nmax,) f_mean, f_var = utils.compute_mean_var_f(x, X, Y, l, sigma, sigma0, invK, dtype=dtype) f_mean = tf.reshape(f_mean, (nx, 1)) f_var = tf.reshape(f_var, (nx, 1)) # f_mean.shape = nx x 1 # f_var.shape = nx x 1 y_var = f_var + sigma0 y_std = tf.sqrt(y_var) ysamples = normal_dist.sample(sample_shape=(nsamples)) # (nsamples, nx) ysamples = tf.transpose(ysamples) * y_std + f_mean # (nx, nsamples) ysamples = tf.expand_dims(ysamples, axis=0) # (1, nx, nsamples) # function g shape: # (nmax, nx, nsamples) ymax = tf.expand_dims(tf.expand_dims(ymax, axis=1), axis=1) # (nmax, 1, 1) f_mean = tf.expand_dims(f_mean, axis=0) # (1, nx, 1) f_var = tf.expand_dims(f_var, axis=0) # (1, nx, 1) y_var = tf.expand_dims(y_var, axis=0) # (1, nx, 1) y_std = tf.expand_dims(y_std, axis=0) f_std = tf.sqrt(f_var) log_upper_cdf = one_normal.log_cdf((ymax - f_mean) / f_std) log_lower_cdf = one_normal.log_cdf(-(ymax - f_mean) / f_std) # (nmax, nx, 1) log_upper_g = one_normal.log_cdf( (y_var * ymax - sigma0 * f_mean - f_var * ysamples) / (tf.sqrt(sigma0) * f_std * y_std)) log_upper_g = tf.cast(log_upper_g, dtype=dtype) log_upper_w = log_upper_g - log_upper_cdf # (nmax, nx, nsamples) log_lower_g = one_normal.log_cdf( -(y_var * ymax - sigma0 * f_mean - f_var * ysamples) / (tf.sqrt(sigma0) * f_std * y_std)) log_lower_g = tf.cast(log_lower_g, dtype=dtype) log_lower_w = log_lower_g - log_lower_cdf # (nmax, nx, nsamples) log_g = tf.stack([log_upper_g, log_lower_g]) log_w = tf.stack([log_upper_w, log_lower_w]) # (2, nmax, nx, nsamples) rmes_i = tf.reduce_mean( tf.reduce_mean( tf.reduce_sum(tf.exp(log_g) * log_w, axis=0), # average over upper, lower axis=0), # average over nmax axis=1) # average over nsample # (nx,) rmes = rmes + tf.squeeze(rmes_i) / tf.constant(n_hyp, dtype=dtype) return rmes
def get_intermediate_tensors(criterion, crit_params, required_placeholders, ls, sigmas, sigma0s, level, dtype, is_debug_mode=False): xdim = crit_params['xdim'] nhyp = crit_params['nhyp'] xmin = crit_params['xmin'] xmax = crit_params['xmax'] opt_crit_top_init_k = crit_params['opt_crit_top_init_k'] X_plc = required_placeholders['X'] Y_plc = required_placeholders['Y'] intermediate_tensors = {} invKs = utils.precomputeInvKs(xdim, nhyp, ls, sigmas, sigma0s, X_plc, dtype) # nhyp x nobs x nobs intermediate_tensors['invKs'] = invKs meanf, varf = utils.compute_mean_var_f( required_placeholders['validation_X'], X_plc, Y_plc, tf.reshape(ls[0, ...], shape=(1, -1)), sigmas[0, ...], sigma0s[0, ...], NKInv=invKs[0, ...], fullcov=False, dtype=dtype) stdf = tf.sqrt(varf) intermediate_tensors['meanf'] = meanf intermediate_tensors['stdf'] = stdf # optimize acquisition function opt_crit_func = lambda x: evaluate_criterion(tf.reshape(x, shape=(-1, xdim)), 1, criterion, crit_params, ls, sigmas, sigma0s, level, required_placeholders, dtype=dtype) opt_crit_multiple_func = lambda x: evaluate_criterion( tf.reshape(x, shape=(-1, xdim)), opt_crit_top_init_k, criterion, crit_params, ls, sigmas, sigma0s, level, required_placeholders, dtype=dtype) opt_crit_assign, opt_crit_train, \ opt_crit_maximizer, opt_crit_maximum \ = utils_for_continuous.optimize_continuous_function( xdim, opt_crit_func, required_placeholders['opt_crit_candidate_xs'], opt_crit_top_init_k, parallel_iterations = parallel_iterations, xmin = xmin, xmax = xmax, dtype = dtype, name = 'optimize_crit', multiple_func = opt_crit_multiple_func) intermediate_tensors['opt_crit_assign'] = opt_crit_assign intermediate_tensors['opt_crit_train'] = opt_crit_train intermediate_tensors['opt_crit_maximizer'] = opt_crit_maximizer intermediate_tensors['opt_crit_maximum'] = opt_crit_maximum return intermediate_tensors
def get_intermediate_tensors(criterion, crit_params, required_placeholders, ls, sigmas, sigma0s, dtype, is_debug_mode=False): xdim = crit_params['xdim'] nhyp = crit_params['nhyp'] nmax = crit_params['nmax'] nfeature = crit_params['nfeature'] xmin = crit_params['xmin'] xmax = crit_params['xmax'] opt_meanf_top_init_k = crit_params['opt_meanf_top_init_k'] opt_fsample_top_init_k = crit_params['opt_fsample_top_init_k'] opt_crit_top_init_k = crit_params['opt_crit_top_init_k'] X_plc = required_placeholders['X'] Y_plc = required_placeholders['Y'] intermediate_tensors = {} max_observed_y = tf.reduce_max(Y_plc) intermediate_tensors['max_observed_y'] = max_observed_y invKs = utils.precomputeInvKs(xdim, nhyp, ls, sigmas, sigma0s, X_plc, dtype) # nhyp x nobs x nobs intermediate_tensors['invKs'] = invKs meanf, varf = utils.compute_mean_var_f( required_placeholders['validation_X'], X_plc, Y_plc, tf.reshape(ls[0, ...], shape=(1, -1)), sigmas[0, ...], sigma0s[0, ...], NKInv=invKs[0, ...], fullcov=False, dtype=dtype) stdf = tf.sqrt(varf) intermediate_tensors['meanf'] = meanf intermediate_tensors['stdf'] = stdf # optimize mean function opt_meanf_func = lambda x: utils.compute_mean_f( tf.reshape(x, shape=(-1, xdim)), xdim, nhyp, X_plc, Y_plc, ls, sigmas, sigma0s, required_placeholders['invKs'], dtype=dtype) opt_meanf_assign, opt_meanf_train, opt_meanf_maximizer, opt_meanf_maximum \ = utils_for_continuous.optimize_continuous_function(xdim, opt_meanf_func, required_placeholders['opt_meanf_candidate_xs'], opt_meanf_top_init_k, parallel_iterations=parallel_iterations, xmin = xmin, xmax = xmax, dtype= dtype, name = 'opt_meanf') intermediate_tensors['opt_meanf_assign'] = opt_meanf_assign intermediate_tensors['opt_meanf_train'] = opt_meanf_train intermediate_tensors['opt_meanf_maximizer'] = opt_meanf_maximizer intermediate_tensors['opt_meanf_maximum'] = opt_meanf_maximum thetas_all, Ws_all, bs_all = utils_for_continuous.sample_function( xdim, nhyp, nmax, nfeature, ls, sigmas, sigma0s, X_plc, Y_plc, dtype=dtype) intermediate_tensors['thetas'] = thetas_all intermediate_tensors['Ws'] = Ws_all intermediate_tensors['bs'] = bs_all # optimize function samples opt_fsample_assigns, opt_fsample_trains, \ opt_fsample_maximizers, opt_fsample_maxima, \ _, opt_fsample_top_k_inits \ = utils_for_continuous.sample_xmaxs_fmaxs( xdim, nhyp, nmax, nfeature, ls, sigmas, sigma0s, required_placeholders['thetas'], required_placeholders['Ws'], required_placeholders['bs'], required_placeholders['opt_fsample_candidate_xs'], opt_fsample_top_init_k, xmin, xmax, get_xs=True, dtype=dtype, parallel_iterations=parallel_iterations, name='sample_xmaxs_fmaxs') intermediate_tensors['opt_fsample_assigns'] = opt_fsample_assigns intermediate_tensors['opt_fsample_trains'] = opt_fsample_trains intermediate_tensors['opt_fsample_maximizers'] = opt_fsample_maximizers intermediate_tensors['opt_fsample_maxima'] = opt_fsample_maxima # for debugging intermediate_tensors['opt_fsample_top_k_inits'] = opt_fsample_top_k_inits # optimize acquisition function opt_fsample_maximizers = required_placeholders[ 'opt_fsample_maximizers'] # (nhyp, nmax, xdim) opt_fsample_maxima = required_placeholders[ 'opt_fsample_maxima'] # (nhyp, nmax) opt_crit_func = lambda x: evaluate_criterion(tf.reshape(x, shape=(-1, xdim)), 1, criterion, crit_params, ls, sigmas, sigma0s, required_placeholders, dtype=dtype) opt_crit_multiple_func = lambda x: evaluate_criterion( tf.reshape(x, shape=(-1, xdim)), opt_crit_top_init_k, criterion, crit_params, ls, sigmas, sigma0s, required_placeholders, dtype=dtype) opt_crit_assign, opt_crit_train, \ opt_crit_maximizer, opt_crit_maximum \ = utils_for_continuous.optimize_continuous_function( xdim, opt_crit_func, required_placeholders['opt_crit_candidate_xs'], opt_crit_top_init_k, parallel_iterations = parallel_iterations, xmin = xmin, xmax = xmax, dtype = dtype, name = 'optimize_crit', multiple_func = opt_crit_multiple_func) intermediate_tensors['opt_crit_assign'] = opt_crit_assign intermediate_tensors['opt_crit_train'] = opt_crit_train intermediate_tensors['opt_crit_maximizer'] = opt_crit_maximizer intermediate_tensors['opt_crit_maximum'] = opt_crit_maximum return intermediate_tensors
def pes(xs, xdim, n_max, n_hyp, Xsamples, Ysamples, ls, sigmas, sigma0s, xmaxs, invKs, invKmaxsams, max_observed_y, dtype=tf.float32, n_x=1): # invKmaxsams shape: (n_hyp, n_max, n_x+1, n_x+1) # TODO: fix xmaxs is of shape(n_hyp, n_max, xdim) pes_vals = [tf.constant(0.0, dtype=dtype) for i in range(n_x)] norm = tfp.distributions.Normal(loc=tf.constant(0., dtype=dtype), scale=tf.constant(1., dtype=dtype)) for i in range(n_hyp): l = tf.reshape(ls[i, :], shape=(1, xdim)) sigma = sigmas[i] sigma0 = sigma0s[i] invK = invKs[i, ...] mean_fmaxs_C2, var_fmaxs_C2, _, _ = imposeC2(xdim, n_max, Xsamples, Ysamples, xmaxs[i, ...], max_observed_y, l, sigma, sigma0, invK, dtype) f_mean_all, f_var_all = utils.compute_mean_var_f(xs, Xsamples, Ysamples, l, sigma, sigma0, invK, dtype=dtype) zero_const = tf.constant(0.0, dtype=dtype) for idx in range(n_x): x = tf.reshape(xs[idx, ...], shape=(1, xdim)) ent_y = utils.evaluate_norm_entropy( tf.sqrt(tf.squeeze(f_var_all[idx]) + sigma0), dtype=dtype) cond_ent_y = zero_const for j in range(n_max): invKmaxsam = invKmaxsams[i, j, ...] mean_fmax_C2 = mean_fmaxs_C2[j] var_fmax_C2 = var_fmaxs_C2[j] xmax = tf.reshape(xmaxs[i, j, ...], shape=(1, xdim)) _, var_f_C2_C3 = imposeC3(xdim, x, Xsamples, Ysamples, xmax, mean_fmax_C2, var_fmax_C2, l, sigma, sigma0, invK, invKmaxsam, norm, dtype) cond_ent_y = cond_ent_y + utils.evaluate_norm_entropy( tf.sqrt(var_f_C2_C3 + sigma0), dtype=dtype) cond_ent_y = cond_ent_y / tf.constant(n_max, dtype=dtype) pes_vals[idx] = pes_vals[idx] + (ent_y - cond_ent_y) / tf.constant( n_hyp, dtype=dtype) return tf.squeeze(tf.stack(pes_vals))
def mes(x, ls, sigmas, sigma0s, Xsamples, Ysamples, xdim, n_hyp, ymaxs, invKs, dtype=tf.float32): """ X: n x xdim Y: n x 1 ls: nh x xdim sigmas: nh x 1 signal variances sigma0s: nh x 1 noise variances where nh is the number of hyperparameters invKs: nh x n x n ymaxs: nh x n_maxs """ nx = tf.shape(x)[0] nmax = tf.shape(ymaxs)[1] mes = tf.constant(0.0, dtype=dtype) norm = tf.distributions.Normal( loc=tf.constant(0., dtype=dtype), scale=tf.constant(1., dtype=dtype)) for i in range(n_hyp): l = tf.reshape(ls[i,:], shape=(1,xdim)) sigma = sigmas[i] sigma0 = sigma0s[i] invK = invKs[i,...] f_mean, f_var = utils.compute_mean_var_f(x, Xsamples, Ysamples, l, sigma, sigma0, invK, dtype=dtype) f_std = tf.sqrt(f_var) f_mean = tf.reshape(f_mean, shape=(1, nx)) f_std = tf.reshape(f_std, shape=(1,nx)) ent_f = utils.evaluate_norm_entropy(f_std, dtype=dtype) # (1,nx) ymax = tf.reshape(ymaxs[i,:], shape=(nmax,1)) # (nmax, 1) standardized_ymax = (ymax - f_mean) / f_std # (nmax, nx) logcdf_ymax = norm.log_cdf(standardized_ymax) cdf_ymax = tf.exp(logcdf_ymax) cond_ent_f = tf.reduce_mean( tf.constant(0.5, dtype=dtype) + tf.log( tf.cast(tf.sqrt(2.0 * np.pi), dtype=dtype) * f_std) + logcdf_ymax - standardized_ymax * norm.prob(standardized_ymax) / 2.0 / cdf_ymax, axis=0) # (nmax,) mes = mes + tf.squeeze(ent_f - cond_ent_f) / tf.constant(n_hyp, dtype=dtype) return mes
xdim = 2 dtype = tf.float32 xplot_plc = tf.placeholder(dtype=dtype, shape=[None, xdim], name='xplot_plc') X_plc = tf.placeholder(dtype=dtype, shape=[None, xdim], name='X_plc') Y_plc = tf.placeholder(dtype=dtype, shape=[None, 1], name='Y_plc') lengthscale_plc = tf.placeholder(dtype=dtype, shape=[1, xdim], name='lengthscales') sigma_plc = tf.placeholder(dtype=dtype, shape=(), name='sigma') sigma0_plc = tf.placeholder(dtype=dtype, shape=(), name='sigma0') meanf, varf = utils.compute_mean_var_f(xplot_plc, X_plc, Y_plc, lengthscale_plc, sigma_plc, sigma0_plc, NKInv=None, fullcov=False, dtype=dtype) Kmm = utils.computeKmm_old(X_plc, lengthscale_plc, sigma_plc, dtype=dtype) Knm = utils.computeKnm(xplot_plc, X_plc, lengthscale_plc, sigma_plc, dtype=dtype) attrs = "East,North,potassium,log10K,pH,phosphorus,log10P".split(',') d = np.genfromtxt('bbarn.csv', delimiter=',') # x1: d[:,0]: 1->18