def fit_gp_multinomial_model(model, test, pi_train=None, N_samples=100, run=1): if pi_train is not None: if isinstance(model, pgmult.gp.LogisticNormalGP): model.data_list[0]["psi"] = ln_pi_to_psi(pi_train) - model.mu elif isinstance(model, pgmult.gp.MultinomialGP): model.data_list[0]["psi"] = pi_to_psi(pi_train) - model.mu model.resample_omega() else: model.initialize_from_data() ### Inference results_base = os.path.join("results", "names", "run%03d" % run, "results") results_file = results_base + ".pkl.gz" if os.path.exists(results_file): with gzip.open(results_file, "r") as f: samples, lls, pred_lls, timestamps = pickle.load(f) else: Z_test = get_inputs(test) lls = [model.log_likelihood()] samples = [model.copy_sample()] pred_ll, pred_pi = model.predictive_log_likelihood(Z_test, test.data) pred_lls = [pred_ll] pred_pis = [pred_pi] times = [0] # Print initial values print("Initial LL: ", lls[0]) print("Initial Pred LL: ", pred_lls[0]) for itr in xrange(N_samples): print("Iteration ", itr) tic = time.time() model.resample_model(verbose=True) times.append(time.time()-tic) samples.append(model.copy_sample()) lls.append(model.log_likelihood()) pred_ll, pred_pi = model.predictive_log_likelihood(get_inputs(test), test.data) pred_lls.append(pred_ll) pred_pis.append(pred_pi) print("Log likelihood: ", lls[-1]) print("Pred Log likelihood: ", pred_ll) # Save this sample # with gzip.open(results_file + ".itr%03d.pkl.gz" % itr, "w") as f: # pickle.dump(model, f, protocol=-1) lls = np.array(lls) pred_lls = np.array(pred_lls) timestamps = np.cumsum(times) return samples, lls, pred_lls, pred_pis, timestamps
def theta(self, theta): self.psi = ln_pi_to_psi(theta)