def single_projF(gll, gram, samp): x = samp(3, 2) proj = bc.ProjectionF(x, gll, Nsamps, lambda: samp(1, 2).flatten()) w = proj.get() assert np.all( np.fabs(gram(x) - w.dot(w.T)) < 1e-2 ), "error: projectionF doesn't converge to expectation; max diff = " + str( np.fabs(gram(x) - w.dot(w.T)).max()) proj.reset() assert proj.get( ).shape == w.shape, "error: proj.reset() doesn't retain shape" is_constant = True gtest = gll(x, samp(1, 2).flatten()) for i in range(10): gtest2 = gll(x, samp(1, 2).flatten()) if np.any(gtest2 != gtest): is_constant = False if not is_constant: assert np.all(np.fabs(w - proj.get()) > 0 ), "error: proj.reset() doesn't refresh entries" proj.reset(5) assert proj.get().shape[ 1] == 5, "error: proj.reset(5) doesn't create a new projection with 5 components"
mu = res.x cov = -np.linalg.inv(hess_log_joint(Z, mu)) t_laplace = time.time() - t0 cputs = np.zeros((len(anms), n_trials, Ms.shape[0])) csizes = np.zeros((len(anms), n_trials, Ms.shape[0])) Fs = np.zeros((len(anms), n_trials, Ms.shape[0])) Fs_full = np.zeros(n_trials) cputs_full = np.zeros(n_trials) for tr in range(n_trials): print('Trial ' + str(tr + 1) + '/' + str(n_trials)) print('Computing random projection') t0 = time.time() proj = bc.ProjectionF(Z, grad_log_likelihood, projection_dim, lambda: np.random.multivariate_normal(mu, cov)) vecs = proj.get() t_projection = time.time() - t0 print('Running MCMC on the full dataset') accept_rate = 1. mcmc_attempt = 1 while (accept_rate < .15 or accept_rate > 0.7): t0 = time.time() mh_param_init = np.random.multivariate_normal(mu, cov) th_samples, accept_rate = mh( mh_param_init, lambda th: log_joint(Z, th, np.ones(Z.shape[0])), None, lambda th, sig: np.random.multivariate_normal( th, sig * np.eye(D)),
cov = -np.linalg.inv(hess_log_joint(Z, mu)) #we can call post_approx() to sample from the approximate posterior post_approx = lambda: np.random.multivariate_normal(mu, cov) #you can replace this step with almost any inference alg: subset MCMC, variational inference, INLA, SGLD, etc ######################################## ######################################## ## Step 3: Discretize the Log Likelihood ######################################## ######################################## projection_dim = 500 #random projection dimension, K #build the discretization of all the log-likelihoods based on random projection proj = bc.ProjectionF(Z, grad_log_likelihood, projection_dim, post_approx) #construct the N x K discretized log-likelihood matrix; each row represents the discretized LL func for one datapoint vecs = proj.get() ############################ ############################ ## Step 4: Build the Coreset ############################ ############################ #build the coreset M = 100 # use 100 datapoints giga = bc.GIGA( vecs ) #do coreset construction using the discretized log-likelihood functions giga.run(M) #build the coreset