if n_tot == 10000: run_time_save = run_time print("") print("Runtime:" + str(round(run_time, 2))) print(len(posteriors)) print("- log prob(theta_true)") log_probs = [] for i in range(len(posteriors)): posterior_samples = posteriors[i].sample_n(1000) post_gauss_approx = func.fit_gaussian_dist(posterior_samples) print(-post_gauss_approx.log_prob(theta_true)) log_probs.append(-post_gauss_approx.log_prob(theta_true)) # save samples np.savetxt('data/abcsmc_posterior_' + str(nbr_sim[i]) + '_' + id_job + '.csv', posterior_samples.detach().numpy(), delimiter=",") # write new results with open('results/abcsmc_' + id_job + '.txt', 'w') as f: f.write('%.4f\n' % run_time_save) for l in log_probs: f.write('%.4f\n' % l)
run_time = end - start print("") print("Runtime:" + str(round(run_time, 2))) log_probs = [] start = time.time() torch.manual_seed(seed) for i in range(nbr_rounds): print(i) posterior_sample = models_post[i].sample(1000, context=s_x_o) posterior_sample = posterior_sample.reshape((1000, 4)) # log_probs.append(-models_post[i].log_prob(theta_true.reshape((1, 4)), context=s_x_o)) post_gauss_approx = func.fit_gaussian_dist(posterior_sample.detach()) # to get correct prob log_probs.append(-post_gauss_approx.log_prob(theta_true)) if hp_tuning == 0 and lambda_val > 0: np.savetxt('lambda_val/post_samples_snpla_' + str(i + 1) + "_" + id_job + '.csv', posterior_sample.detach().numpy(), delimiter=",") elif hp_tuning == 0: np.savetxt('data/post_samples_snpla_' + str(i + 1) + "_" + id_job + '.csv', posterior_sample.detach().numpy(), delimiter=",") else: np.savetxt('hp_tuning/post_samples_snpla_' + str(i + 1) + "_" + id_job + '.csv',