def solve(problem, output_dir, epochs=1, model_size='smaller', n_posterior_samples=1): if model_size == 'bigger': n_hiddens = [50, 50, 50, 50] n_maf = 5 elif model_size == 'smaller': n_hiddens = [20, 20] n_maf = 3 else: raise ValueError(model_size) # Build list of neural networks NDEs = [ ndes.ConditionalMaskedAutoregressiveFlow( n_parameters=problem.param_dim, n_data=problem.observable_dim, n_hiddens=n_hiddens, n_mades=2, act_fun=tf.tanh, index=index) for index in range(n_maf) ] delfi_ensemble = CustomDelfi( problem.true_observation, problem.prior, NDEs, param_limits=[problem.lower, problem.upper], param_names=['param_{}'.format(n) for n in range(problem.param_dim)], results_dir=output_dir + "/", progress_bar=True, # n_procs=10, ) if problem.sim_data is not None: delfi_ensemble.load_simulations(problem.sim_data, problem.sim_params) delfi_ensemble.train_ndes(epochs=epochs) elif problem.simulator is not None: def compressor(data, *args): return data # do nothing. pydelfi should really allow compressor=None - submit a PR? TODO # total_sims = 50000 # num. points to evaluate # num. points to evaluate with uniform priors (before training) n_initial = 10000 n_populations = 5 # number of additional training runs # size of each batch # n_batch = int((total_sims-n_initial)/n_populations) n_batch = 9999 n_epochs = 100 safety = 5 # Overpropose by a factor of safety to (hopefully) cope gracefully with # the possibility of some bad proposals. # from mpi4py import MPI # comm = MPI.COMM_WORLD # rank = comm.Get_rank() # n_procs = 10 # comm = None # rank = None # n_procs = 1 # bayesian optimisation is another option here , to investigate TODO patience = max(1, int(3 * n_epochs / 20)) delfi_ensemble.sequential_training( problem.simulator, compressor, n_initial, n_batch, n_populations, patience=patience, # patience during each NN training sesh epochs=n_epochs, safety=safety, save_intermediate_posteriors=False) n_posterior_samples = 1 print('All training complete. Sampling posterior ({} samples)'.format( n_posterior_samples)) start_time = datetime.datetime.now() posterior_samples = delfi_ensemble.emcee_sample( main_chain=n_posterior_samples) end_time = datetime.datetime.now() print(end_time - start_time) logging.info(end_time - start_time) save_samples(posterior_samples, os.path.join(output_dir, 'samples.hdf5')) # filtered_samples = posterior_samples[( # posterior_samples > 0.) & (posterior_samples < 1.)] figure = corner.corner(posterior_samples) # TODO corner plot labels # , labels=['a', 'b'], # show_titles=True, title_kwargs={"fontsize": 12}) figure.savefig(os.path.join(output_dir, 'corner.png')) if problem.true_params is not None: # TODO automated comparison print(problem.true_params) logging.info(problem.true_params)
n_hidden=[30, 30], activations=[tf.tanh, tf.tanh], index=3), ndes.MixtureDensityNetwork(n_parameters=theta_dim, n_data=data_dim, n_components=5, n_hidden=[30, 30], activations=[tf.tanh, tf.tanh], index=4) ] elif surrogate == 'MAF': NDEs = [ ndes.ConditionalMaskedAutoregressiveFlow( n_parameters=theta_dim, n_data=data_dim, n_hiddens=[50, 50], n_mades=5, act_fun=tf.tanh, index=5) ] meth += '-' + surrogate meth += '(' + str(evidence) + ')' # set the prior for the neural surrogate lower = np.array([bounds[par][0] for par in par_names]) upper = np.array([bounds[par][1] for par in par_names]) prior = priors.Uniform(lower, upper) restore_file = 'restore/' + sim + '-' + meth + str(seed) DelfiEnsemble = delfi.Delfi(compressed_data, prior, NDEs,
i = 0 compressed_data = np.array(test[features])[i] params = np.array(test[theta])[i:i+1] # Initilize LFI #Finv = np.genfromtxt('simulators/cosmic_shear/pre_ran_sims/Finv.dat') #theta_fiducial = np.array([0.3, 0.8, 0.05, 0.70, 0.96]) lower = np.array([0, 1.05, 5.8, -400, -400]) upper = np.array([2.5, 1.4, 6.8, 400, 400]) prior = priors.Uniform(lower, upper) NDEs = [ndes.ConditionalMaskedAutoregressiveFlow(n_parameters=5, n_data=8, n_hiddens=[50,50], n_mades=5, act_fun=tf.tanh, index=0), ndes.MixtureDensityNetwork(n_parameters=5, n_data=8, n_components=1, n_hidden=[30,30], activations=[tf.tanh, tf.tanh], index=1), ndes.MixtureDensityNetwork(n_parameters=5, n_data=8, n_components=2, n_hidden=[30,30], activations=[tf.tanh, tf.tanh], index=2), ndes.MixtureDensityNetwork(n_parameters=5, n_data=8, n_components=3, n_hidden=[30,30], activations=[tf.tanh, tf.tanh], index=3), ndes.MixtureDensityNetwork(n_parameters=5, n_data=8, n_components=4, n_hidden=[30,30], activations=[tf.tanh, tf.tanh], index=4), ndes.MixtureDensityNetwork(n_parameters=5, n_data=8, n_components=5, n_hidden=[30,30], activations=[tf.tanh, tf.tanh], index=5)] DelfiEnsemble = delfi.Delfi(compressed_data, prior, NDEs, #Finv = Finv, #theta_fiducial = theta_fiducial, param_limits = [lower, upper], param_names = theta, # results_dir = '', #input_normalization="fisher" )
def skewerscloud_NDE(sampling='skewers'): ''' ''' datdir = os.path.join(os.environ['MNULFI_DIR'], 'peaks_massivenus') # fiducial theta and scores thetas = np.load(os.path.join(datdir, 'params_conc_means.npy')) # thetas theta_fid = thetas[51, :] scores_fid = np.load(os.path.join(datdir, 'scores.fid.npy')) Finv = np.load(os.path.join(datdir, 'peak.Finv.npy')) # inverse fisher theta_samp = np.load(os.path.join(datdir, 'theta.%s.npy' % sampling)) scores_samp = np.load(os.path.join(datdir, 'scores.%s.npy' % sampling)) # uniform prior theta_lims = [(theta_samp[:, i].min(), theta_samp[:, i].max()) for i in range(theta_samp.shape[1])] lower = np.array([theta_lim[0] for theta_lim in theta_lims]) upper = np.array([theta_lim[1] for theta_lim in theta_lims]) prior = Priors.Uniform(lower, upper) # create an ensemble of ndes ndata = scores_samp.shape[1] ndes = [ NDEs.ConditionalMaskedAutoregressiveFlow(n_parameters=3, n_data=ndata, n_hiddens=[50, 50], n_mades=5, act_fun=tf.tanh, index=0) ] # NDEs.MixtureDensityNetwork(n_parameters=3, n_data=ndata, n_components=1, # n_hidden=[30,30], activations=[tf.tanh, tf.tanh], index=1), # NDEs.MixtureDensityNetwork(n_parameters=3, n_data=ndata, n_components=2, # n_hidden=[30,30], activations=[tf.tanh, tf.tanh], index=2), # NDEs.MixtureDensityNetwork(n_parameters=3, n_data=ndata, n_components=3, # n_hidden=[30,30], activations=[tf.tanh, tf.tanh], index=3), # NDEs.MixtureDensityNetwork(n_parameters=3, n_data=ndata, n_components=4, # n_hidden=[30,30], activations=[tf.tanh, tf.tanh], index=4), # NDEs.MixtureDensityNetwork(n_parameters=3, n_data=ndata, n_components=5, # n_hidden=[30,30], activations=[tf.tanh, tf.tanh], index=5)] # create the delfi object DelfiEnsemble = DELFI.Delfi(scores_fid, prior, ndes, Finv=Finv, theta_fiducial=theta_fid, param_limits=[lower, upper], param_names=[r'M_\nu', '\Omega_m', 'A_s'], results_dir='./', input_normalization='fisher') print('loading simulations') DelfiEnsemble.load_simulations(scores_samp, theta_samp) DelfiEnsemble.fisher_pretraining() print('training ndes') DelfiEnsemble.train_ndes() print('sampling') posterior_samples = DelfiEnsemble.emcee_sample() pickle.dump(posterior_samples, open(os.path.join(datdir, 'posterior.%s.p' % sampling), 'wb')) return None