def get_static_surrogate(D): N = 200 X = np.random.randn(N, D) est = KernelExpLiteGaussian(sigma=1, lmbda=.1, D=D, N=N) est.fit(X) return est
def get_static_surrogate(D): N = 200 X = np.random.randn(N, D) est = KernelExpLiteGaussian(sigma=1, lmbda=.1, D=D, N=N) est.fit(X) return est
class KernelExpLiteGaussianSurrogate(StaticSurrogate): def __init__(self, ndim, sigma, lmbda, N): self.surrogate = KernelExpLiteGaussian(sigma=sigma, lmbda=lmbda, N=N, D=ndim) def train(self, samples): self.surrogate.fit(samples) def log_pdf_gradient(self, x): return self.surrogate.grad(x)
def test_third_order_derivative_tensor_execute(): if not theano_available: raise SkipTest("Theano not available.") sigma = 1. lmbda = 1. N = 100 D = 2 X = np.random.randn(N, D) est = KernelExpLiteGaussian(sigma, lmbda, D, N) est.fit(X) est.third_order_derivative_tensor(X[0])
def test_hessian_execute(): if not theano_available: raise SkipTest("Theano not available.") sigma = 1. lmbda = 1. N = 100 D = 2 X = np.random.randn(N, D) est = KernelExpLiteGaussian(sigma, lmbda, D, N) est.fit(X) est.hessian(X[0])
def test_third_order_derivative_tensor_execute(): if not theano_available: raise SkipTest("Theano not available.") sigma = 1. lmbda = 1. N = 100 D = 2 X = np.random.randn(N, D) est = KernelExpLiteGaussian(sigma, lmbda, D, N) est.fit(X) est.third_order_derivative_tensor(X[0])
def test_hessian_execute(): if not theano_available: raise SkipTest("Theano not available.") sigma = 1. lmbda = 1. N = 100 D = 2 X = np.random.randn(N, D) est = KernelExpLiteGaussian(sigma, lmbda, D, N) est.fit(X) est.hessian(X[0])
def get_kmc_static_kernel(): num_steps_min, num_steps_max, step_size_min, step_size_max = get_hmc_parameters() target, momentum = get_target_momentum() N = 200 X = np.random.randn(N, momentum.D) est = KernelExpLiteGaussian(sigma=1, lmbda=.1, D=momentum.D, N=N) est.fit(X) surrogate = get_static_surrogate(momentum.D) kmc = KMCStatic(surrogate, target, momentum, num_steps_min, num_steps_max, step_size_min, step_size_max) return kmc
def get_kmc_static_kernel(): num_steps_min, num_steps_max, step_size_min, step_size_max = get_hmc_parameters( ) target, momentum = get_target_momentum() N = 200 X = np.random.randn(N, momentum.D) est = KernelExpLiteGaussian(sigma=1, lmbda=.1, D=momentum.D, N=N) est.fit(X) surrogate = get_static_surrogate(momentum.D) kmc = KMCStatic(surrogate, target, momentum, num_steps_min, num_steps_max, step_size_min, step_size_max) return kmc
for i, sigma in enumerate(log_sigmas): est = KernelExpLiteGaussian(np.exp(sigma), lmbda, D, N) # this is an array num_repetitions x num_folds, each containing a objective xval_result = est.xvalidate_objective(X, num_folds=5, num_repetitions=2) O[i] = np.mean(xval_result) O_lower[i] = np.percentile(xval_result, 10) O_upper[i] = np.percentile(xval_result, 90) # best parameter best_log_sigma = log_sigmas[np.argmin(O)] # visualisation plt.figure() plt.plot([best_log_sigma, best_log_sigma], [np.min(O), np.max(O)], 'r') plt.plot(log_sigmas, O, 'b-') plt.plot(log_sigmas, O_lower, 'b--') plt.plot(log_sigmas, O_upper, 'b--') plt.xlim([np.min(log_sigmas) - 1, np.max(log_sigmas) + 1]) plt.xlabel("log sigma") plt.ylabel("objective") plt.title("lmbda=%.4f" % lmbda) plt.legend(["Best sigma", "Performance"]) plt.legend(["Best sigma", "Performance", "80% percentile"]) plt.tight_layout() est.sigma = np.exp(best_log_sigma) est.fit(X) visualise_fit_2d(est, X) plt.show()