def simulator(params): N = params.shape[0] data = torch.zeros(params.shape[0], 19) for i in range(N): data[i, :] = torch.as_tensor( func.whiten(simulator_wrapper(params[i, :]), whiteness_params)) return data
v_true, Iinj = model.simulator(model.log_theta_true, seed_data, True) summary_stats_obs = model.calculate_summary_statistics(v_true) # set up model simulator def simulator_wrapper(params): # return tensor return model.calculate_summary_statistics(model.simulator(params, None)) # run pilot to calc mean and std of summary stats whiteness_params = func.pilot_run(model, simulator_wrapper, summary_stats_obs) summary_stats_obs_w = func.whiten(summary_stats_obs, whiteness_params) w_sim_wrapper = lambda param: torch.as_tensor(func.whiten(simulator_wrapper(param), whiteness_params)) # run inference using SNPE from sbi.inference import SNPE_C, prepare_for_sbi print(model.prior) simulator, prior = prepare_for_sbi(w_sim_wrapper, model.prior) def build_custom_post_net(batch_theta, batch_x): flow_lik, flow_post = func.set_up_networks(model.prior.base_dist.low, model.prior.base_dist.high,
v_true, Iinj = model.simulator(model.log_theta_true, seed_data, True) summary_stats_obs = model.calculate_summary_statistics(v_true) # set up model simulator def simulator_wrapper(params): # return tensor return model.calculate_summary_statistics(model.simulator(params, None)) # run pilot to calc mean and std of summary stats whiteness_params = func.pilot_run(model, simulator_wrapper, summary_stats_obs) summary_stats_obs_w = func.whiten(summary_stats_obs, whiteness_params) w_sim_wrapper = lambda param: torch.as_tensor( func.whiten(simulator_wrapper(param), whiteness_params)) # run inference using SNL from sbi.inference import SNLE_A, prepare_for_sbi simulator, prior = prepare_for_sbi(w_sim_wrapper, model.prior) print("---") print(model.prior.base_dist.low) print(model.prior.base_dist.high)
v_true, Iinj = model.simulator(model.log_theta_true, seed_data, True) summary_stats_obs = model.calculate_summary_statistics(v_true) # set up model simulator def simulator_wrapper(params): # return tensor return model.calculate_summary_statistics(model.simulator(params, None)) # run pilot to calc mean and std of summary stats whiteness_params = func.pilot_run(model, simulator_wrapper, summary_stats_obs) summary_stats_obs_w = func.whiten(summary_stats_obs, whiteness_params) # m_prior, std_prior = func.load_summary_stats_mean_and_std() # w_sim_wrapper = lambda param: torch.as_tensor(func.whiten(simulator_wrapper(param), whiteness_params)) def simulator(params): N = params.shape[0] data = torch.zeros(params.shape[0], 19) for i in range(N): data[i, :] = torch.as_tensor( func.whiten(simulator_wrapper(params[i, :]), whiteness_params)) return data