def update_density_estimate(self): # load or learn parameters if self.learn_parameters: sigma, lmbda = self.determine_sigma_lmbda() else: sigma = self.sigma0 lmbda = self.lmbda0 logger.info("Using sigma: %.2f, lmbda=%.6f" % (sigma, lmbda)) D = self.Z.shape[1] gamma = 0.5 / (sigma**2) omega, u = sample_basis(D, self.m, gamma) logger.info("Estimate density in RKHS, N=%d, m=%d" % (self.N, self.m)) theta = score_matching_sym(self.Z, lmbda, omega, u) # logger.info("Computing objective function") # J = _objective_sym(Z, sigma, lmbda, a, K, b, C) # J_xval = np.mean(xvalidate(Z, 5, sigma, self.lmbda, K)) # logger.info("N=%d, sigma: %.2f, lambda: %.2f, J(a)=%.2f, XJ(a)=%.2f" % \ # (self.N, sigma, self.lmbda, J, J_xval)) dlogq_est = lambda x: log_pdf_estimate_grad( feature_map_grad_single(x, omega, u), theta) return dlogq_est
def callback_sigma_lmbda(model, bounds, info, x, index, ftrue): global D fig = pl.figure(1) fig.clf() sigma = 2**info[-1]['xbest'][0] lmbda = 2**info[-1]['xbest'][1] pl.title("sigma=%.2f, lmbda=%.6f" % (sigma, lmbda)) gamma = 0.5 * (sigma**2) omega, u = sample_basis(D, m, gamma) theta = score_matching_sym(Z, lmbda, omega, u) logq_est = lambda x: np.dot(theta, feature_map_single(x, omega, u)) dlogq_est = lambda x: np.dot(theta, feature_map_grad_single(x, omega, u)) Xs = np.linspace(-3, 3) Ys = np.linspace(-3, 3) Q = evaluate_density_grid(Xs, Ys, logq_est) plot_array(Xs, Ys, Q, pl.gca(), plot_contour=True) pl.plot(Z[:, 0], Z[:, 1], 'bx') for ax in fig.axes: # remove tick labels ax.set_xticklabels([]) ax.set_yticklabels([]) pl.draw() pl.show(block=False)
def predict(x, alphas): D = len(x) f = 0. for i in range(len(alphas)): np.random.seed(seed_offset + i) omega, u = sample_basis(D=D, m=1, gamma=gamma) phi_x = feature_map(x, omega, u) f += alphas[i] * phi_x return f
def sigma_objective(log2_sigma, lmbda): sigma = 2**log2_sigma folds = np.zeros(num_repetitions) for i in range(num_repetitions): gamma = 0.5 * (sigma**2) omega, u = sample_basis(D, m, gamma) folds[i] = np.mean( xvalidate(Z, lmbda, omega, u, num_folds, num_repetitions=1)) result = np.mean(folds) logger.info("xvalidation, sigma: %.2f, lambda: %.2f, J=%.3f" % \ (sigma, lmbda, result)) return result
def lmbda_objective(log2_lmbda, sigma): lmbda = 2**log2_lmbda gamma = 0.5 * (sigma**2) omega, u = sample_basis(D, m, gamma) folds = xvalidate(Z, lmbda, omega, u, num_folds, num_repetitions=num_repetitions) result = np.mean(folds) logger.info("xvalidation, sigma: %.2f, lambda: %.2f, J=%.3f" % \ (sigma, lmbda, result)) return result
def fun(sigma_lmbda, m=100, num_repetitions=3): log2_sigma = sigma_lmbda[0] log2_lmbda = sigma_lmbda[1] sigma = 2**log2_sigma gamma = 0.5 * (sigma**2) lmbda = 2**log2_lmbda omega, u = sample_basis(D, m, gamma) folds = [xvalidate(Z, lmbda, omega, u) for _ in range(num_repetitions)] J = np.mean(folds) J_std = np.std(folds) print("fun: log2_sigma=%.2f, log_lmbda=%.2f, J(a)=%.2f" % (log2_sigma, log2_lmbda, J)) return J, J_std
def multicore_fun(log2_sigma, log2_lmbda, num_repetitions, num_folds, Z, m): D = Z.shape[1] lmbda = 2**log2_lmbda sigma = 2**log2_sigma gamma = 0.5 * (sigma**2) folds = np.zeros(num_repetitions) for j in range(num_repetitions): logger.debug("xvalidation repetition %d/%d" % (j + 1, num_repetitions)) omega, u = sample_basis(D, m, gamma) folds[j] = np.mean( xvalidate(Z, lmbda, omega, u, num_folds, num_repetitions=1)) result = np.mean(folds) logger.info("cma particle, sigma: %.2f, lambda: %.6f, J=%.4f" % \ (sigma, lmbda, result)) return result
def _f(log2_sigma): sigma = 2**log2_sigma folds = np.zeros(num_repetitions) for i in range(num_repetitions): gamma = 0.5 * (sigma**2) omega, u = sample_basis(D, m, gamma) folds[i] = np.mean( xvalidate(Z, lmbda, omega, u, num_folds, num_repetitions=1)) result = np.mean(folds) print("xvalidation, sigma: %.2f, lambda: %.6f, J=%.3f" % \ (sigma, lmbda, result)) # transform to log space to make GP work. # since unbounded, add constant # also pybo maximises so invert sign result = -np.log(result + 100) return result
def set_up(self): if self.learn_parameters or self.force_relearn_parameters: self.sigma, self.lmbda = self.determine_sigma_lmbda() logger.info("Using sigma=%.2f, lmbda=%.6f" % (self.sigma, self.lmbda)) gamma = 0.5 * (self.sigma**2) logger.info("Sampling random basis") omega, u = sample_basis(self.D, self.m, gamma) logger.info("Estimating density in RKHS, N=%d, m=%d, D=%d" % (len(self.Z), self.m, self.D)) theta = score_matching_sym(self.Z, self.lmbda, omega, u) # replace target by kernel estimator to simulate trajectories on # but keep original target for computing acceptance probability self.orig_target = self.target self.target = RandomFeatsEstimator(theta, omega, u) HMCJob.set_up(self) # plot density estimate if self.plot: import matplotlib.pyplot as plt from scripts.tools.plotting import evaluate_density_grid, evaluate_gradient_grid, plot_array Xs = np.linspace(-15, 15) Ys = np.linspace(-7, 3) Xs_grad = np.linspace(-40, 40, 40) Ys_grad = np.linspace(-15, 25, 40) G = evaluate_density_grid(Xs, Ys, self.target.log_pdf) G_norm, quiver_U, quiver_V, _, _ = evaluate_gradient_grid( Xs_grad, Ys_grad, self.target.grad) plt.subplot(211) plt.plot(self.Z[:, 0], self.Z[:, 1], 'bx') plot_array(Xs, Ys, np.exp(G), plot_contour=True) plt.subplot(212) plot_array(Xs_grad, Ys_grad, G_norm, plot_contour=True) plt.quiver(Xs_grad, Ys_grad, quiver_U, quiver_V, color='m') plt.ioff() plt.show()
def select_sigma_grid(Z, m, num_folds=5, num_repetitions=3, log2_sigma_min=-3, log2_sigma_max=10, resolution_sigma=25, lmbda=0.0001, plot_surface=False): D = Z.shape[1] sigmas = 2**np.linspace(log2_sigma_min, log2_sigma_max, resolution_sigma) Js = np.zeros(len(sigmas)) for i, sigma in enumerate(sigmas): gamma = 0.5 * (sigma**2) folds = np.zeros(num_repetitions) for j in range(num_repetitions): # re-sample basis every repetition omega, u = sample_basis(D, m, gamma) folds[j] = xvalidate(Z, lmbda, omega, u, n_folds=num_folds, num_repetitions=1) Js[i] = np.mean(folds) logger.info("fold %d/%d, sigma: %.2f, lambda: %.2f, J=%.3f" % \ (i + 1, len(sigmas), sigma, lmbda, Js[i])) # if plot_surface: # plt.figure() # plt.plot(np.log2(sigmas), Js) return sigmas[Js.argmin()]
N = 20 D = 2 Z = np.random.randn(N, D) m = N # print np.sum(Z) * np.std(Z) * np.sum(Z**2) * np.std(Z**2) num_folds = 5 num_repetitions = 3 cma_opts = {'tolfun': 0.1, 'maxiter': 1, 'verb_disp': 1} num_threads = 2 # D = 2 sigma0 = 0.51 lmbda0 = 0.0000081 sigma, lmbda, es = select_sigma_lambda_cma(Z, m, num_threads, num_folds, num_repetitions, sigma0, lmbda0, cma_opts, return_cma=True) gamma = 0.5 * (sigma**2) omega, u = sample_basis(D, m, gamma) print(sigma, lmbda, np.mean(xvalidate(Z, lmbda, omega, u, n_folds=5, num_repetitions=3)))
def callback_lmbda(model, bounds, info, x, index, ftrue): global D """ Plot the current posterior, the index, and the value of the current recommendation. """ xmin, xmax = bounds[0] xx_ = np.linspace(xmin, xmax, 500) # define grid xx = xx_[:, None] # ff = ftrue(xx) # compute true function acq = index(xx) # compute acquisition mu, s2 = model.posterior(xx) # compute posterior and lo = mu - 2 * np.sqrt(s2) # quantiles hi = mu + 2 * np.sqrt(s2) # ymin, ymax = ff.min(), ff.max() # get plotting ranges # ymin -= 0.2 * (ymax - ymin) # ymax += 0.2 * (ymax - ymin) kwplot = {'lw': 2, 'alpha': 0.5} # common plotting kwargs fig = pl.figure(1) fig.clf() pl.subplot(221) # pl.plot(xx, ff, 'k:', **kwplot) # plot true function pl.plot(xx, mu, 'b-', **kwplot) # plot the posterior and pl.fill_between(xx_, lo, hi, color='b', alpha=0.1) # uncertainty bands pl.scatter( info['x'], info['y'], # plot data marker='o', facecolor='none', zorder=3) pl.axvline(x, color='r', **kwplot) # latest selection pl.axvline(info[-1]['xbest'], color='g', **kwplot) # current recommendation # pl.axis((xmin, xmax, ymin, ymax)) pl.ylabel('posterior') pl.subplot(223) pl.fill_between( xx_, acq.min(), acq, # plot acquisition color='r', alpha=0.1) pl.axis('tight') pl.axvline(x, color='r', **kwplot) # plot latest selection pl.xlabel('input') pl.ylabel('acquisition') pl.subplot(224) pl.plot(info['x'], 'g') pl.subplot(222) lmbda = 2**info[-1]['xbest'] gamma = 0.5 * (sigma**2) omega, u = sample_basis(D, m, gamma) theta = score_matching_sym(Z, lmbda, omega, u) logq_est = lambda x: np.dot(theta, feature_map_single(x, omega, u)) dlogq_est = lambda x: np.dot(theta, feature_map_grad_single(x, omega, u)) Xs = np.linspace(-3, 3) Ys = np.linspace(-3, 3) Q = evaluate_density_grid(Xs, Ys, logq_est) plot_array(Xs, Ys, Q, pl.gca(), plot_contour=True) pl.plot(Z[:, 0], Z[:, 1], 'bx') for ax in fig.axes: # remove tick labels ax.set_xticklabels([]) ax.set_yticklabels([]) pl.draw() pl.show(block=False)
np.random.seed(seed_offset + i) omega, u = sample_basis(D=D, m=1, gamma=gamma) phi_x = feature_map(x, omega, u) f += alphas[i] * phi_x return f alphas = np.zeros(num_iterations) for i in range(num_iterations): logger.info("Iteration %d" % i) x = X[i] # sample random feature np.random.seed(seed_offset + i) omega, u = sample_basis(D=D, m=1, gamma=gamma) phi_x = feature_map(x, omega, u) # sample data point and predict f = predict(x, alphas[:i]) * phi_x # gradient of f at x f_grad = feature_map_grad_single(x, omega, u) * f # gradient grad = 0 for d in range(D): phi_derivative_d = feature_map_derivative_d(x, omega, u, d) phi_derivative2_d = feature_map_derivative2_d(x, omega, u, d) grad += phi_derivative_d * f_grad[d] + phi_derivative2_d