From c507f02168db03f7846758299a90ee07a325ae52 Mon Sep 17 00:00:00 2001 From: daneschi Date: Tue, 14 May 2024 13:10:34 -0400 Subject: [PATCH] changed lod density for printing to account for the coordinate transformation --- linfa/maf.py | 2 -- linfa/run_experiment.py | 11 ++++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/linfa/maf.py b/linfa/maf.py index 6915c7a..b098541 100644 --- a/linfa/maf.py +++ b/linfa/maf.py @@ -334,6 +334,4 @@ def inverse(self, u, y=None): def log_prob(self, x, y=None): u, sum_log_abs_det_jacobians = self.forward(x, y) - print(torch.exp(sum_log_abs_det_jacobians)) - exit() return torch.sum(self.base_dist.log_prob(u) + sum_log_abs_det_jacobians, dim=1) \ No newline at end of file diff --git a/linfa/run_experiment.py b/linfa/run_experiment.py index 36c8f77..424aefc 100644 --- a/linfa/run_experiment.py +++ b/linfa/run_experiment.py @@ -235,22 +235,23 @@ def train(self, nf, optimizer, iteration, log, sampling=True, t=1): # Save normalized domain samples np.savetxt(self.output_dir + '/' + self.name + '_samples_' + str(iteration), xkk.data.clone().cpu().numpy(), newline="\n") - + # Save samples in the original space if self.transform: xkk_samples = self.transform.forward(xkk).data.cpu().numpy() np.savetxt(self.output_dir + '/' + self.name + '_params_' + str(iteration), xkk_samples, newline="\n") + # Conpute the sample density using normalizing flow AND account for the coordinate transformation + xkk_logprob = nf.log_prob(xkk).data.cpu().numpy() - self.transform.compute_log_jacob_func(xkk).data.cpu().numpy().flatten() else: xkk_samples = xkk.data.cpu().numpy() np.savetxt(self.output_dir + '/' + self.name + '_params_' + str(iteration), xkk_samples, newline="\n") + xkk_logprob = nf.log_prob(xkk).data.cpu().numpy() # Save marginal statistics np.savetxt(self.output_dir + '/' + self.name + '_marginal_stats_' + str(iteration), np.concatenate((xkk_samples.mean(axis=0).reshape(-1,1),xkk_samples.std(axis=0).reshape(-1,1)),axis=1), newline="\n") - # Save log density at the same samples - # np.savetxt(self.output_dir + '/' + self.name + '_logdensity_' + str(iteration), self.model_logdensity(xkk).data.cpu().numpy(), newline="\n") - np.savetxt(self.output_dir + '/' + self.name + '_logdensity_' + str(iteration), nf.log_prob(xkk).data.cpu().numpy(), newline="\n") - print('Current MC integral: ',np.exp(nf.log_prob(xkk).data.cpu().numpy()).mean()) + # Save log density at the samples in the PHYSICAL SPACE (!) + np.savetxt(self.output_dir + '/' + self.name + '_logdensity_' + str(iteration), xkk_logprob, newline="\n") # Save model outputs at the samples - If a model is defined if self.transform: